Squelch some warnings with more casty-foo.
[Samba.git] / source3 / passdb / pdb_tdb.c
blobc9a84f3242c2e06a69031101ca604c66d025e846
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 USERPREFIX "USER_"
43 #define RIDPREFIX "RID_"
45 struct tdbsam_privates {
46 TDB_CONTEXT *passwd_tdb;
47 TDB_DATA key;
49 /* retrive-once info */
50 const char *tdbsam_location;
53 /***************************************************************
54 Open the TDB passwd database for SAM account enumeration.
55 ****************************************************************/
57 static NTSTATUS tdbsam_setsampwent(struct pdb_methods *my_methods, BOOL update)
59 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
61 /* Open tdb passwd */
62 if (!(tdb_state->passwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, update?(O_RDWR|O_CREAT):O_RDONLY, 0600)))
64 DEBUG(0, ("Unable to open/create TDB passwd\n"));
65 return NT_STATUS_UNSUCCESSFUL;
68 tdb_state->key = tdb_firstkey(tdb_state->passwd_tdb);
70 return NT_STATUS_OK;
73 static void close_tdb(struct tdbsam_privates *tdb_state)
75 if (tdb_state->passwd_tdb) {
76 tdb_close(tdb_state->passwd_tdb);
77 tdb_state->passwd_tdb = NULL;
81 /***************************************************************
82 End enumeration of the TDB passwd list.
83 ****************************************************************/
85 static void tdbsam_endsampwent(struct pdb_methods *my_methods)
87 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
88 SAFE_FREE(tdb_state->key.dptr);
89 close_tdb(tdb_state);
91 DEBUG(7, ("endtdbpwent: closed sam database.\n"));
94 /*****************************************************************
95 Get one SAM_ACCOUNT from the TDB (next in line)
96 *****************************************************************/
98 static NTSTATUS tdbsam_getsampwent(struct pdb_methods *my_methods, SAM_ACCOUNT *user)
100 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
101 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
102 TDB_DATA data, old_key;
103 const char *prefix = USERPREFIX;
104 int prefixlen = strlen (prefix);
107 if (user==NULL) {
108 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
109 return nt_status;
112 /* skip all non-USER entries (eg. RIDs) */
113 while ((tdb_state->key.dsize != 0) && (strncmp(tdb_state->key.dptr, prefix, prefixlen))) {
115 old_key = tdb_state->key;
117 /* increment to next in line */
118 tdb_state->key = tdb_nextkey(tdb_state->passwd_tdb, tdb_state->key);
120 SAFE_FREE(old_key.dptr);
123 /* do we have an valid iteration pointer? */
124 if(tdb_state->passwd_tdb == NULL) {
125 DEBUG(0,("pdb_get_sampwent: Bad TDB Context pointer.\n"));
126 return nt_status;
129 data = tdb_fetch(tdb_state->passwd_tdb, tdb_state->key);
130 if (!data.dptr) {
131 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
132 return nt_status;
135 /* unpack the buffer */
136 if (!init_sam_from_buffer(user, (unsigned char *)data.dptr, data.dsize)) {
137 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
138 SAFE_FREE(data.dptr);
139 return nt_status;
141 SAFE_FREE(data.dptr);
143 old_key = tdb_state->key;
145 /* increment to next in line */
146 tdb_state->key = tdb_nextkey(tdb_state->passwd_tdb, tdb_state->key);
148 SAFE_FREE(old_key.dptr);
150 return NT_STATUS_OK;
153 /******************************************************************
154 Lookup a name in the SAM TDB
155 ******************************************************************/
157 static NTSTATUS tdbsam_getsampwnam (struct pdb_methods *my_methods, SAM_ACCOUNT *user, const char *sname)
159 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
160 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
161 TDB_CONTEXT *pwd_tdb;
162 TDB_DATA data, key;
163 fstring keystr;
164 fstring name;
166 if (user==NULL) {
167 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
168 return nt_status;
172 /* Data is stored in all lower-case */
173 fstrcpy(name, sname);
174 strlower_m(name);
176 /* set search key */
177 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
178 key.dptr = keystr;
179 key.dsize = strlen(keystr) + 1;
181 /* open the accounts TDB */
182 if (!(pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
184 if (errno == ENOENT) {
186 * TDB file doesn't exist, so try to create new one. This is useful to avoid
187 * confusing error msg when adding user account first time
189 if (!(pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_CREAT, 0600))) {
190 DEBUG(0, ("pdb_getsampwnam: TDB passwd (%s) did not exist. File successfully created.\n",
191 tdb_state->tdbsam_location));
192 } else {
193 DEBUG(0, ("pdb_getsampwnam: TDB passwd (%s) does not exist. Couldn't create new one. Error was: %s\n",
194 tdb_state->tdbsam_location, strerror(errno)));
197 /* requested user isn't there anyway */
198 nt_status = NT_STATUS_NO_SUCH_USER;
199 return nt_status;
201 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
202 return nt_status;
205 /* get the record */
206 data = tdb_fetch(pwd_tdb, key);
207 if (!data.dptr) {
208 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
209 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
210 DEBUGADD(5, (" Key: %s\n", keystr));
211 tdb_close(pwd_tdb);
212 return nt_status;
215 /* unpack the buffer */
216 if (!init_sam_from_buffer(user, (unsigned char *)data.dptr, data.dsize)) {
217 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
218 SAFE_FREE(data.dptr);
219 tdb_close(pwd_tdb);
220 return nt_status;
222 SAFE_FREE(data.dptr);
224 /* no further use for database, close it now */
225 tdb_close(pwd_tdb);
227 return NT_STATUS_OK;
230 /***************************************************************************
231 Search by rid
232 **************************************************************************/
234 static NTSTATUS tdbsam_getsampwrid (struct pdb_methods *my_methods, SAM_ACCOUNT *user, uint32 rid)
236 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
237 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
238 TDB_CONTEXT *pwd_tdb;
239 TDB_DATA data, key;
240 fstring keystr;
241 fstring name;
243 if (user==NULL) {
244 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
245 return nt_status;
248 /* set search key */
249 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
250 key.dptr = keystr;
251 key.dsize = strlen (keystr) + 1;
253 /* open the accounts TDB */
254 if (!(pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
255 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
256 return nt_status;
259 /* get the record */
260 data = tdb_fetch (pwd_tdb, key);
261 if (!data.dptr) {
262 DEBUG(5,("pdb_getsampwrid (TDB): error looking up RID %d by key %s.\n", rid, keystr));
263 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
264 tdb_close (pwd_tdb);
265 return nt_status;
268 fstrcpy(name, data.dptr);
269 SAFE_FREE(data.dptr);
271 tdb_close (pwd_tdb);
273 return tdbsam_getsampwnam (my_methods, user, name);
276 static NTSTATUS tdbsam_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
278 uint32 rid;
279 if (!sid_peek_check_rid(get_global_sam_sid(), sid, &rid))
280 return NT_STATUS_UNSUCCESSFUL;
281 return tdbsam_getsampwrid(my_methods, user, rid);
284 /***************************************************************************
285 Delete a SAM_ACCOUNT
286 ****************************************************************************/
288 static NTSTATUS tdbsam_delete_sam_account(struct pdb_methods *my_methods, SAM_ACCOUNT *sam_pass)
290 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
291 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
292 TDB_CONTEXT *pwd_tdb;
293 TDB_DATA key;
294 fstring keystr;
295 uint32 rid;
296 fstring name;
298 fstrcpy(name, pdb_get_username(sam_pass));
299 strlower_m(name);
301 /* open the TDB */
302 if (!(pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDWR, 0600))) {
303 DEBUG(0, ("Unable to open TDB passwd!"));
304 return nt_status;
307 /* set the search key */
308 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
309 key.dptr = keystr;
310 key.dsize = strlen (keystr) + 1;
312 rid = pdb_get_user_rid(sam_pass);
314 /* it's outaa here! 8^) */
315 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
316 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
317 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
318 tdb_close(pwd_tdb);
319 return nt_status;
322 /* delete also the RID key */
324 /* set the search key */
325 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
326 key.dptr = keystr;
327 key.dsize = strlen (keystr) + 1;
329 /* it's outaa here! 8^) */
330 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
331 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
332 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
333 tdb_close(pwd_tdb);
334 return nt_status;
337 tdb_close(pwd_tdb);
339 return NT_STATUS_OK;
342 /***************************************************************************
343 Update the TDB SAM
344 ****************************************************************************/
346 static BOOL tdb_update_sam(struct pdb_methods *my_methods, SAM_ACCOUNT* newpwd, int flag)
348 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
349 TDB_CONTEXT *pwd_tdb = NULL;
350 TDB_DATA key, data;
351 uint8 *buf = NULL;
352 fstring keystr;
353 fstring name;
354 BOOL ret = True;
355 uint32 user_rid;
357 /* invalidate the existing TDB iterator if it is open */
359 if (tdb_state->passwd_tdb) {
360 tdb_close(tdb_state->passwd_tdb);
361 tdb_state->passwd_tdb = NULL;
364 /* open the account TDB passwd*/
366 pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
368 if (!pwd_tdb) {
369 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd (%s)!\n",
370 tdb_state->tdbsam_location));
371 return False;
374 if (!pdb_get_group_rid(newpwd)) {
375 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a primary group RID\n",
376 pdb_get_username(newpwd)));
377 ret = False;
378 goto done;
381 if ( !(user_rid = pdb_get_user_rid(newpwd)) ) {
382 DEBUG(0,("tdb_update_sam: SAM_ACCOUNT (%s) with no RID!\n", pdb_get_username(newpwd)));
383 ret = False;
384 goto done;
387 /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
388 if ((data.dsize=init_buffer_from_sam (&buf, newpwd, False)) == -1) {
389 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
390 ret = False;
391 goto done;
393 data.dptr = (char *)buf;
395 fstrcpy(name, pdb_get_username(newpwd));
396 strlower_m(name);
398 DEBUG(5, ("Storing %saccount %s with RID %d\n", flag == TDB_INSERT ? "(new) " : "", name, user_rid));
400 /* setup the USER index key */
401 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
402 key.dptr = keystr;
403 key.dsize = strlen(keystr) + 1;
405 /* add the account */
406 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
407 DEBUG(0, ("Unable to modify passwd TDB!"));
408 DEBUGADD(0, (" Error: %s", tdb_errorstr(pwd_tdb)));
409 DEBUGADD(0, (" occured while storing the main record (%s)\n", keystr));
410 ret = False;
411 goto done;
414 /* setup RID data */
415 data.dsize = strlen(name) + 1;
416 data.dptr = name;
418 /* setup the RID index key */
419 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, user_rid);
420 key.dptr = keystr;
421 key.dsize = strlen (keystr) + 1;
423 /* add the reference */
424 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
425 DEBUG(0, ("Unable to modify TDB passwd !"));
426 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
427 DEBUGADD(0, (" occured while storing the RID index (%s)\n", keystr));
428 ret = False;
429 goto done;
432 done:
433 /* cleanup */
434 tdb_close (pwd_tdb);
435 SAFE_FREE(buf);
437 return (ret);
440 /***************************************************************************
441 Modifies an existing SAM_ACCOUNT
442 ****************************************************************************/
444 static NTSTATUS tdbsam_update_sam_account (struct pdb_methods *my_methods, SAM_ACCOUNT *newpwd)
446 if (tdb_update_sam(my_methods, newpwd, TDB_MODIFY))
447 return NT_STATUS_OK;
448 else
449 return NT_STATUS_UNSUCCESSFUL;
452 /***************************************************************************
453 Adds an existing SAM_ACCOUNT
454 ****************************************************************************/
456 static NTSTATUS tdbsam_add_sam_account (struct pdb_methods *my_methods, SAM_ACCOUNT *newpwd)
458 if (tdb_update_sam(my_methods, newpwd, TDB_INSERT))
459 return NT_STATUS_OK;
460 else
461 return NT_STATUS_UNSUCCESSFUL;
464 static void free_private_data(void **vp)
466 struct tdbsam_privates **tdb_state = (struct tdbsam_privates **)vp;
467 close_tdb(*tdb_state);
468 *tdb_state = NULL;
470 /* No need to free any further, as it is talloc()ed */
474 static NTSTATUS pdb_init_tdbsam(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
476 NTSTATUS nt_status;
477 struct tdbsam_privates *tdb_state;
479 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
480 return nt_status;
483 (*pdb_method)->name = "tdbsam";
485 (*pdb_method)->setsampwent = tdbsam_setsampwent;
486 (*pdb_method)->endsampwent = tdbsam_endsampwent;
487 (*pdb_method)->getsampwent = tdbsam_getsampwent;
488 (*pdb_method)->getsampwnam = tdbsam_getsampwnam;
489 (*pdb_method)->getsampwsid = tdbsam_getsampwsid;
490 (*pdb_method)->add_sam_account = tdbsam_add_sam_account;
491 (*pdb_method)->update_sam_account = tdbsam_update_sam_account;
492 (*pdb_method)->delete_sam_account = tdbsam_delete_sam_account;
494 tdb_state = talloc_zero(pdb_context->mem_ctx, sizeof(struct tdbsam_privates));
496 if (!tdb_state) {
497 DEBUG(0, ("talloc() failed for tdbsam private_data!\n"));
498 return NT_STATUS_NO_MEMORY;
501 if (location) {
502 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, location);
503 } else {
504 pstring tdbfile;
505 get_private_directory(tdbfile);
506 pstrcat(tdbfile, "/");
507 pstrcat(tdbfile, PASSDB_FILE_NAME);
508 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, tdbfile);
511 (*pdb_method)->private_data = tdb_state;
513 (*pdb_method)->free_private_data = free_private_data;
515 return NT_STATUS_OK;
518 NTSTATUS pdb_tdbsam_init(void)
520 return smb_register_passdb(PASSDB_INTERFACE_VERSION, "tdbsam", pdb_init_tdbsam);