2 * Unix SMB/CIFS implementation.
3 * SMB parameters and setup
4 * Copyright (C) Andrew Tridgell 1992-1998
5 * Copyright (C) Simo Sorce 2000-2002
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.
27 #if 0 /* when made a module use this */
29 static int tdbsam_debug_level
= DBGC_ALL
;
31 #define DBGC_CLASS tdbsam_debug_level
36 #define DBGC_CLASS DBGC_PASSDB
40 #define PDB_VERSION "20010830"
41 #define PASSDB_FILE_NAME "passdb.tdb"
42 #define TDB_FORMAT_STRING "ddddddBBBBBBBBBBBBddBBwdwdBdd"
43 #define USERPREFIX "USER_"
44 #define RIDPREFIX "RID_"
46 struct tdbsam_privates
{
47 TDB_CONTEXT
*passwd_tdb
;
50 /* retrive-once info */
51 const char *tdbsam_location
;
53 BOOL permit_non_unix_accounts
;
55 BOOL algorithmic_rids
;
61 /**********************************************************************
62 Intialize a SAM_ACCOUNT struct from a BYTE buffer of size len
63 *********************************************************************/
65 static BOOL
init_sam_from_buffer (struct tdbsam_privates
*tdb_state
,
66 SAM_ACCOUNT
*sampass
, uint8
*buf
, uint32 buflen
)
69 /* times are stored as 32bit integer
70 take care on system with 64bit wide time_t
77 pass_must_change_time
;
90 uint32 username_len
, domain_len
, nt_username_len
,
91 dir_drive_len
, unknown_str_len
, munged_dial_len
,
92 fullname_len
, homedir_len
, logon_script_len
,
93 profile_path_len
, acct_desc_len
, workstations_len
;
95 uint32 user_rid
, group_rid
, unknown_3
, hours_len
, unknown_5
, unknown_6
;
96 uint16 acct_ctrl
, logon_divs
;
98 static uint8
*lm_pw_ptr
, *nt_pw_ptr
;
100 uint32 lm_pw_len
, nt_pw_len
, hourslen
;
106 if(sampass
== NULL
|| buf
== NULL
) {
107 DEBUG(0, ("init_sam_from_buffer: NULL parameters found!\n"));
111 /* unpack the buffer into variables */
112 len
= tdb_unpack (buf
, buflen
, TDB_FORMAT_STRING
,
117 &pass_can_change_time
,
118 &pass_must_change_time
,
119 &username_len
, &username
,
120 &domain_len
, &domain
,
121 &nt_username_len
, &nt_username
,
122 &fullname_len
, &fullname
,
123 &homedir_len
, &homedir
,
124 &dir_drive_len
, &dir_drive
,
125 &logon_script_len
, &logon_script
,
126 &profile_path_len
, &profile_path
,
127 &acct_desc_len
, &acct_desc
,
128 &workstations_len
, &workstations
,
129 &unknown_str_len
, &unknown_str
,
130 &munged_dial_len
, &munged_dial
,
133 &lm_pw_len
, &lm_pw_ptr
,
134 &nt_pw_len
, &nt_pw_ptr
,
148 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
149 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
150 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
151 pdb_set_pass_can_change_time(sampass
, pass_can_change_time
, PDB_SET
);
152 pdb_set_pass_must_change_time(sampass
, pass_must_change_time
, PDB_SET
);
153 pdb_set_pass_last_set_time(sampass
, pass_last_set_time
, PDB_SET
);
155 pdb_set_username (sampass
, username
, PDB_SET
);
156 pdb_set_domain (sampass
, domain
, PDB_SET
);
157 pdb_set_nt_username (sampass
, nt_username
, PDB_SET
);
158 pdb_set_fullname (sampass
, fullname
, PDB_SET
);
161 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
164 pdb_set_homedir(sampass
,
165 talloc_sub_specified(sampass
->mem_ctx
,
173 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
175 pdb_set_dir_drive(sampass
,
176 talloc_sub_specified(sampass
->mem_ctx
,
184 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
186 pdb_set_logon_script(sampass
,
187 talloc_sub_specified(sampass
->mem_ctx
,
195 pdb_set_profile_path(sampass
, profile_path
, PDB_SET
);
197 pdb_set_profile_path(sampass
,
198 talloc_sub_specified(sampass
->mem_ctx
,
205 pdb_set_acct_desc (sampass
, acct_desc
, PDB_SET
);
206 pdb_set_workstations (sampass
, workstations
, PDB_SET
);
207 pdb_set_munged_dial (sampass
, munged_dial
, PDB_SET
);
209 if (lm_pw_ptr
&& lm_pw_len
== LM_HASH_LEN
) {
210 if (!pdb_set_lanman_passwd(sampass
, lm_pw_ptr
, PDB_SET
)) {
216 if (nt_pw_ptr
&& nt_pw_len
== NT_HASH_LEN
) {
217 if (!pdb_set_nt_passwd(sampass
, nt_pw_ptr
, PDB_SET
)) {
223 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
224 pdb_set_group_sid_from_rid(sampass
, group_rid
, PDB_SET
);
225 pdb_set_unknown_3(sampass
, unknown_3
, PDB_SET
);
226 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
227 pdb_set_unknown_5(sampass
, unknown_5
, PDB_SET
);
228 pdb_set_unknown_6(sampass
, unknown_6
, PDB_SET
);
229 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
230 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
231 pdb_set_hours(sampass
, hours
, PDB_SET
);
237 SAFE_FREE(nt_username
);
240 SAFE_FREE(dir_drive
);
241 SAFE_FREE(logon_script
);
242 SAFE_FREE(profile_path
);
243 SAFE_FREE(acct_desc
);
244 SAFE_FREE(workstations
);
245 SAFE_FREE(munged_dial
);
250 /**********************************************************************
251 Intialize a BYTE buffer from a SAM_ACCOUNT struct
252 *********************************************************************/
253 static uint32
init_buffer_from_sam (struct tdbsam_privates
*tdb_state
,
254 uint8
**buf
, const SAM_ACCOUNT
*sampass
)
258 /* times are stored as 32bit integer
259 take care on system with 64bit wide time_t
265 pass_can_change_time
,
266 pass_must_change_time
;
268 uint32 user_rid
, group_rid
;
270 const char *username
;
272 const char *nt_username
;
273 const char *dir_drive
;
274 const char *unknown_str
;
275 const char *munged_dial
;
276 const char *fullname
;
278 const char *logon_script
;
279 const char *profile_path
;
280 const char *acct_desc
;
281 const char *workstations
;
282 uint32 username_len
, domain_len
, nt_username_len
,
283 dir_drive_len
, unknown_str_len
, munged_dial_len
,
284 fullname_len
, homedir_len
, logon_script_len
,
285 profile_path_len
, acct_desc_len
, workstations_len
;
289 uint32 lm_pw_len
= 16;
290 uint32 nt_pw_len
= 16;
292 /* do we have a valid SAM_ACCOUNT pointer? */
293 if (sampass
== NULL
) {
294 DEBUG(0, ("init_buffer_from_sam: SAM_ACCOUNT is NULL!\n"));
301 logon_time
= (uint32
)pdb_get_logon_time(sampass
);
302 logoff_time
= (uint32
)pdb_get_logoff_time(sampass
);
303 kickoff_time
= (uint32
)pdb_get_kickoff_time(sampass
);
304 pass_can_change_time
= (uint32
)pdb_get_pass_can_change_time(sampass
);
305 pass_must_change_time
= (uint32
)pdb_get_pass_must_change_time(sampass
);
306 pass_last_set_time
= (uint32
)pdb_get_pass_last_set_time(sampass
);
308 user_rid
= pdb_get_user_rid(sampass
);
309 group_rid
= pdb_get_group_rid(sampass
);
311 username
= pdb_get_username(sampass
);
312 if (username
) username_len
= strlen(username
) +1;
313 else username_len
= 0;
315 domain
= pdb_get_domain(sampass
);
316 if (domain
) domain_len
= strlen(domain
) +1;
319 nt_username
= pdb_get_nt_username(sampass
);
320 if (nt_username
) nt_username_len
= strlen(nt_username
) +1;
321 else nt_username_len
= 0;
323 fullname
= pdb_get_fullname(sampass
);
324 if (fullname
) fullname_len
= strlen(fullname
) +1;
325 else fullname_len
= 0;
328 * Only updates fields which have been set (not defaults from smb.conf)
331 if (!IS_SAM_DEFAULT(sampass
, PDB_DRIVE
))
332 dir_drive
= pdb_get_dir_drive(sampass
);
333 else dir_drive
= NULL
;
334 if (dir_drive
) dir_drive_len
= strlen(dir_drive
) +1;
335 else dir_drive_len
= 0;
337 if (!IS_SAM_DEFAULT(sampass
, PDB_SMBHOME
)) homedir
= pdb_get_homedir(sampass
);
339 if (homedir
) homedir_len
= strlen(homedir
) +1;
340 else homedir_len
= 0;
342 if (!IS_SAM_DEFAULT(sampass
, PDB_LOGONSCRIPT
)) logon_script
= pdb_get_logon_script(sampass
);
343 else logon_script
= NULL
;
344 if (logon_script
) logon_script_len
= strlen(logon_script
) +1;
345 else logon_script_len
= 0;
347 if (!IS_SAM_DEFAULT(sampass
, PDB_PROFILE
)) profile_path
= pdb_get_profile_path(sampass
);
348 else profile_path
= NULL
;
349 if (profile_path
) profile_path_len
= strlen(profile_path
) +1;
350 else profile_path_len
= 0;
352 lm_pw
= pdb_get_lanman_passwd(sampass
);
353 if (!lm_pw
) lm_pw_len
= 0;
355 nt_pw
= pdb_get_nt_passwd(sampass
);
356 if (!nt_pw
) nt_pw_len
= 0;
358 acct_desc
= pdb_get_acct_desc(sampass
);
359 if (acct_desc
) acct_desc_len
= strlen(acct_desc
) +1;
360 else acct_desc_len
= 0;
362 workstations
= pdb_get_workstations(sampass
);
363 if (workstations
) workstations_len
= strlen(workstations
) +1;
364 else workstations_len
= 0;
369 munged_dial
= pdb_get_munged_dial(sampass
);
370 if (munged_dial
) munged_dial_len
= strlen(munged_dial
) +1;
371 else munged_dial_len
= 0;
373 /* one time to get the size needed */
374 len
= tdb_pack(NULL
, 0, TDB_FORMAT_STRING
,
379 pass_can_change_time
,
380 pass_must_change_time
,
381 username_len
, username
,
383 nt_username_len
, nt_username
,
384 fullname_len
, fullname
,
385 homedir_len
, homedir
,
386 dir_drive_len
, dir_drive
,
387 logon_script_len
, logon_script
,
388 profile_path_len
, profile_path
,
389 acct_desc_len
, acct_desc
,
390 workstations_len
, workstations
,
391 unknown_str_len
, unknown_str
,
392 munged_dial_len
, munged_dial
,
397 pdb_get_acct_ctrl(sampass
),
398 pdb_get_unknown_3(sampass
),
399 pdb_get_logon_divs(sampass
),
400 pdb_get_hours_len(sampass
),
401 MAX_HOURS_LEN
, pdb_get_hours(sampass
),
402 pdb_get_unknown_5(sampass
),
403 pdb_get_unknown_6(sampass
));
406 /* malloc the space needed */
407 if ( (*buf
=(uint8
*)malloc(len
)) == NULL
) {
408 DEBUG(0,("init_buffer_from_sam: Unable to malloc() memory for buffer!\n"));
412 /* now for the real call to tdb_pack() */
413 buflen
= tdb_pack(*buf
, len
, TDB_FORMAT_STRING
,
418 pass_can_change_time
,
419 pass_must_change_time
,
420 username_len
, username
,
422 nt_username_len
, nt_username
,
423 fullname_len
, fullname
,
424 homedir_len
, homedir
,
425 dir_drive_len
, dir_drive
,
426 logon_script_len
, logon_script
,
427 profile_path_len
, profile_path
,
428 acct_desc_len
, acct_desc
,
429 workstations_len
, workstations
,
430 unknown_str_len
, unknown_str
,
431 munged_dial_len
, munged_dial
,
436 pdb_get_acct_ctrl(sampass
),
437 pdb_get_unknown_3(sampass
),
438 pdb_get_logon_divs(sampass
),
439 pdb_get_hours_len(sampass
),
440 MAX_HOURS_LEN
, pdb_get_hours(sampass
),
441 pdb_get_unknown_5(sampass
),
442 pdb_get_unknown_6(sampass
));
445 /* check to make sure we got it correct */
447 DEBUG(0, ("init_buffer_from_sam: somthing odd is going on here: bufflen (%d) != len (%d) in tdb_pack operations!\n",
457 /***************************************************************
458 Open the TDB passwd database for SAM account enumeration.
459 ****************************************************************/
461 static NTSTATUS
tdbsam_setsampwent(struct pdb_methods
*my_methods
, BOOL update
)
463 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)my_methods
->private_data
;
465 /* Open tdb passwd */
466 if (!(tdb_state
->passwd_tdb
= tdb_open_log(tdb_state
->tdbsam_location
, 0, TDB_DEFAULT
, update
?(O_RDWR
|O_CREAT
):O_RDONLY
, 0600)))
468 DEBUG(0, ("Unable to open/create TDB passwd\n"));
469 return NT_STATUS_UNSUCCESSFUL
;
472 tdb_state
->key
= tdb_firstkey(tdb_state
->passwd_tdb
);
477 static void close_tdb(struct tdbsam_privates
*tdb_state
)
479 if (tdb_state
->passwd_tdb
) {
480 tdb_close(tdb_state
->passwd_tdb
);
481 tdb_state
->passwd_tdb
= NULL
;
485 /***************************************************************
486 End enumeration of the TDB passwd list.
487 ****************************************************************/
489 static void tdbsam_endsampwent(struct pdb_methods
*my_methods
)
491 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)my_methods
->private_data
;
492 close_tdb(tdb_state
);
494 DEBUG(7, ("endtdbpwent: closed sam database.\n"));
497 /*****************************************************************
498 Get one SAM_ACCOUNT from the TDB (next in line)
499 *****************************************************************/
501 static NTSTATUS
tdbsam_getsampwent(struct pdb_methods
*my_methods
, SAM_ACCOUNT
*user
)
503 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
504 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)my_methods
->private_data
;
506 const char *prefix
= USERPREFIX
;
507 int prefixlen
= strlen (prefix
);
511 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
515 /* skip all non-USER entries (eg. RIDs) */
516 while ((tdb_state
->key
.dsize
!= 0) && (strncmp(tdb_state
->key
.dptr
, prefix
, prefixlen
)))
517 /* increment to next in line */
518 tdb_state
->key
= tdb_nextkey(tdb_state
->passwd_tdb
, tdb_state
->key
);
520 /* do we have an valid iteration pointer? */
521 if(tdb_state
->passwd_tdb
== NULL
) {
522 DEBUG(0,("pdb_get_sampwent: Bad TDB Context pointer.\n"));
526 data
= tdb_fetch(tdb_state
->passwd_tdb
, tdb_state
->key
);
528 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
532 /* unpack the buffer */
533 if (!init_sam_from_buffer(tdb_state
, user
, data
.dptr
, data
.dsize
)) {
534 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
535 SAFE_FREE(data
.dptr
);
538 SAFE_FREE(data
.dptr
);
540 /* increment to next in line */
541 tdb_state
->key
= tdb_nextkey(tdb_state
->passwd_tdb
, tdb_state
->key
);
546 /******************************************************************
547 Lookup a name in the SAM TDB
548 ******************************************************************/
550 static NTSTATUS
tdbsam_getsampwnam (struct pdb_methods
*my_methods
, SAM_ACCOUNT
*user
, const char *sname
)
552 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
553 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)my_methods
->private_data
;
554 TDB_CONTEXT
*pwd_tdb
;
560 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
565 /* Data is stored in all lower-case */
566 fstrcpy(name
, sname
);
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 NTSTATUS
tdbsam_getsampwrid (struct pdb_methods
*my_methods
, SAM_ACCOUNT
*user
, uint32 rid
)
611 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
612 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)my_methods
->private_data
;
613 TDB_CONTEXT
*pwd_tdb
;
619 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
624 slprintf(keystr
, sizeof(keystr
)-1, "%s%.8x", RIDPREFIX
, rid
);
626 key
.dsize
= strlen (keystr
) + 1;
628 /* open the accounts TDB */
629 if (!(pwd_tdb
= tdb_open_log(tdb_state
->tdbsam_location
, 0, TDB_DEFAULT
, O_RDONLY
, 0600))) {
630 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
635 data
= tdb_fetch (pwd_tdb
, key
);
637 DEBUG(5,("pdb_getsampwrid (TDB): error looking up RID %d by key %s.\n", rid
, keystr
));
638 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb
)));
643 fstrcpy (name
, data
.dptr
);
644 SAFE_FREE(data
.dptr
);
648 return tdbsam_getsampwnam (my_methods
, user
, name
);
651 static NTSTATUS
tdbsam_getsampwsid(struct pdb_methods
*my_methods
, SAM_ACCOUNT
* user
, const DOM_SID
*sid
)
654 if (!sid_peek_check_rid(get_global_sam_sid(), sid
, &rid
))
655 return NT_STATUS_UNSUCCESSFUL
;
656 return tdbsam_getsampwrid(my_methods
, user
, rid
);
659 /***************************************************************************
661 ****************************************************************************/
663 static NTSTATUS
tdbsam_delete_sam_account(struct pdb_methods
*my_methods
, SAM_ACCOUNT
*sam_pass
)
665 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
666 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)my_methods
->private_data
;
667 TDB_CONTEXT
*pwd_tdb
;
673 fstrcpy(name
, pdb_get_username(sam_pass
));
677 if (!(pwd_tdb
= tdb_open_log(tdb_state
->tdbsam_location
, 0, TDB_DEFAULT
, O_RDWR
, 0600))) {
678 DEBUG(0, ("Unable to open TDB passwd!"));
682 /* set the search key */
683 slprintf(keystr
, sizeof(keystr
)-1, "%s%s", USERPREFIX
, name
);
685 key
.dsize
= strlen (keystr
) + 1;
687 rid
= pdb_get_user_rid(sam_pass
);
689 /* it's outaa here! 8^) */
690 if (tdb_delete(pwd_tdb
, key
) != TDB_SUCCESS
) {
691 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
692 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb
)));
697 /* delete also the RID key */
699 /* set the search key */
700 slprintf(keystr
, sizeof(keystr
)-1, "%s%.8x", RIDPREFIX
, rid
);
702 key
.dsize
= strlen (keystr
) + 1;
704 /* it's outaa here! 8^) */
705 if (tdb_delete(pwd_tdb
, key
) != TDB_SUCCESS
) {
706 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
707 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb
)));
717 /***************************************************************************
719 ****************************************************************************/
721 static BOOL
tdb_update_sam(struct pdb_methods
*my_methods
, SAM_ACCOUNT
* newpwd
, int flag
)
723 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)my_methods
->private_data
;
724 TDB_CONTEXT
*pwd_tdb
= NULL
;
733 /* invalidate the existing TDB iterator if it is open */
734 if (tdb_state
->passwd_tdb
) {
735 tdb_close(tdb_state
->passwd_tdb
);
736 tdb_state
->passwd_tdb
= NULL
;
739 /* open the account TDB passwd*/
740 pwd_tdb
= tdb_open_log(tdb_state
->tdbsam_location
, 0, TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0600);
743 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd (%s)!\n", tdb_state
->tdbsam_location
));
747 if (!pdb_get_group_rid(newpwd
)) {
748 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a primary group RID\n",pdb_get_username(newpwd
)));
753 /* if flag == TDB_INSERT then make up a new RID else throw an error. */
754 if (!(user_rid
= pdb_get_user_rid(newpwd
))) {
755 if ((flag
& TDB_INSERT
) && tdb_state
->permit_non_unix_accounts
) {
756 uint32 lowrid
, highrid
;
757 if (!idmap_get_free_rid_range(&lowrid
, &highrid
)) {
758 /* should never happen */
759 DEBUG(0, ("tdbsam: something messed up, no high/low rids but nua enabled ?!\n"));
764 tdb_ret
= tdb_change_uint32_atomic(pwd_tdb
, "RID_COUNTER", &user_rid
, RID_MULTIPLIER
);
769 if (user_rid
> highrid
) {
770 DEBUG(0, ("tdbsam: no NUA rids available, cannot add user %s!\n", pdb_get_username(newpwd
)));
775 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a RID\n",pdb_get_username(newpwd
)));
781 /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
782 if ((data
.dsize
=init_buffer_from_sam (tdb_state
, &buf
, newpwd
)) == -1) {
783 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
789 fstrcpy(name
, pdb_get_username(newpwd
));
792 DEBUG(5, ("Storing %saccount %s with RID %d\n", flag
== TDB_INSERT
? "(new) " : "", name
, user_rid
));
794 /* setup the USER index key */
795 slprintf(keystr
, sizeof(keystr
)-1, "%s%s", USERPREFIX
, name
);
797 key
.dsize
= strlen (keystr
) + 1;
799 /* add the account */
800 if (tdb_store(pwd_tdb
, key
, data
, flag
) != TDB_SUCCESS
) {
801 DEBUG(0, ("Unable to modify passwd TDB!"));
802 DEBUGADD(0, (" Error: %s", tdb_errorstr(pwd_tdb
)));
803 DEBUGADD(0, (" occured while storing the main record (%s)\n", keystr
));
809 data
.dsize
= sizeof(fstring
);
812 /* setup the RID index key */
813 slprintf(keystr
, sizeof(keystr
)-1, "%s%.8x", RIDPREFIX
, user_rid
);
815 key
.dsize
= strlen (keystr
) + 1;
817 /* add the reference */
818 if (tdb_store(pwd_tdb
, key
, data
, flag
) != TDB_SUCCESS
) {
819 DEBUG(0, ("Unable to modify TDB passwd !"));
820 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb
)));
821 DEBUGADD(0, (" occured while storing the RID index (%s)\n", keystr
));
835 /***************************************************************************
836 Allocates a new RID and returns it to the caller as a domain sid
838 NOTE: Use carefullt, do not waste RIDs they are a limited resource!
840 ***************************************************************************/
842 static NTSTATUS
tdbsam_get_next_sid (struct pdb_methods
*my_methods
, DOM_SID
*sid
)
844 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
845 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)my_methods
->private_data
;
846 TDB_CONTEXT
*pwd_tdb
;
850 return NT_STATUS_INVALID_PARAMETER
;
853 pwd_tdb
= tdb_open_log(tdb_state
->tdbsam_location
, 0, TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0600);
856 DEBUG(0, ("tdbsam_get_next_sid: Unable to open TDB passwd (%s)!\n", tdb_state
->tdbsam_location
));
857 return NT_STATUS_UNSUCCESSFUL
;
861 if (tdb_change_uint32_atomic(pwd_tdb
, "RID_COUNTER", &rid
, 1)) {
863 sid_copy(sid
, get_global_sam_sid());
864 if (!sid_append_rid(sid
, rid
)) {
877 /***************************************************************************
878 Modifies an existing SAM_ACCOUNT
879 ****************************************************************************/
881 static NTSTATUS
tdbsam_update_sam_account (struct pdb_methods
*my_methods
, SAM_ACCOUNT
*newpwd
)
883 if (tdb_update_sam(my_methods
, newpwd
, TDB_MODIFY
))
886 return NT_STATUS_UNSUCCESSFUL
;
889 /***************************************************************************
890 Adds an existing SAM_ACCOUNT
891 ****************************************************************************/
893 static NTSTATUS
tdbsam_add_sam_account (struct pdb_methods
*my_methods
, SAM_ACCOUNT
*newpwd
)
895 if (tdb_update_sam(my_methods
, newpwd
, TDB_INSERT
))
898 return NT_STATUS_UNSUCCESSFUL
;
901 static void free_private_data(void **vp
)
903 struct tdbsam_privates
**tdb_state
= (struct tdbsam_privates
**)vp
;
904 close_tdb(*tdb_state
);
907 /* No need to free any further, as it is talloc()ed */
911 NTSTATUS
pdb_init_tdbsam(PDB_CONTEXT
*pdb_context
, PDB_METHODS
**pdb_method
, const char *location
)
914 struct tdbsam_privates
*tdb_state
;
915 uint32 low_nua_uid
, high_nua_uid
;
917 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_methods(pdb_context
->mem_ctx
, pdb_method
))) {
921 (*pdb_method
)->name
= "tdbsam";
923 (*pdb_method
)->setsampwent
= tdbsam_setsampwent
;
924 (*pdb_method
)->endsampwent
= tdbsam_endsampwent
;
925 (*pdb_method
)->getsampwent
= tdbsam_getsampwent
;
926 (*pdb_method
)->getsampwnam
= tdbsam_getsampwnam
;
927 (*pdb_method
)->getsampwsid
= tdbsam_getsampwsid
;
928 (*pdb_method
)->add_sam_account
= tdbsam_add_sam_account
;
929 (*pdb_method
)->update_sam_account
= tdbsam_update_sam_account
;
930 (*pdb_method
)->delete_sam_account
= tdbsam_delete_sam_account
;
932 tdb_state
= talloc_zero(pdb_context
->mem_ctx
, sizeof(struct tdbsam_privates
));
935 DEBUG(0, ("talloc() failed for tdbsam private_data!\n"));
936 return NT_STATUS_NO_MEMORY
;
940 tdb_state
->tdbsam_location
= talloc_strdup(pdb_context
->mem_ctx
, location
);
943 get_private_directory(tdbfile
);
944 pstrcat(tdbfile
, "/");
945 pstrcat(tdbfile
, PASSDB_FILE_NAME
);
946 tdb_state
->tdbsam_location
= talloc_strdup(pdb_context
->mem_ctx
, tdbfile
);
949 (*pdb_method
)->private_data
= tdb_state
;
951 (*pdb_method
)->free_private_data
= free_private_data
;
953 if (lp_idmap_uid(&low_nua_uid
, &high_nua_uid
)) {
954 DEBUG(0, ("idmap uid range defined, non unix accounts enabled\n"));
956 tdb_state
->permit_non_unix_accounts
= True
;
958 tdb_state
->low_nua_rid
=fallback_pdb_uid_to_user_rid(low_nua_uid
);
960 tdb_state
->high_nua_rid
=fallback_pdb_uid_to_user_rid(high_nua_uid
);
963 tdb_state
->algorithmic_rids
= True
;
969 int pdb_tdbsam_init(void)
971 smb_register_passdb(PASSDB_INTERFACE_VERSION
, "tdbsam", pdb_init_tdbsam
);