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