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