Make static (patch from metze)
[Samba.git] / source3 / passdb / pdb_tdb.c
blob912634a1b155e461b22990025e84dd5cf1c19dab
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 uid_t uid = -1;
103 gid_t gid = -1;
105 if(sampass == NULL || buf == NULL) {
106 DEBUG(0, ("init_sam_from_buffer: NULL parameters found!\n"));
107 return False;
110 /* unpack the buffer into variables */
111 len = tdb_unpack (buf, buflen, TDB_FORMAT_STRING,
112 &logon_time,
113 &logoff_time,
114 &kickoff_time,
115 &pass_last_set_time,
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,
130 &user_rid,
131 &group_rid,
132 &lm_pw_len, &lm_pw_ptr,
133 &nt_pw_len, &nt_pw_ptr,
134 &acct_ctrl,
135 &unknown_3,
136 &logon_divs,
137 &hours_len,
138 &hourslen, &hours,
139 &unknown_5,
140 &unknown_6);
142 if (len == -1) {
143 ret = False;
144 goto done;
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);
159 if (homedir) {
160 pdb_set_homedir(sampass, homedir, PDB_SET);
162 else {
163 pdb_set_homedir(sampass,
164 talloc_sub_specified(sampass->mem_ctx,
165 lp_logon_home(),
166 username, domain,
167 uid, gid),
168 PDB_DEFAULT);
171 if (dir_drive)
172 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
173 else {
174 pdb_set_dir_drive(sampass,
175 talloc_sub_specified(sampass->mem_ctx,
176 lp_logon_drive(),
177 username, domain,
178 uid, gid),
179 PDB_DEFAULT);
182 if (logon_script)
183 pdb_set_logon_script(sampass, logon_script, PDB_SET);
184 else {
185 pdb_set_logon_script(sampass,
186 talloc_sub_specified(sampass->mem_ctx,
187 lp_logon_script(),
188 username, domain,
189 uid, gid),
190 PDB_DEFAULT);
193 if (profile_path) {
194 pdb_set_profile_path(sampass, profile_path, PDB_SET);
195 } else {
196 pdb_set_profile_path(sampass,
197 talloc_sub_specified(sampass->mem_ctx,
198 lp_logon_path(),
199 username, domain,
200 uid, gid),
201 PDB_DEFAULT);
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)) {
210 ret = False;
211 goto done;
215 if (nt_pw_ptr && nt_pw_len == NT_HASH_LEN) {
216 if (!pdb_set_nt_passwd(sampass, nt_pw_ptr, PDB_SET)) {
217 ret = False;
218 goto done;
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);
232 done:
234 SAFE_FREE(username);
235 SAFE_FREE(domain);
236 SAFE_FREE(nt_username);
237 SAFE_FREE(fullname);
238 SAFE_FREE(homedir);
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);
246 return ret;
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)
255 size_t len, buflen;
257 /* times are stored as 32bit integer
258 take care on system with 64bit wide time_t
259 --SSS */
260 uint32 logon_time,
261 logoff_time,
262 kickoff_time,
263 pass_last_set_time,
264 pass_can_change_time,
265 pass_must_change_time;
267 uint32 user_rid, group_rid;
269 const char *username;
270 const char *domain;
271 const char *nt_username;
272 const char *dir_drive;
273 const char *unknown_str;
274 const char *munged_dial;
275 const char *fullname;
276 const char *homedir;
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;
286 const uint8 *lm_pw;
287 const uint8 *nt_pw;
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"));
294 return -1;
297 *buf = NULL;
298 buflen = 0;
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;
316 else domain_len = 0;
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);
337 else homedir = NULL;
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;
365 unknown_str = NULL;
366 unknown_str_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,
374 logon_time,
375 logoff_time,
376 kickoff_time,
377 pass_last_set_time,
378 pass_can_change_time,
379 pass_must_change_time,
380 username_len, username,
381 domain_len, domain,
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,
392 user_rid,
393 group_rid,
394 lm_pw_len, lm_pw,
395 nt_pw_len, nt_pw,
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"));
408 return (-1);
411 /* now for the real call to tdb_pack() */
412 buflen = tdb_pack(*buf, len, TDB_FORMAT_STRING,
413 logon_time,
414 logoff_time,
415 kickoff_time,
416 pass_last_set_time,
417 pass_can_change_time,
418 pass_must_change_time,
419 username_len, username,
420 domain_len, domain,
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,
431 user_rid,
432 group_rid,
433 lm_pw_len, lm_pw,
434 nt_pw_len, nt_pw,
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 */
445 if (buflen != len) {
446 DEBUG(0, ("init_buffer_from_sam: somthing odd is going on here: bufflen (%d) != len (%d) in tdb_pack operations!\n",
447 buflen, len));
448 /* error */
449 SAFE_FREE (*buf);
450 return (-1);
453 return (buflen);
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);
473 return NT_STATUS_OK;
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;
504 TDB_DATA data;
505 const char *prefix = USERPREFIX;
506 int prefixlen = strlen (prefix);
509 if (user==NULL) {
510 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
511 return nt_status;
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"));
522 return nt_status;
525 data = tdb_fetch(tdb_state->passwd_tdb, tdb_state->key);
526 if (!data.dptr) {
527 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
528 return nt_status;
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);
535 return nt_status;
537 SAFE_FREE(data.dptr);
539 /* increment to next in line */
540 tdb_state->key = tdb_nextkey(tdb_state->passwd_tdb, tdb_state->key);
542 return NT_STATUS_OK;
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;
554 TDB_DATA data, key;
555 fstring keystr;
556 fstring name;
558 if (user==NULL) {
559 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
560 return nt_status;
564 /* Data is stored in all lower-case */
565 fstrcpy(name, sname);
566 strlower(name);
568 /* set search key */
569 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
570 key.dptr = keystr;
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));
576 return nt_status;
579 /* get the record */
580 data = tdb_fetch(pwd_tdb, key);
581 if (!data.dptr) {
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));
585 tdb_close(pwd_tdb);
586 return nt_status;
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);
593 tdb_close(pwd_tdb);
594 return nt_status;
596 SAFE_FREE(data.dptr);
598 /* no further use for database, close it now */
599 tdb_close(pwd_tdb);
601 return NT_STATUS_OK;
604 /***************************************************************************
605 Search by rid
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;
613 TDB_DATA data, key;
614 fstring keystr;
615 fstring name;
617 if (user==NULL) {
618 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
619 return nt_status;
622 /* set search key */
623 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
624 key.dptr = keystr;
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"));
630 return nt_status;
633 /* get the record */
634 data = tdb_fetch (pwd_tdb, key);
635 if (!data.dptr) {
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)));
638 tdb_close (pwd_tdb);
639 return nt_status;
642 fstrcpy(name, data.dptr);
643 SAFE_FREE(data.dptr);
645 tdb_close (pwd_tdb);
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)
652 uint32 rid;
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 /***************************************************************************
659 Delete a SAM_ACCOUNT
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;
667 TDB_DATA key;
668 fstring keystr;
669 uint32 rid;
670 fstring name;
672 fstrcpy(name, pdb_get_username(sam_pass));
673 strlower(name);
675 /* open the TDB */
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!"));
678 return nt_status;
681 /* set the search key */
682 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
683 key.dptr = keystr;
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)));
692 tdb_close(pwd_tdb);
693 return nt_status;
696 /* delete also the RID key */
698 /* set the search key */
699 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
700 key.dptr = keystr;
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)));
707 tdb_close(pwd_tdb);
708 return nt_status;
711 tdb_close(pwd_tdb);
713 return NT_STATUS_OK;
716 /***************************************************************************
717 Update the TDB SAM
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;
724 TDB_DATA key, data;
725 uint8 *buf = NULL;
726 fstring keystr;
727 fstring name;
728 BOOL ret = True;
729 uint32 user_rid;
730 BOOL tdb_ret;
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);
740 if (!pwd_tdb)
742 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
743 return False;
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)));
748 ret = False;
749 goto done;
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"));
759 ret = False;
760 goto done;
762 user_rid = lowrid;
763 tdb_ret = tdb_change_uint32_atomic(pwd_tdb, "RID_COUNTER", &user_rid, RID_MULTIPLIER);
764 if (!tdb_ret) {
765 ret = False;
766 goto done;
768 if (user_rid > highrid) {
769 DEBUG(0, ("tdbsam: no NUA rids available, cannot add user %s!\n", pdb_get_username(newpwd)));
770 ret = False;
771 goto done;
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"));
775 ret = False;
776 goto done;
778 } else {
779 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a RID\n",pdb_get_username(newpwd)));
780 ret = False;
781 goto done;
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"));
788 ret = False;
789 goto done;
791 data.dptr = buf;
793 fstrcpy(name, pdb_get_username(newpwd));
794 strlower(name);
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);
800 key.dptr = keystr;
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));
808 ret = False;
809 goto done;
812 /* setup RID data */
813 data.dsize = strlen(name) + 1;
814 data.dptr = name;
816 /* setup the RID index key */
817 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, user_rid);
818 key.dptr = keystr;
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));
826 ret = False;
827 goto done;
830 done:
831 /* cleanup */
832 tdb_close (pwd_tdb);
833 SAFE_FREE(buf);
835 return (ret);
838 #if 0
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!
843 - SSS
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;
851 uint32 rid;
853 if (sid == NULL) {
854 return NT_STATUS_INVALID_PARAMETER;
857 pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
858 if (!pwd_tdb)
860 DEBUG(0, ("tdbsam_get_next_sid: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
861 return NT_STATUS_UNSUCCESSFUL;
864 rid = BASE_RID;
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)) {
869 goto done;
872 ret = NT_STATUS_OK;
875 done:
876 tdb_close (pwd_tdb);
877 return ret;
879 #endif
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))
888 return NT_STATUS_OK;
889 else
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))
900 return NT_STATUS_OK;
901 else
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);
909 *tdb_state = NULL;
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)
917 NTSTATUS nt_status;
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))) {
922 return nt_status;
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));
938 if (!tdb_state) {
939 DEBUG(0, ("talloc() failed for tdbsam private_data!\n"));
940 return NT_STATUS_NO_MEMORY;
943 if (location) {
944 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, location);
945 } else {
946 pstring tdbfile;
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);
966 } else {
967 tdb_state->algorithmic_rids = True;
970 return NT_STATUS_OK;
973 NTSTATUS pdb_tdbsam_init(void)
975 return smb_register_passdb(PASSDB_INTERFACE_VERSION, "tdbsam", pdb_init_tdbsam);