Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / samba / source / groupdb / aliasdb.c
blobe5e6ebfa53f179e0c527dd33602609eff26fee8c
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
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.
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.
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.
23 #include "includes.h"
24 #include "nterr.h"
26 extern int DEBUGLEVEL;
28 extern fstring global_sam_name;
31 * NOTE. All these functions are abstracted into a structure
32 * that points to the correct function for the selected database. JRA.
35 static struct aliasdb_ops *aldb_ops;
37 /***************************************************************
38 Initialise the alias db operations.
39 ***************************************************************/
41 BOOL initialise_alias_db(void)
43 if (aldb_ops)
45 return True;
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
56 return (aldb_ops != NULL);
60 * Functions that return/manipulate a LOCAL_GRP.
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)
70 return iterate_getaliasrid(pwdb_gid_to_alias_rid(gid), mem, num_mem);
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)
79 LOCAL_GRP *als = NULL;
80 void *fp = NULL;
82 DEBUG(10, ("search by rid: 0x%x\n", rid));
84 /* Open the alias database file - not for update. */
85 fp = startaliasent(False);
87 if (fp == NULL)
89 DEBUG(0, ("unable to open alias database.\n"));
90 return NULL;
93 while ((als = getaliasent(fp, mem, num_mem)) != NULL && als->rid != rid)
97 if (als != NULL)
99 DEBUG(10, ("found alias %s by rid: 0x%x\n", als->name, rid));
102 endaliasent(fp);
103 return als;
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)
112 LOCAL_GRP *als = NULL;
113 void *fp = NULL;
115 DEBUG(10, ("search by name: %s\n", name));
117 /* Open the alias database file - not for update. */
118 fp = startaliasent(False);
120 if (fp == NULL)
122 DEBUG(0, ("unable to open alias database.\n"));
123 return NULL;
126 while ((als = getaliasent(fp, mem, num_mem)) != NULL && !strequal(als->name, name))
130 if (als != NULL)
132 DEBUG(10, ("found by name: %s\n", name));
135 endaliasent(fp);
136 return als;
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)
144 if (alss == NULL || num_alss == NULL || als == NULL)
146 return False;
149 (*alss) = Realloc((*alss), ((*num_alss)+1) * sizeof(LOCAL_GRP));
150 if ((*alss) == NULL)
152 return False;
155 DEBUG(10,("adding alias %s(%s)\n", als->name, als->comment));
157 fstrcpy((*alss)[(*num_alss)].name , als->name);
158 fstrcpy((*alss)[(*num_alss)].comment, als->comment);
159 (*alss)[(*num_alss)].rid = als->rid;
161 (*num_alss)++;
163 return True;
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)
171 int i;
172 pstring name;
173 slprintf(name, sizeof(name)-1, "\\%s\\%s", global_sam_name, user_name);
175 for (i = 0; i < num_mem; i++)
177 DEBUG(10,("searching against user %s...\n", mem[i].name));
178 if (strequal(mem[i].name, name))
180 DEBUG(10,("searching for user %s: found\n", name));
181 return True;
184 DEBUG(10,("searching for user %s: not found\n", name));
185 return False;
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)
194 LOCAL_GRP *als;
195 LOCAL_GRP_MEMBER *mem = NULL;
196 int num_mem = 0;
197 void *fp = NULL;
199 DEBUG(10, ("search for useralias by name: %s\n", user_name));
201 if (user_name == NULL || als == NULL || num_alss == NULL)
203 return False;
206 (*alss) = NULL;
207 (*num_alss) = 0;
209 /* Open the alias database file - not for update. */
210 fp = startaliasent(False);
212 if (fp == NULL)
214 DEBUG(0, ("unable to open alias database.\n"));
215 return False;
218 /* iterate through all aliases. search members for required user */
219 while ((als = getaliasent(fp, &mem, &num_mem)) != NULL)
221 DEBUG(5,("alias name %s members: %d\n", als->name, num_mem));
222 if (num_mem != 0 && mem != NULL)
224 BOOL ret = True;
225 if (user_is_member(user_name, mem, num_mem))
227 ret = add_domain_alias(alss, num_alss, als);
230 free(mem);
231 mem = NULL;
232 num_mem = 0;
234 if (!ret)
236 (*num_alss) = 0;
237 break;
242 if ((*num_alss) != 0)
244 DEBUG(10, ("found %d user aliases:\n", (*num_alss)));
247 endaliasent(fp);
248 return True;
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)
257 LOCAL_GRP *als;
258 void *fp = NULL;
260 DEBUG(10, ("enum user aliases\n"));
262 if (als == NULL || num_alss == NULL)
264 return False;
267 (*alss) = NULL;
268 (*num_alss) = 0;
270 /* Open the alias database file - not for update. */
271 fp = startaliasent(False);
273 if (fp == NULL)
275 DEBUG(0, ("unable to open alias database.\n"));
276 return False;
279 /* iterate through all aliases. */
280 while ((als = getaliasent(fp, NULL, NULL)) != NULL)
282 if (!add_domain_alias(alss, num_alss, als))
284 DEBUG(0,("unable to add alias while enumerating\n"));
285 return False;
289 if ((*num_alss) != 0)
291 DEBUG(10, ("found %d user aliases:\n", (*num_alss)));
294 endaliasent(fp);
295 return True;
298 /***************************************************************
299 Start to enumerate the alias database list. Returns a void pointer
300 to ensure no modification outside this module.
301 ****************************************************************/
303 void *startaliasent(BOOL update)
305 return aldb_ops->startaliasent(update);
308 /***************************************************************
309 End enumeration of the alias database list.
310 ****************************************************************/
312 void endaliasent(void *vp)
314 aldb_ops->endaliasent(vp);
317 /*************************************************************************
318 Routine to return the next entry in the alias database list.
319 *************************************************************************/
321 LOCAL_GRP *getaliasent(void *vp, LOCAL_GRP_MEMBER **mem, int *num_mem)
323 return aldb_ops->getaliasent(vp, mem, num_mem);
326 /************************************************************************
327 Routine to add an entry to the alias database file.
328 *************************************************************************/
330 BOOL add_alias_entry(LOCAL_GRP *newals)
332 return aldb_ops->add_alias_entry(newals);
335 /************************************************************************
336 Routine to search the alias database file for an entry matching the aliasname.
337 and then replace the entry.
338 ************************************************************************/
340 BOOL mod_alias_entry(LOCAL_GRP* als)
342 return aldb_ops->mod_alias_entry(als);
345 /************************************************************************
346 Routine to search alias database by name.
347 *************************************************************************/
349 LOCAL_GRP *getaliasnam(char *name, LOCAL_GRP_MEMBER **mem, int *num_mem)
351 return aldb_ops->getaliasnam(name, mem, num_mem);
354 /************************************************************************
355 Routine to search alias database by alias rid.
356 *************************************************************************/
358 LOCAL_GRP *getaliasrid(uint32 alias_rid, LOCAL_GRP_MEMBER **mem, int *num_mem)
360 return aldb_ops->getaliasrid(alias_rid, mem, num_mem);
363 /************************************************************************
364 Routine to search alias database by gid.
365 *************************************************************************/
367 LOCAL_GRP *getaliasgid(gid_t gid, LOCAL_GRP_MEMBER **mem, int *num_mem)
369 return aldb_ops->getaliasgid(gid, mem, num_mem);
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)
377 return aldb_ops->getuseraliasnam(user_name, als, num_alss);
380 /*************************************************************
381 initialises a LOCAL_GRP.
382 **************************************************************/
384 void aldb_init_als(LOCAL_GRP *als)
386 if (als == NULL) return;
387 ZERO_STRUCTP(als);