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