weekend work. user / group database API.
[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 static void *startgrpunixpwent(BOOL update)
35 {
36         setgrent();
37         return (void*)(-1);
38 }
39
40 /***************************************************************
41  End enumeration of the grppasswd list.
42 ****************************************************************/
43
44 static void endgrpunixpwent(void *vp)
45 {
46         endgrent();
47 }
48
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 getgrpunixpwpos(void *vp)
54 {
55         return (SMB_BIG_UINT)0;
56 }
57
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 setgrpunixpwpos(void *vp, SMB_BIG_UINT tok)
63 {
64         return False;
65 }
66
67 /*************************************************************************
68  Routine to return the next entry in the smbdomaingroup list.
69  *************************************************************************/
70 BOOL get_unixgroup_members(struct group *grp,
71                                 int *num_mem, DOMAIN_GRP_MEMBER **members)
72 {
73         int i;
74         char *unix_name;
75
76         if (num_mem == NULL || members == NULL)
77         {
78                 return False;
79         }
80
81         (*num_mem) = 0;
82         (*members) = NULL;
83
84         for (i = 0; (unix_name = grp->gr_mem[i]) != NULL; i++)
85         {
86                 DOM_NAME_MAP gmep;
87
88                 if (!lookupsmbgrpnam(unix_name, &gmep) &&
89                     !lookupsmbpwnam (unix_name, &gmep))
90                 {
91                         continue;
92                 }
93
94                 if (gmep.type != SID_NAME_DOM_GRP &&
95                     gmep.type != SID_NAME_USER &&
96                     gmep.type != SID_NAME_WKN_GRP)
97                 {
98                         DEBUG(0,("group database: name %s is not in a Domain Group\n",
99                                   unix_name));
100                         continue;
101                 }
102                         
103                 if (!sid_front_equal(&global_sam_sid, &gmep.sid))
104                 {
105                         DEBUG(0,("group database: could not resolve name %s (wrong Domain SID)\n",
106                                   unix_name));
107                         continue;
108                 }
109
110                 (*members) = Realloc((*members), ((*num_mem)+1) * sizeof(DOMAIN_GRP_MEMBER));
111                 if ((*members) == NULL)
112                 {
113                         return False;
114                 }
115
116                 fstrcpy((*members)[(*num_mem)].name, gmep.nt_name);
117                 (*members)[(*num_mem)].attr = 0x07;
118                 (*num_mem)++;
119         }
120         return True;
121 }
122
123 /*************************************************************************
124  Routine to return the next entry in the domain group list.
125
126  if we are not a PDC or BDC, then we do NOT support Domain groups, only
127  aliases.  try running MUSRMGR.EXE or running USRMGR.EXE selected on a
128  workstation, you will find that no Domain groups are listed: only aliases.
129
130  so, as a PDC or BDC, all unix groups not explicitly mapped using
131  map_group_gid() are treated as Domain groups.
132
133  *************************************************************************/
134 static DOMAIN_GRP *getgrpunixpwent(void *vp, DOMAIN_GRP_MEMBER **mem, int *num_mem)
135 {
136         /* Static buffers we will return. */
137         static DOMAIN_GRP gp_buf;
138         struct group *unix_grp;
139
140         if (lp_server_role() == ROLE_DOMAIN_NONE || 
141             lp_server_role() == ROLE_DOMAIN_MEMBER)
142         {
143                 /*
144                  * only PDC and BDC have domain groups in the SAM.
145                  * (however as member of domain you can have LOCAL groups,
146                  * but that's dealt with in the aliasdb...)
147                  */
148
149                 return NULL;
150         }
151
152         gpdb_init_grp(&gp_buf);
153
154         fstrcpy(gp_buf.comment, "");
155         gp_buf.attr    = 0x07;
156
157         /* cycle through unix groups */
158         while ((unix_grp = getgrent()) != NULL)
159         {
160                 DOM_NAME_MAP gmep;
161                 DEBUG(10,("getgrpunixpwent: enum unix group entry %s\n",
162                            unix_grp->gr_name));
163                         
164                 if (!lookupsmbgrpgid(unix_grp->gr_gid, &gmep))
165                 {
166                         continue;
167                 }
168
169                 if (gmep.type != SID_NAME_DOM_GRP &&
170                     gmep.type != SID_NAME_WKN_GRP)
171                 {
172                         continue;
173                 }
174
175                 sid_split_rid(&gmep.sid, &gp_buf.rid);
176                 if (!sid_equal(&gmep.sid, &global_sam_sid))
177                 {
178                         continue;
179                 }
180
181                 fstrcpy(gp_buf.name, gmep.nt_name);
182                 break;
183         }
184
185         if (unix_grp == NULL)
186         {
187                 return NULL;
188         }
189
190         /* get the user's domain groups.  there are a maximum of 32 */
191
192         if (mem != NULL && num_mem != NULL)
193         {
194                 (*mem) = NULL;
195                 (*num_mem) = 0;
196
197                 get_unixgroup_members(unix_grp, num_mem, mem);
198         }
199
200         {
201                 pstring linebuf;
202                 make_group_line(linebuf, sizeof(linebuf), &gp_buf, mem, num_mem);
203                 DEBUG(10,("line: '%s'\n", linebuf));
204         }
205
206         return &gp_buf;
207 }
208
209 /************************************************************************
210  Routine to add an entry to the grppasswd file.
211 *************************************************************************/
212
213 static BOOL add_grpunixgrp_entry(DOMAIN_GRP *newgrp)
214 {
215         DEBUG(0, ("add_grpunixgrp_entry: NOT IMPLEMENTED\n"));
216         return False;
217 }
218
219 /************************************************************************
220  Routine to search the grppasswd file for an entry matching the groupname.
221  and then modify its group entry. We can't use the startgrppwent()/
222  getgrppwent()/endgrppwent() interfaces here as we depend on looking
223  in the actual file to decide how much room we have to write data.
224  override = False, normal
225  override = True, override XXXXXXXX'd out group or NO PASS
226 ************************************************************************/
227
228 static BOOL mod_grpunixgrp_entry(DOMAIN_GRP* grp)
229 {
230         DEBUG(0, ("mod_grpunixgrp_entry: NOT IMPLEMENTED\n"));
231         return False;
232 }
233
234
235 static struct groupdb_ops unix_ops =
236 {
237         startgrpunixpwent,
238         endgrpunixpwent,
239         getgrpunixpwpos,
240         setgrpunixpwpos,
241
242         iterate_getgroupntnam,          /* In groupdb.c */
243         iterate_getgroupgid,          /* In groupdb.c */
244         iterate_getgrouprid,          /* In groupdb.c */
245         getgrpunixpwent,
246
247         add_grpunixgrp_entry,
248         mod_grpunixgrp_entry,
249
250         iterate_getusergroupsnam      /* in groupdb.c */
251 };
252
253 struct groupdb_ops *unix_initialise_group_db(void)
254 {    
255         return &unix_ops;
256 }
257
258 #else
259  /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
260  void unix_grppass_dummy_function(void) { } /* stop some compilers complaining */
261 #endif /* USE_SMBPASS_DB */