fixes from Simo
[Samba/gbeck.git] / source / passdb / pdb_tdb.c
blob8db8b2e60db5e111db43cfb47a17320800fc734c
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 *
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 675
19 * Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 #ifdef WITH_TDBPWD
26 #define PASSDB_FILE_NAME "/passdb.tdb"
27 #define TDB_FORMAT_STRING "BBBBBBBBBBBBBBBBBBddBBwdwdBdd"
28 #define USERPREFIX "USER_"
29 #define RIDPREFIX "RID_"
31 extern int DEBUGLEVEL;
32 extern pstring samlogon_user;
33 extern BOOL sam_logon_in_ssb;
36 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 *********************************************************************/
48 static BOOL init_sam_from_buffer (SAM_ACCOUNT *sampass, uint8 *buf,
49 uint32 buflen)
51 time_t logon_time,
52 logoff_time,
53 kickoff_time,
54 pass_last_set_time,
55 pass_can_change_time,
56 pass_must_change_time;
57 uint32 time_t_len;
58 char *username;
59 char *domain;
60 char *nt_username;
61 char *dir_drive;
62 char *unknown_str;
63 char *munged_dial;
64 char *fullname;
65 char *homedir;
66 char *logon_script;
67 char *profile_path;
68 char *acct_desc;
69 char *workstations;
70 uint32 username_len, domain_len, nt_username_len,
71 dir_drive_len, unknown_str_len, munged_dial_len,
72 fullname_len, homedir_len, logon_script_len,
73 profile_path_len, acct_desc_len, workstations_len;
75 uint32 /* uid, gid,*/ user_rid, group_rid, unknown_3, hours_len, unknown_5, unknown_6;
76 uint16 acct_ctrl, logon_divs;
77 uint8 *hours;
78 static uint8 *lm_pw_ptr,
79 *nt_pw_ptr;
80 uint32 len = 0;
81 uint32 lmpwlen, ntpwlen, hourslen;
84 /* unpack the buffer into variables */
85 len = tdb_unpack (buf, buflen, TDB_FORMAT_STRING,
86 &time_t_len, &logon_time,
87 &time_t_len, &logoff_time,
88 &time_t_len, &kickoff_time,
89 &time_t_len, &pass_last_set_time,
90 &time_t_len, &pass_can_change_time,
91 &time_t_len, &pass_must_change_time,
92 &username_len, &username,
93 &domain_len, &domain,
94 &nt_username_len, &nt_username,
95 &fullname_len, &fullname,
96 &homedir_len, &homedir,
97 &dir_drive_len, &dir_drive,
98 &logon_script_len, &logon_script,
99 &profile_path_len, &profile_path,
100 &acct_desc_len, &acct_desc,
101 &workstations_len, &workstations,
102 &unknown_str_len, &unknown_str,
103 &munged_dial_len, &munged_dial,
104 &user_rid,
105 &group_rid,
106 &lmpwlen, &lm_pw_ptr,
107 &ntpwlen, &nt_pw_ptr,
108 &acct_ctrl,
109 &unknown_3,
110 &logon_divs,
111 &hours_len,
112 &hourslen, &hours,
113 &unknown_5,
114 &unknown_6);
116 if (len == -1)
117 return False;
119 pdb_set_logon_time(sampass, logon_time);
120 pdb_set_logoff_time(sampass, logoff_time);
121 pdb_set_kickoff_time(sampass, kickoff_time);
122 pdb_set_pass_can_change_time(sampass, pass_can_change_time);
123 pdb_set_pass_must_change_time(sampass, pass_must_change_time);
124 pdb_set_pass_last_set_time(sampass, pass_last_set_time);
126 pdb_set_username (sampass, username_len?username:NULL);
127 pdb_set_domain (sampass, domain_len?domain:NULL);
128 pdb_set_nt_username (sampass, nt_username_len?nt_username:NULL);
129 pdb_set_fullname (sampass, fullname_len?fullname:NULL);
130 pdb_set_homedir (sampass, homedir_len?homedir:NULL);
131 pdb_set_dir_drive (sampass, dir_drive_len?dir_drive:NULL);
132 pdb_set_logon_script (sampass, logon_script_len?logon_script:NULL);
133 pdb_set_profile_path (sampass, profile_path_len?profile_path:NULL);
134 pdb_set_acct_desc (sampass, acct_desc_len?acct_desc:NULL);
135 pdb_set_workstations (sampass, workstations_len?workstations:NULL);
136 pdb_set_munged_dial (sampass, munged_dial_len?munged_dial:NULL);
137 pdb_set_lanman_passwd(sampass, lmpwlen?lm_pw_ptr:NULL);
138 pdb_set_nt_passwd (sampass, ntpwlen?nt_pw_ptr:NULL);
140 /*pdb_set_uid(sampass, uid);
141 pdb_set_gid(sampass, gid);*/
142 pdb_set_user_rid(sampass, user_rid);
143 pdb_set_group_rid(sampass, group_rid);
144 pdb_set_unknown_3(sampass, unknown_3);
145 pdb_set_hours_len(sampass, hours_len);
146 pdb_set_unknown_5(sampass, unknown_5);
147 pdb_set_unknown_6(sampass, unknown_6);
148 pdb_set_acct_ctrl(sampass, acct_ctrl);
149 pdb_set_logons_divs(sampass, logon_divs);
150 pdb_set_hours(sampass, hours);
153 return True;
156 /**********************************************************************
157 Intialize a BYTE buffer from a SAM_ACCOUNT struct
158 *********************************************************************/
159 static uint32 init_buffer_from_sam (uint8 **buf, SAM_ACCOUNT *sampass)
161 size_t len, buflen;
163 time_t logon_time,
164 logoff_time,
165 kickoff_time,
166 pass_last_set_time,
167 pass_can_change_time,
168 pass_must_change_time;
169 char *username;
170 char *domain;
171 char *nt_username;
172 char *dir_drive;
173 char *unknown_str;
174 char *munged_dial;
175 char *fullname;
176 char *homedir;
177 char *logon_script;
178 char *profile_path;
179 char *acct_desc;
180 char *workstations;
181 uint32 username_len, domain_len, nt_username_len,
182 dir_drive_len, unknown_str_len, munged_dial_len,
183 fullname_len, homedir_len, logon_script_len,
184 profile_path_len, acct_desc_len, workstations_len;
186 uint8 *lm_pw;
187 uint8 *nt_pw;
188 uint32 time_t_len = sizeof (time_t);
189 char null_pw[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
191 /* do we have a valid SAM_ACCOUNT pointer? */
192 if (sampass == NULL)
193 return -1;
195 *buf = NULL;
196 buflen = 0;
198 logon_time = pdb_get_logon_time(sampass);
199 logoff_time = pdb_get_logoff_time(sampass);
200 kickoff_time = pdb_get_kickoff_time(sampass);
201 pass_can_change_time = pdb_get_pass_can_change_time(sampass);
202 pass_must_change_time = pdb_get_pass_must_change_time(sampass);
203 pass_last_set_time = pdb_get_pass_last_set_time(sampass);
206 username = pdb_get_username(sampass);
207 if (username) username_len = strlen(username) +1;
208 else username_len = 0;
209 domain = pdb_get_domain(sampass);
210 if (domain) domain_len = strlen(domain) +1;
211 else domain_len = 0;
212 nt_username = pdb_get_nt_username(sampass);
213 if (nt_username) nt_username_len = strlen(nt_username) +1;
214 else nt_username_len = 0;
215 dir_drive = pdb_get_dirdrive(sampass);
216 if (dir_drive) dir_drive_len = strlen(dir_drive) +1;
217 else dir_drive_len = 0;
218 unknown_str = NULL;
219 unknown_str_len = 0;
220 munged_dial = pdb_get_munged_dial(sampass);
221 if (munged_dial) munged_dial_len = strlen(munged_dial) +1;
222 else munged_dial_len = 0;
224 fullname = pdb_get_fullname(sampass);
225 if (fullname) fullname_len = strlen(fullname) +1;
226 else fullname_len = 0;
227 homedir = pdb_get_homedir(sampass);
228 if (homedir) homedir_len = strlen(homedir) +1;
229 else homedir_len = 0;
230 logon_script = pdb_get_logon_script(sampass);
231 if (logon_script) logon_script_len = strlen(logon_script) +1;
232 else logon_script_len = 0;
233 profile_path = pdb_get_profile_path(sampass);
234 if (profile_path) profile_path_len = strlen(profile_path) +1;
235 else profile_path_len = 0;
236 acct_desc = pdb_get_acct_desc(sampass);
237 if (acct_desc) acct_desc_len = strlen(acct_desc) +1;
238 else acct_desc_len = 0;
239 workstations = pdb_get_workstations(sampass);
240 if (workstations) workstations_len = strlen(workstations) +1;
241 else workstations_len = 0;
243 lm_pw = pdb_get_lanman_passwd(sampass);
244 if (!lm_pw) pdb_gethexpwd (null_pw, lm_pw);
246 nt_pw = pdb_get_nt_passwd(sampass);
247 if (!nt_pw) pdb_gethexpwd (null_pw, nt_pw);
250 /* one time to get the size needed */
251 len = tdb_pack(NULL, 0, TDB_FORMAT_STRING,
252 time_t_len, &logon_time,
253 time_t_len, &logoff_time,
254 time_t_len, &kickoff_time,
255 time_t_len, &pass_last_set_time,
256 time_t_len, &pass_can_change_time,
257 time_t_len, &pass_must_change_time,
258 username_len, username,
259 domain_len, domain,
260 nt_username_len, nt_username,
261 fullname_len, fullname,
262 homedir_len, homedir,
263 dir_drive_len, dir_drive,
264 logon_script_len, logon_script,
265 profile_path_len, profile_path,
266 acct_desc_len, acct_desc,
267 workstations_len, workstations,
268 unknown_str_len, unknown_str,
269 munged_dial_len, munged_dial,
270 pdb_get_user_rid(sampass),
271 pdb_get_group_rid(sampass),
272 16, lm_pw,
273 16, nt_pw,
274 pdb_get_acct_ctrl(sampass),
275 pdb_get_unknown3(sampass),
276 pdb_get_logon_divs(sampass),
277 pdb_get_hours_len(sampass),
278 MAX_HOURS_LEN, pdb_get_hours(sampass),
279 pdb_get_unknown5(sampass),
280 pdb_get_unknown6(sampass));
283 /* malloc the space needed */
284 if ( (*buf=(uint8*)malloc(len)) == NULL)
286 DEBUG(0,("init_buffer_from_sam: Unable to malloc() memory for buffer!\n"));
287 return (-1);
290 /* now for the real call to tdb_pack() */
291 /* one time to get the size needed */
292 buflen = tdb_pack(*buf, len, TDB_FORMAT_STRING,
293 time_t_len, &logon_time,
294 time_t_len, &logoff_time,
295 time_t_len, &kickoff_time,
296 time_t_len, &pass_last_set_time,
297 time_t_len, &pass_can_change_time,
298 time_t_len, &pass_must_change_time,
299 username_len, username,
300 domain_len, domain,
301 nt_username_len, nt_username,
302 fullname_len, fullname,
303 homedir_len, homedir,
304 dir_drive_len, dir_drive,
305 logon_script_len, logon_script,
306 profile_path_len, profile_path,
307 acct_desc_len, acct_desc,
308 workstations_len, workstations,
309 unknown_str_len, unknown_str,
310 munged_dial_len, munged_dial,
311 pdb_get_user_rid(sampass),
312 pdb_get_group_rid(sampass),
313 16, lm_pw,
314 16, nt_pw,
315 pdb_get_acct_ctrl(sampass),
316 pdb_get_unknown3(sampass),
317 pdb_get_logon_divs(sampass),
318 pdb_get_hours_len(sampass),
319 MAX_HOURS_LEN, pdb_get_hours(sampass),
320 pdb_get_unknown5(sampass),
321 pdb_get_unknown6(sampass));
324 /* check to make sure we got it correct */
325 if (buflen != len)
327 /* error */
328 free (*buf);
329 return (-1);
332 return (buflen);
335 /***************************************************************
336 Open the TDB passwd database for SAM account enumeration.
337 ****************************************************************/
338 BOOL pdb_setsampwent(BOOL update)
340 pstring tdbfile;
342 pstrcpy (tdbfile, lp_private_dir());
343 pstrcat (tdbfile, PASSDB_FILE_NAME);
345 /* Open tdb passwd */
346 if (!(global_tdb_ent.passwd_tdb = tdb_open(tdbfile, 0, 0, update ? O_RDWR : O_RDONLY, 0600)))
348 DEBUG(0, ("Unable to open TDB passwd, trying create new!\n"));
349 if (!(global_tdb_ent.passwd_tdb = tdb_open(tdbfile, 0, 0, O_RDWR | O_CREAT | O_EXCL, 0600)))
351 DEBUG(0, ("Unable to create TDB passwd (passdb.tdb) !!!"));
352 return False;
356 global_tdb_ent.key = tdb_firstkey(global_tdb_ent.passwd_tdb);
358 return True;
361 /***************************************************************
362 End enumeration of the TDB passwd list.
363 ****************************************************************/
364 void pdb_endsampwent(void)
366 if (global_tdb_ent.passwd_tdb)
368 tdb_close(global_tdb_ent.passwd_tdb);
369 global_tdb_ent.passwd_tdb = NULL;
372 DEBUG(7, ("endtdbpwent: closed password file.\n"));
376 /*****************************************************************
377 Get one SAM_ACCOUNT from the TDB (next in line)
378 *****************************************************************/
379 SAM_ACCOUNT* pdb_getsampwent(void)
381 TDB_DATA data;
382 struct passwd *pw;
383 uid_t uid;
384 gid_t gid;
385 char *prefix = USERPREFIX;
386 int prefixlen = strlen (prefix);
388 /* skip all RID entries */
389 while ((global_tdb_ent.key.dsize != 0) && (strncmp (global_tdb_ent.key.dptr, prefix, prefixlen)))
390 /* increment to next in line */
391 global_tdb_ent.key = tdb_nextkey (global_tdb_ent.passwd_tdb, global_tdb_ent.key);
393 /* do we have an valid interation pointer? */
394 if(global_tdb_ent.passwd_tdb == NULL)
396 DEBUG(0,("pdb_get_sampwent: Bad TDB Context pointer.\n"));
397 return NULL;
400 data = tdb_fetch (global_tdb_ent.passwd_tdb, global_tdb_ent.key);
401 if (!data.dptr)
403 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
404 return NULL;
407 /* unpack the buffer */
408 pdb_clear_sam (&global_sam_pass);
409 if (!init_sam_from_buffer (&global_sam_pass, data.dptr, data.dsize))
411 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
412 return NULL;
415 /* validate the account and fill in UNIX uid and gid. sys_getpwnam()
416 is used instaed of Get_Pwnam() as we do not need to try case
417 permutations */
418 if ((pw=sys_getpwnam(pdb_get_username(&global_sam_pass))) == NULL)
420 DEBUG(0,("pdb_getsampwent: getpwnam(%s) return NULL. User does not exist!\n",
421 pdb_get_username(&global_sam_pass)));
422 return NULL;
425 uid = pw->pw_uid;
426 gid = pw->pw_gid;
427 pdb_set_uid (&global_sam_pass, uid);
428 pdb_set_gid (&global_sam_pass, gid);
430 /* 21 days from present */
431 pdb_set_pass_must_change_time(&global_sam_pass, time(NULL)+1814400);
433 standard_sub_advanced(-1, pdb_get_username(&global_sam_pass), "", gid, pdb_get_logon_script(&global_sam_pass));
434 standard_sub_advanced(-1, pdb_get_username(&global_sam_pass), "", gid, pdb_get_profile_path(&global_sam_pass));
435 standard_sub_advanced(-1, pdb_get_username(&global_sam_pass), "", gid, pdb_get_homedir(&global_sam_pass));
437 /* increment to next in line */
438 global_tdb_ent.key = tdb_nextkey (global_tdb_ent.passwd_tdb, global_tdb_ent.key);
440 return (&global_sam_pass);
443 /******************************************************************
444 Lookup a name in the SAM TDB
445 ******************************************************************/
446 SAM_ACCOUNT* pdb_getsampwnam (char *sname)
448 TDB_CONTEXT *pwd_tdb;
449 TDB_DATA data, key;
450 fstring keystr;
451 struct passwd *pw;
452 pstring tdbfile;
453 fstring name;
454 uid_t uid;
455 gid_t gid;
457 fstrcpy (name, sname);
458 strlower (name);
459 pstrcpy (tdbfile, lp_private_dir());
460 pstrcat (tdbfile, PASSDB_FILE_NAME);
462 /* set search key */
463 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
464 key.dptr = keystr;
465 key.dsize = strlen (keystr) + 1;
467 /* open the accounts TDB */
468 if (!(pwd_tdb = tdb_open(tdbfile, 0, 0, O_RDONLY, 0600)))
470 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd!\n"));
471 return False;
474 /* get the record */
475 data = tdb_fetch (pwd_tdb, key);
476 if (!data.dptr)
478 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
479 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
480 tdb_close (pwd_tdb);
481 return NULL;
484 /* unpack the buffer */
485 pdb_clear_sam (&global_sam_pass);
486 if (!init_sam_from_buffer (&global_sam_pass, data.dptr, data.dsize))
488 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
489 return NULL;
492 /* validate the account and fill in UNIX uid and gid. sys_getpwnam()
493 is used instaed of Get_Pwnam() as we do not need to try case
494 permutations */
495 if ((pw=sys_getpwnam(pdb_get_username(&global_sam_pass))) == NULL)
497 DEBUG(0,("pdb_getsampwent: getpwnam(%s) return NULL. User does not exist!\n",
498 pdb_get_username(&global_sam_pass)));
499 return NULL;
502 uid = pw->pw_uid;
503 gid = pw->pw_gid;
504 pdb_set_uid (&global_sam_pass, uid);
505 pdb_set_gid (&global_sam_pass, gid);
507 /* 21 days from present */
508 pdb_set_pass_must_change_time(&global_sam_pass, time(NULL)+1814400);
510 standard_sub_advanced(-1, pdb_get_username(&global_sam_pass), "", gid, pdb_get_logon_script(&global_sam_pass));
511 standard_sub_advanced(-1, pdb_get_username(&global_sam_pass), "", gid, pdb_get_profile_path(&global_sam_pass));
512 standard_sub_advanced(-1, pdb_get_username(&global_sam_pass), "", gid, pdb_get_homedir(&global_sam_pass));
514 /* cleanup */
515 tdb_close (pwd_tdb);
517 return (&global_sam_pass);
520 /***************************************************************************
521 Search by uid
522 **************************************************************************/
523 SAM_ACCOUNT* pdb_getsampwuid (uid_t uid)
525 struct passwd *pw;
526 fstring name;
528 pw = sys_getpwuid(uid);
529 if (pw == NULL)
531 DEBUG(0,("pdb_getsampwuid: getpwuid(%d) return NULL. User does not exist!\n", uid));
532 return NULL;
534 fstrcpy (name, pw->pw_name);
536 return pdb_getsampwnam (name);
540 /***************************************************************************
541 Search by rid
542 **************************************************************************/
543 SAM_ACCOUNT* pdb_getsampwrid (uint32 rid)
545 TDB_CONTEXT *pwd_tdb;
546 TDB_DATA data, key;
547 fstring keystr;
548 pstring tdbfile;
549 fstring name;
551 pstrcpy (tdbfile, lp_private_dir());
552 pstrcat (tdbfile, PASSDB_FILE_NAME);
554 /* set search key */
555 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
556 key.dptr = keystr;
557 key.dsize = strlen (keystr) + 1;
559 /* open the accounts TDB */
560 if (!(pwd_tdb = tdb_open(tdbfile, 0, 0, O_RDONLY, 0600)))
562 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
563 return False;
566 /* get the record */
567 data = tdb_fetch (pwd_tdb, key);
568 if (!data.dptr)
570 DEBUG(5,("pdb_getsampwrid (TDB): error fetching database.\n"));
571 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
572 tdb_close (pwd_tdb);
573 return NULL;
576 fstrcpy (name, data.dptr);
578 tdb_close (pwd_tdb);
580 return pdb_getsampwnam (name);
584 /***************************************************************************
585 Delete a SAM_ACCOUNT
586 ****************************************************************************/
587 BOOL pdb_delete_sam_account(char *sname)
589 struct passwd *pwd = NULL;
590 TDB_CONTEXT *pwd_tdb;
591 TDB_DATA key, data;
592 fstring keystr;
593 pstring tdbfile;
594 uint32 rid;
595 fstring name;
597 fstrcpy (name, sname);
598 strlower (name);
600 pstrcpy (tdbfile, lp_private_dir());
601 pstrcat (tdbfile, PASSDB_FILE_NAME);
603 /* open the TDB */
604 if (!(pwd_tdb = tdb_open(tdbfile, 0, 0, O_RDWR, 0600)))
606 DEBUG(0, ("Unable to open TDB passwd!"));
607 return False;
610 /* set the search key */
611 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
612 key.dptr = keystr;
613 key.dsize = strlen (keystr) + 1;
615 /* get the record */
616 data = tdb_fetch (pwd_tdb, key);
617 if (!data.dptr)
619 DEBUG(5,("pdb_delete_sam_account (TDB): error fetching database.\n"));
620 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
621 tdb_close (pwd_tdb);
622 return False;
625 /* unpack the buffer */
626 pdb_clear_sam (&global_sam_pass);
627 if (!init_sam_from_buffer (&global_sam_pass, data.dptr, data.dsize))
629 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
630 return False;
633 pwd = sys_getpwnam(global_sam_pass.username);
635 rid = pdb_uid_to_user_rid (pwd->pw_uid);
637 /* it's outaa here! 8^) */
638 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS)
640 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
641 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
642 tdb_close(pwd_tdb);
643 return False;
646 /* delete also the RID key */
648 /* set the search key */
649 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
650 key.dptr = keystr;
651 key.dsize = strlen (keystr) + 1;
653 /* it's outaa here! 8^) */
654 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS)
656 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
657 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
658 tdb_close(pwd_tdb);
659 return False;
662 tdb_close(pwd_tdb);
664 return True;
667 /***************************************************************************
668 Update the TDB SAM
669 ****************************************************************************/
670 static BOOL tdb_update_sam(SAM_ACCOUNT* newpwd, BOOL override, int flag)
672 TDB_CONTEXT *pwd_tdb;
673 TDB_DATA key, data;
674 uint8 *buf = NULL;
675 fstring keystr;
676 pstring tdbfile;
677 fstring name;
678 int newtdb = FALSE;
680 pstrcpy (tdbfile, lp_private_dir());
681 pstrcat (tdbfile, PASSDB_FILE_NAME);
683 if ( (!newpwd->uid) || (!newpwd->gid) )
684 DEBUG (0,("tdb_update_sam: Storing a SAM_ACCOUNT for [%s] with uid %d and gid %d!\n",
685 newpwd->username, newpwd->uid, newpwd->gid));
687 /* if we don't have a RID, then generate one */
688 if (!newpwd->user_rid)
689 pdb_set_user_rid (newpwd, pdb_uid_to_user_rid (newpwd->uid));
690 if (!newpwd->group_rid)
691 pdb_set_group_rid (newpwd, pdb_gid_to_group_rid (newpwd->gid));
694 /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
695 if ((data.dsize=init_buffer_from_sam (&buf, newpwd)) == -1)
697 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
698 return False;
700 data.dptr = buf;
702 fstrcpy (name, pdb_get_username(newpwd));
703 strlower (name);
705 /* setup the USER index key */
706 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
707 key.dptr = keystr;
708 key.dsize = strlen (keystr) + 1;
710 /* invalidate the existing TDB iterator if it is open */
711 if (global_tdb_ent.passwd_tdb)
713 tdb_close(global_tdb_ent.passwd_tdb);
714 global_tdb_ent.passwd_tdb = NULL;
717 /* open the account TDB passwd*/
718 if (!(pwd_tdb = tdb_open(tdbfile, 0, 0, O_RDWR, 0600)))
720 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd!\n"));
721 if (flag == TDB_INSERT)
723 DEBUG(0, ("Unable to open TDB passwd, trying create new!\n"));
724 if (!(pwd_tdb = tdb_open(tdbfile, 0, 0, O_RDWR | O_CREAT | O_EXCL, 0600)))
726 DEBUG(0, ("Unable to create TDB passwd (passdb.tdb) !!!\n"));
727 return False;
729 newtdb = TRUE;
733 /* add the account */
734 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS)
736 DEBUG(0, ("Unable to modify passwd TDB!"));
737 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
738 tdb_close (pwd_tdb);
739 return False;
742 /* setup RID data */
743 data.dsize = sizeof(fstring);
744 data.dptr = name;
746 /* setup the RID index key */
747 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, pdb_get_user_rid(newpwd));
748 key.dptr = keystr;
749 key.dsize = strlen (keystr) + 1;
751 /* add the reference */
752 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS)
754 DEBUG(0, ("Unable to modify TDB passwd !"));
755 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
756 tdb_close (pwd_tdb);
757 return False;
760 /* cleanup */
761 tdb_close (pwd_tdb);
763 return (True);
766 /***************************************************************************
767 Modifies an existing SAM_ACCOUNT
768 ****************************************************************************/
769 BOOL pdb_update_sam_account (SAM_ACCOUNT *newpwd, BOOL override)
771 return (tdb_update_sam(newpwd, override, TDB_MODIFY));
774 /***************************************************************************
775 Adds an existing SAM_ACCOUNT
776 ****************************************************************************/
777 BOOL pdb_add_sam_account (SAM_ACCOUNT *newpwd)
779 return (tdb_update_sam(newpwd, True, TDB_INSERT));
783 #else
784 /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
785 void samtdb_dummy_function(void) { } /* stop some compilers complaining */
786 #endif /* WITH_TDBPWD */