Add static, and assert that we will never overflow the static fstring
[Samba/gebeck_regimport.git] / source3 / passdb / passdb.c
blob3df6b5ada9263a03457c77de4c59c54d774625f5
1 /*
2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Jeremy Allison 1996-2001
5 Copyright (C) Luke Kenneth Casson Leighton 1996-1998
6 Copyright (C) Gerald (Jerry) Carter 2000-2001
7 Copyright (C) Andrew Bartlett 2001-2002
8 Copyright (C) Simo Sorce 2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "includes.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_PASSDB
30 /******************************************************************
31 get the default domain/netbios name to be used when
32 testing authentication. For example, if you connect
33 to a Windows member server using a bogus domain name, the
34 Windows box will map the BOGUS\user to DOMAIN\user. A
35 standalone box will map to WKS\user.
36 ******************************************************************/
38 const char *get_default_sam_name(void)
40 /* standalone servers can only use the local netbios name */
41 if ( lp_server_role() == ROLE_STANDALONE )
42 return global_myname();
44 /* Windows domain members default to the DOMAIN
45 name when not specified */
46 return lp_workgroup();
49 /******************************************************************
50 get the default domain/netbios name to be used when dealing
51 with our passdb list of accounts
52 ******************************************************************/
54 const char *get_global_sam_name(void)
56 if ((lp_server_role() == ROLE_DOMAIN_PDC) || (lp_server_role() == ROLE_DOMAIN_BDC)) {
57 return lp_workgroup();
59 return global_myname();
62 /************************************************************
63 Fill the SAM_ACCOUNT with default values.
64 ***********************************************************/
66 void pdb_fill_default_sam(SAM_ACCOUNT *user)
68 ZERO_STRUCT(user->private); /* Don't touch the talloc context */
70 /* no initial methods */
71 user->methods = NULL;
73 /* Don't change these timestamp settings without a good reason.
74 They are important for NT member server compatibility. */
76 user->private.logon_time = (time_t)0;
77 user->private.pass_last_set_time = (time_t)0;
78 user->private.pass_can_change_time = (time_t)0;
79 user->private.logoff_time =
80 user->private.kickoff_time =
81 user->private.pass_must_change_time = get_time_t_max();
82 user->private.unknown_3 = 0x00ffffff; /* don't know */
83 user->private.logon_divs = 168; /* hours per week */
84 user->private.hours_len = 21; /* 21 times 8 bits = 168 */
85 memset(user->private.hours, 0xff, user->private.hours_len); /* available at all hours */
86 user->private.bad_password_count = 0;
87 user->private.logon_count = 0;
88 user->private.unknown_6 = 0x000004ec; /* don't know */
90 /* Some parts of samba strlen their pdb_get...() returns,
91 so this keeps the interface unchanged for now. */
93 user->private.username = "";
94 user->private.domain = "";
95 user->private.nt_username = "";
96 user->private.full_name = "";
97 user->private.home_dir = "";
98 user->private.logon_script = "";
99 user->private.profile_path = "";
100 user->private.acct_desc = "";
101 user->private.workstations = "";
102 user->private.unknown_str = "";
103 user->private.munged_dial = "";
105 user->private.plaintext_pw = NULL;
108 Unless we know otherwise have a Account Control Bit
109 value of 'normal user'. This helps User Manager, which
110 asks for a filtered list of users.
113 user->private.acct_ctrl = ACB_NORMAL;
116 static void destroy_pdb_talloc(SAM_ACCOUNT **user)
118 if (*user) {
119 data_blob_clear_free(&((*user)->private.lm_pw));
120 data_blob_clear_free(&((*user)->private.nt_pw));
122 if((*user)->private.plaintext_pw!=NULL)
123 memset((*user)->private.plaintext_pw,'\0',strlen((*user)->private.plaintext_pw));
124 talloc_destroy((*user)->mem_ctx);
125 *user = NULL;
130 /**********************************************************************
131 Allocates memory and initialises a struct sam_passwd on supplied mem_ctx.
132 ***********************************************************************/
134 NTSTATUS pdb_init_sam_talloc(TALLOC_CTX *mem_ctx, SAM_ACCOUNT **user)
136 if (*user != NULL) {
137 DEBUG(0,("pdb_init_sam_talloc: SAM_ACCOUNT was non NULL\n"));
138 #if 0
139 smb_panic("non-NULL pointer passed to pdb_init_sam\n");
140 #endif
141 return NT_STATUS_UNSUCCESSFUL;
144 if (!mem_ctx) {
145 DEBUG(0,("pdb_init_sam_talloc: mem_ctx was NULL!\n"));
146 return NT_STATUS_UNSUCCESSFUL;
149 *user=(SAM_ACCOUNT *)talloc(mem_ctx, sizeof(SAM_ACCOUNT));
151 if (*user==NULL) {
152 DEBUG(0,("pdb_init_sam_talloc: error while allocating memory\n"));
153 return NT_STATUS_NO_MEMORY;
156 (*user)->mem_ctx = mem_ctx;
158 (*user)->free_fn = NULL;
160 pdb_fill_default_sam(*user);
162 return NT_STATUS_OK;
166 /*************************************************************
167 Alloc memory and initialises a struct sam_passwd.
168 ************************************************************/
170 NTSTATUS pdb_init_sam(SAM_ACCOUNT **user)
172 TALLOC_CTX *mem_ctx;
173 NTSTATUS nt_status;
175 mem_ctx = talloc_init("passdb internal SAM_ACCOUNT allocation");
177 if (!mem_ctx) {
178 DEBUG(0,("pdb_init_sam: error while doing talloc_init()\n"));
179 return NT_STATUS_NO_MEMORY;
182 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_talloc(mem_ctx, user))) {
183 talloc_destroy(mem_ctx);
184 return nt_status;
187 (*user)->free_fn = destroy_pdb_talloc;
189 return NT_STATUS_OK;
193 /*************************************************************
194 Initialises a struct sam_passwd with sane values.
195 ************************************************************/
197 NTSTATUS pdb_fill_sam_pw(SAM_ACCOUNT *sam_account, const struct passwd *pwd)
199 NTSTATUS ret;
201 if (!pwd) {
202 return NT_STATUS_UNSUCCESSFUL;
205 pdb_fill_default_sam(sam_account);
207 pdb_set_username(sam_account, pwd->pw_name, PDB_SET);
208 pdb_set_fullname(sam_account, pwd->pw_gecos, PDB_SET);
210 pdb_set_unix_homedir(sam_account, pwd->pw_dir, PDB_SET);
212 pdb_set_domain (sam_account, get_global_sam_name(), PDB_DEFAULT);
214 /* When we get a proper uid -> SID and SID -> uid allocation
215 mechinism, we should call it here.
217 We can't just set this to 0 or allow it only to be filled
218 in when added to the backend, because the user's SID
219 may already be in security descriptors etc.
221 -- abartlet 11-May-02
224 ret = pdb_set_sam_sids(sam_account, pwd);
225 if (!NT_STATUS_IS_OK(ret)) return ret;
227 /* check if this is a user account or a machine account */
228 if (pwd->pw_name[strlen(pwd->pw_name)-1] != '$')
230 pdb_set_profile_path(sam_account,
231 talloc_sub_specified((sam_account)->mem_ctx,
232 lp_logon_path(),
233 pwd->pw_name, global_myname(),
234 pwd->pw_uid, pwd->pw_gid),
235 PDB_DEFAULT);
237 pdb_set_homedir(sam_account,
238 talloc_sub_specified((sam_account)->mem_ctx,
239 lp_logon_home(),
240 pwd->pw_name, global_myname(),
241 pwd->pw_uid, pwd->pw_gid),
242 PDB_DEFAULT);
244 pdb_set_dir_drive(sam_account,
245 talloc_sub_specified((sam_account)->mem_ctx,
246 lp_logon_drive(),
247 pwd->pw_name, global_myname(),
248 pwd->pw_uid, pwd->pw_gid),
249 PDB_DEFAULT);
251 pdb_set_logon_script(sam_account,
252 talloc_sub_specified((sam_account)->mem_ctx,
253 lp_logon_script(),
254 pwd->pw_name, global_myname(),
255 pwd->pw_uid, pwd->pw_gid),
256 PDB_DEFAULT);
257 if (!pdb_set_acct_ctrl(sam_account, ACB_NORMAL, PDB_DEFAULT)) {
258 DEBUG(1, ("Failed to set 'normal account' flags for user %s.\n", pwd->pw_name));
259 return NT_STATUS_UNSUCCESSFUL;
261 } else {
262 if (!pdb_set_acct_ctrl(sam_account, ACB_WSTRUST, PDB_DEFAULT)) {
263 DEBUG(1, ("Failed to set 'trusted workstation account' flags for user %s.\n", pwd->pw_name));
264 return NT_STATUS_UNSUCCESSFUL;
267 return NT_STATUS_OK;
271 /*************************************************************
272 Initialises a struct sam_passwd with sane values.
273 ************************************************************/
275 NTSTATUS pdb_init_sam_pw(SAM_ACCOUNT **new_sam_acct, const struct passwd *pwd)
277 NTSTATUS nt_status;
279 if (!pwd) {
280 new_sam_acct = NULL;
281 return NT_STATUS_INVALID_PARAMETER;
284 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(new_sam_acct))) {
285 new_sam_acct = NULL;
286 return nt_status;
289 if (!NT_STATUS_IS_OK(nt_status = pdb_fill_sam_pw(*new_sam_acct, pwd))) {
290 pdb_free_sam(new_sam_acct);
291 new_sam_acct = NULL;
292 return nt_status;
295 return NT_STATUS_OK;
299 /*************************************************************
300 Initialises a SAM_ACCOUNT ready to add a new account, based
301 on the UNIX user. Pass in a RID if you have one
302 ************************************************************/
304 NTSTATUS pdb_init_sam_new(SAM_ACCOUNT **new_sam_acct, const char *username,
305 uint32 rid)
307 NTSTATUS nt_status = NT_STATUS_NO_MEMORY;
308 struct passwd *pwd;
309 BOOL ret;
311 pwd = Get_Pwnam(username);
313 if (!pwd)
314 return NT_STATUS_NO_SUCH_USER;
316 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_pw(new_sam_acct, pwd))) {
317 *new_sam_acct = NULL;
318 return nt_status;
321 /* see if we need to generate a new rid using the 2.2 algorithm */
322 if ( rid == 0 && lp_enable_rid_algorithm() ) {
323 DEBUG(10,("pdb_init_sam_new: no RID specified. Generating one via old algorithm\n"));
324 rid = fallback_pdb_uid_to_user_rid(pwd->pw_uid);
327 /* set the new SID */
329 ret = pdb_set_user_sid_from_rid( *new_sam_acct, rid, PDB_SET );
331 return (ret ? NT_STATUS_OK : NT_STATUS_NO_SUCH_USER);
336 * Free the contets of the SAM_ACCOUNT, but not the structure.
338 * Also wipes the LM and NT hashes and plaintext password from
339 * memory.
341 * @param user SAM_ACCOUNT to free members of.
344 static void pdb_free_sam_contents(SAM_ACCOUNT *user)
347 /* Kill off sensitive data. Free()ed by the
348 talloc mechinism */
350 data_blob_clear_free(&(user->private.lm_pw));
351 data_blob_clear_free(&(user->private.nt_pw));
352 if (user->private.plaintext_pw!=NULL)
353 memset(user->private.plaintext_pw,'\0',strlen(user->private.plaintext_pw));
355 if (user->private.backend_private_data && user->private.backend_private_data_free_fn) {
356 user->private.backend_private_data_free_fn(&user->private.backend_private_data);
361 /************************************************************
362 Reset the SAM_ACCOUNT and free the NT/LM hashes.
363 ***********************************************************/
365 NTSTATUS pdb_reset_sam(SAM_ACCOUNT *user)
367 if (user == NULL) {
368 DEBUG(0,("pdb_reset_sam: SAM_ACCOUNT was NULL\n"));
369 #if 0
370 smb_panic("NULL pointer passed to pdb_free_sam\n");
371 #endif
372 return NT_STATUS_UNSUCCESSFUL;
375 pdb_free_sam_contents(user);
377 pdb_fill_default_sam(user);
379 return NT_STATUS_OK;
383 /************************************************************
384 Free the SAM_ACCOUNT and the member pointers.
385 ***********************************************************/
387 NTSTATUS pdb_free_sam(SAM_ACCOUNT **user)
389 if (*user == NULL) {
390 DEBUG(0,("pdb_free_sam: SAM_ACCOUNT was NULL\n"));
391 #if 0
392 smb_panic("NULL pointer passed to pdb_free_sam\n");
393 #endif
394 return NT_STATUS_UNSUCCESSFUL;
397 pdb_free_sam_contents(*user);
399 if ((*user)->free_fn) {
400 (*user)->free_fn(user);
403 return NT_STATUS_OK;
406 /**************************************************************************
407 * This function will take care of all the steps needed to correctly
408 * allocate and set the user SID, please do use this function to create new
409 * users, messing with SIDs is not good.
411 * account_data must be provided initialized, pwd may be null.
412 * SSS
413 ***************************************************************************/
415 static NTSTATUS pdb_set_sam_sids(SAM_ACCOUNT *account_data, const struct passwd *pwd)
417 const char *guest_account = lp_guestaccount();
418 GROUP_MAP map;
419 BOOL ret;
421 if (!account_data || !pwd) {
422 return NT_STATUS_INVALID_PARAMETER;
425 /* this is a hack this thing should not be set
426 this way --SSS */
427 if (!(guest_account && *guest_account)) {
428 DEBUG(1, ("NULL guest account!?!?\n"));
429 return NT_STATUS_UNSUCCESSFUL;
430 } else {
431 /* Ensure this *must* be set right */
432 if (strcmp(pwd->pw_name, guest_account) == 0) {
433 if (!pdb_set_user_sid_from_rid(account_data, DOMAIN_USER_RID_GUEST, PDB_DEFAULT)) {
434 return NT_STATUS_UNSUCCESSFUL;
436 if (!pdb_set_group_sid_from_rid(account_data, DOMAIN_GROUP_RID_GUESTS, PDB_DEFAULT)) {
437 return NT_STATUS_UNSUCCESSFUL;
439 return NT_STATUS_OK;
443 if (!pdb_set_user_sid_from_rid(account_data, fallback_pdb_uid_to_user_rid(pwd->pw_uid), PDB_SET)) {
444 DEBUG(0,("Can't set User SID from RID!\n"));
445 return NT_STATUS_INVALID_PARAMETER;
448 /* call the mapping code here */
449 become_root();
450 ret = pdb_getgrgid(&map, pwd->pw_gid);
451 unbecome_root();
453 if( ret ) {
454 if (!pdb_set_group_sid(account_data, &map.sid, PDB_SET)){
455 DEBUG(0,("Can't set Group SID!\n"));
456 return NT_STATUS_INVALID_PARAMETER;
459 else {
460 if (!pdb_set_group_sid_from_rid(account_data, pdb_gid_to_group_rid(pwd->pw_gid), PDB_SET)) {
461 DEBUG(0,("Can't set Group SID\n"));
462 return NT_STATUS_INVALID_PARAMETER;
466 return NT_STATUS_OK;
469 /**********************************************************
470 Encode the account control bits into a string.
471 length = length of string to encode into (including terminating
472 null). length *MUST BE MORE THAN 2* !
473 **********************************************************/
475 char *pdb_encode_acct_ctrl(uint16 acct_ctrl, size_t length)
477 static fstring acct_str;
479 SMB_ASSERT(length <= sizeof(acct_str));
481 size_t i = 0;
483 acct_str[i++] = '[';
485 if (acct_ctrl & ACB_PWNOTREQ ) acct_str[i++] = 'N';
486 if (acct_ctrl & ACB_DISABLED ) acct_str[i++] = 'D';
487 if (acct_ctrl & ACB_HOMDIRREQ) acct_str[i++] = 'H';
488 if (acct_ctrl & ACB_TEMPDUP ) acct_str[i++] = 'T';
489 if (acct_ctrl & ACB_NORMAL ) acct_str[i++] = 'U';
490 if (acct_ctrl & ACB_MNS ) acct_str[i++] = 'M';
491 if (acct_ctrl & ACB_WSTRUST ) acct_str[i++] = 'W';
492 if (acct_ctrl & ACB_SVRTRUST ) acct_str[i++] = 'S';
493 if (acct_ctrl & ACB_AUTOLOCK ) acct_str[i++] = 'L';
494 if (acct_ctrl & ACB_PWNOEXP ) acct_str[i++] = 'X';
495 if (acct_ctrl & ACB_DOMTRUST ) acct_str[i++] = 'I';
497 for ( ; i < length - 2 ; i++ )
498 acct_str[i] = ' ';
500 i = length - 2;
501 acct_str[i++] = ']';
502 acct_str[i++] = '\0';
504 return acct_str;
507 /**********************************************************
508 Decode the account control bits from a string.
509 **********************************************************/
511 uint16 pdb_decode_acct_ctrl(const char *p)
513 uint16 acct_ctrl = 0;
514 BOOL finished = False;
517 * Check if the account type bits have been encoded after the
518 * NT password (in the form [NDHTUWSLXI]).
521 if (*p != '[')
522 return 0;
524 for (p++; *p && !finished; p++) {
525 switch (*p) {
526 case 'N': { acct_ctrl |= ACB_PWNOTREQ ; break; /* 'N'o password. */ }
527 case 'D': { acct_ctrl |= ACB_DISABLED ; break; /* 'D'isabled. */ }
528 case 'H': { acct_ctrl |= ACB_HOMDIRREQ; break; /* 'H'omedir required. */ }
529 case 'T': { acct_ctrl |= ACB_TEMPDUP ; break; /* 'T'emp account. */ }
530 case 'U': { acct_ctrl |= ACB_NORMAL ; break; /* 'U'ser account (normal). */ }
531 case 'M': { acct_ctrl |= ACB_MNS ; break; /* 'M'NS logon user account. What is this ? */ }
532 case 'W': { acct_ctrl |= ACB_WSTRUST ; break; /* 'W'orkstation account. */ }
533 case 'S': { acct_ctrl |= ACB_SVRTRUST ; break; /* 'S'erver account. */ }
534 case 'L': { acct_ctrl |= ACB_AUTOLOCK ; break; /* 'L'ocked account. */ }
535 case 'X': { acct_ctrl |= ACB_PWNOEXP ; break; /* No 'X'piry on password */ }
536 case 'I': { acct_ctrl |= ACB_DOMTRUST ; break; /* 'I'nterdomain trust account. */ }
537 case ' ': { break; }
538 case ':':
539 case '\n':
540 case '\0':
541 case ']':
542 default: { finished = True; }
546 return acct_ctrl;
549 /*************************************************************
550 Routine to set 32 hex password characters from a 16 byte array.
551 **************************************************************/
553 void pdb_sethexpwd(char *p, const unsigned char *pwd, uint16 acct_ctrl)
555 if (pwd != NULL) {
556 int i;
557 for (i = 0; i < 16; i++)
558 slprintf(&p[i*2], 3, "%02X", pwd[i]);
559 } else {
560 if (acct_ctrl & ACB_PWNOTREQ)
561 safe_strcpy(p, "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX", 33);
562 else
563 safe_strcpy(p, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 33);
567 /*************************************************************
568 Routine to get the 32 hex characters and turn them
569 into a 16 byte array.
570 **************************************************************/
572 BOOL pdb_gethexpwd(const char *p, unsigned char *pwd)
574 int i;
575 unsigned char lonybble, hinybble;
576 const char *hexchars = "0123456789ABCDEF";
577 char *p1, *p2;
579 if (!p)
580 return (False);
582 for (i = 0; i < 32; i += 2) {
583 hinybble = toupper(p[i]);
584 lonybble = toupper(p[i + 1]);
586 p1 = strchr(hexchars, hinybble);
587 p2 = strchr(hexchars, lonybble);
589 if (!p1 || !p2)
590 return (False);
592 hinybble = PTR_DIFF(p1, hexchars);
593 lonybble = PTR_DIFF(p2, hexchars);
595 pwd[i / 2] = (hinybble << 4) | lonybble;
597 return (True);
600 int algorithmic_rid_base(void)
602 static int rid_offset = 0;
604 if (rid_offset != 0)
605 return rid_offset;
607 rid_offset = lp_algorithmic_rid_base();
609 if (rid_offset < BASE_RID) {
610 /* Try to prevent admin foot-shooting, we can't put algorithmic
611 rids below 1000, that's the 'well known RIDs' on NT */
612 DEBUG(0, ("'algorithmic rid base' must be equal to or above %ld\n", BASE_RID));
613 rid_offset = BASE_RID;
615 if (rid_offset & 1) {
616 DEBUG(0, ("algorithmic rid base must be even\n"));
617 rid_offset += 1;
619 return rid_offset;
622 /*******************************************************************
623 Converts NT user RID to a UNIX uid.
624 ********************************************************************/
626 uid_t fallback_pdb_user_rid_to_uid(uint32 user_rid)
628 int rid_offset = algorithmic_rid_base();
629 return (uid_t)(((user_rid & (~USER_RID_TYPE)) - rid_offset)/RID_MULTIPLIER);
632 /*******************************************************************
633 converts UNIX uid to an NT User RID.
634 ********************************************************************/
636 uint32 fallback_pdb_uid_to_user_rid(uid_t uid)
638 int rid_offset = algorithmic_rid_base();
639 return (((((uint32)uid)*RID_MULTIPLIER) + rid_offset) | USER_RID_TYPE);
642 /*******************************************************************
643 Converts NT group RID to a UNIX gid.
644 ********************************************************************/
646 gid_t pdb_group_rid_to_gid(uint32 group_rid)
648 int rid_offset = algorithmic_rid_base();
649 return (gid_t)(((group_rid & (~GROUP_RID_TYPE))- rid_offset)/RID_MULTIPLIER);
652 /*******************************************************************
653 converts NT Group RID to a UNIX uid.
655 warning: you must not call that function only
656 you must do a call to the group mapping first.
657 there is not anymore a direct link between the gid and the rid.
658 ********************************************************************/
660 uint32 pdb_gid_to_group_rid(gid_t gid)
662 int rid_offset = algorithmic_rid_base();
663 return (((((uint32)gid)*RID_MULTIPLIER) + rid_offset) | GROUP_RID_TYPE);
666 /*******************************************************************
667 Decides if a RID is a well known RID.
668 ********************************************************************/
670 static BOOL pdb_rid_is_well_known(uint32 rid)
672 /* Not using rid_offset here, because this is the actual
673 NT fixed value (1000) */
675 return (rid < BASE_RID);
678 /*******************************************************************
679 Decides if a RID is a user or group RID.
680 ********************************************************************/
682 BOOL fallback_pdb_rid_is_user(uint32 rid)
684 /* lkcl i understand that NT attaches an enumeration to a RID
685 * such that it can be identified as either a user, group etc
686 * type. there are 5 such categories, and they are documented.
688 /* However, they are not in the RID, just somthing you can query
689 seperatly. Sorry luke :-) */
691 if(pdb_rid_is_well_known(rid)) {
693 * The only well known user RIDs are DOMAIN_USER_RID_ADMIN
694 * and DOMAIN_USER_RID_GUEST.
696 if(rid == DOMAIN_USER_RID_ADMIN || rid == DOMAIN_USER_RID_GUEST)
697 return True;
698 } else if((rid & RID_TYPE_MASK) == USER_RID_TYPE) {
699 return True;
701 return False;
704 /*******************************************************************
705 Convert a rid into a name. Used in the lookup SID rpc.
706 ********************************************************************/
708 BOOL local_lookup_sid(DOM_SID *sid, char *name, enum SID_NAME_USE *psid_name_use)
710 uint32 rid;
711 SAM_ACCOUNT *sam_account = NULL;
712 GROUP_MAP map;
713 BOOL ret;
715 if (!sid_peek_check_rid(get_global_sam_sid(), sid, &rid)){
716 DEBUG(0,("local_lookup_sid: sid_peek_check_rid return False! SID: %s\n",
717 sid_string_static(&map.sid)));
718 return False;
720 *psid_name_use = SID_NAME_UNKNOWN;
722 DEBUG(5,("local_lookup_sid: looking up RID %u.\n", (unsigned int)rid));
724 if (rid == DOMAIN_USER_RID_ADMIN) {
725 const char **admin_list = lp_admin_users(-1);
726 *psid_name_use = SID_NAME_USER;
727 if (admin_list) {
728 const char *p = *admin_list;
729 if(!next_token(&p, name, NULL, sizeof(fstring)))
730 fstrcpy(name, "Administrator");
731 } else {
732 fstrcpy(name, "Administrator");
734 return True;
737 if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_account))) {
738 return False;
741 /* see if the passdb can help us with the name of the user */
743 /* BEING ROOT BLLOCK */
744 become_root();
745 if (pdb_getsampwsid(sam_account, sid)) {
746 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
747 fstrcpy(name, pdb_get_username(sam_account));
748 *psid_name_use = SID_NAME_USER;
750 pdb_free_sam(&sam_account);
752 return True;
754 pdb_free_sam(&sam_account);
756 ret = pdb_getgrsid(&map, *sid);
757 unbecome_root();
758 /* END BECOME_ROOT BLOCK */
760 if ( ret ) {
761 if (map.gid!=(gid_t)-1) {
762 DEBUG(5,("local_lookup_sid: mapped group %s to gid %u\n", map.nt_name, (unsigned int)map.gid));
763 } else {
764 DEBUG(5,("local_lookup_sid: mapped group %s to no unix gid. Returning name.\n", map.nt_name));
767 fstrcpy(name, map.nt_name);
768 *psid_name_use = map.sid_name_use;
769 return True;
772 if (fallback_pdb_rid_is_user(rid)) {
773 uid_t uid;
774 struct passwd *pw = NULL;
776 DEBUG(5, ("assuming RID %u is a user\n", (unsigned)rid));
778 uid = fallback_pdb_user_rid_to_uid(rid);
779 pw = sys_getpwuid( uid );
781 DEBUG(5,("local_lookup_sid: looking up uid %u %s\n", (unsigned int)uid,
782 pw ? "succeeded" : "failed" ));
784 if ( !pw )
785 fstr_sprintf(name, "unix_user.%u", (unsigned int)uid);
786 else
787 fstrcpy( name, pw->pw_name );
789 DEBUG(5,("local_lookup_sid: found user %s for rid %u\n", name,
790 (unsigned int)rid ));
792 *psid_name_use = SID_NAME_USER;
794 return ( pw != NULL );
795 } else {
796 gid_t gid;
797 struct group *gr;
799 DEBUG(5, ("assuming RID %u is a group\n", (unsigned)rid));
801 gid = pdb_group_rid_to_gid(rid);
802 gr = getgrgid(gid);
804 *psid_name_use = SID_NAME_ALIAS;
806 DEBUG(5,("local_lookup_sid: looking up gid %u %s\n", (unsigned int)gid,
807 gr ? "succeeded" : "failed" ));
809 if( !gr )
810 fstr_sprintf(name, "unix_group.%u", (unsigned int)gid);
811 else
812 fstrcpy( name, gr->gr_name);
814 DEBUG(5,("local_lookup_sid: found group %s for rid %u\n", name,
815 (unsigned int)rid ));
817 /* assume fallback groups aer domain global groups */
819 *psid_name_use = SID_NAME_DOM_GRP;
821 return ( gr != NULL );
825 /*******************************************************************
826 Convert a name into a SID. Used in the lookup name rpc.
827 ********************************************************************/
829 BOOL local_lookup_name(const char *c_user, DOM_SID *psid, enum SID_NAME_USE *psid_name_use)
831 extern DOM_SID global_sid_World_Domain;
832 DOM_SID local_sid;
833 fstring user;
834 SAM_ACCOUNT *sam_account = NULL;
835 struct group *grp;
836 GROUP_MAP map;
838 *psid_name_use = SID_NAME_UNKNOWN;
841 * user may be quoted a const string, and map_username and
842 * friends can modify it. Make a modifiable copy. JRA.
845 fstrcpy(user, c_user);
847 sid_copy(&local_sid, get_global_sam_sid());
850 * Special case for MACHINE\Everyone. Map to the world_sid.
853 if(strequal(user, "Everyone")) {
854 sid_copy( psid, &global_sid_World_Domain);
855 sid_append_rid(psid, 0);
856 *psid_name_use = SID_NAME_ALIAS;
857 return True;
860 (void)map_username(user);
862 if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_account))) {
863 return False;
866 /* BEGIN ROOT BLOCK */
868 become_root();
869 if (pdb_getsampwnam(sam_account, user)) {
870 unbecome_root();
871 sid_copy(psid, pdb_get_user_sid(sam_account));
872 *psid_name_use = SID_NAME_USER;
874 pdb_free_sam(&sam_account);
875 return True;
878 pdb_free_sam(&sam_account);
881 * Maybe it was a group ?
884 /* check if it's a mapped group */
885 if (pdb_getgrnam(&map, user)) {
886 /* yes it's a mapped group */
887 sid_copy(&local_sid, &map.sid);
888 *psid_name_use = map.sid_name_use;
889 } else {
890 /* it's not a mapped group */
891 grp = getgrnam(user);
892 if(!grp) {
893 unbecome_root(); /* ---> exit form block */
894 return False;
898 *check if it's mapped, if it is reply it doesn't exist
900 * that's to prevent this case:
902 * unix group ug is mapped to nt group ng
903 * someone does a lookup on ug
904 * we must not reply as it doesn't "exist" anymore
905 * for NT. For NT only ng exists.
906 * JFM, 30/11/2001
909 if (pdb_getgrgid(&map, grp->gr_gid)){
910 unbecome_root(); /* ---> exit form block */
911 return False;
914 sid_append_rid( &local_sid, pdb_gid_to_group_rid(grp->gr_gid));
915 *psid_name_use = SID_NAME_ALIAS;
917 unbecome_root();
918 /* END ROOT BLOCK */
920 sid_copy( psid, &local_sid);
922 return True;
925 /*************************************************************
926 Change a password entry in the local smbpasswd file.
927 *************************************************************/
929 BOOL local_password_change(const char *user_name, int local_flags,
930 const char *new_passwd,
931 char *err_str, size_t err_str_len,
932 char *msg_str, size_t msg_str_len)
934 SAM_ACCOUNT *sam_pass=NULL;
935 uint16 other_acb;
937 *err_str = '\0';
938 *msg_str = '\0';
940 /* Get the smb passwd entry for this user */
941 pdb_init_sam(&sam_pass);
943 become_root();
944 if(!pdb_getsampwnam(sam_pass, user_name)) {
945 unbecome_root();
946 pdb_free_sam(&sam_pass);
948 if ((local_flags & LOCAL_ADD_USER) || (local_flags & LOCAL_DELETE_USER)) {
949 /* Might not exist in /etc/passwd. Use rid algorithm here */
950 if (!NT_STATUS_IS_OK(pdb_init_sam_new(&sam_pass, user_name, 0))) {
951 slprintf(err_str, err_str_len-1, "Failed to initialise SAM_ACCOUNT for user %s.\n", user_name);
952 return False;
954 } else {
955 slprintf(err_str, err_str_len-1,"Failed to find entry for user %s.\n", user_name);
956 return False;
958 } else {
959 unbecome_root();
960 /* the entry already existed */
961 local_flags &= ~LOCAL_ADD_USER;
964 /* the 'other' acb bits not being changed here */
965 other_acb = (pdb_get_acct_ctrl(sam_pass) & (!(ACB_WSTRUST|ACB_DOMTRUST|ACB_SVRTRUST|ACB_NORMAL)));
966 if (local_flags & LOCAL_TRUST_ACCOUNT) {
967 if (!pdb_set_acct_ctrl(sam_pass, ACB_WSTRUST | other_acb, PDB_CHANGED) ) {
968 slprintf(err_str, err_str_len - 1, "Failed to set 'trusted workstation account' flags for user %s.\n", user_name);
969 pdb_free_sam(&sam_pass);
970 return False;
972 } else if (local_flags & LOCAL_INTERDOM_ACCOUNT) {
973 if (!pdb_set_acct_ctrl(sam_pass, ACB_DOMTRUST | other_acb, PDB_CHANGED)) {
974 slprintf(err_str, err_str_len - 1, "Failed to set 'domain trust account' flags for user %s.\n", user_name);
975 pdb_free_sam(&sam_pass);
976 return False;
978 } else {
979 if (!pdb_set_acct_ctrl(sam_pass, ACB_NORMAL | other_acb, PDB_CHANGED)) {
980 slprintf(err_str, err_str_len - 1, "Failed to set 'normal account' flags for user %s.\n", user_name);
981 pdb_free_sam(&sam_pass);
982 return False;
987 * We are root - just write the new password
988 * and the valid last change time.
991 if (local_flags & LOCAL_DISABLE_USER) {
992 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_DISABLED, PDB_CHANGED)) {
993 slprintf(err_str, err_str_len-1, "Failed to set 'disabled' flag for user %s.\n", user_name);
994 pdb_free_sam(&sam_pass);
995 return False;
997 } else if (local_flags & LOCAL_ENABLE_USER) {
998 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED), PDB_CHANGED)) {
999 slprintf(err_str, err_str_len-1, "Failed to unset 'disabled' flag for user %s.\n", user_name);
1000 pdb_free_sam(&sam_pass);
1001 return False;
1005 if (local_flags & LOCAL_SET_NO_PASSWORD) {
1006 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_PWNOTREQ, PDB_CHANGED)) {
1007 slprintf(err_str, err_str_len-1, "Failed to set 'no password required' flag for user %s.\n", user_name);
1008 pdb_free_sam(&sam_pass);
1009 return False;
1011 } else if (local_flags & LOCAL_SET_PASSWORD) {
1013 * If we're dealing with setting a completely empty user account
1014 * ie. One with a password of 'XXXX', but not set disabled (like
1015 * an account created from scratch) then if the old password was
1016 * 'XX's then getsmbpwent will have set the ACB_DISABLED flag.
1017 * We remove that as we're giving this user their first password
1018 * and the decision hasn't really been made to disable them (ie.
1019 * don't create them disabled). JRA.
1021 if ((pdb_get_lanman_passwd(sam_pass)==NULL) && (pdb_get_acct_ctrl(sam_pass)&ACB_DISABLED)) {
1022 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED), PDB_CHANGED)) {
1023 slprintf(err_str, err_str_len-1, "Failed to unset 'disabled' flag for user %s.\n", user_name);
1024 pdb_free_sam(&sam_pass);
1025 return False;
1028 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_PWNOTREQ), PDB_CHANGED)) {
1029 slprintf(err_str, err_str_len-1, "Failed to unset 'no password required' flag for user %s.\n", user_name);
1030 pdb_free_sam(&sam_pass);
1031 return False;
1034 if (!pdb_set_plaintext_passwd (sam_pass, new_passwd)) {
1035 slprintf(err_str, err_str_len-1, "Failed to set password for user %s.\n", user_name);
1036 pdb_free_sam(&sam_pass);
1037 return False;
1041 if (local_flags & LOCAL_ADD_USER) {
1042 if (pdb_add_sam_account(sam_pass)) {
1043 slprintf(msg_str, msg_str_len-1, "Added user %s.\n", user_name);
1044 pdb_free_sam(&sam_pass);
1045 return True;
1046 } else {
1047 slprintf(err_str, err_str_len-1, "Failed to add entry for user %s.\n", user_name);
1048 pdb_free_sam(&sam_pass);
1049 return False;
1051 } else if (local_flags & LOCAL_DELETE_USER) {
1052 if (!pdb_delete_sam_account(sam_pass)) {
1053 slprintf(err_str,err_str_len-1, "Failed to delete entry for user %s.\n", user_name);
1054 pdb_free_sam(&sam_pass);
1055 return False;
1057 slprintf(msg_str, msg_str_len-1, "Deleted user %s.\n", user_name);
1058 } else {
1059 if(!pdb_update_sam_account(sam_pass)) {
1060 slprintf(err_str, err_str_len-1, "Failed to modify entry for user %s.\n", user_name);
1061 pdb_free_sam(&sam_pass);
1062 return False;
1064 if(local_flags & LOCAL_DISABLE_USER)
1065 slprintf(msg_str, msg_str_len-1, "Disabled user %s.\n", user_name);
1066 else if (local_flags & LOCAL_ENABLE_USER)
1067 slprintf(msg_str, msg_str_len-1, "Enabled user %s.\n", user_name);
1068 else if (local_flags & LOCAL_SET_NO_PASSWORD)
1069 slprintf(msg_str, msg_str_len-1, "User %s password set to none.\n", user_name);
1072 pdb_free_sam(&sam_pass);
1073 return True;
1076 /****************************************************************************
1077 Convert a uid to SID - algorithmic.
1078 ****************************************************************************/
1080 DOM_SID *algorithmic_uid_to_sid(DOM_SID *psid, uid_t uid)
1082 if ( !lp_enable_rid_algorithm() )
1083 return NULL;
1085 DEBUG(8,("algorithmic_uid_to_sid: falling back to RID algorithm\n"));
1086 sid_copy( psid, get_global_sam_sid() );
1087 sid_append_rid( psid, fallback_pdb_uid_to_user_rid(uid) );
1088 DEBUG(10,("algorithmic_uid_to_sid: uid (%d) -> SID %s.\n",
1089 (unsigned int)uid, sid_string_static(psid) ));
1091 return psid;
1094 /****************************************************************************
1095 Convert a uid to SID - locally.
1096 ****************************************************************************/
1098 DOM_SID *local_uid_to_sid(DOM_SID *psid, uid_t uid)
1100 SAM_ACCOUNT *sampw = NULL;
1101 struct passwd *unix_pw;
1102 BOOL ret;
1104 unix_pw = sys_getpwuid( uid );
1106 if ( !unix_pw ) {
1107 DEBUG(4,("local_uid_to_sid: host has no idea of uid %lu\n", (unsigned long)uid));
1108 return algorithmic_uid_to_sid( psid, uid);
1111 if ( !NT_STATUS_IS_OK(pdb_init_sam(&sampw)) ) {
1112 DEBUG(0,("local_uid_to_sid: failed to allocate SAM_ACCOUNT object\n"));
1113 return NULL;
1116 become_root();
1117 ret = pdb_getsampwnam( sampw, unix_pw->pw_name );
1118 unbecome_root();
1120 if ( ret )
1121 sid_copy( psid, pdb_get_user_sid(sampw) );
1122 else {
1123 DEBUG(4,("local_uid_to_sid: User %s [uid == %lu] has no samba account\n",
1124 unix_pw->pw_name, (unsigned long)uid));
1126 return algorithmic_uid_to_sid( psid, uid);
1129 DEBUG(10,("local_uid_to_sid: uid (%d) -> SID %s (%s).\n",
1130 (unsigned int)uid, sid_string_static(psid), unix_pw->pw_name));
1132 return psid;
1135 /****************************************************************************
1136 Convert a SID to uid - locally.
1137 ****************************************************************************/
1139 BOOL local_sid_to_uid(uid_t *puid, const DOM_SID *psid, enum SID_NAME_USE *name_type)
1141 SAM_ACCOUNT *sampw = NULL;
1142 struct passwd *unix_pw;
1143 const char *user_name;
1145 *name_type = SID_NAME_UNKNOWN;
1148 * We can only convert to a uid if this is our local
1149 * Domain SID (ie. we are the controling authority).
1151 if (!sid_check_is_in_our_domain(psid) ) {
1152 DEBUG(5,("local_sid_to_uid: this SID (%s) is not from our domain\n", sid_string_static(psid)));
1153 return False;
1156 /* lookup the user account */
1158 if ( !NT_STATUS_IS_OK(pdb_init_sam(&sampw)) ) {
1159 DEBUG(0,("local_sid_to_uid: Failed to allocate memory for SAM_ACCOUNT object\n"));
1160 return False;
1163 become_root();
1164 if ( !pdb_getsampwsid(sampw, psid) ) {
1165 unbecome_root();
1166 DEBUG(8,("local_sid_to_uid: Could not find SID %s in passdb\n",
1167 sid_string_static(psid)));
1168 return False;
1170 unbecome_root();
1172 user_name = pdb_get_username(sampw);
1174 unix_pw = sys_getpwnam( user_name );
1176 if ( !unix_pw ) {
1177 DEBUG(0,("local_sid_to_uid: %s found in passdb but getpwnam() return NULL!\n",
1178 user_name));
1179 pdb_free_sam( &sampw );
1180 return False;
1183 *puid = unix_pw->pw_uid;
1185 DEBUG(10,("local_sid_to_uid: SID %s -> uid (%u) (%s).\n", sid_string_static(psid),
1186 (unsigned int)*puid, user_name ));
1188 *name_type = SID_NAME_USER;
1190 return True;
1193 /****************************************************************************
1194 Convert a gid to SID - locally.
1195 ****************************************************************************/
1197 DOM_SID *local_gid_to_sid(DOM_SID *psid, gid_t gid)
1199 GROUP_MAP group;
1200 BOOL ret;
1202 /* we don't need to disable winbindd since the gid is stored in
1203 the GROUP_MAP object */
1205 /* done as root since ldap backend requires root to open a connection */
1207 become_root();
1208 ret = pdb_getgrgid( &group, gid );
1209 unbecome_root();
1211 if ( !ret ) {
1213 /* fallback to rid mapping if enabled */
1215 if ( lp_enable_rid_algorithm() ) {
1216 sid_copy(psid, get_global_sam_sid());
1217 sid_append_rid(psid, pdb_gid_to_group_rid(gid));
1219 DEBUG(10,("local_gid_to_sid: Fall back to algorithmic mapping: %u -> %s\n",
1220 (unsigned int)gid, sid_string_static(psid)));
1222 return psid;
1224 else
1225 return NULL;
1228 sid_copy( psid, &group.sid );
1230 DEBUG(10,("local_gid_to_sid: gid (%d) -> SID %s.\n",
1231 (unsigned int)gid, sid_string_static(psid)));
1233 return psid;
1236 /****************************************************************************
1237 Convert a SID to gid - locally.
1238 ****************************************************************************/
1240 BOOL local_sid_to_gid(gid_t *pgid, const DOM_SID *psid, enum SID_NAME_USE *name_type)
1242 uint32 rid;
1243 GROUP_MAP group;
1244 BOOL ret;
1246 *name_type = SID_NAME_UNKNOWN;
1248 /* This call can enumerate group mappings for foreign sids as well.
1249 So don't check for a match against our domain SID */
1251 /* we don't need to disable winbindd since the gid is stored in
1252 the GROUP_MAP object */
1254 become_root();
1255 ret = pdb_getgrsid(&group, *psid);
1256 unbecome_root();
1258 if ( !ret ) {
1260 /* fallback to rid mapping if enabled */
1262 if ( lp_enable_rid_algorithm() ) {
1264 if (!sid_check_is_in_our_domain(psid) ) {
1265 DEBUG(5,("local_sid_to_gid: RID algorithm only supported for our domain (%s is not)\n", sid_string_static(psid)));
1266 return False;
1269 if (!sid_peek_rid(psid, &rid)) {
1270 DEBUG(10,("local_sid_to_uid: invalid SID!\n"));
1271 return False;
1274 DEBUG(10,("local_sid_to_gid: Fall back to algorithmic mapping\n"));
1276 if (fallback_pdb_rid_is_user(rid)) {
1277 DEBUG(3, ("local_sid_to_gid: SID %s is *NOT* a group\n", sid_string_static(psid)));
1278 return False;
1279 } else {
1280 *pgid = pdb_group_rid_to_gid(rid);
1281 DEBUG(10,("local_sid_to_gid: mapping: %s -> %u\n", sid_string_static(psid), (unsigned int)(*pgid)));
1282 return True;
1286 return False;
1289 *pgid = group.gid;
1291 DEBUG(10,("local_sid_to_gid: SID %s -> gid (%u)\n", sid_string_static(psid),
1292 (unsigned int)*pgid));
1294 return True;
1297 /**********************************************************************
1298 Marshall/unmarshall SAM_ACCOUNT structs.
1299 *********************************************************************/
1301 #define TDB_FORMAT_STRING "ddddddBBBBBBBBBBBBddBBwdwdBwwd"
1303 /**********************************************************************
1304 Intialize a SAM_ACCOUNT struct from a BYTE buffer of size len
1305 *********************************************************************/
1307 BOOL init_sam_from_buffer(SAM_ACCOUNT *sampass, uint8 *buf, uint32 buflen)
1310 /* times are stored as 32bit integer
1311 take care on system with 64bit wide time_t
1312 --SSS */
1313 uint32 logon_time,
1314 logoff_time,
1315 kickoff_time,
1316 pass_last_set_time,
1317 pass_can_change_time,
1318 pass_must_change_time;
1319 char *username;
1320 char *domain;
1321 char *nt_username;
1322 char *dir_drive;
1323 char *unknown_str;
1324 char *munged_dial;
1325 char *fullname;
1326 char *homedir;
1327 char *logon_script;
1328 char *profile_path;
1329 char *acct_desc;
1330 char *workstations;
1331 uint32 username_len, domain_len, nt_username_len,
1332 dir_drive_len, unknown_str_len, munged_dial_len,
1333 fullname_len, homedir_len, logon_script_len,
1334 profile_path_len, acct_desc_len, workstations_len;
1336 uint32 user_rid, group_rid, unknown_3, hours_len, unknown_6;
1337 uint16 acct_ctrl, logon_divs;
1338 uint16 bad_password_count, logon_count;
1339 uint8 *hours;
1340 static uint8 *lm_pw_ptr, *nt_pw_ptr;
1341 uint32 len = 0;
1342 uint32 lm_pw_len, nt_pw_len, hourslen;
1343 BOOL ret = True;
1345 if(sampass == NULL || buf == NULL) {
1346 DEBUG(0, ("init_sam_from_buffer: NULL parameters found!\n"));
1347 return False;
1350 /* unpack the buffer into variables */
1351 len = tdb_unpack ((char *)buf, buflen, TDB_FORMAT_STRING,
1352 &logon_time,
1353 &logoff_time,
1354 &kickoff_time,
1355 &pass_last_set_time,
1356 &pass_can_change_time,
1357 &pass_must_change_time,
1358 &username_len, &username,
1359 &domain_len, &domain,
1360 &nt_username_len, &nt_username,
1361 &fullname_len, &fullname,
1362 &homedir_len, &homedir,
1363 &dir_drive_len, &dir_drive,
1364 &logon_script_len, &logon_script,
1365 &profile_path_len, &profile_path,
1366 &acct_desc_len, &acct_desc,
1367 &workstations_len, &workstations,
1368 &unknown_str_len, &unknown_str,
1369 &munged_dial_len, &munged_dial,
1370 &user_rid,
1371 &group_rid,
1372 &lm_pw_len, &lm_pw_ptr,
1373 &nt_pw_len, &nt_pw_ptr,
1374 &acct_ctrl,
1375 &unknown_3,
1376 &logon_divs,
1377 &hours_len,
1378 &hourslen, &hours,
1379 &bad_password_count,
1380 &logon_count,
1381 &unknown_6);
1383 if (len == -1) {
1384 ret = False;
1385 goto done;
1388 pdb_set_logon_time(sampass, logon_time, PDB_SET);
1389 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
1390 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
1391 pdb_set_pass_can_change_time(sampass, pass_can_change_time, PDB_SET);
1392 pdb_set_pass_must_change_time(sampass, pass_must_change_time, PDB_SET);
1393 pdb_set_pass_last_set_time(sampass, pass_last_set_time, PDB_SET);
1395 pdb_set_username(sampass, username, PDB_SET);
1396 pdb_set_domain(sampass, domain, PDB_SET);
1397 pdb_set_nt_username(sampass, nt_username, PDB_SET);
1398 pdb_set_fullname(sampass, fullname, PDB_SET);
1400 if (homedir) {
1401 pdb_set_homedir(sampass, homedir, PDB_SET);
1403 else {
1404 pdb_set_homedir(sampass,
1405 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_home()),
1406 PDB_DEFAULT);
1409 if (dir_drive)
1410 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
1411 else {
1412 pdb_set_dir_drive(sampass,
1413 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_drive()),
1414 PDB_DEFAULT);
1417 if (logon_script)
1418 pdb_set_logon_script(sampass, logon_script, PDB_SET);
1419 else {
1420 pdb_set_logon_script(sampass,
1421 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_script()),
1422 PDB_DEFAULT);
1425 if (profile_path) {
1426 pdb_set_profile_path(sampass, profile_path, PDB_SET);
1427 } else {
1428 pdb_set_profile_path(sampass,
1429 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_path()),
1430 PDB_DEFAULT);
1433 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
1434 pdb_set_workstations(sampass, workstations, PDB_SET);
1435 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
1437 if (lm_pw_ptr && lm_pw_len == LM_HASH_LEN) {
1438 if (!pdb_set_lanman_passwd(sampass, lm_pw_ptr, PDB_SET)) {
1439 ret = False;
1440 goto done;
1444 if (nt_pw_ptr && nt_pw_len == NT_HASH_LEN) {
1445 if (!pdb_set_nt_passwd(sampass, nt_pw_ptr, PDB_SET)) {
1446 ret = False;
1447 goto done;
1451 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
1452 pdb_set_group_sid_from_rid(sampass, group_rid, PDB_SET);
1453 pdb_set_unknown_3(sampass, unknown_3, PDB_SET);
1454 pdb_set_hours_len(sampass, hours_len, PDB_SET);
1455 pdb_set_bad_password_count(sampass, bad_password_count, PDB_SET);
1456 pdb_set_logon_count(sampass, logon_count, PDB_SET);
1457 pdb_set_unknown_6(sampass, unknown_6, PDB_SET);
1458 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
1459 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
1460 pdb_set_hours(sampass, hours, PDB_SET);
1462 done:
1464 SAFE_FREE(username);
1465 SAFE_FREE(domain);
1466 SAFE_FREE(nt_username);
1467 SAFE_FREE(fullname);
1468 SAFE_FREE(homedir);
1469 SAFE_FREE(dir_drive);
1470 SAFE_FREE(logon_script);
1471 SAFE_FREE(profile_path);
1472 SAFE_FREE(acct_desc);
1473 SAFE_FREE(workstations);
1474 SAFE_FREE(munged_dial);
1475 SAFE_FREE(unknown_str);
1476 SAFE_FREE(hours);
1478 return ret;
1481 /**********************************************************************
1482 Intialize a BYTE buffer from a SAM_ACCOUNT struct
1483 *********************************************************************/
1485 uint32 init_buffer_from_sam (uint8 **buf, const SAM_ACCOUNT *sampass, BOOL size_only)
1487 size_t len, buflen;
1489 /* times are stored as 32bit integer
1490 take care on system with 64bit wide time_t
1491 --SSS */
1492 uint32 logon_time,
1493 logoff_time,
1494 kickoff_time,
1495 pass_last_set_time,
1496 pass_can_change_time,
1497 pass_must_change_time;
1499 uint32 user_rid, group_rid;
1501 const char *username;
1502 const char *domain;
1503 const char *nt_username;
1504 const char *dir_drive;
1505 const char *unknown_str;
1506 const char *munged_dial;
1507 const char *fullname;
1508 const char *homedir;
1509 const char *logon_script;
1510 const char *profile_path;
1511 const char *acct_desc;
1512 const char *workstations;
1513 uint32 username_len, domain_len, nt_username_len,
1514 dir_drive_len, unknown_str_len, munged_dial_len,
1515 fullname_len, homedir_len, logon_script_len,
1516 profile_path_len, acct_desc_len, workstations_len;
1518 const uint8 *lm_pw;
1519 const uint8 *nt_pw;
1520 uint32 lm_pw_len = 16;
1521 uint32 nt_pw_len = 16;
1523 /* do we have a valid SAM_ACCOUNT pointer? */
1524 if (sampass == NULL) {
1525 DEBUG(0, ("init_buffer_from_sam: SAM_ACCOUNT is NULL!\n"));
1526 return -1;
1529 *buf = NULL;
1530 buflen = 0;
1532 logon_time = (uint32)pdb_get_logon_time(sampass);
1533 logoff_time = (uint32)pdb_get_logoff_time(sampass);
1534 kickoff_time = (uint32)pdb_get_kickoff_time(sampass);
1535 pass_can_change_time = (uint32)pdb_get_pass_can_change_time(sampass);
1536 pass_must_change_time = (uint32)pdb_get_pass_must_change_time(sampass);
1537 pass_last_set_time = (uint32)pdb_get_pass_last_set_time(sampass);
1539 user_rid = pdb_get_user_rid(sampass);
1540 group_rid = pdb_get_group_rid(sampass);
1542 username = pdb_get_username(sampass);
1543 if (username)
1544 username_len = strlen(username) +1;
1545 else
1546 username_len = 0;
1548 domain = pdb_get_domain(sampass);
1549 if (domain)
1550 domain_len = strlen(domain) +1;
1551 else
1552 domain_len = 0;
1554 nt_username = pdb_get_nt_username(sampass);
1555 if (nt_username)
1556 nt_username_len = strlen(nt_username) +1;
1557 else
1558 nt_username_len = 0;
1560 fullname = pdb_get_fullname(sampass);
1561 if (fullname)
1562 fullname_len = strlen(fullname) +1;
1563 else
1564 fullname_len = 0;
1567 * Only updates fields which have been set (not defaults from smb.conf)
1570 if (!IS_SAM_DEFAULT(sampass, PDB_DRIVE))
1571 dir_drive = pdb_get_dir_drive(sampass);
1572 else
1573 dir_drive = NULL;
1574 if (dir_drive)
1575 dir_drive_len = strlen(dir_drive) +1;
1576 else
1577 dir_drive_len = 0;
1579 if (!IS_SAM_DEFAULT(sampass, PDB_SMBHOME))
1580 homedir = pdb_get_homedir(sampass);
1581 else
1582 homedir = NULL;
1583 if (homedir)
1584 homedir_len = strlen(homedir) +1;
1585 else
1586 homedir_len = 0;
1588 if (!IS_SAM_DEFAULT(sampass, PDB_LOGONSCRIPT))
1589 logon_script = pdb_get_logon_script(sampass);
1590 else
1591 logon_script = NULL;
1592 if (logon_script)
1593 logon_script_len = strlen(logon_script) +1;
1594 else
1595 logon_script_len = 0;
1597 if (!IS_SAM_DEFAULT(sampass, PDB_PROFILE))
1598 profile_path = pdb_get_profile_path(sampass);
1599 else
1600 profile_path = NULL;
1601 if (profile_path)
1602 profile_path_len = strlen(profile_path) +1;
1603 else
1604 profile_path_len = 0;
1606 lm_pw = pdb_get_lanman_passwd(sampass);
1607 if (!lm_pw)
1608 lm_pw_len = 0;
1610 nt_pw = pdb_get_nt_passwd(sampass);
1611 if (!nt_pw)
1612 nt_pw_len = 0;
1614 acct_desc = pdb_get_acct_desc(sampass);
1615 if (acct_desc)
1616 acct_desc_len = strlen(acct_desc) +1;
1617 else
1618 acct_desc_len = 0;
1620 workstations = pdb_get_workstations(sampass);
1621 if (workstations)
1622 workstations_len = strlen(workstations) +1;
1623 else
1624 workstations_len = 0;
1626 unknown_str = NULL;
1627 unknown_str_len = 0;
1629 munged_dial = pdb_get_munged_dial(sampass);
1630 if (munged_dial)
1631 munged_dial_len = strlen(munged_dial) +1;
1632 else
1633 munged_dial_len = 0;
1635 /* one time to get the size needed */
1636 len = tdb_pack(NULL, 0, TDB_FORMAT_STRING,
1637 logon_time,
1638 logoff_time,
1639 kickoff_time,
1640 pass_last_set_time,
1641 pass_can_change_time,
1642 pass_must_change_time,
1643 username_len, username,
1644 domain_len, domain,
1645 nt_username_len, nt_username,
1646 fullname_len, fullname,
1647 homedir_len, homedir,
1648 dir_drive_len, dir_drive,
1649 logon_script_len, logon_script,
1650 profile_path_len, profile_path,
1651 acct_desc_len, acct_desc,
1652 workstations_len, workstations,
1653 unknown_str_len, unknown_str,
1654 munged_dial_len, munged_dial,
1655 user_rid,
1656 group_rid,
1657 lm_pw_len, lm_pw,
1658 nt_pw_len, nt_pw,
1659 pdb_get_acct_ctrl(sampass),
1660 pdb_get_unknown_3(sampass),
1661 pdb_get_logon_divs(sampass),
1662 pdb_get_hours_len(sampass),
1663 MAX_HOURS_LEN, pdb_get_hours(sampass),
1664 pdb_get_bad_password_count(sampass),
1665 pdb_get_logon_count(sampass),
1666 pdb_get_unknown_6(sampass));
1669 if (size_only)
1670 return buflen;
1672 /* malloc the space needed */
1673 if ( (*buf=(uint8*)malloc(len)) == NULL) {
1674 DEBUG(0,("init_buffer_from_sam: Unable to malloc() memory for buffer!\n"));
1675 return (-1);
1678 /* now for the real call to tdb_pack() */
1679 buflen = tdb_pack((char *)*buf, len, TDB_FORMAT_STRING,
1680 logon_time,
1681 logoff_time,
1682 kickoff_time,
1683 pass_last_set_time,
1684 pass_can_change_time,
1685 pass_must_change_time,
1686 username_len, username,
1687 domain_len, domain,
1688 nt_username_len, nt_username,
1689 fullname_len, fullname,
1690 homedir_len, homedir,
1691 dir_drive_len, dir_drive,
1692 logon_script_len, logon_script,
1693 profile_path_len, profile_path,
1694 acct_desc_len, acct_desc,
1695 workstations_len, workstations,
1696 unknown_str_len, unknown_str,
1697 munged_dial_len, munged_dial,
1698 user_rid,
1699 group_rid,
1700 lm_pw_len, lm_pw,
1701 nt_pw_len, nt_pw,
1702 pdb_get_acct_ctrl(sampass),
1703 pdb_get_unknown_3(sampass),
1704 pdb_get_logon_divs(sampass),
1705 pdb_get_hours_len(sampass),
1706 MAX_HOURS_LEN, pdb_get_hours(sampass),
1707 pdb_get_bad_password_count(sampass),
1708 pdb_get_logon_count(sampass),
1709 pdb_get_unknown_6(sampass));
1712 /* check to make sure we got it correct */
1713 if (buflen != len) {
1714 DEBUG(0, ("init_buffer_from_sam: somthing odd is going on here: bufflen (%lu) != len (%lu) in tdb_pack operations!\n",
1715 (unsigned long)buflen, (unsigned long)len));
1716 /* error */
1717 SAFE_FREE (*buf);
1718 return (-1);
1721 return (buflen);
1725 /**********************************************************************
1726 **********************************************************************/
1728 static BOOL get_free_ugid_range(uint32 *low, uint32 *high)
1730 uid_t u_low, u_high;
1731 gid_t g_low, g_high;
1733 if (!lp_idmap_uid(&u_low, &u_high) || !lp_idmap_gid(&g_low, &g_high)) {
1734 return False;
1737 *low = (u_low < g_low) ? u_low : g_low;
1738 *high = (u_high < g_high) ? u_high : g_high;
1740 return True;
1743 /******************************************************************
1744 Get the the non-algorithmic RID range if idmap range are defined
1745 ******************************************************************/
1747 BOOL get_free_rid_range(uint32 *low, uint32 *high)
1749 uint32 id_low, id_high;
1751 if (!lp_enable_rid_algorithm()) {
1752 *low = BASE_RID;
1753 *high = (uint32)-1;
1756 if (!get_free_ugid_range(&id_low, &id_high)) {
1757 return False;
1760 *low = fallback_pdb_uid_to_user_rid(id_low);
1761 if (fallback_pdb_user_rid_to_uid((uint32)-1) < id_high) {
1762 *high = (uint32)-1;
1763 } else {
1764 *high = fallback_pdb_uid_to_user_rid(id_high);
1767 return True;