first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[gd/samba/.git] / source / groupdb / groupfile.c
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.
14  * 
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.
18  */
19
20 #include "includes.h"
21
22 #ifdef USE_SMBPASS_DB
23
24 static int gp_file_lock_depth = 0;
25 extern int DEBUGLEVEL;
26
27 static char s_readbuf[1024];
28
29 /***************************************************************
30  Start to enumerate the grppasswd list. Returns a void pointer
31  to ensure no modification outside this module.
32 ****************************************************************/
33
34 static void *startgrpfilepwent(BOOL update)
35 {
36         return startfilepwent(lp_smb_group_file(),
37                               s_readbuf, sizeof(s_readbuf),
38                               &gp_file_lock_depth, update);
39 }
40
41 /***************************************************************
42  End enumeration of the grppasswd list.
43 ****************************************************************/
44
45 static void endgrpfilepwent(void *vp)
46 {
47         endfilepwent(vp, &gp_file_lock_depth);
48 }
49
50 /*************************************************************************
51  Return the current position in the grppasswd list as an SMB_BIG_UINT.
52  This must be treated as an opaque token.
53 *************************************************************************/
54 static SMB_BIG_UINT getgrpfilepwpos(void *vp)
55 {
56         return getfilepwpos(vp);
57 }
58
59 /*************************************************************************
60  Set the current position in the grppasswd list from an SMB_BIG_UINT.
61  This must be treated as an opaque token.
62 *************************************************************************/
63 static BOOL setgrpfilepwpos(void *vp, SMB_BIG_UINT tok)
64 {
65         return setfilepwpos(vp, tok);
66 }
67
68 static BOOL make_group_line(char *p, int max_len,
69                                 DOMAIN_GRP *grp,
70                                 DOMAIN_GRP_MEMBER **mem, int *num_mem)
71 {
72         int i;
73         int len;
74         len = slprintf(p, max_len-1, "%s:%s:%d:", grp->name, grp->comment, grp->rid);
75
76         if (len == -1)
77         {
78                 DEBUG(0,("make_group_line: cannot create entry\n"));
79                 return False;
80         }
81
82         p += len;
83         max_len -= len;
84
85         if (mem == NULL || num_mem == NULL)
86         {
87                 return True;
88         }
89
90         for (i = 0; i < (*num_mem); i++)
91         {
92                 len = strlen((*mem)[i].name);
93                 p = safe_strcpy(p, (*mem)[i].name, max_len); 
94
95                 if (p == NULL)
96                 {
97                         DEBUG(0, ("make_group_line: out of space for groups!\n"));
98                         return False;
99                 }
100
101                 max_len -= len;
102
103                 if (i != (*num_mem)-1)
104                 {
105                         *p = ',';
106                         p++;
107                         max_len--;
108                 }
109         }
110
111         return True;
112 }
113
114 /*************************************************************************
115  Routine to return the next entry in the smbdomaingroup list.
116  *************************************************************************/
117 static char *get_group_members(char *p, int *num_mem, DOMAIN_GRP_MEMBER **members)
118 {
119         fstring name;
120
121         if (num_mem == NULL || members == NULL)
122         {
123                 return NULL;
124         }
125
126         (*num_mem) = 0;
127         (*members) = NULL;
128
129         while (next_token(&p, name, ",", sizeof(fstring)))
130         {
131                 (*members) = Realloc((*members), ((*num_mem)+1) * sizeof(DOMAIN_GRP_MEMBER));
132                 if ((*members) == NULL)
133                 {
134                         return NULL;
135                 }
136                 fstrcpy((*members)[(*num_mem)].name, name);
137                 (*members)[(*num_mem)].attr = 0x07;
138                 (*num_mem)++;
139         }
140         return p;
141 }
142
143 /*************************************************************************
144  Routine to return the next entry in the smbdomaingroup list.
145  *************************************************************************/
146 static DOMAIN_GRP *getgrpfilepwent(void *vp, DOMAIN_GRP_MEMBER **mem, int *num_mem)
147 {
148         /* Static buffers we will return. */
149         static DOMAIN_GRP gp_buf;
150
151         int gidval;
152
153         pstring linebuf;
154         char  *p;
155         size_t            linebuf_len;
156
157         gpdb_init_grp(&gp_buf);
158
159         /*
160          * Scan the file, a line at a time and check if the name matches.
161          */
162         while ((linebuf_len = getfileline(vp, linebuf, sizeof(linebuf))) > 0)
163         {
164                 /* get group name */
165
166                 p = strncpyn(gp_buf.name, linebuf, sizeof(gp_buf.name), ':');
167                 if (p == NULL)
168                 {
169                         DEBUG(0, ("getgrpfilepwent: malformed group entry (no :)\n"));
170                         continue;
171                 }
172
173                 /* Go past ':' */
174                 p++;
175
176                 /* get group comment */
177
178                 p = strncpyn(gp_buf.comment, p, sizeof(gp_buf.comment), ':');
179                 if (p == NULL)
180                 {
181                         DEBUG(0, ("getgrpfilepwent: malformed group entry (no :)\n"));
182                         continue;
183                 }
184
185                 /* Go past ':' */
186                 p++;
187
188                 /* Get group gid. */
189
190                 p = Atoic(p, &gidval, ":");
191
192                 if (p == NULL)
193                 {
194                         DEBUG(0, ("getgrpfilepwent: malformed group entry (no : after uid)\n"));
195                         continue;
196                 }
197
198                 /* Go past ':' */
199                 p++;
200
201                 /* now get the user's groups.  there are a maximum of 32 */
202
203                 if (mem != NULL && num_mem != NULL)
204                 {
205                         (*mem) = NULL;
206                         (*num_mem) = 0;
207
208                         p = get_group_members(p, num_mem, mem);
209                         if (p == NULL)
210                         {
211                                 DEBUG(0, ("getgrpfilepwent: malformed group entry (no : after members)\n"));
212                         }
213                 }
214
215                 /* ok, set up the static data structure and return it */
216
217                 gp_buf.rid     = pwdb_gid_to_group_rid((gid_t)gidval);
218                 gp_buf.attr    = 0x07;
219
220                 make_group_line(linebuf, sizeof(linebuf), &gp_buf, mem, num_mem);
221                 DEBUG(10,("line: '%s'\n", linebuf));
222
223                 return &gp_buf;
224         }
225
226         DEBUG(5,("getgrpfilepwent: end of file reached.\n"));
227         return NULL;
228 }
229
230 /************************************************************************
231  Routine to add an entry to the grppasswd file.
232 *************************************************************************/
233
234 static BOOL add_grpfilegrp_entry(DOMAIN_GRP *newgrp)
235 {
236         DEBUG(0, ("add_grpfilegrp_entry: NOT IMPLEMENTED\n"));
237         return False;
238 }
239
240 /************************************************************************
241  Routine to search the grppasswd file for an entry matching the groupname.
242  and then modify its group entry. We can't use the startgrppwent()/
243  getgrppwent()/endgrppwent() interfaces here as we depend on looking
244  in the actual file to decide how much room we have to write data.
245  override = False, normal
246  override = True, override XXXXXXXX'd out group or NO PASS
247 ************************************************************************/
248
249 static BOOL mod_grpfilegrp_entry(DOMAIN_GRP* grp)
250 {
251         DEBUG(0, ("mod_grpfilegrp_entry: NOT IMPLEMENTED\n"));
252         return False;
253 }
254
255
256 static struct groupdb_ops file_ops =
257 {
258         startgrpfilepwent,
259         endgrpfilepwent,
260         getgrpfilepwpos,
261         setgrpfilepwpos,
262
263         iterate_getgroupnam,          /* In groupdb.c */
264         iterate_getgroupgid,          /* In groupdb.c */
265         iterate_getgrouprid,          /* In groupdb.c */
266         getgrpfilepwent,
267
268         add_grpfilegrp_entry,
269         mod_grpfilegrp_entry,
270
271         iterate_getusergroupsnam      /* in groupdb.c */
272 };
273
274 struct groupdb_ops *file_initialise_group_db(void)
275 {    
276         return &file_ops;
277 }
278
279 #else
280  /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
281  void grppass_dummy_function(void) { } /* stop some compilers complaining */
282 #endif /* USE_SMBPASS_DB */