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