preparing for release of alpha.1.4
[Samba.git] / source / groupdb / aliasdb.c
blob2dce670994e1d3fab08ffa28dfe7c79debdc5335
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"
25 #include "sids.h"
27 extern int DEBUGLEVEL;
30 * NOTE. All these functions are abstracted into a structure
31 * that points to the correct function for the selected database. JRA.
34 static struct aliasdb_ops *aldb_ops = NULL;
36 /***************************************************************
37 Initialise the alias db operations.
38 ***************************************************************/
40 BOOL initialise_alias_db(void)
42 if (aldb_ops)
44 return True;
47 #ifdef WITH_NISPLUS
48 aldb_ops = nisplus_initialise_alias_db();
49 #elif defined(WITH_NT5LDAP)
50 aldb_ops = nt5ldap_initialise_alias_db();
51 #elif defined(WITH_LDAP)
52 aldb_ops = ldap_initialise_alias_db();
53 #elif defined(USE_SMBUNIX_DB)
54 aldb_ops = unix_initialise_alias_db();
55 #endif
57 return (aldb_ops != NULL);
61 * Functions that return/manipulate a LOCAL_GRP.
64 /************************************************************************
65 Utility function to search alias database by gid: the LOCAL_GRP
66 structure does not have a gid member, so we have to convert here
67 from gid to alias rid.
68 *************************************************************************/
69 LOCAL_GRP *iterate_getaliasgid(gid_t gid, LOCAL_GRP_MEMBER **mem, int *num_mem)
71 DOM_NAME_MAP gmep;
72 uint32 rid;
73 if (!lookupsmbgrpgid(gid, &gmep))
75 DEBUG(0,("iterate_getaliasgid: gid %d does not map to one of our Domain's Aliases\n", gid));
76 return NULL;
79 if (gmep.type != SID_NAME_ALIAS )
81 DEBUG(0,("iterate_getaliasgid: gid %d does not map to one of our Domain's Aliases\n", gid));
82 return NULL;
85 sid_split_rid(&gmep.sid, &rid);
86 if (!sid_equal(&gmep.sid, &global_sam_sid))
88 DEBUG(0,("iterate_getaliasgid: gid %d does not map into our Domain SID\n", gid));
89 return NULL;
92 return iterate_getaliasrid(rid, mem, num_mem);
95 /************************************************************************
96 Utility function to search alias database by rid. use this if your database
97 does not have search facilities.
98 *************************************************************************/
99 LOCAL_GRP *iterate_getaliasrid(uint32 rid, LOCAL_GRP_MEMBER **mem, int *num_mem)
101 LOCAL_GRP *als = NULL;
102 void *fp = NULL;
104 DEBUG(10, ("search by rid: 0x%x\n", rid));
106 /* Open the alias database file - not for update. */
107 fp = startaliasent(False);
109 if (fp == NULL)
111 DEBUG(0, ("unable to open alias database.\n"));
112 return NULL;
115 while ((als = getaliasent(fp, mem, num_mem)) != NULL && als->rid != rid)
117 DEBUG(10,("iterate: %s 0x%x", als->name, als->rid));
120 if (als != NULL)
122 DEBUG(10, ("found alias %s by rid: 0x%x\n", als->name, rid));
125 endaliasent(fp);
126 return als;
129 /************************************************************************
130 Utility function to search alias database by name. use this if your database
131 does not have search facilities.
132 *************************************************************************/
133 LOCAL_GRP *iterate_getaliasntnam(const char *name, LOCAL_GRP_MEMBER **mem, int *num_mem)
135 LOCAL_GRP *als = NULL;
136 void *fp = NULL;
138 DEBUG(10, ("search by name: %s\n", name));
140 /* Open the alias database file - not for update. */
141 fp = startaliasent(False);
143 if (fp == NULL)
145 DEBUG(0, ("unable to open alias database.\n"));
146 return NULL;
149 while ((als = getaliasent(fp, mem, num_mem)) != NULL && !strequal(als->name, name))
153 if (als != NULL)
155 DEBUG(10, ("found by name: %s\n", name));
158 endaliasent(fp);
159 return als;
162 /*************************************************************************
163 Routine to return the next entry in the smbdomainalias list.
164 *************************************************************************/
165 BOOL add_domain_alias(LOCAL_GRP **alss, int *num_alss, LOCAL_GRP *als)
167 if (alss == NULL || num_alss == NULL || als == NULL)
169 return False;
172 (*alss) = Realloc((*alss), ((*num_alss)+1) * sizeof(LOCAL_GRP));
173 if ((*alss) == NULL)
175 return False;
178 DEBUG(10,("adding alias %s(%s)\n", als->name, als->comment));
180 fstrcpy((*alss)[(*num_alss)].name , als->name);
181 fstrcpy((*alss)[(*num_alss)].comment, als->comment);
182 (*alss)[(*num_alss)].rid = als->rid;
184 (*num_alss)++;
186 return True;
189 /*************************************************************************
190 checks to see if a user is a member of a domain alias
191 *************************************************************************/
192 static BOOL user_is_member(const char *user_name, LOCAL_GRP_MEMBER *mem, int num_mem)
194 int i;
195 pstring name;
196 slprintf(name, sizeof(name)-1, "%s\\%s", global_sam_name, user_name);
198 for (i = 0; i < num_mem; i++)
200 DEBUG(10,("searching against user %s...\n", mem[i].name));
201 if (strequal(mem[i].name, name))
203 DEBUG(10,("searching for user %s: found\n", name));
204 return True;
207 DEBUG(10,("searching for user %s: not found\n", name));
208 return False;
211 /*************************************************************************
212 gets an array of aliases that a user is in. use this if your database
213 does not have search facilities
214 *************************************************************************/
215 BOOL iterate_getuseraliasntnam(const char *user_name, LOCAL_GRP **alss, int *num_alss)
217 LOCAL_GRP *als = NULL;
218 LOCAL_GRP_MEMBER *mem = NULL;
219 int num_mem = 0;
220 void *fp = NULL;
222 DEBUG(10, ("search for useralias by name: %s\n", user_name));
224 if (user_name == NULL || alss == NULL || num_alss == NULL)
226 return False;
229 (*alss) = NULL;
230 (*num_alss) = 0;
232 /* Open the alias database file - not for update. */
233 fp = startaliasent(False);
235 if (fp == NULL)
237 DEBUG(0, ("unable to open alias database.\n"));
238 return False;
241 /* iterate through all aliases. search members for required user */
242 while ((als = getaliasent(fp, &mem, &num_mem)) != NULL)
244 DEBUG(5,("alias name %s members: %d\n", als->name, num_mem));
245 if (num_mem != 0 && mem != NULL)
247 BOOL ret = True;
248 if (user_is_member(user_name, mem, num_mem))
250 ret = add_domain_alias(alss, num_alss, als);
253 free(mem);
254 mem = NULL;
255 num_mem = 0;
257 if (!ret)
259 (*num_alss) = 0;
260 break;
265 if ((*num_alss) != 0)
267 DEBUG(10, ("found %d user aliases:\n", (*num_alss)));
270 endaliasent(fp);
271 return True;
274 /*************************************************************************
275 gets an array of aliases that a user is in. use this if your database
276 does not have search facilities
277 *************************************************************************/
278 BOOL enumdomaliases(LOCAL_GRP **alss, int *num_alss)
280 LOCAL_GRP *als = NULL;
281 void *fp = NULL;
283 DEBUG(10, ("enum user aliases\n"));
285 if (alss == NULL || num_alss == NULL)
287 return False;
290 (*alss) = NULL;
291 (*num_alss) = 0;
293 /* Open the alias database file - not for update. */
294 fp = startaliasent(False);
296 if (fp == NULL)
298 DEBUG(0, ("unable to open alias database.\n"));
299 return False;
302 /* iterate through all aliases. */
303 while ((als = getaliasent(fp, NULL, NULL)) != NULL)
305 if (!add_domain_alias(alss, num_alss, als))
307 DEBUG(0,("unable to add alias while enumerating\n"));
308 return False;
312 if ((*num_alss) != 0)
314 DEBUG(10, ("found %d user aliases:\n", (*num_alss)));
317 endaliasent(fp);
318 return True;
321 /***************************************************************
322 Start to enumerate the alias database list. Returns a void pointer
323 to ensure no modification outside this module.
324 ****************************************************************/
326 void *startaliasent(BOOL update)
328 return aldb_ops->startaliasent(update);
331 /***************************************************************
332 End enumeration of the alias database list.
333 ****************************************************************/
335 void endaliasent(void *vp)
337 aldb_ops->endaliasent(vp);
340 /*************************************************************************
341 Routine to return the next entry in the alias database list.
342 *************************************************************************/
344 LOCAL_GRP *getaliasent(void *vp, LOCAL_GRP_MEMBER **mem, int *num_mem)
346 return aldb_ops->getaliasent(vp, mem, num_mem);
349 /************************************************************************
350 Routine to add an entry to the alias database file.
351 on entry, the entry is added by name.
352 on exit, the RID is expected to have been set.
353 *************************************************************************/
354 BOOL add_alias_entry(LOCAL_GRP *newgrp)
356 BOOL ret;
357 if (newgrp->rid != 0xffffffff)
359 DEBUG(0,("add_alias_entry - RID must be 0xffffffff, \
360 database instance is responsible for allocating the RID, not you.\n"));
361 return False;
363 ret = aldb_ops->add_alias_entry(newgrp);
364 if (newgrp->rid == 0xffffffff)
366 DEBUG(0,("add_alias_entry - RID has not been set by database\n"));
367 return False;
369 return ret;
372 /************************************************************************
373 Routine to search the alias database file for an entry matching the aliasname.
374 and then replace the entry.
375 ************************************************************************/
377 BOOL mod_alias_entry(LOCAL_GRP* als)
379 return aldb_ops->mod_alias_entry(als);
382 /************************************************************************
383 Routine to delete alias database entry matching by rid.
384 ************************************************************************/
385 BOOL del_alias_entry(uint32 rid)
387 return aldb_ops->del_alias_entry(rid);
390 /************************************************************************
391 Routine to add a member to an entry in the alias database file.
392 *************************************************************************/
393 BOOL add_alias_member(uint32 rid, const DOM_SID *member_sid)
395 return aldb_ops->add_alias_member(rid, member_sid);
398 /************************************************************************
399 Routine to delete a member from an entry in the alias database file.
400 *************************************************************************/
401 BOOL del_alias_member(uint32 rid, const DOM_SID *member_sid)
403 return aldb_ops->del_alias_member(rid, member_sid);
405 /************************************************************************
406 Routine to search alias database by name.
407 *************************************************************************/
409 LOCAL_GRP *getaliasntnam(const char *name, LOCAL_GRP_MEMBER **mem, int *num_mem)
411 return aldb_ops->getaliasntnam(name, mem, num_mem);
414 /************************************************************************
415 Routine to search alias database by alias rid.
416 *************************************************************************/
418 LOCAL_GRP *getaliasrid(uint32 alias_rid, LOCAL_GRP_MEMBER **mem, int *num_mem)
420 return aldb_ops->getaliasrid(alias_rid, mem, num_mem);
423 /************************************************************************
424 Routine to search alias database by gid.
425 *************************************************************************/
427 LOCAL_GRP *getaliasgid(gid_t gid, LOCAL_GRP_MEMBER **mem, int *num_mem)
429 return aldb_ops->getaliasgid(gid, mem, num_mem);
432 /*************************************************************************
433 gets an array of aliases that a user is in.
434 *************************************************************************/
435 BOOL getuseraliasntnam(const char *user_name, LOCAL_GRP **als, int *num_alss)
437 return aldb_ops->getuseraliasntnam(user_name, als, num_alss);
440 /*************************************************************
441 initialises a LOCAL_GRP.
442 **************************************************************/
443 void aldb_init_als(LOCAL_GRP *als)
445 if (als == NULL) return;
446 ZERO_STRUCTP(als);
449 /*************************************************************
450 turns an alias entry into a string.
451 **************************************************************/
452 BOOL make_alias_line(char *p, int max_len,
453 LOCAL_GRP *als,
454 LOCAL_GRP_MEMBER **mem, int *num_mem)
456 int i;
457 int len;
458 len = slprintf(p, max_len-1, "%s:%s:%d:", als->name, als->comment, als->rid);
460 if (len == -1)
462 DEBUG(0,("make_alias_line: cannot create entry\n"));
463 return False;
466 p += len;
467 max_len -= len;
469 if (mem == NULL || num_mem == NULL)
471 return True;
474 for (i = 0; i < (*num_mem); i++)
476 len = strlen((*mem)[i].name);
477 p = safe_strcpy(p, (*mem)[i].name, max_len);
479 if (p == NULL)
481 DEBUG(0, ("make_alias_line: out of space for aliases!\n"));
482 return False;
485 max_len -= len;
487 if (i != (*num_mem)-1)
489 *p = ',';
490 p++;
491 max_len--;
495 return True;