summaryrefslogtreecommitdiff
path: root/source3/passdb/pdb_get_set.c
blob: 5cb72e7f86395c7703578f8ca6d89482cfaacff2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
/* 
   Unix SMB/CIFS implementation.
   struct samu access routines
   Copyright (C) Jeremy Allison 		1996-2001
   Copyright (C) Luke Kenneth Casson Leighton 	1996-1998
   Copyright (C) Gerald (Jerry) Carter		2000-2001
   Copyright (C) Andrew Bartlett		2001-2002
   Copyright (C) Stefan (metze) Metzmacher	2002
      
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_PASSDB

/**
 * @todo Redefine this to NULL, but this changes the API because
 *       much of samba assumes that the pdb_get...() funtions 
 *       return pstrings.  (ie not null-pointers).
 *       See also pdb_fill_default_sam().
 */

#define PDB_NOT_QUITE_NULL ""

/*********************************************************************
 Collection of get...() functions for struct samu.
 ********************************************************************/

uint32 pdb_get_acct_ctrl (const struct samu *sampass)
{
	if (sampass)
		return (sampass->acct_ctrl);
	else
		return (ACB_DISABLED);
}

time_t pdb_get_logon_time (const struct samu *sampass)
{
	if (sampass)
		return (sampass->logon_time);
	else
		return (0);
}

time_t pdb_get_logoff_time (const struct samu *sampass)
{
	if (sampass)
		return (sampass->logoff_time);
	else
		return (-1);
}

time_t pdb_get_kickoff_time (const struct samu *sampass)
{
	if (sampass)
		return (sampass->kickoff_time);
	else
		return (-1);
}

time_t pdb_get_bad_password_time (const struct samu *sampass)
{
	if (sampass)
		return (sampass->bad_password_time);
	else
		return (-1);
}

time_t pdb_get_pass_last_set_time (const struct samu *sampass)
{
	if (sampass)
		return (sampass->pass_last_set_time);
	else
		return (-1);
}

time_t pdb_get_pass_can_change_time (const struct samu *sampass)
{
	if (sampass)
		return (sampass->pass_can_change_time);
	else
		return (-1);
}

time_t pdb_get_pass_must_change_time (const struct samu *sampass)
{
	if (sampass)
		return (sampass->pass_must_change_time);
	else
		return (-1);
}

uint16 pdb_get_logon_divs (const struct samu *sampass)
{
	if (sampass)
		return (sampass->logon_divs);
	else
		return (-1);
}

uint32 pdb_get_hours_len (const struct samu *sampass)
{
	if (sampass)
		return (sampass->hours_len);
	else
		return (-1);
}

const uint8* pdb_get_hours (const struct samu *sampass)
{
	if (sampass)
		return (sampass->hours);
	else
		return (NULL);
}

const uint8* pdb_get_nt_passwd (const struct samu *sampass)
{
	if (sampass) {
		SMB_ASSERT((!sampass->nt_pw.data) 
			   || sampass->nt_pw.length == NT_HASH_LEN);
		return ((uint8*)sampass->nt_pw.data);
	}
	else
		return (NULL);
}

const uint8* pdb_get_lanman_passwd (const struct samu *sampass)
{
	if (sampass) {
		SMB_ASSERT((!sampass->lm_pw.data) 
			   || sampass->lm_pw.length == LM_HASH_LEN);
		return ((uint8*)sampass->lm_pw.data);
	}
	else
		return (NULL);
}

const uint8* pdb_get_pw_history (const struct samu *sampass, uint32 *current_hist_len)
{
	if (sampass) {
		SMB_ASSERT((!sampass->nt_pw_his.data) 
		   || ((sampass->nt_pw_his.length % PW_HISTORY_ENTRY_LEN) == 0));
		*current_hist_len = sampass->nt_pw_his.length / PW_HISTORY_ENTRY_LEN;
		return ((uint8*)sampass->nt_pw_his.data);
	} else {
		*current_hist_len = 0;
		return (NULL);
	}
}

/* Return the plaintext password if known.  Most of the time
   it isn't, so don't assume anything magic about this function.
   
   Used to pass the plaintext to passdb backends that might 
   want to store more than just the NTLM hashes.
*/
const char* pdb_get_plaintext_passwd (const struct samu *sampass)
{
	if (sampass) {
		return (sampass->plaintext_pw);
	}
	else
		return (NULL);
}
const DOM_SID *pdb_get_user_sid(const struct samu *sampass)
{
	if (sampass) 
		return &sampass->user_sid;
		
	return NULL;
}

const DOM_SID *pdb_get_group_sid(struct samu *sampass)
{
	DOM_SID *gsid;
	struct passwd *pwd;
	
	/* sanity check */
	
	if ( !sampass ) {
		return NULL;
	}
		
	/* Return the cached group SID if we have that */
	    
	if ( sampass->group_sid ) {
		return sampass->group_sid;
	}
		
	/* generate the group SID from the user's primary Unix group */
	
	if ( !(gsid  = TALLOC_P( sampass, DOM_SID )) ) {
		return NULL;
	}
	
	/* No algorithmic mapping, meaning that we have to figure out the
	   primary group SID according to group mapping and the user SID must
	   be a newly allocated one.  We rely on the user's Unix primary gid.
	   We have no choice but to fail if we can't find it. */

	if ( sampass->unix_pw )
		pwd = sampass->unix_pw;
	else
		pwd = getpwnam_alloc( sampass, pdb_get_username(sampass) );

	if ( !pwd ) {
		DEBUG(0,("pdb_get_group_sid: Failed to find Unix account for %s\n", pdb_get_username(sampass) ));
		return NULL;
	}
	
	if ( pdb_gid_to_sid(pwd->pw_gid, gsid) ) {
		enum SID_NAME_USE type = SID_NAME_UNKNOWN;
		TALLOC_CTX *mem_ctx = talloc_init("pdb_get_group_sid");
		BOOL lookup_ret;
		
		if (!mem_ctx) {
			return NULL;
		}

		/* Now check that it's actually a domain group and not something else */

		lookup_ret = lookup_sid(mem_ctx, gsid, NULL, NULL, &type);

		TALLOC_FREE( mem_ctx );

		if ( lookup_ret && (type == SID_NAME_DOM_GRP) ) {
			sampass->group_sid = gsid;
			return sampass->group_sid;
		}

		DEBUG(3, ("Primary group for user %s is a %s and not a domain group\n", 
			pwd->pw_name, sid_type_lookup(type)));
	}

	/* Just set it to the 'Domain Users' RID of 512 which will 
	   always resolve to a name */
		   
	sid_copy( gsid, get_global_sam_sid() );
	sid_append_rid( gsid, DOMAIN_GROUP_RID_USERS );
		
	sampass->group_sid = gsid;
		
	return sampass->group_sid;
}	

/**
 * Get flags showing what is initalised in the struct samu
 * @param sampass the struct samu in question
 * @return the flags indicating the members initialised in the struct.
 **/
 
enum pdb_value_state pdb_get_init_flags (const struct samu *sampass, enum pdb_elements element)
{
	enum pdb_value_state ret = PDB_DEFAULT;
	
        if (!sampass || !sampass->change_flags || !sampass->set_flags)
        	return ret;
        	
        if (bitmap_query(sampass->set_flags, element)) {
		DEBUG(11, ("element %d: SET\n", element)); 
        	ret = PDB_SET;
	}
		
        if (bitmap_query(sampass->change_flags, element)) {
		DEBUG(11, ("element %d: CHANGED\n", element)); 
        	ret = PDB_CHANGED;
	}

	if (ret == PDB_DEFAULT) {
		DEBUG(11, ("element %d: DEFAULT\n", element)); 
	}

        return ret;
}

const char* pdb_get_username (const struct samu *sampass)
{
	if (sampass)
		return (sampass->username);
	else
		return (NULL);
}

const char* pdb_get_domain (const struct samu *sampass)
{
	if (sampass)
		return (sampass->domain);
	else
		return (NULL);
}

const char* pdb_get_nt_username (const struct samu *sampass)
{
	if (sampass)
		return (sampass->nt_username);
	else
		return (NULL);
}

const char* pdb_get_fullname (const struct samu *sampass)
{
	if (sampass)
		return (sampass->full_name);
	else
		return (NULL);
}

const char* pdb_get_homedir (const struct samu *sampass)
{
	if (sampass)
		return (sampass->home_dir);
	else
		return (NULL);
}

const char* pdb_get_unix_homedir (const struct samu *sampass)
{
	if ( sampass && sampass->unix_pw )
		return ( sampass->unix_pw->pw_dir );

	return (NULL);
}

const char* pdb_get_dir_drive (const struct samu *sampass)
{
	if (sampass)
		return (sampass->dir_drive);
	else
		return (NULL);
}

const char* pdb_get_logon_script (const struct samu *sampass)
{
	if (sampass)
		return (sampass->logon_script);
	else
		return (NULL);
}

const char* pdb_get_profile_path (const struct samu *sampass)
{
	if (sampass)
		return (sampass->profile_path);
	else
		return (NULL);
}

const char* pdb_get_acct_desc (const struct samu *sampass)
{
	if (sampass)
		return (sampass->acct_desc);
	else
		return (NULL);
}

const char* pdb_get_workstations (const struct samu *sampass)
{
	if (sampass)
		return (sampass->workstations);
	else
		return (NULL);
}

const char* pdb_get_unknown_str (const struct samu *sampass)
{
	if (sampass)
		return (sampass->unknown_str);
	else
		return (NULL);
}

const char* pdb_get_munged_dial (const struct samu *sampass)
{
	if (sampass)
		return (sampass->munged_dial);
	else
		return (NULL);
}

uint16 pdb_get_bad_password_count(const struct samu *sampass)
{
	if (sampass)
		return (sampass->bad_password_count);
	else
		return 0;
}

uint16 pdb_get_logon_count(const struct samu *sampass)
{
	if (sampass)
		return (sampass->logon_count);
	else
		return 0;
}

uint32 pdb_get_unknown_6 (const struct samu *sampass)
{
	if (sampass)
		return (sampass->unknown_6);
	else
		return (-1);
}

void *pdb_get_backend_private_data (const struct samu *sampass, const struct pdb_methods *my_methods)
{
	if (sampass && my_methods == sampass->backend_private_methods)
		return sampass->backend_private_data;
	else
		return NULL;
}

/*********************************************************************
 Collection of set...() functions for struct samu.
 ********************************************************************/

BOOL pdb_set_acct_ctrl (struct samu *sampass, uint32 acct_ctrl, enum pdb_value_state flag)
{
	if (!sampass)
		return False;
		
	sampass->acct_ctrl = acct_ctrl;

	return pdb_set_init_flags(sampass, PDB_ACCTCTRL, flag);
}

BOOL pdb_set_logon_time (struct samu *sampass, time_t mytime, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	sampass->logon_time = mytime;

	return pdb_set_init_flags(sampass, PDB_LOGONTIME, flag);
}

BOOL pdb_set_logoff_time (struct samu *sampass, time_t mytime, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	sampass->logoff_time = mytime;

	return pdb_set_init_flags(sampass, PDB_LOGOFFTIME, flag);
}

BOOL pdb_set_kickoff_time (struct samu *sampass, time_t mytime, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	sampass->kickoff_time = mytime;

	return pdb_set_init_flags(sampass, PDB_KICKOFFTIME, flag);
}

BOOL pdb_set_bad_password_time (struct samu *sampass, time_t mytime, 
				enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	sampass->bad_password_time = mytime;

	return pdb_set_init_flags(sampass, PDB_BAD_PASSWORD_TIME, flag);
}

BOOL pdb_set_pass_can_change_time (struct samu *sampass, time_t mytime, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	sampass->pass_can_change_time = mytime;

	return pdb_set_init_flags(sampass, PDB_CANCHANGETIME, flag);
}

BOOL pdb_set_pass_must_change_time (struct samu *sampass, time_t mytime, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	sampass->pass_must_change_time = mytime;

	return pdb_set_init_flags(sampass, PDB_MUSTCHANGETIME, flag);
}

BOOL pdb_set_pass_last_set_time (struct samu *sampass, time_t mytime, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	sampass->pass_last_set_time = mytime;

	return pdb_set_init_flags(sampass, PDB_PASSLASTSET, flag);
}

BOOL pdb_set_hours_len (struct samu *sampass, uint32 len, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	sampass->hours_len = len;

	return pdb_set_init_flags(sampass, PDB_HOURSLEN, flag);
}

BOOL pdb_set_logon_divs (struct samu *sampass, uint16 hours, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	sampass->logon_divs = hours;

	return pdb_set_init_flags(sampass, PDB_LOGONDIVS, flag);
}

/**
 * Set flags showing what is initalised in the struct samu
 * @param sampass the struct samu in question
 * @param flag The *new* flag to be set.  Old flags preserved
 *             this flag is only added.  
 **/
 
BOOL pdb_set_init_flags (struct samu *sampass, enum pdb_elements element, enum pdb_value_state value_flag)
{
        if (!sampass || !sampass)
                return False;

        if (!sampass->set_flags) {
        	if ((sampass->set_flags = 
        		bitmap_talloc(sampass, 
        				PDB_COUNT))==NULL) {
        		DEBUG(0,("bitmap_talloc failed\n"));
        		return False;
        	}
        }
        if (!sampass->change_flags) {
        	if ((sampass->change_flags = 
        		bitmap_talloc(sampass, 
        				PDB_COUNT))==NULL) {
        		DEBUG(0,("bitmap_talloc failed\n"));
        		return False;
        	}
        }
        
        switch(value_flag) {
        	case PDB_CHANGED:
        		if (!bitmap_set(sampass->change_flags, element)) {
				DEBUG(0,("Can't set flag: %d in change_flags.\n",element));
				return False;
			}
        		if (!bitmap_set(sampass->set_flags, element)) {
				DEBUG(0,("Can't set flag: %d in set_flags.\n",element));
				return False;
			}
			DEBUG(11, ("element %d -> now CHANGED\n", element)); 
        		break;
        	case PDB_SET:
        		if (!bitmap_clear(sampass->change_flags, element)) {
				DEBUG(0,("Can't set flag: %d in change_flags.\n",element));
				return False;
			}
        		if (!bitmap_set(sampass->set_flags, element)) {
				DEBUG(0,("Can't set flag: %d in set_flags.\n",element));
				return False;
			}
			DEBUG(11, ("element %d -> now SET\n", element)); 
        		break;
        	case PDB_DEFAULT:
        	default:
        		if (!bitmap_clear(sampass->change_flags, element)) {
				DEBUG(0,("Can't set flag: %d in change_flags.\n",element));
				return False;
			}
        		if (!bitmap_clear(sampass->set_flags, element)) {
				DEBUG(0,("Can't set flag: %d in set_flags.\n",element));
				return False;
			}
			DEBUG(11, ("element %d -> now DEFAULT\n", element)); 
        		break;
	}

        return True;
}

BOOL pdb_set_user_sid (struct samu *sampass, const DOM_SID *u_sid, enum pdb_value_state flag)
{
	if (!sampass || !u_sid)
		return False;
	
	sid_copy(&sampass->user_sid, u_sid);

	DEBUG(10, ("pdb_set_user_sid: setting user sid %s\n", 
		    sid_string_static(&sampass->user_sid)));

	return pdb_set_init_flags(sampass, PDB_USERSID, flag);
}

BOOL pdb_set_user_sid_from_string (struct samu *sampass, fstring u_sid, enum pdb_value_state flag)
{
	DOM_SID new_sid;
	
	if (!sampass || !u_sid)
		return False;

	DEBUG(10, ("pdb_set_user_sid_from_string: setting user sid %s\n",
		   u_sid));

	if (!string_to_sid(&new_sid, u_sid)) { 
		DEBUG(1, ("pdb_set_user_sid_from_string: %s isn't a valid SID!\n", u_sid));
		return False;
	}
	 
	if (!pdb_set_user_sid(sampass, &new_sid, flag)) {
		DEBUG(1, ("pdb_set_user_sid_from_string: could not set sid %s on struct samu!\n", u_sid));
		return False;
	}

	return True;
}

/********************************************************************
 We never fill this in from a passdb backend but rather set is 
 based on the user's primary group membership.  However, the 
 struct samu* is overloaded and reused in domain memship code 
 as well and built from the NET_USER_INFO_3 or PAC so we 
 have to allow the explicitly setting of a group SID here.
********************************************************************/

BOOL pdb_set_group_sid (struct samu *sampass, const DOM_SID *g_sid, enum pdb_value_state flag)
{
	gid_t gid;

	if (!sampass || !g_sid)
		return False;

	if ( !(sampass->group_sid = TALLOC_P( sampass, DOM_SID )) ) {
		return False;
	}

	/* if we cannot resolve the SID to gid, then just ignore it and 
	   store DOMAIN_USERS as the primary groupSID */

	if ( sid_to_gid( g_sid, &gid ) ) {
		sid_copy(sampass->group_sid, g_sid);
	} else {
		sid_copy( sampass->group_sid, get_global_sam_sid() );
		sid_append_rid( sampass->group_sid, DOMAIN_GROUP_RID_USERS );
	}

	DEBUG(10, ("pdb_set_group_sid: setting group sid %s\n", 
		sid_string_static(sampass->group_sid)));

	return pdb_set_init_flags(sampass, PDB_GROUPSID, flag);
}

/*********************************************************************
 Set the user's UNIX name.
 ********************************************************************/

BOOL pdb_set_username(struct samu *sampass, const char *username, enum pdb_value_state flag)
{
	if (!sampass)
		return False;
 
	if (username) { 
		DEBUG(10, ("pdb_set_username: setting username %s, was %s\n", username,
			(sampass->username)?(sampass->username):"NULL"));

		sampass->username = talloc_strdup(sampass, username);

		if (!sampass->username) {
			DEBUG(0, ("pdb_set_username: talloc_strdup() failed!\n"));
			return False;
		}

	} else {
		sampass->username = PDB_NOT_QUITE_NULL;
	}
	
	return pdb_set_init_flags(sampass, PDB_USERNAME, flag);
}

/*********************************************************************
 Set the domain name.
 ********************************************************************/

BOOL pdb_set_domain(struct samu *sampass, const char *domain, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	if (domain) { 
		DEBUG(10, ("pdb_set_domain: setting domain %s, was %s\n", domain,
			(sampass->domain)?(sampass->domain):"NULL"));

		sampass->domain = talloc_strdup(sampass, domain);

		if (!sampass->domain) {
			DEBUG(0, ("pdb_set_domain: talloc_strdup() failed!\n"));
			return False;
		}

	} else {
		sampass->domain = PDB_NOT_QUITE_NULL;
	}

	return pdb_set_init_flags(sampass, PDB_DOMAIN, flag);
}

/*********************************************************************
 Set the user's NT name.
 ********************************************************************/

BOOL pdb_set_nt_username(struct samu *sampass, const char *nt_username, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	if (nt_username) { 
		DEBUG(10, ("pdb_set_nt_username: setting nt username %s, was %s\n", nt_username,
			(sampass->nt_username)?(sampass->nt_username):"NULL"));
 
		sampass->nt_username = talloc_strdup(sampass, nt_username);
		
		if (!sampass->nt_username) {
			DEBUG(0, ("pdb_set_nt_username: talloc_strdup() failed!\n"));
			return False;
		}

	} else {
		sampass->nt_username = PDB_NOT_QUITE_NULL;
	}

	return pdb_set_init_flags(sampass, PDB_NTUSERNAME, flag);
}

/*********************************************************************
 Set the user's full name.
 ********************************************************************/

BOOL pdb_set_fullname(struct samu *sampass, const char *full_name, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	if (full_name) { 
		DEBUG(10, ("pdb_set_full_name: setting full name %s, was %s\n", full_name,
			(sampass->full_name)?(sampass->full_name):"NULL"));
	
		sampass->full_name = talloc_strdup(sampass, full_name);

		if (!sampass->full_name) {
			DEBUG(0, ("pdb_set_fullname: talloc_strdup() failed!\n"));
			return False;
		}

	} else {
		sampass->full_name = PDB_NOT_QUITE_NULL;
	}

	return pdb_set_init_flags(sampass, PDB_FULLNAME, flag);
}

/*********************************************************************
 Set the user's logon script.
 ********************************************************************/

BOOL pdb_set_logon_script(struct samu *sampass, const char *logon_script, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	if (logon_script) { 
		DEBUG(10, ("pdb_set_logon_script: setting logon script %s, was %s\n", logon_script,
			(sampass->logon_script)?(sampass->logon_script):"NULL"));
 
		sampass->logon_script = talloc_strdup(sampass, logon_script);

		if (!sampass->logon_script) {
			DEBUG(0, ("pdb_set_logon_script: talloc_strdup() failed!\n"));
			return False;
		}

	} else {
		sampass->logon_script = PDB_NOT_QUITE_NULL;
	}
	
	return pdb_set_init_flags(sampass, PDB_LOGONSCRIPT, flag);
}

/*********************************************************************
 Set the user's profile path.
 ********************************************************************/

BOOL pdb_set_profile_path (struct samu *sampass, const char *profile_path, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	if (profile_path) { 
		DEBUG(10, ("pdb_set_profile_path: setting profile path %s, was %s\n", profile_path,
			(sampass->profile_path)?(sampass->profile_path):"NULL"));
 
		sampass->profile_path = talloc_strdup(sampass, profile_path);
		
		if (!sampass->profile_path) {
			DEBUG(0, ("pdb_set_profile_path: talloc_strdup() failed!\n"));
			return False;
		}

	} else {
		sampass->profile_path = PDB_NOT_QUITE_NULL;
	}

	return pdb_set_init_flags(sampass, PDB_PROFILE, flag);
}

/*********************************************************************
 Set the user's directory drive.
 ********************************************************************/

BOOL pdb_set_dir_drive (struct samu *sampass, const char *dir_drive, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	if (dir_drive) { 
		DEBUG(10, ("pdb_set_dir_drive: setting dir drive %s, was %s\n", dir_drive,
			(sampass->dir_drive)?(sampass->dir_drive):"NULL"));
 
		sampass->dir_drive = talloc_strdup(sampass, dir_drive);
		
		if (!sampass->dir_drive) {
			DEBUG(0, ("pdb_set_dir_drive: talloc_strdup() failed!\n"));
			return False;
		}

	} else {
		sampass->dir_drive = PDB_NOT_QUITE_NULL;
	}
	
	return pdb_set_init_flags(sampass, PDB_DRIVE, flag);
}

/*********************************************************************
 Set the user's home directory.
 ********************************************************************/

BOOL pdb_set_homedir (struct samu *sampass, const char *home_dir, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	if (home_dir) { 
		DEBUG(10, ("pdb_set_homedir: setting home dir %s, was %s\n", home_dir,
			(sampass->home_dir)?(sampass->home_dir):"NULL"));
 
		sampass->home_dir = talloc_strdup(sampass, home_dir);
		
		if (!sampass->home_dir) {
			DEBUG(0, ("pdb_set_home_dir: talloc_strdup() failed!\n"));
			return False;
		}

	} else {
		sampass->home_dir = PDB_NOT_QUITE_NULL;
	}

	return pdb_set_init_flags(sampass, PDB_SMBHOME, flag);
}

/*********************************************************************
 Set the user's account description.
 ********************************************************************/

BOOL pdb_set_acct_desc (struct samu *sampass, const char *acct_desc, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	if (acct_desc) { 
		sampass->acct_desc = talloc_strdup(sampass, acct_desc);

		if (!sampass->acct_desc) {
			DEBUG(0, ("pdb_set_acct_desc: talloc_strdup() failed!\n"));
			return False;
		}

	} else {
		sampass->acct_desc = PDB_NOT_QUITE_NULL;
	}

	return pdb_set_init_flags(sampass, PDB_ACCTDESC, flag);
}

/*********************************************************************
 Set the user's workstation allowed list.
 ********************************************************************/

BOOL pdb_set_workstations (struct samu *sampass, const char *workstations, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	if (workstations) { 
		DEBUG(10, ("pdb_set_workstations: setting workstations %s, was %s\n", workstations,
			(sampass->workstations)?(sampass->workstations):"NULL"));
 
		sampass->workstations = talloc_strdup(sampass, workstations);

		if (!sampass->workstations) {
			DEBUG(0, ("pdb_set_workstations: talloc_strdup() failed!\n"));
			return False;
		}

	} else {
		sampass->workstations = PDB_NOT_QUITE_NULL;
	}

	return pdb_set_init_flags(sampass, PDB_WORKSTATIONS, flag);
}

/*********************************************************************
 Set the user's 'unknown_str', whatever the heck this actually is...
 ********************************************************************/

BOOL pdb_set_unknown_str (struct samu *sampass, const char *unknown_str, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	if (unknown_str) { 
		sampass->unknown_str = talloc_strdup(sampass, unknown_str);
		
		if (!sampass->unknown_str) {
			DEBUG(0, ("pdb_set_unknown_str: talloc_strdup() failed!\n"));
			return False;
		}

	} else {
		sampass->unknown_str = PDB_NOT_QUITE_NULL;
	}

	return pdb_set_init_flags(sampass, PDB_UNKNOWNSTR, flag);
}

/*********************************************************************
 Set the user's dial string.
 ********************************************************************/

BOOL pdb_set_munged_dial (struct samu *sampass, const char *munged_dial, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	if (munged_dial) { 
		sampass->munged_dial = talloc_strdup(sampass, munged_dial);
		
		if (!sampass->munged_dial) {
			DEBUG(0, ("pdb_set_munged_dial: talloc_strdup() failed!\n"));
			return False;
		}

	} else {
		sampass->munged_dial = PDB_NOT_QUITE_NULL;
	}

	return pdb_set_init_flags(sampass, PDB_MUNGEDDIAL, flag);
}

/*********************************************************************
 Set the user's NT hash.
 ********************************************************************/

BOOL pdb_set_nt_passwd (struct samu *sampass, const uint8 pwd[NT_HASH_LEN], enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	data_blob_clear_free(&sampass->nt_pw);
	
       if (pwd) {
               sampass->nt_pw =
		       data_blob_talloc(sampass, pwd, NT_HASH_LEN);
       } else {
               sampass->nt_pw = data_blob(NULL, 0);
       }

	return pdb_set_init_flags(sampass, PDB_NTPASSWD, flag);
}

/*********************************************************************
 Set the user's LM hash.
 ********************************************************************/

BOOL pdb_set_lanman_passwd (struct samu *sampass, const uint8 pwd[LM_HASH_LEN], enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	data_blob_clear_free(&sampass->lm_pw);
	
       if (pwd) {
               sampass->lm_pw =
		       data_blob_talloc(sampass, pwd, LM_HASH_LEN);
       } else {
               sampass->lm_pw = data_blob(NULL, 0);
       }

	return pdb_set_init_flags(sampass, PDB_LMPASSWD, flag);
}

/*********************************************************************
 Set the user's password history hash. historyLen is the number of 
 PW_HISTORY_SALT_LEN+SALTED_MD5_HASH_LEN length
 entries to store in the history - this must match the size of the uint8 array
 in pwd.
********************************************************************/

BOOL pdb_set_pw_history (struct samu *sampass, const uint8 *pwd, uint32 historyLen, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	if (historyLen && pwd){
		sampass->nt_pw_his = data_blob_talloc(sampass,
						pwd, historyLen*PW_HISTORY_ENTRY_LEN);
		if (!sampass->nt_pw_his.length) {
			DEBUG(0, ("pdb_set_pw_history: data_blob_talloc() failed!\n"));
			return False;
		}
	} else {
		sampass->nt_pw_his = data_blob_talloc(sampass, NULL, 0);
	}

	return pdb_set_init_flags(sampass, PDB_PWHISTORY, flag);
}

/*********************************************************************
 Set the user's plaintext password only (base procedure, see helper
 below)
 ********************************************************************/

BOOL pdb_set_plaintext_pw_only (struct samu *sampass, const char *password, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	if (password) { 
		if (sampass->plaintext_pw!=NULL) 
			memset(sampass->plaintext_pw,'\0',strlen(sampass->plaintext_pw)+1);

		sampass->plaintext_pw = talloc_strdup(sampass, password);
		
		if (!sampass->plaintext_pw) {
			DEBUG(0, ("pdb_set_unknown_str: talloc_strdup() failed!\n"));
			return False;
		}

	} else {
		sampass->plaintext_pw = NULL;
	}

	return pdb_set_init_flags(sampass, PDB_PLAINTEXT_PW, flag);
}

BOOL pdb_set_bad_password_count(struct samu *sampass, uint16 bad_password_count, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	sampass->bad_password_count = bad_password_count;

	return pdb_set_init_flags(sampass, PDB_BAD_PASSWORD_COUNT, flag);
}

BOOL pdb_set_logon_count(struct samu *sampass, uint16 logon_count, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	sampass->logon_count = logon_count;

	return pdb_set_init_flags(sampass, PDB_LOGON_COUNT, flag);
}

BOOL pdb_set_unknown_6 (struct samu *sampass, uint32 unkn, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	sampass->unknown_6 = unkn;

	return pdb_set_init_flags(sampass, PDB_UNKNOWN6, flag);
}

BOOL pdb_set_hours (struct samu *sampass, const uint8 *hours, enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	if (!hours) {
		memset ((char *)sampass->hours, 0, MAX_HOURS_LEN);
		return True;
	}
	
	memcpy (sampass->hours, hours, MAX_HOURS_LEN);

	return pdb_set_init_flags(sampass, PDB_HOURS, flag);
}

BOOL pdb_set_backend_private_data (struct samu *sampass, void *private_data, 
				   void (*free_fn)(void **), 
				   const struct pdb_methods *my_methods, 
				   enum pdb_value_state flag)
{
	if (!sampass)
		return False;

	if (sampass->backend_private_data &&
	    sampass->backend_private_data_free_fn) {
		sampass->backend_private_data_free_fn(
			&sampass->backend_private_data);
	}

	sampass->backend_private_data = private_data;
	sampass->backend_private_data_free_fn = free_fn;
	sampass->backend_private_methods = my_methods;

	return pdb_set_init_flags(sampass, PDB_BACKEND_PRIVATE_DATA, flag);
}


/* Helpful interfaces to the above */

/*********************************************************************
 Sets the last changed times and must change times for a normal
 password change.
 ********************************************************************/

BOOL pdb_set_pass_changed_now (struct samu *sampass)
{
	uint32 expire;
	uint32 min_age;

	if (!sampass)
		return False;
	
	if (!pdb_set_pass_last_set_time (sampass, time(NULL), PDB_CHANGED))
		return False;

	if (!pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &expire) 
	    || (expire==(uint32)-1) || (expire == 0)) {
		if (!pdb_set_pass_must_change_time (sampass, get_time_t_max(), PDB_CHANGED))
			return False;
	} else {
		if (!pdb_set_pass_must_change_time (sampass, 
						    pdb_get_pass_last_set_time(sampass)
						    + expire, PDB_CHANGED))
			return False;
	}
	
	if (!pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &min_age) 
	    || (min_age==(uint32)-1)) {
		if (!pdb_set_pass_can_change_time (sampass, 0, PDB_CHANGED))
			return False;
	} else {
		if (!pdb_set_pass_can_change_time (sampass, 
						    pdb_get_pass_last_set_time(sampass)
						    + min_age, PDB_CHANGED))
			return False;
	}
	return True;
}

/*********************************************************************
 Set the user's PLAINTEXT password.  Used as an interface to the above.
 Also sets the last change time to NOW.
 ********************************************************************/

BOOL pdb_set_plaintext_passwd (struct samu *sampass, const char *plaintext)
{
	uchar new_lanman_p16[LM_HASH_LEN];
	uchar new_nt_p16[NT_HASH_LEN];

	if (!sampass || !plaintext)
		return False;

	/* Calculate the MD4 hash (NT compatible) of the password */
	E_md4hash(plaintext, new_nt_p16);

	if (!pdb_set_nt_passwd (sampass, new_nt_p16, PDB_CHANGED)) 
		return False;

	if (!E_deshash(plaintext, new_lanman_p16)) {
		/* E_deshash returns false for 'long' passwords (> 14
		   DOS chars).  This allows us to match Win2k, which
		   does not store a LM hash for these passwords (which
		   would reduce the effective password length to 14 */

		if (!pdb_set_lanman_passwd (sampass, NULL, PDB_CHANGED)) 
			return False;
	} else {
		if (!pdb_set_lanman_passwd (sampass, new_lanman_p16, PDB_CHANGED)) 
			return False;
	}

	if (!pdb_set_plaintext_pw_only (sampass, plaintext, PDB_CHANGED)) 
		return False;

	if (!pdb_set_pass_changed_now (sampass))
		return False;

	/* Store the password history. */
	if (pdb_get_acct_ctrl(sampass) & ACB_NORMAL) {
		uchar *pwhistory;
		uint32 pwHistLen;
		pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
		if (pwHistLen != 0){
			uint32 current_history_len;
			/* We need to make sure we don't have a race condition here - the
			   account policy history length can change between when the pw_history
			   was first loaded into the struct samu struct and now.... JRA. */
			pwhistory = (uchar *)pdb_get_pw_history(sampass, &current_history_len);

			if (current_history_len != pwHistLen) {
				/* After closing and reopening struct samu the history
					values will sync up. We can't do this here. */

				/* current_history_len > pwHistLen is not a problem - we
					have more history than we need. */

				if (current_history_len < pwHistLen) {
					/* Ensure we have space for the needed history. */
					uchar *new_history = TALLOC(sampass,
								pwHistLen*PW_HISTORY_ENTRY_LEN);
					if (!new_history) {
						return False;
					}

					/* And copy it into the new buffer. */
					if (current_history_len) {
						memcpy(new_history, pwhistory,
							current_history_len*PW_HISTORY_ENTRY_LEN);
					}
					/* Clearing out any extra space. */
					memset(&new_history[current_history_len*PW_HISTORY_ENTRY_LEN],
						'\0', (pwHistLen-current_history_len)*PW_HISTORY_ENTRY_LEN);
					/* Finally replace it. */
					pwhistory = new_history;
				}
			}
			if (pwhistory && pwHistLen){
				/* Make room for the new password in the history list. */
				if (pwHistLen > 1) {
					memmove(&pwhistory[PW_HISTORY_ENTRY_LEN],
						pwhistory, (pwHistLen -1)*PW_HISTORY_ENTRY_LEN );
				}
				/* Create the new salt as the first part of the history entry. */
				generate_random_buffer(pwhistory, PW_HISTORY_SALT_LEN);

				/* Generate the md5 hash of the salt+new password as the second
					part of the history entry. */

				E_md5hash(pwhistory, new_nt_p16, &pwhistory[PW_HISTORY_SALT_LEN]);
				pdb_set_pw_history(sampass, pwhistory, pwHistLen, PDB_CHANGED);
			} else {
				DEBUG (10,("pdb_get_set.c: pdb_set_plaintext_passwd: pwhistory was NULL!\n"));
			}
		} else {
			/* Set the history length to zero. */
			pdb_set_pw_history(sampass, NULL, 0, PDB_CHANGED);
		}
	}

	return True;
}

/* check for any PDB_SET/CHANGED field and fill the appropriate mask bit */
uint32 pdb_build_fields_present (struct samu *sampass)
{
	/* value set to all for testing */
	return 0x00ffffff;
}