9abc3b5a28acbd64924cb96bab8658f98b940b84
[ira/wip.git] / source3 / passdb / pdb_get_set.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    SAM_ACCOUNT access routines
5    Copyright (C) Jeremy Allison                 1996-2001
6    Copyright (C) Luke Kenneth Casson Leighton   1996-1998
7    Copyright (C) Gerald (Jerry) Carter          2000-2001
8    Copyright (C) Andrew Bartlett                2001-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 /*********************************************************************
28  Collection of get...() functions for SAM_ACCOUNT_INFO.
29  ********************************************************************/
30
31 uint16 pdb_get_acct_ctrl (const SAM_ACCOUNT *sampass)
32 {
33         if (sampass)
34                 return (sampass->private.acct_ctrl);
35         else
36                 return (ACB_DISABLED);
37 }
38
39 time_t pdb_get_logon_time (const SAM_ACCOUNT *sampass)
40 {
41         if (sampass)
42                 return (sampass->private.logon_time);
43         else
44                 return (0);
45 }
46
47 time_t pdb_get_logoff_time (const SAM_ACCOUNT *sampass)
48 {
49         if (sampass)
50                 return (sampass->private.logoff_time);
51         else
52                 return (-1);
53 }
54
55 time_t pdb_get_kickoff_time (const SAM_ACCOUNT *sampass)
56 {
57         if (sampass)
58                 return (sampass->private.kickoff_time);
59         else
60                 return (-1);
61 }
62
63 time_t pdb_get_pass_last_set_time (const SAM_ACCOUNT *sampass)
64 {
65         if (sampass)
66                 return (sampass->private.pass_last_set_time);
67         else
68                 return (-1);
69 }
70
71 time_t pdb_get_pass_can_change_time (const SAM_ACCOUNT *sampass)
72 {
73         if (sampass)
74                 return (sampass->private.pass_can_change_time);
75         else
76                 return (-1);
77 }
78
79 time_t pdb_get_pass_must_change_time (const SAM_ACCOUNT *sampass)
80 {
81         if (sampass)
82                 return (sampass->private.pass_must_change_time);
83         else
84                 return (-1);
85 }
86
87 uint16 pdb_get_logon_divs (const SAM_ACCOUNT *sampass)
88 {
89         if (sampass)
90                 return (sampass->private.logon_divs);
91         else
92                 return (-1);
93 }
94
95 uint32 pdb_get_hours_len (const SAM_ACCOUNT *sampass)
96 {
97         if (sampass)
98                 return (sampass->private.hours_len);
99         else
100                 return (-1);
101 }
102
103 const uint8* pdb_get_hours (const SAM_ACCOUNT *sampass)
104 {
105         if (sampass)
106                 return (sampass->private.hours);
107         else
108                 return (NULL);
109 }
110
111 const uint8* pdb_get_nt_passwd (const SAM_ACCOUNT *sampass)
112 {
113         if (sampass) {
114                 SMB_ASSERT((!sampass->private.nt_pw.data) 
115                            || sampass->private.nt_pw.length == NT_HASH_LEN);
116                 return ((uint8*)sampass->private.nt_pw.data);
117         }
118         else
119                 return (NULL);
120 }
121
122 const uint8* pdb_get_lanman_passwd (const SAM_ACCOUNT *sampass)
123 {
124         if (sampass) {
125                 SMB_ASSERT((!sampass->private.lm_pw.data) 
126                            || sampass->private.lm_pw.length == LM_HASH_LEN);
127                 return ((uint8*)sampass->private.lm_pw.data);
128         }
129         else
130                 return (NULL);
131 }
132
133 uint32 pdb_get_user_rid (const SAM_ACCOUNT *sampass)
134 {
135         if (sampass)
136                 return (sampass->private.user_rid);
137         else
138                 return (-1);
139 }
140
141 uint32 pdb_get_group_rid (const SAM_ACCOUNT *sampass)
142 {
143         if (sampass)
144                 return (sampass->private.group_rid);
145         else
146                 return (-1);
147 }
148
149 /**
150  * Get flags showing what is initalised in the SAM_ACCOUNT
151  * @param sampass the SAM_ACCOUNT in question
152  * @return the flags indicating the members initialised in the struct.
153  **/
154  
155 uint32 pdb_get_init_flag (SAM_ACCOUNT *sampass)
156 {
157         if (sampass)
158                 return sampass->private.init_flag;
159         else 
160                 return FLAG_SAM_UNINIT;
161 }
162
163 uid_t pdb_get_uid (const SAM_ACCOUNT *sampass)
164 {
165         if (sampass)
166                 return (sampass->private.uid);
167         else
168                 return (-1);
169 }
170
171 gid_t pdb_get_gid (const SAM_ACCOUNT *sampass)
172 {
173         if (sampass)
174                 return (sampass->private.gid);
175         else
176                 return (-1);
177 }
178
179 const char* pdb_get_username (const SAM_ACCOUNT *sampass)
180 {
181         if (sampass)
182                 return (sampass->private.username);
183         else
184                 return (NULL);
185 }
186
187 const char* pdb_get_domain (const SAM_ACCOUNT *sampass)
188 {
189         if (sampass)
190                 return (sampass->private.domain);
191         else
192                 return (NULL);
193 }
194
195 const char* pdb_get_nt_username (const SAM_ACCOUNT *sampass)
196 {
197         if (sampass)
198                 return (sampass->private.nt_username);
199         else
200                 return (NULL);
201 }
202
203 const char* pdb_get_fullname (const SAM_ACCOUNT *sampass)
204 {
205         if (sampass)
206                 return (sampass->private.full_name);
207         else
208                 return (NULL);
209 }
210
211 const char* pdb_get_homedir (const SAM_ACCOUNT *sampass)
212 {
213         if (sampass)
214                 return (sampass->private.home_dir);
215         else
216                 return (NULL);
217 }
218
219 const char* pdb_get_dirdrive (const SAM_ACCOUNT *sampass)
220 {
221         if (sampass)
222                 return (sampass->private.dir_drive);
223         else
224                 return (NULL);
225 }
226
227 const char* pdb_get_logon_script (const SAM_ACCOUNT *sampass)
228 {
229         if (sampass)
230                 return (sampass->private.logon_script);
231         else
232                 return (NULL);
233 }
234
235 const char* pdb_get_profile_path (const SAM_ACCOUNT *sampass)
236 {
237         if (sampass)
238                 return (sampass->private.profile_path);
239         else
240                 return (NULL);
241 }
242
243 const char* pdb_get_acct_desc (const SAM_ACCOUNT *sampass)
244 {
245         if (sampass)
246                 return (sampass->private.acct_desc);
247         else
248                 return (NULL);
249 }
250
251 const char* pdb_get_workstations (const SAM_ACCOUNT *sampass)
252 {
253         if (sampass)
254                 return (sampass->private.workstations);
255         else
256                 return (NULL);
257 }
258
259 const char* pdb_get_unknown_str (const SAM_ACCOUNT *sampass)
260 {
261         if (sampass)
262                 return (sampass->private.unknown_str);
263         else
264                 return (NULL);
265 }
266
267 const char* pdb_get_munged_dial (const SAM_ACCOUNT *sampass)
268 {
269         if (sampass)
270                 return (sampass->private.munged_dial);
271         else
272                 return (NULL);
273 }
274
275 uint32 pdb_get_unknown3 (const SAM_ACCOUNT *sampass)
276 {
277         if (sampass)
278                 return (sampass->private.unknown_3);
279         else
280                 return (-1);
281 }
282
283 uint32 pdb_get_unknown5 (const SAM_ACCOUNT *sampass)
284 {
285         if (sampass)
286                 return (sampass->private.unknown_5);
287         else
288                 return (-1);
289 }
290
291 uint32 pdb_get_unknown6 (const SAM_ACCOUNT *sampass)
292 {
293         if (sampass)
294                 return (sampass->private.unknown_6);
295         else
296                 return (-1);
297 }
298
299 /*********************************************************************
300  Collection of set...() functions for SAM_ACCOUNT_INFO.
301  ********************************************************************/
302
303 BOOL pdb_set_acct_ctrl (SAM_ACCOUNT *sampass, uint16 flags)
304 {
305         if (!sampass)
306                 return False;
307                 
308         if (sampass) {
309                 sampass->private.acct_ctrl = flags;
310                 return True;
311         }
312         
313         return False;
314 }
315
316 BOOL pdb_set_logon_time (SAM_ACCOUNT *sampass, time_t mytime)
317 {
318         if (!sampass)
319                 return False;
320
321         sampass->private.logon_time = mytime;
322         return True;
323 }
324
325 BOOL pdb_set_logoff_time (SAM_ACCOUNT *sampass, time_t mytime)
326 {
327         if (!sampass)
328                 return False;
329
330         sampass->private.logoff_time = mytime;
331         return True;
332 }
333
334 BOOL pdb_set_kickoff_time (SAM_ACCOUNT *sampass, time_t mytime)
335 {
336         if (!sampass)
337                 return False;
338
339         sampass->private.kickoff_time = mytime;
340         return True;
341 }
342
343 BOOL pdb_set_pass_can_change_time (SAM_ACCOUNT *sampass, time_t mytime)
344 {
345         if (!sampass)
346                 return False;
347
348         sampass->private.pass_can_change_time = mytime;
349         return True;
350 }
351
352 BOOL pdb_set_pass_must_change_time (SAM_ACCOUNT *sampass, time_t mytime)
353 {
354         if (!sampass)
355                 return False;
356
357         sampass->private.pass_must_change_time = mytime;
358         return True;
359 }
360
361 BOOL pdb_set_pass_last_set_time (SAM_ACCOUNT *sampass, time_t mytime)
362 {
363         if (!sampass)
364                 return False;
365
366         sampass->private.pass_last_set_time = mytime;
367         return True;
368 }
369
370 BOOL pdb_set_hours_len (SAM_ACCOUNT *sampass, uint32 len)
371 {
372         if (!sampass)
373                 return False;
374
375         sampass->private.hours_len = len;
376         return True;
377 }
378
379 BOOL pdb_set_logon_divs (SAM_ACCOUNT *sampass, uint16 hours)
380 {
381         if (!sampass)
382                 return False;
383
384         sampass->private.logon_divs = hours;
385         return True;
386 }
387
388 /**
389  * Set flags showing what is initalised in the SAM_ACCOUNT
390  * @param sampass the SAM_ACCOUNT in question
391  * @param flag The *new* flag to be set.  Old flags preserved
392  *             this flag is only added.  
393  **/
394  
395 BOOL pdb_set_init_flag (SAM_ACCOUNT *sampass, uint32 flag)
396 {
397         if (!sampass)
398                 return False;
399
400         sampass->private.init_flag |= flag;
401
402         return True;
403 }
404
405 BOOL pdb_set_uid (SAM_ACCOUNT *sampass, const uid_t uid)
406 {
407         if (!sampass)
408                 return False;
409         
410         DEBUG(10, ("pdb_set_uid: setting uid %d, was %d\n", 
411                    (int)uid, (int)sampass->private.uid));
412  
413         sampass->private.uid = uid;
414         pdb_set_init_flag(sampass, FLAG_SAM_UID); 
415
416         return True;
417
418 }
419
420 BOOL pdb_set_gid (SAM_ACCOUNT *sampass, const gid_t gid)
421 {
422         if (!sampass)
423                 return False;
424                 
425         DEBUG(10, ("pdb_set_gid: setting gid %d, was %d\n", 
426                    (int)gid, (int)sampass->private.gid));
427  
428         sampass->private.gid = gid; 
429         pdb_set_init_flag(sampass, FLAG_SAM_GID); 
430
431         return True;
432
433 }
434
435 BOOL pdb_set_user_rid (SAM_ACCOUNT *sampass, uint32 rid)
436 {
437         if (!sampass)
438                 return False;
439
440         DEBUG(10, ("pdb_set_rid: setting user rid %d, was %d\n", 
441                    rid, sampass->private.user_rid));
442  
443         sampass->private.user_rid = rid;
444         return True;
445 }
446
447 BOOL pdb_set_group_rid (SAM_ACCOUNT *sampass, uint32 grid)
448 {
449         if (!sampass)
450                 return False;
451
452         DEBUG(10, ("pdb_set_group_rid: setting group rid %d, was %d\n", 
453                    grid, sampass->private.group_rid));
454  
455         sampass->private.group_rid = grid;
456         return True;
457 }
458
459 /*********************************************************************
460  Set the user's UNIX name.
461  ********************************************************************/
462
463 BOOL pdb_set_username(SAM_ACCOUNT *sampass, const char *username)
464 {       
465         if (!sampass)
466                 return False;
467         
468         *sampass->private.username = '\0';
469         DEBUG(10, ("pdb_set_username: setting username %s, was %s\n", 
470                    username, sampass->private.username));
471  
472         if (!username)
473                 return False;
474         StrnCpy (sampass->private.username, username, sizeof(pstring) - 1);
475
476         return True;
477 }
478
479 /*********************************************************************
480  Set the domain name.
481  ********************************************************************/
482
483 BOOL pdb_set_domain(SAM_ACCOUNT *sampass, const char *domain)
484 {       
485         if (!sampass)
486                 return False;
487         *sampass->private.domain = '\0';
488         if (!domain)
489                 return False;
490
491         StrnCpy (sampass->private.domain, domain, sizeof(pstring) - 1);
492
493         return True;
494 }
495
496 /*********************************************************************
497  Set the user's NT name.
498  ********************************************************************/
499
500 BOOL pdb_set_nt_username(SAM_ACCOUNT *sampass, const char *nt_username)
501 {
502         if (!sampass)
503                 return False;
504         *sampass->private.nt_username = '\0';
505         if (!nt_username)
506                 return False;
507
508         StrnCpy (sampass->private.nt_username, nt_username, sizeof(pstring) - 1);
509
510         return True;
511 }
512
513 /*********************************************************************
514  Set the user's full name.
515  ********************************************************************/
516
517 BOOL pdb_set_fullname(SAM_ACCOUNT *sampass, const char *fullname)
518 {
519         if (!sampass)
520                 return False;
521
522         DEBUG(10, ("pdb_set_fullname: setting full name %s, was %s\n", 
523                    fullname, sampass->private.full_name));
524  
525         *sampass->private.full_name = '\0';
526         if (!fullname)
527                 return False;
528
529         StrnCpy (sampass->private.full_name, fullname, sizeof(pstring) - 1);
530
531         return True;
532 }
533
534 /*********************************************************************
535  Set the user's logon script.
536  ********************************************************************/
537
538 BOOL pdb_set_logon_script(SAM_ACCOUNT *sampass, const char *logon_script, BOOL store)
539 {
540         if (!sampass)
541                 return False;
542
543         DEBUG(10, ("pdb_set_logon_script: setting logon script (store:%d) %s, was %s\n", 
544                    store, logon_script, sampass->private.logon_script));
545  
546         *sampass->private.logon_script = '\0';
547         if (!logon_script)
548                 return False;
549
550         StrnCpy (sampass->private.logon_script, logon_script, sizeof(pstring) - 1);
551
552         if (store)
553                 pdb_set_init_flag(sampass, FLAG_SAM_LOGONSCRIPT); 
554
555         return True;
556 }
557
558 /*********************************************************************
559  Set the user's profile path.
560  ********************************************************************/
561
562 BOOL pdb_set_profile_path (SAM_ACCOUNT *sampass, const char *profile_path, BOOL store)
563 {
564         if (!sampass)
565                 return False;
566
567         DEBUG(10, ("pdb_set_profile_path: setting profile path (store:%d) %s, was %s\n", 
568                    store, profile_path, sampass->private.profile_path));
569  
570         *sampass->private.profile_path = '\0';
571         if (!profile_path)
572                 return False;
573         
574         StrnCpy (sampass->private.profile_path, profile_path, sizeof(pstring) - 1);
575
576         if (store)
577                 pdb_set_init_flag(sampass, FLAG_SAM_PROFILE);
578         
579         return True;
580 }
581
582 /*********************************************************************
583  Set the user's directory drive.
584  ********************************************************************/
585
586 BOOL pdb_set_dir_drive (SAM_ACCOUNT *sampass, const char *dir_drive, BOOL store)
587 {
588         if (!sampass)
589                 return False;
590         *sampass->private.dir_drive = '\0';
591         if (!dir_drive)
592                 return False;
593
594         StrnCpy (sampass->private.dir_drive, dir_drive, sizeof(pstring) - 1);
595
596         if (store)
597                 pdb_set_init_flag(sampass, FLAG_SAM_DRIVE);
598
599         return True;
600 }
601
602 /*********************************************************************
603  Set the user's home directory.
604  ********************************************************************/
605
606 BOOL pdb_set_homedir (SAM_ACCOUNT *sampass, const char *homedir, BOOL store)
607 {
608         if (!sampass)
609                 return False;
610         *sampass->private.home_dir = '\0';
611         if (!homedir)
612                 return False;
613         
614         StrnCpy (sampass->private.home_dir, homedir, sizeof(pstring) - 1);
615
616         if (store)
617                 pdb_set_init_flag(sampass, FLAG_SAM_SMBHOME);
618
619         return True;
620 }
621
622 /*********************************************************************
623  Set the user's account description.
624  ********************************************************************/
625
626 BOOL pdb_set_acct_desc (SAM_ACCOUNT *sampass, const char *acct_desc)
627 {
628         if (!sampass)
629                 return False;
630         *sampass->private.acct_desc = '\0';
631         if (!acct_desc)
632                 return False;
633         
634         StrnCpy (sampass->private.acct_desc, acct_desc, sizeof(pstring) - 1);
635
636         return True;
637 }
638
639 /*********************************************************************
640  Set the user's workstation allowed list.
641  ********************************************************************/
642
643 BOOL pdb_set_workstations (SAM_ACCOUNT *sampass, const char *workstations)
644 {
645         if (!sampass)
646                 return False;
647         *sampass->private.workstations = '\0';
648         if (!workstations)
649                 return False;
650
651         StrnCpy (sampass->private.workstations, workstations, sizeof(pstring) - 1);
652
653         return True;
654 }
655
656 /*********************************************************************
657  Set the user's 'unknown_str', whatever the heck this actually is...
658  ********************************************************************/
659
660 BOOL pdb_set_unknown_str (SAM_ACCOUNT *sampass, const char *unknown_str)
661 {
662         if (!sampass)
663                 return False;
664         *sampass->private.unknown_str = '\0';
665         if (!unknown_str)
666                 return False;
667
668         StrnCpy (sampass->private.unknown_str, unknown_str, sizeof(pstring) - 1);
669
670         return True;
671 }
672
673 /*********************************************************************
674  Set the user's dial string.
675  ********************************************************************/
676
677 BOOL pdb_set_munged_dial (SAM_ACCOUNT *sampass, const char *munged_dial)
678 {
679         if (!sampass)
680                 return False;
681         *sampass->private.munged_dial = '\0';
682         if (!munged_dial)
683                 return False;
684
685         StrnCpy (sampass->private.munged_dial, munged_dial, sizeof(pstring) - 1);
686
687         return True;
688 }
689
690 /*********************************************************************
691  Set the user's NT hash.
692  ********************************************************************/
693
694 BOOL pdb_set_nt_passwd (SAM_ACCOUNT *sampass, const uint8 *pwd)
695 {
696         if (!sampass)
697                 return False;
698
699         data_blob_clear_free(&sampass->private.nt_pw);
700         
701         sampass->private.nt_pw = data_blob(pwd, NT_HASH_LEN);
702
703         return True;
704 }
705
706 /*********************************************************************
707  Set the user's LM hash.
708  ********************************************************************/
709
710 BOOL pdb_set_lanman_passwd (SAM_ACCOUNT *sampass, const uint8 *pwd)
711 {
712         if (!sampass)
713                 return False;
714
715         data_blob_clear_free(&sampass->private.lm_pw);
716         
717         sampass->private.lm_pw = data_blob(pwd, LM_HASH_LEN);
718
719         return True;
720 }
721
722 BOOL pdb_set_unknown_3 (SAM_ACCOUNT *sampass, uint32 unkn)
723 {
724         if (!sampass)
725                 return False;
726
727         sampass->private.unknown_3 = unkn;
728         return True;
729 }
730
731 BOOL pdb_set_unknown_5 (SAM_ACCOUNT *sampass, uint32 unkn)
732 {
733         if (!sampass)
734                 return False;
735
736         sampass->private.unknown_5 = unkn;
737         return True;
738 }
739
740 BOOL pdb_set_unknown_6 (SAM_ACCOUNT *sampass, uint32 unkn)
741 {
742         if (!sampass)
743                 return False;
744
745         sampass->private.unknown_6 = unkn;
746         return True;
747 }
748
749 BOOL pdb_set_hours (SAM_ACCOUNT *sampass, const uint8 *hours)
750 {
751         if (!sampass)
752                 return False;
753
754         if (!hours) {
755                 memset ((char *)sampass->private.hours, 0, MAX_HOURS_LEN);
756                 return True;
757         }
758         
759         memcpy (sampass->private.hours, hours, MAX_HOURS_LEN);
760
761         return True;
762 }
763
764
765 /* Helpful interfaces to the above */
766
767 /*********************************************************************
768  Sets the last changed times and must change times for a normal
769  password change.
770  ********************************************************************/
771
772 BOOL pdb_set_pass_changed_now (SAM_ACCOUNT *sampass)
773 {
774         uint32 expire;
775
776         if (!sampass)
777                 return False;
778         
779         if (!pdb_set_pass_last_set_time (sampass, time(NULL)))
780                 return False;
781
782         account_policy_get(AP_MAX_PASSWORD_AGE, &expire);
783
784         if (expire==(uint32)-1) {
785                 if (!pdb_set_pass_must_change_time (sampass, 0))
786                         return False;
787         } else {
788                 if (!pdb_set_pass_must_change_time (sampass, 
789                                             pdb_get_pass_last_set_time(sampass)
790                                             + expire))
791                         return False;
792         }
793         
794         return True;
795 }
796
797 /*********************************************************************
798  Set the user's PLAINTEXT password.  Used as an interface to the above.
799  Also sets the last change time to NOW.
800  ********************************************************************/
801
802 BOOL pdb_set_plaintext_passwd (SAM_ACCOUNT *sampass, const char *plaintext)
803 {
804         uchar new_lanman_p16[16];
805         uchar new_nt_p16[16];
806
807         if (!sampass || !plaintext)
808                 return False;
809         
810         nt_lm_owf_gen (plaintext, new_nt_p16, new_lanman_p16);
811
812         if (!pdb_set_nt_passwd (sampass, new_nt_p16)) 
813                 return False;
814
815         if (!pdb_set_lanman_passwd (sampass, new_lanman_p16)) 
816                 return False;
817         
818         if (!pdb_set_pass_changed_now (sampass))
819                 return False;
820
821         return True;
822 }
823