Change the passdb interface to use allocated strings.
[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  * @todo Redefine this to NULL, but this changes the API becouse
29  *       much of samba assumes that the pdb_get...() funtions 
30  *       return pstrings.  (ie not null-pointers).
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 (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)
325 {
326         if (!sampass)
327                 return False;
328
329         sampass->private.logon_time = mytime;
330         return True;
331 }
332
333 BOOL pdb_set_logoff_time (SAM_ACCOUNT *sampass, time_t mytime)
334 {
335         if (!sampass)
336                 return False;
337
338         sampass->private.logoff_time = mytime;
339         return True;
340 }
341
342 BOOL pdb_set_kickoff_time (SAM_ACCOUNT *sampass, time_t mytime)
343 {
344         if (!sampass)
345                 return False;
346
347         sampass->private.kickoff_time = mytime;
348         return True;
349 }
350
351 BOOL pdb_set_pass_can_change_time (SAM_ACCOUNT *sampass, time_t mytime)
352 {
353         if (!sampass)
354                 return False;
355
356         sampass->private.pass_can_change_time = mytime;
357         return True;
358 }
359
360 BOOL pdb_set_pass_must_change_time (SAM_ACCOUNT *sampass, time_t mytime)
361 {
362         if (!sampass)
363                 return False;
364
365         sampass->private.pass_must_change_time = mytime;
366         return True;
367 }
368
369 BOOL pdb_set_pass_last_set_time (SAM_ACCOUNT *sampass, time_t mytime)
370 {
371         if (!sampass)
372                 return False;
373
374         sampass->private.pass_last_set_time = mytime;
375         return True;
376 }
377
378 BOOL pdb_set_hours_len (SAM_ACCOUNT *sampass, uint32 len)
379 {
380         if (!sampass)
381                 return False;
382
383         sampass->private.hours_len = len;
384         return True;
385 }
386
387 BOOL pdb_set_logon_divs (SAM_ACCOUNT *sampass, uint16 hours)
388 {
389         if (!sampass)
390                 return False;
391
392         sampass->private.logon_divs = hours;
393         return True;
394 }
395
396 /**
397  * Set flags showing what is initalised in the SAM_ACCOUNT
398  * @param sampass the SAM_ACCOUNT in question
399  * @param flag The *new* flag to be set.  Old flags preserved
400  *             this flag is only added.  
401  **/
402  
403 BOOL pdb_set_init_flag (SAM_ACCOUNT *sampass, uint32 flag)
404 {
405         if (!sampass)
406                 return False;
407
408         sampass->private.init_flag |= flag;
409
410         return True;
411 }
412
413 BOOL pdb_set_uid (SAM_ACCOUNT *sampass, const uid_t uid)
414 {
415         if (!sampass)
416                 return False;
417         
418         DEBUG(10, ("pdb_set_uid: setting uid %d, was %d\n", 
419                    (int)uid, (int)sampass->private.uid));
420  
421         sampass->private.uid = uid;
422         pdb_set_init_flag(sampass, FLAG_SAM_UID); 
423
424         return True;
425
426 }
427
428 BOOL pdb_set_gid (SAM_ACCOUNT *sampass, const gid_t gid)
429 {
430         if (!sampass)
431                 return False;
432                 
433         DEBUG(10, ("pdb_set_gid: setting gid %d, was %d\n", 
434                    (int)gid, (int)sampass->private.gid));
435  
436         sampass->private.gid = gid; 
437         pdb_set_init_flag(sampass, FLAG_SAM_GID); 
438
439         return True;
440
441 }
442
443 BOOL pdb_set_user_rid (SAM_ACCOUNT *sampass, uint32 rid)
444 {
445         if (!sampass)
446                 return False;
447
448         DEBUG(10, ("pdb_set_rid: setting user rid %d, was %d\n", 
449                    rid, sampass->private.user_rid));
450  
451         sampass->private.user_rid = rid;
452         return True;
453 }
454
455 BOOL pdb_set_group_rid (SAM_ACCOUNT *sampass, uint32 grid)
456 {
457         if (!sampass)
458                 return False;
459
460         DEBUG(10, ("pdb_set_group_rid: setting group rid %d, was %d\n", 
461                    grid, sampass->private.group_rid));
462  
463         sampass->private.group_rid = grid;
464         return True;
465 }
466
467 /*********************************************************************
468  Set the user's UNIX name.
469  ********************************************************************/
470
471 BOOL pdb_set_username(SAM_ACCOUNT *sampass, const char *username)
472 {       
473         if (!sampass)
474                 return False;
475
476         DEBUG(10, ("pdb_set_username: setting username %s, was %s\n", 
477                    username, sampass->private.username));
478  
479         if (username) { 
480                 sampass->private.username = talloc_strdup(sampass->mem_ctx, username);
481         } else {
482                 sampass->private.username = PDB_NOT_QUITE_NULL;
483         }
484
485         return True;
486 }
487
488 /*********************************************************************
489  Set the domain name.
490  ********************************************************************/
491
492 BOOL pdb_set_domain(SAM_ACCOUNT *sampass, const char *domain)
493 {       
494         if (!sampass)
495                 return False;
496
497         DEBUG(10, ("pdb_set_domain: setting domain %s, was %s\n", 
498                    domain, sampass->private.domain));
499  
500         if (domain) { 
501                 sampass->private.domain = talloc_strdup(sampass->mem_ctx, domain);
502         } else {
503                 sampass->private.domain = PDB_NOT_QUITE_NULL;
504         }
505
506         return True;
507 }
508
509 /*********************************************************************
510  Set the user's NT name.
511  ********************************************************************/
512
513 BOOL pdb_set_nt_username(SAM_ACCOUNT *sampass, const char *nt_username)
514 {
515         if (!sampass)
516                 return False;
517
518         DEBUG(10, ("pdb_set_nt_username: setting nt username %s, was %s\n", 
519                    nt_username, sampass->private.nt_username));
520  
521         if (nt_username) { 
522                 sampass->private.nt_username = talloc_strdup(sampass->mem_ctx, nt_username);
523         } else {
524                 sampass->private.nt_username = PDB_NOT_QUITE_NULL;
525         }
526
527         return True;
528 }
529
530 /*********************************************************************
531  Set the user's full name.
532  ********************************************************************/
533
534 BOOL pdb_set_fullname(SAM_ACCOUNT *sampass, const char *full_name)
535 {
536         if (!sampass)
537                 return False;
538
539         DEBUG(10, ("pdb_set_full_name: setting full name %s, was %s\n", 
540                    full_name, sampass->private.full_name));
541         
542         if (full_name) { 
543                 sampass->private.full_name = talloc_strdup(sampass->mem_ctx, full_name);
544         } else {
545                 sampass->private.full_name = PDB_NOT_QUITE_NULL;
546         }
547
548         return True;
549 }
550
551 /*********************************************************************
552  Set the user's logon script.
553  ********************************************************************/
554
555 BOOL pdb_set_logon_script(SAM_ACCOUNT *sampass, const char *logon_script, BOOL store)
556 {
557         if (!sampass)
558                 return False;
559
560         DEBUG(10, ("pdb_set_logon_script: setting logon script (store:%d) %s, was %s\n", 
561                    store, logon_script, sampass->private.logon_script));
562  
563         if (logon_script) { 
564                 sampass->private.logon_script = talloc_strdup(sampass->mem_ctx, logon_script);
565         } else {
566                 sampass->private.logon_script = PDB_NOT_QUITE_NULL;
567         }
568         
569         if (store)
570                 pdb_set_init_flag(sampass, FLAG_SAM_LOGONSCRIPT); 
571
572         return True;
573 }
574
575 /*********************************************************************
576  Set the user's profile path.
577  ********************************************************************/
578
579 BOOL pdb_set_profile_path (SAM_ACCOUNT *sampass, const char *profile_path, BOOL store)
580 {
581         if (!sampass)
582                 return False;
583
584         DEBUG(10, ("pdb_set_profile_path: setting profile path (store:%d) %s, was %s\n", 
585                    store, profile_path, sampass->private.profile_path));
586  
587         if (profile_path) { 
588                 sampass->private.profile_path = talloc_strdup(sampass->mem_ctx, profile_path);
589         } else {
590                 sampass->private.profile_path = PDB_NOT_QUITE_NULL;
591         }
592
593         if (store)
594                 pdb_set_init_flag(sampass, FLAG_SAM_PROFILE);
595
596         return True;
597 }
598
599 /*********************************************************************
600  Set the user's directory drive.
601  ********************************************************************/
602
603 BOOL pdb_set_dir_drive (SAM_ACCOUNT *sampass, const char *dir_drive, BOOL store)
604 {
605         if (!sampass)
606                 return False;
607
608         if (dir_drive) { 
609                 sampass->private.dir_drive = talloc_strdup(sampass->mem_ctx, dir_drive);
610         } else {
611                 sampass->private.dir_drive = PDB_NOT_QUITE_NULL;
612         }
613         
614         if (store)
615                 pdb_set_init_flag(sampass, FLAG_SAM_DRIVE);
616
617         return True;
618 }
619
620 /*********************************************************************
621  Set the user's home directory.
622  ********************************************************************/
623
624 BOOL pdb_set_homedir (SAM_ACCOUNT *sampass, const char *home_dir, BOOL store)
625 {
626         if (!sampass)
627                 return False;
628
629         if (home_dir) { 
630                 sampass->private.home_dir = talloc_strdup(sampass->mem_ctx, home_dir);
631         } else {
632                 sampass->private.home_dir = PDB_NOT_QUITE_NULL;
633         }
634
635         if (store)
636                 pdb_set_init_flag(sampass, FLAG_SAM_SMBHOME);
637
638         return True;
639 }
640
641 /*********************************************************************
642  Set the user's account description.
643  ********************************************************************/
644
645 BOOL pdb_set_acct_desc (SAM_ACCOUNT *sampass, const char *acct_desc)
646 {
647         if (!sampass)
648                 return False;
649
650         if (acct_desc) { 
651                 sampass->private.acct_desc = talloc_strdup(sampass->mem_ctx, acct_desc);
652         } else {
653                 sampass->private.acct_desc = PDB_NOT_QUITE_NULL;
654         }
655
656         return True;
657 }
658
659 /*********************************************************************
660  Set the user's workstation allowed list.
661  ********************************************************************/
662
663 BOOL pdb_set_workstations (SAM_ACCOUNT *sampass, const char *workstations)
664 {
665         if (!sampass)
666                 return False;
667
668         if (workstations) { 
669                 sampass->private.workstations = talloc_strdup(sampass->mem_ctx, workstations);
670         } else {
671                 sampass->private.workstations = PDB_NOT_QUITE_NULL;
672         }
673
674         return True;
675 }
676
677 /*********************************************************************
678  Set the user's 'unknown_str', whatever the heck this actually is...
679  ********************************************************************/
680
681 BOOL pdb_set_unknown_str (SAM_ACCOUNT *sampass, const char *unknown_str)
682 {
683         if (!sampass)
684                 return False;
685
686         if (unknown_str) { 
687                 sampass->private.unknown_str = talloc_strdup(sampass->mem_ctx, unknown_str);
688         } else {
689                 sampass->private.unknown_str = PDB_NOT_QUITE_NULL;
690         }
691
692         return True;
693 }
694
695 /*********************************************************************
696  Set the user's dial string.
697  ********************************************************************/
698
699 BOOL pdb_set_munged_dial (SAM_ACCOUNT *sampass, const char *munged_dial)
700 {
701         if (!sampass)
702                 return False;
703         if (munged_dial) { 
704                 sampass->private.munged_dial = talloc_strdup(sampass->mem_ctx, munged_dial);
705         } else {
706                 sampass->private.munged_dial = PDB_NOT_QUITE_NULL;
707         }
708
709         return True;
710 }
711
712 /*********************************************************************
713  Set the user's NT hash.
714  ********************************************************************/
715
716 BOOL pdb_set_nt_passwd (SAM_ACCOUNT *sampass, const uint8 *pwd)
717 {
718         if (!sampass)
719                 return False;
720
721         data_blob_clear_free(&sampass->private.nt_pw);
722         
723         sampass->private.nt_pw = data_blob(pwd, NT_HASH_LEN);
724
725         return True;
726 }
727
728 /*********************************************************************
729  Set the user's LM hash.
730  ********************************************************************/
731
732 BOOL pdb_set_lanman_passwd (SAM_ACCOUNT *sampass, const uint8 *pwd)
733 {
734         if (!sampass)
735                 return False;
736
737         data_blob_clear_free(&sampass->private.lm_pw);
738         
739         sampass->private.lm_pw = data_blob(pwd, LM_HASH_LEN);
740
741         return True;
742 }
743
744 BOOL pdb_set_unknown_3 (SAM_ACCOUNT *sampass, uint32 unkn)
745 {
746         if (!sampass)
747                 return False;
748
749         sampass->private.unknown_3 = unkn;
750         return True;
751 }
752
753 BOOL pdb_set_unknown_5 (SAM_ACCOUNT *sampass, uint32 unkn)
754 {
755         if (!sampass)
756                 return False;
757
758         sampass->private.unknown_5 = unkn;
759         return True;
760 }
761
762 BOOL pdb_set_unknown_6 (SAM_ACCOUNT *sampass, uint32 unkn)
763 {
764         if (!sampass)
765                 return False;
766
767         sampass->private.unknown_6 = unkn;
768         return True;
769 }
770
771 BOOL pdb_set_hours (SAM_ACCOUNT *sampass, const uint8 *hours)
772 {
773         if (!sampass)
774                 return False;
775
776         if (!hours) {
777                 memset ((char *)sampass->private.hours, 0, MAX_HOURS_LEN);
778                 return True;
779         }
780         
781         memcpy (sampass->private.hours, hours, MAX_HOURS_LEN);
782
783         return True;
784 }
785
786
787 /* Helpful interfaces to the above */
788
789 /*********************************************************************
790  Sets the last changed times and must change times for a normal
791  password change.
792  ********************************************************************/
793
794 BOOL pdb_set_pass_changed_now (SAM_ACCOUNT *sampass)
795 {
796         uint32 expire;
797
798         if (!sampass)
799                 return False;
800         
801         if (!pdb_set_pass_last_set_time (sampass, time(NULL)))
802                 return False;
803
804         account_policy_get(AP_MAX_PASSWORD_AGE, &expire);
805
806         if (expire==(uint32)-1) {
807                 if (!pdb_set_pass_must_change_time (sampass, 0))
808                         return False;
809         } else {
810                 if (!pdb_set_pass_must_change_time (sampass, 
811                                             pdb_get_pass_last_set_time(sampass)
812                                             + expire))
813                         return False;
814         }
815         
816         return True;
817 }
818
819 /*********************************************************************
820  Set the user's PLAINTEXT password.  Used as an interface to the above.
821  Also sets the last change time to NOW.
822  ********************************************************************/
823
824 BOOL pdb_set_plaintext_passwd (SAM_ACCOUNT *sampass, const char *plaintext)
825 {
826         uchar new_lanman_p16[16];
827         uchar new_nt_p16[16];
828
829         if (!sampass || !plaintext)
830                 return False;
831         
832         nt_lm_owf_gen (plaintext, new_nt_p16, new_lanman_p16);
833
834         if (!pdb_set_nt_passwd (sampass, new_nt_p16)) 
835                 return False;
836
837         if (!pdb_set_lanman_passwd (sampass, new_lanman_p16)) 
838                 return False;
839         
840         if (!pdb_set_pass_changed_now (sampass))
841                 return False;
842
843         return True;
844 }
845