2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Jeremy Allison 1996-1998
5 Copyright (C) Luke Kenneth Casson Leighton 1996-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * NOTE. All these functions are abstracted into a structure
26 * that points to the correct function for the selected database. JRA.
29 static struct groupdb_ops *gpdb_ops;
31 /***************************************************************
32 Initialise the group db operations.
33 ***************************************************************/
35 BOOL initialise_group_db(void)
43 gpdb_ops = nisplus_initialise_group_db();
44 #elif defined(WITH_LDAP)
45 gpdb_ops = ldap_initialise_group_db();
47 gpdb_ops = file_initialise_group_db();
50 return (gpdb_ops != NULL);
54 * Functions that return/manipulate a DOMAIN_GRP.
57 /************************************************************************
58 Utility function to search group database by gid: the DOMAIN_GRP
59 structure does not have a gid member, so we have to convert here
60 from gid to group rid.
61 *************************************************************************/
62 DOMAIN_GRP *iterate_getgroupgid(gid_t gid, DOMAIN_GRP_MEMBER **mem, int *num_mem)
64 return iterate_getgrouprid(pwdb_gid_to_group_rid(gid), mem, num_mem);
67 /************************************************************************
68 Utility function to search group database by rid. use this if your database
69 does not have search facilities.
70 *************************************************************************/
71 DOMAIN_GRP *iterate_getgrouprid(uint32 rid, DOMAIN_GRP_MEMBER **mem, int *num_mem)
73 DOMAIN_GRP *grp = NULL;
76 DEBUG(10, ("search by rid: 0x%x\n", rid));
78 /* Open the group database file - not for update. */
79 fp = startgroupent(False);
83 DEBUG(0, ("unable to open group database.\n"));
87 while ((grp = getgroupent(fp, mem, num_mem)) != NULL && grp->rid != rid)
93 DEBUG(10, ("found group %s by rid: 0x%x\n", grp->name, rid));
100 /************************************************************************
101 Utility function to search group database by name. use this if your database
102 does not have search facilities.
103 *************************************************************************/
104 DOMAIN_GRP *iterate_getgroupnam(char *name, DOMAIN_GRP_MEMBER **mem, int *num_mem)
106 DOMAIN_GRP *grp = NULL;
109 DEBUG(10, ("search by name: %s\n", name));
111 /* Open the group database file - not for update. */
112 fp = startgroupent(False);
116 DEBUG(0, ("unable to open group database.\n"));
120 while ((grp = getgroupent(fp, mem, num_mem)) != NULL && !strequal(grp->name, name))
126 DEBUG(10, ("found by name: %s\n", name));
133 /*************************************************************************
134 Routine to return the next entry in the smbdomaingroup list.
135 *************************************************************************/
136 BOOL add_domain_group(DOMAIN_GRP **grps, int *num_grps, DOMAIN_GRP *grp)
140 if (grps == NULL || num_grps == NULL || grp == NULL)
143 tgrps = Realloc((*grps), ((*num_grps)+1) * sizeof(DOMAIN_GRP));
150 DEBUG(10,("adding group %s(%s)\n", grp->name, grp->comment));
152 fstrcpy((*grps)[(*num_grps)].name , grp->name);
153 fstrcpy((*grps)[(*num_grps)].comment, grp->comment);
154 (*grps)[(*num_grps)].attr = grp->attr;
155 (*grps)[(*num_grps)].rid = grp->rid ;
162 /*************************************************************************
163 checks to see if a user is a member of a domain group
164 *************************************************************************/
165 static BOOL user_is_member(char *user_name, DOMAIN_GRP_MEMBER *mem, int num_mem)
168 for (i = 0; i < num_mem; i++)
170 DEBUG(10,("searching against user %s...\n", mem[i].name));
171 if (strequal(mem[i].name, user_name))
173 DEBUG(10,("searching for user %s: found\n", user_name));
177 DEBUG(10,("searching for user %s: not found\n", user_name));
181 /*************************************************************************
182 gets an array of groups that a user is in. use this if your database
183 does not have search facilities
184 *************************************************************************/
185 BOOL iterate_getusergroupsnam(char *user_name, DOMAIN_GRP **grps, int *num_grps)
188 DOMAIN_GRP_MEMBER *mem = NULL;
192 DEBUG(10, ("search for usergroups by name: %s\n", user_name));
194 if (user_name == NULL || grp == NULL || num_grps == NULL)
202 /* Open the group database file - not for update. */
203 fp = startgroupent(False);
207 DEBUG(0, ("unable to open group database.\n"));
211 /* iterate through all groups. search members for required user */
212 while ((grp = getgroupent(fp, &mem, &num_mem)) != NULL)
214 DEBUG(5,("group name %s members: %d\n", grp->name, num_mem));
215 if (num_mem != 0 && mem != NULL)
218 if (user_is_member(user_name, mem, num_mem))
220 ret = add_domain_group(grps, num_grps, grp);
234 if ((*num_grps) != 0)
236 DEBUG(10, ("found %d user groups:\n", (*num_grps)));
243 /*************************************************************************
244 gets an array of groups that a user is in. use this if your database
245 does not have search facilities
246 *************************************************************************/
247 BOOL enumdomgroups(DOMAIN_GRP **grps, int *num_grps)
252 DEBUG(10, ("enum user groups\n"));
254 if (grp == NULL || num_grps == NULL)
262 /* Open the group database file - not for update. */
263 fp = startgroupent(False);
267 DEBUG(0, ("unable to open group database.\n"));
271 /* iterate through all groups. */
272 while ((grp = getgroupent(fp, NULL, NULL)) != NULL)
274 if (!add_domain_group(grps, num_grps, grp))
276 DEBUG(0,("unable to add group while enumerating\n"));
281 if ((*num_grps) != 0)
283 DEBUG(10, ("found %d user groups:\n", (*num_grps)));
290 /***************************************************************
291 Start to enumerate the group database list. Returns a void pointer
292 to ensure no modification outside this module.
293 ****************************************************************/
295 void *startgroupent(BOOL update)
297 return gpdb_ops->startgroupent(update);
300 /***************************************************************
301 End enumeration of the group database list.
302 ****************************************************************/
304 void endgroupent(void *vp)
306 gpdb_ops->endgroupent(vp);
309 /*************************************************************************
310 Routine to return the next entry in the group database list.
311 *************************************************************************/
313 DOMAIN_GRP *getgroupent(void *vp, DOMAIN_GRP_MEMBER **mem, int *num_mem)
315 return gpdb_ops->getgroupent(vp, mem, num_mem);
318 /************************************************************************
319 Routine to add an entry to the group database file.
320 *************************************************************************/
322 BOOL add_group_entry(DOMAIN_GRP *newgrp)
324 return gpdb_ops->add_group_entry(newgrp);
327 /************************************************************************
328 Routine to search the group database file for an entry matching the groupname.
329 and then replace the entry.
330 ************************************************************************/
332 BOOL mod_group_entry(DOMAIN_GRP* grp)
334 return gpdb_ops->mod_group_entry(grp);
337 /************************************************************************
338 Routine to search group database by name.
339 *************************************************************************/
341 DOMAIN_GRP *getgroupnam(char *name, DOMAIN_GRP_MEMBER **mem, int *num_mem)
343 return gpdb_ops->getgroupnam(name, mem, num_mem);
346 /************************************************************************
347 Routine to search group database by group rid.
348 *************************************************************************/
350 DOMAIN_GRP *getgrouprid(uint32 group_rid, DOMAIN_GRP_MEMBER **mem, int *num_mem)
352 return gpdb_ops->getgrouprid(group_rid, mem, num_mem);
355 /************************************************************************
356 Routine to search group database by gid.
357 *************************************************************************/
359 DOMAIN_GRP *getgroupgid(gid_t gid, DOMAIN_GRP_MEMBER **mem, int *num_mem)
361 return gpdb_ops->getgroupgid(gid, mem, num_mem);
364 /*************************************************************************
365 gets an array of groups that a user is in.
366 *************************************************************************/
367 BOOL getusergroupsnam(char *user_name, DOMAIN_GRP **grp, int *num_grps)
369 return gpdb_ops->getusergroupsnam(user_name, grp, num_grps);
372 /*************************************************************
373 initialises a DOMAIN_GRP.
374 **************************************************************/
376 void gpdb_init_grp(DOMAIN_GRP *grp)
378 if (grp == NULL) return;