preparing for release of alpha.2.5
[Samba.git] / source / groupdb / groupfile.c
blobb43494769191de6ac65c4074c1561f7e3ee961f2
1 /*
2 * Unix SMB/Netbios implementation. Version 1.9. SMB parameters and setup
3 * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 675
17 * Mass Ave, Cambridge, MA 02139, USA.
20 #include "includes.h"
21 #include "sids.h"
23 #ifdef USE_SMBGROUP_DB
25 static int gp_file_lock_depth = 0;
26 extern int DEBUGLEVEL;
28 static char s_readbuf[1024];
30 /***************************************************************
31 Start to enumerate the grppasswd list. Returns a void pointer
32 to ensure no modification outside this module.
33 ****************************************************************/
35 static void *startgrpfilepwent(BOOL update)
37 return startfileent(lp_smb_group_file(),
38 s_readbuf, sizeof(s_readbuf),
39 &gp_file_lock_depth, update);
42 /***************************************************************
43 End enumeration of the grppasswd list.
44 ****************************************************************/
46 static void endgrpfilepwent(void *vp)
48 endfileent(vp, &gp_file_lock_depth);
51 /*************************************************************************
52 Return the current position in the grppasswd list as an SMB_BIG_UINT.
53 This must be treated as an opaque token.
54 *************************************************************************/
55 static SMB_BIG_UINT getgrpfilepwpos(void *vp)
57 return getfilepwpos(vp);
60 /*************************************************************************
61 Set the current position in the grppasswd list from an SMB_BIG_UINT.
62 This must be treated as an opaque token.
63 *************************************************************************/
64 static BOOL setgrpfilepwpos(void *vp, SMB_BIG_UINT tok)
66 return setfilepwpos(vp, tok);
70 /*************************************************************************
71 Routine to return the next entry in the smbdomaingroup list.
72 *************************************************************************/
73 static char *get_group_members(char *p, int *num_mem, DOMAIN_GRP_MEMBER **members)
75 fstring name;
77 if (num_mem == NULL || members == NULL)
79 return NULL;
82 (*num_mem) = 0;
83 (*members) = NULL;
85 while (next_token(&p, name, ",", sizeof(fstring)))
87 DOM_SID sid;
88 uint8 type;
89 BOOL found = False;
91 if (isdigit(name))
93 uint32 rid = get_number(name);
94 sid_copy(&sid, &global_sam_sid);
95 sid_append_rid(&sid, rid);
97 found = lookup_sid(&sid, name, &type) == 0x0;
99 else
101 found = lookup_name(name, &sid, &type) == 0x0;
104 if (!found)
106 DEBUG(0,("group database: could not resolve name %s in domain %s\n",
107 name, global_sam_name));
108 continue;
111 (*members) = Realloc((*members), ((*num_mem)+1) * sizeof(DOMAIN_GRP_MEMBER));
112 if ((*members) == NULL)
114 return NULL;
117 fstrcpy((*members)[(*num_mem)].name, name);
118 (*members)[(*num_mem)].attr = 0x07;
119 (*num_mem)++;
121 return p;
124 /*************************************************************************
125 Routine to return the next entry in the smbdomaingroup list.
126 *************************************************************************/
127 static DOMAIN_GRP *getgrpfilepwent(void *vp, DOMAIN_GRP_MEMBER **mem, int *num_mem)
129 /* Static buffers we will return. */
130 static DOMAIN_GRP gp_buf;
131 DOM_NAME_MAP gmep;
133 int gidval;
135 pstring linebuf;
136 char *p;
138 gpdb_init_grp(&gp_buf);
141 * Scan the file, a line at a time and check if the name matches.
143 while (getfileline(vp, linebuf, sizeof(linebuf)) > 0)
145 /* get group name */
147 p = strncpyn(gp_buf.name, linebuf, sizeof(gp_buf.name), ':');
148 if (p == NULL)
150 DEBUG(0, ("getgrpfilepwent: malformed group entry (no :)\n"));
151 continue;
154 /* Go past ':' */
155 p++;
157 /* get group comment */
159 p = strncpyn(gp_buf.comment, p, sizeof(gp_buf.comment), ':');
160 if (p == NULL)
162 DEBUG(0, ("getgrpfilepwent: malformed group entry (no :)\n"));
163 continue;
166 /* Go past ':' */
167 p++;
169 /* Get group gid. */
171 p = Atoic(p, &gidval, ":");
173 if (p == NULL)
175 DEBUG(0, ("getgrpfilepwent: malformed group entry (no : after uid)\n"));
176 continue;
179 /* Go past ':' */
180 p++;
182 /* now get the user's groups. there are a maximum of 32 */
184 if (mem != NULL && num_mem != NULL)
186 (*mem) = NULL;
187 (*num_mem) = 0;
189 p = get_group_members(p, num_mem, mem);
190 if (p == NULL)
192 DEBUG(0, ("getgrpfilepwent: malformed group entry (no : after members)\n"));
196 /* ok, set up the static data structure and return it */
198 if (!lookupsmbgrpgid((gid_t)gidval, &gmep))
200 continue;
202 if (gmep.type != SID_NAME_DOM_GRP &&
203 gmep.type != SID_NAME_WKN_GRP))
205 continue;
208 sid_split_rid(&gmep.sid, &gp_buf.rid);
209 if (!sid_equal(&gmep.sid, &global_sam_sid))
211 continue;
214 gp_buf.attr = 0x07;
216 make_group_line(linebuf, sizeof(linebuf), &gp_buf, mem, num_mem);
217 DEBUG(10,("line: '%s'\n", linebuf));
219 return &gp_buf;
222 DEBUG(5,("getgrpfilepwent: end of file reached.\n"));
223 return NULL;
226 /************************************************************************
227 Routine to add an entry to the grppasswd file.
228 *************************************************************************/
230 static BOOL add_grpfilegrp_entry(DOMAIN_GRP *newgrp)
232 DEBUG(0, ("add_grpfilegrp_entry: NOT IMPLEMENTED\n"));
233 return False;
236 /************************************************************************
237 Routine to search the grppasswd file for an entry matching the groupname.
238 and then modify its group entry.
239 ************************************************************************/
241 static BOOL mod_grpfilegrp_entry(DOMAIN_GRP* grp)
243 DEBUG(0, ("mod_grpfilegrp_entry: NOT IMPLEMENTED\n"));
244 return False;
248 static struct groupdb_ops file_ops =
250 startgrpfilepwent,
251 endgrpfilepwent,
252 getgrpfilepwpos,
253 setgrpfilepwpos,
255 iterate_getgroupntnam, /* In groupdb.c */
256 iterate_getgroupgid, /* In groupdb.c */
257 iterate_getgrouprid, /* In groupdb.c */
258 getgrpfilepwent,
260 add_grpfilegrp_entry,
261 mod_grpfilegrp_entry,
263 iterate_getusergroupntnam /* in groupdb.c */
266 struct groupdb_ops *file_initialise_group_db(void)
268 return &file_ops;
271 #else
272 /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
273 void grppass_dummy_function(void) { } /* stop some compilers complaining */
274 #endif /* USE_SMBPASS_DB */