And finally IDMAP in 3_0
[bbaumbach/samba-autobuild/.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 TDB_FORMAT_STRING       "ddddddBBBBBBBBBBBBddBBwdwdBdd"
43 #define USERPREFIX              "USER_"
44 #define RIDPREFIX               "RID_"
45
46 struct tdbsam_privates {
47         TDB_CONTEXT     *passwd_tdb;
48         TDB_DATA        key;
49
50         /* retrive-once info */
51         const char *tdbsam_location;
52
53         BOOL permit_non_unix_accounts;
54
55         BOOL algorithmic_rids;
56
57         uint32 low_nua_rid; 
58         uint32 high_nua_rid;
59 };
60
61 /**********************************************************************
62  Intialize a SAM_ACCOUNT struct from a BYTE buffer of size len
63  *********************************************************************/
64
65 static BOOL init_sam_from_buffer (struct tdbsam_privates *tdb_state,
66                                   SAM_ACCOUNT *sampass, uint8 *buf, uint32 buflen)
67 {
68
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;
94                 
95         uint32  user_rid, group_rid, unknown_3, hours_len, unknown_5, unknown_6;
96         uint16  acct_ctrl, logon_divs;
97         uint8   *hours;
98         static uint8    *lm_pw_ptr, *nt_pw_ptr;
99         uint32          len = 0;
100         uint32          lm_pw_len, nt_pw_len, hourslen;
101         BOOL ret = True;
102         struct passwd *pw;
103         uid_t uid = -1;
104         gid_t gid = -1;
105         
106         if(sampass == NULL || buf == NULL) {
107                 DEBUG(0, ("init_sam_from_buffer: NULL parameters found!\n"));
108                 return False;
109         }
110                                                                         
111         /* unpack the buffer into variables */
112         len = tdb_unpack (buf, buflen, TDB_FORMAT_STRING,
113                 &logon_time,
114                 &logoff_time,
115                 &kickoff_time,
116                 &pass_last_set_time,
117                 &pass_can_change_time,
118                 &pass_must_change_time,
119                 &username_len, &username,
120                 &domain_len, &domain,
121                 &nt_username_len, &nt_username,
122                 &fullname_len, &fullname,
123                 &homedir_len, &homedir,
124                 &dir_drive_len, &dir_drive,
125                 &logon_script_len, &logon_script,
126                 &profile_path_len, &profile_path,
127                 &acct_desc_len, &acct_desc,
128                 &workstations_len, &workstations,
129                 &unknown_str_len, &unknown_str,
130                 &munged_dial_len, &munged_dial,
131                 &user_rid,
132                 &group_rid,
133                 &lm_pw_len, &lm_pw_ptr,
134                 &nt_pw_len, &nt_pw_ptr,
135                 &acct_ctrl,
136                 &unknown_3,
137                 &logon_divs,
138                 &hours_len,
139                 &hourslen, &hours,
140                 &unknown_5,
141                 &unknown_6);
142                 
143         if (len == -1)  {
144                 ret = False;
145                 goto done;
146         }
147
148         pdb_set_logon_time(sampass, logon_time, PDB_SET);
149         pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
150         pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
151         pdb_set_pass_can_change_time(sampass, pass_can_change_time, PDB_SET);
152         pdb_set_pass_must_change_time(sampass, pass_must_change_time, PDB_SET);
153         pdb_set_pass_last_set_time(sampass, pass_last_set_time, PDB_SET);
154
155         pdb_set_username     (sampass, username, PDB_SET); 
156         pdb_set_domain       (sampass, domain, PDB_SET);
157         pdb_set_nt_username  (sampass, nt_username, PDB_SET);
158         pdb_set_fullname     (sampass, fullname, PDB_SET);
159
160         if (homedir) {
161                 pdb_set_homedir(sampass, homedir, PDB_SET);
162         }
163         else {
164                 pdb_set_homedir(sampass, 
165                                 talloc_sub_specified(sampass->mem_ctx, 
166                                                        lp_logon_home(),
167                                                        username, domain, 
168                                                        uid, gid),
169                                 PDB_DEFAULT);
170         }
171
172         if (dir_drive)  
173                 pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
174         else {
175                 pdb_set_dir_drive(sampass, 
176                                   talloc_sub_specified(sampass->mem_ctx, 
177                                                          lp_logon_drive(),
178                                                          username, domain, 
179                                                          uid, gid),
180                                   PDB_DEFAULT);
181         }
182
183         if (logon_script) 
184                 pdb_set_logon_script(sampass, logon_script, PDB_SET);
185         else {
186                 pdb_set_logon_script(sampass, 
187                                      talloc_sub_specified(sampass->mem_ctx, 
188                                                             lp_logon_script(),
189                                                             username, domain, 
190                                                             uid, gid),
191                                   PDB_DEFAULT);
192         }
193         
194         if (profile_path) {     
195                 pdb_set_profile_path(sampass, profile_path, PDB_SET);
196         } else {
197                 pdb_set_profile_path(sampass, 
198                                      talloc_sub_specified(sampass->mem_ctx, 
199                                                             lp_logon_path(),
200                                                             username, domain, 
201                                                             uid, gid),
202                                      PDB_DEFAULT);
203         }
204
205         pdb_set_acct_desc    (sampass, acct_desc, PDB_SET);
206         pdb_set_workstations (sampass, workstations, PDB_SET);
207         pdb_set_munged_dial  (sampass, munged_dial, PDB_SET);
208
209         if (lm_pw_ptr && lm_pw_len == LM_HASH_LEN) {
210                 if (!pdb_set_lanman_passwd(sampass, lm_pw_ptr, PDB_SET)) {
211                         ret = False;
212                         goto done;
213                 }
214         }
215
216         if (nt_pw_ptr && nt_pw_len == NT_HASH_LEN) {
217                 if (!pdb_set_nt_passwd(sampass, nt_pw_ptr, PDB_SET)) {
218                         ret = False;
219                         goto done;
220                 }
221         }
222
223         pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
224         pdb_set_group_sid_from_rid(sampass, group_rid, PDB_SET);
225         pdb_set_unknown_3(sampass, unknown_3, PDB_SET);
226         pdb_set_hours_len(sampass, hours_len, PDB_SET);
227         pdb_set_unknown_5(sampass, unknown_5, PDB_SET);
228         pdb_set_unknown_6(sampass, unknown_6, PDB_SET);
229         pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
230         pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
231         pdb_set_hours(sampass, hours, PDB_SET);
232
233 done:
234
235         SAFE_FREE(username);
236         SAFE_FREE(domain);
237         SAFE_FREE(nt_username);
238         SAFE_FREE(fullname);
239         SAFE_FREE(homedir);
240         SAFE_FREE(dir_drive);
241         SAFE_FREE(logon_script);
242         SAFE_FREE(profile_path);
243         SAFE_FREE(acct_desc);
244         SAFE_FREE(workstations);
245         SAFE_FREE(munged_dial);
246
247         return ret;
248 }
249
250 /**********************************************************************
251  Intialize a BYTE buffer from a SAM_ACCOUNT struct
252  *********************************************************************/
253 static uint32 init_buffer_from_sam (struct tdbsam_privates *tdb_state,
254                                     uint8 **buf, const SAM_ACCOUNT *sampass)
255 {
256         size_t          len, buflen;
257
258         /* times are stored as 32bit integer
259            take care on system with 64bit wide time_t
260            --SSS */
261         uint32  logon_time,
262                 logoff_time,
263                 kickoff_time,
264                 pass_last_set_time,
265                 pass_can_change_time,
266                 pass_must_change_time;
267
268         uint32  user_rid, group_rid;
269
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;
286
287         const uint8 *lm_pw;
288         const uint8 *nt_pw;
289         uint32  lm_pw_len = 16;
290         uint32  nt_pw_len = 16;
291
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;
296         }
297         
298         *buf = NULL;
299         buflen = 0;
300
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);
307
308         user_rid = pdb_get_user_rid(sampass);
309         group_rid = pdb_get_group_rid(sampass);
310
311         username = pdb_get_username(sampass);
312         if (username) username_len = strlen(username) +1;
313         else username_len = 0;
314
315         domain = pdb_get_domain(sampass);
316         if (domain) domain_len = strlen(domain) +1;
317         else domain_len = 0;
318
319         nt_username = pdb_get_nt_username(sampass);
320         if (nt_username) nt_username_len = strlen(nt_username) +1;
321         else nt_username_len = 0;
322
323         fullname = pdb_get_fullname(sampass);
324         if (fullname) fullname_len = strlen(fullname) +1;
325         else fullname_len = 0;
326
327         /*
328          * Only updates fields which have been set (not defaults from smb.conf)
329          */
330
331         if (!IS_SAM_DEFAULT(sampass, PDB_DRIVE)) 
332           dir_drive = pdb_get_dir_drive(sampass);
333         else dir_drive = NULL;
334         if (dir_drive) dir_drive_len = strlen(dir_drive) +1;
335         else dir_drive_len = 0;
336
337         if (!IS_SAM_DEFAULT(sampass, PDB_SMBHOME)) homedir = pdb_get_homedir(sampass);
338         else homedir = NULL;
339         if (homedir) homedir_len = strlen(homedir) +1;
340         else homedir_len = 0;
341
342         if (!IS_SAM_DEFAULT(sampass, PDB_LOGONSCRIPT)) logon_script = pdb_get_logon_script(sampass);
343         else logon_script = NULL;
344         if (logon_script) logon_script_len = strlen(logon_script) +1;
345         else logon_script_len = 0;
346
347         if (!IS_SAM_DEFAULT(sampass, PDB_PROFILE)) profile_path = pdb_get_profile_path(sampass);
348         else profile_path = NULL;
349         if (profile_path) profile_path_len = strlen(profile_path) +1;
350         else profile_path_len = 0;
351         
352         lm_pw = pdb_get_lanman_passwd(sampass);
353         if (!lm_pw) lm_pw_len = 0;
354         
355         nt_pw = pdb_get_nt_passwd(sampass);
356         if (!nt_pw) nt_pw_len = 0;
357                 
358         acct_desc = pdb_get_acct_desc(sampass);
359         if (acct_desc) acct_desc_len = strlen(acct_desc) +1;
360         else acct_desc_len = 0;
361
362         workstations = pdb_get_workstations(sampass);
363         if (workstations) workstations_len = strlen(workstations) +1;
364         else workstations_len = 0;
365
366         unknown_str = NULL;
367         unknown_str_len = 0;
368
369         munged_dial = pdb_get_munged_dial(sampass);
370         if (munged_dial) munged_dial_len = strlen(munged_dial) +1;
371         else munged_dial_len = 0;       
372                 
373         /* one time to get the size needed */
374         len = tdb_pack(NULL, 0,  TDB_FORMAT_STRING,
375                 logon_time,
376                 logoff_time,
377                 kickoff_time,
378                 pass_last_set_time,
379                 pass_can_change_time,
380                 pass_must_change_time,
381                 username_len, username,
382                 domain_len, domain,
383                 nt_username_len, nt_username,
384                 fullname_len, fullname,
385                 homedir_len, homedir,
386                 dir_drive_len, dir_drive,
387                 logon_script_len, logon_script,
388                 profile_path_len, profile_path,
389                 acct_desc_len, acct_desc,
390                 workstations_len, workstations,
391                 unknown_str_len, unknown_str,
392                 munged_dial_len, munged_dial,
393                 user_rid,
394                 group_rid,
395                 lm_pw_len, lm_pw,
396                 nt_pw_len, nt_pw,
397                 pdb_get_acct_ctrl(sampass),
398                 pdb_get_unknown_3(sampass),
399                 pdb_get_logon_divs(sampass),
400                 pdb_get_hours_len(sampass),
401                 MAX_HOURS_LEN, pdb_get_hours(sampass),
402                 pdb_get_unknown_5(sampass),
403                 pdb_get_unknown_6(sampass));
404
405
406         /* malloc the space needed */
407         if ( (*buf=(uint8*)malloc(len)) == NULL) {
408                 DEBUG(0,("init_buffer_from_sam: Unable to malloc() memory for buffer!\n"));
409                 return (-1);
410         }
411         
412         /* now for the real call to tdb_pack() */
413         buflen = tdb_pack(*buf, len,  TDB_FORMAT_STRING,
414                 logon_time,
415                 logoff_time,
416                 kickoff_time,
417                 pass_last_set_time,
418                 pass_can_change_time,
419                 pass_must_change_time,
420                 username_len, username,
421                 domain_len, domain,
422                 nt_username_len, nt_username,
423                 fullname_len, fullname,
424                 homedir_len, homedir,
425                 dir_drive_len, dir_drive,
426                 logon_script_len, logon_script,
427                 profile_path_len, profile_path,
428                 acct_desc_len, acct_desc,
429                 workstations_len, workstations,
430                 unknown_str_len, unknown_str,
431                 munged_dial_len, munged_dial,
432                 user_rid,
433                 group_rid,
434                 lm_pw_len, lm_pw,
435                 nt_pw_len, nt_pw,
436                 pdb_get_acct_ctrl(sampass),
437                 pdb_get_unknown_3(sampass),
438                 pdb_get_logon_divs(sampass),
439                 pdb_get_hours_len(sampass),
440                 MAX_HOURS_LEN, pdb_get_hours(sampass),
441                 pdb_get_unknown_5(sampass),
442                 pdb_get_unknown_6(sampass));
443         
444         
445         /* check to make sure we got it correct */
446         if (buflen != len) {
447                 DEBUG(0, ("init_buffer_from_sam: somthing odd is going on here: bufflen (%d) != len (%d) in tdb_pack operations!\n", 
448                           buflen, len));  
449                 /* error */
450                 SAFE_FREE (*buf);
451                 return (-1);
452         }
453
454         return (buflen);
455 }
456
457 /***************************************************************
458  Open the TDB passwd database for SAM account enumeration.
459 ****************************************************************/
460
461 static NTSTATUS tdbsam_setsampwent(struct pdb_methods *my_methods, BOOL update)
462 {
463         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
464         
465         /* Open tdb passwd */
466         if (!(tdb_state->passwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, update?(O_RDWR|O_CREAT):O_RDONLY, 0600)))
467         {
468                 DEBUG(0, ("Unable to open/create TDB passwd\n"));
469                 return NT_STATUS_UNSUCCESSFUL;
470         }
471         
472         tdb_state->key = tdb_firstkey(tdb_state->passwd_tdb);
473
474         return NT_STATUS_OK;
475 }
476
477 static void close_tdb(struct tdbsam_privates *tdb_state) 
478 {
479         if (tdb_state->passwd_tdb) {
480                 tdb_close(tdb_state->passwd_tdb);
481                 tdb_state->passwd_tdb = NULL;
482         }
483 }
484
485 /***************************************************************
486  End enumeration of the TDB passwd list.
487 ****************************************************************/
488
489 static void tdbsam_endsampwent(struct pdb_methods *my_methods)
490 {
491         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
492         close_tdb(tdb_state);
493         
494         DEBUG(7, ("endtdbpwent: closed sam database.\n"));
495 }
496
497 /*****************************************************************
498  Get one SAM_ACCOUNT from the TDB (next in line)
499 *****************************************************************/
500
501 static NTSTATUS tdbsam_getsampwent(struct pdb_methods *my_methods, SAM_ACCOUNT *user)
502 {
503         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
504         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
505         TDB_DATA        data;
506         const char *prefix = USERPREFIX;
507         int  prefixlen = strlen (prefix);
508
509
510         if (user==NULL) {
511                 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
512                 return nt_status;
513         }
514
515         /* skip all non-USER entries (eg. RIDs) */
516         while ((tdb_state->key.dsize != 0) && (strncmp(tdb_state->key.dptr, prefix, prefixlen)))
517                 /* increment to next in line */
518                 tdb_state->key = tdb_nextkey(tdb_state->passwd_tdb, tdb_state->key);
519
520         /* do we have an valid iteration pointer? */
521         if(tdb_state->passwd_tdb == NULL) {
522                 DEBUG(0,("pdb_get_sampwent: Bad TDB Context pointer.\n"));
523                 return nt_status;
524         }
525
526         data = tdb_fetch(tdb_state->passwd_tdb, tdb_state->key);
527         if (!data.dptr) {
528                 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
529                 return nt_status;
530         }
531   
532         /* unpack the buffer */
533         if (!init_sam_from_buffer(tdb_state, user, data.dptr, data.dsize)) {
534                 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
535                 SAFE_FREE(data.dptr);
536                 return nt_status;
537         }
538         SAFE_FREE(data.dptr);
539         
540         /* increment to next in line */
541         tdb_state->key = tdb_nextkey(tdb_state->passwd_tdb, tdb_state->key);
542
543         return NT_STATUS_OK;
544 }
545
546 /******************************************************************
547  Lookup a name in the SAM TDB
548 ******************************************************************/
549
550 static NTSTATUS tdbsam_getsampwnam (struct pdb_methods *my_methods, SAM_ACCOUNT *user, const char *sname)
551 {
552         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
553         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
554         TDB_CONTEXT     *pwd_tdb;
555         TDB_DATA        data, key;
556         fstring         keystr;
557         fstring         name;
558
559         if (user==NULL) {
560                 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
561                 return nt_status;
562         }
563
564         
565         /* Data is stored in all lower-case */
566         fstrcpy(name, sname);
567         strlower(name);
568
569         /* set search key */
570         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
571         key.dptr = keystr;
572         key.dsize = strlen(keystr) + 1;
573
574         /* open the accounts TDB */
575         if (!(pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
576                 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
577                 return nt_status;
578         }
579
580         /* get the record */
581         data = tdb_fetch(pwd_tdb, key);
582         if (!data.dptr) {
583                 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
584                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
585                 DEBUGADD(5, (" Key: %s\n", keystr));
586                 tdb_close(pwd_tdb);
587                 return nt_status;
588         }
589   
590         /* unpack the buffer */
591         if (!init_sam_from_buffer(tdb_state, user, data.dptr, data.dsize)) {
592                 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
593                 SAFE_FREE(data.dptr);
594                 tdb_close(pwd_tdb);
595                 return nt_status;
596         }
597         SAFE_FREE(data.dptr);
598
599         /* no further use for database, close it now */
600         tdb_close(pwd_tdb);
601         
602         return NT_STATUS_OK;
603 }
604
605 /***************************************************************************
606  Search by rid
607  **************************************************************************/
608
609 static NTSTATUS tdbsam_getsampwrid (struct pdb_methods *my_methods, SAM_ACCOUNT *user, uint32 rid)
610 {
611         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
612         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
613         TDB_CONTEXT             *pwd_tdb;
614         TDB_DATA                data, key;
615         fstring                 keystr;
616         fstring                 name;
617         
618         if (user==NULL) {
619                 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
620                 return nt_status;
621         }
622
623         /* set search key */
624         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
625         key.dptr = keystr;
626         key.dsize = strlen (keystr) + 1;
627
628         /* open the accounts TDB */
629         if (!(pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
630                 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
631                 return nt_status;
632         }
633
634         /* get the record */
635         data = tdb_fetch (pwd_tdb, key);
636         if (!data.dptr) {
637                 DEBUG(5,("pdb_getsampwrid (TDB): error looking up RID %d by key %s.\n", rid, keystr));
638                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
639                 tdb_close (pwd_tdb);
640                 return nt_status;
641         }
642
643         fstrcpy(name, data.dptr);
644         SAFE_FREE(data.dptr);
645         
646         tdb_close (pwd_tdb);
647         
648         return tdbsam_getsampwnam (my_methods, user, name);
649 }
650
651 static NTSTATUS tdbsam_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
652 {
653         uint32 rid;
654         if (!sid_peek_check_rid(get_global_sam_sid(), sid, &rid))
655                 return NT_STATUS_UNSUCCESSFUL;
656         return tdbsam_getsampwrid(my_methods, user, rid);
657 }
658
659 /***************************************************************************
660  Delete a SAM_ACCOUNT
661 ****************************************************************************/
662
663 static NTSTATUS tdbsam_delete_sam_account(struct pdb_methods *my_methods, SAM_ACCOUNT *sam_pass)
664 {
665         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
666         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
667         TDB_CONTEXT     *pwd_tdb;
668         TDB_DATA        key;
669         fstring         keystr;
670         uint32          rid;
671         fstring         name;
672         
673         fstrcpy(name, pdb_get_username(sam_pass));
674         strlower(name);
675         
676         /* open the TDB */
677         if (!(pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDWR, 0600))) {
678                 DEBUG(0, ("Unable to open TDB passwd!"));
679                 return nt_status;
680         }
681   
682         /* set the search key */
683         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
684         key.dptr = keystr;
685         key.dsize = strlen (keystr) + 1;
686         
687         rid = pdb_get_user_rid(sam_pass);
688
689         /* it's outaa here!  8^) */
690         if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
691                 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
692                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
693                 tdb_close(pwd_tdb); 
694                 return nt_status;
695         }       
696
697         /* delete also the RID key */
698
699         /* set the search key */
700         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
701         key.dptr = keystr;
702         key.dsize = strlen (keystr) + 1;
703
704         /* it's outaa here!  8^) */
705         if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
706                 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
707                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
708                 tdb_close(pwd_tdb); 
709                 return nt_status;
710         }
711         
712         tdb_close(pwd_tdb);
713         
714         return NT_STATUS_OK;
715 }
716
717 /***************************************************************************
718  Update the TDB SAM
719 ****************************************************************************/
720
721 static BOOL tdb_update_sam(struct pdb_methods *my_methods, SAM_ACCOUNT* newpwd, int flag)
722 {
723         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
724         TDB_CONTEXT     *pwd_tdb = NULL;
725         TDB_DATA        key, data;
726         uint8           *buf = NULL;
727         fstring         keystr;
728         fstring         name;
729         BOOL            ret = True;
730         uint32          user_rid;
731         BOOL            tdb_ret;
732
733         /* invalidate the existing TDB iterator if it is open */
734         if (tdb_state->passwd_tdb) {
735                 tdb_close(tdb_state->passwd_tdb);
736                 tdb_state->passwd_tdb = NULL;
737         }
738
739         /* open the account TDB passwd*/
740         pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
741         if (!pwd_tdb)
742         {
743                 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
744                 return False;
745         }
746
747         if (!pdb_get_group_rid(newpwd)) {
748                 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a primary group RID\n",pdb_get_username(newpwd)));
749                 ret = False;
750                 goto done;
751         }
752
753         /* if flag == TDB_INSERT then make up a new RID else throw an error. */
754         if (!(user_rid = pdb_get_user_rid(newpwd))) {
755                 if ((flag & TDB_INSERT) && tdb_state->permit_non_unix_accounts) {
756                         uint32 lowrid, highrid;
757                         if (!idmap_get_free_rid_range(&lowrid, &highrid)) {
758                                 /* should never happen */
759                                 DEBUG(0, ("tdbsam: something messed up, no high/low rids but nua enabled ?!\n"));
760                                 ret = False;
761                                 goto done;
762                         }
763                         user_rid = lowrid;
764                         tdb_ret = tdb_change_uint32_atomic(pwd_tdb, "RID_COUNTER", &user_rid, RID_MULTIPLIER);
765                         if (!tdb_ret) {
766                                 ret = False;
767                                 goto done;
768                         }
769                         if (user_rid > highrid) {
770                                 DEBUG(0, ("tdbsam: no NUA rids available, cannot add user %s!\n", pdb_get_username(newpwd)));
771                                 ret = False;
772                                 goto done;
773                         }
774                         if (!pdb_set_user_sid_from_rid(newpwd, user_rid, PDB_CHANGED)) {
775                                 DEBUG(0, ("tdbsam: not able to set new allocated user RID into sam account!\n"));
776                                 ret = False;
777                                 goto done;
778                         }
779                 } else {
780                         DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a RID\n",pdb_get_username(newpwd)));
781                         ret = False;
782                         goto done;
783                 }
784         }
785
786         /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
787         if ((data.dsize=init_buffer_from_sam (tdb_state, &buf, newpwd)) == -1) {
788                 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
789                 ret = False;
790                 goto done;
791         }
792         data.dptr = buf;
793
794         fstrcpy(name, pdb_get_username(newpwd));
795         strlower(name);
796         
797         DEBUG(5, ("Storing %saccount %s with RID %d\n", flag == TDB_INSERT ? "(new) " : "", name, user_rid));
798
799         /* setup the USER index key */
800         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
801         key.dptr = keystr;
802         key.dsize = strlen(keystr) + 1;
803
804         /* add the account */
805         if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
806                 DEBUG(0, ("Unable to modify passwd TDB!"));
807                 DEBUGADD(0, (" Error: %s", tdb_errorstr(pwd_tdb)));
808                 DEBUGADD(0, (" occured while storing the main record (%s)\n", keystr));
809                 ret = False;
810                 goto done;
811         }
812         
813         /* setup RID data */
814         data.dsize = strlen(name) + 1;
815         data.dptr = name;
816
817         /* setup the RID index key */
818         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, user_rid);
819         key.dptr = keystr;
820         key.dsize = strlen (keystr) + 1;
821         
822         /* add the reference */
823         if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
824                 DEBUG(0, ("Unable to modify TDB passwd !"));
825                 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
826                 DEBUGADD(0, (" occured while storing the RID index (%s)\n", keystr));
827                 ret = False;
828                 goto done;
829         }
830
831 done:   
832         /* cleanup */
833         tdb_close (pwd_tdb);
834         SAFE_FREE(buf);
835         
836         return (ret);   
837 }
838
839 #if 0
840 /***************************************************************************
841  Allocates a new RID and returns it to the caller as a domain sid
842
843  NOTE: Use carefullt, do not waste RIDs they are a limited resource!
844                                                         - SSS
845  ***************************************************************************/
846
847 static NTSTATUS tdbsam_get_next_sid (struct pdb_methods *my_methods, DOM_SID *sid)
848 {
849         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
850         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
851         TDB_CONTEXT     *pwd_tdb;
852         uint32          rid;
853
854         if (sid == NULL) {
855                 return NT_STATUS_INVALID_PARAMETER;
856         }
857         
858         pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
859         if (!pwd_tdb)
860         {
861                 DEBUG(0, ("tdbsam_get_next_sid: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
862                 return NT_STATUS_UNSUCCESSFUL;
863         }
864
865         rid = BASE_RID;
866         if (tdb_change_uint32_atomic(pwd_tdb, "RID_COUNTER", &rid, 1)) {
867
868                 sid_copy(sid, get_global_sam_sid());
869                 if (!sid_append_rid(sid, rid)) {
870                         goto done;
871                 }
872                 
873                 ret = NT_STATUS_OK;
874         }
875
876 done:
877         tdb_close (pwd_tdb);
878         return ret;
879 }
880 #endif
881
882 /***************************************************************************
883  Modifies an existing SAM_ACCOUNT
884 ****************************************************************************/
885
886 static NTSTATUS tdbsam_update_sam_account (struct pdb_methods *my_methods, SAM_ACCOUNT *newpwd)
887 {
888         if (tdb_update_sam(my_methods, newpwd, TDB_MODIFY))
889                 return NT_STATUS_OK;
890         else
891                 return NT_STATUS_UNSUCCESSFUL;
892 }
893
894 /***************************************************************************
895  Adds an existing SAM_ACCOUNT
896 ****************************************************************************/
897
898 static NTSTATUS tdbsam_add_sam_account (struct pdb_methods *my_methods, SAM_ACCOUNT *newpwd)
899 {
900         if (tdb_update_sam(my_methods, newpwd, TDB_INSERT))
901                 return NT_STATUS_OK;
902         else
903                 return NT_STATUS_UNSUCCESSFUL;
904 }
905
906 static void free_private_data(void **vp) 
907 {
908         struct tdbsam_privates **tdb_state = (struct tdbsam_privates **)vp;
909         close_tdb(*tdb_state);
910         *tdb_state = NULL;
911
912         /* No need to free any further, as it is talloc()ed */
913 }
914
915
916 NTSTATUS pdb_init_tdbsam(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
917 {
918         NTSTATUS nt_status;
919         struct tdbsam_privates *tdb_state;
920         uint32 low_nua_uid, high_nua_uid;
921
922         if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
923                 return nt_status;
924         }
925
926         (*pdb_method)->name = "tdbsam";
927
928         (*pdb_method)->setsampwent = tdbsam_setsampwent;
929         (*pdb_method)->endsampwent = tdbsam_endsampwent;
930         (*pdb_method)->getsampwent = tdbsam_getsampwent;
931         (*pdb_method)->getsampwnam = tdbsam_getsampwnam;
932         (*pdb_method)->getsampwsid = tdbsam_getsampwsid;
933         (*pdb_method)->add_sam_account = tdbsam_add_sam_account;
934         (*pdb_method)->update_sam_account = tdbsam_update_sam_account;
935         (*pdb_method)->delete_sam_account = tdbsam_delete_sam_account;
936
937         tdb_state = talloc_zero(pdb_context->mem_ctx, sizeof(struct tdbsam_privates));
938
939         if (!tdb_state) {
940                 DEBUG(0, ("talloc() failed for tdbsam private_data!\n"));
941                 return NT_STATUS_NO_MEMORY;
942         }
943
944         if (location) {
945                 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, location);
946         } else {
947                 pstring tdbfile;
948                 get_private_directory(tdbfile);
949                 pstrcat(tdbfile, "/");
950                 pstrcat(tdbfile, PASSDB_FILE_NAME);
951                 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, tdbfile);
952         }
953
954         (*pdb_method)->private_data = tdb_state;
955
956         (*pdb_method)->free_private_data = free_private_data;
957
958         if (lp_idmap_uid(&low_nua_uid, &high_nua_uid)) {
959                 DEBUG(0, ("idmap uid range defined, non unix accounts enabled\n"));
960
961                 tdb_state->permit_non_unix_accounts = True;
962
963                 tdb_state->low_nua_rid=fallback_pdb_uid_to_user_rid(low_nua_uid);
964
965                 tdb_state->high_nua_rid=fallback_pdb_uid_to_user_rid(high_nua_uid);
966
967         } else {
968                 tdb_state->algorithmic_rids = True;
969         }
970
971         return NT_STATUS_OK;
972 }
973
974 int pdb_tdbsam_init(void)
975 {
976         smb_register_passdb(PASSDB_INTERFACE_VERSION, "tdbsam", pdb_init_tdbsam);
977         return True;
978 }
979