6636285b6f7b256c8a26141e129a0f9bdd769ab2
[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
46         char*   modulename = lp_passdb_module_path();
47         
48         /* This function is unfinished right now, so just 
49            ignore the details and always return True.  It is here 
50            only as a placeholder --jerry */
51         return True;
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, 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                                 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 }
89
90 /*************************************************************
91  initialises a struct sam_disp_info.
92  **************************************************************/
93 static void pdb_init_dispinfo(struct sam_disp_info *user)
94 {
95         if (user == NULL) 
96                 return;
97         ZERO_STRUCTP(user);
98 }
99
100 /*************************************************************
101  initialises a struct sam_passwd.
102  ************************************************************/
103 void pdb_init_sam(SAM_ACCOUNT *user)
104 {
105         if (user == NULL) 
106                 return;
107         
108         ZERO_STRUCTP(user);
109
110         user->mem_ctx = talloc_init();
111         DEBUG(10, ("pdb_init_sam: obtained a talloc context of 0x%x\n", user->mem_ctx));
112
113         user->logon_time            = (time_t)0;
114         user->logoff_time           = (time_t)-1;
115         user->kickoff_time          = (time_t)-1;
116         user->pass_last_set_time    = (time_t)-1;
117         user->pass_can_change_time  = (time_t)-1;
118         user->pass_must_change_time = (time_t)-1;
119
120         user->unknown_3 = 0x00ffffff;   /* don't know */
121         user->logon_divs = 168;         /* hours per week */
122         user->hours_len = 21;           /* 21 times 8 bits = 168 */
123         memset(user->hours, 0xff, user->hours_len); /* available at all hours */
124         user->unknown_5 = 0x00000000; /* don't know */
125         user->unknown_6 = 0x000004ec; /* don't know */
126 }
127
128 /************************************************************
129  free all pointer members and then reinit the SAM_ACCOUNT
130  ***********************************************************/
131 void pdb_clear_sam(SAM_ACCOUNT *user)
132 {
133         if (user == NULL)
134                 return;
135
136         /* free upany memory used */
137         DEBUG(10, ("pdb_clear_sam: releasing memory.  talloc context is 0x%x\n",user->mem_ctx));
138         talloc_destroy (user->mem_ctx);
139                 
140         /* now initialize */
141         pdb_init_sam(user);
142         
143 }
144
145
146 /*************************************************************************
147  Routine to return the next entry in the sam passwd list.
148  *************************************************************************/
149 struct sam_disp_info *pdb_sam_to_dispinfo(SAM_ACCOUNT *user)
150 {
151         static struct sam_disp_info disp_info;
152
153         if (user == NULL) 
154                 return NULL;
155
156         pdb_init_dispinfo(&disp_info);
157
158         disp_info.smb_name  = user->username;
159         disp_info.full_name = user->full_name;
160         disp_info.user_rid  = user->user_rid;
161
162         return &disp_info;
163 }
164
165
166 /**********************************************************
167  Encode the account control bits into a string.
168  length = length of string to encode into (including terminating
169  null). length *MUST BE MORE THAN 2* !
170  **********************************************************/
171 char *pdb_encode_acct_ctrl(uint16 acct_ctrl, size_t length)
172 {
173         static fstring acct_str;
174         size_t i = 0;
175
176         acct_str[i++] = '[';
177
178         if (acct_ctrl & ACB_PWNOTREQ ) acct_str[i++] = 'N';
179         if (acct_ctrl & ACB_DISABLED ) acct_str[i++] = 'D';
180         if (acct_ctrl & ACB_HOMDIRREQ) acct_str[i++] = 'H';
181         if (acct_ctrl & ACB_TEMPDUP  ) acct_str[i++] = 'T'; 
182         if (acct_ctrl & ACB_NORMAL   ) acct_str[i++] = 'U';
183         if (acct_ctrl & ACB_MNS      ) acct_str[i++] = 'M';
184         if (acct_ctrl & ACB_WSTRUST  ) acct_str[i++] = 'W';
185         if (acct_ctrl & ACB_SVRTRUST ) acct_str[i++] = 'S';
186         if (acct_ctrl & ACB_AUTOLOCK ) acct_str[i++] = 'L';
187         if (acct_ctrl & ACB_PWNOEXP  ) acct_str[i++] = 'X';
188         if (acct_ctrl & ACB_DOMTRUST ) acct_str[i++] = 'I';
189
190         for ( ; i < length - 2 ; i++ ) { acct_str[i] = ' '; }
191
192         i = length - 2;
193         acct_str[i++] = ']';
194         acct_str[i++] = '\0';
195
196         return acct_str;
197 }     
198
199 /**********************************************************
200  Decode the account control bits from a string.
201
202  this function breaks coding standards minimum line width of 80 chars.
203  reason: vertical line-up code clarity - all case statements fit into
204  15 lines, which is more important.
205  **********************************************************/
206
207 uint16 pdb_decode_acct_ctrl(const char *p)
208 {
209         uint16 acct_ctrl = 0;
210         BOOL finished = False;
211
212         /*
213          * Check if the account type bits have been encoded after the
214          * NT password (in the form [NDHTUWSLXI]).
215          */
216
217         if (*p != '[') return 0;
218
219         for (p++; *p && !finished; p++)
220         {
221                 switch (*p)
222                 {
223                         case 'N': { acct_ctrl |= ACB_PWNOTREQ ; break; /* 'N'o password. */ }
224                         case 'D': { acct_ctrl |= ACB_DISABLED ; break; /* 'D'isabled. */ }
225                         case 'H': { acct_ctrl |= ACB_HOMDIRREQ; break; /* 'H'omedir required. */ }
226                         case 'T': { acct_ctrl |= ACB_TEMPDUP  ; break; /* 'T'emp account. */ } 
227                         case 'U': { acct_ctrl |= ACB_NORMAL   ; break; /* 'U'ser account (normal). */ } 
228                         case 'M': { acct_ctrl |= ACB_MNS      ; break; /* 'M'NS logon user account. What is this ? */ } 
229                         case 'W': { acct_ctrl |= ACB_WSTRUST  ; break; /* 'W'orkstation account. */ } 
230                         case 'S': { acct_ctrl |= ACB_SVRTRUST ; break; /* 'S'erver account. */ } 
231                         case 'L': { acct_ctrl |= ACB_AUTOLOCK ; break; /* 'L'ocked account. */ } 
232                         case 'X': { acct_ctrl |= ACB_PWNOEXP  ; break; /* No 'X'piry on password */ } 
233                         case 'I': { acct_ctrl |= ACB_DOMTRUST ; break; /* 'I'nterdomain trust account. */ }
234             case ' ': { break; }
235                         case ':':
236                         case '\n':
237                         case '\0': 
238                         case ']':
239                         default:  { finished = True; }
240                 }
241         }
242
243         return acct_ctrl;
244 }
245
246 /*************************************************************
247  Routine to set 32 hex password characters from a 16 byte array.
248 **************************************************************/
249 void pdb_sethexpwd(char *p, unsigned char *pwd, uint16 acct_ctrl)
250 {
251         if (pwd != NULL) {
252                 int i;
253                 for (i = 0; i < 16; i++)
254                         slprintf(&p[i*2], 3, "%02X", pwd[i]);
255         } else {
256                 if (acct_ctrl & ACB_PWNOTREQ)
257                         safe_strcpy(p, "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX", 33);
258                 else
259                         safe_strcpy(p, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 33);
260         }
261 }
262
263 /*************************************************************
264  Routine to get the 32 hex characters and turn them
265  into a 16 byte array.
266 **************************************************************/
267 BOOL pdb_gethexpwd(char *p, unsigned char *pwd)
268 {
269         int i;
270         unsigned char   lonybble, hinybble;
271         char           *hexchars = "0123456789ABCDEF";
272         char           *p1, *p2;
273         
274         if (!p) return (False);
275         
276         for (i = 0; i < 32; i += 2)
277         {
278                 hinybble = toupper(p[i]);
279                 lonybble = toupper(p[i + 1]);
280
281                 p1 = strchr(hexchars, hinybble);
282                 p2 = strchr(hexchars, lonybble);
283
284                 if (!p1 || !p2)
285                 {
286                         return (False);
287                 }
288
289                 hinybble = PTR_DIFF(p1, hexchars);
290                 lonybble = PTR_DIFF(p2, hexchars);
291
292                 pwd[i / 2] = (hinybble << 4) | lonybble;
293         }
294         return (True);
295 }
296
297 /*******************************************************************
298  Group and User RID username mapping function
299  ********************************************************************/
300 BOOL pdb_name_to_rid(char *user_name, uint32 *u_rid, uint32 *g_rid)
301 {
302         struct passwd *pw = Get_Pwnam(user_name, False);
303
304         if (u_rid == NULL || g_rid == NULL || user_name == NULL)
305         {
306                 return False;
307         }
308
309         if (!pw)
310         {
311                 DEBUG(1,("Username %s is invalid on this system\n", user_name));
312                 return False;
313         }
314
315         if (user_in_list(user_name, lp_domain_guest_users()))
316         {
317                 *u_rid = DOMAIN_USER_RID_GUEST;
318         }
319         else if (user_in_list(user_name, lp_domain_admin_users()))
320         {
321                 *u_rid = DOMAIN_USER_RID_ADMIN;
322         }
323         else
324         {
325                 /* turn the unix UID into a Domain RID.  this is what the posix
326                    sub-system does (adds 1000 to the uid) */
327                 *u_rid = pdb_uid_to_user_rid(pw->pw_uid);
328         }
329
330         /* absolutely no idea what to do about the unix GID to Domain RID mapping */
331         *g_rid = pdb_gid_to_group_rid(pw->pw_gid);
332
333         return True;
334 }
335
336 /*******************************************************************
337  Converts NT user RID to a UNIX uid.
338  ********************************************************************/
339 uid_t pdb_user_rid_to_uid(uint32 user_rid)
340 {
341         return (uid_t)(((user_rid & (~USER_RID_TYPE))- 1000)/RID_MULTIPLIER);
342 }
343
344 /*******************************************************************
345  Converts NT user RID to a UNIX gid.
346  ********************************************************************/
347
348 gid_t pdb_user_rid_to_gid(uint32 user_rid)
349 {
350         return (uid_t)(((user_rid & (~GROUP_RID_TYPE))- 1000)/RID_MULTIPLIER);
351 }
352
353 /*******************************************************************
354  converts UNIX uid to an NT User RID.
355  ********************************************************************/
356
357 uint32 pdb_uid_to_user_rid(uid_t uid)
358 {
359         return (((((uint32)uid)*RID_MULTIPLIER) + 1000) | USER_RID_TYPE);
360 }
361
362 /*******************************************************************
363  converts NT Group RID to a UNIX uid.
364  ********************************************************************/
365
366 uint32 pdb_gid_to_group_rid(gid_t gid)
367 {
368   return (((((uint32)gid)*RID_MULTIPLIER) + 1000) | GROUP_RID_TYPE);
369 }
370
371 /*******************************************************************
372  Decides if a RID is a well known RID.
373  ********************************************************************/
374
375 static BOOL pdb_rid_is_well_known(uint32 rid)
376 {
377   return (rid < 1000);
378 }
379
380 /*******************************************************************
381  Decides if a RID is a user or group RID.
382  ********************************************************************/
383 BOOL pdb_rid_is_user(uint32 rid)
384 {
385   /* lkcl i understand that NT attaches an enumeration to a RID
386    * such that it can be identified as either a user, group etc
387    * type.  there are 5 such categories, and they are documented.
388    */
389    if(pdb_rid_is_well_known(rid)) {
390       /*
391        * The only well known user RIDs are DOMAIN_USER_RID_ADMIN
392        * and DOMAIN_USER_RID_GUEST.
393        */
394      if(rid == DOMAIN_USER_RID_ADMIN || rid == DOMAIN_USER_RID_GUEST)
395        return True;
396    } else if((rid & RID_TYPE_MASK) == USER_RID_TYPE) {
397      return True;
398    }
399    return False;
400 }
401
402 /*******************************************************************
403  Convert a rid into a name. Used in the lookup SID rpc.
404  ********************************************************************/
405 BOOL local_lookup_rid(uint32 rid, char *name, enum SID_NAME_USE *psid_name_use)
406 {
407
408         BOOL is_user = pdb_rid_is_user(rid);
409
410         DEBUG(5,("local_lookup_rid: looking up %s RID %u.\n", is_user ? "user" :
411                         "group", (unsigned int)rid));
412
413         if(is_user) {
414                 if(rid == DOMAIN_USER_RID_ADMIN) {
415                         pstring admin_users;
416                         char *p = admin_users;
417                         pstrcpy( admin_users, lp_domain_admin_users());
418                         if(!next_token(&p, name, NULL, sizeof(fstring)))
419                                 fstrcpy(name, "Administrator");
420                 } else if (rid == DOMAIN_USER_RID_GUEST) {
421                         pstring guest_users;
422                         char *p = guest_users;
423                         pstrcpy( guest_users, lp_domain_guest_users());
424                         if(!next_token(&p, name, NULL, sizeof(fstring)))
425                                 fstrcpy(name, "Guest");
426                 } else {
427                         uid_t uid;
428                         struct passwd *pass;
429                         
430                         /*
431                          * Don't try to convert the rid to a name if 
432                          * running in appliance mode
433                          */
434                         if (lp_hide_local_users())
435                                 return False;
436                         
437                         uid = pdb_user_rid_to_uid(rid);
438                         pass = sys_getpwuid(uid);
439
440                         *psid_name_use = SID_NAME_USER;
441
442                         DEBUG(5,("local_lookup_rid: looking up uid %u %s\n", (unsigned int)uid,
443                                 pass ? "succeeded" : "failed" ));
444
445                         if(!pass) {
446                                 slprintf(name, sizeof(fstring)-1, "unix_user.%u", (unsigned int)uid);
447                                 return True;
448                         }
449
450                         fstrcpy(name, pass->pw_name);
451
452                         DEBUG(5,("local_lookup_rid: found user %s for rid %u\n", name,
453                                 (unsigned int)rid ));
454                 }
455
456         } else {
457                 gid_t gid;
458                 struct group *gr; 
459
460                 /* 
461                  * Don't try to convert the rid to a name if running
462                  * in appliance mode
463                  */
464                 
465                 if (lp_hide_local_users()) 
466                         return False;
467                 
468                 gid = pdb_user_rid_to_gid(rid);
469                 gr = getgrgid(gid);
470
471                 *psid_name_use = SID_NAME_ALIAS;
472
473                 DEBUG(5,("local_local_rid: looking up gid %u %s\n", (unsigned int)gid,
474                         gr ? "succeeded" : "failed" ));
475
476                 if(!gr) {
477                         slprintf(name, sizeof(fstring)-1, "unix_group.%u", (unsigned int)gid);
478                         return True;
479                 }
480
481                 fstrcpy( name, gr->gr_name);
482
483                 DEBUG(5,("local_lookup_rid: found group %s for rid %u\n", name,
484                         (unsigned int)rid ));
485         }
486
487         return True;
488 }
489
490 /*******************************************************************
491  Convert a name into a SID. Used in the lookup name rpc.
492  ********************************************************************/
493
494 BOOL local_lookup_name(const char *c_domain, const char *c_user, DOM_SID *psid, enum SID_NAME_USE *psid_name_use)
495 {
496         extern DOM_SID global_sid_World_Domain;
497         struct passwd *pass = NULL;
498         DOM_SID local_sid;
499         fstring user;
500         fstring domain;
501
502         /*
503          * domain and user may be quoted const strings, and map_username and
504          * friends can modify them. Make a modifiable copy. JRA.
505          */
506
507         fstrcpy(domain, c_domain);
508         fstrcpy(user, c_user);
509
510         sid_copy(&local_sid, &global_sam_sid);
511
512         /*
513          * Special case for MACHINE\Everyone. Map to the world_sid.
514          */
515
516         if(strequal(user, "Everyone")) {
517                 sid_copy( psid, &global_sid_World_Domain);
518                 sid_append_rid(psid, 0);
519                 *psid_name_use = SID_NAME_ALIAS;
520                 return True;
521         }
522
523         /* 
524          * Don't lookup local unix users if running in appliance mode
525          */
526         if (lp_hide_local_users()) 
527                 return False;
528
529         (void)map_username(user);
530
531         if(!(pass = Get_Pwnam(user, True))) {
532                 /*
533                  * Maybe it was a group ?
534                  */
535                 struct group *grp = getgrnam(user);
536
537                 if(!grp)
538                         return False;
539
540                 sid_append_rid( &local_sid, pdb_gid_to_group_rid(grp->gr_gid));
541                 *psid_name_use = SID_NAME_ALIAS;
542         } else {
543
544                 sid_append_rid( &local_sid, pdb_uid_to_user_rid(pass->pw_uid));
545                 *psid_name_use = SID_NAME_USER;
546         }
547
548         sid_copy( psid, &local_sid);
549
550         return True;
551 }
552
553 /****************************************************************************
554  Convert a uid to SID - locally.
555 ****************************************************************************/
556 DOM_SID *local_uid_to_sid(DOM_SID *psid, uid_t uid)
557 {
558         extern DOM_SID global_sam_sid;
559
560         sid_copy(psid, &global_sam_sid);
561         sid_append_rid(psid, pdb_uid_to_user_rid(uid));
562
563         return psid;
564 }
565
566
567 /****************************************************************************
568  Convert a SID to uid - locally.
569 ****************************************************************************/
570 BOOL local_sid_to_uid(uid_t *puid, DOM_SID *psid, enum SID_NAME_USE *name_type)
571 {
572         extern DOM_SID global_sam_sid;
573
574         DOM_SID dom_sid;
575         uint32 rid;
576         fstring str;
577         struct passwd *pass;
578
579         *name_type = SID_NAME_UNKNOWN;
580
581         sid_copy(&dom_sid, psid);
582         sid_split_rid(&dom_sid, &rid);
583
584         if (!pdb_rid_is_user(rid))
585                 return False;
586
587         /*
588          * We can only convert to a uid if this is our local
589          * Domain SID (ie. we are the controling authority).
590          */
591         if (!sid_equal(&global_sam_sid, &dom_sid))
592                 return False;
593
594         *puid = pdb_user_rid_to_uid(rid);
595
596         /*
597          * Ensure this uid really does exist.
598          */
599         if(!(pass = sys_getpwuid(*puid)))
600                 return False;
601
602         DEBUG(10,("local_sid_to_uid: SID %s -> uid (%u) (%s).\n", sid_to_string( str, psid),
603                 (unsigned int)*puid, pass->pw_name ));
604
605         return True;
606 }
607
608 /****************************************************************************
609  Convert a gid to SID - locally.
610 ****************************************************************************/
611 DOM_SID *local_gid_to_sid(DOM_SID *psid, gid_t gid)
612 {
613     extern DOM_SID global_sam_sid;
614
615         sid_copy(psid, &global_sam_sid);
616         sid_append_rid(psid, pdb_gid_to_group_rid(gid));
617
618         return psid;
619 }
620
621 /****************************************************************************
622  Convert a SID to gid - locally.
623 ****************************************************************************/
624 BOOL local_sid_to_gid(gid_t *pgid, DOM_SID *psid, enum SID_NAME_USE *name_type)
625 {
626     extern DOM_SID global_sam_sid;
627         DOM_SID dom_sid;
628         uint32 rid;
629         fstring str;
630         struct group *grp;
631
632         *name_type = SID_NAME_UNKNOWN;
633
634         sid_copy(&dom_sid, psid);
635         sid_split_rid(&dom_sid, &rid);
636
637         /*
638          * We can only convert to a gid if this is our local
639          * Domain SID (ie. we are the controling authority).
640          */
641
642         if (!sid_equal(&global_sam_sid, &dom_sid))
643                 return False;
644
645         if (pdb_rid_is_user(rid))
646                 return False;
647
648         *pgid = pdb_user_rid_to_gid(rid);
649
650         /*
651          * Ensure this gid really does exist.
652          */
653
654         if(!(grp = getgrgid(*pgid)))
655                 return False;
656
657         DEBUG(10,("local_sid_to_gid: SID %s -> gid (%u) (%s).\n", sid_to_string( str, psid),
658                 (unsigned int)*pgid, grp->gr_name ));
659
660         return True;
661 }
662
663 static void select_name(fstring *string, char **name, const UNISTR2 *from)
664 {
665         if (from->buffer != 0)
666         {
667                 unistr2_to_ascii(*string, from, sizeof(*string));
668                 *name = *string;
669         }
670 }
671
672 /*************************************************************
673  copies a SAM_USER_INFO_23 to a SAM_ACCOUNT
674  **************************************************************/
675 void copy_id23_to_sam_passwd(SAM_ACCOUNT *to, SAM_USER_INFO_23 *from)
676 {
677         static fstring smb_name;
678         static fstring full_name;
679         static fstring home_dir;
680         static fstring dir_drive;
681         static fstring logon_script;
682         static fstring profile_path;
683         static fstring acct_desc;
684         static fstring workstations;
685         static fstring unknown_str;
686         static fstring munged_dial;
687
688         if (from == NULL || to == NULL) 
689                 return;
690
691         to->logon_time = nt_time_to_unix(&from->logon_time);
692         to->logoff_time = nt_time_to_unix(&from->logoff_time);
693         to->kickoff_time = nt_time_to_unix(&from->kickoff_time);
694         to->pass_last_set_time = nt_time_to_unix(&from->pass_last_set_time);
695         to->pass_can_change_time = nt_time_to_unix(&from->pass_can_change_time);
696         to->pass_must_change_time = nt_time_to_unix(&from->pass_must_change_time);
697
698         select_name(&smb_name    , &to->username    , &from->uni_user_name   );
699         select_name(&full_name   , &to->full_name   , &from->uni_full_name   );
700         select_name(&home_dir    , &to->home_dir    , &from->uni_home_dir    );
701         select_name(&dir_drive   , &to->dir_drive   , &from->uni_dir_drive   );
702         select_name(&logon_script, &to->logon_script, &from->uni_logon_script);
703         select_name(&profile_path, &to->profile_path, &from->uni_profile_path);
704         select_name(&acct_desc   , &to->acct_desc   , &from->uni_acct_desc   );
705         select_name(&workstations, &to->workstations, &from->uni_workstations);
706         select_name(&unknown_str , &to->unknown_str , &from->uni_unknown_str );
707         select_name(&munged_dial , &to->munged_dial , &from->uni_munged_dial );
708
709         to->user_rid = from->user_rid;
710         to->group_rid = from->group_rid;
711
712         to->acct_ctrl = from->acb_info;
713         to->unknown_3 = from->unknown_3;
714
715         to->logon_divs = from->logon_divs;
716         to->hours_len = from->logon_hrs.len;
717         memcpy(to->hours, from->logon_hrs.hours, MAX_HOURS_LEN);
718
719         to->unknown_5 = from->unknown_5;
720         to->unknown_6 = from->unknown_6;
721 }
722
723 /*************************************************************
724  copies a sam passwd.
725  **************************************************************/
726 void copy_id21_to_sam_passwd(SAM_ACCOUNT *to, SAM_USER_INFO_21 *from)
727 {
728         static fstring smb_name;
729         static fstring full_name;
730         static fstring home_dir;
731         static fstring dir_drive;
732         static fstring logon_script;
733         static fstring profile_path;
734         static fstring acct_desc;
735         static fstring workstations;
736         static fstring unknown_str;
737         static fstring munged_dial;
738
739         if (from == NULL || to == NULL) 
740                 return;
741
742         to->logon_time = nt_time_to_unix(&from->logon_time);
743         to->logoff_time = nt_time_to_unix(&from->logoff_time);
744         to->kickoff_time = nt_time_to_unix(&from->kickoff_time);
745         to->pass_last_set_time = nt_time_to_unix(&from->pass_last_set_time);
746         to->pass_can_change_time = nt_time_to_unix(&from->pass_can_change_time);
747         to->pass_must_change_time = nt_time_to_unix(&from->pass_must_change_time);
748
749         select_name(&smb_name    , &to->username    , &from->uni_user_name   );
750         select_name(&full_name   , &to->full_name   , &from->uni_full_name   );
751         select_name(&home_dir    , &to->home_dir    , &from->uni_home_dir    );
752         select_name(&dir_drive   , &to->dir_drive   , &from->uni_dir_drive   );
753         select_name(&logon_script, &to->logon_script, &from->uni_logon_script);
754         select_name(&profile_path, &to->profile_path, &from->uni_profile_path);
755         select_name(&acct_desc   , &to->acct_desc   , &from->uni_acct_desc   );
756         select_name(&workstations, &to->workstations, &from->uni_workstations);
757         select_name(&unknown_str , &to->unknown_str , &from->uni_unknown_str );
758         select_name(&munged_dial , &to->munged_dial , &from->uni_munged_dial );
759
760         to->user_rid = from->user_rid;
761         to->group_rid = from->group_rid;
762         
763         /* FIXME!!  Do we need to copy the passwords here as well?
764            I don't know.  Need to figure this out   --jerry */
765
766         to->acct_ctrl = from->acb_info;
767         to->unknown_3 = from->unknown_3;
768
769         to->logon_divs = from->logon_divs;
770         to->hours_len = from->logon_hrs.len;
771         memcpy(to->hours, from->logon_hrs.hours, MAX_HOURS_LEN);
772
773         to->unknown_5 = from->unknown_5;
774         to->unknown_6 = from->unknown_6;
775 }
776
777
778 /*************************************************************
779  copies a sam passwd.
780  **************************************************************/
781 void copy_sam_passwd(SAM_ACCOUNT *to, const SAM_ACCOUNT *from)
782 {
783         int len;
784         
785         if (!from || !to) return;
786
787         pdb_clear_sam (to);
788
789         /* copy all non-pointers */
790         memcpy(to, from, sizeof(*from));
791
792         if (from->username) {
793                 len=strlen(from->username)+1;
794                 to->username = talloc(to->mem_ctx, len);
795                 StrnCpy (to->username, from->username, len-1);
796         }
797
798         if (from->full_name) {
799                 len=strlen(from->full_name)+1;
800                 to->full_name = talloc(to->mem_ctx, len);
801                 StrnCpy (to->full_name, from->full_name, len-1);
802         }
803
804         if (from->nt_username) {
805                 len=strlen(from->nt_username)+1;
806                 to->nt_username = talloc(to->mem_ctx, len);
807                 StrnCpy (to->nt_username, from->nt_username, len-1);
808         }
809
810         if (from->profile_path) {
811                 len=strlen(from->profile_path)+1;
812                 to->profile_path = talloc(to->mem_ctx, len);
813                 StrnCpy (to->profile_path, from->profile_path, len-1);
814         }
815
816         if (from->logon_script) {
817                 len=strlen(from->logon_script)+1;
818                 to->logon_script = talloc(to->mem_ctx, len);
819                 StrnCpy (to->logon_script, from->logon_script, len-1);
820         }
821
822         if (from->home_dir) {
823                 len=strlen(from->home_dir)+1;
824                 to->home_dir = talloc(to->mem_ctx, len);
825                 StrnCpy (to->home_dir, from->home_dir, len-1);
826         }
827         
828         if (from->dir_drive) {
829                 len=strlen(from->dir_drive)+1;
830                 to->dir_drive = talloc(to->mem_ctx, len);
831                 StrnCpy (to->dir_drive, from->dir_drive, len-1);
832         }
833         
834         if (from->workstations) {
835                 len=strlen(from->workstations)+1;
836                 to->workstations = talloc(to->mem_ctx, len);
837                 StrnCpy (to->workstations, from->workstations, len-1);
838         }
839         
840         if (from->acct_desc) {
841                 len=strlen(from->acct_desc)+1;
842                 to->acct_desc = talloc(to->mem_ctx, len);
843                 StrnCpy (to->acct_desc, from->acct_desc, len-1);
844         }
845         
846         if (from->munged_dial) {
847                 len=strlen(from->munged_dial)+1;
848                 to->munged_dial = talloc(to->mem_ctx, len);
849                 StrnCpy (to->munged_dial, from->munged_dial, len);
850         }
851         
852         if (from->unknown_str) {
853                 len=strlen(from->unknown_str)+1;
854                 to->unknown_str = talloc(to->mem_ctx, len);
855                 StrnCpy (to->unknown_str, from->unknown_str, len-1);
856         }
857
858
859         if (from->nt_pw) {
860                 to->nt_pw = talloc(to->mem_ctx, 16);
861                 memcpy (to->nt_pw, from->nt_pw, 16);
862         }
863         
864         if (from->lm_pw) {
865                 to->lm_pw = talloc(to->mem_ctx, 16);
866                 memcpy (to->lm_pw, from->lm_pw, 16);
867         }
868
869         return;
870 }
871
872 /*************************************************************
873  change a password entry in the local smbpasswd file
874
875  FIXME!!  The function needs to be abstracted into the
876  passdb interface or something.  It is currently being called
877  by _api_samr_create_user() in rpc_server/srv_samr.c
878  
879  --jerry
880  *************************************************************/
881
882 BOOL local_password_change(char *user_name, int local_flags,
883                            char *new_passwd, 
884                            char *err_str, size_t err_str_len,
885                            char *msg_str, size_t msg_str_len)
886 {
887         struct passwd  *pwd = NULL;
888         SAM_ACCOUNT     *sam_pass;
889         SAM_ACCOUNT     new_sam_acct;
890         uchar           new_p16[16];
891         uchar           new_nt_p16[16];
892
893         *err_str = '\0';
894         *msg_str = '\0';
895
896         if (local_flags & LOCAL_ADD_USER) {
897         
898                 /*
899                  * Check for a local account - if we're adding only.
900                  */
901         
902                 if(!(pwd = sys_getpwnam(user_name))) {
903                         slprintf(err_str, err_str_len - 1, "User %s does not \
904 exist in system password file (usually /etc/passwd). Cannot add \
905 account without a valid local system user.\n", user_name);
906                         return False;
907                 }
908         }
909
910         /* Calculate the MD4 hash (NT compatible) of the new password. */
911         nt_lm_owf_gen(new_passwd, new_nt_p16, new_p16);
912
913         /* Get the smb passwd entry for this user */
914         sam_pass = pdb_getsampwnam(user_name);
915         if (sam_pass == NULL) 
916         {
917                 if(!(local_flags & LOCAL_ADD_USER)) 
918                 {
919                         slprintf(err_str, err_str_len-1,"Failed to find entry for user %s.\n", user_name);
920                         return False;
921                 }
922
923                 /* create the SAM_ACCOUNT struct and call pdb_add_sam_account.
924                    Because the new_sam_pwd only exists in the scope of this function
925                    we will not allocate memory for members */
926                 pdb_init_sam          (&new_sam_acct);
927                 pdb_set_username      (&new_sam_acct, user_name);
928                 pdb_set_fullname      (&new_sam_acct, pwd->pw_gecos);
929                 pdb_set_uid           (&new_sam_acct, pwd->pw_uid);
930                 pdb_set_gid           (&new_sam_acct, pwd->pw_gid);
931                 pdb_set_pass_last_set_time(&new_sam_acct, time(NULL));
932                 pdb_set_profile_path  (&new_sam_acct, lp_logon_path());
933                 pdb_set_homedir       (&new_sam_acct, lp_logon_home());
934                 pdb_set_dir_drive     (&new_sam_acct, lp_logon_drive());
935                 pdb_set_logon_script  (&new_sam_acct, lp_logon_script());
936
937                 /* set account flags */
938                 pdb_set_acct_ctrl(&new_sam_acct,((local_flags & LOCAL_TRUST_ACCOUNT) ? ACB_WSTRUST : ACB_NORMAL) );
939                 if (local_flags & LOCAL_DISABLE_USER)
940                 {
941                         pdb_set_acct_ctrl (&new_sam_acct, pdb_get_acct_ctrl(&new_sam_acct)|ACB_DISABLED);
942                 }
943                 if (local_flags & LOCAL_SET_NO_PASSWORD)
944                 {
945                         pdb_set_acct_ctrl (&new_sam_acct, pdb_get_acct_ctrl(&new_sam_acct)|ACB_PWNOTREQ);
946                 }
947                 else
948                 {
949                         /* set the passwords here.  if we get to here it means
950                            we have a valid, active account */
951                         pdb_set_lanman_passwd (&new_sam_acct, new_p16);
952                         pdb_set_nt_passwd     (&new_sam_acct, new_nt_p16);
953                 }
954                 
955                         
956                 if (pdb_add_sam_account(&new_sam_acct)) 
957                 {
958                         slprintf(msg_str, msg_str_len-1, "Added user %s.\n", user_name);
959                         pdb_clear_sam (&new_sam_acct);
960                         return True;
961                 } 
962                 else 
963                 {
964                         slprintf(err_str, err_str_len-1, "Failed to add entry for user %s.\n", user_name);
965                         return False;
966                 }
967         } 
968         else 
969         {
970                 /* the entry already existed */
971                 local_flags &= ~LOCAL_ADD_USER;
972         }
973
974         /*
975          * We are root - just write the new password
976          * and the valid last change time.
977          */
978
979         if(local_flags & LOCAL_DISABLE_USER) 
980         {
981                 pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_DISABLED);
982         }
983         else if (local_flags & LOCAL_ENABLE_USER) 
984         {
985                 if(pdb_get_lanman_passwd(sam_pass) == NULL) 
986                 {
987                         pdb_set_lanman_passwd (sam_pass, new_p16);
988                         pdb_set_nt_passwd     (sam_pass, new_nt_p16);
989                 }
990                 pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED));
991         } else if (local_flags & LOCAL_SET_NO_PASSWORD) 
992         {
993                 pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_PWNOTREQ);
994                 
995                 /* This is needed to preserve ACB_PWNOTREQ in mod_smbfilepwd_entry */
996                 pdb_set_lanman_passwd (sam_pass, NULL);
997                 pdb_set_nt_passwd     (sam_pass, NULL);
998         } 
999         else 
1000         {
1001                 /*
1002                  * If we're dealing with setting a completely empty user account
1003                  * ie. One with a password of 'XXXX', but not set disabled (like
1004                  * an account created from scratch) then if the old password was
1005                  * 'XX's then getsmbpwent will have set the ACB_DISABLED flag.
1006                  * We remove that as we're giving this user their first password
1007                  * and the decision hasn't really been made to disable them (ie.
1008                  * don't create them disabled). JRA.
1009                  */
1010                 if ((pdb_get_lanman_passwd(sam_pass)==NULL) && (pdb_get_acct_ctrl(sam_pass)&ACB_DISABLED))
1011                         pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED));
1012                 pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_PWNOTREQ));
1013                 pdb_set_lanman_passwd (sam_pass, new_p16);
1014                 pdb_set_nt_passwd     (sam_pass, new_nt_p16);
1015         }
1016         
1017         if(local_flags & LOCAL_DELETE_USER) 
1018         {
1019                 if (!pdb_delete_sam_account(user_name)) 
1020                 {
1021                         slprintf(err_str,err_str_len-1, "Failed to delete entry for user %s.\n", user_name);
1022                         return False;
1023                 }
1024                 slprintf(msg_str, msg_str_len-1, "Deleted user %s.\n", user_name);
1025         } 
1026         else 
1027         {
1028                 if(!pdb_update_sam_account(sam_pass, True)) 
1029                 {
1030                         slprintf(err_str, err_str_len-1, "Failed to modify entry for user %s.\n", user_name);
1031                         return False;
1032                 }
1033                 if(local_flags & LOCAL_DISABLE_USER)
1034                         slprintf(msg_str, msg_str_len-1, "Disabled user %s.\n", user_name);
1035                 else if (local_flags & LOCAL_ENABLE_USER)
1036                         slprintf(msg_str, msg_str_len-1, "Enabled user %s.\n", user_name);
1037                 else if (local_flags & LOCAL_SET_NO_PASSWORD)
1038                         slprintf(msg_str, msg_str_len-1, "User %s password set to none.\n", user_name);
1039         }
1040
1041         return True;
1042 }
1043
1044
1045 /*********************************************************************
1046  collection of get...() functions for SAM_ACCOUNT_INFO
1047  ********************************************************************/
1048 uint16 pdb_get_acct_ctrl (SAM_ACCOUNT *sampass)
1049 {
1050         if (sampass)
1051                 return (sampass->acct_ctrl);
1052         else
1053                 return (ACB_DISABLED);
1054 }
1055
1056 time_t pdb_get_logon_time (SAM_ACCOUNT *sampass)
1057 {
1058         if (sampass)
1059                 return (sampass->logon_time);
1060         else
1061                 return (0);
1062 }
1063
1064 time_t pdb_get_logoff_time (SAM_ACCOUNT *sampass)
1065 {
1066         if (sampass)
1067                 return (sampass->logoff_time);
1068         else
1069                 return (-1);
1070 }
1071
1072 time_t pdb_get_kickoff_time (SAM_ACCOUNT *sampass)
1073 {
1074         if (sampass)
1075                 return (sampass->kickoff_time);
1076         else
1077                 return (-1);
1078 }
1079
1080 time_t pdb_get_pass_last_set_time (SAM_ACCOUNT *sampass)
1081 {
1082         if (sampass)
1083                 return (sampass->pass_last_set_time);
1084         else
1085                 return (-1);
1086 }
1087
1088 time_t pdb_get_pass_can_change_time (SAM_ACCOUNT *sampass)
1089 {
1090         if (sampass)
1091                 return (sampass->pass_can_change_time);
1092         else
1093                 return (-1);
1094 }
1095
1096 time_t pdb_get_pass_must_change_time (SAM_ACCOUNT *sampass)
1097 {
1098         if (sampass)
1099                 return (sampass->pass_must_change_time);
1100         else
1101                 return (-1);
1102 }
1103
1104 uint16 pdb_get_logon_divs (SAM_ACCOUNT *sampass)
1105 {
1106         if (sampass)
1107                 return (sampass->logon_divs);
1108         else
1109                 return (-1);
1110 }
1111
1112 uint32 pdb_get_hours_len (SAM_ACCOUNT *sampass)
1113 {
1114         if (sampass)
1115                 return (sampass->hours_len);
1116         else
1117                 return (-1);
1118 }
1119
1120 uint8* pdb_get_hours (SAM_ACCOUNT *sampass)
1121 {
1122         if (sampass)
1123                 return (sampass->hours);
1124         else
1125                 return (NULL);
1126 }
1127
1128 uint8* pdb_get_nt_passwd (SAM_ACCOUNT *sampass)
1129 {
1130         if (sampass)
1131                 return (sampass->nt_pw);
1132         else
1133                 return (NULL);
1134 }
1135
1136 uint8* pdb_get_lanman_passwd (SAM_ACCOUNT *sampass)
1137 {
1138         if (sampass)
1139                 return (sampass->lm_pw);
1140         else
1141                 return (NULL);
1142 }
1143
1144
1145 uint32 pdb_get_user_rid (SAM_ACCOUNT *sampass)
1146 {
1147         if (sampass)
1148                 return (sampass->user_rid);
1149         else
1150                 return (-1);
1151 }
1152
1153 uint32 pdb_get_group_rid (SAM_ACCOUNT *sampass)
1154 {
1155         if (sampass)
1156                 return (sampass->group_rid);
1157         else
1158                 return (-1);
1159 }
1160
1161 uid_t pdb_get_uid (SAM_ACCOUNT *sampass)
1162 {
1163         if (sampass)
1164                 return (sampass->uid);
1165         else
1166                 return ((uid_t)-1);
1167 }
1168
1169 gid_t pdb_get_gid (SAM_ACCOUNT *sampass)
1170 {
1171         if (sampass)
1172                 return (sampass->gid);
1173         else
1174                 return ((gid_t)-1);
1175 }
1176
1177 char* pdb_get_username (SAM_ACCOUNT *sampass)
1178 {
1179         if (sampass)
1180                 return (sampass->username);
1181         else
1182                 return (NULL);
1183 }
1184
1185 char* pdb_get_domain (SAM_ACCOUNT *sampass)
1186 {
1187         if (sampass)
1188                 return (sampass->domain);
1189         else
1190                 return (NULL);
1191 }
1192
1193 char* pdb_get_nt_username (SAM_ACCOUNT *sampass)
1194 {
1195         if (sampass)
1196                 return (sampass->nt_username);
1197         else
1198                 return (NULL);
1199 }
1200
1201 char* pdb_get_fullname (SAM_ACCOUNT *sampass)
1202 {
1203         if (sampass)
1204                 return (sampass->full_name);
1205         else
1206                 return (NULL);
1207 }
1208
1209 char* pdb_get_homedir (SAM_ACCOUNT *sampass)
1210 {
1211         if (sampass)
1212                 return (sampass->home_dir);
1213         else
1214                 return (NULL);
1215 }
1216
1217 char* pdb_get_dirdrive (SAM_ACCOUNT *sampass)
1218 {
1219         if (sampass)
1220                 return (sampass->dir_drive);
1221         else
1222                 return (NULL);
1223 }
1224
1225 char* pdb_get_logon_script (SAM_ACCOUNT *sampass)
1226 {
1227         if (sampass)
1228                 return (sampass->logon_script);
1229         else
1230                 return (NULL);
1231 }
1232
1233 char* pdb_get_profile_path (SAM_ACCOUNT *sampass)
1234 {
1235         if (sampass)
1236                 return (sampass->profile_path);
1237         else
1238                 return (NULL);
1239 }
1240
1241 char* pdb_get_acct_desc (SAM_ACCOUNT *sampass)
1242 {
1243         if (sampass)
1244                 return (sampass->acct_desc);
1245         else
1246                 return (NULL);
1247 }
1248
1249 char* pdb_get_workstations (SAM_ACCOUNT *sampass)
1250 {
1251         if (sampass)
1252                 return (sampass->workstations);
1253         else
1254                 return (NULL);
1255 }
1256
1257 char* pdb_get_munged_dial (SAM_ACCOUNT *sampass)
1258 {
1259         if (sampass)
1260                 return (sampass->munged_dial);
1261         else
1262                 return (NULL);
1263 }
1264
1265 uint32 pdb_get_unknown3 (SAM_ACCOUNT *sampass)
1266 {
1267         if (sampass)
1268                 return (sampass->unknown_3);
1269         else
1270                 return (-1);
1271 }
1272
1273 uint32 pdb_get_unknown5 (SAM_ACCOUNT *sampass)
1274 {
1275         if (sampass)
1276                 return (sampass->unknown_5);
1277         else
1278                 return (-1);
1279 }
1280
1281 uint32 pdb_get_unknown6 (SAM_ACCOUNT *sampass)
1282 {
1283         if (sampass)
1284                 return (sampass->unknown_6);
1285         else
1286                 return (-1);
1287 }
1288
1289 /*********************************************************************
1290  collection of set...() functions for SAM_ACCOUNT_INFO
1291  ********************************************************************/
1292 BOOL pdb_set_acct_ctrl (SAM_ACCOUNT *sampass, uint16 flags)
1293 {
1294         if (!sampass)
1295                 return False;
1296                 
1297         if (sampass)
1298         {
1299                 sampass->acct_ctrl = flags;
1300                 return True;
1301         }
1302         
1303         return False;
1304 }
1305
1306 BOOL pdb_set_logon_time (SAM_ACCOUNT *sampass, time_t mytime)
1307 {
1308         if (!sampass)
1309                 return False;
1310
1311         sampass->logon_time = mytime;
1312         return True;
1313 }
1314
1315 BOOL pdb_set_logoff_time (SAM_ACCOUNT *sampass, time_t mytime)
1316 {
1317         if (!sampass)
1318                 return False;
1319
1320         sampass->logoff_time = mytime;
1321         return True;
1322 }
1323
1324 BOOL pdb_set_kickoff_time (SAM_ACCOUNT *sampass, time_t mytime)
1325 {
1326         if (!sampass)
1327                 return False;
1328
1329         sampass->kickoff_time = mytime;
1330         return True;
1331 }
1332
1333 BOOL pdb_set_pass_can_change_time (SAM_ACCOUNT *sampass, time_t mytime)
1334 {
1335         if (!sampass)
1336                 return False;
1337
1338         sampass->pass_can_change_time = mytime;
1339         return True;
1340 }
1341
1342 BOOL pdb_set_pass_must_change_time (SAM_ACCOUNT *sampass, time_t mytime)
1343 {
1344         if (!sampass)
1345                 return False;
1346
1347         sampass->pass_must_change_time = mytime;
1348         return True;
1349 }
1350
1351 BOOL pdb_set_pass_last_set_time (SAM_ACCOUNT *sampass, time_t mytime)
1352 {
1353         if (!sampass)
1354                 return False;
1355
1356         sampass->pass_last_set_time = mytime;
1357         return True;
1358 }
1359
1360 BOOL pdb_set_hours_len (SAM_ACCOUNT *sampass, uint32 len)
1361 {
1362         if (!sampass)
1363                 return False;
1364
1365         sampass->hours_len = len;
1366         return True;
1367 }
1368
1369 BOOL pdb_set_logons_divs (SAM_ACCOUNT *sampass, uint16 hours)
1370 {
1371         if (!sampass)
1372                 return False;
1373
1374         sampass->logon_divs = hours;
1375         return True;
1376 }
1377
1378 BOOL pdb_set_uid (SAM_ACCOUNT *sampass, uid_t uid)
1379 {
1380         if (!sampass)
1381                 return False;
1382
1383         sampass->uid = uid;
1384         return True;
1385 }
1386
1387 BOOL pdb_set_gid (SAM_ACCOUNT *sampass, gid_t gid)
1388 {
1389         if (!sampass)
1390                 return False;
1391
1392         sampass->gid = gid;
1393         return True;
1394 }
1395
1396 BOOL pdb_set_user_rid (SAM_ACCOUNT *sampass, uint32 rid)
1397 {
1398         if (!sampass)
1399                 return False;
1400
1401         sampass->user_rid = rid;
1402         return True;
1403 }
1404
1405 BOOL pdb_set_group_rid (SAM_ACCOUNT *sampass, uint32 grid)
1406 {
1407         if (!sampass)
1408                 return False;
1409
1410         sampass->group_rid = grid;
1411         return True;
1412 }
1413
1414 BOOL pdb_set_username (SAM_ACCOUNT *sampass, char *username)
1415 {
1416         int len;
1417         
1418         if (!sampass || !sampass->mem_ctx) return False;
1419
1420         if (!username) 
1421         {
1422                 sampass->username = NULL;
1423                 return True;
1424         }
1425
1426         len = strlen(username)+1;
1427         sampass->username = (char*)talloc(sampass->mem_ctx, len);
1428
1429         if (sampass->username == NULL )
1430         {
1431                 DEBUG (0,("pdb_set_username: ERROR - Unable to talloc memory for [%s]\n", username));
1432                 return False;
1433         }
1434         
1435         StrnCpy (sampass->username, username, len-1);
1436
1437         return True;
1438 }
1439
1440 BOOL pdb_set_domain (SAM_ACCOUNT *sampass, char *domain)
1441 {
1442         int len;
1443         
1444         if (!sampass || !sampass->mem_ctx) return False;
1445
1446         if (!domain) 
1447         {
1448                 sampass->domain = NULL;
1449                 return True;
1450         }
1451
1452         len = strlen(domain)+1;
1453         sampass->domain = talloc (sampass->mem_ctx, len);
1454
1455         if (sampass->domain == NULL )
1456         {
1457                 DEBUG (0,("pdb_set_domain: ERROR - Unable to talloc memory for [%s]\n", domain));
1458                 return False;
1459         }
1460         
1461         StrnCpy (sampass->domain, domain, len-1);
1462
1463         return True;
1464 }
1465
1466 BOOL pdb_set_nt_username (SAM_ACCOUNT *sampass, char *nt_username)
1467 {
1468         int len;
1469         
1470         if (!sampass || !sampass->mem_ctx) return False;
1471
1472         if (!nt_username) 
1473         {
1474                 sampass->nt_username = NULL;
1475                 return True;
1476         }
1477
1478         len = strlen(nt_username)+1;
1479         sampass->nt_username = talloc (sampass->mem_ctx, len);
1480
1481         if (sampass->nt_username == NULL )
1482         {
1483                 DEBUG (0,("pdb_set_nt_username: ERROR - Unable to talloc memory for [%s]\n", nt_username));
1484                 return False;
1485         }
1486         
1487         StrnCpy (sampass->nt_username, nt_username, len-1);
1488
1489         return True;
1490 }
1491
1492 BOOL pdb_set_fullname (SAM_ACCOUNT *sampass, char *fullname)
1493 {
1494         int len;
1495         
1496         if (!sampass || !sampass->mem_ctx) return False;
1497
1498         if (!fullname) 
1499         {
1500                 sampass->full_name = NULL;
1501                 return True;
1502         }
1503
1504         len = strlen(fullname)+1;
1505         sampass->full_name = talloc (sampass->mem_ctx, len);
1506
1507         if (sampass->full_name == NULL )
1508         {
1509                 DEBUG (0,("pdb_set_fullname: ERROR - Unable to talloc memory for [%s]\n", fullname));
1510                 return False;
1511         }
1512         
1513         StrnCpy (sampass->full_name, fullname, len-1);
1514
1515         return True;
1516 }
1517
1518 BOOL pdb_set_logon_script (SAM_ACCOUNT *sampass, char *logon_script)
1519 {
1520         int len;
1521         
1522         if (!sampass || !sampass->mem_ctx) return False;
1523
1524         if (!logon_script) 
1525         {
1526                 sampass->logon_script = NULL;
1527                 return True;
1528         }
1529
1530         len = strlen(logon_script)+1;
1531         sampass->logon_script = talloc (sampass->mem_ctx, len);
1532
1533         if (sampass->logon_script == NULL )
1534         {
1535                 DEBUG (0,("pdb_set_logon_script: ERROR - Unable to talloc memory for [%s]\n", logon_script));
1536                 return False;
1537         }
1538         
1539         StrnCpy (sampass->logon_script, logon_script, len-1);
1540
1541         return True;
1542 }
1543
1544 BOOL pdb_set_profile_path (SAM_ACCOUNT *sampass, char *profile_path)
1545 {
1546         int len;
1547         
1548         if (!sampass || !sampass->mem_ctx) return False;
1549
1550         if (!profile_path) 
1551         {
1552                 sampass->profile_path = NULL;
1553                 return True;
1554         }
1555
1556         len = strlen(profile_path)+1;
1557         sampass->profile_path = talloc (sampass->mem_ctx, len);
1558
1559         if (!sampass->profile_path)
1560         {
1561                 DEBUG (0,("pdb_set_profile_path: ERROR - Unable to talloc memory for [%s]\n", profile_path));
1562                 return False;
1563         }
1564         
1565         StrnCpy (sampass->profile_path, profile_path, len-1);
1566         
1567         return True;
1568 }
1569
1570 BOOL pdb_set_dir_drive (SAM_ACCOUNT *sampass, char *dir_drive)
1571 {
1572         int len;
1573         
1574         if (!sampass || !sampass->mem_ctx) return False;
1575
1576         if (!dir_drive) 
1577         {
1578                 sampass->dir_drive = NULL;
1579                 return True;
1580         }
1581
1582         len = strlen(dir_drive)+1;
1583         sampass->dir_drive = talloc (sampass->mem_ctx, len);
1584
1585         if (sampass->dir_drive == NULL )
1586         {
1587                 DEBUG (0,("pdb_set_dir_drive: ERROR - Unable to talloc memory for [%s]\n", dir_drive));
1588                 return False;
1589         }
1590         
1591         StrnCpy (sampass->dir_drive, dir_drive, len-1);
1592
1593         return True;
1594 }
1595
1596 BOOL pdb_set_homedir (SAM_ACCOUNT *sampass, char *homedir)
1597 {
1598         int len;
1599         
1600         if (!sampass || !sampass->mem_ctx) return False;
1601
1602         if (!homedir) 
1603         {
1604                 sampass->home_dir = NULL;
1605                 return True;
1606         }
1607
1608         len = strlen(homedir)+1;
1609         sampass->home_dir = talloc (sampass->mem_ctx, len);
1610
1611         if (sampass->home_dir == NULL )
1612         {
1613                 DEBUG (0,("pdb_set_homedir: ERROR - Unable to talloc memory for [%s]\n", homedir));
1614                 return False;
1615         }
1616         
1617         StrnCpy (sampass->home_dir, homedir, len-1);
1618
1619         return True;
1620 }
1621
1622 BOOL pdb_set_acct_desc (SAM_ACCOUNT *sampass, char *acct_desc)
1623 {
1624         int len;
1625         
1626         if (!sampass || !sampass->mem_ctx) return False;
1627
1628         if (!acct_desc) 
1629         {
1630                 sampass->acct_desc = NULL;
1631                 return True;
1632         }
1633
1634         len = strlen(acct_desc)+1;
1635         sampass->acct_desc = talloc (sampass->mem_ctx, len);
1636
1637         if (sampass->acct_desc == NULL )
1638         {
1639                 DEBUG (0,("pdb_set_acct_desc: ERROR - Unable to talloc memory for [%s]\n", acct_desc));
1640                 return False;
1641         }
1642         
1643         StrnCpy (sampass->acct_desc, acct_desc, len-1);
1644
1645         return True;
1646 }
1647
1648 BOOL pdb_set_workstations (SAM_ACCOUNT *sampass, char *workstations)
1649 {
1650         int len;
1651         
1652         if (!sampass || !sampass->mem_ctx) return False;
1653
1654         if (!workstations) 
1655         {
1656                 sampass->workstations = NULL;
1657                 return True;
1658         }
1659
1660         len = strlen(workstations)+1;
1661         sampass->workstations = talloc (sampass->mem_ctx, len);
1662
1663         if (sampass->workstations == NULL )
1664         {
1665                 DEBUG (0,("pdb_set_workstations: ERROR - Unable to talloc memory for [%s]\n", workstations));
1666                 return False;
1667         }
1668         
1669         StrnCpy (sampass->workstations, workstations, len-1);
1670
1671         return True;
1672 }
1673
1674 BOOL pdb_set_munged_dial (SAM_ACCOUNT *sampass, char *munged_dial)
1675 {
1676         int len;
1677         
1678         if (!sampass || !sampass->mem_ctx) return False;
1679
1680         if (!munged_dial) 
1681         {
1682                 sampass->munged_dial = NULL;
1683                 return True;
1684         }
1685
1686         len = strlen(munged_dial)+1;
1687         sampass->munged_dial = talloc (sampass->mem_ctx, len);
1688
1689         if (sampass->munged_dial == NULL )
1690         {
1691                 DEBUG (0,("pdb_set_munged_dial: ERROR - Unable to talloc memory for [%s]\n", munged_dial));
1692                 return False;
1693         }
1694         
1695         StrnCpy (sampass->munged_dial, munged_dial, len-1);
1696
1697         return True;
1698 }
1699
1700 BOOL pdb_set_nt_passwd (SAM_ACCOUNT *sampass, uint8 *pwd)
1701 {
1702         if (!sampass || !sampass->mem_ctx) return False;
1703
1704         if (!pwd) 
1705         {
1706                 sampass->nt_pw = NULL;
1707                 return True;
1708         }
1709         
1710         sampass->nt_pw = talloc (sampass->mem_ctx, 16);
1711
1712         if (sampass->nt_pw == NULL )
1713         {
1714                 DEBUG (0,("pdb_set_nt_passwd: ERROR - Unable to talloc memory for [%s]\n", pwd));
1715                 return False;
1716         }
1717         
1718         memcpy (sampass->nt_pw, pwd, 16);
1719
1720         return True;
1721 }
1722
1723 BOOL pdb_set_lanman_passwd (SAM_ACCOUNT *sampass, uint8 *pwd)
1724 {
1725         if (!sampass || !sampass->mem_ctx) return False;
1726         
1727         if (!pwd)
1728         {
1729                 sampass->lm_pw = NULL;
1730                 return True;
1731         }
1732
1733         sampass->lm_pw = talloc (sampass->mem_ctx, 16);
1734
1735         if (sampass->lm_pw == NULL )
1736         {
1737                 DEBUG (0,("pdb_set_lanman_passwd: ERROR - Unable to talloc memory for [%s]\n", pwd));
1738                 return False;
1739         }
1740         
1741         memcpy (sampass->lm_pw, pwd, 16);
1742
1743         return True;
1744 }