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