trying to fix some linking issues with Solaris LDAP libraries.
[Samba.git] / source / passdb / pdb_tdb.c
blob58143977e08aeeb899ded5714e14b310d5b67160
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 *
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 675
20 * Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
25 #ifdef WITH_TDB_SAM
27 #define PDB_VERSION "20010830"
28 #define PASSDB_FILE_NAME "passdb.tdb"
29 #define TDB_FORMAT_STRING "ddddddBBBBBBBBBBBBddBBwdwdBdd"
30 #define USERPREFIX "USER_"
31 #define RIDPREFIX "RID_"
33 extern int DEBUGLEVEL;
34 extern pstring samlogon_user;
35 extern BOOL sam_logon_in_ssb;
37 struct tdb_enum_info {
38 TDB_CONTEXT *passwd_tdb;
39 TDB_DATA key;
42 static struct tdb_enum_info global_tdb_ent;
43 /*static SAM_ACCOUNT global_sam_pass;*/
45 /**********************************************************************
46 Intialize a SAM_ACCOUNT struct from a BYTE buffer of size len
47 *********************************************************************/
49 static BOOL init_sam_from_buffer (SAM_ACCOUNT *sampass, uint8 *buf, uint32 buflen)
52 /* times are stored as 32bit integer
53 take care on system with 64bit wide time_t
54 --SSS */
55 uint32 logon_time,
56 logoff_time,
57 kickoff_time,
58 pass_last_set_time,
59 pass_can_change_time,
60 pass_must_change_time;
61 char *username;
62 char *domain;
63 char *nt_username;
64 char *dir_drive;
65 char *unknown_str;
66 char *munged_dial;
67 char *fullname;
68 char *homedir;
69 char *logon_script;
70 char *profile_path;
71 char *acct_desc;
72 char *workstations;
73 uint32 username_len, domain_len, nt_username_len,
74 dir_drive_len, unknown_str_len, munged_dial_len,
75 fullname_len, homedir_len, logon_script_len,
76 profile_path_len, acct_desc_len, workstations_len;
78 uint32 /* uid, gid,*/ user_rid, group_rid, unknown_3, hours_len, unknown_5, unknown_6;
79 uint16 acct_ctrl, logon_divs;
80 uint8 *hours;
81 static uint8 *lm_pw_ptr, *nt_pw_ptr;
82 uint32 len = 0;
83 uint32 lmpwlen, ntpwlen, hourslen;
84 BOOL ret = True;
85 BOOL setflag;
86 struct passwd *pw;
87 uid_t uid;
88 gid_t gid;
90 if(sampass == NULL || buf == NULL) {
91 DEBUG(0, ("init_sam_from_buffer: NULL parameters found!\n"));
92 return False;
95 /* unpack the buffer into variables */
96 len = tdb_unpack (buf, buflen, TDB_FORMAT_STRING,
97 &logon_time,
98 &logoff_time,
99 &kickoff_time,
100 &pass_last_set_time,
101 &pass_can_change_time,
102 &pass_must_change_time,
103 &username_len, &username,
104 &domain_len, &domain,
105 &nt_username_len, &nt_username,
106 &fullname_len, &fullname,
107 &homedir_len, &homedir,
108 &dir_drive_len, &dir_drive,
109 &logon_script_len, &logon_script,
110 &profile_path_len, &profile_path,
111 &acct_desc_len, &acct_desc,
112 &workstations_len, &workstations,
113 &unknown_str_len, &unknown_str,
114 &munged_dial_len, &munged_dial,
115 &user_rid,
116 &group_rid,
117 &lmpwlen, &lm_pw_ptr,
118 &ntpwlen, &nt_pw_ptr,
119 &acct_ctrl,
120 &unknown_3,
121 &logon_divs,
122 &hours_len,
123 &hourslen, &hours,
124 &unknown_5,
125 &unknown_6);
127 if (len == -1) {
128 ret = False;
129 goto done;
132 /* validate the account and fill in UNIX uid and gid. Standard
133 * getpwnam() is used instead of Get_Pwnam() as we do not need
134 * to try case permutations
136 if (!username || !(pw=getpwnam(username))) {
137 DEBUG(0,("tdb_sam: getpwnam(%s) return NULL. User does not exist!\n",
138 username?username:"NULL"));
139 ret = False;
140 goto done;
143 uid = pw->pw_uid;
144 gid = pw->pw_gid;
145 pdb_set_uid(sampass, uid);
146 pdb_set_gid(sampass, gid);
148 pdb_set_logon_time(sampass, logon_time);
149 pdb_set_logoff_time(sampass, logoff_time);
150 pdb_set_kickoff_time(sampass, kickoff_time);
151 pdb_set_pass_can_change_time(sampass, pass_can_change_time);
152 pdb_set_pass_must_change_time(sampass, pass_must_change_time);
153 pdb_set_pass_last_set_time(sampass, pass_last_set_time);
155 pdb_set_username (sampass, username_len?username:NULL);
156 pdb_set_domain (sampass, domain_len?domain:NULL);
157 pdb_set_nt_username (sampass, nt_username_len?nt_username:NULL);
158 pdb_set_fullname (sampass, fullname_len?fullname:NULL);
160 if (homedir) setflag = True;
161 else {
162 setflag = False;
163 homedir = strdup(lp_logon_home());
164 if(!homedir) { ret = False; goto done; }
165 standard_sub_advanced(-1, username, "", gid, homedir);
166 DEBUG(5,("Home directory set back to %s\n", homedir));
168 pdb_set_homedir(sampass, homedir, setflag);
170 if (dir_drive) setflag = True;
171 else {
172 setflag = False;
173 dir_drive = strdup(lp_logon_drive());
174 if(!dir_drive) { ret = False; goto done; }
175 standard_sub_advanced(-1, username, "", gid, dir_drive);
176 DEBUG(5,("Home directory set back to %s\n", dir_drive));
178 pdb_set_dir_drive(sampass, dir_drive, setflag);
180 if (logon_script) setflag = True;
181 else {
182 setflag = False;
183 logon_script = strdup(lp_logon_script());
184 if(!logon_script) { ret = False; goto done; }
185 standard_sub_advanced(-1, username, "", gid, logon_script);
186 DEBUG(5,("Home directory set back to %s\n", logon_script));
188 pdb_set_logon_script(sampass, logon_script, setflag);
190 if (profile_path) setflag = True;
191 else {
192 setflag = False;
193 profile_path = strdup(lp_logon_path());
194 if(!profile_path) { ret = False; goto done; }
195 standard_sub_advanced(-1, username, "", gid, profile_path);
196 DEBUG(5,("Home directory set back to %s\n", profile_path));
198 pdb_set_profile_path(sampass, profile_path, setflag);
200 pdb_set_acct_desc (sampass, acct_desc);
201 pdb_set_workstations (sampass, workstations);
202 pdb_set_munged_dial (sampass, munged_dial);
203 if (!pdb_set_lanman_passwd(sampass, lm_pw_ptr)) {
204 ret = False;
205 goto done;
207 if (!pdb_set_nt_passwd(sampass, nt_pw_ptr)) {
208 ret = False;
209 goto done;
212 /*pdb_set_uid(sampass, uid);
213 pdb_set_gid(sampass, gid);*/
214 pdb_set_user_rid(sampass, user_rid);
215 pdb_set_group_rid(sampass, group_rid);
216 pdb_set_unknown_3(sampass, unknown_3);
217 pdb_set_hours_len(sampass, hours_len);
218 pdb_set_unknown_5(sampass, unknown_5);
219 pdb_set_unknown_6(sampass, unknown_6);
220 pdb_set_acct_ctrl(sampass, acct_ctrl);
221 pdb_set_logon_divs(sampass, logon_divs);
222 pdb_set_hours(sampass, hours);
224 done:
226 SAFE_FREE(username);
227 SAFE_FREE(domain);
228 SAFE_FREE(nt_username);
229 SAFE_FREE(fullname);
230 SAFE_FREE(homedir);
231 SAFE_FREE(dir_drive);
232 SAFE_FREE(logon_script);
233 SAFE_FREE(profile_path);
234 SAFE_FREE(acct_desc);
235 SAFE_FREE(workstations);
236 SAFE_FREE(munged_dial);
238 return ret;
241 /**********************************************************************
242 Intialize a BYTE buffer from a SAM_ACCOUNT struct
243 *********************************************************************/
244 static uint32 init_buffer_from_sam (uint8 **buf, SAM_ACCOUNT *sampass)
246 size_t len, buflen;
248 /* times are stored as 32bit integer
249 take care on system with 64bit wide time_t
250 --SSS */
251 uint32 logon_time,
252 logoff_time,
253 kickoff_time,
254 pass_last_set_time,
255 pass_can_change_time,
256 pass_must_change_time;
257 char *username;
258 char *domain;
259 char *nt_username;
260 char *dir_drive;
261 char *unknown_str;
262 char *munged_dial;
263 char *fullname;
264 char *homedir;
265 char *logon_script;
266 char *profile_path;
267 char *acct_desc;
268 char *workstations;
269 uint32 username_len, domain_len, nt_username_len,
270 dir_drive_len, unknown_str_len, munged_dial_len,
271 fullname_len, homedir_len, logon_script_len,
272 profile_path_len, acct_desc_len, workstations_len;
274 const uint8 *lm_pw;
275 const uint8 *nt_pw;
276 uint32 lm_pw_len = 16;
277 uint32 nt_pw_len = 16;
279 /* do we have a valid SAM_ACCOUNT pointer? */
280 if (sampass == NULL) {
281 DEBUG(0, ("init_buffer_from_sam: SAM_ACCOUNT is NULL!\n"));
282 return -1;
285 *buf = NULL;
286 buflen = 0;
288 logon_time = (uint32)pdb_get_logon_time(sampass);
289 logoff_time = (uint32)pdb_get_logoff_time(sampass);
290 kickoff_time = (uint32)pdb_get_kickoff_time(sampass);
291 pass_can_change_time = (uint32)pdb_get_pass_can_change_time(sampass);
292 pass_must_change_time = (uint32)pdb_get_pass_must_change_time(sampass);
293 pass_last_set_time = (uint32)pdb_get_pass_last_set_time(sampass);
296 username = pdb_get_username(sampass);
297 if (username) username_len = strlen(username) +1;
298 else username_len = 0;
300 domain = pdb_get_domain(sampass);
301 if (domain) domain_len = strlen(domain) +1;
302 else domain_len = 0;
304 nt_username = pdb_get_nt_username(sampass);
305 if (nt_username) nt_username_len = strlen(nt_username) +1;
306 else nt_username_len = 0;
308 fullname = pdb_get_fullname(sampass);
309 if (fullname) fullname_len = strlen(fullname) +1;
310 else fullname_len = 0;
313 * Only updates fields which have been set (not defaults from smb.conf)
316 if (IS_SAM_SET(sampass, FLAG_SAM_DRIVE)) dir_drive = pdb_get_dirdrive(sampass);
317 else dir_drive = NULL;
318 if (dir_drive) dir_drive_len = strlen(dir_drive) +1;
319 else dir_drive_len = 0;
321 if (IS_SAM_SET(sampass, FLAG_SAM_SMBHOME)) homedir = pdb_get_homedir(sampass);
322 else homedir = NULL;
323 if (homedir) homedir_len = strlen(homedir) +1;
324 else homedir_len = 0;
326 if (IS_SAM_SET(sampass, FLAG_SAM_LOGONSCRIPT)) logon_script = pdb_get_logon_script(sampass);
327 else logon_script = NULL;
328 if (logon_script) logon_script_len = strlen(logon_script) +1;
329 else logon_script_len = 0;
331 if (IS_SAM_SET(sampass, FLAG_SAM_PROFILE)) profile_path = pdb_get_profile_path(sampass);
332 else profile_path = NULL;
333 if (profile_path) profile_path_len = strlen(profile_path) +1;
334 else profile_path_len = 0;
336 lm_pw = pdb_get_lanman_passwd(sampass);
337 if (!lm_pw) lm_pw_len = 0;
339 nt_pw = pdb_get_nt_passwd(sampass);
340 if (!nt_pw) nt_pw_len = 0;
342 acct_desc = pdb_get_acct_desc(sampass);
343 if (acct_desc) acct_desc_len = strlen(acct_desc) +1;
344 else acct_desc_len = 0;
346 workstations = pdb_get_workstations(sampass);
347 if (workstations) workstations_len = strlen(workstations) +1;
348 else workstations_len = 0;
350 unknown_str = NULL;
351 unknown_str_len = 0;
353 munged_dial = pdb_get_munged_dial(sampass);
354 if (munged_dial) munged_dial_len = strlen(munged_dial) +1;
355 else munged_dial_len = 0;
357 /* one time to get the size needed */
358 len = tdb_pack(NULL, 0, TDB_FORMAT_STRING,
359 logon_time,
360 logoff_time,
361 kickoff_time,
362 pass_last_set_time,
363 pass_can_change_time,
364 pass_must_change_time,
365 username_len, username,
366 domain_len, domain,
367 nt_username_len, nt_username,
368 fullname_len, fullname,
369 homedir_len, homedir,
370 dir_drive_len, dir_drive,
371 logon_script_len, logon_script,
372 profile_path_len, profile_path,
373 acct_desc_len, acct_desc,
374 workstations_len, workstations,
375 unknown_str_len, unknown_str,
376 munged_dial_len, munged_dial,
377 pdb_get_user_rid(sampass),
378 pdb_get_group_rid(sampass),
379 lm_pw_len, lm_pw,
380 nt_pw_len, nt_pw,
381 pdb_get_acct_ctrl(sampass),
382 pdb_get_unknown3(sampass),
383 pdb_get_logon_divs(sampass),
384 pdb_get_hours_len(sampass),
385 MAX_HOURS_LEN, pdb_get_hours(sampass),
386 pdb_get_unknown5(sampass),
387 pdb_get_unknown6(sampass));
390 /* malloc the space needed */
391 if ( (*buf=(uint8*)malloc(len)) == NULL) {
392 DEBUG(0,("init_buffer_from_sam: Unable to malloc() memory for buffer!\n"));
393 return (-1);
396 /* now for the real call to tdb_pack() */
397 buflen = tdb_pack(*buf, len, TDB_FORMAT_STRING,
398 logon_time,
399 logoff_time,
400 kickoff_time,
401 pass_last_set_time,
402 pass_can_change_time,
403 pass_must_change_time,
404 username_len, username,
405 domain_len, domain,
406 nt_username_len, nt_username,
407 fullname_len, fullname,
408 homedir_len, homedir,
409 dir_drive_len, dir_drive,
410 logon_script_len, logon_script,
411 profile_path_len, profile_path,
412 acct_desc_len, acct_desc,
413 workstations_len, workstations,
414 unknown_str_len, unknown_str,
415 munged_dial_len, munged_dial,
416 pdb_get_user_rid(sampass),
417 pdb_get_group_rid(sampass),
418 lm_pw_len, lm_pw,
419 nt_pw_len, nt_pw,
420 pdb_get_acct_ctrl(sampass),
421 pdb_get_unknown3(sampass),
422 pdb_get_logon_divs(sampass),
423 pdb_get_hours_len(sampass),
424 MAX_HOURS_LEN, pdb_get_hours(sampass),
425 pdb_get_unknown5(sampass),
426 pdb_get_unknown6(sampass));
429 /* check to make sure we got it correct */
430 if (buflen != len) {
431 /* error */
432 SAFE_FREE (*buf);
433 return (-1);
436 return (buflen);
439 /***************************************************************
440 Open the TDB passwd database for SAM account enumeration.
441 ****************************************************************/
443 BOOL pdb_setsampwent(BOOL update)
445 pstring tdbfile;
447 get_private_directory(tdbfile);
448 pstrcat(tdbfile, "/");
449 pstrcat(tdbfile, PASSDB_FILE_NAME);
451 /* Open tdb passwd */
452 if (!(global_tdb_ent.passwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, update?(O_RDWR|O_CREAT):O_RDONLY, 0600)))
454 DEBUG(0, ("Unable to open/create TDB passwd\n"));
455 return False;
458 global_tdb_ent.key = tdb_firstkey(global_tdb_ent.passwd_tdb);
460 return True;
463 /***************************************************************
464 End enumeration of the TDB passwd list.
465 ****************************************************************/
467 void pdb_endsampwent(void)
469 if (global_tdb_ent.passwd_tdb) {
470 tdb_close(global_tdb_ent.passwd_tdb);
471 global_tdb_ent.passwd_tdb = NULL;
474 DEBUG(7, ("endtdbpwent: closed sam database.\n"));
477 /*****************************************************************
478 Get one SAM_ACCOUNT from the TDB (next in line)
479 *****************************************************************/
481 BOOL pdb_getsampwent(SAM_ACCOUNT *user)
483 TDB_DATA data;
484 struct passwd *pw;
485 uid_t uid;
486 gid_t gid;
487 char *prefix = USERPREFIX;
488 int prefixlen = strlen (prefix);
490 if (user==NULL) {
491 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
492 return False;
495 /* skip all non-USER entries (eg. RIDS) */
496 while ((global_tdb_ent.key.dsize != 0) && (strncmp(global_tdb_ent.key.dptr, prefix, prefixlen)))
497 /* increment to next in line */
498 global_tdb_ent.key = tdb_nextkey(global_tdb_ent.passwd_tdb, global_tdb_ent.key);
500 /* do we have an valid interation pointer? */
501 if(global_tdb_ent.passwd_tdb == NULL) {
502 DEBUG(0,("pdb_get_sampwent: Bad TDB Context pointer.\n"));
503 return False;
506 data = tdb_fetch(global_tdb_ent.passwd_tdb, global_tdb_ent.key);
507 if (!data.dptr) {
508 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
509 return False;
512 /* unpack the buffer */
513 if (!init_sam_from_buffer(user, data.dptr, data.dsize)) {
514 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
515 SAFE_FREE(data.dptr);
516 return False;
518 SAFE_FREE(data.dptr);
520 /* increment to next in line */
521 global_tdb_ent.key = tdb_nextkey(global_tdb_ent.passwd_tdb, global_tdb_ent.key);
523 return True;
526 /******************************************************************
527 Lookup a name in the SAM TDB
528 ******************************************************************/
530 BOOL pdb_getsampwnam (SAM_ACCOUNT *user, char *sname)
532 TDB_CONTEXT *pwd_tdb;
533 TDB_DATA data, key;
534 fstring keystr;
535 struct passwd *pw;
536 pstring tdbfile;
537 fstring name;
538 uid_t uid;
539 gid_t gid;
542 if (user==NULL) {
543 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
544 return False;
547 /* Data is stored in all lower-case */
548 fstrcpy(name, sname);
549 strlower(name);
551 get_private_directory(tdbfile);
552 pstrcat(tdbfile, "/");
553 pstrcat(tdbfile, PASSDB_FILE_NAME);
555 /* set search key */
556 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
557 key.dptr = keystr;
558 key.dsize = strlen(keystr) + 1;
560 /* open the accounts TDB */
561 if (!(pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
562 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd!\n"));
563 return False;
566 /* get the record */
567 data = tdb_fetch(pwd_tdb, key);
568 if (!data.dptr) {
569 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
570 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
571 tdb_close(pwd_tdb);
572 return False;
575 /* unpack the buffer */
576 if (!init_sam_from_buffer(user, data.dptr, data.dsize)) {
577 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
578 SAFE_FREE(data.dptr);
579 tdb_close(pwd_tdb);
580 return False;
582 SAFE_FREE(data.dptr);
584 /* no further use for database, close it now */
585 tdb_close(pwd_tdb);
587 return True;
590 /***************************************************************************
591 Search by rid
592 **************************************************************************/
594 BOOL pdb_getsampwrid (SAM_ACCOUNT *user, uint32 rid)
596 TDB_CONTEXT *pwd_tdb;
597 TDB_DATA data, key;
598 fstring keystr;
599 pstring tdbfile;
600 fstring name;
602 if (user==NULL) {
603 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
604 return False;
607 get_private_directory(tdbfile);
608 pstrcat(tdbfile, "/");
609 pstrcat(tdbfile, PASSDB_FILE_NAME);
611 /* set search key */
612 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
613 key.dptr = keystr;
614 key.dsize = strlen (keystr) + 1;
616 /* open the accounts TDB */
617 if (!(pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
618 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
619 return False;
622 /* get the record */
623 data = tdb_fetch (pwd_tdb, key);
624 if (!data.dptr) {
625 DEBUG(5,("pdb_getsampwrid (TDB): error fetching database.\n"));
626 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
627 tdb_close (pwd_tdb);
628 return False;
631 fstrcpy (name, data.dptr);
632 SAFE_FREE(data.dptr);
634 tdb_close (pwd_tdb);
636 return pdb_getsampwnam (user, name);
639 /***************************************************************************
640 Delete a SAM_ACCOUNT
641 ****************************************************************************/
643 BOOL pdb_delete_sam_account(char *sname)
645 SAM_ACCOUNT *sam_pass = NULL;
646 TDB_CONTEXT *pwd_tdb;
647 TDB_DATA key, data;
648 fstring keystr;
649 pstring tdbfile;
650 uint32 rid;
651 fstring name;
653 fstrcpy(name, sname);
654 strlower(name);
656 get_private_directory(tdbfile);
657 pstrcat(tdbfile, "/");
658 pstrcat(tdbfile, PASSDB_FILE_NAME);
660 /* open the TDB */
661 if (!(pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDWR, 0600))) {
662 DEBUG(0, ("Unable to open TDB passwd!"));
663 return False;
666 /* set the search key */
667 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
668 key.dptr = keystr;
669 key.dsize = strlen (keystr) + 1;
671 /* get the record */
672 data = tdb_fetch (pwd_tdb, key);
673 if (!data.dptr) {
674 DEBUG(5,("pdb_delete_sam_account (TDB): error fetching database.\n"));
675 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
676 tdb_close (pwd_tdb);
677 return False;
680 /* unpack the buffer */
681 if (!pdb_init_sam (&sam_pass)) {
682 tdb_close (pwd_tdb);
683 return False;
686 if (!init_sam_from_buffer (sam_pass, data.dptr, data.dsize)) {
687 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
688 tdb_close (pwd_tdb);
689 SAFE_FREE(data.dptr);
690 return False;
692 SAFE_FREE(data.dptr);
694 rid = pdb_get_user_rid(sam_pass);
696 pdb_free_sam (sam_pass);
698 /* it's outaa here! 8^) */
699 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
700 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
701 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
702 tdb_close(pwd_tdb);
703 return False;
706 /* delete also the RID key */
708 /* set the search key */
709 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
710 key.dptr = keystr;
711 key.dsize = strlen (keystr) + 1;
713 /* it's outaa here! 8^) */
714 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
715 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
716 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
717 tdb_close(pwd_tdb);
718 return False;
721 tdb_close(pwd_tdb);
723 return True;
726 /***************************************************************************
727 Update the TDB SAM
728 ****************************************************************************/
730 static BOOL tdb_update_sam(SAM_ACCOUNT* newpwd, BOOL override, int flag)
732 TDB_CONTEXT *pwd_tdb = NULL;
733 TDB_DATA key, data;
734 uint8 *buf = NULL;
735 fstring keystr;
736 pstring tdbfile;
737 fstring name;
738 BOOL ret = True;
740 get_private_directory(tdbfile);
741 pstrcat(tdbfile, "/");
742 pstrcat(tdbfile, PASSDB_FILE_NAME);
744 if ( (!newpwd->uid) || (!newpwd->gid) )
745 DEBUG (0,("tdb_update_sam: Storing a SAM_ACCOUNT for [%s] with uid %d and gid %d!\n",
746 newpwd->username, newpwd->uid, newpwd->gid));
748 /* if we don't have a RID, then generate one */
749 if (!newpwd->user_rid)
750 pdb_set_user_rid (newpwd, pdb_uid_to_user_rid (newpwd->uid));
751 if (!newpwd->group_rid)
752 pdb_set_group_rid (newpwd, pdb_gid_to_group_rid (newpwd->gid));
754 /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
755 if ((data.dsize=init_buffer_from_sam (&buf, newpwd)) == -1) {
756 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
757 ret = False;
758 goto done;
760 data.dptr = buf;
762 fstrcpy(name,pdb_get_username(newpwd));
763 strlower(name);
765 /* setup the USER index key */
766 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
767 key.dptr = keystr;
768 key.dsize = strlen (keystr) + 1;
770 /* invalidate the existing TDB iterator if it is open */
771 if (global_tdb_ent.passwd_tdb) {
772 tdb_close(global_tdb_ent.passwd_tdb);
773 global_tdb_ent.passwd_tdb = NULL;
776 /* open the account TDB passwd*/
777 pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
778 if (!pwd_tdb)
780 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd!\n"));
781 return False;
784 /* add the account */
785 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
786 DEBUG(0, ("Unable to modify passwd TDB!"));
787 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
788 ret = False;
789 goto done;
792 /* setup RID data */
793 data.dsize = sizeof(fstring);
794 data.dptr = name;
796 /* setup the RID index key */
797 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, pdb_get_user_rid(newpwd));
798 key.dptr = keystr;
799 key.dsize = strlen (keystr) + 1;
801 /* add the reference */
802 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
803 DEBUG(0, ("Unable to modify TDB passwd !"));
804 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
805 ret = False;
806 goto done;
809 done:
810 /* cleanup */
811 tdb_close (pwd_tdb);
812 SAFE_FREE(buf);
814 return (ret);
817 /***************************************************************************
818 Modifies an existing SAM_ACCOUNT
819 ****************************************************************************/
821 BOOL pdb_update_sam_account (SAM_ACCOUNT *newpwd, BOOL override)
823 return (tdb_update_sam(newpwd, override, TDB_MODIFY));
826 /***************************************************************************
827 Adds an existing SAM_ACCOUNT
828 ****************************************************************************/
830 BOOL pdb_add_sam_account (SAM_ACCOUNT *newpwd)
832 return (tdb_update_sam(newpwd, True, TDB_INSERT));
835 #else
836 /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
837 void samtdb_dummy_function(void) { } /* stop some compilers complaining */
838 #endif /* WITH_TDB_SAM */