This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[samba.git] / source3 / groupdb / groupdb.c
1 /* 
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
6       
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24 /*
25  * NOTE. All these functions are abstracted into a structure
26  * that points to the correct function for the selected database. JRA.
27  */
28
29 static struct groupdb_ops *gpdb_ops;
30
31 /***************************************************************
32  Initialise the group db operations.
33 ***************************************************************/
34
35 BOOL initialise_group_db(void)
36 {
37   if (gpdb_ops)
38   {
39     return True;
40   }
41
42 #ifdef WITH_NISPLUS
43   gpdb_ops =  nisplus_initialise_group_db();
44 #elif defined(WITH_LDAP)
45   gpdb_ops = ldap_initialise_group_db();
46 #else 
47   gpdb_ops = file_initialise_group_db();
48 #endif 
49
50   return (gpdb_ops != NULL);
51 }
52
53 /*
54  * Functions that return/manipulate a DOMAIN_GRP.
55  */
56
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)
63 {
64         return iterate_getgrouprid(pwdb_gid_to_group_rid(gid), mem, num_mem);
65 }
66
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)
72 {
73         DOMAIN_GRP *grp = NULL;
74         void *fp = NULL;
75
76         DEBUG(10, ("search by rid: 0x%x\n", rid));
77
78         /* Open the group database file - not for update. */
79         fp = startgroupent(False);
80
81         if (fp == NULL)
82         {
83                 DEBUG(0, ("unable to open group database.\n"));
84                 return NULL;
85         }
86
87         while ((grp = getgroupent(fp, mem, num_mem)) != NULL && grp->rid != rid)
88         {
89         }
90
91         if (grp != NULL)
92         {
93                 DEBUG(10, ("found group %s by rid: 0x%x\n", grp->name, rid));
94         }
95
96         endgroupent(fp);
97         return grp;
98 }
99
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)
105 {
106         DOMAIN_GRP *grp = NULL;
107         void *fp = NULL;
108
109         DEBUG(10, ("search by name: %s\n", name));
110
111         /* Open the group database file - not for update. */
112         fp = startgroupent(False);
113
114         if (fp == NULL)
115         {
116                 DEBUG(0, ("unable to open group database.\n"));
117                 return NULL;
118         }
119
120         while ((grp = getgroupent(fp, mem, num_mem)) != NULL && !strequal(grp->name, name))
121         {
122         }
123
124         if (grp != NULL)
125         {
126                 DEBUG(10, ("found by name: %s\n", name));
127         }
128
129         endgroupent(fp);
130         return grp;
131 }
132
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)
137 {
138         DOMAIN_GRP *tgrps;
139
140         if (grps == NULL || num_grps == NULL || grp == NULL)
141                 return False;
142
143         tgrps = Realloc((*grps), ((*num_grps)+1) * sizeof(DOMAIN_GRP));
144         if (tgrps == NULL) {
145                 SAFE_FREE(*grps);
146                 return False;
147         } else
148                 (*grps) = tgrps;
149
150         DEBUG(10,("adding group %s(%s)\n", grp->name, grp->comment));
151
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 ;
156
157         (*num_grps)++;
158
159         return True;
160 }
161
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)
166 {
167         int i;
168         for (i = 0; i < num_mem; i++)
169         {
170                 DEBUG(10,("searching against user %s...\n", mem[i].name));
171                 if (strequal(mem[i].name, user_name))
172                 {
173                         DEBUG(10,("searching for user %s: found\n", user_name));
174                         return True;
175                 }
176         }
177         DEBUG(10,("searching for user %s: not found\n", user_name));
178         return False;
179 }
180
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)
186 {
187         DOMAIN_GRP *grp;
188         DOMAIN_GRP_MEMBER *mem = NULL;
189         int num_mem = 0;
190         void *fp = NULL;
191
192         DEBUG(10, ("search for usergroups by name: %s\n", user_name));
193
194         if (user_name == NULL || grp == NULL || num_grps == NULL)
195         {
196                 return False;
197         }
198
199         (*grps) = NULL;
200         (*num_grps) = 0;
201
202         /* Open the group database file - not for update. */
203         fp = startgroupent(False);
204
205         if (fp == NULL)
206         {
207                 DEBUG(0, ("unable to open group database.\n"));
208                 return False;
209         }
210
211         /* iterate through all groups.  search members for required user */
212         while ((grp = getgroupent(fp, &mem, &num_mem)) != NULL)
213         {
214                 DEBUG(5,("group name %s members: %d\n", grp->name, num_mem));
215                 if (num_mem != 0 && mem != NULL)
216                 {
217                         BOOL ret = True;
218                         if (user_is_member(user_name, mem, num_mem))
219                         {
220                                 ret = add_domain_group(grps, num_grps, grp);
221                         }
222
223                         SAFE_FREE(mem);
224                         num_mem = 0;
225
226                         if (!ret)
227                         {
228                                 (*num_grps) = 0;
229                                 break;
230                         }
231                 }
232         }
233
234         if ((*num_grps) != 0)
235         {
236                 DEBUG(10, ("found %d user groups:\n", (*num_grps)));
237         }
238
239         endgroupent(fp);
240         return True;
241 }
242
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)
248 {
249         DOMAIN_GRP *grp;
250         void *fp = NULL;
251
252         DEBUG(10, ("enum user groups\n"));
253
254         if (grp == NULL || num_grps == NULL)
255         {
256                 return False;
257         }
258
259         (*grps) = NULL;
260         (*num_grps) = 0;
261
262         /* Open the group database file - not for update. */
263         fp = startgroupent(False);
264
265         if (fp == NULL)
266         {
267                 DEBUG(0, ("unable to open group database.\n"));
268                 return False;
269         }
270
271         /* iterate through all groups. */
272         while ((grp = getgroupent(fp, NULL, NULL)) != NULL)
273         {
274                 if (!add_domain_group(grps, num_grps, grp))
275                 {
276                         DEBUG(0,("unable to add group while enumerating\n"));
277                         return False;
278                 }
279         }
280
281         if ((*num_grps) != 0)
282         {
283                 DEBUG(10, ("found %d user groups:\n", (*num_grps)));
284         }
285
286         endgroupent(fp);
287         return True;
288 }
289
290 /***************************************************************
291  Start to enumerate the group database list. Returns a void pointer
292  to ensure no modification outside this module.
293 ****************************************************************/
294
295 void *startgroupent(BOOL update)
296 {
297   return gpdb_ops->startgroupent(update);
298 }
299
300 /***************************************************************
301  End enumeration of the group database list.
302 ****************************************************************/
303
304 void endgroupent(void *vp)
305 {
306   gpdb_ops->endgroupent(vp);
307 }
308
309 /*************************************************************************
310  Routine to return the next entry in the group database list.
311  *************************************************************************/
312
313 DOMAIN_GRP *getgroupent(void *vp, DOMAIN_GRP_MEMBER **mem, int *num_mem)
314 {
315         return gpdb_ops->getgroupent(vp, mem, num_mem);
316 }
317
318 /************************************************************************
319  Routine to add an entry to the group database file.
320 *************************************************************************/
321
322 BOOL add_group_entry(DOMAIN_GRP *newgrp)
323 {
324         return gpdb_ops->add_group_entry(newgrp);
325 }
326
327 /************************************************************************
328  Routine to search the group database file for an entry matching the groupname.
329  and then replace the entry.
330 ************************************************************************/
331
332 BOOL mod_group_entry(DOMAIN_GRP* grp)
333 {
334         return gpdb_ops->mod_group_entry(grp);
335 }
336
337 /************************************************************************
338  Routine to search group database by name.
339 *************************************************************************/
340
341 DOMAIN_GRP *getgroupnam(char *name, DOMAIN_GRP_MEMBER **mem, int *num_mem)
342 {
343         return gpdb_ops->getgroupnam(name, mem, num_mem);
344 }
345
346 /************************************************************************
347  Routine to search group database by group rid.
348 *************************************************************************/
349
350 DOMAIN_GRP *getgrouprid(uint32 group_rid, DOMAIN_GRP_MEMBER **mem, int *num_mem)
351 {
352         return gpdb_ops->getgrouprid(group_rid, mem, num_mem);
353 }
354
355 /************************************************************************
356  Routine to search group database by gid.
357 *************************************************************************/
358
359 DOMAIN_GRP *getgroupgid(gid_t gid, DOMAIN_GRP_MEMBER **mem, int *num_mem)
360 {
361         return gpdb_ops->getgroupgid(gid, mem, num_mem);
362 }
363
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)
368 {
369         return gpdb_ops->getusergroupsnam(user_name, grp, num_grps);
370 }
371
372 /*************************************************************
373  initialises a DOMAIN_GRP.
374  **************************************************************/
375
376 void gpdb_init_grp(DOMAIN_GRP *grp)
377 {
378         if (grp == NULL) return;
379         ZERO_STRUCTP(grp);
380 }
381