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