sync'ing up for 3.0alpha20 release
[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 (sampass->private.plaintext_pw);
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 /**
176  * Get flags showing what is initalised in the SAM_ACCOUNT
177  * @param sampass the SAM_ACCOUNT in question
178  * @return the flags indicating the members initialised in the struct.
179  **/
180  
181 uint32 pdb_get_init_flag (const SAM_ACCOUNT *sampass)
182 {
183         if (sampass)
184                 return sampass->private.init_flag;
185         else 
186                 return FLAG_SAM_UNINIT;
187 }
188
189 uid_t pdb_get_uid (const SAM_ACCOUNT *sampass)
190 {
191         if (sampass)
192                 return (sampass->private.uid);
193         else
194                 return (-1);
195 }
196
197 gid_t pdb_get_gid (const SAM_ACCOUNT *sampass)
198 {
199         if (sampass)
200                 return (sampass->private.gid);
201         else
202                 return (-1);
203 }
204
205 const char* pdb_get_username (const SAM_ACCOUNT *sampass)
206 {
207         if (sampass)
208                 return (sampass->private.username);
209         else
210                 return (NULL);
211 }
212
213 const char* pdb_get_domain (const SAM_ACCOUNT *sampass)
214 {
215         if (sampass)
216                 return (sampass->private.domain);
217         else
218                 return (NULL);
219 }
220
221 const char* pdb_get_nt_username (const SAM_ACCOUNT *sampass)
222 {
223         if (sampass)
224                 return (sampass->private.nt_username);
225         else
226                 return (NULL);
227 }
228
229 const char* pdb_get_fullname (const SAM_ACCOUNT *sampass)
230 {
231         if (sampass)
232                 return (sampass->private.full_name);
233         else
234                 return (NULL);
235 }
236
237 const char* pdb_get_homedir (const SAM_ACCOUNT *sampass)
238 {
239         if (sampass)
240                 return (sampass->private.home_dir);
241         else
242                 return (NULL);
243 }
244
245 const char* pdb_get_unix_homedir (const SAM_ACCOUNT *sampass)
246 {
247         if (sampass)
248                 return (sampass->private.unix_home_dir);
249         else
250                 return (NULL);
251 }
252
253 const char* pdb_get_dir_drive (const SAM_ACCOUNT *sampass)
254 {
255         if (sampass)
256                 return (sampass->private.dir_drive);
257         else
258                 return (NULL);
259 }
260
261 const char* pdb_get_logon_script (const SAM_ACCOUNT *sampass)
262 {
263         if (sampass)
264                 return (sampass->private.logon_script);
265         else
266                 return (NULL);
267 }
268
269 const char* pdb_get_profile_path (const SAM_ACCOUNT *sampass)
270 {
271         if (sampass)
272                 return (sampass->private.profile_path);
273         else
274                 return (NULL);
275 }
276
277 const char* pdb_get_acct_desc (const SAM_ACCOUNT *sampass)
278 {
279         if (sampass)
280                 return (sampass->private.acct_desc);
281         else
282                 return (NULL);
283 }
284
285 const char* pdb_get_workstations (const SAM_ACCOUNT *sampass)
286 {
287         if (sampass)
288                 return (sampass->private.workstations);
289         else
290                 return (NULL);
291 }
292
293 const char* pdb_get_unknown_str (const SAM_ACCOUNT *sampass)
294 {
295         if (sampass)
296                 return (sampass->private.unknown_str);
297         else
298                 return (NULL);
299 }
300
301 const char* pdb_get_munged_dial (const SAM_ACCOUNT *sampass)
302 {
303         if (sampass)
304                 return (sampass->private.munged_dial);
305         else
306                 return (NULL);
307 }
308
309 uint32 pdb_get_unknown3 (const SAM_ACCOUNT *sampass)
310 {
311         if (sampass)
312                 return (sampass->private.unknown_3);
313         else
314                 return (-1);
315 }
316
317 uint32 pdb_get_unknown5 (const SAM_ACCOUNT *sampass)
318 {
319         if (sampass)
320                 return (sampass->private.unknown_5);
321         else
322                 return (-1);
323 }
324
325 uint32 pdb_get_unknown6 (const SAM_ACCOUNT *sampass)
326 {
327         if (sampass)
328                 return (sampass->private.unknown_6);
329         else
330                 return (-1);
331 }
332
333 /*********************************************************************
334  Collection of set...() functions for SAM_ACCOUNT_INFO.
335  ********************************************************************/
336
337 BOOL pdb_set_acct_ctrl (SAM_ACCOUNT *sampass, uint16 flags)
338 {
339         if (!sampass)
340                 return False;
341                 
342         if (sampass) {
343                 sampass->private.acct_ctrl = flags;
344                 return True;
345         }
346         
347         return False;
348 }
349
350 BOOL pdb_set_logon_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
351 {
352         if (!sampass)
353                 return False;
354
355         sampass->private.logon_time = mytime;
356
357         if (store)
358                 pdb_set_init_flag(sampass, FLAG_SAM_LOGONTIME); 
359
360         return True;
361 }
362
363 BOOL pdb_set_logoff_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
364 {
365         if (!sampass)
366                 return False;
367
368         sampass->private.logoff_time = mytime;
369
370         if (store)
371                 pdb_set_init_flag(sampass, FLAG_SAM_LOGOFFTIME); 
372
373         return True;
374 }
375
376 BOOL pdb_set_kickoff_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
377 {
378         if (!sampass)
379                 return False;
380
381         sampass->private.kickoff_time = mytime;
382
383         if (store)
384                 pdb_set_init_flag(sampass, FLAG_SAM_KICKOFFTIME); 
385
386         return True;
387 }
388
389 BOOL pdb_set_pass_can_change_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
390 {
391         if (!sampass)
392                 return False;
393
394         sampass->private.pass_can_change_time = mytime;
395
396         if (store)
397                 pdb_set_init_flag(sampass, FLAG_SAM_CANCHANGETIME); 
398
399         return True;
400 }
401
402 BOOL pdb_set_pass_must_change_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
403 {
404         if (!sampass)
405                 return False;
406
407         sampass->private.pass_must_change_time = mytime;
408
409         if (store)
410                 pdb_set_init_flag(sampass, FLAG_SAM_MUSTCHANGETIME); 
411
412         return True;
413 }
414
415 BOOL pdb_set_pass_last_set_time (SAM_ACCOUNT *sampass, time_t mytime)
416 {
417         if (!sampass)
418                 return False;
419
420         sampass->private.pass_last_set_time = mytime;
421
422         return True;
423 }
424
425 BOOL pdb_set_hours_len (SAM_ACCOUNT *sampass, uint32 len)
426 {
427         if (!sampass)
428                 return False;
429
430         sampass->private.hours_len = len;
431         return True;
432 }
433
434 BOOL pdb_set_logon_divs (SAM_ACCOUNT *sampass, uint16 hours)
435 {
436         if (!sampass)
437                 return False;
438
439         sampass->private.logon_divs = hours;
440         return True;
441 }
442
443 /**
444  * Set flags showing what is initalised in the SAM_ACCOUNT
445  * @param sampass the SAM_ACCOUNT in question
446  * @param flag The *new* flag to be set.  Old flags preserved
447  *             this flag is only added.  
448  **/
449  
450 BOOL pdb_set_init_flag (SAM_ACCOUNT *sampass, uint32 flag)
451 {
452         if (!sampass)
453                 return False;
454
455         sampass->private.init_flag |= flag;
456
457         return True;
458 }
459
460 BOOL pdb_set_uid (SAM_ACCOUNT *sampass, const uid_t uid)
461 {
462         if (!sampass)
463                 return False;
464         
465         DEBUG(10, ("pdb_set_uid: setting uid %d, was %d\n", 
466                    (int)uid, (int)sampass->private.uid));
467  
468         sampass->private.uid = uid;
469         pdb_set_init_flag(sampass, FLAG_SAM_UID); 
470
471         return True;
472
473 }
474
475 BOOL pdb_set_gid (SAM_ACCOUNT *sampass, const gid_t gid)
476 {
477         if (!sampass)
478                 return False;
479                 
480         DEBUG(10, ("pdb_set_gid: setting gid %d, was %d\n", 
481                    (int)gid, (int)sampass->private.gid));
482  
483         sampass->private.gid = gid; 
484         pdb_set_init_flag(sampass, FLAG_SAM_GID); 
485
486         return True;
487
488 }
489
490 BOOL pdb_set_user_sid (SAM_ACCOUNT *sampass, DOM_SID *u_sid)
491 {
492         if (!sampass || !u_sid)
493                 return False;
494         
495         sid_copy(&sampass->private.user_sid, u_sid);
496
497         DEBUG(10, ("pdb_set_user_sid: setting user sid %s\n", 
498                     sid_string_static(&sampass->private.user_sid)));
499         
500         return True;
501 }
502
503 BOOL pdb_set_user_sid_from_string (SAM_ACCOUNT *sampass, fstring u_sid)
504 {
505         DOM_SID new_sid;
506         if (!sampass || !u_sid)
507                 return False;
508
509         DEBUG(10, ("pdb_set_user_sid_from_string: setting user sid %s\n",
510                    u_sid));
511
512         if (!string_to_sid(&new_sid, u_sid)) { 
513                 DEBUG(1, ("pdb_set_user_sid_from_string: %s isn't a valid SID!\n", u_sid));
514                 return False;
515         }
516          
517         if (!pdb_set_user_sid(sampass, &new_sid)) {
518                 DEBUG(1, ("pdb_set_user_sid_from_string: could not set sid %s on SAM_ACCOUNT!\n", u_sid));
519                 return False;
520         }
521
522         return True;
523 }
524
525 BOOL pdb_set_group_sid (SAM_ACCOUNT *sampass, DOM_SID *g_sid)
526 {
527         if (!sampass || !g_sid)
528                 return False;
529
530         sid_copy(&sampass->private.group_sid, g_sid);
531
532         DEBUG(10, ("pdb_set_group_sid: setting group sid %s\n", 
533                     sid_string_static(&sampass->private.group_sid)));
534
535         return True;
536 }
537
538 BOOL pdb_set_group_sid_from_string (SAM_ACCOUNT *sampass, fstring g_sid)
539 {
540         DOM_SID new_sid;
541         if (!sampass || !g_sid)
542                 return False;
543
544         DEBUG(10, ("pdb_set_group_sid_from_string: setting group sid %s\n",
545                    g_sid));
546
547         if (!string_to_sid(&new_sid, g_sid)) { 
548                 DEBUG(1, ("pdb_set_group_sid_from_string: %s isn't a valid SID!\n", g_sid));
549                 return False;
550         }
551          
552         if (!pdb_set_group_sid(sampass, &new_sid)) {
553                 DEBUG(1, ("pdb_set_group_sid_from_string: could not set sid %s on SAM_ACCOUNT!\n", g_sid));
554                 return False;
555         }
556         return True;
557 }
558
559 /*********************************************************************
560  Set the user's UNIX name.
561  ********************************************************************/
562
563 BOOL pdb_set_username(SAM_ACCOUNT *sampass, const char *username)
564 {       
565         if (!sampass)
566                 return False;
567  
568         if (username) { 
569                 DEBUG(10, ("pdb_set_username: setting username %s, was %s\n", username,
570                         (sampass->private.username)?(sampass->private.username):"NULL"));
571
572                 sampass->private.username = talloc_strdup(sampass->mem_ctx, username);
573
574                 if (!sampass->private.username) {
575                         DEBUG(0, ("pdb_set_username: talloc_strdup() failed!\n"));
576                         return False;
577                 }
578
579         } else {
580                 sampass->private.username = PDB_NOT_QUITE_NULL;
581         }
582
583         return True;
584 }
585
586 /*********************************************************************
587  Set the domain name.
588  ********************************************************************/
589
590 BOOL pdb_set_domain(SAM_ACCOUNT *sampass, const char *domain)
591 {       
592         if (!sampass)
593                 return False;
594
595         if (domain) { 
596                 DEBUG(10, ("pdb_set_domain: setting domain %s, was %s\n", domain,
597                         (sampass->private.domain)?(sampass->private.domain):"NULL"));
598
599                 sampass->private.domain = talloc_strdup(sampass->mem_ctx, domain);
600
601                 if (!sampass->private.domain) {
602                         DEBUG(0, ("pdb_set_domain: talloc_strdup() failed!\n"));
603                         return False;
604                 }
605
606         } else {
607                 sampass->private.domain = PDB_NOT_QUITE_NULL;
608         }
609
610         return True;
611 }
612
613 /*********************************************************************
614  Set the user's NT name.
615  ********************************************************************/
616
617 BOOL pdb_set_nt_username(SAM_ACCOUNT *sampass, const char *nt_username)
618 {
619         if (!sampass)
620                 return False;
621
622         if (nt_username) { 
623                 DEBUG(10, ("pdb_set_nt_username: setting nt username %s, was %s\n", nt_username,
624                         (sampass->private.nt_username)?(sampass->private.nt_username):"NULL"));
625  
626                 sampass->private.nt_username = talloc_strdup(sampass->mem_ctx, nt_username);
627                 
628                 if (!sampass->private.nt_username) {
629                         DEBUG(0, ("pdb_set_nt_username: talloc_strdup() failed!\n"));
630                         return False;
631                 }
632
633         } else {
634                 sampass->private.nt_username = PDB_NOT_QUITE_NULL;
635         }
636
637         return True;
638 }
639
640 /*********************************************************************
641  Set the user's full name.
642  ********************************************************************/
643
644 BOOL pdb_set_fullname(SAM_ACCOUNT *sampass, const char *full_name)
645 {
646         if (!sampass)
647                 return False;
648
649         if (full_name) { 
650                 DEBUG(10, ("pdb_set_full_name: setting full name %s, was %s\n", full_name,
651                         (sampass->private.full_name)?(sampass->private.full_name):"NULL"));
652         
653                 sampass->private.full_name = talloc_strdup(sampass->mem_ctx, full_name);
654
655                 if (!sampass->private.full_name) {
656                         DEBUG(0, ("pdb_set_fullname: talloc_strdup() failed!\n"));
657                         return False;
658                 }
659
660         } else {
661                 sampass->private.full_name = PDB_NOT_QUITE_NULL;
662         }
663
664         return True;
665 }
666
667 /*********************************************************************
668  Set the user's logon script.
669  ********************************************************************/
670
671 BOOL pdb_set_logon_script(SAM_ACCOUNT *sampass, const char *logon_script, BOOL store)
672 {
673         if (!sampass)
674                 return False;
675
676         if (logon_script) { 
677                 DEBUG(10, ("pdb_set_logon_script: setting logon script %s, was %s\n", logon_script,
678                         (sampass->private.logon_script)?(sampass->private.logon_script):"NULL"));
679  
680                 sampass->private.logon_script = talloc_strdup(sampass->mem_ctx, logon_script);
681
682                 if (!sampass->private.logon_script) {
683                         DEBUG(0, ("pdb_set_logon_script: talloc_strdup() failed!\n"));
684                         return False;
685                 }
686
687         } else {
688                 sampass->private.logon_script = PDB_NOT_QUITE_NULL;
689         }
690         
691         if (store) {
692                 DEBUG(10, ("pdb_set_logon_script: setting logon script sam flag!\n"));
693                 pdb_set_init_flag(sampass, FLAG_SAM_LOGONSCRIPT);
694         }
695
696         return True;
697 }
698
699 /*********************************************************************
700  Set the user's profile path.
701  ********************************************************************/
702
703 BOOL pdb_set_profile_path (SAM_ACCOUNT *sampass, const char *profile_path, BOOL store)
704 {
705         if (!sampass)
706                 return False;
707
708         if (profile_path) { 
709                 DEBUG(10, ("pdb_set_profile_path: setting profile path %s, was %s\n", profile_path,
710                         (sampass->private.profile_path)?(sampass->private.profile_path):"NULL"));
711  
712                 sampass->private.profile_path = talloc_strdup(sampass->mem_ctx, profile_path);
713                 
714                 if (!sampass->private.profile_path) {
715                         DEBUG(0, ("pdb_set_profile_path: talloc_strdup() failed!\n"));
716                         return False;
717                 }
718
719         } else {
720                 sampass->private.profile_path = PDB_NOT_QUITE_NULL;
721         }
722
723         if (store) {
724                 DEBUG(10, ("pdb_set_profile_path: setting profile path sam flag!\n"));
725                 pdb_set_init_flag(sampass, FLAG_SAM_PROFILE);
726         }
727
728         return True;
729 }
730
731 /*********************************************************************
732  Set the user's directory drive.
733  ********************************************************************/
734
735 BOOL pdb_set_dir_drive (SAM_ACCOUNT *sampass, const char *dir_drive, BOOL store)
736 {
737         if (!sampass)
738                 return False;
739
740         if (dir_drive) { 
741                 DEBUG(10, ("pdb_set_dir_drive: setting dir drive %s, was %s\n", dir_drive,
742                         (sampass->private.dir_drive)?(sampass->private.dir_drive):"NULL"));
743  
744                 sampass->private.dir_drive = talloc_strdup(sampass->mem_ctx, dir_drive);
745                 
746                 if (!sampass->private.dir_drive) {
747                         DEBUG(0, ("pdb_set_dir_drive: talloc_strdup() failed!\n"));
748                         return False;
749                 }
750
751         } else {
752                 sampass->private.dir_drive = PDB_NOT_QUITE_NULL;
753         }
754         
755         if (store) {
756                 DEBUG(10, ("pdb_set_dir_drive: setting dir drive sam flag!\n"));
757                 pdb_set_init_flag(sampass, FLAG_SAM_DRIVE);
758         }
759
760         return True;
761 }
762
763 /*********************************************************************
764  Set the user's home directory.
765  ********************************************************************/
766
767 BOOL pdb_set_homedir (SAM_ACCOUNT *sampass, const char *home_dir, BOOL store)
768 {
769         if (!sampass)
770                 return False;
771
772         if (home_dir) { 
773                 DEBUG(10, ("pdb_set_homedir: setting home dir %s, was %s\n", home_dir,
774                         (sampass->private.home_dir)?(sampass->private.home_dir):"NULL"));
775  
776                 sampass->private.home_dir = talloc_strdup(sampass->mem_ctx, home_dir);
777                 
778                 if (!sampass->private.home_dir) {
779                         DEBUG(0, ("pdb_set_home_dir: talloc_strdup() failed!\n"));
780                         return False;
781                 }
782
783         } else {
784                 sampass->private.home_dir = PDB_NOT_QUITE_NULL;
785         }
786
787         if (store) {
788                 DEBUG(10, ("pdb_set_homedir: setting home dir sam flag!\n"));
789                 pdb_set_init_flag(sampass, FLAG_SAM_SMBHOME);
790         }
791
792         return True;
793 }
794
795 /*********************************************************************
796  Set the user's unix home directory.
797  ********************************************************************/
798
799 BOOL pdb_set_unix_homedir (SAM_ACCOUNT *sampass, const char *unix_home_dir)
800 {
801         if (!sampass)
802                 return False;
803
804         if (unix_home_dir) { 
805                 DEBUG(10, ("pdb_set_unix_homedir: setting home dir %s, was %s\n", unix_home_dir,
806                         (sampass->private.unix_home_dir)?(sampass->private.unix_home_dir):"NULL"));
807  
808                 sampass->private.unix_home_dir = talloc_strdup(sampass->mem_ctx, 
809                                                           unix_home_dir);
810                 
811                 if (!sampass->private.unix_home_dir) {
812                         DEBUG(0, ("pdb_set_unix_home_dir: talloc_strdup() failed!\n"));
813                         return False;
814                 }
815
816         } else {
817                 sampass->private.unix_home_dir = PDB_NOT_QUITE_NULL;
818         }
819
820         return True;
821 }
822
823 /*********************************************************************
824  Set the user's account description.
825  ********************************************************************/
826
827 BOOL pdb_set_acct_desc (SAM_ACCOUNT *sampass, const char *acct_desc)
828 {
829         if (!sampass)
830                 return False;
831
832         if (acct_desc) { 
833                 sampass->private.acct_desc = talloc_strdup(sampass->mem_ctx, acct_desc);
834
835                 if (!sampass->private.acct_desc) {
836                         DEBUG(0, ("pdb_set_acct_desc: talloc_strdup() failed!\n"));
837                         return False;
838                 }
839
840         } else {
841                 sampass->private.acct_desc = PDB_NOT_QUITE_NULL;
842         }
843
844         return True;
845 }
846
847 /*********************************************************************
848  Set the user's workstation allowed list.
849  ********************************************************************/
850
851 BOOL pdb_set_workstations (SAM_ACCOUNT *sampass, const char *workstations)
852 {
853         if (!sampass)
854                 return False;
855
856         if (workstations) { 
857                 DEBUG(10, ("pdb_set_workstations: setting workstations %s, was %s\n", workstations,
858                         (sampass->private.workstations)?(sampass->private.workstations):"NULL"));
859  
860                 sampass->private.workstations = talloc_strdup(sampass->mem_ctx, workstations);
861
862                 if (!sampass->private.workstations) {
863                         DEBUG(0, ("pdb_set_workstations: talloc_strdup() failed!\n"));
864                         return False;
865                 }
866
867         } else {
868                 sampass->private.workstations = PDB_NOT_QUITE_NULL;
869         }
870
871         return True;
872 }
873
874 /*********************************************************************
875  Set the user's 'unknown_str', whatever the heck this actually is...
876  ********************************************************************/
877
878 BOOL pdb_set_unknown_str (SAM_ACCOUNT *sampass, const char *unknown_str)
879 {
880         if (!sampass)
881                 return False;
882
883         if (unknown_str) { 
884                 sampass->private.unknown_str = talloc_strdup(sampass->mem_ctx, unknown_str);
885                 
886                 if (!sampass->private.unknown_str) {
887                         DEBUG(0, ("pdb_set_unknown_str: talloc_strdup() failed!\n"));
888                         return False;
889                 }
890
891         } else {
892                 sampass->private.unknown_str = PDB_NOT_QUITE_NULL;
893         }
894
895         return True;
896 }
897
898 /*********************************************************************
899  Set the user's dial string.
900  ********************************************************************/
901
902 BOOL pdb_set_munged_dial (SAM_ACCOUNT *sampass, const char *munged_dial)
903 {
904         if (!sampass)
905                 return False;
906
907         if (munged_dial) { 
908                 sampass->private.munged_dial = talloc_strdup(sampass->mem_ctx, munged_dial);
909                 
910                 if (!sampass->private.munged_dial) {
911                         DEBUG(0, ("pdb_set_munged_dial: talloc_strdup() failed!\n"));
912                         return False;
913                 }
914
915         } else {
916                 sampass->private.munged_dial = PDB_NOT_QUITE_NULL;
917         }
918
919         return True;
920 }
921
922 /*********************************************************************
923  Set the user's NT hash.
924  ********************************************************************/
925
926 BOOL pdb_set_nt_passwd (SAM_ACCOUNT *sampass, const uint8 *pwd)
927 {
928         if (!sampass)
929                 return False;
930
931         data_blob_clear_free(&sampass->private.nt_pw);
932         
933         sampass->private.nt_pw = data_blob(pwd, NT_HASH_LEN);
934
935         return True;
936 }
937
938 /*********************************************************************
939  Set the user's LM hash.
940  ********************************************************************/
941
942 BOOL pdb_set_lanman_passwd (SAM_ACCOUNT *sampass, const uint8 pwd[16])
943 {
944         if (!sampass)
945                 return False;
946
947         data_blob_clear_free(&sampass->private.lm_pw);
948         
949         sampass->private.lm_pw = data_blob(pwd, LM_HASH_LEN);
950
951         return True;
952 }
953
954 /*********************************************************************
955  Set the user's plaintext password only (base procedure, see helper
956  below)
957  ********************************************************************/
958
959 BOOL pdb_set_plaintext_pw_only (SAM_ACCOUNT *sampass, const char *password)
960 {
961         if (!sampass)
962                 return False;
963
964         if (password) { 
965                 if (sampass->private.plaintext_pw!=NULL) 
966                         memset(sampass->private.plaintext_pw,'\0',strlen(sampass->private.plaintext_pw)+1);
967                 sampass->private.plaintext_pw = talloc_strdup(sampass->mem_ctx, password);
968                 
969                 if (!sampass->private.plaintext_pw) {
970                         DEBUG(0, ("pdb_set_unknown_str: talloc_strdup() failed!\n"));
971                         return False;
972                 }
973
974         } else {
975                 sampass->private.plaintext_pw = NULL;
976         }
977
978         return True;
979 }
980
981 BOOL pdb_set_unknown_3 (SAM_ACCOUNT *sampass, uint32 unkn)
982 {
983         if (!sampass)
984                 return False;
985
986         sampass->private.unknown_3 = unkn;
987         return True;
988 }
989
990 BOOL pdb_set_unknown_5 (SAM_ACCOUNT *sampass, uint32 unkn)
991 {
992         if (!sampass)
993                 return False;
994
995         sampass->private.unknown_5 = unkn;
996         return True;
997 }
998
999 BOOL pdb_set_unknown_6 (SAM_ACCOUNT *sampass, uint32 unkn)
1000 {
1001         if (!sampass)
1002                 return False;
1003
1004         sampass->private.unknown_6 = unkn;
1005         return True;
1006 }
1007
1008 BOOL pdb_set_hours (SAM_ACCOUNT *sampass, const uint8 *hours)
1009 {
1010         if (!sampass)
1011                 return False;
1012
1013         if (!hours) {
1014                 memset ((char *)sampass->private.hours, 0, MAX_HOURS_LEN);
1015                 return True;
1016         }
1017         
1018         memcpy (sampass->private.hours, hours, MAX_HOURS_LEN);
1019
1020         return True;
1021 }
1022
1023
1024 /* Helpful interfaces to the above */
1025
1026 /*********************************************************************
1027  Sets the last changed times and must change times for a normal
1028  password change.
1029  ********************************************************************/
1030
1031 BOOL pdb_set_pass_changed_now (SAM_ACCOUNT *sampass)
1032 {
1033         uint32 expire;
1034
1035         if (!sampass)
1036                 return False;
1037         
1038         if (!pdb_set_pass_last_set_time (sampass, time(NULL)))
1039                 return False;
1040
1041         if (!account_policy_get(AP_MAX_PASSWORD_AGE, &expire) 
1042             || (expire==(uint32)-1)) {
1043                 if (!pdb_set_pass_must_change_time (sampass, get_time_t_max(), False))
1044                         return False;
1045         } else {
1046                 if (!pdb_set_pass_must_change_time (sampass, 
1047                                                     pdb_get_pass_last_set_time(sampass)
1048                                                     + expire, True))
1049                         return False;
1050         }
1051         
1052         return True;
1053 }
1054
1055 /*********************************************************************
1056  Set the user's PLAINTEXT password.  Used as an interface to the above.
1057  Also sets the last change time to NOW.
1058  ********************************************************************/
1059
1060 BOOL pdb_set_plaintext_passwd (SAM_ACCOUNT *sampass, const char *plaintext)
1061 {
1062         uchar new_lanman_p16[16];
1063         uchar new_nt_p16[16];
1064
1065         if (!sampass || !plaintext)
1066                 return False;
1067         
1068         nt_lm_owf_gen (plaintext, new_nt_p16, new_lanman_p16);
1069
1070         if (!pdb_set_nt_passwd (sampass, new_nt_p16)) 
1071                 return False;
1072
1073         if (!pdb_set_lanman_passwd (sampass, new_lanman_p16)) 
1074                 return False;
1075
1076         if (!pdb_set_plaintext_pw_only (sampass, plaintext)) 
1077                 return False;
1078
1079         if (!pdb_set_pass_changed_now (sampass))
1080                 return False;
1081
1082         return True;
1083 }
1084