2 Unix SMB/Netbios implementation.
4 Pasesword and authentication handling
5 Copyright (C) Jeremy Allison 1996-1998
6 Copyright (C) Luke Kenneth Caseson Leighton 1996-1998
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mases Ave, Cambridge, MA 02139, USA.
26 extern int DEBUGLEVEL;
28 extern fstring global_sam_name;
29 extern DOM_SID global_sam_sid;
32 * NOTE. All these functions are abstracted into a structure
33 * that points to the correct function for the selected database. JRA.
36 static struct aliasdb_ops *bidb_ops = NULL;
38 /***************************************************************
39 Initialise the builtin db operations.
40 ***************************************************************/
42 BOOL initialise_builtin_db(void)
50 bidb_ops = nisplus_initialise_builtin_db();
51 #elif defined(WITH_LDAP)
52 bidb_ops = ldap_initialise_builtin_db();
53 #elif defined(USE_SMBUNIX_DB)
54 bidb_ops = unix_initialise_builtin_db();
57 return (bidb_ops != NULL);
61 * Functions that return/manipulate a LOCAL_GRP.
64 /************************************************************************
65 Utility function to search builtin database by gid: the LOCAL_GRP
66 structure does not have a gid member, so we have to convert here
67 from gid to builtin rid.
68 *************************************************************************/
69 LOCAL_GRP *iterate_getbuiltingid(gid_t gid, LOCAL_GRP_MEMBER **mem, int *num_mem)
73 if (!lookupsmbgrpgid(gid, &gmep))
75 DEBUG(0,("iterate_getbuiltingid: gid %d does not map to one of our Domain's Aliases\n", gid));
79 if (gmep.type != SID_NAME_ALIAS )
81 DEBUG(0,("iterate_getbuiltingid: gid %d does not map to one of our Domain's Aliases\n", gid));
85 sid_split_rid(&gmep.sid, &rid);
86 if (!sid_equal(&gmep.sid, &global_sam_sid))
88 DEBUG(0,("iterate_getbuiltingid: gid %d does not map into our Domain SID\n", gid));
92 return iterate_getbuiltinrid(rid, mem, num_mem);
95 /************************************************************************
96 Utility function to search builtin database by rid. use this if your database
97 does not have search facilities.
98 *************************************************************************/
99 LOCAL_GRP *iterate_getbuiltinrid(uint32 rid, LOCAL_GRP_MEMBER **mem, int *num_mem)
101 LOCAL_GRP *blt = NULL;
104 DEBUG(10, ("search by rid: 0x%x\n", rid));
106 /* Open the builtin database file - not for update. */
107 fp = startbuiltinent(False);
111 DEBUG(0, ("unable to open builtin database.\n"));
115 while ((blt = getbuiltinent(fp, mem, num_mem)) != NULL && blt->rid != rid)
117 DEBUG(10,("iterate: %s 0x%x", blt->name, blt->rid));
122 DEBUG(10, ("found builtin %s by rid: 0x%x\n", blt->name, rid));
129 /************************************************************************
130 Utility function to search builtin database by name. use this if your database
131 does not have search facilities.
132 *************************************************************************/
133 LOCAL_GRP *iterate_getbuiltinntnam(const char *name, LOCAL_GRP_MEMBER **mem, int *num_mem)
135 LOCAL_GRP *blt = NULL;
138 DEBUG(10, ("search by name: %s\n", name));
140 /* Open the builtin database file - not for update. */
141 fp = startbuiltinent(False);
145 DEBUG(0, ("unable to open builtin database.\n"));
149 while ((blt = getbuiltinent(fp, mem, num_mem)) != NULL && !strequal(blt->name, name))
155 DEBUG(10, ("found by name: %s\n", name));
162 /*************************************************************************
163 Routine to return the next entry in the smbdomainbuiltin list.
164 *************************************************************************/
165 BOOL add_domain_builtin(LOCAL_GRP **blts, int *num_blts, LOCAL_GRP *blt)
167 if (blts == NULL || num_blts == NULL || blt == NULL)
172 (*blts) = Realloc((*blts), ((*num_blts)+1) * sizeof(LOCAL_GRP));
178 DEBUG(10,("adding builtin %s(%s)\n", blt->name, blt->comment));
180 fstrcpy((*blts)[(*num_blts)].name , blt->name);
181 fstrcpy((*blts)[(*num_blts)].comment, blt->comment);
182 (*blts)[(*num_blts)].rid = blt->rid;
189 /*************************************************************************
190 checks to see if a user is a member of a domain builtin
191 *************************************************************************/
192 static BOOL user_is_member(const char *user_name, LOCAL_GRP_MEMBER *mem, int num_mem)
196 slprintf(name, sizeof(name)-1, "%s\\%s", global_sam_name, user_name);
198 for (i = 0; i < num_mem; i++)
200 DEBUG(10,("searching against user %s...\n", mem[i].name));
201 if (strequal(mem[i].name, name))
203 DEBUG(10,("searching for user %s: found\n", name));
207 DEBUG(10,("searching for user %s: not found\n", name));
211 /*************************************************************************
212 gets an array of builtin aliases that a user is in. use this if your database
213 does not have search facilities
214 *************************************************************************/
215 BOOL iterate_getuserbuiltinntnam(const char *user_name, LOCAL_GRP **blts, int *num_blts)
217 LOCAL_GRP *blt = NULL;
218 LOCAL_GRP_MEMBER *mem = NULL;
222 DEBUG(10, ("search for userbuiltin by name: %s\n", user_name));
224 if (user_name == NULL || blts == NULL || num_blts == NULL)
232 /* Open the builtin database file - not for update. */
233 fp = startbuiltinent(False);
237 DEBUG(0, ("unable to open builtin database.\n"));
241 /* iterate through all builtin aliases. search members for required user */
242 while ((blt = getbuiltinent(fp, &mem, &num_mem)) != NULL)
244 DEBUG(5,("builtin name %s members: %d\n", blt->name, num_mem));
245 if (num_mem != 0 && mem != NULL)
248 if (user_is_member(user_name, mem, num_mem))
250 ret = add_domain_builtin(blts, num_blts, blt);
265 if ((*num_blts) != 0)
267 DEBUG(10, ("found %d user builtin aliases:\n", (*num_blts)));
274 /*************************************************************************
275 gets an array of builtin aliases that a user is in. use this if your database
276 does not have search facilities
277 *************************************************************************/
278 BOOL enumdombuiltins(LOCAL_GRP **blts, int *num_blts)
280 LOCAL_GRP *blt = NULL;
283 DEBUG(10, ("enum user builtin aliases\n"));
285 if (blts == NULL || num_blts == NULL)
293 /* Open the builtin database file - not for update. */
294 fp = startbuiltinent(False);
298 DEBUG(0, ("unable to open builtin database.\n"));
302 /* iterate through all builtin aliases. */
303 while ((blt = getbuiltinent(fp, NULL, NULL)) != NULL)
305 if (!add_domain_builtin(blts, num_blts, blt))
307 DEBUG(0,("unable to add builtin while enumerating\n"));
312 if ((*num_blts) != 0)
314 DEBUG(10, ("found %d user builtin aliases:\n", (*num_blts)));
321 /***************************************************************
322 Start to enumerate the builtin database list. Returns a void pointer
323 to ensure no modification outside this module.
324 ****************************************************************/
326 void *startbuiltinent(BOOL update)
328 return bidb_ops->startaliasent(update);
331 /***************************************************************
332 End enumeration of the builtin database list.
333 ****************************************************************/
335 void endbuiltinent(void *vp)
337 bidb_ops->endaliasent(vp);
340 /*************************************************************************
341 Routine to return the next entry in the builtin database list.
342 *************************************************************************/
344 LOCAL_GRP *getbuiltinent(void *vp, LOCAL_GRP_MEMBER **mem, int *num_mem)
346 return bidb_ops->getaliasent(vp, mem, num_mem);
349 /************************************************************************
350 Routine to add an entry to the builtin database file.
351 *************************************************************************/
353 BOOL add_builtin_entry(LOCAL_GRP *newblt)
355 return bidb_ops->add_alias_entry(newblt);
358 /************************************************************************
359 Routine to search the builtin database file for an entry matching the builtinname.
360 and then replace the entry.
361 ************************************************************************/
363 BOOL mod_builtin_entry(LOCAL_GRP* blt)
365 return bidb_ops->mod_alias_entry(blt);
368 /************************************************************************
369 Routine to add a member to an entry in the builtin database file.
370 *************************************************************************/
371 BOOL add_builtin_member(uint32 rid, DOM_SID *member_sid)
373 return bidb_ops->add_alias_member(rid, member_sid);
376 /************************************************************************
377 Routine to delete a member from an entry in the builtindatabase file.
378 *************************************************************************/
379 BOOL del_builtin_member(uint32 rid, DOM_SID *member_sid)
381 return bidb_ops->del_alias_member(rid, member_sid);
384 /************************************************************************
385 Routine to search builtin database by name.
386 *************************************************************************/
388 LOCAL_GRP *getbuiltinntnam(const char *name, LOCAL_GRP_MEMBER **mem, int *num_mem)
390 return bidb_ops->getaliasntnam(name, mem, num_mem);
393 /************************************************************************
394 Routine to search builtin database by builtin rid.
395 *************************************************************************/
397 LOCAL_GRP *getbuiltinrid(uint32 builtin_rid, LOCAL_GRP_MEMBER **mem, int *num_mem)
399 return bidb_ops->getaliasrid(builtin_rid, mem, num_mem);
402 /************************************************************************
403 Routine to search builtin database by gid.
404 *************************************************************************/
406 LOCAL_GRP *getbuiltingid(gid_t gid, LOCAL_GRP_MEMBER **mem, int *num_mem)
408 return bidb_ops->getaliasgid(gid, mem, num_mem);
411 /*************************************************************************
412 gets an array of builtin aliases that a user is in.
413 *************************************************************************/
414 BOOL getuserbuiltinntnam(const char *user_name, LOCAL_GRP **blt, int *num_blts)
416 return bidb_ops->getuseraliasntnam(user_name, blt, num_blts);
419 /*************************************************************
420 initialises a LOCAL_GRP.
421 **************************************************************/
422 void bidb_init_blt(LOCAL_GRP *blt)
424 if (blt == NULL) return;
428 /*************************************************************
429 turns an builtin entry into a string.
430 **************************************************************/
431 BOOL make_builtin_line(char *p, int max_len,
433 LOCAL_GRP_MEMBER **mem, int *num_mem)
437 len = slprintf(p, max_len-1, "%s:%s:%d:", blt->name, blt->comment, blt->rid);
441 DEBUG(0,("make_builtin_line: cannot create entry\n"));
448 if (mem == NULL || num_mem == NULL)
453 for (i = 0; i < (*num_mem); i++)
455 len = strlen((*mem)[i].name);
456 p = safe_strcpy(p, (*mem)[i].name, max_len);
460 DEBUG(0, ("make_builtin_line: out of space for builtin aliases!\n"));
466 if (i != (*num_mem)-1)