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
;
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
/* uid, gid,*/ 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
;
90 gid_t gid
= -1; /* This is what standard sub advanced expects if no gid is known */
92 if(sampass
== NULL
|| buf
== NULL
) {
93 DEBUG(0, ("init_sam_from_buffer: NULL parameters found!\n"));
97 /* unpack the buffer into variables */
98 len
= tdb_unpack (buf
, buflen
, TDB_FORMAT_STRING
,
103 &pass_can_change_time
,
104 &pass_must_change_time
,
105 &username_len
, &username
,
106 &domain_len
, &domain
,
107 &nt_username_len
, &nt_username
,
108 &fullname_len
, &fullname
,
109 &homedir_len
, &homedir
,
110 &dir_drive_len
, &dir_drive
,
111 &logon_script_len
, &logon_script
,
112 &profile_path_len
, &profile_path
,
113 &acct_desc_len
, &acct_desc
,
114 &workstations_len
, &workstations
,
115 &unknown_str_len
, &unknown_str
,
116 &munged_dial_len
, &munged_dial
,
119 &lmpwlen
, &lm_pw_ptr
,
120 &ntpwlen
, &nt_pw_ptr
,
134 if ((tdb_state
->permit_non_unix_accounts
)
135 && (user_rid
>= tdb_state
->low_nua_rid
)
136 && (user_rid
<= tdb_state
->high_nua_rid
)) {
141 /* validate the account and fill in UNIX uid and gid. Standard
142 * getpwnam() is used instead of Get_Pwnam() as we do not need
143 * to try case permutations
145 if (!username
|| !(pw
=getpwnam_alloc(username
))) {
146 DEBUG(0,("tdbsam: getpwnam_alloc(%s) return NULL. User does not exist!\n",
147 username
?username
:"NULL"));
156 pdb_set_uid(sampass
, uid
);
157 pdb_set_gid(sampass
, gid
);
160 pdb_set_logon_time(sampass
, logon_time
, True
);
161 pdb_set_logoff_time(sampass
, logoff_time
, True
);
162 pdb_set_kickoff_time(sampass
, kickoff_time
, True
);
163 pdb_set_pass_can_change_time(sampass
, pass_can_change_time
, True
);
164 pdb_set_pass_must_change_time(sampass
, pass_must_change_time
, True
);
165 pdb_set_pass_last_set_time(sampass
, pass_last_set_time
);
167 pdb_set_username (sampass
, username
);
168 pdb_set_domain (sampass
, domain
);
169 pdb_set_nt_username (sampass
, nt_username
);
170 pdb_set_fullname (sampass
, fullname
);
172 if (homedir
) setflag
= True
;
175 homedir
= strdup(lp_logon_home());
176 if(!homedir
) { ret
= False
; goto done
; }
177 standard_sub_advanced(-1, username
, "", gid
, username
, homedir
);
178 DEBUG(5,("Home directory set back to %s\n", homedir
));
180 pdb_set_homedir(sampass
, homedir
, setflag
);
182 if (dir_drive
) setflag
= True
;
185 dir_drive
= strdup(lp_logon_drive());
186 if(!dir_drive
) { ret
= False
; goto done
; }
187 standard_sub_advanced(-1, username
, "", gid
, username
, dir_drive
);
188 DEBUG(5,("Home directory set back to %s\n", dir_drive
));
190 pdb_set_dir_drive(sampass
, dir_drive
, setflag
);
192 if (logon_script
) setflag
= True
;
195 logon_script
= strdup(lp_logon_script());
196 if(!logon_script
) { ret
= False
; goto done
; }
197 standard_sub_advanced(-1, username
, "", gid
, username
, logon_script
);
198 DEBUG(5,("Home directory set back to %s\n", logon_script
));
200 pdb_set_logon_script(sampass
, logon_script
, setflag
);
202 if (profile_path
) setflag
= True
;
205 profile_path
= strdup(lp_logon_path());
206 if(!profile_path
) { ret
= False
; goto done
; }
207 standard_sub_advanced(-1, username
, "", gid
, username
, profile_path
);
208 DEBUG(5,("Home directory set back to %s\n", profile_path
));
210 pdb_set_profile_path(sampass
, profile_path
, setflag
);
212 pdb_set_acct_desc (sampass
, acct_desc
);
213 pdb_set_workstations (sampass
, workstations
);
214 pdb_set_munged_dial (sampass
, munged_dial
);
215 if (!pdb_set_lanman_passwd(sampass
, lm_pw_ptr
)) {
219 if (!pdb_set_nt_passwd(sampass
, nt_pw_ptr
)) {
224 /*pdb_set_uid(sampass, uid);
225 pdb_set_gid(sampass, gid);*/
226 pdb_set_user_rid(sampass
, user_rid
);
227 pdb_set_group_rid(sampass
, group_rid
);
228 pdb_set_unknown_3(sampass
, unknown_3
);
229 pdb_set_hours_len(sampass
, hours_len
);
230 pdb_set_unknown_5(sampass
, unknown_5
);
231 pdb_set_unknown_6(sampass
, unknown_6
);
232 pdb_set_acct_ctrl(sampass
, acct_ctrl
);
233 pdb_set_logon_divs(sampass
, logon_divs
);
234 pdb_set_hours(sampass
, hours
);
240 SAFE_FREE(nt_username
);
243 SAFE_FREE(dir_drive
);
244 SAFE_FREE(logon_script
);
245 SAFE_FREE(profile_path
);
246 SAFE_FREE(acct_desc
);
247 SAFE_FREE(workstations
);
248 SAFE_FREE(munged_dial
);
253 /**********************************************************************
254 Intialize a BYTE buffer from a SAM_ACCOUNT struct
255 *********************************************************************/
256 static uint32
init_buffer_from_sam (struct tdbsam_privates
*tdb_state
, uint8
**buf
,
257 const SAM_ACCOUNT
*sampass
, uint32 user_rid
, uint32 group_rid
)
261 /* times are stored as 32bit integer
262 take care on system with 64bit wide time_t
268 pass_can_change_time
,
269 pass_must_change_time
;
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
);
309 username
= pdb_get_username(sampass
);
310 if (username
) username_len
= strlen(username
) +1;
311 else username_len
= 0;
313 domain
= pdb_get_domain(sampass
);
314 if (domain
) domain_len
= strlen(domain
) +1;
317 nt_username
= pdb_get_nt_username(sampass
);
318 if (nt_username
) nt_username_len
= strlen(nt_username
) +1;
319 else nt_username_len
= 0;
321 fullname
= pdb_get_fullname(sampass
);
322 if (fullname
) fullname_len
= strlen(fullname
) +1;
323 else fullname_len
= 0;
326 * Only updates fields which have been set (not defaults from smb.conf)
329 if (IS_SAM_SET(sampass
, FLAG_SAM_DRIVE
)) dir_drive
= pdb_get_dirdrive(sampass
);
330 else dir_drive
= NULL
;
331 if (dir_drive
) dir_drive_len
= strlen(dir_drive
) +1;
332 else dir_drive_len
= 0;
334 if (IS_SAM_SET(sampass
, FLAG_SAM_SMBHOME
)) homedir
= pdb_get_homedir(sampass
);
336 if (homedir
) homedir_len
= strlen(homedir
) +1;
337 else homedir_len
= 0;
339 if (IS_SAM_SET(sampass
, FLAG_SAM_LOGONSCRIPT
)) logon_script
= pdb_get_logon_script(sampass
);
340 else logon_script
= NULL
;
341 if (logon_script
) logon_script_len
= strlen(logon_script
) +1;
342 else logon_script_len
= 0;
344 if (IS_SAM_SET(sampass
, FLAG_SAM_PROFILE
)) profile_path
= pdb_get_profile_path(sampass
);
345 else profile_path
= NULL
;
346 if (profile_path
) profile_path_len
= strlen(profile_path
) +1;
347 else profile_path_len
= 0;
349 lm_pw
= pdb_get_lanman_passwd(sampass
);
350 if (!lm_pw
) lm_pw_len
= 0;
352 nt_pw
= pdb_get_nt_passwd(sampass
);
353 if (!nt_pw
) nt_pw_len
= 0;
355 acct_desc
= pdb_get_acct_desc(sampass
);
356 if (acct_desc
) acct_desc_len
= strlen(acct_desc
) +1;
357 else acct_desc_len
= 0;
359 workstations
= pdb_get_workstations(sampass
);
360 if (workstations
) workstations_len
= strlen(workstations
) +1;
361 else workstations_len
= 0;
366 munged_dial
= pdb_get_munged_dial(sampass
);
367 if (munged_dial
) munged_dial_len
= strlen(munged_dial
) +1;
368 else munged_dial_len
= 0;
370 /* one time to get the size needed */
371 len
= tdb_pack(NULL
, 0, TDB_FORMAT_STRING
,
376 pass_can_change_time
,
377 pass_must_change_time
,
378 username_len
, username
,
380 nt_username_len
, nt_username
,
381 fullname_len
, fullname
,
382 homedir_len
, homedir
,
383 dir_drive_len
, dir_drive
,
384 logon_script_len
, logon_script
,
385 profile_path_len
, profile_path
,
386 acct_desc_len
, acct_desc
,
387 workstations_len
, workstations
,
388 unknown_str_len
, unknown_str
,
389 munged_dial_len
, munged_dial
,
394 pdb_get_acct_ctrl(sampass
),
395 pdb_get_unknown3(sampass
),
396 pdb_get_logon_divs(sampass
),
397 pdb_get_hours_len(sampass
),
398 MAX_HOURS_LEN
, pdb_get_hours(sampass
),
399 pdb_get_unknown5(sampass
),
400 pdb_get_unknown6(sampass
));
403 /* malloc the space needed */
404 if ( (*buf
=(uint8
*)malloc(len
)) == NULL
) {
405 DEBUG(0,("init_buffer_from_sam: Unable to malloc() memory for buffer!\n"));
409 /* now for the real call to tdb_pack() */
410 buflen
= tdb_pack(*buf
, len
, TDB_FORMAT_STRING
,
415 pass_can_change_time
,
416 pass_must_change_time
,
417 username_len
, username
,
419 nt_username_len
, nt_username
,
420 fullname_len
, fullname
,
421 homedir_len
, homedir
,
422 dir_drive_len
, dir_drive
,
423 logon_script_len
, logon_script
,
424 profile_path_len
, profile_path
,
425 acct_desc_len
, acct_desc
,
426 workstations_len
, workstations
,
427 unknown_str_len
, unknown_str
,
428 munged_dial_len
, munged_dial
,
433 pdb_get_acct_ctrl(sampass
),
434 pdb_get_unknown3(sampass
),
435 pdb_get_logon_divs(sampass
),
436 pdb_get_hours_len(sampass
),
437 MAX_HOURS_LEN
, pdb_get_hours(sampass
),
438 pdb_get_unknown5(sampass
),
439 pdb_get_unknown6(sampass
));
442 /* check to make sure we got it correct */
444 DEBUG(0, ("init_buffer_from_sam: somthing odd is going on here: bufflen (%d) != len (%d) in tdb_pack operations!\n",
454 /***************************************************************
455 Open the TDB passwd database for SAM account enumeration.
456 ****************************************************************/
458 static BOOL
tdbsam_setsampwent(struct pdb_context
*context
, BOOL update
)
460 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)context
->pdb_selected
->private_data
;
462 /* Open tdb passwd */
463 if (!(tdb_state
->passwd_tdb
= tdb_open_log(tdb_state
->tdbsam_location
, 0, TDB_DEFAULT
, update
?(O_RDWR
|O_CREAT
):O_RDONLY
, 0600)))
465 DEBUG(0, ("Unable to open/create TDB passwd\n"));
469 tdb_state
->key
= tdb_firstkey(tdb_state
->passwd_tdb
);
474 static void close_tdb(struct tdbsam_privates
*tdb_state
)
476 if (tdb_state
->passwd_tdb
) {
477 tdb_close(tdb_state
->passwd_tdb
);
478 tdb_state
->passwd_tdb
= NULL
;
482 /***************************************************************
483 End enumeration of the TDB passwd list.
484 ****************************************************************/
486 static void tdbsam_endsampwent(struct pdb_context
*context
)
488 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)context
->pdb_selected
->private_data
;
489 close_tdb(tdb_state
);
491 DEBUG(7, ("endtdbpwent: closed sam database.\n"));
494 /*****************************************************************
495 Get one SAM_ACCOUNT from the TDB (next in line)
496 *****************************************************************/
498 static BOOL
tdbsam_getsampwent(struct pdb_context
*context
, SAM_ACCOUNT
*user
)
500 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)context
->pdb_selected
->private_data
;
502 char *prefix
= USERPREFIX
;
503 int prefixlen
= strlen (prefix
);
507 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
511 /* skip all non-USER entries (eg. RIDs) */
512 while ((tdb_state
->key
.dsize
!= 0) && (strncmp(tdb_state
->key
.dptr
, prefix
, prefixlen
)))
513 /* increment to next in line */
514 tdb_state
->key
= tdb_nextkey(tdb_state
->passwd_tdb
, tdb_state
->key
);
516 /* do we have an valid interation pointer? */
517 if(tdb_state
->passwd_tdb
== NULL
) {
518 DEBUG(0,("pdb_get_sampwent: Bad TDB Context pointer.\n"));
522 data
= tdb_fetch(tdb_state
->passwd_tdb
, tdb_state
->key
);
524 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
528 /* unpack the buffer */
529 if (!init_sam_from_buffer(tdb_state
, user
, data
.dptr
, data
.dsize
)) {
530 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
531 SAFE_FREE(data
.dptr
);
534 SAFE_FREE(data
.dptr
);
536 /* increment to next in line */
537 tdb_state
->key
= tdb_nextkey(tdb_state
->passwd_tdb
, tdb_state
->key
);
542 /******************************************************************
543 Lookup a name in the SAM TDB
544 ******************************************************************/
546 static BOOL
tdbsam_getsampwnam (struct pdb_context
*context
, SAM_ACCOUNT
*user
, const char *sname
)
548 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)context
->pdb_selected
->private_data
;
549 TDB_CONTEXT
*pwd_tdb
;
555 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
559 /* Data is stored in all lower-case */
560 unix_strlower(sname
, -1, name
, sizeof(name
));
563 slprintf(keystr
, sizeof(keystr
)-1, "%s%s", USERPREFIX
, name
);
565 key
.dsize
= strlen(keystr
) + 1;
567 /* open the accounts TDB */
568 if (!(pwd_tdb
= tdb_open_log(tdb_state
->tdbsam_location
, 0, TDB_DEFAULT
, O_RDONLY
, 0600))) {
569 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd (%s)!\n", tdb_state
->tdbsam_location
));
574 data
= tdb_fetch(pwd_tdb
, key
);
576 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
577 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb
)));
578 DEBUGADD(5, (" Key: %s\n", keystr
));
583 /* unpack the buffer */
584 if (!init_sam_from_buffer(tdb_state
, user
, data
.dptr
, data
.dsize
)) {
585 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
586 SAFE_FREE(data
.dptr
);
590 SAFE_FREE(data
.dptr
);
592 /* no further use for database, close it now */
598 /***************************************************************************
600 **************************************************************************/
602 static BOOL
tdbsam_getsampwrid (struct pdb_context
*context
, SAM_ACCOUNT
*user
, uint32 rid
)
604 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)context
->pdb_selected
->private_data
;
605 TDB_CONTEXT
*pwd_tdb
;
611 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
616 slprintf(keystr
, sizeof(keystr
)-1, "%s%.8x", RIDPREFIX
, rid
);
618 key
.dsize
= strlen (keystr
) + 1;
620 /* open the accounts TDB */
621 if (!(pwd_tdb
= tdb_open_log(tdb_state
->tdbsam_location
, 0, TDB_DEFAULT
, O_RDONLY
, 0600))) {
622 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
627 data
= tdb_fetch (pwd_tdb
, key
);
629 DEBUG(5,("pdb_getsampwrid (TDB): error looking up RID %d by key %s.\n", rid
, keystr
));
630 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb
)));
635 fstrcpy (name
, data
.dptr
);
636 SAFE_FREE(data
.dptr
);
640 return tdbsam_getsampwnam (context
, user
, name
);
643 /***************************************************************************
645 ****************************************************************************/
647 static BOOL
tdbsam_delete_sam_account(struct pdb_context
*context
, const SAM_ACCOUNT
*sam_pass
)
649 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)context
->pdb_selected
->private_data
;
650 TDB_CONTEXT
*pwd_tdb
;
656 unix_strlower(pdb_get_username(sam_pass
), -1, name
, sizeof(name
));
659 if (!(pwd_tdb
= tdb_open_log(tdb_state
->tdbsam_location
, 0, TDB_DEFAULT
, O_RDWR
, 0600))) {
660 DEBUG(0, ("Unable to open TDB passwd!"));
664 /* set the search key */
665 slprintf(keystr
, sizeof(keystr
)-1, "%s%s", USERPREFIX
, name
);
667 key
.dsize
= strlen (keystr
) + 1;
669 rid
= pdb_get_user_rid(sam_pass
);
671 /* it's outaa here! 8^) */
672 if (tdb_delete(pwd_tdb
, key
) != TDB_SUCCESS
) {
673 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
674 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb
)));
679 /* delete also the RID key */
681 /* set the search key */
682 slprintf(keystr
, sizeof(keystr
)-1, "%s%.8x", RIDPREFIX
, rid
);
684 key
.dsize
= strlen (keystr
) + 1;
686 /* it's outaa here! 8^) */
687 if (tdb_delete(pwd_tdb
, key
) != TDB_SUCCESS
) {
688 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
689 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb
)));
699 /***************************************************************************
701 ****************************************************************************/
703 static BOOL
tdb_update_sam(struct pdb_context
*context
, const SAM_ACCOUNT
* newpwd
, int flag
)
705 struct tdbsam_privates
*tdb_state
= (struct tdbsam_privates
*)context
->pdb_selected
->private_data
;
706 TDB_CONTEXT
*pwd_tdb
= NULL
;
716 /* invalidate the existing TDB iterator if it is open */
717 if (tdb_state
->passwd_tdb
) {
718 tdb_close(tdb_state
->passwd_tdb
);
719 tdb_state
->passwd_tdb
= NULL
;
722 /* open the account TDB passwd*/
723 pwd_tdb
= tdb_open_log(tdb_state
->tdbsam_location
, 0, TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0600);
726 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd (%s)!\n", tdb_state
->tdbsam_location
));
730 /* if we don't have a RID, then make them up. */
731 if (!(user_rid
= pdb_get_user_rid(newpwd
))) {
732 if (!tdb_state
->permit_non_unix_accounts
) {
733 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a RID\n",pdb_get_username(newpwd
)));
737 user_rid
= tdb_state
->low_nua_rid
;
738 tdb_ret
= tdb_change_int32_atomic(pwd_tdb
, "NUA_NEXT_RID", &user_rid
, RID_MULTIPLIER
);
746 if (!(group_rid
= pdb_get_group_rid(newpwd
))) {
747 if (!tdb_state
->permit_non_unix_accounts
) {
748 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a primary group RID\n",pdb_get_username(newpwd
)));
752 /* This seems like a good default choice for non-unix users */
753 group_rid
= DOMAIN_GROUP_RID_USERS
;
757 /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
758 if ((data
.dsize
=init_buffer_from_sam (tdb_state
, &buf
, newpwd
, user_rid
, group_rid
)) == -1) {
759 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
765 unix_strlower(pdb_get_username(newpwd
), -1, name
, sizeof(name
));
767 DEBUG(5, ("Storing %saccount %s with RID %d\n", flag
== TDB_INSERT
? "(new) " : "", name
, user_rid
));
769 /* setup the USER index key */
770 slprintf(keystr
, sizeof(keystr
)-1, "%s%s", USERPREFIX
, name
);
772 key
.dsize
= strlen (keystr
) + 1;
774 /* add the account */
775 if (tdb_store(pwd_tdb
, key
, data
, flag
) != TDB_SUCCESS
) {
776 DEBUG(0, ("Unable to modify passwd TDB!"));
777 DEBUGADD(0, (" Error: %s", tdb_errorstr(pwd_tdb
)));
778 DEBUGADD(0, (" occured while storing the main record (%s)\n", keystr
));
784 data
.dsize
= sizeof(fstring
);
787 /* setup the RID index key */
788 slprintf(keystr
, sizeof(keystr
)-1, "%s%.8x", RIDPREFIX
, user_rid
);
790 key
.dsize
= strlen (keystr
) + 1;
792 /* add the reference */
793 if (tdb_store(pwd_tdb
, key
, data
, flag
) != TDB_SUCCESS
) {
794 DEBUG(0, ("Unable to modify TDB passwd !"));
795 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb
)));
796 DEBUGADD(0, (" occured while storing the RID index (%s)\n", keystr
));
809 /***************************************************************************
810 Modifies an existing SAM_ACCOUNT
811 ****************************************************************************/
813 static BOOL
tdbsam_update_sam_account (struct pdb_context
*context
, const SAM_ACCOUNT
*newpwd
)
815 return (tdb_update_sam(context
, newpwd
, TDB_MODIFY
));
818 /***************************************************************************
819 Adds an existing SAM_ACCOUNT
820 ****************************************************************************/
822 static BOOL
tdbsam_add_sam_account (struct pdb_context
*context
, const SAM_ACCOUNT
*newpwd
)
824 return (tdb_update_sam(context
, newpwd
, TDB_INSERT
));
827 static void free_private_data(void **vp
)
829 struct tdbsam_privates
**tdb_state
= (struct tdbsam_privates
**)vp
;
830 close_tdb(*tdb_state
);
833 /* No need to free any further, as it is talloc()ed */
837 NTSTATUS
pdb_init_tdbsam(PDB_CONTEXT
*pdb_context
, PDB_METHODS
**pdb_method
, const char *location
)
840 struct tdbsam_privates
*tdb_state
;
842 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_methods(pdb_context
->mem_ctx
, pdb_method
))) {
846 (*pdb_method
)->name
= "tdbsam";
848 (*pdb_method
)->setsampwent
= tdbsam_setsampwent
;
849 (*pdb_method
)->endsampwent
= tdbsam_endsampwent
;
850 (*pdb_method
)->getsampwent
= tdbsam_getsampwent
;
851 (*pdb_method
)->getsampwnam
= tdbsam_getsampwnam
;
852 (*pdb_method
)->getsampwrid
= tdbsam_getsampwrid
;
853 (*pdb_method
)->add_sam_account
= tdbsam_add_sam_account
;
854 (*pdb_method
)->update_sam_account
= tdbsam_update_sam_account
;
855 (*pdb_method
)->delete_sam_account
= tdbsam_delete_sam_account
;
857 tdb_state
= talloc_zero(pdb_context
->mem_ctx
, sizeof(struct tdbsam_privates
));
860 DEBUG(0, ("talloc() failed for tdbsam private_data!\n"));
861 return NT_STATUS_NO_MEMORY
;
865 tdb_state
->tdbsam_location
= talloc_strdup(pdb_context
->mem_ctx
, location
);
868 get_private_directory(tdbfile
);
869 pstrcat(tdbfile
, "/");
870 pstrcat(tdbfile
, PASSDB_FILE_NAME
);
871 tdb_state
->tdbsam_location
= talloc_strdup(pdb_context
->mem_ctx
, tdbfile
);
874 (*pdb_method
)->private_data
= tdb_state
;
876 (*pdb_method
)->free_private_data
= free_private_data
;
881 NTSTATUS
pdb_init_tdbsam_nua(PDB_CONTEXT
*pdb_context
, PDB_METHODS
**pdb_method
, const char *location
)
884 struct tdbsam_privates
*tdb_state
;
885 uint32 low_nua_uid
, high_nua_uid
;
887 if (!NT_STATUS_IS_OK(nt_status
= pdb_init_tdbsam(pdb_context
, pdb_method
, location
))) {
891 (*pdb_method
)->name
= "tdbsam_nua";
893 tdb_state
= (*pdb_method
)->private_data
;
895 tdb_state
->permit_non_unix_accounts
= True
;
897 if (!lp_non_unix_account_range(&low_nua_uid
, &high_nua_uid
)) {
898 DEBUG(0, ("cannot use tdbsam_nua without 'non unix account range' in smb.conf!\n"));
899 return NT_STATUS_UNSUCCESSFUL
;
902 tdb_state
->low_nua_rid
=pdb_uid_to_user_rid(low_nua_uid
);
904 tdb_state
->high_nua_rid
=pdb_uid_to_user_rid(high_nua_uid
);
912 NTSTATUS
pdb_init_tdbsam(PDB_CONTEXT
*pdb_context
, PDB_METHODS
**pdb_method
, const char *location
)
914 DEBUG(0, ("tdbsam not compiled in!\n"));
915 return NT_STATUS_UNSUCCESSFUL
;
918 NTSTATUS
pdb_init_tdbsam_nua(PDB_CONTEXT
*pdb_context
, PDB_METHODS
**pdb_method
, const char *location
)
920 DEBUG(0, ("tdbsam_nua not compiled in!\n"));
921 return NT_STATUS_UNSUCCESSFUL
;