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