correctly initiazlize idmap tdb when creationg new
[Samba/ekacnet.git] / source3 / passdb / pdb_tdb.c
blob3ed5d2d4d683bfe8aacf126f66c443ed6887daff
1 /*
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
9 *
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)
13 * any later version.
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
18 * more details.
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.
25 #include "includes.h"
27 #if 0 /* when made a module use this */
29 static int tdbsam_debug_level = DBGC_ALL;
30 #undef DBGC_CLASS
31 #define DBGC_CLASS tdbsam_debug_level
33 #else
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_PASSDB
38 #endif
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;
48 TDB_DATA key;
50 /* retrive-once info */
51 const char *tdbsam_location;
53 BOOL permit_non_unix_accounts;
55 BOOL algorithmic_rids;
57 uint32 low_nua_rid;
58 uint32 high_nua_rid;
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
71 --SSS */
72 uint32 logon_time,
73 logoff_time,
74 kickoff_time,
75 pass_last_set_time,
76 pass_can_change_time,
77 pass_must_change_time;
78 char *username;
79 char *domain;
80 char *nt_username;
81 char *dir_drive;
82 char *unknown_str;
83 char *munged_dial;
84 char *fullname;
85 char *homedir;
86 char *logon_script;
87 char *profile_path;
88 char *acct_desc;
89 char *workstations;
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;
97 uint8 *hours;
98 static uint8 *lm_pw_ptr, *nt_pw_ptr;
99 uint32 len = 0;
100 uint32 lm_pw_len, nt_pw_len, hourslen;
101 BOOL ret = True;
102 struct passwd *pw;
103 uid_t uid = -1;
104 gid_t gid = -1;
106 if(sampass == NULL || buf == NULL) {
107 DEBUG(0, ("init_sam_from_buffer: NULL parameters found!\n"));
108 return False;
111 /* unpack the buffer into variables */
112 len = tdb_unpack (buf, buflen, TDB_FORMAT_STRING,
113 &logon_time,
114 &logoff_time,
115 &kickoff_time,
116 &pass_last_set_time,
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,
131 &user_rid,
132 &group_rid,
133 &lm_pw_len, &lm_pw_ptr,
134 &nt_pw_len, &nt_pw_ptr,
135 &acct_ctrl,
136 &unknown_3,
137 &logon_divs,
138 &hours_len,
139 &hourslen, &hours,
140 &unknown_5,
141 &unknown_6);
143 if (len == -1) {
144 ret = False;
145 goto done;
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);
160 if (homedir) {
161 pdb_set_homedir(sampass, homedir, PDB_SET);
163 else {
164 pdb_set_homedir(sampass,
165 talloc_sub_specified(sampass->mem_ctx,
166 lp_logon_home(),
167 username, domain,
168 uid, gid),
169 PDB_DEFAULT);
172 if (dir_drive)
173 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
174 else {
175 pdb_set_dir_drive(sampass,
176 talloc_sub_specified(sampass->mem_ctx,
177 lp_logon_drive(),
178 username, domain,
179 uid, gid),
180 PDB_DEFAULT);
183 if (logon_script)
184 pdb_set_logon_script(sampass, logon_script, PDB_SET);
185 else {
186 pdb_set_logon_script(sampass,
187 talloc_sub_specified(sampass->mem_ctx,
188 lp_logon_script(),
189 username, domain,
190 uid, gid),
191 PDB_DEFAULT);
194 if (profile_path) {
195 pdb_set_profile_path(sampass, profile_path, PDB_SET);
196 } else {
197 pdb_set_profile_path(sampass,
198 talloc_sub_specified(sampass->mem_ctx,
199 lp_logon_path(),
200 username, domain,
201 uid, gid),
202 PDB_DEFAULT);
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)) {
211 ret = False;
212 goto done;
216 if (nt_pw_ptr && nt_pw_len == NT_HASH_LEN) {
217 if (!pdb_set_nt_passwd(sampass, nt_pw_ptr, PDB_SET)) {
218 ret = False;
219 goto done;
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);
233 done:
235 SAFE_FREE(username);
236 SAFE_FREE(domain);
237 SAFE_FREE(nt_username);
238 SAFE_FREE(fullname);
239 SAFE_FREE(homedir);
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);
247 return ret;
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)
256 size_t len, buflen;
258 /* times are stored as 32bit integer
259 take care on system with 64bit wide time_t
260 --SSS */
261 uint32 logon_time,
262 logoff_time,
263 kickoff_time,
264 pass_last_set_time,
265 pass_can_change_time,
266 pass_must_change_time;
268 uint32 user_rid, group_rid;
270 const char *username;
271 const char *domain;
272 const char *nt_username;
273 const char *dir_drive;
274 const char *unknown_str;
275 const char *munged_dial;
276 const char *fullname;
277 const char *homedir;
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;
287 const uint8 *lm_pw;
288 const uint8 *nt_pw;
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"));
295 return -1;
298 *buf = NULL;
299 buflen = 0;
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;
317 else domain_len = 0;
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);
338 else homedir = NULL;
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;
366 unknown_str = NULL;
367 unknown_str_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,
375 logon_time,
376 logoff_time,
377 kickoff_time,
378 pass_last_set_time,
379 pass_can_change_time,
380 pass_must_change_time,
381 username_len, username,
382 domain_len, domain,
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,
393 user_rid,
394 group_rid,
395 lm_pw_len, lm_pw,
396 nt_pw_len, nt_pw,
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"));
409 return (-1);
412 /* now for the real call to tdb_pack() */
413 buflen = tdb_pack(*buf, len, TDB_FORMAT_STRING,
414 logon_time,
415 logoff_time,
416 kickoff_time,
417 pass_last_set_time,
418 pass_can_change_time,
419 pass_must_change_time,
420 username_len, username,
421 domain_len, domain,
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,
432 user_rid,
433 group_rid,
434 lm_pw_len, lm_pw,
435 nt_pw_len, nt_pw,
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 */
446 if (buflen != len) {
447 DEBUG(0, ("init_buffer_from_sam: somthing odd is going on here: bufflen (%d) != len (%d) in tdb_pack operations!\n",
448 buflen, len));
449 /* error */
450 SAFE_FREE (*buf);
451 return (-1);
454 return (buflen);
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);
474 return NT_STATUS_OK;
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;
505 TDB_DATA data;
506 const char *prefix = USERPREFIX;
507 int prefixlen = strlen (prefix);
510 if (user==NULL) {
511 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
512 return nt_status;
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"));
523 return nt_status;
526 data = tdb_fetch(tdb_state->passwd_tdb, tdb_state->key);
527 if (!data.dptr) {
528 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
529 return nt_status;
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);
536 return nt_status;
538 SAFE_FREE(data.dptr);
540 /* increment to next in line */
541 tdb_state->key = tdb_nextkey(tdb_state->passwd_tdb, tdb_state->key);
543 return NT_STATUS_OK;
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;
555 TDB_DATA data, key;
556 fstring keystr;
557 fstring name;
559 if (user==NULL) {
560 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
561 return nt_status;
565 /* Data is stored in all lower-case */
566 fstrcpy(name, sname);
567 strlower(name);
569 /* set search key */
570 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
571 key.dptr = keystr;
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));
577 return nt_status;
580 /* get the record */
581 data = tdb_fetch(pwd_tdb, key);
582 if (!data.dptr) {
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));
586 tdb_close(pwd_tdb);
587 return nt_status;
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);
594 tdb_close(pwd_tdb);
595 return nt_status;
597 SAFE_FREE(data.dptr);
599 /* no further use for database, close it now */
600 tdb_close(pwd_tdb);
602 return NT_STATUS_OK;
605 /***************************************************************************
606 Search by rid
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;
614 TDB_DATA data, key;
615 fstring keystr;
616 fstring name;
618 if (user==NULL) {
619 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
620 return nt_status;
623 /* set search key */
624 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
625 key.dptr = keystr;
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"));
631 return nt_status;
634 /* get the record */
635 data = tdb_fetch (pwd_tdb, key);
636 if (!data.dptr) {
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)));
639 tdb_close (pwd_tdb);
640 return nt_status;
643 fstrcpy (name, data.dptr);
644 SAFE_FREE(data.dptr);
646 tdb_close (pwd_tdb);
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)
653 uint32 rid;
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 /***************************************************************************
660 Delete a SAM_ACCOUNT
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;
668 TDB_DATA key;
669 fstring keystr;
670 uint32 rid;
671 fstring name;
673 fstrcpy(name, pdb_get_username(sam_pass));
674 strlower(name);
676 /* open the TDB */
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!"));
679 return nt_status;
682 /* set the search key */
683 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
684 key.dptr = keystr;
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)));
693 tdb_close(pwd_tdb);
694 return nt_status;
697 /* delete also the RID key */
699 /* set the search key */
700 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
701 key.dptr = keystr;
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)));
708 tdb_close(pwd_tdb);
709 return nt_status;
712 tdb_close(pwd_tdb);
714 return NT_STATUS_OK;
717 /***************************************************************************
718 Update the TDB SAM
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;
725 TDB_DATA key, data;
726 uint8 *buf = NULL;
727 fstring keystr;
728 fstring name;
729 BOOL ret = True;
730 uint32 user_rid;
731 BOOL tdb_ret;
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);
741 if (!pwd_tdb)
743 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
744 return False;
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)));
749 ret = False;
750 goto done;
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"));
760 ret = False;
761 goto done;
763 user_rid = lowrid;
764 tdb_ret = tdb_change_uint32_atomic(pwd_tdb, "RID_COUNTER", &user_rid, RID_MULTIPLIER);
765 if (!tdb_ret) {
766 ret = False;
767 goto done;
769 if (user_rid > highrid) {
770 DEBUG(0, ("tdbsam: no NUA rids available, cannot add user %s!\n", pdb_get_username(newpwd)));
771 ret = False;
772 goto done;
774 } else {
775 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a RID\n",pdb_get_username(newpwd)));
776 ret = False;
777 goto done;
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"));
784 ret = False;
785 goto done;
787 data.dptr = buf;
789 fstrcpy(name, pdb_get_username(newpwd));
790 strlower(name);
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);
796 key.dptr = keystr;
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));
804 ret = False;
805 goto done;
808 /* setup RID data */
809 data.dsize = sizeof(fstring);
810 data.dptr = name;
812 /* setup the RID index key */
813 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, user_rid);
814 key.dptr = keystr;
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));
822 ret = False;
823 goto done;
826 done:
827 /* cleanup */
828 tdb_close (pwd_tdb);
829 SAFE_FREE(buf);
831 return (ret);
834 #if 0
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!
839 - SSS
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;
847 uint32 rid;
849 if (sid == NULL) {
850 return NT_STATUS_INVALID_PARAMETER;
853 pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
854 if (!pwd_tdb)
856 DEBUG(0, ("tdbsam_get_next_sid: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
857 return NT_STATUS_UNSUCCESSFUL;
860 rid = BASE_RID;
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)) {
865 goto done;
868 ret = NT_STATUS_OK;
871 done:
872 tdb_close (pwd_tdb);
873 return ret;
875 #endif
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))
884 return NT_STATUS_OK;
885 else
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))
896 return NT_STATUS_OK;
897 else
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);
905 *tdb_state = NULL;
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)
913 NTSTATUS nt_status;
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))) {
918 return nt_status;
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));
934 if (!tdb_state) {
935 DEBUG(0, ("talloc() failed for tdbsam private_data!\n"));
936 return NT_STATUS_NO_MEMORY;
939 if (location) {
940 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, location);
941 } else {
942 pstring tdbfile;
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);
962 } else {
963 tdb_state->algorithmic_rids = True;
966 return NT_STATUS_OK;
969 int pdb_tdbsam_init(void)
971 smb_register_passdb(PASSDB_INTERFACE_VERSION, "tdbsam", pdb_init_tdbsam);
972 return True;