last unix group not being listed. spotted by jacques sansdrap.
[samba.git] / source3 / groupdb / groupunix.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_SMBUNIX_DB
23
24 extern int DEBUGLEVEL;
25
26
27 extern DOM_SID global_sam_sid;
28
29 /***************************************************************
30  Start to enumerate the grppasswd list. Returns a void pointer
31  to ensure no modification outside this module.
32 ****************************************************************/
33
34 struct unix_entries
35 {
36         struct group *grps;
37         int num_grps;
38         int grp_idx;
39 };
40
41 static void *startgrpunixpwent(BOOL update)
42 {
43         struct unix_entries *grps;
44         grps = (struct unix_entries*)malloc(sizeof(struct unix_entries));
45
46         if (grps == NULL)
47         {
48                 return NULL;
49         }
50
51         if (!get_unix_grps(&grps->num_grps, &grps->grps))
52         {
53                 free(grps);
54                 return NULL;
55         }
56
57         grps->grp_idx = 0;
58
59         return (void*)grps;
60 }
61
62 /***************************************************************
63  End enumeration of the grppasswd list.
64 ****************************************************************/
65
66 static void endgrpunixpwent(void *vp)
67 {
68         struct unix_entries *grps = (struct unix_entries *)vp;
69
70         if (grps != NULL)
71         {
72                 free_unix_grps(grps->num_grps, grps->grps);
73                 free(vp);
74         }
75 }
76
77 /*************************************************************************
78  Return the current position in the grppasswd list as an SMB_BIG_UINT.
79  This must be treated as an opaque token.
80 *************************************************************************/
81 static SMB_BIG_UINT getgrpunixpwpos(void *vp)
82 {
83         return (SMB_BIG_UINT)0;
84 }
85
86 /*************************************************************************
87  Set the current position in the grppasswd list from an SMB_BIG_UINT.
88  This must be treated as an opaque token.
89 *************************************************************************/
90 static BOOL setgrpunixpwpos(void *vp, SMB_BIG_UINT tok)
91 {
92         return False;
93 }
94
95 /*************************************************************************
96  Routine to return the next entry in the smbdomaingroup list.
97  *************************************************************************/
98 BOOL get_unixgroup_members(struct group *grp,
99                                 int *num_mem, DOMAIN_GRP_MEMBER **members)
100 {
101         int i;
102         char *unix_name;
103
104         if (num_mem == NULL || members == NULL)
105         {
106                 return False;
107         }
108
109         (*num_mem) = 0;
110         (*members) = NULL;
111
112         for (i = 0; (unix_name = grp->gr_mem[i]) != NULL; i++)
113         {
114                 DOM_NAME_MAP gmep;
115                 DOMAIN_GRP_MEMBER *mem;
116                 uint32 rid;
117
118                 if (!lookupsmbpwnam (unix_name, &gmep) &&
119                     !lookupsmbgrpnam(unix_name, &gmep))
120                 {
121                         continue;
122                 }
123
124                 if (gmep.type != SID_NAME_DOM_GRP &&
125                     gmep.type != SID_NAME_USER &&
126                     gmep.type != SID_NAME_WKN_GRP)
127                 {
128                         DEBUG(0,("group database: name %s is not in a Domain Group\n",
129                                   unix_name));
130                         continue;
131                 }
132                         
133                 sid_split_rid(&gmep.sid, &rid);
134                 if (!sid_equal(&global_sam_sid, &gmep.sid))
135                 {
136                         DEBUG(0,("group database: could not resolve name %s (wrong Domain SID)\n",
137                                   unix_name));
138                         continue;
139                 }
140
141                 (*members) = Realloc((*members), ((*num_mem)+1) * sizeof(DOMAIN_GRP_MEMBER));
142                 if ((*members) == NULL)
143                 {
144                         return False;
145                 }
146
147                 mem = &(*members)[(*num_mem)];
148                 (*num_mem)++;
149
150                 fstrcpy(mem->name, gmep.nt_name);
151                 mem->attr    = 0x07;
152                 mem->sid_use = gmep.type;
153                 mem->rid     = rid;
154         }
155         return True;
156 }
157
158 /*************************************************************************
159  Routine to return the next entry in the domain group list.
160
161  if we are not a PDC or BDC, then we do NOT support Domain groups, only
162  aliases.  try running MUSRMGR.EXE or running USRMGR.EXE selected on a
163  workstation, you will find that no Domain groups are listed: only aliases.
164
165  so, as a PDC or BDC, all unix groups not explicitly mapped using
166  map_group_gid() are treated as Domain groups.
167
168  *************************************************************************/
169 static DOMAIN_GRP *getgrpunixpwent(void *vp, DOMAIN_GRP_MEMBER **mem, int *num_mem)
170 {
171         /* Static buffers we will return. */
172         static DOMAIN_GRP gp_buf;
173         struct group *unix_grp = NULL;
174         struct unix_entries *grps = (struct unix_entries *)vp;
175
176         if (grps == NULL)
177         {
178                 return NULL;
179         }
180
181         if (lp_server_role() == ROLE_DOMAIN_NONE || 
182             lp_server_role() == ROLE_DOMAIN_MEMBER)
183         {
184                 /*
185                  * only PDC and BDC have domain groups in the SAM.
186                  * (however as member of domain you can have LOCAL groups,
187                  * but that's dealt with in the aliasdb...)
188                  */
189
190                 return NULL;
191         }
192
193         gpdb_init_grp(&gp_buf);
194
195         fstrcpy(gp_buf.comment, "");
196         gp_buf.attr    = 0x07;
197
198         /* get array of unix names + gids.  this function does NOT
199            get a copy of the unix group members
200          */
201
202         /* cycle through unix groups */
203         for (; grps->grp_idx < grps->num_grps; grps->grp_idx++)
204         {
205                 DOM_NAME_MAP gmep;
206
207                 unix_grp = &grps->grps[grps->grp_idx];
208
209                 DEBUG(10,("getgrpunixpwent: enum unix group entry %s\n",
210                            unix_grp->gr_name));
211                         
212                 if (!lookupsmbgrpgid(unix_grp->gr_gid, &gmep))
213                 {
214                         continue;
215                 }
216
217                 if (gmep.type != SID_NAME_DOM_GRP &&
218                     gmep.type != SID_NAME_WKN_GRP)
219                 {
220                         continue;
221                 }
222
223                 sid_split_rid(&gmep.sid, &gp_buf.rid);
224                 if (!sid_equal(&gmep.sid, &global_sam_sid))
225                 {
226                         continue;
227                 }
228
229                 fstrcpy(gp_buf.name, gmep.nt_name);
230                 break;
231         }
232
233         if (unix_grp == NULL || grps->grp_idx >= grps->num_grps)
234         {
235                 return NULL;
236         }
237
238         /* get the user's domain groups.  there are a maximum of 32 */
239
240         if (mem != NULL && num_mem != NULL)
241         {
242                 (*mem) = NULL;
243                 (*num_mem) = 0;
244
245                 unix_grp = getgrgid(unix_grp->gr_gid);
246                 get_unixgroup_members(unix_grp, num_mem, mem);
247         }
248
249         {
250                 pstring linebuf;
251                 make_group_line(linebuf, sizeof(linebuf), &gp_buf, mem, num_mem);
252                 DEBUG(10,("line: '%s'\n", linebuf));
253         }
254
255         grps->grp_idx++; /* advance so next enum gets next entry */
256         return &gp_buf;
257 }
258
259 /************************************************************************
260  Routine to add an entry to the grppasswd file.
261 *************************************************************************/
262
263 static BOOL add_grpunixgrp_entry(DOMAIN_GRP *newgrp)
264 {
265         DEBUG(0, ("add_grpunixgrp_entry: NOT IMPLEMENTED\n"));
266         return False;
267 }
268
269 /************************************************************************
270  Routine to search database for entry matching the groupname and/or rid.
271  and then modify its group entry. 
272 ************************************************************************/
273
274 static BOOL mod_grpunixgrp_entry(DOMAIN_GRP* grp)
275 {
276         DEBUG(0, ("mod_grpunixgrp_entry: NOT IMPLEMENTED\n"));
277         return False;
278 }
279
280 /************************************************************************
281  Routine to search the grppasswd file for an entry matching the rid.
282  and then delete it.
283 ************************************************************************/
284
285 static BOOL del_grpunixgrp_entry(uint32 rid)
286 {
287         DEBUG(0, ("del_grpunixgrp_entry: NOT IMPLEMENTED\n"));
288         return False;
289 }
290
291 /************************************************************************
292  Routine to add a member to an entry to the grppasswd file.
293 *************************************************************************/
294 static BOOL add_grpunixgrp_member(uint32 rid, uint32 member_rid)
295 {
296         DEBUG(0, ("add_grpunixgrp_member: NOT IMPLEMENTED\n"));
297         return False;
298 }
299
300 /************************************************************************
301  Routine to delete a member from an entry to the grppasswd file.
302 *************************************************************************/
303 static BOOL del_grpunixgrp_member(uint32 rid, uint32 member_rid)
304 {
305         DEBUG(0, ("del_grpunixgrp_member: NOT IMPLEMENTED\n"));
306         return False;
307 }
308
309 static struct groupdb_ops unix_ops =
310 {
311         startgrpunixpwent,
312         endgrpunixpwent,
313         getgrpunixpwpos,
314         setgrpunixpwpos,
315
316         iterate_getgroupntnam,          /* In groupdb.c */
317         iterate_getgroupgid,          /* In groupdb.c */
318         iterate_getgrouprid,          /* In groupdb.c */
319         getgrpunixpwent,
320
321         add_grpunixgrp_entry,
322         mod_grpunixgrp_entry,
323         del_grpunixgrp_entry,
324
325         add_grpunixgrp_member,
326         del_grpunixgrp_member,
327
328         iterate_getusergroupsnam      /* in groupdb.c */
329 };
330
331 struct groupdb_ops *unix_initialise_group_db(void)
332 {    
333         return &unix_ops;
334 }
335
336 #else
337  /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
338  void unix_grppass_dummy_function(void) { } /* stop some compilers complaining */
339 #endif /* USE_SMBPASS_DB */