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