Convenience function to allow a SID to be specified as a string.
[kai/samba.git] / source3 / passdb / pdb_get_set.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SAM_ACCOUNT access routines
4    Copyright (C) Jeremy Allison                 1996-2001
5    Copyright (C) Luke Kenneth Casson Leighton   1996-1998
6    Copyright (C) Gerald (Jerry) Carter          2000-2001
7    Copyright (C) Andrew Bartlett                2001-2002
8    Copyright (C) Stefan (metze) Metzmacher      2002
9       
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_PASSDB
29
30 /**
31  * @todo Redefine this to NULL, but this changes the API becouse
32  *       much of samba assumes that the pdb_get...() funtions 
33  *       return pstrings.  (ie not null-pointers).
34  *       See also pdb_fill_default_sam().
35  */
36
37 #define PDB_NOT_QUITE_NULL ""
38
39 /*********************************************************************
40  Collection of get...() functions for SAM_ACCOUNT_INFO.
41  ********************************************************************/
42
43 uint16 pdb_get_acct_ctrl (const SAM_ACCOUNT *sampass)
44 {
45         if (sampass)
46                 return (sampass->private.acct_ctrl);
47         else
48                 return (ACB_DISABLED);
49 }
50
51 time_t pdb_get_logon_time (const SAM_ACCOUNT *sampass)
52 {
53         if (sampass)
54                 return (sampass->private.logon_time);
55         else
56                 return (0);
57 }
58
59 time_t pdb_get_logoff_time (const SAM_ACCOUNT *sampass)
60 {
61         if (sampass)
62                 return (sampass->private.logoff_time);
63         else
64                 return (-1);
65 }
66
67 time_t pdb_get_kickoff_time (const SAM_ACCOUNT *sampass)
68 {
69         if (sampass)
70                 return (sampass->private.kickoff_time);
71         else
72                 return (-1);
73 }
74
75 time_t pdb_get_pass_last_set_time (const SAM_ACCOUNT *sampass)
76 {
77         if (sampass)
78                 return (sampass->private.pass_last_set_time);
79         else
80                 return (-1);
81 }
82
83 time_t pdb_get_pass_can_change_time (const SAM_ACCOUNT *sampass)
84 {
85         if (sampass)
86                 return (sampass->private.pass_can_change_time);
87         else
88                 return (-1);
89 }
90
91 time_t pdb_get_pass_must_change_time (const SAM_ACCOUNT *sampass)
92 {
93         if (sampass)
94                 return (sampass->private.pass_must_change_time);
95         else
96                 return (-1);
97 }
98
99 uint16 pdb_get_logon_divs (const SAM_ACCOUNT *sampass)
100 {
101         if (sampass)
102                 return (sampass->private.logon_divs);
103         else
104                 return (-1);
105 }
106
107 uint32 pdb_get_hours_len (const SAM_ACCOUNT *sampass)
108 {
109         if (sampass)
110                 return (sampass->private.hours_len);
111         else
112                 return (-1);
113 }
114
115 const uint8* pdb_get_hours (const SAM_ACCOUNT *sampass)
116 {
117         if (sampass)
118                 return (sampass->private.hours);
119         else
120                 return (NULL);
121 }
122
123 const uint8* pdb_get_nt_passwd (const SAM_ACCOUNT *sampass)
124 {
125         if (sampass) {
126                 SMB_ASSERT((!sampass->private.nt_pw.data) 
127                            || sampass->private.nt_pw.length == NT_HASH_LEN);
128                 return ((uint8*)sampass->private.nt_pw.data);
129         }
130         else
131                 return (NULL);
132 }
133
134 const uint8* pdb_get_lanman_passwd (const SAM_ACCOUNT *sampass)
135 {
136         if (sampass) {
137                 SMB_ASSERT((!sampass->private.lm_pw.data) 
138                            || sampass->private.lm_pw.length == LM_HASH_LEN);
139                 return ((uint8*)sampass->private.lm_pw.data);
140         }
141         else
142                 return (NULL);
143 }
144
145 /* Return the plaintext password if known.  Most of the time
146    it isn't, so don't assume anything magic about this function.
147    
148    Used to pass the plaintext to passdb backends that might 
149    want to store more than just the NTLM hashes.
150 */
151 const char* pdb_get_plaintext_passwd (const SAM_ACCOUNT *sampass)
152 {
153         if (sampass) {
154                 return ((char*)sampass->private.plaintext_pw.data);
155         }
156         else
157                 return (NULL);
158 }
159 const DOM_SID *pdb_get_user_sid(const SAM_ACCOUNT *sampass)
160 {
161         if (sampass) 
162                 return &sampass->private.user_sid;
163         else
164                 return (NULL);
165 }
166
167 const DOM_SID *pdb_get_group_sid(const SAM_ACCOUNT *sampass)
168 {
169         if (sampass)
170                 return &sampass->private.group_sid;
171         else    
172                 return (NULL);
173 }       
174
175 uint32 pdb_get_user_rid (const SAM_ACCOUNT *sampass)
176 {
177         uint32 u_rid;
178
179         if (sampass)
180                 if (sid_peek_check_rid(get_global_sam_sid(), pdb_get_user_sid(sampass),&u_rid))
181                         return u_rid;
182         
183         return (0);
184 }
185
186 uint32 pdb_get_group_rid (const SAM_ACCOUNT *sampass)
187 {
188         uint32 g_rid;
189
190         if (sampass)
191                 if (sid_peek_check_rid(get_global_sam_sid(), pdb_get_group_sid(sampass),&g_rid))
192                         return g_rid;
193         return (0);
194 }
195
196 /**
197  * Get flags showing what is initalised in the SAM_ACCOUNT
198  * @param sampass the SAM_ACCOUNT in question
199  * @return the flags indicating the members initialised in the struct.
200  **/
201  
202 uint32 pdb_get_init_flag (const SAM_ACCOUNT *sampass)
203 {
204         if (sampass)
205                 return sampass->private.init_flag;
206         else 
207                 return FLAG_SAM_UNINIT;
208 }
209
210 uid_t pdb_get_uid (const SAM_ACCOUNT *sampass)
211 {
212         if (sampass)
213                 return (sampass->private.uid);
214         else
215                 return (-1);
216 }
217
218 gid_t pdb_get_gid (const SAM_ACCOUNT *sampass)
219 {
220         if (sampass)
221                 return (sampass->private.gid);
222         else
223                 return (-1);
224 }
225
226 const char* pdb_get_username (const SAM_ACCOUNT *sampass)
227 {
228         if (sampass)
229                 return (sampass->private.username);
230         else
231                 return (NULL);
232 }
233
234 const char* pdb_get_domain (const SAM_ACCOUNT *sampass)
235 {
236         if (sampass)
237                 return (sampass->private.domain);
238         else
239                 return (NULL);
240 }
241
242 const char* pdb_get_nt_username (const SAM_ACCOUNT *sampass)
243 {
244         if (sampass)
245                 return (sampass->private.nt_username);
246         else
247                 return (NULL);
248 }
249
250 const char* pdb_get_fullname (const SAM_ACCOUNT *sampass)
251 {
252         if (sampass)
253                 return (sampass->private.full_name);
254         else
255                 return (NULL);
256 }
257
258 const char* pdb_get_homedir (const SAM_ACCOUNT *sampass)
259 {
260         if (sampass)
261                 return (sampass->private.home_dir);
262         else
263                 return (NULL);
264 }
265
266 const char* pdb_get_unix_homedir (const SAM_ACCOUNT *sampass)
267 {
268         if (sampass)
269                 return (sampass->private.unix_home_dir);
270         else
271                 return (NULL);
272 }
273
274 const char* pdb_get_dirdrive (const SAM_ACCOUNT *sampass)
275 {
276         if (sampass)
277                 return (sampass->private.dir_drive);
278         else
279                 return (NULL);
280 }
281
282 const char* pdb_get_logon_script (const SAM_ACCOUNT *sampass)
283 {
284         if (sampass)
285                 return (sampass->private.logon_script);
286         else
287                 return (NULL);
288 }
289
290 const char* pdb_get_profile_path (const SAM_ACCOUNT *sampass)
291 {
292         if (sampass)
293                 return (sampass->private.profile_path);
294         else
295                 return (NULL);
296 }
297
298 const char* pdb_get_acct_desc (const SAM_ACCOUNT *sampass)
299 {
300         if (sampass)
301                 return (sampass->private.acct_desc);
302         else
303                 return (NULL);
304 }
305
306 const char* pdb_get_workstations (const SAM_ACCOUNT *sampass)
307 {
308         if (sampass)
309                 return (sampass->private.workstations);
310         else
311                 return (NULL);
312 }
313
314 const char* pdb_get_unknown_str (const SAM_ACCOUNT *sampass)
315 {
316         if (sampass)
317                 return (sampass->private.unknown_str);
318         else
319                 return (NULL);
320 }
321
322 const char* pdb_get_munged_dial (const SAM_ACCOUNT *sampass)
323 {
324         if (sampass)
325                 return (sampass->private.munged_dial);
326         else
327                 return (NULL);
328 }
329
330 uint32 pdb_get_unknown3 (const SAM_ACCOUNT *sampass)
331 {
332         if (sampass)
333                 return (sampass->private.unknown_3);
334         else
335                 return (-1);
336 }
337
338 uint32 pdb_get_unknown5 (const SAM_ACCOUNT *sampass)
339 {
340         if (sampass)
341                 return (sampass->private.unknown_5);
342         else
343                 return (-1);
344 }
345
346 uint32 pdb_get_unknown6 (const SAM_ACCOUNT *sampass)
347 {
348         if (sampass)
349                 return (sampass->private.unknown_6);
350         else
351                 return (-1);
352 }
353
354 /*********************************************************************
355  Collection of set...() functions for SAM_ACCOUNT_INFO.
356  ********************************************************************/
357
358 BOOL pdb_set_acct_ctrl (SAM_ACCOUNT *sampass, uint16 flags)
359 {
360         if (!sampass)
361                 return False;
362                 
363         if (sampass) {
364                 sampass->private.acct_ctrl = flags;
365                 return True;
366         }
367         
368         return False;
369 }
370
371 BOOL pdb_set_logon_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
372 {
373         if (!sampass)
374                 return False;
375
376         sampass->private.logon_time = mytime;
377
378         if (store)
379                 pdb_set_init_flag(sampass, FLAG_SAM_LOGONTIME); 
380
381         return True;
382 }
383
384 BOOL pdb_set_logoff_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
385 {
386         if (!sampass)
387                 return False;
388
389         sampass->private.logoff_time = mytime;
390
391         if (store)
392                 pdb_set_init_flag(sampass, FLAG_SAM_LOGOFFTIME); 
393
394         return True;
395 }
396
397 BOOL pdb_set_kickoff_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
398 {
399         if (!sampass)
400                 return False;
401
402         sampass->private.kickoff_time = mytime;
403
404         if (store)
405                 pdb_set_init_flag(sampass, FLAG_SAM_KICKOFFTIME); 
406
407         return True;
408 }
409
410 BOOL pdb_set_pass_can_change_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
411 {
412         if (!sampass)
413                 return False;
414
415         sampass->private.pass_can_change_time = mytime;
416
417         if (store)
418                 pdb_set_init_flag(sampass, FLAG_SAM_CANCHANGETIME); 
419
420         return True;
421 }
422
423 BOOL pdb_set_pass_must_change_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
424 {
425         if (!sampass)
426                 return False;
427
428         sampass->private.pass_must_change_time = mytime;
429
430         if (store)
431                 pdb_set_init_flag(sampass, FLAG_SAM_MUSTCHANGETIME); 
432
433         return True;
434 }
435
436 BOOL pdb_set_pass_last_set_time (SAM_ACCOUNT *sampass, time_t mytime)
437 {
438         if (!sampass)
439                 return False;
440
441         sampass->private.pass_last_set_time = mytime;
442
443         return True;
444 }
445
446 BOOL pdb_set_hours_len (SAM_ACCOUNT *sampass, uint32 len)
447 {
448         if (!sampass)
449                 return False;
450
451         sampass->private.hours_len = len;
452         return True;
453 }
454
455 BOOL pdb_set_logon_divs (SAM_ACCOUNT *sampass, uint16 hours)
456 {
457         if (!sampass)
458                 return False;
459
460         sampass->private.logon_divs = hours;
461         return True;
462 }
463
464 /**
465  * Set flags showing what is initalised in the SAM_ACCOUNT
466  * @param sampass the SAM_ACCOUNT in question
467  * @param flag The *new* flag to be set.  Old flags preserved
468  *             this flag is only added.  
469  **/
470  
471 BOOL pdb_set_init_flag (SAM_ACCOUNT *sampass, uint32 flag)
472 {
473         if (!sampass)
474                 return False;
475
476         sampass->private.init_flag |= flag;
477
478         return True;
479 }
480
481 BOOL pdb_set_uid (SAM_ACCOUNT *sampass, const uid_t uid)
482 {
483         if (!sampass)
484                 return False;
485         
486         DEBUG(10, ("pdb_set_uid: setting uid %d, was %d\n", 
487                    (int)uid, (int)sampass->private.uid));
488  
489         sampass->private.uid = uid;
490         pdb_set_init_flag(sampass, FLAG_SAM_UID); 
491
492         return True;
493
494 }
495
496 BOOL pdb_set_gid (SAM_ACCOUNT *sampass, const gid_t gid)
497 {
498         if (!sampass)
499                 return False;
500                 
501         DEBUG(10, ("pdb_set_gid: setting gid %d, was %d\n", 
502                    (int)gid, (int)sampass->private.gid));
503  
504         sampass->private.gid = gid; 
505         pdb_set_init_flag(sampass, FLAG_SAM_GID); 
506
507         return True;
508
509 }
510
511 BOOL pdb_set_user_sid (SAM_ACCOUNT *sampass, DOM_SID *u_sid)
512 {
513         if (!sampass || !u_sid)
514                 return False;
515         
516         sid_copy(&sampass->private.user_sid, u_sid);
517
518         DEBUG(10, ("pdb_set_user_sid: setting user sid %s\n", 
519                     sid_string_static(&sampass->private.user_sid)));
520         
521         return True;
522 }
523
524 BOOL pdb_set_user_sid_from_string (SAM_ACCOUNT *sampass, fstring u_sid)
525 {
526         DOM_SID new_sid;
527         if (!sampass || !u_sid)
528                 return False;
529
530         DEBUG(10, ("pdb_set_user_sid_from_string: setting user sid %s\n",
531                    u_sid));
532
533         if (!string_to_sid(&new_sid, u_sid)) { 
534                 DEBUG(1, ("pdb_set_user_sid_from_string: %s isn't a valid SID!\n", u_sid));
535                 return False;
536         }
537          
538         if (!pdb_set_user_sid(sampass, &new_sid)) {
539                 DEBUG(1, ("pdb_set_user_sid_from_string: could not set sid %s on SAM_ACCOUNT!\n", u_sid));
540                 return False;
541         }
542
543         return True;
544 }
545
546 BOOL pdb_set_group_sid (SAM_ACCOUNT *sampass, DOM_SID *g_sid)
547 {
548         if (!sampass || !g_sid)
549                 return False;
550
551         sid_copy(&sampass->private.group_sid, g_sid);
552
553         DEBUG(10, ("pdb_set_group_sid: setting group sid %s\n", 
554                     sid_string_static(&sampass->private.group_sid)));
555
556         return True;
557 }
558
559 BOOL pdb_set_group_sid_from_string (SAM_ACCOUNT *sampass, fstring g_sid)
560 {
561         DOM_SID new_sid;
562         if (!sampass || !g_sid)
563                 return False;
564
565         DEBUG(10, ("pdb_set_group_sid_from_string: setting group sid %s\n",
566                    g_sid));
567
568         if (!string_to_sid(&new_sid, g_sid)) { 
569                 DEBUG(1, ("pdb_set_group_sid_from_string: %s isn't a valid SID!\n", g_sid));
570                 return False;
571         }
572          
573         if (!pdb_set_group_sid(sampass, &new_sid)) {
574                 DEBUG(1, ("pdb_set_group_sid_from_string: could not set sid %s on SAM_ACCOUNT!\n", g_sid));
575                 return False;
576         }
577         return True;
578 }
579
580 BOOL pdb_set_user_sid_from_rid (SAM_ACCOUNT *sampass, uint32 rid)
581 {
582         DOM_SID u_sid;
583         const DOM_SID *global_sam_sid;
584         
585         if (!sampass)
586                 return False;
587
588         if (!(global_sam_sid = get_global_sam_sid())) {
589                 DEBUG(1, ("pdb_set_user_sid_from_rid: Could not read global sam sid!\n"));
590                 return False;
591         }
592
593         sid_copy(&u_sid, global_sam_sid);
594
595         if (!sid_append_rid(&u_sid, rid))
596                 return False;
597
598         if (!pdb_set_user_sid(sampass, &u_sid))
599                 return False;
600
601         DEBUG(10, ("pdb_set_user_sid_from_rid:\n\tsetting user sid %s from rid %d\n", 
602                     sid_string_static(&u_sid),rid));
603
604         return True;
605 }
606
607 BOOL pdb_set_group_sid_from_rid (SAM_ACCOUNT *sampass, uint32 grid)
608 {
609         DOM_SID g_sid;
610         const DOM_SID *global_sam_sid;
611
612         if (!sampass)
613                 return False;
614         
615         if (!(global_sam_sid = get_global_sam_sid())) {
616                 DEBUG(1, ("pdb_set_user_sid_from_rid: Could not read global sam sid!\n"));
617                 return False;
618         }
619
620         sid_copy(&g_sid, global_sam_sid);
621         
622         if (!sid_append_rid(&g_sid, grid))
623                 return False;
624
625         if (!pdb_set_group_sid(sampass, &g_sid))
626                 return False;
627
628         DEBUG(10, ("pdb_set_group_sid_from_rid:\n\tsetting group sid %s from rid %d\n", 
629                     sid_string_static(&g_sid), grid));
630
631         return True;
632 }
633
634 /*********************************************************************
635  Set the user's UNIX name.
636  ********************************************************************/
637
638 BOOL pdb_set_username(SAM_ACCOUNT *sampass, const char *username)
639 {       
640         if (!sampass)
641                 return False;
642  
643         if (username) { 
644                 DEBUG(10, ("pdb_set_username: setting username %s, was %s\n", username,
645                         (sampass->private.username)?(sampass->private.username):"NULL"));
646
647                 sampass->private.username = talloc_strdup(sampass->mem_ctx, username);
648
649                 if (!sampass->private.username) {
650                         DEBUG(0, ("pdb_set_username: talloc_strdup() failed!\n"));
651                         return False;
652                 }
653
654         } else {
655                 sampass->private.username = PDB_NOT_QUITE_NULL;
656         }
657
658         return True;
659 }
660
661 /*********************************************************************
662  Set the domain name.
663  ********************************************************************/
664
665 BOOL pdb_set_domain(SAM_ACCOUNT *sampass, const char *domain)
666 {       
667         if (!sampass)
668                 return False;
669
670         if (domain) { 
671                 DEBUG(10, ("pdb_set_domain: setting domain %s, was %s\n", domain,
672                         (sampass->private.domain)?(sampass->private.domain):"NULL"));
673
674                 sampass->private.domain = talloc_strdup(sampass->mem_ctx, domain);
675
676                 if (!sampass->private.domain) {
677                         DEBUG(0, ("pdb_set_domain: talloc_strdup() failed!\n"));
678                         return False;
679                 }
680
681         } else {
682                 sampass->private.domain = PDB_NOT_QUITE_NULL;
683         }
684
685         return True;
686 }
687
688 /*********************************************************************
689  Set the user's NT name.
690  ********************************************************************/
691
692 BOOL pdb_set_nt_username(SAM_ACCOUNT *sampass, const char *nt_username)
693 {
694         if (!sampass)
695                 return False;
696
697         if (nt_username) { 
698                 DEBUG(10, ("pdb_set_nt_username: setting nt username %s, was %s\n", nt_username,
699                         (sampass->private.nt_username)?(sampass->private.nt_username):"NULL"));
700  
701                 sampass->private.nt_username = talloc_strdup(sampass->mem_ctx, nt_username);
702                 
703                 if (!sampass->private.nt_username) {
704                         DEBUG(0, ("pdb_set_nt_username: talloc_strdup() failed!\n"));
705                         return False;
706                 }
707
708         } else {
709                 sampass->private.nt_username = PDB_NOT_QUITE_NULL;
710         }
711
712         return True;
713 }
714
715 /*********************************************************************
716  Set the user's full name.
717  ********************************************************************/
718
719 BOOL pdb_set_fullname(SAM_ACCOUNT *sampass, const char *full_name)
720 {
721         if (!sampass)
722                 return False;
723
724         if (full_name) { 
725                 DEBUG(10, ("pdb_set_full_name: setting full name %s, was %s\n", full_name,
726                         (sampass->private.full_name)?(sampass->private.full_name):"NULL"));
727         
728                 sampass->private.full_name = talloc_strdup(sampass->mem_ctx, full_name);
729
730                 if (!sampass->private.full_name) {
731                         DEBUG(0, ("pdb_set_fullname: talloc_strdup() failed!\n"));
732                         return False;
733                 }
734
735         } else {
736                 sampass->private.full_name = PDB_NOT_QUITE_NULL;
737         }
738
739         return True;
740 }
741
742 /*********************************************************************
743  Set the user's logon script.
744  ********************************************************************/
745
746 BOOL pdb_set_logon_script(SAM_ACCOUNT *sampass, const char *logon_script, BOOL store)
747 {
748         if (!sampass)
749                 return False;
750
751         if (logon_script) { 
752                 DEBUG(10, ("pdb_set_logon_script: setting logon script %s, was %s\n", logon_script,
753                         (sampass->private.logon_script)?(sampass->private.logon_script):"NULL"));
754  
755                 sampass->private.logon_script = talloc_strdup(sampass->mem_ctx, logon_script);
756
757                 if (!sampass->private.logon_script) {
758                         DEBUG(0, ("pdb_set_logon_script: talloc_strdup() failed!\n"));
759                         return False;
760                 }
761
762         } else {
763                 sampass->private.logon_script = PDB_NOT_QUITE_NULL;
764         }
765         
766         if (store) {
767                 DEBUG(10, ("pdb_set_logon_script: setting logon script sam flag!\n"));
768                 pdb_set_init_flag(sampass, FLAG_SAM_LOGONSCRIPT);
769         }
770
771         return True;
772 }
773
774 /*********************************************************************
775  Set the user's profile path.
776  ********************************************************************/
777
778 BOOL pdb_set_profile_path (SAM_ACCOUNT *sampass, const char *profile_path, BOOL store)
779 {
780         if (!sampass)
781                 return False;
782
783         if (profile_path) { 
784                 DEBUG(10, ("pdb_set_profile_path: setting profile path %s, was %s\n", profile_path,
785                         (sampass->private.profile_path)?(sampass->private.profile_path):"NULL"));
786  
787                 sampass->private.profile_path = talloc_strdup(sampass->mem_ctx, profile_path);
788                 
789                 if (!sampass->private.profile_path) {
790                         DEBUG(0, ("pdb_set_profile_path: talloc_strdup() failed!\n"));
791                         return False;
792                 }
793
794         } else {
795                 sampass->private.profile_path = PDB_NOT_QUITE_NULL;
796         }
797
798         if (store) {
799                 DEBUG(10, ("pdb_set_profile_path: setting profile path sam flag!\n"));
800                 pdb_set_init_flag(sampass, FLAG_SAM_PROFILE);
801         }
802
803         return True;
804 }
805
806 /*********************************************************************
807  Set the user's directory drive.
808  ********************************************************************/
809
810 BOOL pdb_set_dir_drive (SAM_ACCOUNT *sampass, const char *dir_drive, BOOL store)
811 {
812         if (!sampass)
813                 return False;
814
815         if (dir_drive) { 
816                 DEBUG(10, ("pdb_set_dir_drive: setting dir drive %s, was %s\n", dir_drive,
817                         (sampass->private.dir_drive)?(sampass->private.dir_drive):"NULL"));
818  
819                 sampass->private.dir_drive = talloc_strdup(sampass->mem_ctx, dir_drive);
820                 
821                 if (!sampass->private.dir_drive) {
822                         DEBUG(0, ("pdb_set_dir_drive: talloc_strdup() failed!\n"));
823                         return False;
824                 }
825
826         } else {
827                 sampass->private.dir_drive = PDB_NOT_QUITE_NULL;
828         }
829         
830         if (store) {
831                 DEBUG(10, ("pdb_set_dir_drive: setting dir drive sam flag!\n"));
832                 pdb_set_init_flag(sampass, FLAG_SAM_DRIVE);
833         }
834
835         return True;
836 }
837
838 /*********************************************************************
839  Set the user's home directory.
840  ********************************************************************/
841
842 BOOL pdb_set_homedir (SAM_ACCOUNT *sampass, const char *home_dir, BOOL store)
843 {
844         if (!sampass)
845                 return False;
846
847         if (home_dir) { 
848                 DEBUG(10, ("pdb_set_homedir: setting home dir %s, was %s\n", home_dir,
849                         (sampass->private.home_dir)?(sampass->private.home_dir):"NULL"));
850  
851                 sampass->private.home_dir = talloc_strdup(sampass->mem_ctx, home_dir);
852                 
853                 if (!sampass->private.home_dir) {
854                         DEBUG(0, ("pdb_set_home_dir: talloc_strdup() failed!\n"));
855                         return False;
856                 }
857
858         } else {
859                 sampass->private.home_dir = PDB_NOT_QUITE_NULL;
860         }
861
862         if (store) {
863                 DEBUG(10, ("pdb_set_homedir: setting home dir sam flag!"));
864                 pdb_set_init_flag(sampass, FLAG_SAM_SMBHOME);
865         }
866
867         return True;
868 }
869
870 /*********************************************************************
871  Set the user's unix home directory.
872  ********************************************************************/
873
874 BOOL pdb_set_unix_homedir (SAM_ACCOUNT *sampass, const char *unix_home_dir)
875 {
876         if (!sampass)
877                 return False;
878
879         if (unix_home_dir) { 
880                 DEBUG(10, ("pdb_set_homedir: setting home dir %s, was %s\n", unix_home_dir,
881                         (sampass->private.unix_home_dir)?(sampass->private.unix_home_dir):"NULL"));
882  
883                 sampass->private.unix_home_dir = talloc_strdup(sampass->mem_ctx, 
884                                                           unix_home_dir);
885                 
886                 if (!sampass->private.unix_home_dir) {
887                         DEBUG(0, ("pdb_set_unix_home_dir: talloc_strdup() failed!\n"));
888                         return False;
889                 }
890
891         } else {
892                 sampass->private.unix_home_dir = PDB_NOT_QUITE_NULL;
893         }
894
895         return True;
896 }
897
898 /*********************************************************************
899  Set the user's account description.
900  ********************************************************************/
901
902 BOOL pdb_set_acct_desc (SAM_ACCOUNT *sampass, const char *acct_desc)
903 {
904         if (!sampass)
905                 return False;
906
907         if (acct_desc) { 
908                 sampass->private.acct_desc = talloc_strdup(sampass->mem_ctx, acct_desc);
909
910                 if (!sampass->private.acct_desc) {
911                         DEBUG(0, ("pdb_set_acct_desc: talloc_strdup() failed!\n"));
912                         return False;
913                 }
914
915         } else {
916                 sampass->private.acct_desc = PDB_NOT_QUITE_NULL;
917         }
918
919         return True;
920 }
921
922 /*********************************************************************
923  Set the user's workstation allowed list.
924  ********************************************************************/
925
926 BOOL pdb_set_workstations (SAM_ACCOUNT *sampass, const char *workstations)
927 {
928         if (!sampass)
929                 return False;
930
931         if (workstations) { 
932                 DEBUG(10, ("pdb_set_workstations: setting workstations %s, was %s\n", workstations,
933                         (sampass->private.workstations)?(sampass->private.workstations):"NULL"));
934  
935                 sampass->private.workstations = talloc_strdup(sampass->mem_ctx, workstations);
936
937                 if (!sampass->private.workstations) {
938                         DEBUG(0, ("pdb_set_workstations: talloc_strdup() failed!\n"));
939                         return False;
940                 }
941
942         } else {
943                 sampass->private.workstations = PDB_NOT_QUITE_NULL;
944         }
945
946         return True;
947 }
948
949 /*********************************************************************
950  Set the user's 'unknown_str', whatever the heck this actually is...
951  ********************************************************************/
952
953 BOOL pdb_set_unknown_str (SAM_ACCOUNT *sampass, const char *unknown_str)
954 {
955         if (!sampass)
956                 return False;
957
958         if (unknown_str) { 
959                 sampass->private.unknown_str = talloc_strdup(sampass->mem_ctx, unknown_str);
960                 
961                 if (!sampass->private.unknown_str) {
962                         DEBUG(0, ("pdb_set_unknown_str: talloc_strdup() failed!\n"));
963                         return False;
964                 }
965
966         } else {
967                 sampass->private.unknown_str = PDB_NOT_QUITE_NULL;
968         }
969
970         return True;
971 }
972
973 /*********************************************************************
974  Set the user's dial string.
975  ********************************************************************/
976
977 BOOL pdb_set_munged_dial (SAM_ACCOUNT *sampass, const char *munged_dial)
978 {
979         if (!sampass)
980                 return False;
981
982         if (munged_dial) { 
983                 sampass->private.munged_dial = talloc_strdup(sampass->mem_ctx, munged_dial);
984                 
985                 if (!sampass->private.munged_dial) {
986                         DEBUG(0, ("pdb_set_munged_dial: talloc_strdup() failed!\n"));
987                         return False;
988                 }
989
990         } else {
991                 sampass->private.munged_dial = PDB_NOT_QUITE_NULL;
992         }
993
994         return True;
995 }
996
997 /*********************************************************************
998  Set the user's NT hash.
999  ********************************************************************/
1000
1001 BOOL pdb_set_nt_passwd (SAM_ACCOUNT *sampass, const uint8 *pwd)
1002 {
1003         if (!sampass)
1004                 return False;
1005
1006         data_blob_clear_free(&sampass->private.nt_pw);
1007         
1008         sampass->private.nt_pw = data_blob(pwd, NT_HASH_LEN);
1009
1010         return True;
1011 }
1012
1013 /*********************************************************************
1014  Set the user's LM hash.
1015  ********************************************************************/
1016
1017 BOOL pdb_set_lanman_passwd (SAM_ACCOUNT *sampass, const uint8 pwd[16])
1018 {
1019         if (!sampass)
1020                 return False;
1021
1022         data_blob_clear_free(&sampass->private.lm_pw);
1023         
1024         sampass->private.lm_pw = data_blob(pwd, LM_HASH_LEN);
1025
1026         return True;
1027 }
1028
1029 /*********************************************************************
1030  Set the user's plaintext password only (base procedure, see helper
1031  below)
1032  ********************************************************************/
1033
1034 BOOL pdb_set_plaintext_pw_only (SAM_ACCOUNT *sampass, const uint8 *password, size_t len)
1035 {
1036         if (!sampass)
1037                 return False;
1038
1039         data_blob_clear_free(&sampass->private.plaintext_pw);
1040         
1041         sampass->private.plaintext_pw = data_blob(password, len);
1042
1043         return True;
1044 }
1045
1046 BOOL pdb_set_unknown_3 (SAM_ACCOUNT *sampass, uint32 unkn)
1047 {
1048         if (!sampass)
1049                 return False;
1050
1051         sampass->private.unknown_3 = unkn;
1052         return True;
1053 }
1054
1055 BOOL pdb_set_unknown_5 (SAM_ACCOUNT *sampass, uint32 unkn)
1056 {
1057         if (!sampass)
1058                 return False;
1059
1060         sampass->private.unknown_5 = unkn;
1061         return True;
1062 }
1063
1064 BOOL pdb_set_unknown_6 (SAM_ACCOUNT *sampass, uint32 unkn)
1065 {
1066         if (!sampass)
1067                 return False;
1068
1069         sampass->private.unknown_6 = unkn;
1070         return True;
1071 }
1072
1073 BOOL pdb_set_hours (SAM_ACCOUNT *sampass, const uint8 *hours)
1074 {
1075         if (!sampass)
1076                 return False;
1077
1078         if (!hours) {
1079                 memset ((char *)sampass->private.hours, 0, MAX_HOURS_LEN);
1080                 return True;
1081         }
1082         
1083         memcpy (sampass->private.hours, hours, MAX_HOURS_LEN);
1084
1085         return True;
1086 }
1087
1088
1089 /* Helpful interfaces to the above */
1090
1091 /*********************************************************************
1092  Sets the last changed times and must change times for a normal
1093  password change.
1094  ********************************************************************/
1095
1096 BOOL pdb_set_pass_changed_now (SAM_ACCOUNT *sampass)
1097 {
1098         uint32 expire;
1099
1100         if (!sampass)
1101                 return False;
1102         
1103         if (!pdb_set_pass_last_set_time (sampass, time(NULL)))
1104                 return False;
1105
1106         account_policy_get(AP_MAX_PASSWORD_AGE, &expire);
1107
1108         if (expire==(uint32)-1) {
1109                 if (!pdb_set_pass_must_change_time (sampass, get_time_t_max(), False))
1110                         return False;
1111         } else {
1112                 if (!pdb_set_pass_must_change_time (sampass, 
1113                                             pdb_get_pass_last_set_time(sampass)
1114                                             + expire, True))
1115                         return False;
1116         }
1117         
1118         return True;
1119 }
1120
1121 /*********************************************************************
1122  Set the user's PLAINTEXT password.  Used as an interface to the above.
1123  Also sets the last change time to NOW.
1124  ********************************************************************/
1125
1126 BOOL pdb_set_plaintext_passwd (SAM_ACCOUNT *sampass, const char *plaintext)
1127 {
1128         uchar new_lanman_p16[16];
1129         uchar new_nt_p16[16];
1130
1131         if (!sampass || !plaintext)
1132                 return False;
1133         
1134         nt_lm_owf_gen (plaintext, new_nt_p16, new_lanman_p16);
1135
1136         if (!pdb_set_nt_passwd (sampass, new_nt_p16)) 
1137                 return False;
1138
1139         if (!pdb_set_lanman_passwd (sampass, new_lanman_p16)) 
1140                 return False;
1141         
1142         if (!pdb_set_pass_changed_now (sampass))
1143                 return False;
1144
1145         return True;
1146 }
1147