preparing for release of 3.0-alpha17
[Samba/ekacnet.git] / source / passdb / pdb_tdb.c
blob40ba8dd475b94f6705aa94718aaf0c631b5096b9
1 /*
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
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 #ifdef WITH_TDB_SAM
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;
37 TDB_DATA key;
39 /* retrive-once info */
40 const char *tdbsam_location;
42 BOOL permit_non_unix_accounts;
44 uint32 low_nua_rid;
45 uint32 high_nua_rid;
48 /**********************************************************************
49 Intialize a SAM_ACCOUNT struct from a BYTE buffer of size len
50 *********************************************************************/
52 static BOOL init_sam_from_buffer (struct tdbsam_privates *tdb_state,
53 SAM_ACCOUNT *sampass, uint8 *buf, uint32 buflen)
56 /* times are stored as 32bit integer
57 take care on system with 64bit wide time_t
58 --SSS */
59 uint32 logon_time,
60 logoff_time,
61 kickoff_time,
62 pass_last_set_time,
63 pass_can_change_time,
64 pass_must_change_time;
65 char *username;
66 char *domain;
67 char *nt_username;
68 char *dir_drive;
69 char *unknown_str;
70 char *munged_dial;
71 char *fullname;
72 char *homedir;
73 char *logon_script;
74 char *profile_path;
75 char *acct_desc;
76 char *workstations;
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;
84 uint8 *hours;
85 static uint8 *lm_pw_ptr, *nt_pw_ptr;
86 uint32 len = 0;
87 uint32 lmpwlen, ntpwlen, hourslen;
88 BOOL ret = True;
89 BOOL setflag;
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"));
94 return False;
97 /* unpack the buffer into variables */
98 len = tdb_unpack (buf, buflen, TDB_FORMAT_STRING,
99 &logon_time,
100 &logoff_time,
101 &kickoff_time,
102 &pass_last_set_time,
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,
117 &user_rid,
118 &group_rid,
119 &lmpwlen, &lm_pw_ptr,
120 &ntpwlen, &nt_pw_ptr,
121 &acct_ctrl,
122 &unknown_3,
123 &logon_divs,
124 &hours_len,
125 &hourslen, &hours,
126 &unknown_5,
127 &unknown_6);
129 if (len == -1) {
130 ret = False;
131 goto done;
134 if ((tdb_state->permit_non_unix_accounts)
135 && (user_rid >= tdb_state->low_nua_rid)
136 && (user_rid <= tdb_state->high_nua_rid)) {
138 } else {
139 struct passwd *pw;
140 uid_t uid;
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"));
148 ret = False;
149 goto done;
151 uid = pw->pw_uid;
152 gid = pw->pw_gid;
154 passwd_free(&pw);
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;
173 else {
174 setflag = False;
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;
183 else {
184 setflag = False;
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;
193 else {
194 setflag = False;
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;
203 else {
204 setflag = False;
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)) {
216 ret = False;
217 goto done;
219 if (!pdb_set_nt_passwd(sampass, nt_pw_ptr)) {
220 ret = False;
221 goto done;
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);
236 done:
238 SAFE_FREE(username);
239 SAFE_FREE(domain);
240 SAFE_FREE(nt_username);
241 SAFE_FREE(fullname);
242 SAFE_FREE(homedir);
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);
250 return ret;
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)
259 size_t len, buflen;
261 /* times are stored as 32bit integer
262 take care on system with 64bit wide time_t
263 --SSS */
264 uint32 logon_time,
265 logoff_time,
266 kickoff_time,
267 pass_last_set_time,
268 pass_can_change_time,
269 pass_must_change_time;
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);
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;
315 else domain_len = 0;
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);
335 else homedir = NULL;
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;
363 unknown_str = NULL;
364 unknown_str_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,
372 logon_time,
373 logoff_time,
374 kickoff_time,
375 pass_last_set_time,
376 pass_can_change_time,
377 pass_must_change_time,
378 username_len, username,
379 domain_len, domain,
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,
390 user_rid,
391 group_rid,
392 lm_pw_len, lm_pw,
393 nt_pw_len, nt_pw,
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"));
406 return (-1);
409 /* now for the real call to tdb_pack() */
410 buflen = tdb_pack(*buf, len, TDB_FORMAT_STRING,
411 logon_time,
412 logoff_time,
413 kickoff_time,
414 pass_last_set_time,
415 pass_can_change_time,
416 pass_must_change_time,
417 username_len, username,
418 domain_len, domain,
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,
429 user_rid,
430 group_rid,
431 lm_pw_len, lm_pw,
432 nt_pw_len, nt_pw,
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 */
443 if (buflen != len) {
444 DEBUG(0, ("init_buffer_from_sam: somthing odd is going on here: bufflen (%d) != len (%d) in tdb_pack operations!\n",
445 buflen, len));
446 /* error */
447 SAFE_FREE (*buf);
448 return (-1);
451 return (buflen);
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"));
466 return False;
469 tdb_state->key = tdb_firstkey(tdb_state->passwd_tdb);
471 return True;
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;
501 TDB_DATA data;
502 char *prefix = USERPREFIX;
503 int prefixlen = strlen (prefix);
506 if (user==NULL) {
507 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
508 return False;
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"));
519 return False;
522 data = tdb_fetch(tdb_state->passwd_tdb, tdb_state->key);
523 if (!data.dptr) {
524 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
525 return False;
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);
532 return False;
534 SAFE_FREE(data.dptr);
536 /* increment to next in line */
537 tdb_state->key = tdb_nextkey(tdb_state->passwd_tdb, tdb_state->key);
539 return True;
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;
550 TDB_DATA data, key;
551 fstring keystr;
552 fstring name;
554 if (user==NULL) {
555 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
556 return False;
559 /* Data is stored in all lower-case */
560 unix_strlower(sname, -1, name, sizeof(name));
562 /* set search key */
563 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
564 key.dptr = keystr;
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));
570 return False;
573 /* get the record */
574 data = tdb_fetch(pwd_tdb, key);
575 if (!data.dptr) {
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));
579 tdb_close(pwd_tdb);
580 return False;
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);
587 tdb_close(pwd_tdb);
588 return False;
590 SAFE_FREE(data.dptr);
592 /* no further use for database, close it now */
593 tdb_close(pwd_tdb);
595 return True;
598 /***************************************************************************
599 Search by rid
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;
606 TDB_DATA data, key;
607 fstring keystr;
608 fstring name;
610 if (user==NULL) {
611 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
612 return False;
615 /* set search key */
616 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
617 key.dptr = keystr;
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"));
623 return False;
626 /* get the record */
627 data = tdb_fetch (pwd_tdb, key);
628 if (!data.dptr) {
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)));
631 tdb_close (pwd_tdb);
632 return False;
635 fstrcpy (name, data.dptr);
636 SAFE_FREE(data.dptr);
638 tdb_close (pwd_tdb);
640 return tdbsam_getsampwnam (context, user, name);
643 /***************************************************************************
644 Delete a SAM_ACCOUNT
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;
651 TDB_DATA key;
652 fstring keystr;
653 uint32 rid;
654 fstring name;
656 unix_strlower(pdb_get_username(sam_pass), -1, name, sizeof(name));
658 /* open the TDB */
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!"));
661 return False;
664 /* set the search key */
665 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
666 key.dptr = keystr;
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)));
675 tdb_close(pwd_tdb);
676 return False;
679 /* delete also the RID key */
681 /* set the search key */
682 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
683 key.dptr = keystr;
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)));
690 tdb_close(pwd_tdb);
691 return False;
694 tdb_close(pwd_tdb);
696 return True;
699 /***************************************************************************
700 Update the TDB SAM
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;
707 TDB_DATA key, data;
708 uint8 *buf = NULL;
709 fstring keystr;
710 fstring name;
711 BOOL ret = True;
712 uint32 user_rid;
713 uint32 group_rid;
714 int32 tdb_ret;
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);
724 if (!pwd_tdb)
726 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
727 return False;
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)));
734 ret = False;
735 goto done;
736 } else {
737 user_rid = tdb_state->low_nua_rid;
738 tdb_ret = tdb_change_int32_atomic(pwd_tdb, "NUA_NEXT_RID", &user_rid, RID_MULTIPLIER);
739 if (tdb_ret == -1) {
740 ret = False;
741 goto done;
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)));
749 ret = False;
750 goto done;
751 } else {
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"));
760 ret = False;
761 goto done;
763 data.dptr = buf;
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);
771 key.dptr = keystr;
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));
779 ret = False;
780 goto done;
783 /* setup RID data */
784 data.dsize = sizeof(fstring);
785 data.dptr = name;
787 /* setup the RID index key */
788 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, user_rid);
789 key.dptr = keystr;
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));
797 ret = False;
798 goto done;
801 done:
802 /* cleanup */
803 tdb_close (pwd_tdb);
804 SAFE_FREE(buf);
806 return (ret);
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);
831 *tdb_state = NULL;
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)
839 NTSTATUS nt_status;
840 struct tdbsam_privates *tdb_state;
842 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
843 return nt_status;
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));
859 if (!tdb_state) {
860 DEBUG(0, ("talloc() failed for tdbsam private_data!\n"));
861 return NT_STATUS_NO_MEMORY;
864 if (location) {
865 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, location);
866 } else {
867 pstring tdbfile;
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;
878 return NT_STATUS_OK;
881 NTSTATUS pdb_init_tdbsam_nua(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
883 NTSTATUS nt_status;
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))) {
888 return nt_status;
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);
906 return NT_STATUS_OK;
910 #else
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;
925 #endif