Typo fix.
[Samba.git] / source / passdb / passdb.c
blob8fbd8d4cdc62ee411dc72dc3b62eccf65e87116b
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 Alloc 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 Allocates 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 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;
478 size_t i = 0;
480 acct_str[i++] = '[';
482 if (acct_ctrl & ACB_PWNOTREQ ) acct_str[i++] = 'N';
483 if (acct_ctrl & ACB_DISABLED ) acct_str[i++] = 'D';
484 if (acct_ctrl & ACB_HOMDIRREQ) acct_str[i++] = 'H';
485 if (acct_ctrl & ACB_TEMPDUP ) acct_str[i++] = 'T';
486 if (acct_ctrl & ACB_NORMAL ) acct_str[i++] = 'U';
487 if (acct_ctrl & ACB_MNS ) acct_str[i++] = 'M';
488 if (acct_ctrl & ACB_WSTRUST ) acct_str[i++] = 'W';
489 if (acct_ctrl & ACB_SVRTRUST ) acct_str[i++] = 'S';
490 if (acct_ctrl & ACB_AUTOLOCK ) acct_str[i++] = 'L';
491 if (acct_ctrl & ACB_PWNOEXP ) acct_str[i++] = 'X';
492 if (acct_ctrl & ACB_DOMTRUST ) acct_str[i++] = 'I';
494 for ( ; i < length - 2 ; i++ )
495 acct_str[i] = ' ';
497 i = length - 2;
498 acct_str[i++] = ']';
499 acct_str[i++] = '\0';
501 return acct_str;
504 /**********************************************************
505 Decode the account control bits from a string.
506 **********************************************************/
508 uint16 pdb_decode_acct_ctrl(const char *p)
510 uint16 acct_ctrl = 0;
511 BOOL finished = False;
514 * Check if the account type bits have been encoded after the
515 * NT password (in the form [NDHTUWSLXI]).
518 if (*p != '[')
519 return 0;
521 for (p++; *p && !finished; p++) {
522 switch (*p) {
523 case 'N': { acct_ctrl |= ACB_PWNOTREQ ; break; /* 'N'o password. */ }
524 case 'D': { acct_ctrl |= ACB_DISABLED ; break; /* 'D'isabled. */ }
525 case 'H': { acct_ctrl |= ACB_HOMDIRREQ; break; /* 'H'omedir required. */ }
526 case 'T': { acct_ctrl |= ACB_TEMPDUP ; break; /* 'T'emp account. */ }
527 case 'U': { acct_ctrl |= ACB_NORMAL ; break; /* 'U'ser account (normal). */ }
528 case 'M': { acct_ctrl |= ACB_MNS ; break; /* 'M'NS logon user account. What is this ? */ }
529 case 'W': { acct_ctrl |= ACB_WSTRUST ; break; /* 'W'orkstation account. */ }
530 case 'S': { acct_ctrl |= ACB_SVRTRUST ; break; /* 'S'erver account. */ }
531 case 'L': { acct_ctrl |= ACB_AUTOLOCK ; break; /* 'L'ocked account. */ }
532 case 'X': { acct_ctrl |= ACB_PWNOEXP ; break; /* No 'X'piry on password */ }
533 case 'I': { acct_ctrl |= ACB_DOMTRUST ; break; /* 'I'nterdomain trust account. */ }
534 case ' ': { break; }
535 case ':':
536 case '\n':
537 case '\0':
538 case ']':
539 default: { finished = True; }
543 return acct_ctrl;
546 /*************************************************************
547 Routine to set 32 hex password characters from a 16 byte array.
548 **************************************************************/
550 void pdb_sethexpwd(char *p, const unsigned char *pwd, uint16 acct_ctrl)
552 if (pwd != NULL) {
553 int i;
554 for (i = 0; i < 16; i++)
555 slprintf(&p[i*2], 3, "%02X", pwd[i]);
556 } else {
557 if (acct_ctrl & ACB_PWNOTREQ)
558 safe_strcpy(p, "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX", 33);
559 else
560 safe_strcpy(p, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 33);
564 /*************************************************************
565 Routine to get the 32 hex characters and turn them
566 into a 16 byte array.
567 **************************************************************/
569 BOOL pdb_gethexpwd(const char *p, unsigned char *pwd)
571 int i;
572 unsigned char lonybble, hinybble;
573 const char *hexchars = "0123456789ABCDEF";
574 char *p1, *p2;
576 if (!p)
577 return (False);
579 for (i = 0; i < 32; i += 2) {
580 hinybble = toupper(p[i]);
581 lonybble = toupper(p[i + 1]);
583 p1 = strchr(hexchars, hinybble);
584 p2 = strchr(hexchars, lonybble);
586 if (!p1 || !p2)
587 return (False);
589 hinybble = PTR_DIFF(p1, hexchars);
590 lonybble = PTR_DIFF(p2, hexchars);
592 pwd[i / 2] = (hinybble << 4) | lonybble;
594 return (True);
597 int algorithmic_rid_base(void)
599 static int rid_offset = 0;
601 if (rid_offset != 0)
602 return rid_offset;
604 rid_offset = lp_algorithmic_rid_base();
606 if (rid_offset < BASE_RID) {
607 /* Try to prevent admin foot-shooting, we can't put algorithmic
608 rids below 1000, that's the 'well known RIDs' on NT */
609 DEBUG(0, ("'algorithmic rid base' must be equal to or above %ld\n", BASE_RID));
610 rid_offset = BASE_RID;
612 if (rid_offset & 1) {
613 DEBUG(0, ("algorithmic rid base must be even\n"));
614 rid_offset += 1;
616 return rid_offset;
619 /*******************************************************************
620 Converts NT user RID to a UNIX uid.
621 ********************************************************************/
623 uid_t fallback_pdb_user_rid_to_uid(uint32 user_rid)
625 int rid_offset = algorithmic_rid_base();
626 return (uid_t)(((user_rid & (~USER_RID_TYPE)) - rid_offset)/RID_MULTIPLIER);
629 /*******************************************************************
630 converts UNIX uid to an NT User RID.
631 ********************************************************************/
633 uint32 fallback_pdb_uid_to_user_rid(uid_t uid)
635 int rid_offset = algorithmic_rid_base();
636 return (((((uint32)uid)*RID_MULTIPLIER) + rid_offset) | USER_RID_TYPE);
639 /*******************************************************************
640 Converts NT group RID to a UNIX gid.
641 ********************************************************************/
643 gid_t pdb_group_rid_to_gid(uint32 group_rid)
645 int rid_offset = algorithmic_rid_base();
646 return (gid_t)(((group_rid & (~GROUP_RID_TYPE))- rid_offset)/RID_MULTIPLIER);
649 /*******************************************************************
650 converts NT Group RID to a UNIX uid.
652 warning: you must not call that function only
653 you must do a call to the group mapping first.
654 there is not anymore a direct link between the gid and the rid.
655 ********************************************************************/
657 uint32 pdb_gid_to_group_rid(gid_t gid)
659 int rid_offset = algorithmic_rid_base();
660 return (((((uint32)gid)*RID_MULTIPLIER) + rid_offset) | GROUP_RID_TYPE);
663 /*******************************************************************
664 Decides if a RID is a well known RID.
665 ********************************************************************/
667 static BOOL pdb_rid_is_well_known(uint32 rid)
669 /* Not using rid_offset here, because this is the actual
670 NT fixed value (1000) */
672 return (rid < BASE_RID);
675 /*******************************************************************
676 Decides if a RID is a user or group RID.
677 ********************************************************************/
679 BOOL fallback_pdb_rid_is_user(uint32 rid)
681 /* lkcl i understand that NT attaches an enumeration to a RID
682 * such that it can be identified as either a user, group etc
683 * type. there are 5 such categories, and they are documented.
685 /* However, they are not in the RID, just somthing you can query
686 seperatly. Sorry luke :-) */
688 if(pdb_rid_is_well_known(rid)) {
690 * The only well known user RIDs are DOMAIN_USER_RID_ADMIN
691 * and DOMAIN_USER_RID_GUEST.
693 if(rid == DOMAIN_USER_RID_ADMIN || rid == DOMAIN_USER_RID_GUEST)
694 return True;
695 } else if((rid & RID_TYPE_MASK) == USER_RID_TYPE) {
696 return True;
698 return False;
701 /*******************************************************************
702 Convert a rid into a name. Used in the lookup SID rpc.
703 ********************************************************************/
705 BOOL local_lookup_sid(DOM_SID *sid, char *name, enum SID_NAME_USE *psid_name_use)
707 uint32 rid;
708 SAM_ACCOUNT *sam_account = NULL;
709 GROUP_MAP map;
710 BOOL ret;
712 if (!sid_peek_check_rid(get_global_sam_sid(), sid, &rid)){
713 DEBUG(0,("local_lookup_sid: sid_peek_check_rid return False! SID: %s\n",
714 sid_string_static(&map.sid)));
715 return False;
717 *psid_name_use = SID_NAME_UNKNOWN;
719 DEBUG(5,("local_lookup_sid: looking up RID %u.\n", (unsigned int)rid));
721 if (rid == DOMAIN_USER_RID_ADMIN) {
722 const char **admin_list = lp_admin_users(-1);
723 *psid_name_use = SID_NAME_USER;
724 if (admin_list) {
725 const char *p = *admin_list;
726 if(!next_token(&p, name, NULL, sizeof(fstring)))
727 fstrcpy(name, "Administrator");
728 } else {
729 fstrcpy(name, "Administrator");
731 return True;
734 if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_account))) {
735 return False;
738 /* see if the passdb can help us with the name of the user */
740 /* BEING ROOT BLLOCK */
741 become_root();
742 if (pdb_getsampwsid(sam_account, sid)) {
743 unbecome_root(); /* -----> EXIT BECOME_ROOT() */
744 fstrcpy(name, pdb_get_username(sam_account));
745 *psid_name_use = SID_NAME_USER;
747 pdb_free_sam(&sam_account);
749 return True;
751 pdb_free_sam(&sam_account);
753 ret = pdb_getgrsid(&map, *sid);
754 unbecome_root();
755 /* END BECOME_ROOT BLOCK */
757 if ( ret ) {
758 if (map.gid!=(gid_t)-1) {
759 DEBUG(5,("local_lookup_sid: mapped group %s to gid %u\n", map.nt_name, (unsigned int)map.gid));
760 } else {
761 DEBUG(5,("local_lookup_sid: mapped group %s to no unix gid. Returning name.\n", map.nt_name));
764 fstrcpy(name, map.nt_name);
765 *psid_name_use = map.sid_name_use;
766 return True;
769 if (fallback_pdb_rid_is_user(rid)) {
770 uid_t uid;
771 struct passwd *pw = NULL;
773 DEBUG(5, ("assuming RID %u is a user\n", (unsigned)rid));
775 uid = fallback_pdb_user_rid_to_uid(rid);
776 pw = sys_getpwuid( uid );
778 DEBUG(5,("local_lookup_sid: looking up uid %u %s\n", (unsigned int)uid,
779 pw ? "succeeded" : "failed" ));
781 if ( !pw )
782 fstr_sprintf(name, "unix_user.%u", (unsigned int)uid);
783 else
784 fstrcpy( name, pw->pw_name );
786 DEBUG(5,("local_lookup_sid: found user %s for rid %u\n", name,
787 (unsigned int)rid ));
789 *psid_name_use = SID_NAME_USER;
791 return ( pw != NULL );
792 } else {
793 gid_t gid;
794 struct group *gr;
796 DEBUG(5, ("assuming RID %u is a group\n", (unsigned)rid));
798 gid = pdb_group_rid_to_gid(rid);
799 gr = getgrgid(gid);
801 *psid_name_use = SID_NAME_ALIAS;
803 DEBUG(5,("local_lookup_sid: looking up gid %u %s\n", (unsigned int)gid,
804 gr ? "succeeded" : "failed" ));
806 if( !gr )
807 fstr_sprintf(name, "unix_group.%u", (unsigned int)gid);
808 else
809 fstrcpy( name, gr->gr_name);
811 DEBUG(5,("local_lookup_sid: found group %s for rid %u\n", name,
812 (unsigned int)rid ));
814 /* assume fallback groups aer domain global groups */
816 *psid_name_use = SID_NAME_DOM_GRP;
818 return ( gr != NULL );
822 /*******************************************************************
823 Convert a name into a SID. Used in the lookup name rpc.
824 ********************************************************************/
826 BOOL local_lookup_name(const char *c_user, DOM_SID *psid, enum SID_NAME_USE *psid_name_use)
828 extern DOM_SID global_sid_World_Domain;
829 DOM_SID local_sid;
830 fstring user;
831 SAM_ACCOUNT *sam_account = NULL;
832 struct group *grp;
833 GROUP_MAP map;
835 *psid_name_use = SID_NAME_UNKNOWN;
838 * user may be quoted a const string, and map_username and
839 * friends can modify it. Make a modifiable copy. JRA.
842 fstrcpy(user, c_user);
844 sid_copy(&local_sid, get_global_sam_sid());
847 * Special case for MACHINE\Everyone. Map to the world_sid.
850 if(strequal(user, "Everyone")) {
851 sid_copy( psid, &global_sid_World_Domain);
852 sid_append_rid(psid, 0);
853 *psid_name_use = SID_NAME_ALIAS;
854 return True;
857 (void)map_username(user);
859 if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_account))) {
860 return False;
863 /* BEGIN ROOT BLOCK */
865 become_root();
866 if (pdb_getsampwnam(sam_account, user)) {
867 unbecome_root();
868 sid_copy(psid, pdb_get_user_sid(sam_account));
869 *psid_name_use = SID_NAME_USER;
871 pdb_free_sam(&sam_account);
872 return True;
875 pdb_free_sam(&sam_account);
878 * Maybe it was a group ?
881 /* check if it's a mapped group */
882 if (pdb_getgrnam(&map, user)) {
883 /* yes it's a mapped group */
884 sid_copy(&local_sid, &map.sid);
885 *psid_name_use = map.sid_name_use;
886 } else {
887 /* it's not a mapped group */
888 grp = getgrnam(user);
889 if(!grp) {
890 unbecome_root(); /* ---> exit form block */
891 return False;
895 *check if it's mapped, if it is reply it doesn't exist
897 * that's to prevent this case:
899 * unix group ug is mapped to nt group ng
900 * someone does a lookup on ug
901 * we must not reply as it doesn't "exist" anymore
902 * for NT. For NT only ng exists.
903 * JFM, 30/11/2001
906 if (pdb_getgrgid(&map, grp->gr_gid)){
907 unbecome_root(); /* ---> exit form block */
908 return False;
911 sid_append_rid( &local_sid, pdb_gid_to_group_rid(grp->gr_gid));
912 *psid_name_use = SID_NAME_ALIAS;
914 unbecome_root();
915 /* END ROOT BLOCK */
917 sid_copy( psid, &local_sid);
919 return True;
922 /*************************************************************
923 Change a password entry in the local smbpasswd file.
924 *************************************************************/
926 BOOL local_password_change(const char *user_name, int local_flags,
927 const char *new_passwd,
928 char *err_str, size_t err_str_len,
929 char *msg_str, size_t msg_str_len)
931 SAM_ACCOUNT *sam_pass=NULL;
932 uint16 other_acb;
934 *err_str = '\0';
935 *msg_str = '\0';
937 /* Get the smb passwd entry for this user */
938 pdb_init_sam(&sam_pass);
940 become_root();
941 if(!pdb_getsampwnam(sam_pass, user_name)) {
942 unbecome_root();
943 pdb_free_sam(&sam_pass);
945 if ((local_flags & LOCAL_ADD_USER) || (local_flags & LOCAL_DELETE_USER)) {
946 /* Might not exist in /etc/passwd. Use rid algorithm here */
947 if (!NT_STATUS_IS_OK(pdb_init_sam_new(&sam_pass, user_name, 0))) {
948 slprintf(err_str, err_str_len-1, "Failed to initialise SAM_ACCOUNT for user %s.\n", user_name);
949 return False;
951 } else {
952 slprintf(err_str, err_str_len-1,"Failed to find entry for user %s.\n", user_name);
953 return False;
955 } else {
956 unbecome_root();
957 /* the entry already existed */
958 local_flags &= ~LOCAL_ADD_USER;
961 /* the 'other' acb bits not being changed here */
962 other_acb = (pdb_get_acct_ctrl(sam_pass) & (!(ACB_WSTRUST|ACB_DOMTRUST|ACB_SVRTRUST|ACB_NORMAL)));
963 if (local_flags & LOCAL_TRUST_ACCOUNT) {
964 if (!pdb_set_acct_ctrl(sam_pass, ACB_WSTRUST | other_acb, PDB_CHANGED) ) {
965 slprintf(err_str, err_str_len - 1, "Failed to set 'trusted workstation account' flags for user %s.\n", user_name);
966 pdb_free_sam(&sam_pass);
967 return False;
969 } else if (local_flags & LOCAL_INTERDOM_ACCOUNT) {
970 if (!pdb_set_acct_ctrl(sam_pass, ACB_DOMTRUST | other_acb, PDB_CHANGED)) {
971 slprintf(err_str, err_str_len - 1, "Failed to set 'domain trust account' flags for user %s.\n", user_name);
972 pdb_free_sam(&sam_pass);
973 return False;
975 } else {
976 if (!pdb_set_acct_ctrl(sam_pass, ACB_NORMAL | other_acb, PDB_CHANGED)) {
977 slprintf(err_str, err_str_len - 1, "Failed to set 'normal account' flags for user %s.\n", user_name);
978 pdb_free_sam(&sam_pass);
979 return False;
984 * We are root - just write the new password
985 * and the valid last change time.
988 if (local_flags & LOCAL_DISABLE_USER) {
989 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_DISABLED, PDB_CHANGED)) {
990 slprintf(err_str, err_str_len-1, "Failed to set 'disabled' flag for user %s.\n", user_name);
991 pdb_free_sam(&sam_pass);
992 return False;
994 } else if (local_flags & LOCAL_ENABLE_USER) {
995 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED), PDB_CHANGED)) {
996 slprintf(err_str, err_str_len-1, "Failed to unset 'disabled' flag for user %s.\n", user_name);
997 pdb_free_sam(&sam_pass);
998 return False;
1002 if (local_flags & LOCAL_SET_NO_PASSWORD) {
1003 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_PWNOTREQ, PDB_CHANGED)) {
1004 slprintf(err_str, err_str_len-1, "Failed to set 'no password required' flag for user %s.\n", user_name);
1005 pdb_free_sam(&sam_pass);
1006 return False;
1008 } else if (local_flags & LOCAL_SET_PASSWORD) {
1010 * If we're dealing with setting a completely empty user account
1011 * ie. One with a password of 'XXXX', but not set disabled (like
1012 * an account created from scratch) then if the old password was
1013 * 'XX's then getsmbpwent will have set the ACB_DISABLED flag.
1014 * We remove that as we're giving this user their first password
1015 * and the decision hasn't really been made to disable them (ie.
1016 * don't create them disabled). JRA.
1018 if ((pdb_get_lanman_passwd(sam_pass)==NULL) && (pdb_get_acct_ctrl(sam_pass)&ACB_DISABLED)) {
1019 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED), PDB_CHANGED)) {
1020 slprintf(err_str, err_str_len-1, "Failed to unset 'disabled' flag for user %s.\n", user_name);
1021 pdb_free_sam(&sam_pass);
1022 return False;
1025 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_PWNOTREQ), PDB_CHANGED)) {
1026 slprintf(err_str, err_str_len-1, "Failed to unset 'no password required' flag for user %s.\n", user_name);
1027 pdb_free_sam(&sam_pass);
1028 return False;
1031 if (!pdb_set_plaintext_passwd (sam_pass, new_passwd)) {
1032 slprintf(err_str, err_str_len-1, "Failed to set password for user %s.\n", user_name);
1033 pdb_free_sam(&sam_pass);
1034 return False;
1038 if (local_flags & LOCAL_ADD_USER) {
1039 if (pdb_add_sam_account(sam_pass)) {
1040 slprintf(msg_str, msg_str_len-1, "Added user %s.\n", user_name);
1041 pdb_free_sam(&sam_pass);
1042 return True;
1043 } else {
1044 slprintf(err_str, err_str_len-1, "Failed to add entry for user %s.\n", user_name);
1045 pdb_free_sam(&sam_pass);
1046 return False;
1048 } else if (local_flags & LOCAL_DELETE_USER) {
1049 if (!pdb_delete_sam_account(sam_pass)) {
1050 slprintf(err_str,err_str_len-1, "Failed to delete entry for user %s.\n", user_name);
1051 pdb_free_sam(&sam_pass);
1052 return False;
1054 slprintf(msg_str, msg_str_len-1, "Deleted user %s.\n", user_name);
1055 } else {
1056 if(!pdb_update_sam_account(sam_pass)) {
1057 slprintf(err_str, err_str_len-1, "Failed to modify entry for user %s.\n", user_name);
1058 pdb_free_sam(&sam_pass);
1059 return False;
1061 if(local_flags & LOCAL_DISABLE_USER)
1062 slprintf(msg_str, msg_str_len-1, "Disabled user %s.\n", user_name);
1063 else if (local_flags & LOCAL_ENABLE_USER)
1064 slprintf(msg_str, msg_str_len-1, "Enabled user %s.\n", user_name);
1065 else if (local_flags & LOCAL_SET_NO_PASSWORD)
1066 slprintf(msg_str, msg_str_len-1, "User %s password set to none.\n", user_name);
1069 pdb_free_sam(&sam_pass);
1070 return True;
1073 /****************************************************************************
1074 Convert a uid to SID - algorithmic.
1075 ****************************************************************************/
1077 DOM_SID *algorithmic_uid_to_sid(DOM_SID *psid, uid_t uid)
1079 if ( !lp_enable_rid_algorithm() )
1080 return NULL;
1082 DEBUG(8,("algorithmic_uid_to_sid: falling back to RID algorithm\n"));
1083 sid_copy( psid, get_global_sam_sid() );
1084 sid_append_rid( psid, fallback_pdb_uid_to_user_rid(uid) );
1085 DEBUG(10,("algorithmic_uid_to_sid: uid (%d) -> SID %s.\n",
1086 (unsigned int)uid, sid_string_static(psid) ));
1088 return psid;
1091 /****************************************************************************
1092 Convert a uid to SID - locally.
1093 ****************************************************************************/
1095 DOM_SID *local_uid_to_sid(DOM_SID *psid, uid_t uid)
1097 SAM_ACCOUNT *sampw = NULL;
1098 struct passwd *unix_pw;
1099 BOOL ret;
1101 unix_pw = sys_getpwuid( uid );
1103 if ( !unix_pw ) {
1104 DEBUG(4,("local_uid_to_sid: host has no idea of uid %lu\n", (unsigned long)uid));
1105 return algorithmic_uid_to_sid( psid, uid);
1108 if ( !NT_STATUS_IS_OK(pdb_init_sam(&sampw)) ) {
1109 DEBUG(0,("local_uid_to_sid: failed to allocate SAM_ACCOUNT object\n"));
1110 return NULL;
1113 become_root();
1114 ret = pdb_getsampwnam( sampw, unix_pw->pw_name );
1115 unbecome_root();
1117 if ( ret )
1118 sid_copy( psid, pdb_get_user_sid(sampw) );
1119 else {
1120 DEBUG(4,("local_uid_to_sid: User %s [uid == %lu] has no samba account\n",
1121 unix_pw->pw_name, (unsigned long)uid));
1123 return algorithmic_uid_to_sid( psid, uid);
1126 DEBUG(10,("local_uid_to_sid: uid (%d) -> SID %s (%s).\n",
1127 (unsigned int)uid, sid_string_static(psid), unix_pw->pw_name));
1129 return psid;
1132 /****************************************************************************
1133 Convert a SID to uid - locally.
1134 ****************************************************************************/
1136 BOOL local_sid_to_uid(uid_t *puid, const DOM_SID *psid, enum SID_NAME_USE *name_type)
1138 SAM_ACCOUNT *sampw = NULL;
1139 struct passwd *unix_pw;
1140 const char *user_name;
1142 *name_type = SID_NAME_UNKNOWN;
1145 * We can only convert to a uid if this is our local
1146 * Domain SID (ie. we are the controling authority).
1148 if (!sid_check_is_in_our_domain(psid) ) {
1149 DEBUG(5,("local_sid_to_uid: this SID (%s) is not from our domain\n", sid_string_static(psid)));
1150 return False;
1153 /* lookup the user account */
1155 if ( !NT_STATUS_IS_OK(pdb_init_sam(&sampw)) ) {
1156 DEBUG(0,("local_sid_to_uid: Failed to allocate memory for SAM_ACCOUNT object\n"));
1157 return False;
1160 become_root();
1161 if ( !pdb_getsampwsid(sampw, psid) ) {
1162 unbecome_root();
1163 DEBUG(8,("local_sid_to_uid: Could not find SID %s in passdb\n",
1164 sid_string_static(psid)));
1165 return False;
1167 unbecome_root();
1169 user_name = pdb_get_username(sampw);
1171 unix_pw = sys_getpwnam( user_name );
1173 if ( !unix_pw ) {
1174 DEBUG(0,("local_sid_to_uid: %s found in passdb but getpwnam() return NULL!\n",
1175 user_name));
1176 pdb_free_sam( &sampw );
1177 return False;
1180 *puid = unix_pw->pw_uid;
1182 DEBUG(10,("local_sid_to_uid: SID %s -> uid (%u) (%s).\n", sid_string_static(psid),
1183 (unsigned int)*puid, user_name ));
1185 *name_type = SID_NAME_USER;
1187 return True;
1190 /****************************************************************************
1191 Convert a gid to SID - locally.
1192 ****************************************************************************/
1194 DOM_SID *local_gid_to_sid(DOM_SID *psid, gid_t gid)
1196 GROUP_MAP group;
1197 BOOL ret;
1199 /* we don't need to disable winbindd since the gid is stored in
1200 the GROUP_MAP object */
1202 /* done as root since ldap backend requires root to open a connection */
1204 become_root();
1205 ret = pdb_getgrgid( &group, gid );
1206 unbecome_root();
1208 if ( !ret ) {
1210 /* fallback to rid mapping if enabled */
1212 if ( lp_enable_rid_algorithm() ) {
1213 sid_copy(psid, get_global_sam_sid());
1214 sid_append_rid(psid, pdb_gid_to_group_rid(gid));
1216 DEBUG(10,("local_gid_to_sid: Fall back to algorithmic mapping: %u -> %s\n",
1217 (unsigned int)gid, sid_string_static(psid)));
1219 return psid;
1221 else
1222 return NULL;
1225 sid_copy( psid, &group.sid );
1227 DEBUG(10,("local_gid_to_sid: gid (%d) -> SID %s.\n",
1228 (unsigned int)gid, sid_string_static(psid)));
1230 return psid;
1233 /****************************************************************************
1234 Convert a SID to gid - locally.
1235 ****************************************************************************/
1237 BOOL local_sid_to_gid(gid_t *pgid, const DOM_SID *psid, enum SID_NAME_USE *name_type)
1239 uint32 rid;
1240 GROUP_MAP group;
1241 BOOL ret;
1243 *name_type = SID_NAME_UNKNOWN;
1245 /* This call can enumerate group mappings for foreign sids as well.
1246 So don't check for a match against our domain SID */
1248 /* we don't need to disable winbindd since the gid is stored in
1249 the GROUP_MAP object */
1251 become_root();
1252 ret = pdb_getgrsid(&group, *psid);
1253 unbecome_root();
1255 if ( !ret ) {
1257 /* fallback to rid mapping if enabled */
1259 if ( lp_enable_rid_algorithm() ) {
1261 if (!sid_check_is_in_our_domain(psid) ) {
1262 DEBUG(5,("local_sid_to_gid: RID algorithm only supported for our domain (%s is not)\n", sid_string_static(psid)));
1263 return False;
1266 if (!sid_peek_rid(psid, &rid)) {
1267 DEBUG(10,("local_sid_to_uid: invalid SID!\n"));
1268 return False;
1271 DEBUG(10,("local_sid_to_gid: Fall back to algorithmic mapping\n"));
1273 if (fallback_pdb_rid_is_user(rid)) {
1274 DEBUG(3, ("local_sid_to_gid: SID %s is *NOT* a group\n", sid_string_static(psid)));
1275 return False;
1276 } else {
1277 *pgid = pdb_group_rid_to_gid(rid);
1278 DEBUG(10,("local_sid_to_gid: mapping: %s -> %u\n", sid_string_static(psid), (unsigned int)(*pgid)));
1279 return True;
1283 return False;
1286 *pgid = group.gid;
1288 DEBUG(10,("local_sid_to_gid: SID %s -> gid (%u)\n", sid_string_static(psid),
1289 (unsigned int)*pgid));
1291 return True;
1294 /**********************************************************************
1295 Marshall/unmarshall SAM_ACCOUNT structs.
1296 *********************************************************************/
1298 #define TDB_FORMAT_STRING_V0 "ddddddBBBBBBBBBBBBddBBwdwdBwwd"
1299 #define TDB_FORMAT_STRING_V1 "dddddddBBBBBBBBBBBBddBBwdwdBwwd"
1301 /**********************************************************************
1302 Intialize a SAM_ACCOUNT struct from a BYTE buffer of size len
1303 *********************************************************************/
1305 BOOL init_sam_from_buffer(SAM_ACCOUNT *sampass, uint8 *buf, uint32 buflen)
1307 return(init_sam_from_buffer_v1(sampass, buf, buflen));
1310 /**********************************************************************
1311 Intialize a BYTE buffer from a SAM_ACCOUNT struct
1312 *********************************************************************/
1314 uint32 init_buffer_from_sam (uint8 **buf, const SAM_ACCOUNT *sampass, BOOL size_only)
1316 return(init_buffer_from_sam_v1(buf, sampass, size_only));
1320 BOOL init_sam_from_buffer_v0(SAM_ACCOUNT *sampass, uint8 *buf, uint32 buflen)
1323 /* times are stored as 32bit integer
1324 take care on system with 64bit wide time_t
1325 --SSS */
1326 uint32 logon_time,
1327 logoff_time,
1328 kickoff_time,
1329 pass_last_set_time,
1330 pass_can_change_time,
1331 pass_must_change_time;
1332 char *username;
1333 char *domain;
1334 char *nt_username;
1335 char *dir_drive;
1336 char *unknown_str;
1337 char *munged_dial;
1338 char *fullname;
1339 char *homedir;
1340 char *logon_script;
1341 char *profile_path;
1342 char *acct_desc;
1343 char *workstations;
1344 uint32 username_len, domain_len, nt_username_len,
1345 dir_drive_len, unknown_str_len, munged_dial_len,
1346 fullname_len, homedir_len, logon_script_len,
1347 profile_path_len, acct_desc_len, workstations_len;
1349 uint32 user_rid, group_rid, unknown_3, hours_len, unknown_6;
1350 uint16 acct_ctrl, logon_divs;
1351 uint16 bad_password_count, logon_count;
1352 uint8 *hours;
1353 static uint8 *lm_pw_ptr, *nt_pw_ptr;
1354 uint32 len = 0;
1355 uint32 lm_pw_len, nt_pw_len, hourslen;
1356 BOOL ret = True;
1358 if(sampass == NULL || buf == NULL) {
1359 DEBUG(0, ("init_sam_from_buffer: NULL parameters found!\n"));
1360 return False;
1363 /* unpack the buffer into variables */
1364 len = tdb_unpack ((char *)buf, buflen, TDB_FORMAT_STRING_V0,
1365 &logon_time,
1366 &logoff_time,
1367 &kickoff_time,
1368 &pass_last_set_time,
1369 &pass_can_change_time,
1370 &pass_must_change_time,
1371 &username_len, &username,
1372 &domain_len, &domain,
1373 &nt_username_len, &nt_username,
1374 &fullname_len, &fullname,
1375 &homedir_len, &homedir,
1376 &dir_drive_len, &dir_drive,
1377 &logon_script_len, &logon_script,
1378 &profile_path_len, &profile_path,
1379 &acct_desc_len, &acct_desc,
1380 &workstations_len, &workstations,
1381 &unknown_str_len, &unknown_str,
1382 &munged_dial_len, &munged_dial,
1383 &user_rid,
1384 &group_rid,
1385 &lm_pw_len, &lm_pw_ptr,
1386 &nt_pw_len, &nt_pw_ptr,
1387 &acct_ctrl,
1388 &unknown_3,
1389 &logon_divs,
1390 &hours_len,
1391 &hourslen, &hours,
1392 &bad_password_count,
1393 &logon_count,
1394 &unknown_6);
1396 if (len == -1) {
1397 ret = False;
1398 goto done;
1401 pdb_set_logon_time(sampass, logon_time, PDB_SET);
1402 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
1403 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
1404 pdb_set_pass_can_change_time(sampass, pass_can_change_time, PDB_SET);
1405 pdb_set_pass_must_change_time(sampass, pass_must_change_time, PDB_SET);
1406 pdb_set_pass_last_set_time(sampass, pass_last_set_time, PDB_SET);
1408 pdb_set_username(sampass, username, PDB_SET);
1409 pdb_set_domain(sampass, domain, PDB_SET);
1410 pdb_set_nt_username(sampass, nt_username, PDB_SET);
1411 pdb_set_fullname(sampass, fullname, PDB_SET);
1413 if (homedir) {
1414 pdb_set_homedir(sampass, homedir, PDB_SET);
1416 else {
1417 pdb_set_homedir(sampass,
1418 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_home()),
1419 PDB_DEFAULT);
1422 if (dir_drive)
1423 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
1424 else {
1425 pdb_set_dir_drive(sampass,
1426 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_drive()),
1427 PDB_DEFAULT);
1430 if (logon_script)
1431 pdb_set_logon_script(sampass, logon_script, PDB_SET);
1432 else {
1433 pdb_set_logon_script(sampass,
1434 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_script()),
1435 PDB_DEFAULT);
1438 if (profile_path) {
1439 pdb_set_profile_path(sampass, profile_path, PDB_SET);
1440 } else {
1441 pdb_set_profile_path(sampass,
1442 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_path()),
1443 PDB_DEFAULT);
1446 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
1447 pdb_set_workstations(sampass, workstations, PDB_SET);
1448 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
1450 if (lm_pw_ptr && lm_pw_len == LM_HASH_LEN) {
1451 if (!pdb_set_lanman_passwd(sampass, lm_pw_ptr, PDB_SET)) {
1452 ret = False;
1453 goto done;
1457 if (nt_pw_ptr && nt_pw_len == NT_HASH_LEN) {
1458 if (!pdb_set_nt_passwd(sampass, nt_pw_ptr, PDB_SET)) {
1459 ret = False;
1460 goto done;
1464 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
1465 pdb_set_group_sid_from_rid(sampass, group_rid, PDB_SET);
1466 pdb_set_unknown_3(sampass, unknown_3, PDB_SET);
1467 pdb_set_hours_len(sampass, hours_len, PDB_SET);
1468 pdb_set_bad_password_count(sampass, bad_password_count, PDB_SET);
1469 pdb_set_logon_count(sampass, logon_count, PDB_SET);
1470 pdb_set_unknown_6(sampass, unknown_6, PDB_SET);
1471 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
1472 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
1473 pdb_set_hours(sampass, hours, PDB_SET);
1475 done:
1477 SAFE_FREE(username);
1478 SAFE_FREE(domain);
1479 SAFE_FREE(nt_username);
1480 SAFE_FREE(fullname);
1481 SAFE_FREE(homedir);
1482 SAFE_FREE(dir_drive);
1483 SAFE_FREE(logon_script);
1484 SAFE_FREE(profile_path);
1485 SAFE_FREE(acct_desc);
1486 SAFE_FREE(workstations);
1487 SAFE_FREE(munged_dial);
1488 SAFE_FREE(unknown_str);
1489 SAFE_FREE(hours);
1491 return ret;
1495 uint32 init_buffer_from_sam_v0 (uint8 **buf, const SAM_ACCOUNT *sampass, BOOL size_only)
1497 size_t len, buflen;
1499 /* times are stored as 32bit integer
1500 take care on system with 64bit wide time_t
1501 --SSS */
1502 uint32 logon_time,
1503 logoff_time,
1504 kickoff_time,
1505 pass_last_set_time,
1506 pass_can_change_time,
1507 pass_must_change_time;
1509 uint32 user_rid, group_rid;
1511 const char *username;
1512 const char *domain;
1513 const char *nt_username;
1514 const char *dir_drive;
1515 const char *unknown_str;
1516 const char *munged_dial;
1517 const char *fullname;
1518 const char *homedir;
1519 const char *logon_script;
1520 const char *profile_path;
1521 const char *acct_desc;
1522 const char *workstations;
1523 uint32 username_len, domain_len, nt_username_len,
1524 dir_drive_len, unknown_str_len, munged_dial_len,
1525 fullname_len, homedir_len, logon_script_len,
1526 profile_path_len, acct_desc_len, workstations_len;
1528 const uint8 *lm_pw;
1529 const uint8 *nt_pw;
1530 uint32 lm_pw_len = 16;
1531 uint32 nt_pw_len = 16;
1533 /* do we have a valid SAM_ACCOUNT pointer? */
1534 if (sampass == NULL) {
1535 DEBUG(0, ("init_buffer_from_sam: SAM_ACCOUNT is NULL!\n"));
1536 return -1;
1539 *buf = NULL;
1540 buflen = 0;
1542 logon_time = (uint32)pdb_get_logon_time(sampass);
1543 logoff_time = (uint32)pdb_get_logoff_time(sampass);
1544 kickoff_time = (uint32)pdb_get_kickoff_time(sampass);
1545 pass_can_change_time = (uint32)pdb_get_pass_can_change_time(sampass);
1546 pass_must_change_time = (uint32)pdb_get_pass_must_change_time(sampass);
1547 pass_last_set_time = (uint32)pdb_get_pass_last_set_time(sampass);
1549 user_rid = pdb_get_user_rid(sampass);
1550 group_rid = pdb_get_group_rid(sampass);
1552 username = pdb_get_username(sampass);
1553 if (username)
1554 username_len = strlen(username) +1;
1555 else
1556 username_len = 0;
1558 domain = pdb_get_domain(sampass);
1559 if (domain)
1560 domain_len = strlen(domain) +1;
1561 else
1562 domain_len = 0;
1564 nt_username = pdb_get_nt_username(sampass);
1565 if (nt_username)
1566 nt_username_len = strlen(nt_username) +1;
1567 else
1568 nt_username_len = 0;
1570 fullname = pdb_get_fullname(sampass);
1571 if (fullname)
1572 fullname_len = strlen(fullname) +1;
1573 else
1574 fullname_len = 0;
1577 * Only updates fields which have been set (not defaults from smb.conf)
1580 if (!IS_SAM_DEFAULT(sampass, PDB_DRIVE))
1581 dir_drive = pdb_get_dir_drive(sampass);
1582 else
1583 dir_drive = NULL;
1584 if (dir_drive)
1585 dir_drive_len = strlen(dir_drive) +1;
1586 else
1587 dir_drive_len = 0;
1589 if (!IS_SAM_DEFAULT(sampass, PDB_SMBHOME))
1590 homedir = pdb_get_homedir(sampass);
1591 else
1592 homedir = NULL;
1593 if (homedir)
1594 homedir_len = strlen(homedir) +1;
1595 else
1596 homedir_len = 0;
1598 if (!IS_SAM_DEFAULT(sampass, PDB_LOGONSCRIPT))
1599 logon_script = pdb_get_logon_script(sampass);
1600 else
1601 logon_script = NULL;
1602 if (logon_script)
1603 logon_script_len = strlen(logon_script) +1;
1604 else
1605 logon_script_len = 0;
1607 if (!IS_SAM_DEFAULT(sampass, PDB_PROFILE))
1608 profile_path = pdb_get_profile_path(sampass);
1609 else
1610 profile_path = NULL;
1611 if (profile_path)
1612 profile_path_len = strlen(profile_path) +1;
1613 else
1614 profile_path_len = 0;
1616 lm_pw = pdb_get_lanman_passwd(sampass);
1617 if (!lm_pw)
1618 lm_pw_len = 0;
1620 nt_pw = pdb_get_nt_passwd(sampass);
1621 if (!nt_pw)
1622 nt_pw_len = 0;
1624 acct_desc = pdb_get_acct_desc(sampass);
1625 if (acct_desc)
1626 acct_desc_len = strlen(acct_desc) +1;
1627 else
1628 acct_desc_len = 0;
1630 workstations = pdb_get_workstations(sampass);
1631 if (workstations)
1632 workstations_len = strlen(workstations) +1;
1633 else
1634 workstations_len = 0;
1636 unknown_str = NULL;
1637 unknown_str_len = 0;
1639 munged_dial = pdb_get_munged_dial(sampass);
1640 if (munged_dial)
1641 munged_dial_len = strlen(munged_dial) +1;
1642 else
1643 munged_dial_len = 0;
1645 /* one time to get the size needed */
1646 len = tdb_pack(NULL, 0, TDB_FORMAT_STRING_V0,
1647 logon_time,
1648 logoff_time,
1649 kickoff_time,
1650 pass_last_set_time,
1651 pass_can_change_time,
1652 pass_must_change_time,
1653 username_len, username,
1654 domain_len, domain,
1655 nt_username_len, nt_username,
1656 fullname_len, fullname,
1657 homedir_len, homedir,
1658 dir_drive_len, dir_drive,
1659 logon_script_len, logon_script,
1660 profile_path_len, profile_path,
1661 acct_desc_len, acct_desc,
1662 workstations_len, workstations,
1663 unknown_str_len, unknown_str,
1664 munged_dial_len, munged_dial,
1665 user_rid,
1666 group_rid,
1667 lm_pw_len, lm_pw,
1668 nt_pw_len, nt_pw,
1669 pdb_get_acct_ctrl(sampass),
1670 pdb_get_unknown_3(sampass),
1671 pdb_get_logon_divs(sampass),
1672 pdb_get_hours_len(sampass),
1673 MAX_HOURS_LEN, pdb_get_hours(sampass),
1674 pdb_get_bad_password_count(sampass),
1675 pdb_get_logon_count(sampass),
1676 pdb_get_unknown_6(sampass));
1679 if (size_only)
1680 return buflen;
1682 /* malloc the space needed */
1683 if ( (*buf=(uint8*)malloc(len)) == NULL) {
1684 DEBUG(0,("init_buffer_from_sam: Unable to malloc() memory for buffer!\n"));
1685 return (-1);
1688 /* now for the real call to tdb_pack() */
1689 buflen = tdb_pack((char *)*buf, len, TDB_FORMAT_STRING_V0,
1690 logon_time,
1691 logoff_time,
1692 kickoff_time,
1693 pass_last_set_time,
1694 pass_can_change_time,
1695 pass_must_change_time,
1696 username_len, username,
1697 domain_len, domain,
1698 nt_username_len, nt_username,
1699 fullname_len, fullname,
1700 homedir_len, homedir,
1701 dir_drive_len, dir_drive,
1702 logon_script_len, logon_script,
1703 profile_path_len, profile_path,
1704 acct_desc_len, acct_desc,
1705 workstations_len, workstations,
1706 unknown_str_len, unknown_str,
1707 munged_dial_len, munged_dial,
1708 user_rid,
1709 group_rid,
1710 lm_pw_len, lm_pw,
1711 nt_pw_len, nt_pw,
1712 pdb_get_acct_ctrl(sampass),
1713 pdb_get_unknown_3(sampass),
1714 pdb_get_logon_divs(sampass),
1715 pdb_get_hours_len(sampass),
1716 MAX_HOURS_LEN, pdb_get_hours(sampass),
1717 pdb_get_bad_password_count(sampass),
1718 pdb_get_logon_count(sampass),
1719 pdb_get_unknown_6(sampass));
1722 /* check to make sure we got it correct */
1723 if (buflen != len) {
1724 DEBUG(0, ("init_buffer_from_sam: somthing odd is going on here: bufflen (%lu) != len (%lu) in tdb_pack operations!\n",
1725 (unsigned long)buflen, (unsigned long)len));
1726 /* error */
1727 SAFE_FREE (*buf);
1728 return (-1);
1731 return (buflen);
1735 BOOL init_sam_from_buffer_v1(SAM_ACCOUNT *sampass, uint8 *buf, uint32 buflen)
1738 /* times are stored as 32bit integer
1739 take care on system with 64bit wide time_t
1740 --SSS */
1741 uint32 logon_time,
1742 logoff_time,
1743 kickoff_time,
1744 lockout_time,
1745 pass_last_set_time,
1746 pass_can_change_time,
1747 pass_must_change_time;
1748 char *username;
1749 char *domain;
1750 char *nt_username;
1751 char *dir_drive;
1752 char *unknown_str;
1753 char *munged_dial;
1754 char *fullname;
1755 char *homedir;
1756 char *logon_script;
1757 char *profile_path;
1758 char *acct_desc;
1759 char *workstations;
1760 uint32 username_len, domain_len, nt_username_len,
1761 dir_drive_len, unknown_str_len, munged_dial_len,
1762 fullname_len, homedir_len, logon_script_len,
1763 profile_path_len, acct_desc_len, workstations_len;
1765 uint32 user_rid, group_rid, unknown_3, hours_len, unknown_6;
1766 uint16 acct_ctrl, logon_divs;
1767 uint16 bad_password_count, logon_count;
1768 uint8 *hours;
1769 static uint8 *lm_pw_ptr, *nt_pw_ptr;
1770 uint32 len = 0;
1771 uint32 lm_pw_len, nt_pw_len, hourslen;
1772 BOOL ret = True;
1774 if(sampass == NULL || buf == NULL) {
1775 DEBUG(0, ("init_sam_from_buffer: NULL parameters found!\n"));
1776 return False;
1779 /* unpack the buffer into variables */
1780 len = tdb_unpack ((char *)buf, buflen, TDB_FORMAT_STRING_V1,
1781 &logon_time,
1782 &logoff_time,
1783 &kickoff_time,
1784 &lockout_time,
1785 &pass_last_set_time,
1786 &pass_can_change_time,
1787 &pass_must_change_time,
1788 &username_len, &username,
1789 &domain_len, &domain,
1790 &nt_username_len, &nt_username,
1791 &fullname_len, &fullname,
1792 &homedir_len, &homedir,
1793 &dir_drive_len, &dir_drive,
1794 &logon_script_len, &logon_script,
1795 &profile_path_len, &profile_path,
1796 &acct_desc_len, &acct_desc,
1797 &workstations_len, &workstations,
1798 &unknown_str_len, &unknown_str,
1799 &munged_dial_len, &munged_dial,
1800 &user_rid,
1801 &group_rid,
1802 &lm_pw_len, &lm_pw_ptr,
1803 &nt_pw_len, &nt_pw_ptr,
1804 &acct_ctrl,
1805 &unknown_3,
1806 &logon_divs,
1807 &hours_len,
1808 &hourslen, &hours,
1809 &bad_password_count,
1810 &logon_count,
1811 &unknown_6);
1813 if (len == -1) {
1814 ret = False;
1815 goto done;
1818 pdb_set_logon_time(sampass, logon_time, PDB_SET);
1819 pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
1820 pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
1821 pdb_set_pass_can_change_time(sampass, pass_can_change_time, PDB_SET);
1822 pdb_set_pass_must_change_time(sampass, pass_must_change_time, PDB_SET);
1823 pdb_set_pass_last_set_time(sampass, pass_last_set_time, PDB_SET);
1825 pdb_set_username(sampass, username, PDB_SET);
1826 pdb_set_domain(sampass, domain, PDB_SET);
1827 pdb_set_nt_username(sampass, nt_username, PDB_SET);
1828 pdb_set_fullname(sampass, fullname, PDB_SET);
1830 if (homedir) {
1831 pdb_set_homedir(sampass, homedir, PDB_SET);
1833 else {
1834 pdb_set_homedir(sampass,
1835 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_home()),
1836 PDB_DEFAULT);
1839 if (dir_drive)
1840 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
1841 else {
1842 pdb_set_dir_drive(sampass,
1843 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_drive()),
1844 PDB_DEFAULT);
1847 if (logon_script)
1848 pdb_set_logon_script(sampass, logon_script, PDB_SET);
1849 else {
1850 pdb_set_logon_script(sampass,
1851 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_script()),
1852 PDB_DEFAULT);
1855 if (profile_path) {
1856 pdb_set_profile_path(sampass, profile_path, PDB_SET);
1857 } else {
1858 pdb_set_profile_path(sampass,
1859 talloc_sub_basic(sampass->mem_ctx, username, lp_logon_path()),
1860 PDB_DEFAULT);
1863 pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
1864 pdb_set_workstations(sampass, workstations, PDB_SET);
1865 pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
1867 if (lm_pw_ptr && lm_pw_len == LM_HASH_LEN) {
1868 if (!pdb_set_lanman_passwd(sampass, lm_pw_ptr, PDB_SET)) {
1869 ret = False;
1870 goto done;
1874 if (nt_pw_ptr && nt_pw_len == NT_HASH_LEN) {
1875 if (!pdb_set_nt_passwd(sampass, nt_pw_ptr, PDB_SET)) {
1876 ret = False;
1877 goto done;
1881 pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
1882 pdb_set_group_sid_from_rid(sampass, group_rid, PDB_SET);
1883 pdb_set_unknown_3(sampass, unknown_3, PDB_SET);
1884 pdb_set_hours_len(sampass, hours_len, PDB_SET);
1885 pdb_set_bad_password_count(sampass, bad_password_count, PDB_SET);
1886 pdb_set_logon_count(sampass, logon_count, PDB_SET);
1887 pdb_set_unknown_6(sampass, unknown_6, PDB_SET);
1888 pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
1889 pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
1890 pdb_set_hours(sampass, hours, PDB_SET);
1892 done:
1894 SAFE_FREE(username);
1895 SAFE_FREE(domain);
1896 SAFE_FREE(nt_username);
1897 SAFE_FREE(fullname);
1898 SAFE_FREE(homedir);
1899 SAFE_FREE(dir_drive);
1900 SAFE_FREE(logon_script);
1901 SAFE_FREE(profile_path);
1902 SAFE_FREE(acct_desc);
1903 SAFE_FREE(workstations);
1904 SAFE_FREE(munged_dial);
1905 SAFE_FREE(unknown_str);
1906 SAFE_FREE(hours);
1908 return ret;
1912 uint32 init_buffer_from_sam_v1 (uint8 **buf, const SAM_ACCOUNT *sampass, BOOL size_only)
1914 size_t len, buflen;
1916 /* times are stored as 32bit integer
1917 take care on system with 64bit wide time_t
1918 --SSS */
1919 uint32 logon_time,
1920 logoff_time,
1921 kickoff_time,
1922 lockout_time,
1923 pass_last_set_time,
1924 pass_can_change_time,
1925 pass_must_change_time;
1927 uint32 user_rid, group_rid;
1929 const char *username;
1930 const char *domain;
1931 const char *nt_username;
1932 const char *dir_drive;
1933 const char *unknown_str;
1934 const char *munged_dial;
1935 const char *fullname;
1936 const char *homedir;
1937 const char *logon_script;
1938 const char *profile_path;
1939 const char *acct_desc;
1940 const char *workstations;
1941 uint32 username_len, domain_len, nt_username_len,
1942 dir_drive_len, unknown_str_len, munged_dial_len,
1943 fullname_len, homedir_len, logon_script_len,
1944 profile_path_len, acct_desc_len, workstations_len;
1946 const uint8 *lm_pw;
1947 const uint8 *nt_pw;
1948 uint32 lm_pw_len = 16;
1949 uint32 nt_pw_len = 16;
1951 /* do we have a valid SAM_ACCOUNT pointer? */
1952 if (sampass == NULL) {
1953 DEBUG(0, ("init_buffer_from_sam: SAM_ACCOUNT is NULL!\n"));
1954 return -1;
1957 *buf = NULL;
1958 buflen = 0;
1960 logon_time = (uint32)pdb_get_logon_time(sampass);
1961 logoff_time = (uint32)pdb_get_logoff_time(sampass);
1962 kickoff_time = (uint32)pdb_get_kickoff_time(sampass);
1963 lockout_time = (uint32)0;
1964 pass_can_change_time = (uint32)pdb_get_pass_can_change_time(sampass);
1965 pass_must_change_time = (uint32)pdb_get_pass_must_change_time(sampass);
1966 pass_last_set_time = (uint32)pdb_get_pass_last_set_time(sampass);
1968 user_rid = pdb_get_user_rid(sampass);
1969 group_rid = pdb_get_group_rid(sampass);
1971 username = pdb_get_username(sampass);
1972 if (username)
1973 username_len = strlen(username) +1;
1974 else
1975 username_len = 0;
1977 domain = pdb_get_domain(sampass);
1978 if (domain)
1979 domain_len = strlen(domain) +1;
1980 else
1981 domain_len = 0;
1983 nt_username = pdb_get_nt_username(sampass);
1984 if (nt_username)
1985 nt_username_len = strlen(nt_username) +1;
1986 else
1987 nt_username_len = 0;
1989 fullname = pdb_get_fullname(sampass);
1990 if (fullname)
1991 fullname_len = strlen(fullname) +1;
1992 else
1993 fullname_len = 0;
1996 * Only updates fields which have been set (not defaults from smb.conf)
1999 if (!IS_SAM_DEFAULT(sampass, PDB_DRIVE))
2000 dir_drive = pdb_get_dir_drive(sampass);
2001 else
2002 dir_drive = NULL;
2003 if (dir_drive)
2004 dir_drive_len = strlen(dir_drive) +1;
2005 else
2006 dir_drive_len = 0;
2008 if (!IS_SAM_DEFAULT(sampass, PDB_SMBHOME))
2009 homedir = pdb_get_homedir(sampass);
2010 else
2011 homedir = NULL;
2012 if (homedir)
2013 homedir_len = strlen(homedir) +1;
2014 else
2015 homedir_len = 0;
2017 if (!IS_SAM_DEFAULT(sampass, PDB_LOGONSCRIPT))
2018 logon_script = pdb_get_logon_script(sampass);
2019 else
2020 logon_script = NULL;
2021 if (logon_script)
2022 logon_script_len = strlen(logon_script) +1;
2023 else
2024 logon_script_len = 0;
2026 if (!IS_SAM_DEFAULT(sampass, PDB_PROFILE))
2027 profile_path = pdb_get_profile_path(sampass);
2028 else
2029 profile_path = NULL;
2030 if (profile_path)
2031 profile_path_len = strlen(profile_path) +1;
2032 else
2033 profile_path_len = 0;
2035 lm_pw = pdb_get_lanman_passwd(sampass);
2036 if (!lm_pw)
2037 lm_pw_len = 0;
2039 nt_pw = pdb_get_nt_passwd(sampass);
2040 if (!nt_pw)
2041 nt_pw_len = 0;
2043 acct_desc = pdb_get_acct_desc(sampass);
2044 if (acct_desc)
2045 acct_desc_len = strlen(acct_desc) +1;
2046 else
2047 acct_desc_len = 0;
2049 workstations = pdb_get_workstations(sampass);
2050 if (workstations)
2051 workstations_len = strlen(workstations) +1;
2052 else
2053 workstations_len = 0;
2055 unknown_str = NULL;
2056 unknown_str_len = 0;
2058 munged_dial = pdb_get_munged_dial(sampass);
2059 if (munged_dial)
2060 munged_dial_len = strlen(munged_dial) +1;
2061 else
2062 munged_dial_len = 0;
2064 /* one time to get the size needed */
2065 len = tdb_pack(NULL, 0, TDB_FORMAT_STRING_V1,
2066 logon_time,
2067 logoff_time,
2068 kickoff_time,
2069 lockout_time,
2070 pass_last_set_time,
2071 pass_can_change_time,
2072 pass_must_change_time,
2073 username_len, username,
2074 domain_len, domain,
2075 nt_username_len, nt_username,
2076 fullname_len, fullname,
2077 homedir_len, homedir,
2078 dir_drive_len, dir_drive,
2079 logon_script_len, logon_script,
2080 profile_path_len, profile_path,
2081 acct_desc_len, acct_desc,
2082 workstations_len, workstations,
2083 unknown_str_len, unknown_str,
2084 munged_dial_len, munged_dial,
2085 user_rid,
2086 group_rid,
2087 lm_pw_len, lm_pw,
2088 nt_pw_len, nt_pw,
2089 pdb_get_acct_ctrl(sampass),
2090 pdb_get_unknown_3(sampass),
2091 pdb_get_logon_divs(sampass),
2092 pdb_get_hours_len(sampass),
2093 MAX_HOURS_LEN, pdb_get_hours(sampass),
2094 pdb_get_bad_password_count(sampass),
2095 pdb_get_logon_count(sampass),
2096 pdb_get_unknown_6(sampass));
2099 if (size_only)
2100 return buflen;
2102 /* malloc the space needed */
2103 if ( (*buf=(uint8*)malloc(len)) == NULL) {
2104 DEBUG(0,("init_buffer_from_sam: Unable to malloc() memory for buffer!\n"));
2105 return (-1);
2108 /* now for the real call to tdb_pack() */
2109 buflen = tdb_pack((char *)*buf, len, TDB_FORMAT_STRING_V1,
2110 logon_time,
2111 logoff_time,
2112 kickoff_time,
2113 lockout_time,
2114 pass_last_set_time,
2115 pass_can_change_time,
2116 pass_must_change_time,
2117 username_len, username,
2118 domain_len, domain,
2119 nt_username_len, nt_username,
2120 fullname_len, fullname,
2121 homedir_len, homedir,
2122 dir_drive_len, dir_drive,
2123 logon_script_len, logon_script,
2124 profile_path_len, profile_path,
2125 acct_desc_len, acct_desc,
2126 workstations_len, workstations,
2127 unknown_str_len, unknown_str,
2128 munged_dial_len, munged_dial,
2129 user_rid,
2130 group_rid,
2131 lm_pw_len, lm_pw,
2132 nt_pw_len, nt_pw,
2133 pdb_get_acct_ctrl(sampass),
2134 pdb_get_unknown_3(sampass),
2135 pdb_get_logon_divs(sampass),
2136 pdb_get_hours_len(sampass),
2137 MAX_HOURS_LEN, pdb_get_hours(sampass),
2138 pdb_get_bad_password_count(sampass),
2139 pdb_get_logon_count(sampass),
2140 pdb_get_unknown_6(sampass));
2143 /* check to make sure we got it correct */
2144 if (buflen != len) {
2145 DEBUG(0, ("init_buffer_from_sam: somthing odd is going on here: bufflen (%lu) != len (%lu) in tdb_pack operations!\n",
2146 (unsigned long)buflen, (unsigned long)len));
2147 /* error */
2148 SAFE_FREE (*buf);
2149 return (-1);
2152 return (buflen);
2156 /**********************************************************************
2157 **********************************************************************/
2159 static BOOL get_free_ugid_range(uint32 *low, uint32 *high)
2161 uid_t u_low, u_high;
2162 gid_t g_low, g_high;
2164 if (!lp_idmap_uid(&u_low, &u_high) || !lp_idmap_gid(&g_low, &g_high)) {
2165 return False;
2168 *low = (u_low < g_low) ? u_low : g_low;
2169 *high = (u_high < g_high) ? u_high : g_high;
2171 return True;
2174 /******************************************************************
2175 Get the the non-algorithmic RID range if idmap range are defined
2176 ******************************************************************/
2178 BOOL get_free_rid_range(uint32 *low, uint32 *high)
2180 uint32 id_low, id_high;
2182 if (!lp_enable_rid_algorithm()) {
2183 *low = BASE_RID;
2184 *high = (uint32)-1;
2187 if (!get_free_ugid_range(&id_low, &id_high)) {
2188 return False;
2191 *low = fallback_pdb_uid_to_user_rid(id_low);
2192 if (fallback_pdb_user_rid_to_uid((uint32)-1) < id_high) {
2193 *high = (uint32)-1;
2194 } else {
2195 *high = fallback_pdb_uid_to_user_rid(id_high);
2198 return True;