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