move to SAFE_FREE()
[ira/wip.git] / source3 / passdb / passdb.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Password and authentication handling
5    Copyright (C) Jeremy Allison                 1996-1998
6    Copyright (C) Luke Kenneth Casson Leighton   1996-1998
7    Copyright (C) Gerald (Jerry) Carter          2000-2001
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 extern int DEBUGLEVEL;
27
28 /*
29  * This is set on startup - it defines the SID for this
30  * machine, and therefore the SAM database for which it is
31  * responsible.
32  */
33
34 extern DOM_SID global_sam_sid;
35
36 struct passdb_ops *pdb_ops;
37
38 static void* pdb_handle = NULL;
39
40 /***************************************************************
41  Initialize the password db operations.
42 ***************************************************************/
43 BOOL initialize_password_db(BOOL reload)
44 {
45         /* This function is unfinished right now, so just 
46            ignore the details and always return True.  It is here 
47            only as a placeholder --jerry */
48         return True;
49
50 #if _NOT_YET_   
51         char*   modulename = lp_passdb_module_path();
52         
53         /* load another module? */
54         if (reload && pdb_handle)
55         {
56                 sys_dlclose (pdb_handle);
57                 pdb_handle = NULL;
58         }
59         
60         /* do we have a module defined or use the default? */
61         if (strlen (modulename) != 0)
62         {
63                 if ((pdb_handle=sys_dlopen (modulename, RTLD_LAZY)) == NULL)
64                 {
65                         DEBUG(0,("initialize_password_db: ERROR - Unable to open passdb module \"%s\"!\n%s\n",
66                                 modulename, sys_dlerror()));
67                 }
68                 else
69                         DEBUG(1,("initialize_password_db: passdb module \"%s\" loaded successfully\n", modulename));
70         }       
71         
72         /* either no module name defined or the one that was failed 
73            to open.  Let's try the default */
74         if (pdb_handle == NULL)
75         {
76                 if ((pdb_handle=sys_dlopen ("libpdbfile.so", RTLD_LAZY)) == NULL)
77                 {
78                         DEBUG(0,("initialize_password_db: ERROR - Unable to open \"libpdbfile.so\" passdb module!  No user authentication possible!\n%s\n",
79                                 sys_dlerror()));
80                         return False;
81                 }
82                 else
83                         DEBUG(1,("initialize_password_db: passdb module \"libpdbfile.so\" loaded successfully\n"));
84         }
85                                         
86
87         return (pdb_handle != NULL);
88 #endif
89 }
90
91 /*************************************************************
92  initialises a struct sam_disp_info.
93  **************************************************************/
94 static void pdb_init_dispinfo(struct sam_disp_info *user)
95 {
96         if (user == NULL) 
97                 return;
98         ZERO_STRUCTP(user);
99 }
100
101 /*************************************************************
102  alloc memory and initialises a struct sam_passwd.
103  ************************************************************/
104 BOOL pdb_init_sam(SAM_ACCOUNT **user)
105 {
106         if (*user != NULL) {
107                 DEBUG(0,("pdb_init_sam: SAM_ACCOUNT was non NULL\n"));
108 #if 0
109                 smb_panic("NULL pointer passed to pdb_init_sam\n");
110 #endif
111                 return False;
112         }
113         
114         *user=(SAM_ACCOUNT *)malloc(sizeof(SAM_ACCOUNT));
115         
116         if (*user==NULL) {
117                 DEBUG(0,("pdb_init_sam: error while allocating memory\n"));
118                 return False;
119         }
120         
121         ZERO_STRUCTP(*user);
122
123         (*user)->logon_time            = (time_t)0;
124         (*user)->logoff_time           = (time_t)-1;
125         (*user)->kickoff_time          = (time_t)-1;
126         (*user)->pass_last_set_time    = (time_t)-1;
127         (*user)->pass_can_change_time  = (time_t)-1;
128         (*user)->pass_must_change_time = (time_t)-1;
129
130         (*user)->unknown_3 = 0x00ffffff;        /* don't know */
131         (*user)->logon_divs = 168;      /* hours per week */
132         (*user)->hours_len = 21;                /* 21 times 8 bits = 168 */
133         memset((*user)->hours, 0xff, (*user)->hours_len); /* available at all hours */
134         (*user)->unknown_5 = 0x00000000; /* don't know */
135         (*user)->unknown_6 = 0x000004ec; /* don't know */
136         
137         return True;
138 }
139
140 /************************************************************
141  free the SAM_ACCOUNT and the NT/LM hashes.
142  ***********************************************************/
143 BOOL pdb_free_sam(SAM_ACCOUNT *user)
144 {
145         if (user == NULL) {
146                 DEBUG(0,("pdb_free_sam: SAM_ACCOUNT was NULL\n"));
147 #if 0
148                 smb_panic("NULL pointer passed to pdb_free_sam\n");
149 #endif
150                 return False;
151         }
152
153         SAFE_FREE(user->nt_pw);
154         SAFE_FREE(user->lm_pw);
155         SAFE_FREE(user);
156         
157         return True;    
158 }
159
160 /************************************************************
161  reset the SAM_ACCOUNT and the NT/LM hashes.
162  ***********************************************************/
163
164 BOOL pdb_reset_sam(SAM_ACCOUNT *user)
165 {
166         if (user == NULL) {
167                 DEBUG(0,("pdb_reset_sam: SAM_ACCOUNT was NULL\n"));
168                 return False;
169         }
170         
171         SAFE_FREE(user->nt_pw);
172         SAFE_FREE(user->lm_pw); 
173         ZERO_STRUCTP(user);
174
175         return True;
176 }
177
178 /*************************************************************************
179  Routine to return the next entry in the sam passwd list.
180  *************************************************************************/
181 struct sam_disp_info *pdb_sam_to_dispinfo(SAM_ACCOUNT *user)
182 {
183         static struct sam_disp_info disp_info;
184
185         if (user == NULL) 
186                 return NULL;
187
188         pdb_init_dispinfo(&disp_info);
189
190         disp_info.smb_name  = user->username;
191         disp_info.full_name = user->full_name;
192         disp_info.user_rid  = user->user_rid;
193
194         return &disp_info;
195 }
196
197 /*******************************************************************
198  Group and User RID username mapping function
199  ********************************************************************/
200 BOOL pdb_name_to_rid(char *user_name, uint32 *u_rid, uint32 *g_rid)
201 {
202         struct passwd *pw = Get_Pwnam(user_name, False);
203
204         if (u_rid == NULL || g_rid == NULL || user_name == NULL)
205         {
206                 return False;
207         }
208
209         if (!pw)
210         {
211                 DEBUG(1,("Username %s is invalid on this system\n", user_name));
212                 return False;
213         }
214
215         /* turn the unix UID into a Domain RID.  this is what the posix
216            sub-system does (adds 1000 to the uid) */
217         *u_rid = pdb_uid_to_user_rid(pw->pw_uid);
218
219         /* absolutely no idea what to do about the unix GID to Domain RID mapping */
220         *g_rid = pdb_gid_to_group_rid(pw->pw_gid);
221
222         return True;
223 }
224
225 /*******************************************************************
226  Converts NT user RID to a UNIX uid.
227  ********************************************************************/
228 uid_t pdb_user_rid_to_uid(uint32 user_rid)
229 {
230         return (uid_t)(((user_rid & (~USER_RID_TYPE))- 1000)/RID_MULTIPLIER);
231 }
232
233 /*******************************************************************
234  Converts NT user RID to a UNIX gid.
235  ********************************************************************/
236
237 gid_t pdb_user_rid_to_gid(uint32 user_rid)
238 {
239         return (uid_t)(((user_rid & (~GROUP_RID_TYPE))- 1000)/RID_MULTIPLIER);
240 }
241
242 /*******************************************************************
243  converts UNIX uid to an NT User RID.
244  ********************************************************************/
245
246 uint32 pdb_uid_to_user_rid(uid_t uid)
247 {
248         return (((((uint32)uid)*RID_MULTIPLIER) + 1000) | USER_RID_TYPE);
249 }
250
251 /*******************************************************************
252  converts NT Group RID to a UNIX uid.
253  ********************************************************************/
254
255 uint32 pdb_gid_to_group_rid(gid_t gid)
256 {
257   return (((((uint32)gid)*RID_MULTIPLIER) + 1000) | GROUP_RID_TYPE);
258 }
259
260 /*******************************************************************
261  Decides if a RID is a well known RID.
262  ********************************************************************/
263
264 static BOOL pdb_rid_is_well_known(uint32 rid)
265 {
266   return (rid < 1000);
267 }
268
269 /*******************************************************************
270  Decides if a RID is a user or group RID.
271  ********************************************************************/
272 BOOL pdb_rid_is_user(uint32 rid)
273 {
274   /* lkcl i understand that NT attaches an enumeration to a RID
275    * such that it can be identified as either a user, group etc
276    * type.  there are 5 such categories, and they are documented.
277    */
278    if(pdb_rid_is_well_known(rid)) {
279       /*
280        * The only well known user RIDs are DOMAIN_USER_RID_ADMIN
281        * and DOMAIN_USER_RID_GUEST.
282        */
283      if(rid == DOMAIN_USER_RID_ADMIN || rid == DOMAIN_USER_RID_GUEST)
284        return True;
285    } else if((rid & RID_TYPE_MASK) == USER_RID_TYPE) {
286      return True;
287    }
288    return False;
289 }
290
291 /*******************************************************************
292  Convert a rid into a name. Used in the lookup SID rpc.
293  ********************************************************************/
294 BOOL local_lookup_rid(uint32 rid, char *name, enum SID_NAME_USE *psid_name_use)
295 {
296
297         BOOL is_user = pdb_rid_is_user(rid);
298
299         DEBUG(5,("local_lookup_rid: looking up %s RID %u.\n", is_user ? "user" :
300                         "group", (unsigned int)rid));
301
302         if(is_user) {
303                 if(rid == DOMAIN_USER_RID_ADMIN) {
304                         pstring admin_users;
305                         char *p = admin_users;
306                         if(!next_token(&p, name, NULL, sizeof(fstring)))
307                                 fstrcpy(name, "Administrator");
308                 } else if (rid == DOMAIN_USER_RID_GUEST) {
309                         pstring guest_users;
310                         char *p = guest_users;
311                         if(!next_token(&p, name, NULL, sizeof(fstring)))
312                                 fstrcpy(name, "Guest");
313                 } else {
314                         uid_t uid;
315                         struct passwd *pass;
316                         
317                         /*
318                          * Don't try to convert the rid to a name if 
319                          * running in appliance mode
320                          */
321                         if (lp_hide_local_users())
322                                 return False;
323                         
324                         uid = pdb_user_rid_to_uid(rid);
325                         pass = sys_getpwuid(uid);
326
327                         *psid_name_use = SID_NAME_USER;
328
329                         DEBUG(5,("local_lookup_rid: looking up uid %u %s\n", (unsigned int)uid,
330                                 pass ? "succeeded" : "failed" ));
331
332                         if(!pass) {
333                                 slprintf(name, sizeof(fstring)-1, "unix_user.%u", (unsigned int)uid);
334                                 return True;
335                         }
336
337                         fstrcpy(name, pass->pw_name);
338
339                         DEBUG(5,("local_lookup_rid: found user %s for rid %u\n", name,
340                                 (unsigned int)rid ));
341                 }
342
343         } else {
344                 gid_t gid=-1;
345                 struct group *gr; 
346                 GROUP_MAP map;
347                 DOM_SID sid;
348                 /* 
349                  * Don't try to convert the rid to a name if running
350                  * in appliance mode
351                  */
352                 
353                 if (lp_hide_local_users()) 
354                         return False;
355                 
356                 /*
357                  * First try the TDB. If the RID exists and is mapped to a unix group,
358                  * return the NT name and the type.
359                  */
360                 
361                 sid_copy(&sid, &global_sam_sid);
362                 sid_append_rid(&sid, rid);
363                 if (get_group_map_from_sid(sid, &map) && map.gid!=-1) {
364                         *psid_name_use = map.sid_name_use;
365                         fstrcpy(name, map.nt_name);
366
367                         DEBUG(5,("local_lookup_rid: found NT group %s mapped to Unix gid %u for rid %u\n",
368                                   name, (unsigned int)map.gid, (unsigned int)rid ));
369
370                         if(!getgrgid(map.gid))
371                                 return False;
372                         else
373                                 return True;
374                 }
375
376                 *psid_name_use = SID_NAME_ALIAS;
377                 gid = pdb_user_rid_to_gid(rid);
378                 
379                 gr = getgrgid(gid);
380                 DEBUG(5,("local_local_rid: looking up gid %u %s\n", (unsigned int)gid,
381                         gr ? "succeeded" : "failed" ));
382
383                 if(!gr) {
384                         slprintf(name, sizeof(fstring)-1, "unix_group.%u", (unsigned int)gid);
385                         return True;
386                 }
387
388                 fstrcpy( name, gr->gr_name);
389
390                 DEBUG(5,("local_lookup_rid: found group %s for rid %u\n", name, (unsigned int)rid ));
391         }
392
393         return True;
394 }
395
396 /*******************************************************************
397  Convert a name into a SID. Used in the lookup name rpc.
398  ********************************************************************/
399
400 BOOL local_lookup_name(const char *c_domain, const char *c_user, DOM_SID *psid, enum SID_NAME_USE *psid_name_use)
401 {
402         extern DOM_SID global_sid_World_Domain;
403         struct passwd *pass = NULL;
404         DOM_SID local_sid;
405         fstring user;
406         fstring domain;
407
408         /*
409          * domain and user may be quoted const strings, and map_username and
410          * friends can modify them. Make a modifiable copy. JRA.
411          */
412
413         fstrcpy(domain, c_domain);
414         fstrcpy(user, c_user);
415
416         sid_copy(&local_sid, &global_sam_sid);
417
418         /*
419          * Special case for MACHINE\Everyone. Map to the world_sid.
420          */
421
422         if(strequal(user, "Everyone")) {
423                 sid_copy( psid, &global_sid_World_Domain);
424                 sid_append_rid(psid, 0);
425                 *psid_name_use = SID_NAME_ALIAS;
426                 return True;
427         }
428
429         /* 
430          * Don't lookup local unix users if running in appliance mode
431          */
432         if (lp_hide_local_users()) 
433                 return False;
434
435         (void)map_username(user);
436
437         if((pass = Get_Pwnam(user, True))) {
438                 sid_append_rid( &local_sid, pdb_uid_to_user_rid(pass->pw_uid));
439                 *psid_name_use = SID_NAME_USER;
440         } else {
441                 /*
442                  * Maybe it was a group ?
443                  */
444                 GROUP_MAP map;
445                 struct group *grp = NULL;
446
447                 /* It can be a mapped group */
448                 if (get_group_map_from_ntname(user, &map) && map.gid!=-1) {
449
450                         grp=getgrgid(map.gid);
451                         if (!grp)
452                                 return False;
453
454                         sid_copy(&local_sid, &map.sid);
455                         *psid_name_use = map.sid_name_use;
456                 } else {
457                         /* It wasn't mapped, it can be a Unix group */
458                         grp=getgrnam(user);
459                         if(!grp)
460                                 return False;
461
462                         sid_append_rid( &local_sid, pdb_gid_to_group_rid(grp->gr_gid));
463                         *psid_name_use = SID_NAME_ALIAS;
464                 }
465         }
466
467         sid_copy( psid, &local_sid);
468
469         return True;
470 }
471
472 /****************************************************************************
473  Convert a uid to SID - locally.
474 ****************************************************************************/
475 DOM_SID *local_uid_to_sid(DOM_SID *psid, uid_t uid)
476 {
477         extern DOM_SID global_sam_sid;
478
479         sid_copy(psid, &global_sam_sid);
480         sid_append_rid(psid, pdb_uid_to_user_rid(uid));
481
482         return psid;
483 }
484
485
486 /****************************************************************************
487  Convert a SID to uid - locally.
488 ****************************************************************************/
489 BOOL local_sid_to_uid(uid_t *puid, DOM_SID *psid, enum SID_NAME_USE *name_type)
490 {
491         extern DOM_SID global_sam_sid;
492
493         DOM_SID dom_sid;
494         uint32 rid;
495         fstring str;
496         struct passwd *pass;
497
498         *name_type = SID_NAME_UNKNOWN;
499
500         sid_copy(&dom_sid, psid);
501         sid_split_rid(&dom_sid, &rid);
502
503         if (!pdb_rid_is_user(rid))
504                 return False;
505
506         /*
507          * We can only convert to a uid if this is our local
508          * Domain SID (ie. we are the controling authority).
509          */
510         if (!sid_equal(&global_sam_sid, &dom_sid))
511                 return False;
512
513         *puid = pdb_user_rid_to_uid(rid);
514
515         /*
516          * Ensure this uid really does exist.
517          */
518         if(!(pass = sys_getpwuid(*puid)))
519                 return False;
520
521         DEBUG(10,("local_sid_to_uid: SID %s -> uid (%u) (%s).\n", sid_to_string( str, psid),
522                 (unsigned int)*puid, pass->pw_name ));
523
524         return True;
525 }
526
527 /****************************************************************************
528  Convert a gid to SID - locally.
529 ****************************************************************************/
530 DOM_SID *local_gid_to_sid(DOM_SID *psid, gid_t gid)
531 {
532     extern DOM_SID global_sam_sid;
533
534         sid_copy(psid, &global_sam_sid);
535         sid_append_rid(psid, pdb_gid_to_group_rid(gid));
536
537         return psid;
538 }
539
540 /****************************************************************************
541  Convert a SID to gid - locally.
542 ****************************************************************************/
543 BOOL local_sid_to_gid(gid_t *pgid, DOM_SID *psid, enum SID_NAME_USE *name_type)
544 {
545     extern DOM_SID global_sam_sid;
546         DOM_SID dom_sid;
547         uint32 rid;
548         fstring str;
549         struct group *grp;
550
551         *name_type = SID_NAME_UNKNOWN;
552
553         sid_copy(&dom_sid, psid);
554         sid_split_rid(&dom_sid, &rid);
555
556         /*
557          * We can only convert to a gid if this is our local
558          * Domain SID (ie. we are the controling authority).
559          */
560
561         if (!sid_equal(&global_sam_sid, &dom_sid))
562                 return False;
563
564         if (pdb_rid_is_user(rid))
565                 return False;
566
567         *pgid = pdb_user_rid_to_gid(rid);
568
569         /*
570          * Ensure this gid really does exist.
571          */
572
573         if(!(grp = getgrgid(*pgid)))
574                 return False;
575
576         DEBUG(10,("local_sid_to_gid: SID %s -> gid (%u) (%s).\n", sid_to_string( str, psid),
577                 (unsigned int)*pgid, grp->gr_name ));
578
579         return True;
580 }
581
582 static void select_name(pstring string, const UNISTR2 *from)
583 {
584         if (from->buffer != 0)
585                 unistr2_to_ascii(string, from, sizeof(string));
586 }
587
588 /*************************************************************
589  copies a SAM_USER_INFO_23 to a SAM_ACCOUNT
590  **************************************************************/
591 void copy_id23_to_sam_passwd(SAM_ACCOUNT *to, SAM_USER_INFO_23 *from)
592 {
593
594         if (from == NULL || to == NULL) 
595                 return;
596
597         to->logon_time = nt_time_to_unix(&from->logon_time);
598         to->logoff_time = nt_time_to_unix(&from->logoff_time);
599         to->kickoff_time = nt_time_to_unix(&from->kickoff_time);
600         to->pass_last_set_time = nt_time_to_unix(&from->pass_last_set_time);
601         to->pass_can_change_time = nt_time_to_unix(&from->pass_can_change_time);
602         to->pass_must_change_time = nt_time_to_unix(&from->pass_must_change_time);
603
604         select_name(to->username    , &from->uni_user_name   );
605         select_name(to->full_name   , &from->uni_full_name   );
606         select_name(to->home_dir    , &from->uni_home_dir    );
607         select_name(to->dir_drive   , &from->uni_dir_drive   );
608         select_name(to->logon_script, &from->uni_logon_script);
609         select_name(to->profile_path, &from->uni_profile_path);
610         select_name(to->acct_desc   , &from->uni_acct_desc   );
611         select_name(to->workstations, &from->uni_workstations);
612         select_name(to->unknown_str , &from->uni_unknown_str );
613         select_name(to->munged_dial , &from->uni_munged_dial );
614
615         to->user_rid = from->user_rid;
616         to->group_rid = from->group_rid;
617
618         to->acct_ctrl = from->acb_info;
619         to->unknown_3 = from->unknown_3;
620
621         to->logon_divs = from->logon_divs;
622         to->hours_len = from->logon_hrs.len;
623         memcpy(to->hours, from->logon_hrs.hours, MAX_HOURS_LEN);
624
625         to->unknown_5 = from->unknown_5;
626         to->unknown_6 = from->unknown_6;
627 }
628
629 /*************************************************************
630  copies a sam passwd.
631  **************************************************************/
632 void copy_id21_to_sam_passwd(SAM_ACCOUNT *to, SAM_USER_INFO_21 *from)
633 {
634         if (from == NULL || to == NULL) 
635                 return;
636
637         to->logon_time = nt_time_to_unix(&from->logon_time);
638         to->logoff_time = nt_time_to_unix(&from->logoff_time);
639         to->kickoff_time = nt_time_to_unix(&from->kickoff_time);
640         to->pass_last_set_time = nt_time_to_unix(&from->pass_last_set_time);
641         to->pass_can_change_time = nt_time_to_unix(&from->pass_can_change_time);
642         to->pass_must_change_time = nt_time_to_unix(&from->pass_must_change_time);
643
644         select_name(to->username    , &from->uni_user_name   );
645         select_name(to->full_name   , &from->uni_full_name   );
646         select_name(to->home_dir    , &from->uni_home_dir    );
647         select_name(to->dir_drive   , &from->uni_dir_drive   );
648         select_name(to->logon_script, &from->uni_logon_script);
649         select_name(to->profile_path, &from->uni_profile_path);
650         select_name(to->acct_desc   , &from->uni_acct_desc   );
651         select_name(to->workstations, &from->uni_workstations);
652         select_name(to->unknown_str , &from->uni_unknown_str );
653         select_name(to->munged_dial , &from->uni_munged_dial );
654
655         to->user_rid = from->user_rid;
656         to->group_rid = from->group_rid;
657         
658         /* FIXME!!  Do we need to copy the passwords here as well?
659            I don't know.  Need to figure this out   --jerry */
660
661         to->acct_ctrl = from->acb_info;
662         to->unknown_3 = from->unknown_3;
663
664         to->logon_divs = from->logon_divs;
665         to->hours_len = from->logon_hrs.len;
666         memcpy(to->hours, from->logon_hrs.hours, MAX_HOURS_LEN);
667
668         to->unknown_5 = from->unknown_5;
669         to->unknown_6 = from->unknown_6;
670 }
671
672
673 /*************************************************************
674  copies a sam passwd.
675  **************************************************************/
676 void copy_sam_passwd(SAM_ACCOUNT *to, const SAM_ACCOUNT *from)
677 {
678         if (!from || !to) return;
679
680         memcpy(to, from, sizeof(SAM_ACCOUNT));
681 }
682
683 /*************************************************************
684  change a password entry in the local smbpasswd file
685
686  FIXME!!  The function needs to be abstracted into the
687  passdb interface or something.  It is currently being called
688  by _api_samr_create_user() in rpc_server/srv_samr.c
689  
690  --jerry
691  *************************************************************/
692
693 BOOL local_password_change(char *user_name, int local_flags,
694                            char *new_passwd, 
695                            char *err_str, size_t err_str_len,
696                            char *msg_str, size_t msg_str_len)
697 {
698         struct passwd  *pwd = NULL;
699         SAM_ACCOUNT     *sam_pass=NULL;
700         SAM_ACCOUNT     *new_sam_acct=NULL;
701         uchar           new_p16[16];
702         uchar           new_nt_p16[16];
703
704         *err_str = '\0';
705         *msg_str = '\0';
706
707         if (local_flags & LOCAL_ADD_USER) {
708         
709                 /*
710                  * Check for a local account - if we're adding only.
711                  */
712         
713                 if(!(pwd = sys_getpwnam(user_name))) {
714                         slprintf(err_str, err_str_len - 1, "User %s does not \
715 exist in system password file (usually /etc/passwd). Cannot add \
716 account without a valid local system user.\n", user_name);
717                         return False;
718                 }
719         }
720
721         /* Calculate the MD4 hash (NT compatible) of the new password. */
722         nt_lm_owf_gen(new_passwd, new_nt_p16, new_p16);
723
724         /* Get the smb passwd entry for this user */
725         pdb_init_sam(&sam_pass);
726         if(!pdb_getsampwnam(sam_pass, user_name))
727         {
728                 pdb_free_sam(sam_pass);
729                 
730                 if(!(local_flags & LOCAL_ADD_USER)) {
731                         slprintf(err_str, err_str_len-1,"Failed to find entry for user %s.\n", user_name);
732                         return False;
733                 }
734
735                 /* create the SAM_ACCOUNT struct and call pdb_add_sam_account.
736                    Because the new_sam_pwd only exists in the scope of this function
737                    we will not allocate memory for members */
738                 pdb_init_sam(&new_sam_acct);
739                 pdb_set_username(new_sam_acct, user_name);
740                 pdb_set_fullname(new_sam_acct, pwd->pw_gecos);
741                 pdb_set_uid(new_sam_acct, pwd->pw_uid);
742                 pdb_set_gid(new_sam_acct, pwd->pw_gid);
743                 pdb_set_pass_last_set_time(new_sam_acct, time(NULL));
744                 pdb_set_profile_path(new_sam_acct, lp_logon_path());
745                 pdb_set_homedir(new_sam_acct, lp_logon_home());
746                 pdb_set_dir_drive(new_sam_acct, lp_logon_drive());
747                 pdb_set_logon_script(new_sam_acct, lp_logon_script());
748
749                 /* set account flags */
750                 pdb_set_acct_ctrl(new_sam_acct,((local_flags & LOCAL_TRUST_ACCOUNT) ? ACB_WSTRUST : ACB_NORMAL) );
751
752                 if (local_flags & LOCAL_DISABLE_USER)
753                         pdb_set_acct_ctrl (new_sam_acct, pdb_get_acct_ctrl(new_sam_acct)|ACB_DISABLED);
754
755                 if (local_flags & LOCAL_SET_NO_PASSWORD)
756                         pdb_set_acct_ctrl (new_sam_acct, pdb_get_acct_ctrl(new_sam_acct)|ACB_PWNOTREQ);
757                 else {
758                         /* set the passwords here.  if we get to here it means
759                            we have a valid, active account */
760                         pdb_set_lanman_passwd (new_sam_acct, new_p16);
761                         pdb_set_nt_passwd     (new_sam_acct, new_nt_p16);
762                 }
763
764                 if (pdb_add_sam_account(new_sam_acct)) {
765                         slprintf(msg_str, msg_str_len-1, "Added user %s.\n", user_name);
766                         pdb_free_sam(new_sam_acct);
767                         return True;
768                 } else {
769                         slprintf(err_str, err_str_len-1, "Failed to add entry for user %s.\n", user_name);
770                         pdb_free_sam(new_sam_acct);
771                         return False;
772                 }
773         } else {
774                 /* the entry already existed */
775                 local_flags &= ~LOCAL_ADD_USER;
776         }
777
778         /*
779          * We are root - just write the new password
780          * and the valid last change time.
781          */
782
783         if(local_flags & LOCAL_DISABLE_USER) 
784                 pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_DISABLED);
785         else if (local_flags & LOCAL_ENABLE_USER) 
786                 {
787                 if(pdb_get_lanman_passwd(sam_pass) == NULL) {
788                         pdb_set_lanman_passwd (sam_pass, new_p16);
789                         pdb_set_nt_passwd     (sam_pass, new_nt_p16);
790                 }
791                 pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED));
792         }
793         else if (local_flags & LOCAL_SET_NO_PASSWORD) {
794                 pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_PWNOTREQ);
795                 
796                 /* This is needed to preserve ACB_PWNOTREQ in mod_smbfilepwd_entry */
797                 pdb_set_lanman_passwd (sam_pass, NULL);
798                 pdb_set_nt_passwd     (sam_pass, NULL);
799         } 
800         else 
801         {
802                 /*
803                  * If we're dealing with setting a completely empty user account
804                  * ie. One with a password of 'XXXX', but not set disabled (like
805                  * an account created from scratch) then if the old password was
806                  * 'XX's then getsmbpwent will have set the ACB_DISABLED flag.
807                  * We remove that as we're giving this user their first password
808                  * and the decision hasn't really been made to disable them (ie.
809                  * don't create them disabled). JRA.
810                  */
811                 if ((pdb_get_lanman_passwd(sam_pass)==NULL) && (pdb_get_acct_ctrl(sam_pass)&ACB_DISABLED))
812                         pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED));
813                 pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_PWNOTREQ));
814                 pdb_set_lanman_passwd (sam_pass, new_p16);
815                 pdb_set_nt_passwd     (sam_pass, new_nt_p16);
816         }
817         
818         if(local_flags & LOCAL_DELETE_USER) {
819                 if (!pdb_delete_sam_account(user_name)) {
820                         slprintf(err_str,err_str_len-1, "Failed to delete entry for user %s.\n", user_name);
821                         pdb_free_sam(sam_pass);
822                         return False;
823                 }
824                 slprintf(msg_str, msg_str_len-1, "Deleted user %s.\n", user_name);
825         } 
826         else 
827         {
828                 if(!pdb_update_sam_account(sam_pass, True)) {
829                         slprintf(err_str, err_str_len-1, "Failed to modify entry for user %s.\n", user_name);
830                         pdb_free_sam(sam_pass);
831                         return False;
832                 }
833                 if(local_flags & LOCAL_DISABLE_USER)
834                         slprintf(msg_str, msg_str_len-1, "Disabled user %s.\n", user_name);
835                 else if (local_flags & LOCAL_ENABLE_USER)
836                         slprintf(msg_str, msg_str_len-1, "Enabled user %s.\n", user_name);
837                 else if (local_flags & LOCAL_SET_NO_PASSWORD)
838                         slprintf(msg_str, msg_str_len-1, "User %s password set to none.\n", user_name);
839         }
840
841         pdb_free_sam(sam_pass);
842         return True;
843 }
844
845
846 /*********************************************************************
847  collection of get...() functions for SAM_ACCOUNT_INFO
848  ********************************************************************/
849 uint16 pdb_get_acct_ctrl (SAM_ACCOUNT *sampass)
850 {
851         if (sampass)
852                 return (sampass->acct_ctrl);
853         else
854                 return (ACB_DISABLED);
855 }
856
857 time_t pdb_get_logon_time (SAM_ACCOUNT *sampass)
858 {
859         if (sampass)
860                 return (sampass->logon_time);
861         else
862                 return (0);
863 }
864
865 time_t pdb_get_logoff_time (SAM_ACCOUNT *sampass)
866 {
867         if (sampass)
868                 return (sampass->logoff_time);
869         else
870                 return (-1);
871 }
872
873 time_t pdb_get_kickoff_time (SAM_ACCOUNT *sampass)
874 {
875         if (sampass)
876                 return (sampass->kickoff_time);
877         else
878                 return (-1);
879 }
880
881 time_t pdb_get_pass_last_set_time (SAM_ACCOUNT *sampass)
882 {
883         if (sampass)
884                 return (sampass->pass_last_set_time);
885         else
886                 return (-1);
887 }
888
889 time_t pdb_get_pass_can_change_time (SAM_ACCOUNT *sampass)
890 {
891         if (sampass)
892                 return (sampass->pass_can_change_time);
893         else
894                 return (-1);
895 }
896
897 time_t pdb_get_pass_must_change_time (SAM_ACCOUNT *sampass)
898 {
899         if (sampass)
900                 return (sampass->pass_must_change_time);
901         else
902                 return (-1);
903 }
904
905 uint16 pdb_get_logon_divs (SAM_ACCOUNT *sampass)
906 {
907         if (sampass)
908                 return (sampass->logon_divs);
909         else
910                 return (-1);
911 }
912
913 uint32 pdb_get_hours_len (SAM_ACCOUNT *sampass)
914 {
915         if (sampass)
916                 return (sampass->hours_len);
917         else
918                 return (-1);
919 }
920
921 uint8* pdb_get_hours (SAM_ACCOUNT *sampass)
922 {
923         if (sampass)
924                 return (sampass->hours);
925         else
926                 return (NULL);
927 }
928
929 uint8* pdb_get_nt_passwd (SAM_ACCOUNT *sampass)
930 {
931         if (sampass)
932                 return (sampass->nt_pw);
933         else
934                 return (NULL);
935 }
936
937 uint8* pdb_get_lanman_passwd (SAM_ACCOUNT *sampass)
938 {
939         if (sampass)
940                 return (sampass->lm_pw);
941         else
942                 return (NULL);
943 }
944
945
946 uint32 pdb_get_user_rid (SAM_ACCOUNT *sampass)
947 {
948         if (sampass)
949                 return (sampass->user_rid);
950         else
951                 return (-1);
952 }
953
954 uint32 pdb_get_group_rid (SAM_ACCOUNT *sampass)
955 {
956         if (sampass)
957                 return (sampass->group_rid);
958         else
959                 return (-1);
960 }
961
962 uid_t pdb_get_uid (SAM_ACCOUNT *sampass)
963 {
964         if (sampass)
965                 return (sampass->uid);
966         else
967                 return ((uid_t)-1);
968 }
969
970 gid_t pdb_get_gid (SAM_ACCOUNT *sampass)
971 {
972         if (sampass)
973                 return (sampass->gid);
974         else
975                 return ((gid_t)-1);
976 }
977
978 char* pdb_get_username (SAM_ACCOUNT *sampass)
979 {
980         if (sampass)
981                 return (sampass->username);
982         else
983                 return (NULL);
984 }
985
986 char* pdb_get_domain (SAM_ACCOUNT *sampass)
987 {
988         if (sampass)
989                 return (sampass->domain);
990         else
991                 return (NULL);
992 }
993
994 char* pdb_get_nt_username (SAM_ACCOUNT *sampass)
995 {
996         if (sampass)
997                 return (sampass->nt_username);
998         else
999                 return (NULL);
1000 }
1001
1002 char* pdb_get_fullname (SAM_ACCOUNT *sampass)
1003 {
1004         if (sampass)
1005                 return (sampass->full_name);
1006         else
1007                 return (NULL);
1008 }
1009
1010 char* pdb_get_homedir (SAM_ACCOUNT *sampass)
1011 {
1012         if (sampass)
1013                 return (sampass->home_dir);
1014         else
1015                 return (NULL);
1016 }
1017
1018 char* pdb_get_dirdrive (SAM_ACCOUNT *sampass)
1019 {
1020         if (sampass)
1021                 return (sampass->dir_drive);
1022         else
1023                 return (NULL);
1024 }
1025
1026 char* pdb_get_logon_script (SAM_ACCOUNT *sampass)
1027 {
1028         if (sampass)
1029                 return (sampass->logon_script);
1030         else
1031                 return (NULL);
1032 }
1033
1034 char* pdb_get_profile_path (SAM_ACCOUNT *sampass)
1035 {
1036         if (sampass)
1037                 return (sampass->profile_path);
1038         else
1039                 return (NULL);
1040 }
1041
1042 char* pdb_get_acct_desc (SAM_ACCOUNT *sampass)
1043 {
1044         if (sampass)
1045                 return (sampass->acct_desc);
1046         else
1047                 return (NULL);
1048 }
1049
1050 char* pdb_get_workstations (SAM_ACCOUNT *sampass)
1051 {
1052         if (sampass)
1053                 return (sampass->workstations);
1054         else
1055                 return (NULL);
1056 }
1057
1058 char* pdb_get_munged_dial (SAM_ACCOUNT *sampass)
1059 {
1060         if (sampass)
1061                 return (sampass->munged_dial);
1062         else
1063                 return (NULL);
1064 }
1065
1066 uint32 pdb_get_unknown3 (SAM_ACCOUNT *sampass)
1067 {
1068         if (sampass)
1069                 return (sampass->unknown_3);
1070         else
1071                 return (-1);
1072 }
1073
1074 uint32 pdb_get_unknown5 (SAM_ACCOUNT *sampass)
1075 {
1076         if (sampass)
1077                 return (sampass->unknown_5);
1078         else
1079                 return (-1);
1080 }
1081
1082 uint32 pdb_get_unknown6 (SAM_ACCOUNT *sampass)
1083 {
1084         if (sampass)
1085                 return (sampass->unknown_6);
1086         else
1087                 return (-1);
1088 }
1089
1090 /*********************************************************************
1091  collection of set...() functions for SAM_ACCOUNT_INFO
1092  ********************************************************************/
1093 BOOL pdb_set_acct_ctrl (SAM_ACCOUNT *sampass, uint16 flags)
1094 {
1095         if (!sampass)
1096                 return False;
1097                 
1098         if (sampass)
1099         {
1100                 sampass->acct_ctrl = flags;
1101                 return True;
1102         }
1103         
1104         return False;
1105 }
1106
1107 BOOL pdb_set_logon_time (SAM_ACCOUNT *sampass, time_t mytime)
1108 {
1109         if (!sampass)
1110                 return False;
1111
1112         sampass->logon_time = mytime;
1113         return True;
1114 }
1115
1116 BOOL pdb_set_logoff_time (SAM_ACCOUNT *sampass, time_t mytime)
1117 {
1118         if (!sampass)
1119                 return False;
1120
1121         sampass->logoff_time = mytime;
1122         return True;
1123 }
1124
1125 BOOL pdb_set_kickoff_time (SAM_ACCOUNT *sampass, time_t mytime)
1126 {
1127         if (!sampass)
1128                 return False;
1129
1130         sampass->kickoff_time = mytime;
1131         return True;
1132 }
1133
1134 BOOL pdb_set_pass_can_change_time (SAM_ACCOUNT *sampass, time_t mytime)
1135 {
1136         if (!sampass)
1137                 return False;
1138
1139         sampass->pass_can_change_time = mytime;
1140         return True;
1141 }
1142
1143 BOOL pdb_set_pass_must_change_time (SAM_ACCOUNT *sampass, time_t mytime)
1144 {
1145         if (!sampass)
1146                 return False;
1147
1148         sampass->pass_must_change_time = mytime;
1149         return True;
1150 }
1151
1152 BOOL pdb_set_pass_last_set_time (SAM_ACCOUNT *sampass, time_t mytime)
1153 {
1154         if (!sampass)
1155                 return False;
1156
1157         sampass->pass_last_set_time = mytime;
1158         return True;
1159 }
1160
1161 BOOL pdb_set_hours_len (SAM_ACCOUNT *sampass, uint32 len)
1162 {
1163         if (!sampass)
1164                 return False;
1165
1166         sampass->hours_len = len;
1167         return True;
1168 }
1169
1170 BOOL pdb_set_logons_divs (SAM_ACCOUNT *sampass, uint16 hours)
1171 {
1172         if (!sampass)
1173                 return False;
1174
1175         sampass->logon_divs = hours;
1176         return True;
1177 }
1178
1179 BOOL pdb_set_uid (SAM_ACCOUNT *sampass, uid_t uid)
1180 {
1181         if (!sampass)
1182                 return False;
1183
1184         sampass->uid = uid;
1185         return True;
1186 }
1187
1188 BOOL pdb_set_gid (SAM_ACCOUNT *sampass, gid_t gid)
1189 {
1190         if (!sampass)
1191                 return False;
1192
1193         sampass->gid = gid;
1194         return True;
1195 }
1196
1197 BOOL pdb_set_user_rid (SAM_ACCOUNT *sampass, uint32 rid)
1198 {
1199         if (!sampass)
1200                 return False;
1201
1202         sampass->user_rid = rid;
1203         return True;
1204 }
1205
1206 BOOL pdb_set_group_rid (SAM_ACCOUNT *sampass, uint32 grid)
1207 {
1208         if (!sampass)
1209                 return False;
1210
1211         sampass->group_rid = grid;
1212         return True;
1213 }
1214
1215 /*********************************************************************
1216  set the user's UNIX name
1217  ********************************************************************/
1218 BOOL pdb_set_username(SAM_ACCOUNT *sampass, char *username)
1219 {       
1220         if (!sampass || !username)
1221                 return False;
1222
1223         StrnCpy (sampass->username, username, strlen(username));
1224
1225         return True;
1226 }
1227
1228 /*********************************************************************
1229  set the domain name
1230  ********************************************************************/
1231 BOOL pdb_set_domain(SAM_ACCOUNT *sampass, char *domain)
1232 {       
1233         if (!sampass || !domain)
1234                 return False;
1235
1236         StrnCpy (sampass->domain, domain, strlen(domain));
1237
1238         return True;
1239 }
1240
1241 /*********************************************************************
1242  set the user's NT name
1243  ********************************************************************/
1244 BOOL pdb_set_nt_username(SAM_ACCOUNT *sampass, char *nt_username)
1245 {
1246         if (!sampass || !nt_username)
1247                 return False;
1248
1249         StrnCpy (sampass->nt_username, nt_username, strlen(nt_username));
1250
1251         return True;
1252 }
1253
1254 /*********************************************************************
1255  set the user's full name
1256  ********************************************************************/
1257 BOOL pdb_set_fullname(SAM_ACCOUNT *sampass, char *fullname)
1258 {
1259         if (!sampass || !fullname)
1260                 return False;
1261
1262         StrnCpy (sampass->full_name, fullname, strlen(fullname));
1263
1264         return True;
1265 }
1266
1267 /*********************************************************************
1268  set the user's logon script
1269  ********************************************************************/
1270 BOOL pdb_set_logon_script(SAM_ACCOUNT *sampass, char *logon_script)
1271 {
1272         if (!sampass || !logon_script)
1273                 return False;
1274
1275         StrnCpy (sampass->logon_script, logon_script, strlen(logon_script));
1276
1277         return True;
1278 }
1279
1280 /*********************************************************************
1281  set the user's profile path
1282  ********************************************************************/
1283 BOOL pdb_set_profile_path (SAM_ACCOUNT *sampass, char *profile_path)
1284 {
1285         if (!sampass || !profile_path)
1286                 return False;
1287         
1288         StrnCpy (sampass->profile_path, profile_path, strlen(profile_path));
1289         
1290         return True;
1291 }
1292
1293 /*********************************************************************
1294  set the user's directory drive
1295  ********************************************************************/
1296 BOOL pdb_set_dir_drive (SAM_ACCOUNT *sampass, char *dir_drive)
1297 {
1298         if (!sampass || !dir_drive)
1299                 return False;
1300
1301         StrnCpy (sampass->dir_drive, dir_drive, strlen(dir_drive));
1302
1303         return True;
1304 }
1305
1306 /*********************************************************************
1307  set the user's home directory
1308  ********************************************************************/
1309 BOOL pdb_set_homedir (SAM_ACCOUNT *sampass, char *homedir)
1310 {
1311         if (!sampass || !homedir)
1312                 return False;
1313         
1314         StrnCpy (sampass->home_dir, homedir, strlen(homedir));
1315
1316         return True;
1317 }
1318
1319 /*********************************************************************
1320  set the user's account description
1321  ********************************************************************/
1322 BOOL pdb_set_acct_desc (SAM_ACCOUNT *sampass, char *acct_desc)
1323 {
1324         if (!sampass || !acct_desc)
1325                 return False;
1326         
1327         StrnCpy (sampass->acct_desc, acct_desc, strlen(acct_desc));
1328
1329         return True;
1330 }
1331
1332 /*********************************************************************
1333  set the user's workstation allowed list
1334  ********************************************************************/
1335 BOOL pdb_set_workstations (SAM_ACCOUNT *sampass, char *workstations)
1336 {
1337         if (!sampass || !workstations) return False;
1338
1339         StrnCpy (sampass->workstations, workstations, strlen(workstations));
1340
1341         return True;
1342 }
1343
1344 /*********************************************************************
1345  set the user's dial string
1346  ********************************************************************/
1347 BOOL pdb_set_munged_dial (SAM_ACCOUNT *sampass, char *munged_dial)
1348 {
1349         if (!sampass || !munged_dial) return False;
1350
1351         StrnCpy (sampass->munged_dial, munged_dial, strlen(munged_dial));
1352
1353         return True;
1354 }
1355
1356 /*********************************************************************
1357  set the user's NT hash
1358  ********************************************************************/
1359 BOOL pdb_set_nt_passwd (SAM_ACCOUNT *sampass, uint8 *pwd)
1360 {
1361         if (!sampass || !pwd) return False;
1362         
1363         if (sampass->nt_pw!=NULL)
1364                 DEBUG(0,("pdb_set_nt_passwd: NT hash non NULL overwriting ?\n"));
1365         else
1366                 sampass->nt_pw=(unsigned char *)malloc(sizeof(unsigned char)*16);
1367         
1368         if (sampass->nt_pw==NULL)
1369                 return False;
1370
1371         memcpy (sampass->nt_pw, pwd, 16);
1372
1373         return True;
1374 }
1375
1376 /*********************************************************************
1377  set the user's LM hash
1378  ********************************************************************/
1379 BOOL pdb_set_lanman_passwd (SAM_ACCOUNT *sampass, uint8 *pwd)
1380 {
1381         if (!sampass || !pwd) return False;
1382         
1383         if (sampass->lm_pw!=NULL)
1384                 DEBUG(0,("pdb_set_lanman_passwd: LM hash non NULL overwriting ?\n"));
1385         else
1386                 sampass->lm_pw=(unsigned char *)malloc(sizeof(unsigned char)*16);
1387         
1388         if (sampass->lm_pw==NULL)
1389                 return False;
1390
1391         memcpy (sampass->lm_pw, pwd, 16);
1392
1393         return True;
1394 }
1395
1396 BOOL pdb_set_unknown_3 (SAM_ACCOUNT *sampass, uint32 unkn)
1397 {
1398         if (!sampass)
1399                 return False;
1400
1401         sampass->unknown_3 = unkn;
1402         return True;
1403 }
1404
1405 BOOL pdb_set_unknown_5 (SAM_ACCOUNT *sampass, uint32 unkn)
1406 {
1407         if (!sampass)
1408                 return False;
1409
1410         sampass->unknown_5 = unkn;
1411         return True;
1412 }
1413
1414 BOOL pdb_set_unknown_6 (SAM_ACCOUNT *sampass, uint32 unkn)
1415 {
1416         if (!sampass)
1417                 return False;
1418
1419         sampass->unknown_6 = unkn;
1420         return True;
1421 }
1422
1423 BOOL pdb_set_hours (SAM_ACCOUNT *sampass, uint8 *hours)
1424 {
1425         if (!sampass) return False;
1426
1427         if (!hours) 
1428         {
1429                 memset ((char *)sampass->hours, 0, MAX_HOURS_LEN);
1430                 return True;
1431         }
1432         
1433         memcpy (sampass->hours, hours, MAX_HOURS_LEN);
1434
1435         return True;
1436 }
1437