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