moving more code around.
[jra/samba/.git] / source3 / passdb / pdb_tdb.c
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.
14  * 
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.
19  * 
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.
23  */
24
25 #include "includes.h"
26
27 #if 0 /* when made a module use this */
28
29 static int tdbsam_debug_level = DBGC_ALL;
30 #undef DBGC_CLASS
31 #define DBGC_CLASS tdbsam_debug_level
32
33 #else
34
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_PASSDB
37
38 #endif
39
40 #define PDB_VERSION             "20010830"
41 #define PASSDB_FILE_NAME        "passdb.tdb"
42 #define USERPREFIX              "USER_"
43 #define RIDPREFIX               "RID_"
44
45 struct tdbsam_privates {
46         TDB_CONTEXT     *passwd_tdb;
47         TDB_DATA        key;
48
49         /* retrive-once info */
50         const char *tdbsam_location;
51 };
52
53 /***************************************************************
54  Open the TDB passwd database for SAM account enumeration.
55 ****************************************************************/
56
57 static NTSTATUS tdbsam_setsampwent(struct pdb_methods *my_methods, BOOL update)
58 {
59         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
60         
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)))
63         {
64                 DEBUG(0, ("Unable to open/create TDB passwd\n"));
65                 return NT_STATUS_UNSUCCESSFUL;
66         }
67         
68         tdb_state->key = tdb_firstkey(tdb_state->passwd_tdb);
69
70         return NT_STATUS_OK;
71 }
72
73 static void close_tdb(struct tdbsam_privates *tdb_state) 
74 {
75         if (tdb_state->passwd_tdb) {
76                 tdb_close(tdb_state->passwd_tdb);
77                 tdb_state->passwd_tdb = NULL;
78         }
79 }
80
81 /***************************************************************
82  End enumeration of the TDB passwd list.
83 ****************************************************************/
84
85 static void tdbsam_endsampwent(struct pdb_methods *my_methods)
86 {
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);
90         
91         DEBUG(7, ("endtdbpwent: closed sam database.\n"));
92 }
93
94 /*****************************************************************
95  Get one SAM_ACCOUNT from the TDB (next in line)
96 *****************************************************************/
97
98 static NTSTATUS tdbsam_getsampwent(struct pdb_methods *my_methods, SAM_ACCOUNT *user)
99 {
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);
105
106
107         if (user==NULL) {
108                 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
109                 return nt_status;
110         }
111
112         /* skip all non-USER entries (eg. RIDs) */
113         while ((tdb_state->key.dsize != 0) && (strncmp(tdb_state->key.dptr, prefix, prefixlen))) {
114
115                 old_key = tdb_state->key;
116
117                 /* increment to next in line */
118                 tdb_state->key = tdb_nextkey(tdb_state->passwd_tdb, tdb_state->key);
119
120                 SAFE_FREE(old_key.dptr);
121         }
122
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;
127         }
128
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;
133         }
134   
135         /* unpack the buffer */
136         if (!init_sam_from_buffer(user, 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;
140         }
141         SAFE_FREE(data.dptr);
142         
143         old_key = tdb_state->key;
144         
145         /* increment to next in line */
146         tdb_state->key = tdb_nextkey(tdb_state->passwd_tdb, tdb_state->key);
147
148         SAFE_FREE(old_key.dptr);
149
150         return NT_STATUS_OK;
151 }
152
153 /******************************************************************
154  Lookup a name in the SAM TDB
155 ******************************************************************/
156
157 static NTSTATUS tdbsam_getsampwnam (struct pdb_methods *my_methods, SAM_ACCOUNT *user, const char *sname)
158 {
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;
165
166         if (user==NULL) {
167                 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
168                 return nt_status;
169         }
170
171         
172         /* Data is stored in all lower-case */
173         fstrcpy(name, sname);
174         strlower_m(name);
175
176         /* set search key */
177         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
178         key.dptr = keystr;
179         key.dsize = strlen(keystr) + 1;
180
181         /* open the accounts TDB */
182         if (!(pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
183                 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
184                 return nt_status;
185         }
186
187         /* get the record */
188         data = tdb_fetch(pwd_tdb, key);
189         if (!data.dptr) {
190                 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
191                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
192                 DEBUGADD(5, (" Key: %s\n", keystr));
193                 tdb_close(pwd_tdb);
194                 return nt_status;
195         }
196   
197         /* unpack the buffer */
198         if (!init_sam_from_buffer(user, data.dptr, data.dsize)) {
199                 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
200                 SAFE_FREE(data.dptr);
201                 tdb_close(pwd_tdb);
202                 return nt_status;
203         }
204         SAFE_FREE(data.dptr);
205
206         /* no further use for database, close it now */
207         tdb_close(pwd_tdb);
208         
209         return NT_STATUS_OK;
210 }
211
212 /***************************************************************************
213  Search by rid
214  **************************************************************************/
215
216 static NTSTATUS tdbsam_getsampwrid (struct pdb_methods *my_methods, SAM_ACCOUNT *user, uint32 rid)
217 {
218         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
219         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
220         TDB_CONTEXT             *pwd_tdb;
221         TDB_DATA                data, key;
222         fstring                 keystr;
223         fstring                 name;
224         
225         if (user==NULL) {
226                 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
227                 return nt_status;
228         }
229
230         /* set search key */
231         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
232         key.dptr = keystr;
233         key.dsize = strlen (keystr) + 1;
234
235         /* open the accounts TDB */
236         if (!(pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
237                 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
238                 return nt_status;
239         }
240
241         /* get the record */
242         data = tdb_fetch (pwd_tdb, key);
243         if (!data.dptr) {
244                 DEBUG(5,("pdb_getsampwrid (TDB): error looking up RID %d by key %s.\n", rid, keystr));
245                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
246                 tdb_close (pwd_tdb);
247                 return nt_status;
248         }
249
250         fstrcpy(name, data.dptr);
251         SAFE_FREE(data.dptr);
252         
253         tdb_close (pwd_tdb);
254         
255         return tdbsam_getsampwnam (my_methods, user, name);
256 }
257
258 static NTSTATUS tdbsam_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
259 {
260         uint32 rid;
261         if (!sid_peek_check_rid(get_global_sam_sid(), sid, &rid))
262                 return NT_STATUS_UNSUCCESSFUL;
263         return tdbsam_getsampwrid(my_methods, user, rid);
264 }
265
266 /***************************************************************************
267  Delete a SAM_ACCOUNT
268 ****************************************************************************/
269
270 static NTSTATUS tdbsam_delete_sam_account(struct pdb_methods *my_methods, SAM_ACCOUNT *sam_pass)
271 {
272         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
273         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
274         TDB_CONTEXT     *pwd_tdb;
275         TDB_DATA        key;
276         fstring         keystr;
277         uint32          rid;
278         fstring         name;
279         
280         fstrcpy(name, pdb_get_username(sam_pass));
281         strlower_m(name);
282         
283         /* open the TDB */
284         if (!(pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDWR, 0600))) {
285                 DEBUG(0, ("Unable to open TDB passwd!"));
286                 return nt_status;
287         }
288   
289         /* set the search key */
290         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
291         key.dptr = keystr;
292         key.dsize = strlen (keystr) + 1;
293         
294         rid = pdb_get_user_rid(sam_pass);
295
296         /* it's outaa here!  8^) */
297         if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
298                 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
299                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
300                 tdb_close(pwd_tdb); 
301                 return nt_status;
302         }       
303
304         /* delete also the RID key */
305
306         /* set the search key */
307         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
308         key.dptr = keystr;
309         key.dsize = strlen (keystr) + 1;
310
311         /* it's outaa here!  8^) */
312         if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
313                 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
314                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
315                 tdb_close(pwd_tdb); 
316                 return nt_status;
317         }
318         
319         tdb_close(pwd_tdb);
320         
321         return NT_STATUS_OK;
322 }
323
324 /***************************************************************************
325  Update the TDB SAM
326 ****************************************************************************/
327
328 static BOOL tdb_update_sam(struct pdb_methods *my_methods, SAM_ACCOUNT* newpwd, int flag)
329 {
330         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
331         TDB_CONTEXT     *pwd_tdb = NULL;
332         TDB_DATA        key, data;
333         uint8           *buf = NULL;
334         fstring         keystr;
335         fstring         name;
336         BOOL            ret = True;
337         uint32          user_rid;
338
339         /* invalidate the existing TDB iterator if it is open */
340         
341         if (tdb_state->passwd_tdb) {
342                 tdb_close(tdb_state->passwd_tdb);
343                 tdb_state->passwd_tdb = NULL;
344         }
345
346         /* open the account TDB passwd*/
347         
348         pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
349         
350         if (!pwd_tdb) {
351                 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd (%s)!\n", 
352                         tdb_state->tdbsam_location));
353                 return False;
354         }
355
356         if (!pdb_get_group_rid(newpwd)) {
357                 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a primary group RID\n",
358                         pdb_get_username(newpwd)));
359                 ret = False;
360                 goto done;
361         }
362
363         /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
364         if ((data.dsize=init_buffer_from_sam (&buf, newpwd, False)) == -1) {
365                 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
366                 ret = False;
367                 goto done;
368         }
369         data.dptr = buf;
370
371         fstrcpy(name, pdb_get_username(newpwd));
372         strlower_m(name);
373         
374         DEBUG(5, ("Storing %saccount %s with RID %d\n", flag == TDB_INSERT ? "(new) " : "", name, user_rid));
375
376         /* setup the USER index key */
377         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
378         key.dptr = keystr;
379         key.dsize = strlen(keystr) + 1;
380
381         /* add the account */
382         if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
383                 DEBUG(0, ("Unable to modify passwd TDB!"));
384                 DEBUGADD(0, (" Error: %s", tdb_errorstr(pwd_tdb)));
385                 DEBUGADD(0, (" occured while storing the main record (%s)\n", keystr));
386                 ret = False;
387                 goto done;
388         }
389         
390         /* setup RID data */
391         data.dsize = strlen(name) + 1;
392         data.dptr = name;
393
394         /* setup the RID index key */
395         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, user_rid);
396         key.dptr = keystr;
397         key.dsize = strlen (keystr) + 1;
398         
399         /* add the reference */
400         if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
401                 DEBUG(0, ("Unable to modify TDB passwd !"));
402                 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
403                 DEBUGADD(0, (" occured while storing the RID index (%s)\n", keystr));
404                 ret = False;
405                 goto done;
406         }
407
408 done:   
409         /* cleanup */
410         tdb_close (pwd_tdb);
411         SAFE_FREE(buf);
412         
413         return (ret);   
414 }
415
416 #if 0
417 /***************************************************************************
418  Allocates a new RID and returns it to the caller as a domain sid
419
420  NOTE: Use carefullt, do not waste RIDs they are a limited resource!
421                                                         - SSS
422  ***************************************************************************/
423
424 static NTSTATUS tdbsam_get_next_sid (struct pdb_methods *my_methods, DOM_SID *sid)
425 {
426         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
427         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
428         TDB_CONTEXT     *pwd_tdb;
429         uint32          rid;
430
431         if (sid == NULL) {
432                 return NT_STATUS_INVALID_PARAMETER;
433         }
434         
435         pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
436         if (!pwd_tdb)
437         {
438                 DEBUG(0, ("tdbsam_get_next_sid: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
439                 return NT_STATUS_UNSUCCESSFUL;
440         }
441
442         rid = BASE_RID;
443         if (tdb_change_uint32_atomic(pwd_tdb, "RID_COUNTER", &rid, 1)) {
444
445                 sid_copy(sid, get_global_sam_sid());
446                 if (!sid_append_rid(sid, rid)) {
447                         goto done;
448                 }
449                 
450                 ret = NT_STATUS_OK;
451         }
452
453 done:
454         tdb_close (pwd_tdb);
455         return ret;
456 }
457 #endif
458
459 /***************************************************************************
460  Modifies an existing SAM_ACCOUNT
461 ****************************************************************************/
462
463 static NTSTATUS tdbsam_update_sam_account (struct pdb_methods *my_methods, SAM_ACCOUNT *newpwd)
464 {
465         if (tdb_update_sam(my_methods, newpwd, TDB_MODIFY))
466                 return NT_STATUS_OK;
467         else
468                 return NT_STATUS_UNSUCCESSFUL;
469 }
470
471 /***************************************************************************
472  Adds an existing SAM_ACCOUNT
473 ****************************************************************************/
474
475 static NTSTATUS tdbsam_add_sam_account (struct pdb_methods *my_methods, SAM_ACCOUNT *newpwd)
476 {
477         if (tdb_update_sam(my_methods, newpwd, TDB_INSERT))
478                 return NT_STATUS_OK;
479         else
480                 return NT_STATUS_UNSUCCESSFUL;
481 }
482
483 static void free_private_data(void **vp) 
484 {
485         struct tdbsam_privates **tdb_state = (struct tdbsam_privates **)vp;
486         close_tdb(*tdb_state);
487         *tdb_state = NULL;
488
489         /* No need to free any further, as it is talloc()ed */
490 }
491
492
493 static NTSTATUS pdb_init_tdbsam(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
494 {
495         NTSTATUS nt_status;
496         struct tdbsam_privates *tdb_state;
497
498         if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
499                 return nt_status;
500         }
501
502         (*pdb_method)->name = "tdbsam";
503
504         (*pdb_method)->setsampwent = tdbsam_setsampwent;
505         (*pdb_method)->endsampwent = tdbsam_endsampwent;
506         (*pdb_method)->getsampwent = tdbsam_getsampwent;
507         (*pdb_method)->getsampwnam = tdbsam_getsampwnam;
508         (*pdb_method)->getsampwsid = tdbsam_getsampwsid;
509         (*pdb_method)->add_sam_account = tdbsam_add_sam_account;
510         (*pdb_method)->update_sam_account = tdbsam_update_sam_account;
511         (*pdb_method)->delete_sam_account = tdbsam_delete_sam_account;
512
513         tdb_state = talloc_zero(pdb_context->mem_ctx, sizeof(struct tdbsam_privates));
514
515         if (!tdb_state) {
516                 DEBUG(0, ("talloc() failed for tdbsam private_data!\n"));
517                 return NT_STATUS_NO_MEMORY;
518         }
519
520         if (location) {
521                 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, location);
522         } else {
523                 pstring tdbfile;
524                 get_private_directory(tdbfile);
525                 pstrcat(tdbfile, "/");
526                 pstrcat(tdbfile, PASSDB_FILE_NAME);
527                 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, tdbfile);
528         }
529
530         (*pdb_method)->private_data = tdb_state;
531
532         (*pdb_method)->free_private_data = free_private_data;
533
534         return NT_STATUS_OK;
535 }
536
537 NTSTATUS pdb_tdbsam_init(void)
538 {
539         return smb_register_passdb(PASSDB_INTERFACE_VERSION, "tdbsam", pdb_init_tdbsam);
540 }
541