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
;
105 if(sampass
== NULL
|| buf
== NULL
) {
106 DEBUG(0, ("init_sam_from_buffer: NULL parameters found!\n"));
110 /* unpack the buffer into variables */
111 len
= tdb_unpack (buf
, buflen
, TDB_FORMAT_STRING
,
116 &pass_can_change_time
,
117 &pass_must_change_time
,
118 &username_len
, &username
,
119 &domain_len
, &domain
,
120 &nt_username_len
, &nt_username
,
121 &fullname_len
, &fullname
,
122 &homedir_len
, &homedir
,
123 &dir_drive_len
, &dir_drive
,
124 &logon_script_len
, &logon_script
,
125 &profile_path_len
, &profile_path
,
126 &acct_desc_len
, &acct_desc
,
127 &workstations_len
, &workstations
,
128 &unknown_str_len
, &unknown_str
,
129 &munged_dial_len
, &munged_dial
,
132 &lm_pw_len
, &lm_pw_ptr
,
133 &nt_pw_len
, &nt_pw_ptr
,
147 pdb_set_logon_time(sampass
, logon_time
, PDB_SET
);
148 pdb_set_logoff_time(sampass
, logoff_time
, PDB_SET
);
149 pdb_set_kickoff_time(sampass
, kickoff_time
, PDB_SET
);
150 pdb_set_pass_can_change_time(sampass
, pass_can_change_time
, PDB_SET
);
151 pdb_set_pass_must_change_time(sampass
, pass_must_change_time
, PDB_SET
);
152 pdb_set_pass_last_set_time(sampass
, pass_last_set_time
, PDB_SET
);
154 pdb_set_username (sampass
, username
, PDB_SET
);
155 pdb_set_domain (sampass
, domain
, PDB_SET
);
156 pdb_set_nt_username (sampass
, nt_username
, PDB_SET
);
157 pdb_set_fullname (sampass
, fullname
, PDB_SET
);
160 pdb_set_homedir(sampass
, homedir
, PDB_SET
);
163 pdb_set_homedir(sampass
,
164 talloc_sub_specified(sampass
->mem_ctx
,
172 pdb_set_dir_drive(sampass
, dir_drive
, PDB_SET
);
174 pdb_set_dir_drive(sampass
,
175 talloc_sub_specified(sampass
->mem_ctx
,
183 pdb_set_logon_script(sampass
, logon_script
, PDB_SET
);
185 pdb_set_logon_script(sampass
,
186 talloc_sub_specified(sampass
->mem_ctx
,
194 pdb_set_profile_path(sampass
, profile_path
, PDB_SET
);
196 pdb_set_profile_path(sampass
,
197 talloc_sub_specified(sampass
->mem_ctx
,
204 pdb_set_acct_desc (sampass
, acct_desc
, PDB_SET
);
205 pdb_set_workstations (sampass
, workstations
, PDB_SET
);
206 pdb_set_munged_dial (sampass
, munged_dial
, PDB_SET
);
208 if (lm_pw_ptr
&& lm_pw_len
== LM_HASH_LEN
) {
209 if (!pdb_set_lanman_passwd(sampass
, lm_pw_ptr
, PDB_SET
)) {
215 if (nt_pw_ptr
&& nt_pw_len
== NT_HASH_LEN
) {
216 if (!pdb_set_nt_passwd(sampass
, nt_pw_ptr
, PDB_SET
)) {
222 pdb_set_user_sid_from_rid(sampass
, user_rid
, PDB_SET
);
223 pdb_set_group_sid_from_rid(sampass
, group_rid
, PDB_SET
);
224 pdb_set_unknown_3(sampass
, unknown_3
, PDB_SET
);
225 pdb_set_hours_len(sampass
, hours_len
, PDB_SET
);
226 pdb_set_unknown_5(sampass
, unknown_5
, PDB_SET
);
227 pdb_set_unknown_6(sampass
, unknown_6
, PDB_SET
);
228 pdb_set_acct_ctrl(sampass
, acct_ctrl
, PDB_SET
);
229 pdb_set_logon_divs(sampass
, logon_divs
, PDB_SET
);
230 pdb_set_hours(sampass
, hours
, PDB_SET
);
236 SAFE_FREE(nt_username
);
239 SAFE_FREE(dir_drive
);
240 SAFE_FREE(logon_script
);
241 SAFE_FREE(profile_path
);
242 SAFE_FREE(acct_desc
);
243 SAFE_FREE(workstations
);
244 SAFE_FREE(munged_dial
);
249 /**********************************************************************
250 Intialize a BYTE buffer from a SAM_ACCOUNT struct
251 *********************************************************************/
252 static uint32
init_buffer_from_sam (struct tdbsam_privates
*tdb_state
,
253 uint8
**buf
, const SAM_ACCOUNT
*sampass
)
257 /* times are stored as 32bit integer
258 take care on system with 64bit wide time_t
264 pass_can_change_time
,
265 pass_must_change_time
;
267 uint32 user_rid
, group_rid
;
269 const char *username
;
271 const char *nt_username
;
272 const char *dir_drive
;
273 const char *unknown_str
;
274 const char *munged_dial
;
275 const char *fullname
;
277 const char *logon_script
;
278 const char *profile_path
;
279 const char *acct_desc
;
280 const char *workstations
;
281 uint32 username_len
, domain_len
, nt_username_len
,
282 dir_drive_len
, unknown_str_len
, munged_dial_len
,
283 fullname_len
, homedir_len
, logon_script_len
,
284 profile_path_len
, acct_desc_len
, workstations_len
;
288 uint32 lm_pw_len
= 16;
289 uint32 nt_pw_len
= 16;
291 /* do we have a valid SAM_ACCOUNT pointer? */
292 if (sampass
== NULL
) {
293 DEBUG(0, ("init_buffer_from_sam: SAM_ACCOUNT is NULL!\n"));
300 logon_time
= (uint32
)pdb_get_logon_time(sampass
);
301 logoff_time
= (uint32
)pdb_get_logoff_time(sampass
);
302 kickoff_time
= (uint32
)pdb_get_kickoff_time(sampass
);
303 pass_can_change_time
= (uint32
)pdb_get_pass_can_change_time(sampass
);
304 pass_must_change_time
= (uint32
)pdb_get_pass_must_change_time(sampass
);
305 pass_last_set_time
= (uint32
)pdb_get_pass_last_set_time(sampass
);
307 user_rid
= pdb_get_user_rid(sampass
);
308 group_rid
= pdb_get_group_rid(sampass
);
310 username
= pdb_get_username(sampass
);
311 if (username
) username_len
= strlen(username
) +1;
312 else username_len
= 0;
314 domain
= pdb_get_domain(sampass
);
315 if (domain
) domain_len
= strlen(domain
) +1;
318 nt_username
= pdb_get_nt_username(sampass
);
319 if (nt_username
) nt_username_len
= strlen(nt_username
) +1;
320 else nt_username_len
= 0;
322 fullname
= pdb_get_fullname(sampass
);
323 if (fullname
) fullname_len
= strlen(fullname
) +1;
324 else fullname_len
= 0;
327 * Only updates fields which have been set (not defaults from smb.conf)
330 if (!IS_SAM_DEFAULT(sampass
, PDB_DRIVE
))
331 dir_drive
= pdb_get_dir_drive(sampass
);
332 else dir_drive
= NULL
;
333 if (dir_drive
) dir_drive_len
= strlen(dir_drive
) +1;
334 else dir_drive_len
= 0;
336 if (!IS_SAM_DEFAULT(sampass
, PDB_SMBHOME
)) homedir
= pdb_get_homedir(sampass
);
338 if (homedir
) homedir_len
= strlen(homedir
) +1;
339 else homedir_len
= 0;
341 if (!IS_SAM_DEFAULT(sampass
, PDB_LOGONSCRIPT
)) logon_script
= pdb_get_logon_script(sampass
);
342 else logon_script
= NULL
;
343 if (logon_script
) logon_script_len
= strlen(logon_script
) +1;
344 else logon_script_len
= 0;
346 if (!IS_SAM_DEFAULT(sampass
, PDB_PROFILE
)) profile_path
= pdb_get_profile_path(sampass
);
347 else profile_path
= NULL
;
348 if (profile_path
) profile_path_len
= strlen(profile_path
) +1;
349 else profile_path_len
= 0;
351 lm_pw
= pdb_get_lanman_passwd(sampass
);
352 if (!lm_pw
) lm_pw_len
= 0;
354 nt_pw
= pdb_get_nt_passwd(sampass
);
355 if (!nt_pw
) nt_pw_len
= 0;
357 acct_desc
= pdb_get_acct_desc(sampass
);
358 if (acct_desc
) acct_desc_len
= strlen(acct_desc
) +1;
359 else acct_desc_len
= 0;
361 workstations
= pdb_get_workstations(sampass
);
362 if (workstations
) workstations_len
= strlen(workstations
) +1;
363 else workstations_len
= 0;
368 munged_dial
= pdb_get_munged_dial(sampass
);
369 if (munged_dial
) munged_dial_len
= strlen(munged_dial
) +1;
370 else munged_dial_len
= 0;
372 /* one time to get the size needed */
373 len
= tdb_pack(NULL
, 0, TDB_FORMAT_STRING
,
378 pass_can_change_time
,
379 pass_must_change_time
,
380 username_len
, username
,
382 nt_username_len
, nt_username
,
383 fullname_len
, fullname
,
384 homedir_len
, homedir
,
385 dir_drive_len
, dir_drive
,
386 logon_script_len
, logon_script
,
387 profile_path_len
, profile_path
,
388 acct_desc_len
, acct_desc
,
389 workstations_len
, workstations
,
390 unknown_str_len
, unknown_str
,
391 munged_dial_len
, munged_dial
,
396 pdb_get_acct_ctrl(sampass
),
397 pdb_get_unknown_3(sampass
),
398 pdb_get_logon_divs(sampass
),
399 pdb_get_hours_len(sampass
),
400 MAX_HOURS_LEN
, pdb_get_hours(sampass
),
401 pdb_get_unknown_5(sampass
),
402 pdb_get_unknown_6(sampass
));
405 /* malloc the space needed */
406 if ( (*buf
=(uint8
*)malloc(len
)) == NULL
) {
407 DEBUG(0,("init_buffer_from_sam: Unable to malloc() memory for buffer!\n"));
411 /* now for the real call to tdb_pack() */
412 buflen
= tdb_pack(*buf
, len
, TDB_FORMAT_STRING
,
417 pass_can_change_time
,
418 pass_must_change_time
,
419 username_len
, username
,
421 nt_username_len
, nt_username
,
422 fullname_len
, fullname
,
423 homedir_len
, homedir
,
424 dir_drive_len
, dir_drive
,
425 logon_script_len
, logon_script
,
426 profile_path_len
, profile_path
,
427 acct_desc_len
, acct_desc
,
428 workstations_len
, workstations
,
429 unknown_str_len
, unknown_str
,
430 munged_dial_len
, munged_dial
,
435 pdb_get_acct_ctrl(sampass
),
436 pdb_get_unknown_3(sampass
),
437 pdb_get_logon_divs(sampass
),
438 pdb_get_hours_len(sampass
),
439 MAX_HOURS_LEN
, pdb_get_hours(sampass
),
440 pdb_get_unknown_5(sampass
),
441 pdb_get_unknown_6(sampass
));
444 /* check to make sure we got it correct */
446 DEBUG(0, ("init_buffer_from_sam: somthing odd is going on here: bufflen (%d) != len (%d) in tdb_pack operations!\n",
456 /***************************************************************
457 Open the TDB passwd database for SAM account enumeration.
458 ****************************************************************/
460 static NTSTATUS
tdbsam_setsampwent(struct pdb_methods
*my_methods
, BOOL update
)
462 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)my_methods
->private_data
;
464 /* Open tdb passwd */
465 if (!(tdb_state
->passwd_tdb
= tdb_open_log(tdb_state
->tdbsam_location
, 0, TDB_DEFAULT
, update
?(O_RDWR
|O_CREAT
):O_RDONLY
, 0600)))
467 DEBUG(0, ("Unable to open/create TDB passwd\n"));
468 return NT_STATUS_UNSUCCESSFUL
;
471 tdb_state
->key
= tdb_firstkey(tdb_state
->passwd_tdb
);
476 static void close_tdb(struct tdbsam_privates
*tdb_state
)
478 if (tdb_state
->passwd_tdb
) {
479 tdb_close(tdb_state
->passwd_tdb
);
480 tdb_state
->passwd_tdb
= NULL
;
484 /***************************************************************
485 End enumeration of the TDB passwd list.
486 ****************************************************************/
488 static void tdbsam_endsampwent(struct pdb_methods
*my_methods
)
490 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)my_methods
->private_data
;
491 close_tdb(tdb_state
);
493 DEBUG(7, ("endtdbpwent: closed sam database.\n"));
496 /*****************************************************************
497 Get one SAM_ACCOUNT from the TDB (next in line)
498 *****************************************************************/
500 static NTSTATUS
tdbsam_getsampwent(struct pdb_methods
*my_methods
, SAM_ACCOUNT
*user
)
502 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
503 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)my_methods
->private_data
;
505 const char *prefix
= USERPREFIX
;
506 int prefixlen
= strlen (prefix
);
510 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
514 /* skip all non-USER entries (eg. RIDs) */
515 while ((tdb_state
->key
.dsize
!= 0) && (strncmp(tdb_state
->key
.dptr
, prefix
, prefixlen
)))
516 /* increment to next in line */
517 tdb_state
->key
= tdb_nextkey(tdb_state
->passwd_tdb
, tdb_state
->key
);
519 /* do we have an valid iteration pointer? */
520 if(tdb_state
->passwd_tdb
== NULL
) {
521 DEBUG(0,("pdb_get_sampwent: Bad TDB Context pointer.\n"));
525 data
= tdb_fetch(tdb_state
->passwd_tdb
, tdb_state
->key
);
527 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
531 /* unpack the buffer */
532 if (!init_sam_from_buffer(tdb_state
, user
, data
.dptr
, data
.dsize
)) {
533 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
534 SAFE_FREE(data
.dptr
);
537 SAFE_FREE(data
.dptr
);
539 /* increment to next in line */
540 tdb_state
->key
= tdb_nextkey(tdb_state
->passwd_tdb
, tdb_state
->key
);
545 /******************************************************************
546 Lookup a name in the SAM TDB
547 ******************************************************************/
549 static NTSTATUS
tdbsam_getsampwnam (struct pdb_methods
*my_methods
, SAM_ACCOUNT
*user
, const char *sname
)
551 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
552 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)my_methods
->private_data
;
553 TDB_CONTEXT
*pwd_tdb
;
559 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
564 /* Data is stored in all lower-case */
565 fstrcpy(name
, sname
);
569 slprintf(keystr
, sizeof(keystr
)-1, "%s%s", USERPREFIX
, name
);
571 key
.dsize
= strlen(keystr
) + 1;
573 /* open the accounts TDB */
574 if (!(pwd_tdb
= tdb_open_log(tdb_state
->tdbsam_location
, 0, TDB_DEFAULT
, O_RDONLY
, 0600))) {
575 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd (%s)!\n", tdb_state
->tdbsam_location
));
580 data
= tdb_fetch(pwd_tdb
, key
);
582 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
583 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb
)));
584 DEBUGADD(5, (" Key: %s\n", keystr
));
589 /* unpack the buffer */
590 if (!init_sam_from_buffer(tdb_state
, user
, data
.dptr
, data
.dsize
)) {
591 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
592 SAFE_FREE(data
.dptr
);
596 SAFE_FREE(data
.dptr
);
598 /* no further use for database, close it now */
604 /***************************************************************************
606 **************************************************************************/
608 static NTSTATUS
tdbsam_getsampwrid (struct pdb_methods
*my_methods
, SAM_ACCOUNT
*user
, uint32 rid
)
610 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
611 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)my_methods
->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 (my_methods
, user
, name
);
650 static NTSTATUS
tdbsam_getsampwsid(struct pdb_methods
*my_methods
, SAM_ACCOUNT
* user
, const DOM_SID
*sid
)
653 if (!sid_peek_check_rid(get_global_sam_sid(), sid
, &rid
))
654 return NT_STATUS_UNSUCCESSFUL
;
655 return tdbsam_getsampwrid(my_methods
, user
, rid
);
658 /***************************************************************************
660 ****************************************************************************/
662 static NTSTATUS
tdbsam_delete_sam_account(struct pdb_methods
*my_methods
, SAM_ACCOUNT
*sam_pass
)
664 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
665 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)my_methods
->private_data
;
666 TDB_CONTEXT
*pwd_tdb
;
672 fstrcpy(name
, pdb_get_username(sam_pass
));
676 if (!(pwd_tdb
= tdb_open_log(tdb_state
->tdbsam_location
, 0, TDB_DEFAULT
, O_RDWR
, 0600))) {
677 DEBUG(0, ("Unable to open TDB passwd!"));
681 /* set the search key */
682 slprintf(keystr
, sizeof(keystr
)-1, "%s%s", USERPREFIX
, name
);
684 key
.dsize
= strlen (keystr
) + 1;
686 rid
= pdb_get_user_rid(sam_pass
);
688 /* it's outaa here! 8^) */
689 if (tdb_delete(pwd_tdb
, key
) != TDB_SUCCESS
) {
690 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
691 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb
)));
696 /* delete also the RID key */
698 /* set the search key */
699 slprintf(keystr
, sizeof(keystr
)-1, "%s%.8x", RIDPREFIX
, rid
);
701 key
.dsize
= strlen (keystr
) + 1;
703 /* it's outaa here! 8^) */
704 if (tdb_delete(pwd_tdb
, key
) != TDB_SUCCESS
) {
705 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
706 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb
)));
716 /***************************************************************************
718 ****************************************************************************/
720 static BOOL
tdb_update_sam(struct pdb_methods
*my_methods
, SAM_ACCOUNT
* newpwd
, int flag
)
722 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)my_methods
->private_data
;
723 TDB_CONTEXT
*pwd_tdb
= NULL
;
732 /* invalidate the existing TDB iterator if it is open */
733 if (tdb_state
->passwd_tdb
) {
734 tdb_close(tdb_state
->passwd_tdb
);
735 tdb_state
->passwd_tdb
= NULL
;
738 /* open the account TDB passwd*/
739 pwd_tdb
= tdb_open_log(tdb_state
->tdbsam_location
, 0, TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0600);
742 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd (%s)!\n", tdb_state
->tdbsam_location
));
746 if (!pdb_get_group_rid(newpwd
)) {
747 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a primary group RID\n",pdb_get_username(newpwd
)));
752 /* if flag == TDB_INSERT then make up a new RID else throw an error. */
753 if (!(user_rid
= pdb_get_user_rid(newpwd
))) {
754 if ((flag
& TDB_INSERT
) && tdb_state
->permit_non_unix_accounts
) {
755 uint32 lowrid
, highrid
;
756 if (!idmap_get_free_rid_range(&lowrid
, &highrid
)) {
757 /* should never happen */
758 DEBUG(0, ("tdbsam: something messed up, no high/low rids but nua enabled ?!\n"));
763 tdb_ret
= tdb_change_uint32_atomic(pwd_tdb
, "RID_COUNTER", &user_rid
, RID_MULTIPLIER
);
768 if (user_rid
> highrid
) {
769 DEBUG(0, ("tdbsam: no NUA rids available, cannot add user %s!\n", pdb_get_username(newpwd
)));
773 if (!pdb_set_user_sid_from_rid(newpwd
, user_rid
, PDB_CHANGED
)) {
774 DEBUG(0, ("tdbsam: not able to set new allocated user RID into sam account!\n"));
779 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a RID\n",pdb_get_username(newpwd
)));
785 /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
786 if ((data
.dsize
=init_buffer_from_sam (tdb_state
, &buf
, newpwd
)) == -1) {
787 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
793 fstrcpy(name
, pdb_get_username(newpwd
));
796 DEBUG(5, ("Storing %saccount %s with RID %d\n", flag
== TDB_INSERT
? "(new) " : "", name
, user_rid
));
798 /* setup the USER index key */
799 slprintf(keystr
, sizeof(keystr
)-1, "%s%s", USERPREFIX
, name
);
801 key
.dsize
= strlen(keystr
) + 1;
803 /* add the account */
804 if (tdb_store(pwd_tdb
, key
, data
, flag
) != TDB_SUCCESS
) {
805 DEBUG(0, ("Unable to modify passwd TDB!"));
806 DEBUGADD(0, (" Error: %s", tdb_errorstr(pwd_tdb
)));
807 DEBUGADD(0, (" occured while storing the main record (%s)\n", keystr
));
813 data
.dsize
= strlen(name
) + 1;
816 /* setup the RID index key */
817 slprintf(keystr
, sizeof(keystr
)-1, "%s%.8x", RIDPREFIX
, user_rid
);
819 key
.dsize
= strlen (keystr
) + 1;
821 /* add the reference */
822 if (tdb_store(pwd_tdb
, key
, data
, flag
) != TDB_SUCCESS
) {
823 DEBUG(0, ("Unable to modify TDB passwd !"));
824 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb
)));
825 DEBUGADD(0, (" occured while storing the RID index (%s)\n", keystr
));
839 /***************************************************************************
840 Allocates a new RID and returns it to the caller as a domain sid
842 NOTE: Use carefullt, do not waste RIDs they are a limited resource!
844 ***************************************************************************/
846 static NTSTATUS
tdbsam_get_next_sid (struct pdb_methods
*my_methods
, DOM_SID
*sid
)
848 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
849 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)my_methods
->private_data
;
850 TDB_CONTEXT
*pwd_tdb
;
854 return NT_STATUS_INVALID_PARAMETER
;
857 pwd_tdb
= tdb_open_log(tdb_state
->tdbsam_location
, 0, TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0600);
860 DEBUG(0, ("tdbsam_get_next_sid: Unable to open TDB passwd (%s)!\n", tdb_state
->tdbsam_location
));
861 return NT_STATUS_UNSUCCESSFUL
;
865 if (tdb_change_uint32_atomic(pwd_tdb
, "RID_COUNTER", &rid
, 1)) {
867 sid_copy(sid
, get_global_sam_sid());
868 if (!sid_append_rid(sid
, rid
)) {
881 /***************************************************************************
882 Modifies an existing SAM_ACCOUNT
883 ****************************************************************************/
885 static NTSTATUS
tdbsam_update_sam_account (struct pdb_methods
*my_methods
, SAM_ACCOUNT
*newpwd
)
887 if (tdb_update_sam(my_methods
, newpwd
, TDB_MODIFY
))
890 return NT_STATUS_UNSUCCESSFUL
;
893 /***************************************************************************
894 Adds an existing SAM_ACCOUNT
895 ****************************************************************************/
897 static NTSTATUS
tdbsam_add_sam_account (struct pdb_methods
*my_methods
, SAM_ACCOUNT
*newpwd
)
899 if (tdb_update_sam(my_methods
, newpwd
, TDB_INSERT
))
902 return NT_STATUS_UNSUCCESSFUL
;
905 static void free_private_data(void **vp
)
907 struct tdbsam_privates
**tdb_state
= (struct tdbsam_privates
**)vp
;
908 close_tdb(*tdb_state
);
911 /* No need to free any further, as it is talloc()ed */
915 static NTSTATUS
pdb_init_tdbsam(PDB_CONTEXT
*pdb_context
, PDB_METHODS
**pdb_method
, const char *location
)
918 struct tdbsam_privates
*tdb_state
;
919 uid_t low_nua_uid
, high_nua_uid
;
921 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_methods(pdb_context
->mem_ctx
, pdb_method
))) {
925 (*pdb_method
)->name
= "tdbsam";
927 (*pdb_method
)->setsampwent
= tdbsam_setsampwent
;
928 (*pdb_method
)->endsampwent
= tdbsam_endsampwent
;
929 (*pdb_method
)->getsampwent
= tdbsam_getsampwent
;
930 (*pdb_method
)->getsampwnam
= tdbsam_getsampwnam
;
931 (*pdb_method
)->getsampwsid
= tdbsam_getsampwsid
;
932 (*pdb_method
)->add_sam_account
= tdbsam_add_sam_account
;
933 (*pdb_method
)->update_sam_account
= tdbsam_update_sam_account
;
934 (*pdb_method
)->delete_sam_account
= tdbsam_delete_sam_account
;
936 tdb_state
= talloc_zero(pdb_context
->mem_ctx
, sizeof(struct tdbsam_privates
));
939 DEBUG(0, ("talloc() failed for tdbsam private_data!\n"));
940 return NT_STATUS_NO_MEMORY
;
944 tdb_state
->tdbsam_location
= talloc_strdup(pdb_context
->mem_ctx
, location
);
947 get_private_directory(tdbfile
);
948 pstrcat(tdbfile
, "/");
949 pstrcat(tdbfile
, PASSDB_FILE_NAME
);
950 tdb_state
->tdbsam_location
= talloc_strdup(pdb_context
->mem_ctx
, tdbfile
);
953 (*pdb_method
)->private_data
= tdb_state
;
955 (*pdb_method
)->free_private_data
= free_private_data
;
957 if (lp_idmap_uid(&low_nua_uid
, &high_nua_uid
)) {
958 DEBUG(3, ("idmap uid range defined, non unix accounts enabled\n"));
960 tdb_state
->permit_non_unix_accounts
= True
;
962 tdb_state
->low_nua_rid
=fallback_pdb_uid_to_user_rid(low_nua_uid
);
964 tdb_state
->high_nua_rid
=fallback_pdb_uid_to_user_rid(high_nua_uid
);
967 tdb_state
->algorithmic_rids
= True
;
973 NTSTATUS
pdb_tdbsam_init(void)
975 return smb_register_passdb(PASSDB_INTERFACE_VERSION
, "tdbsam", pdb_init_tdbsam
);