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