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