2 Unix SMB/Netbios implementation.
4 Password and authentication handling
5 Copyright (C) Jeremy Allison 1996-1998
6 Copyright (C) Luke Kenneth Casson 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 Mass Ave, Cambridge, MA 02139, USA.
26 extern int DEBUGLEVEL
;
29 * NOTE. All these functions are abstracted into a structure
30 * that points to the correct function for the selected database. JRA.
33 static struct groupdb_ops
*gpdb_ops
;
35 /***************************************************************
36 Initialise the group db operations.
37 ***************************************************************/
39 BOOL
initialise_group_db(void)
47 gpdb_ops
= nisplus_initialise_group_db();
48 #elif defined(WITH_LDAP)
49 gpdb_ops
= ldap_initialise_group_db();
51 gpdb_ops
= file_initialise_group_db();
54 return (gpdb_ops
!= NULL
);
58 * Functions that return/manipulate a DOMAIN_GRP.
61 /************************************************************************
62 Utility function to search group database by gid: the DOMAIN_GRP
63 structure does not have a gid member, so we have to convert here
64 from gid to group rid.
65 *************************************************************************/
66 DOMAIN_GRP
*iterate_getgroupgid(gid_t gid
, DOMAIN_GRP_MEMBER
**mem
, int *num_mem
)
68 return iterate_getgrouprid(pwdb_gid_to_group_rid(gid
), mem
, num_mem
);
71 /************************************************************************
72 Utility function to search group database by rid. use this if your database
73 does not have search facilities.
74 *************************************************************************/
75 DOMAIN_GRP
*iterate_getgrouprid(uint32 rid
, DOMAIN_GRP_MEMBER
**mem
, int *num_mem
)
77 DOMAIN_GRP
*grp
= NULL
;
80 DEBUG(10, ("search by rid: 0x%x\n", rid
));
82 /* Open the group database file - not for update. */
83 fp
= startgroupent(False
);
87 DEBUG(0, ("unable to open group database.\n"));
91 while ((grp
= getgroupent(fp
, mem
, num_mem
)) != NULL
&& grp
->rid
!= rid
)
97 DEBUG(10, ("found group %s by rid: 0x%x\n", grp
->name
, rid
));
104 /************************************************************************
105 Utility function to search group database by name. use this if your database
106 does not have search facilities.
107 *************************************************************************/
108 DOMAIN_GRP
*iterate_getgroupnam(char *name
, DOMAIN_GRP_MEMBER
**mem
, int *num_mem
)
110 DOMAIN_GRP
*grp
= NULL
;
113 DEBUG(10, ("search by name: %s\n", name
));
115 /* Open the group database file - not for update. */
116 fp
= startgroupent(False
);
120 DEBUG(0, ("unable to open group database.\n"));
124 while ((grp
= getgroupent(fp
, mem
, num_mem
)) != NULL
&& !strequal(grp
->name
, name
))
130 DEBUG(10, ("found by name: %s\n", name
));
137 /*************************************************************************
138 Routine to return the next entry in the smbdomaingroup list.
139 *************************************************************************/
140 BOOL
add_domain_group(DOMAIN_GRP
**grps
, int *num_grps
, DOMAIN_GRP
*grp
)
142 if (grps
== NULL
|| num_grps
== NULL
|| grp
== NULL
)
147 (*grps
) = Realloc((*grps
), ((*num_grps
)+1) * sizeof(DOMAIN_GRP
));
153 DEBUG(10,("adding group %s(%s)\n", grp
->name
, grp
->comment
));
155 fstrcpy((*grps
)[(*num_grps
)].name
, grp
->name
);
156 fstrcpy((*grps
)[(*num_grps
)].comment
, grp
->comment
);
157 (*grps
)[(*num_grps
)].attr
= grp
->attr
;
158 (*grps
)[(*num_grps
)].rid
= grp
->rid
;
165 /*************************************************************************
166 checks to see if a user is a member of a domain group
167 *************************************************************************/
168 static BOOL
user_is_member(char *user_name
, DOMAIN_GRP_MEMBER
*mem
, int num_mem
)
171 for (i
= 0; i
< num_mem
; i
++)
173 DEBUG(10,("searching against user %s...\n", mem
[i
].name
));
174 if (strequal(mem
[i
].name
, user_name
))
176 DEBUG(10,("searching for user %s: found\n", user_name
));
180 DEBUG(10,("searching for user %s: not found\n", user_name
));
184 /*************************************************************************
185 gets an array of groups that a user is in. use this if your database
186 does not have search facilities
187 *************************************************************************/
188 BOOL
iterate_getusergroupsnam(char *user_name
, DOMAIN_GRP
**grps
, int *num_grps
)
191 DOMAIN_GRP_MEMBER
*mem
= NULL
;
195 DEBUG(10, ("search for usergroups by name: %s\n", user_name
));
197 if (user_name
== NULL
|| grp
== NULL
|| num_grps
== NULL
)
205 /* Open the group database file - not for update. */
206 fp
= startgroupent(False
);
210 DEBUG(0, ("unable to open group database.\n"));
214 /* iterate through all groups. search members for required user */
215 while ((grp
= getgroupent(fp
, &mem
, &num_mem
)) != NULL
)
217 DEBUG(5,("group name %s members: %d\n", grp
->name
, num_mem
));
218 if (num_mem
!= 0 && mem
!= NULL
)
221 if (user_is_member(user_name
, mem
, num_mem
))
223 ret
= add_domain_group(grps
, num_grps
, grp
);
238 if ((*num_grps
) != 0)
240 DEBUG(10, ("found %d user groups:\n", (*num_grps
)));
247 /*************************************************************************
248 gets an array of groups that a user is in. use this if your database
249 does not have search facilities
250 *************************************************************************/
251 BOOL
enumdomgroups(DOMAIN_GRP
**grps
, int *num_grps
)
256 DEBUG(10, ("enum user groups\n"));
258 if (grp
== NULL
|| num_grps
== NULL
)
266 /* Open the group database file - not for update. */
267 fp
= startgroupent(False
);
271 DEBUG(0, ("unable to open group database.\n"));
275 /* iterate through all groups. */
276 while ((grp
= getgroupent(fp
, NULL
, NULL
)) != NULL
)
278 if (!add_domain_group(grps
, num_grps
, grp
))
280 DEBUG(0,("unable to add group while enumerating\n"));
285 if ((*num_grps
) != 0)
287 DEBUG(10, ("found %d user groups:\n", (*num_grps
)));
294 /***************************************************************
295 Start to enumerate the group database list. Returns a void pointer
296 to ensure no modification outside this module.
297 ****************************************************************/
299 void *startgroupent(BOOL update
)
301 return gpdb_ops
->startgroupent(update
);
304 /***************************************************************
305 End enumeration of the group database list.
306 ****************************************************************/
308 void endgroupent(void *vp
)
310 gpdb_ops
->endgroupent(vp
);
313 /*************************************************************************
314 Routine to return the next entry in the group database list.
315 *************************************************************************/
317 DOMAIN_GRP
*getgroupent(void *vp
, DOMAIN_GRP_MEMBER
**mem
, int *num_mem
)
319 return gpdb_ops
->getgroupent(vp
, mem
, num_mem
);
322 /************************************************************************
323 Routine to add an entry to the group database file.
324 *************************************************************************/
326 BOOL
add_group_entry(DOMAIN_GRP
*newgrp
)
328 return gpdb_ops
->add_group_entry(newgrp
);
331 /************************************************************************
332 Routine to search the group database file for an entry matching the groupname.
333 and then replace the entry.
334 ************************************************************************/
336 BOOL
mod_group_entry(DOMAIN_GRP
* grp
)
338 return gpdb_ops
->mod_group_entry(grp
);
341 /************************************************************************
342 Routine to search group database by name.
343 *************************************************************************/
345 DOMAIN_GRP
*getgroupnam(char *name
, DOMAIN_GRP_MEMBER
**mem
, int *num_mem
)
347 return gpdb_ops
->getgroupnam(name
, mem
, num_mem
);
350 /************************************************************************
351 Routine to search group database by group rid.
352 *************************************************************************/
354 DOMAIN_GRP
*getgrouprid(uint32 group_rid
, DOMAIN_GRP_MEMBER
**mem
, int *num_mem
)
356 return gpdb_ops
->getgrouprid(group_rid
, mem
, num_mem
);
359 /************************************************************************
360 Routine to search group database by gid.
361 *************************************************************************/
363 DOMAIN_GRP
*getgroupgid(gid_t gid
, DOMAIN_GRP_MEMBER
**mem
, int *num_mem
)
365 return gpdb_ops
->getgroupgid(gid
, mem
, num_mem
);
368 /*************************************************************************
369 gets an array of groups that a user is in.
370 *************************************************************************/
371 BOOL
getusergroupsnam(char *user_name
, DOMAIN_GRP
**grp
, int *num_grps
)
373 return gpdb_ops
->getusergroupsnam(user_name
, grp
, num_grps
);
376 /*************************************************************
377 initialises a DOMAIN_GRP.
378 **************************************************************/
380 void gpdb_init_grp(DOMAIN_GRP
*grp
)
382 if (grp
== NULL
) return;