- lib/unix_sec_ctxt.c
[samba.git] / source3 / groupdb / builtinunix.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_sid_S_1_5_20;
28 extern DOM_SID global_sam_sid;
29 extern fstring global_sam_name;
30
31 /***************************************************************
32  Start to enumerate the bltpasswd list. Returns a void pointer
33  to ensure no modification outside this module.
34 ****************************************************************/
35
36 static void *startbltunixpwent(BOOL update)
37 {
38         setgrent();
39         return (void*)(-1);
40 }
41
42 /***************************************************************
43  End enumeration of the bltpasswd list.
44 ****************************************************************/
45
46 static void endbltunixpwent(void *vp)
47 {
48         endgrent();
49 }
50
51 /*************************************************************************
52  Return the current position in the bltpasswd list as an SMB_BIG_UINT.
53  This must be treated as an opaque token.
54 *************************************************************************/
55 static SMB_BIG_UINT getbltunixpwpos(void *vp)
56 {
57         return (SMB_BIG_UINT)0;
58 }
59
60 /*************************************************************************
61  Set the current position in the bltpasswd list from an SMB_BIG_UINT.
62  This must be treated as an opaque token.
63 *************************************************************************/
64 static BOOL setbltunixpwpos(void *vp, SMB_BIG_UINT tok)
65 {
66         return False;
67 }
68
69 /*************************************************************************
70  Routine to return the next entry in the smbdomainbuiltin list.
71  *************************************************************************/
72 BOOL get_unixbuiltin_members(struct group *grp,
73                                 int *num_mem, LOCAL_GRP_MEMBER **members)
74 {
75         int i;
76         char *unix_name;
77
78         if (num_mem == NULL || members == NULL)
79         {
80                 return False;
81         }
82
83         (*num_mem) = 0;
84         (*members) = NULL;
85
86         for (i = 0; (unix_name = grp->gr_mem[i]) != NULL; i++)
87         {
88                 fstring name;
89                 DOM_NAME_MAP gmep;
90                 LOCAL_GRP_MEMBER *mem;
91
92                 fstrcpy(name, unix_name);
93
94                 if (!lookupsmbpwnam (name, &gmep) &&
95                     !lookupsmbgrpnam(name, &gmep))
96                 {
97                         continue;
98                 }
99
100                 if (!sid_front_equal(&global_sam_sid, &gmep.sid))
101                 {
102                         DEBUG(0,("builtin database: could not resolve name %s (wrong Domain SID)\n",
103                                   name));
104                         continue;
105                 }
106
107                 (*num_mem)++;
108                 (*members) = Realloc((*members), (*num_mem) * sizeof(LOCAL_GRP_MEMBER));
109                 if ((*members) == NULL)
110                 {
111                         DEBUG(0,("get_unixbuiltin_members: could not realloc LOCAL_GRP_MEMBERs\n"));
112                         return False;
113                 }
114
115                 mem = &(*members)[(*num_mem)-1];
116                 slprintf(mem->name, sizeof(mem->name)-1, "%s\\%s",
117                          gmep.nt_domain, gmep.nt_name);
118                 sid_copy(&mem->sid, &gmep.sid);
119                 mem->sid_use = gmep.type;
120
121                 DEBUG(10,("get_unixbuiltin_members: adding to builtin alias %s\n",
122                            mem->name));
123         }
124         return True;
125 }
126
127 /*************************************************************************
128  Routine to return the next entry in the domain builtin list.
129
130  when we are a PDC or BDC, then unix groups that are explicitly NOT mapped
131  to builtin aliases are treated as DOMAIN groups (see groupunix.c).
132
133  when we are a member of a domain (not a PDC or BDC) then unix groups
134  that are explicitly NOT mapped to builtin aliases are treated
135  as LOCAL groups.
136
137  the reasoning behind this is to make it as simple as possible (not an easy
138  task) for people to set up a domain-aware samba server, in each role that
139  the server can take.
140
141  *************************************************************************/
142 static LOCAL_GRP *getbltunixpwent(void *vp, LOCAL_GRP_MEMBER **mem, int *num_mem)
143 {
144         /* Static buffers we will return. */
145         static LOCAL_GRP gp_buf;
146         struct group *unix_grp;
147
148         if (lp_server_role() == ROLE_DOMAIN_NONE)
149         {
150                 /*
151                  * no domain role, no domain builtin aliases (or domain groups,
152                  * but that's dealt with by groupdb...).
153                  */
154
155                 return NULL;
156         }
157
158         bidb_init_blt(&gp_buf);
159
160         /* cycle through unix groups */
161         while ((unix_grp = getgrent()) != NULL)
162         {
163                 DOM_NAME_MAP gmep;
164                 fstring sid_str;
165                 DEBUG(10,("getbltunixpwent: enum unix group entry %s\n",
166                            unix_grp->gr_name));
167                         
168                 if (!lookupsmbgrpgid(unix_grp->gr_gid, &gmep))
169                 {
170                         continue;
171                 }
172
173                 sid_to_string(sid_str, &gmep.sid);
174                 DEBUG(10,("group %s found, sid %s type %d\n",
175                         gmep.nt_name, sid_str, gmep.type));
176
177                 if (gmep.type != SID_NAME_ALIAS)
178                 {
179                         continue;
180                 }
181
182                 sid_split_rid(&gmep.sid, &gp_buf.rid);
183                 if (!sid_equal(&global_sid_S_1_5_20, &gmep.sid))
184                 {
185                         continue;
186                 }
187
188                 fstrcpy(gp_buf.name, gmep.nt_name);
189                 break;
190         }
191
192         if (unix_grp == NULL)
193         {
194                 return NULL;
195         }
196
197         /* get the user's domain builtin aliases.  there are a maximum of 32 */
198
199         if (mem != NULL && num_mem != NULL)
200         {
201                 (*mem) = NULL;
202                 (*num_mem) = 0;
203
204                 get_unixbuiltin_members(unix_grp, num_mem, mem);
205         }
206
207         {
208                 pstring linebuf;
209                 make_builtin_line(linebuf, sizeof(linebuf), &gp_buf, mem, num_mem);
210                 DEBUG(10,("line: '%s'\n", linebuf));
211         }
212
213         return &gp_buf;
214 }
215
216 /************************************************************************
217  Routine to add an entry to the bltpasswd file.
218 *************************************************************************/
219
220 static BOOL add_bltunixgrp_entry(LOCAL_GRP *newblt)
221 {
222         DEBUG(0, ("add_bltunixgrp_entry: NOT IMPLEMENTED\n"));
223         return False;
224 }
225
226 /************************************************************************
227  Routine to search the bltpasswd file for an entry matching the builtinname.
228  and then modify its builtin entry. 
229 ************************************************************************/
230
231 static BOOL mod_bltunixgrp_entry(LOCAL_GRP* blt)
232 {
233         DEBUG(0, ("mod_bltunixgrp_entry: NOT IMPLEMENTED\n"));
234         return False;
235 }
236
237 /************************************************************************
238  Routine to add a member to an entry to the bltpasswd file.
239 *************************************************************************/
240 static BOOL add_bltunixgrp_member(uint32 rid, DOM_SID *member_sid)
241 {
242         DEBUG(0, ("add_bltunixgrp_member: NOT IMPLEMENTED\n"));
243         return False;
244 }
245
246 /************************************************************************
247  Routine to delete a member from an entry to the bltpasswd file.
248 *************************************************************************/
249 static BOOL del_bltunixgrp_member(uint32 rid, DOM_SID *member_sid)
250 {
251         DEBUG(0, ("del_bltunixgrp_member: NOT IMPLEMENTED\n"));
252         return False;
253 }
254
255 static struct aliasdb_ops unix_ops =
256 {
257         startbltunixpwent,
258         endbltunixpwent,
259         getbltunixpwpos,
260         setbltunixpwpos,
261
262         iterate_getbuiltinntnam,          /* In builtindb.c */
263         iterate_getbuiltingid,          /* In builtindb.c */
264         iterate_getbuiltinrid,          /* In builtindb.c */
265         getbltunixpwent,
266
267         add_bltunixgrp_entry,
268         mod_bltunixgrp_entry,
269         NULL, /* deliberately NULL: you can't delete builtin aliases */
270
271         add_bltunixgrp_member,
272         del_bltunixgrp_member,
273
274         iterate_getuserbuiltinntnam      /* in builtindb.c */
275 };
276
277 struct aliasdb_ops *unix_initialise_builtin_db(void)
278 {    
279         return &unix_ops;
280 }
281
282 #else
283  /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
284  void unix_bltpass_dummy_function(void) { } /* stop some compilers complaining */
285 #endif /* USE_SMBPASS_DB */