2 * Unix SMB/CIFS implementation.
3 * SMB parameters and setup
4 * Copyright (C) Andrew Tridgell 1992-1998
5 * Copyright (C) Simo Sorce 2000
6 * Copyright (C) Gerald Carter 2000
7 * Copyright (C) Jeremy Allison 2001
8 * Copyright (C) Andrew Bartlett 2002
10 * This program is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * You should have received a copy of the GNU General Public License along with
21 * this program; if not, write to the Free Software Foundation, Inc., 675
22 * Mass Ave, Cambridge, MA 02139, USA.
29 #define PDB_VERSION "20010830"
30 #define PASSDB_FILE_NAME "passdb.tdb"
31 #define TDB_FORMAT_STRING "ddddddBBBBBBBBBBBBddBBwdwdBdd"
32 #define USERPREFIX "USER_"
33 #define RIDPREFIX "RID_"
35 struct tdbsam_privates {
36 TDB_CONTEXT *passwd_tdb;
39 /* retrive-once info */
40 const char *tdbsam_location;
42 BOOL permit_non_unix_accounts;
44 /* uint32 low_nua_rid;
45 uint32 high_nua_rid; */
48 /**********************************************************************
49 Intialize a SAM_ACCOUNT struct from a BYTE buffer of size len
50 *********************************************************************/
52 static BOOL init_sam_from_buffer (struct tdbsam_privates *tdb_state,
53 SAM_ACCOUNT *sampass, uint8 *buf, uint32 buflen)
56 /* times are stored as 32bit integer
57 take care on system with 64bit wide time_t
64 pass_must_change_time;
77 uint32 username_len, domain_len, nt_username_len,
78 dir_drive_len, unknown_str_len, munged_dial_len,
79 fullname_len, homedir_len, logon_script_len,
80 profile_path_len, acct_desc_len, workstations_len;
82 uint32 user_rid, group_rid, unknown_3, hours_len, unknown_5, unknown_6;
83 uint16 acct_ctrl, logon_divs;
85 static uint8 *lm_pw_ptr, *nt_pw_ptr;
87 uint32 lmpwlen, ntpwlen, hourslen;
93 gid_t gid = -1; /* This is what standard sub advanced expects if no gid is known */
95 if(sampass == NULL || buf == NULL) {
96 DEBUG(0, ("init_sam_from_buffer: NULL parameters found!\n"));
100 /* unpack the buffer into variables */
101 len = tdb_unpack (buf, buflen, TDB_FORMAT_STRING,
106 &pass_can_change_time,
107 &pass_must_change_time,
108 &username_len, &username,
109 &domain_len, &domain,
110 &nt_username_len, &nt_username,
111 &fullname_len, &fullname,
112 &homedir_len, &homedir,
113 &dir_drive_len, &dir_drive,
114 &logon_script_len, &logon_script,
115 &profile_path_len, &profile_path,
116 &acct_desc_len, &acct_desc,
117 &workstations_len, &workstations,
118 &unknown_str_len, &unknown_str,
119 &munged_dial_len, &munged_dial,
122 &lmpwlen, &lm_pw_ptr,
123 &ntpwlen, &nt_pw_ptr,
137 /* validate the account and fill in UNIX uid and gid. Standard
138 * getpwnam() is used instead of Get_Pwnam() as we do not need
139 * to try case permutations
141 if (!username || !(pw = getpwnam_alloc(username))) {
142 if (!(tdb_state->permit_non_unix_accounts)) {
143 DEBUG(0,("tdbsam: getpwnam_alloc(%s) return NULL. User does not exist!\n", username));
155 pdb_set_uid(sampass, uid);
156 pdb_set_gid(sampass, gid);
159 pdb_set_logon_time(sampass, logon_time, True);
160 pdb_set_logoff_time(sampass, logoff_time, True);
161 pdb_set_kickoff_time(sampass, kickoff_time, True);
162 pdb_set_pass_can_change_time(sampass, pass_can_change_time, True);
163 pdb_set_pass_must_change_time(sampass, pass_must_change_time, True);
164 pdb_set_pass_last_set_time(sampass, pass_last_set_time);
166 pdb_set_username (sampass, username);
167 pdb_set_domain (sampass, domain);
168 pdb_set_nt_username (sampass, nt_username);
169 pdb_set_fullname (sampass, fullname);
171 if (homedir) setflag = True;
174 pstrcpy(sub_buffer, lp_logon_home());
175 /* standard_sub_advanced() assumes pstring is passed!! */
176 standard_sub_advanced(-1, username, "", gid, username, sub_buffer);
177 homedir = strdup(sub_buffer);
178 if(!homedir) { ret = False; goto done; }
179 DEBUG(5,("Home directory set back to %s\n", homedir));
181 pdb_set_homedir(sampass, homedir, setflag);
183 if (dir_drive) setflag = True;
186 pstrcpy(sub_buffer, lp_logon_drive());
187 standard_sub_advanced(-1, username, "", gid, username, sub_buffer);
188 dir_drive = strdup(sub_buffer);
189 if(!dir_drive) { ret = False; goto done; }
190 DEBUG(5,("Drive set back to %s\n", dir_drive));
192 pdb_set_dir_drive(sampass, dir_drive, setflag);
194 if (logon_script) setflag = True;
197 pstrcpy(sub_buffer, lp_logon_script());
198 standard_sub_advanced(-1, username, "", gid, username, sub_buffer);
199 logon_script = strdup(sub_buffer);
200 if(!logon_script) { ret = False; goto done; }
201 DEBUG(5,("Logon script set back to %s\n", logon_script));
203 pdb_set_logon_script(sampass, logon_script, setflag);
205 if (profile_path) setflag = True;
208 pstrcpy(sub_buffer, lp_logon_path());
209 standard_sub_advanced(-1, username, "", gid, username, sub_buffer);
210 profile_path = strdup(sub_buffer);
211 if(!profile_path) { ret = False; goto done; }
212 DEBUG(5,("Profile path set back to %s\n", profile_path));
214 pdb_set_profile_path(sampass, profile_path, setflag);
216 pdb_set_acct_desc (sampass, acct_desc);
217 pdb_set_workstations (sampass, workstations);
218 pdb_set_munged_dial (sampass, munged_dial);
219 if (!pdb_set_lanman_passwd(sampass, lm_pw_ptr)) {
223 if (!pdb_set_nt_passwd(sampass, nt_pw_ptr)) {
228 pdb_set_user_rid(sampass, user_rid);
229 pdb_set_group_rid(sampass, group_rid);
230 pdb_set_unknown_3(sampass, unknown_3);
231 pdb_set_hours_len(sampass, hours_len);
232 pdb_set_unknown_5(sampass, unknown_5);
233 pdb_set_unknown_6(sampass, unknown_6);
234 pdb_set_acct_ctrl(sampass, acct_ctrl);
235 pdb_set_logon_divs(sampass, logon_divs);
236 pdb_set_hours(sampass, hours);
242 SAFE_FREE(nt_username);
245 SAFE_FREE(dir_drive);
246 SAFE_FREE(logon_script);
247 SAFE_FREE(profile_path);
248 SAFE_FREE(acct_desc);
249 SAFE_FREE(workstations);
250 SAFE_FREE(munged_dial);
255 /**********************************************************************
256 Intialize a BYTE buffer from a SAM_ACCOUNT struct
257 *********************************************************************/
258 static uint32 init_buffer_from_sam (struct tdbsam_privates *tdb_state,
259 uint8 **buf, const SAM_ACCOUNT *sampass)
263 /* times are stored as 32bit integer
264 take care on system with 64bit wide time_t
270 pass_can_change_time,
271 pass_must_change_time;
273 uint32 user_rid, group_rid;
275 const char *username;
277 const char *nt_username;
278 const char *dir_drive;
279 const char *unknown_str;
280 const char *munged_dial;
281 const char *fullname;
283 const char *logon_script;
284 const char *profile_path;
285 const char *acct_desc;
286 const char *workstations;
287 uint32 username_len, domain_len, nt_username_len,
288 dir_drive_len, unknown_str_len, munged_dial_len,
289 fullname_len, homedir_len, logon_script_len,
290 profile_path_len, acct_desc_len, workstations_len;
294 uint32 lm_pw_len = 16;
295 uint32 nt_pw_len = 16;
297 /* do we have a valid SAM_ACCOUNT pointer? */
298 if (sampass == NULL) {
299 DEBUG(0, ("init_buffer_from_sam: SAM_ACCOUNT is NULL!\n"));
306 logon_time = (uint32)pdb_get_logon_time(sampass);
307 logoff_time = (uint32)pdb_get_logoff_time(sampass);
308 kickoff_time = (uint32)pdb_get_kickoff_time(sampass);
309 pass_can_change_time = (uint32)pdb_get_pass_can_change_time(sampass);
310 pass_must_change_time = (uint32)pdb_get_pass_must_change_time(sampass);
311 pass_last_set_time = (uint32)pdb_get_pass_last_set_time(sampass);
313 user_rid = pdb_get_user_rid(sampass);
314 group_rid = pdb_get_group_rid(sampass);
316 username = pdb_get_username(sampass);
317 if (username) username_len = strlen(username) +1;
318 else username_len = 0;
320 domain = pdb_get_domain(sampass);
321 if (domain) domain_len = strlen(domain) +1;
324 nt_username = pdb_get_nt_username(sampass);
325 if (nt_username) nt_username_len = strlen(nt_username) +1;
326 else nt_username_len = 0;
328 fullname = pdb_get_fullname(sampass);
329 if (fullname) fullname_len = strlen(fullname) +1;
330 else fullname_len = 0;
333 * Only updates fields which have been set (not defaults from smb.conf)
336 if (IS_SAM_SET(sampass, FLAG_SAM_DRIVE)) dir_drive = pdb_get_dirdrive(sampass);
337 else dir_drive = NULL;
338 if (dir_drive) dir_drive_len = strlen(dir_drive) +1;
339 else dir_drive_len = 0;
341 if (IS_SAM_SET(sampass, FLAG_SAM_SMBHOME)) homedir = pdb_get_homedir(sampass);
343 if (homedir) homedir_len = strlen(homedir) +1;
344 else homedir_len = 0;
346 if (IS_SAM_SET(sampass, FLAG_SAM_LOGONSCRIPT)) logon_script = pdb_get_logon_script(sampass);
347 else logon_script = NULL;
348 if (logon_script) logon_script_len = strlen(logon_script) +1;
349 else logon_script_len = 0;
351 if (IS_SAM_SET(sampass, FLAG_SAM_PROFILE)) profile_path = pdb_get_profile_path(sampass);
352 else profile_path = NULL;
353 if (profile_path) profile_path_len = strlen(profile_path) +1;
354 else profile_path_len = 0;
356 lm_pw = pdb_get_lanman_passwd(sampass);
357 if (!lm_pw) lm_pw_len = 0;
359 nt_pw = pdb_get_nt_passwd(sampass);
360 if (!nt_pw) nt_pw_len = 0;
362 acct_desc = pdb_get_acct_desc(sampass);
363 if (acct_desc) acct_desc_len = strlen(acct_desc) +1;
364 else acct_desc_len = 0;
366 workstations = pdb_get_workstations(sampass);
367 if (workstations) workstations_len = strlen(workstations) +1;
368 else workstations_len = 0;
373 munged_dial = pdb_get_munged_dial(sampass);
374 if (munged_dial) munged_dial_len = strlen(munged_dial) +1;
375 else munged_dial_len = 0;
377 /* one time to get the size needed */
378 len = tdb_pack(NULL, 0, TDB_FORMAT_STRING,
383 pass_can_change_time,
384 pass_must_change_time,
385 username_len, username,
387 nt_username_len, nt_username,
388 fullname_len, fullname,
389 homedir_len, homedir,
390 dir_drive_len, dir_drive,
391 logon_script_len, logon_script,
392 profile_path_len, profile_path,
393 acct_desc_len, acct_desc,
394 workstations_len, workstations,
395 unknown_str_len, unknown_str,
396 munged_dial_len, munged_dial,
401 pdb_get_acct_ctrl(sampass),
402 pdb_get_unknown3(sampass),
403 pdb_get_logon_divs(sampass),
404 pdb_get_hours_len(sampass),
405 MAX_HOURS_LEN, pdb_get_hours(sampass),
406 pdb_get_unknown5(sampass),
407 pdb_get_unknown6(sampass));
410 /* malloc the space needed */
411 if ( (*buf=(uint8*)malloc(len)) == NULL) {
412 DEBUG(0,("init_buffer_from_sam: Unable to malloc() memory for buffer!\n"));
416 /* now for the real call to tdb_pack() */
417 buflen = tdb_pack(*buf, len, TDB_FORMAT_STRING,
422 pass_can_change_time,
423 pass_must_change_time,
424 username_len, username,
426 nt_username_len, nt_username,
427 fullname_len, fullname,
428 homedir_len, homedir,
429 dir_drive_len, dir_drive,
430 logon_script_len, logon_script,
431 profile_path_len, profile_path,
432 acct_desc_len, acct_desc,
433 workstations_len, workstations,
434 unknown_str_len, unknown_str,
435 munged_dial_len, munged_dial,
440 pdb_get_acct_ctrl(sampass),
441 pdb_get_unknown3(sampass),
442 pdb_get_logon_divs(sampass),
443 pdb_get_hours_len(sampass),
444 MAX_HOURS_LEN, pdb_get_hours(sampass),
445 pdb_get_unknown5(sampass),
446 pdb_get_unknown6(sampass));
449 /* check to make sure we got it correct */
451 DEBUG(0, ("init_buffer_from_sam: somthing odd is going on here: bufflen (%d) != len (%d) in tdb_pack operations!\n",
461 /***************************************************************
462 Open the TDB passwd database for SAM account enumeration.
463 ****************************************************************/
465 static BOOL tdbsam_setsampwent(struct pdb_context *context, BOOL update)
467 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)context->pdb_selected->private_data;
469 /* Open tdb passwd */
470 if (!(tdb_state->passwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, update?(O_RDWR|O_CREAT):O_RDONLY, 0600)))
472 DEBUG(0, ("Unable to open/create TDB passwd\n"));
476 tdb_state->key = tdb_firstkey(tdb_state->passwd_tdb);
481 static void close_tdb(struct tdbsam_privates *tdb_state)
483 if (tdb_state->passwd_tdb) {
484 tdb_close(tdb_state->passwd_tdb);
485 tdb_state->passwd_tdb = NULL;
489 /***************************************************************
490 End enumeration of the TDB passwd list.
491 ****************************************************************/
493 static void tdbsam_endsampwent(struct pdb_context *context)
495 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)context->pdb_selected->private_data;
496 close_tdb(tdb_state);
498 DEBUG(7, ("endtdbpwent: closed sam database.\n"));
501 /*****************************************************************
502 Get one SAM_ACCOUNT from the TDB (next in line)
503 *****************************************************************/
505 static BOOL tdbsam_getsampwent(struct pdb_context *context, SAM_ACCOUNT *user)
507 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)context->pdb_selected->private_data;
509 char *prefix = USERPREFIX;
510 int prefixlen = strlen (prefix);
514 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
518 /* skip all non-USER entries (eg. RIDs) */
519 while ((tdb_state->key.dsize != 0) && (strncmp(tdb_state->key.dptr, prefix, prefixlen)))
520 /* increment to next in line */
521 tdb_state->key = tdb_nextkey(tdb_state->passwd_tdb, tdb_state->key);
523 /* do we have an valid interation pointer? */
524 if(tdb_state->passwd_tdb == NULL) {
525 DEBUG(0,("pdb_get_sampwent: Bad TDB Context pointer.\n"));
529 data = tdb_fetch(tdb_state->passwd_tdb, tdb_state->key);
531 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
535 /* unpack the buffer */
536 if (!init_sam_from_buffer(tdb_state, user, data.dptr, data.dsize)) {
537 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
538 SAFE_FREE(data.dptr);
541 SAFE_FREE(data.dptr);
543 /* increment to next in line */
544 tdb_state->key = tdb_nextkey(tdb_state->passwd_tdb, tdb_state->key);
549 /******************************************************************
550 Lookup a name in the SAM TDB
551 ******************************************************************/
553 static BOOL tdbsam_getsampwnam (struct pdb_context *context, SAM_ACCOUNT *user, const char *sname)
555 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)context->pdb_selected->private_data;
556 TDB_CONTEXT *pwd_tdb;
562 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
566 /* Data is stored in all lower-case */
567 unix_strlower(sname, -1, name, sizeof(name));
570 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
572 key.dsize = strlen(keystr) + 1;
574 /* open the accounts TDB */
575 if (!(pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
576 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
581 data = tdb_fetch(pwd_tdb, key);
583 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
584 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
585 DEBUGADD(5, (" Key: %s\n", keystr));
590 /* unpack the buffer */
591 if (!init_sam_from_buffer(tdb_state, user, data.dptr, data.dsize)) {
592 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
593 SAFE_FREE(data.dptr);
597 SAFE_FREE(data.dptr);
599 /* no further use for database, close it now */
605 /***************************************************************************
607 **************************************************************************/
609 static BOOL tdbsam_getsampwrid (struct pdb_context *context, SAM_ACCOUNT *user, uint32 rid)
611 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)context->pdb_selected->private_data;
612 TDB_CONTEXT *pwd_tdb;
618 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
623 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
625 key.dsize = strlen (keystr) + 1;
627 /* open the accounts TDB */
628 if (!(pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
629 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
634 data = tdb_fetch (pwd_tdb, key);
636 DEBUG(5,("pdb_getsampwrid (TDB): error looking up RID %d by key %s.\n", rid, keystr));
637 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
642 fstrcpy (name, data.dptr);
643 SAFE_FREE(data.dptr);
647 return tdbsam_getsampwnam (context, user, name);
650 /***************************************************************************
652 ****************************************************************************/
654 static BOOL tdbsam_delete_sam_account(struct pdb_context *context, const SAM_ACCOUNT *sam_pass)
656 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)context->pdb_selected->private_data;
657 TDB_CONTEXT *pwd_tdb;
663 unix_strlower(pdb_get_username(sam_pass), -1, name, sizeof(name));
666 if (!(pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDWR, 0600))) {
667 DEBUG(0, ("Unable to open TDB passwd!"));
671 /* set the search key */
672 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
674 key.dsize = strlen (keystr) + 1;
676 rid = pdb_get_user_rid(sam_pass);
678 /* it's outaa here! 8^) */
679 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
680 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
681 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
686 /* delete also the RID key */
688 /* set the search key */
689 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
691 key.dsize = strlen (keystr) + 1;
693 /* it's outaa here! 8^) */
694 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
695 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
696 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
706 /***************************************************************************
708 ****************************************************************************/
710 static BOOL tdb_update_sam(struct pdb_context *context, const SAM_ACCOUNT* newpwd, int flag)
712 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)context->pdb_selected->private_data;
713 TDB_CONTEXT *pwd_tdb = NULL;
722 /* invalidate the existing TDB iterator if it is open */
723 if (tdb_state->passwd_tdb) {
724 tdb_close(tdb_state->passwd_tdb);
725 tdb_state->passwd_tdb = NULL;
728 /* open the account TDB passwd*/
729 pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
732 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
736 /* if flag == TDB_INSERT then make up a new RID else throw an error. */
737 if (!(user_rid = pdb_get_user_rid(newpwd))) {
738 if (flag & TDB_INSERT) {
740 tdb_ret = tdb_change_int32_atomic(pwd_tdb, "RID_COUNTER", &user_rid, RID_MULTIPLIER);
745 pdb_set_user_rid(newpwd, user_rid);
747 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a RID\n",pdb_get_username(newpwd)));
753 if (!pdb_get_group_rid(newpwd)) {
754 if (flag & TDB_INSERT) {
755 if (!tdb_state->permit_non_unix_accounts) {
756 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a primary group RID\n",pdb_get_username(newpwd)));
760 /* This seems like a good default choice for non-unix users */
761 pdb_set_group_rid(newpwd, DOMAIN_GROUP_RID_USERS);
764 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a primary group RID\n",pdb_get_username(newpwd)));
770 /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
771 if ((data.dsize=init_buffer_from_sam (tdb_state, &buf, newpwd)) == -1) {
772 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
778 unix_strlower(pdb_get_username(newpwd), -1, name, sizeof(name));
780 DEBUG(5, ("Storing %saccount %s with RID %d\n", flag == TDB_INSERT ? "(new) " : "", name, user_rid));
782 /* setup the USER index key */
783 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
785 key.dsize = strlen (keystr) + 1;
787 /* add the account */
788 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
789 DEBUG(0, ("Unable to modify passwd TDB!"));
790 DEBUGADD(0, (" Error: %s", tdb_errorstr(pwd_tdb)));
791 DEBUGADD(0, (" occured while storing the main record (%s)\n", keystr));
797 data.dsize = sizeof(fstring);
800 /* setup the RID index key */
801 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, user_rid);
803 key.dsize = strlen (keystr) + 1;
805 /* add the reference */
806 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
807 DEBUG(0, ("Unable to modify TDB passwd !"));
808 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
809 DEBUGADD(0, (" occured while storing the RID index (%s)\n", keystr));
822 /***************************************************************************
823 Modifies an existing SAM_ACCOUNT
824 ****************************************************************************/
826 static BOOL tdbsam_update_sam_account (struct pdb_context *context, const SAM_ACCOUNT *newpwd)
828 return (tdb_update_sam(context, newpwd, TDB_MODIFY));
831 /***************************************************************************
832 Adds an existing SAM_ACCOUNT
833 ****************************************************************************/
835 static BOOL tdbsam_add_sam_account (struct pdb_context *context, const SAM_ACCOUNT *newpwd)
837 return (tdb_update_sam(context, newpwd, TDB_INSERT));
840 static void free_private_data(void **vp)
842 struct tdbsam_privates **tdb_state = (struct tdbsam_privates **)vp;
843 close_tdb(*tdb_state);
846 /* No need to free any further, as it is talloc()ed */
850 NTSTATUS pdb_init_tdbsam(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
853 struct tdbsam_privates *tdb_state;
855 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
859 (*pdb_method)->name = "tdbsam";
861 (*pdb_method)->setsampwent = tdbsam_setsampwent;
862 (*pdb_method)->endsampwent = tdbsam_endsampwent;
863 (*pdb_method)->getsampwent = tdbsam_getsampwent;
864 (*pdb_method)->getsampwnam = tdbsam_getsampwnam;
865 (*pdb_method)->getsampwrid = tdbsam_getsampwrid;
866 (*pdb_method)->add_sam_account = tdbsam_add_sam_account;
867 (*pdb_method)->update_sam_account = tdbsam_update_sam_account;
868 (*pdb_method)->delete_sam_account = tdbsam_delete_sam_account;
870 tdb_state = talloc_zero(pdb_context->mem_ctx, sizeof(struct tdbsam_privates));
873 DEBUG(0, ("talloc() failed for tdbsam private_data!\n"));
874 return NT_STATUS_NO_MEMORY;
878 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, location);
881 get_private_directory(tdbfile);
882 pstrcat(tdbfile, "/");
883 pstrcat(tdbfile, PASSDB_FILE_NAME);
884 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, tdbfile);
887 (*pdb_method)->private_data = tdb_state;
889 (*pdb_method)->free_private_data = free_private_data;
894 NTSTATUS pdb_init_tdbsam_nua(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
897 struct tdbsam_privates *tdb_state;
898 uint32 low_nua_uid, high_nua_uid;
900 if (!NT_STATUS_IS_OK(nt_status = pdb_init_tdbsam(pdb_context, pdb_method, location))) {
904 (*pdb_method)->name = "tdbsam_nua";
906 tdb_state = (*pdb_method)->private_data;
908 tdb_state->permit_non_unix_accounts = True;
910 if (!lp_non_unix_account_range(&low_nua_uid, &high_nua_uid)) {
911 DEBUG(0, ("cannot use tdbsam_nua without 'non unix account range' in smb.conf!\n"));
912 return NT_STATUS_UNSUCCESSFUL;
915 /* tdb_state->low_nua_rid=fallback_pdb_uid_to_user_rid(low_nua_uid);
917 tdb_state->high_nua_rid=fallback_pdb_uid_to_user_rid(high_nua_uid);
925 NTSTATUS pdb_init_tdbsam(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
927 DEBUG(0, ("tdbsam not compiled in!\n"));
928 return NT_STATUS_UNSUCCESSFUL;
931 NTSTATUS pdb_init_tdbsam_nua(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
933 DEBUG(0, ("tdbsam_nua not compiled in!\n"));
934 return NT_STATUS_UNSUCCESSFUL;