oopss. message for previous commit should have been
[Samba.git] / source / groupdb / groupfile.c
blob7f3cec5c1802f71e0dfb1648fff0100ecb329503
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"
22 #ifdef USE_SMBPASS_DB
24 static int gp_file_lock_depth = 0;
26 static char s_readbuf[1024];
28 /***************************************************************
29 Start to enumerate the grppasswd list. Returns a void pointer
30 to ensure no modification outside this module.
31 ****************************************************************/
33 static void *startgrpfilepwent(BOOL update)
35 return startfilepwent(lp_smb_group_file(),
36 s_readbuf, sizeof(s_readbuf),
37 &gp_file_lock_depth, update);
40 /***************************************************************
41 End enumeration of the grppasswd list.
42 ****************************************************************/
44 static void endgrpfilepwent(void *vp)
46 endfilepwent(vp, &gp_file_lock_depth);
49 /*************************************************************************
50 Return the current position in the grppasswd list as an SMB_BIG_UINT.
51 This must be treated as an opaque token.
52 *************************************************************************/
53 static SMB_BIG_UINT getgrpfilepwpos(void *vp)
55 return getfilepwpos(vp);
58 /*************************************************************************
59 Set the current position in the grppasswd list from an SMB_BIG_UINT.
60 This must be treated as an opaque token.
61 *************************************************************************/
62 static BOOL setgrpfilepwpos(void *vp, SMB_BIG_UINT tok)
64 return setfilepwpos(vp, tok);
67 static BOOL make_group_line(char *p, int max_len,
68 DOMAIN_GRP *grp,
69 DOMAIN_GRP_MEMBER **mem, int *num_mem)
71 int i;
72 int len;
73 len = slprintf(p, max_len-1, "%s:%s:%d:", grp->name, grp->comment, grp->rid);
75 if (len == -1)
77 DEBUG(0,("make_group_line: cannot create entry\n"));
78 return False;
81 p += len;
82 max_len -= len;
84 if (mem == NULL || num_mem == NULL)
86 return True;
89 for (i = 0; i < (*num_mem); i++)
91 len = strlen((*mem)[i].name);
92 p = safe_strcpy(p, (*mem)[i].name, max_len);
94 if (p == NULL)
96 DEBUG(0, ("make_group_line: out of space for groups!\n"));
97 return False;
100 max_len -= len;
102 if (i != (*num_mem)-1)
104 *p = ',';
105 p++;
106 max_len--;
110 return True;
113 /*************************************************************************
114 Routine to return the next entry in the smbdomaingroup list.
115 *************************************************************************/
116 static char *get_group_members(char *p, int *num_mem, DOMAIN_GRP_MEMBER **members)
118 fstring name;
120 if (num_mem == NULL || members == NULL)
122 return NULL;
125 (*num_mem) = 0;
126 (*members) = NULL;
128 while (next_token(&p, name, ",", sizeof(fstring)))
130 DOMAIN_GRP_MEMBER *mbrs;
132 mbrs = (DOMAIN_GRP_MEMBER *)Realloc((*members), ((*num_mem)+1) * sizeof(DOMAIN_GRP_MEMBER));
133 if (mbrs == NULL) {
134 if (*members)
135 free(*members);
136 return NULL;
137 } else
138 (*members) = mbrs;
139 fstrcpy((*members)[(*num_mem)].name, name);
140 (*members)[(*num_mem)].attr = 0x07;
141 (*num_mem)++;
143 return p;
146 /*************************************************************************
147 Routine to return the next entry in the smbdomaingroup list.
148 *************************************************************************/
149 static DOMAIN_GRP *getgrpfilepwent(void *vp, DOMAIN_GRP_MEMBER **mem, int *num_mem)
151 /* Static buffers we will return. */
152 static DOMAIN_GRP gp_buf;
154 int gidval;
156 pstring linebuf;
157 char *p;
158 size_t linebuf_len;
160 gpdb_init_grp(&gp_buf);
163 * Scan the file, a line at a time and check if the name matches.
165 while ((linebuf_len = getfileline(vp, linebuf, sizeof(linebuf))) > 0)
167 /* get group name */
169 p = strncpyn(gp_buf.name, linebuf, sizeof(gp_buf.name), ':');
170 if (p == NULL)
172 DEBUG(0, ("getgrpfilepwent: malformed group entry (no :)\n"));
173 continue;
176 /* Go past ':' */
177 p++;
179 /* get group comment */
181 p = strncpyn(gp_buf.comment, p, sizeof(gp_buf.comment), ':');
182 if (p == NULL)
184 DEBUG(0, ("getgrpfilepwent: malformed group entry (no :)\n"));
185 continue;
188 /* Go past ':' */
189 p++;
191 /* Get group gid. */
193 p = Atoic(p, &gidval, ":");
195 if (p == NULL)
197 DEBUG(0, ("getgrpfilepwent: malformed group entry (no : after uid)\n"));
198 continue;
201 /* Go past ':' */
202 p++;
204 /* now get the user's groups. there are a maximum of 32 */
206 if (mem != NULL && num_mem != NULL)
208 (*mem) = NULL;
209 (*num_mem) = 0;
211 p = get_group_members(p, num_mem, mem);
212 if (p == NULL)
214 DEBUG(0, ("getgrpfilepwent: malformed group entry (no : after members)\n"));
218 /* ok, set up the static data structure and return it */
220 gp_buf.rid = pwdb_gid_to_group_rid((gid_t)gidval);
221 gp_buf.attr = 0x07;
223 make_group_line(linebuf, sizeof(linebuf), &gp_buf, mem, num_mem);
224 DEBUG(10,("line: '%s'\n", linebuf));
226 return &gp_buf;
229 DEBUG(5,("getgrpfilepwent: end of file reached.\n"));
230 return NULL;
233 /************************************************************************
234 Routine to add an entry to the grppasswd file.
235 *************************************************************************/
237 static BOOL add_grpfilegrp_entry(DOMAIN_GRP *newgrp)
239 DEBUG(0, ("add_grpfilegrp_entry: NOT IMPLEMENTED\n"));
240 return False;
243 /************************************************************************
244 Routine to search the grppasswd file for an entry matching the groupname.
245 and then modify its group entry. We can't use the startgrppwent()/
246 getgrppwent()/endgrppwent() interfaces here as we depend on looking
247 in the actual file to decide how much room we have to write data.
248 override = False, normal
249 override = True, override XXXXXXXX'd out group or NO PASS
250 ************************************************************************/
252 static BOOL mod_grpfilegrp_entry(DOMAIN_GRP* grp)
254 DEBUG(0, ("mod_grpfilegrp_entry: NOT IMPLEMENTED\n"));
255 return False;
259 static struct groupdb_ops file_ops =
261 startgrpfilepwent,
262 endgrpfilepwent,
263 getgrpfilepwpos,
264 setgrpfilepwpos,
266 iterate_getgroupnam, /* In groupdb.c */
267 iterate_getgroupgid, /* In groupdb.c */
268 iterate_getgrouprid, /* In groupdb.c */
269 getgrpfilepwent,
271 add_grpfilegrp_entry,
272 mod_grpfilegrp_entry,
274 iterate_getusergroupsnam /* in groupdb.c */
277 struct groupdb_ops *file_initialise_group_db(void)
279 return &file_ops;
282 #else
283 /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
284 void grppass_dummy_function(void) { } /* stop some compilers complaining */
285 #endif /* USE_SMBPASS_DB */