b9d9dc6856b1bbe6af1df636936f03995656b64d
[samba.git] / source / 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  maps a unix group to a domain sid and an nt alias name.  
70 *************************************************************************/
71 static void map_unix_grp_to_nt_als(char *unix_name,
72         struct group *unix_grp, char *nt_name, DOM_SID *sid)
73 {
74         BOOL found = False;
75         uint32 rid;
76         fstring ntname;
77         fstring ntdomain;
78
79         if (isdigit(unix_name[0]))
80         {
81                 unix_grp->gr_gid = get_number(unix_name);
82                 unix_grp->gr_name = unix_name;
83                 found = map_alias_gid(unix_grp->gr_gid, sid, ntname, ntdomain);
84         }
85         else
86         {
87                 unix_grp->gr_name = unix_name;
88                 found = map_unix_alias_name(unix_grp->gr_name, sid, ntname, ntdomain);
89         }
90
91         if (found)
92         {
93                 /*
94                  * find the NT name represented by this UNIX gid.
95                  * then, only accept NT aliass that are in our domain
96                  */
97
98                 sid_split_rid(sid, &rid);
99         }
100         else
101         {
102                 /*
103                  * assume that the UNIX group is an NT alias with
104                  * the same name.  convert gid to a alias rid.
105                  */
106                 
107                 fstrcpy(ntdomain, global_sam_name);
108                 fstrcpy(ntname, unix_grp->gr_name);
109                 sid_copy(sid, &global_sam_sid);
110         }
111
112         slprintf(nt_name, sizeof(fstring)-1, "\\%s\\%s",
113                  ntdomain, ntname);
114 }
115
116 /*************************************************************************
117  Routine to return the next entry in the smbdomainalias list.
118  *************************************************************************/
119 BOOL get_unixalias_members(struct group *als,
120                                 int *num_mem, LOCAL_GRP_MEMBER **members)
121 {
122         int i;
123         char *unix_name;
124         fstring nt_name;
125
126         if (num_mem == NULL || members == NULL)
127         {
128                 return False;
129         }
130
131         (*num_mem) = 0;
132         (*members) = NULL;
133
134         for (i = 0; (unix_name = als->gr_mem[i]) != NULL; i++)
135         {
136                 DOM_SID sid;
137                 struct group unix_grp;
138
139                 map_unix_grp_to_nt_als(unix_name, &unix_grp, nt_name, &sid);
140
141                 if (!sid_equal(&sid, &global_sam_sid))
142                 {
143                         DEBUG(0,("alias database: could not resolve name %s in domain %s\n",
144                                   unix_name, global_sam_name));
145                         continue;
146                 }
147
148                 (*members) = Realloc((*members), ((*num_mem)+1) * sizeof(LOCAL_GRP_MEMBER));
149                 if ((*members) == NULL)
150                 {
151                         return False;
152                 }
153
154                 fstrcpy((*members)[(*num_mem)].name, nt_name);
155                 (*num_mem)++;
156         }
157         return True;
158 }
159
160 /*************************************************************************
161  Routine to return the next entry in the domain alias list.
162
163  when we are a PDC or BDC, then unix groups that are explicitly NOT mapped
164  to aliases (map_alias_gid) are treated as DOMAIN groups (see groupunix.c).
165
166  when we are a member of a domain (not a PDC or BDC) then unix groups
167  that are explicitly NOT mapped to aliases (map_alias_gid) are treated
168  as LOCAL groups.
169
170  the reasoning behind this is to make it as simple as possible (not an easy
171  task) for people to set up a domain-aware samba server, in each role that
172  the server can take.
173
174  *************************************************************************/
175 static LOCAL_GRP *getalsunixpwent(void *vp, LOCAL_GRP_MEMBER **mem, int *num_mem)
176 {
177         /* Static buffers we will return. */
178         static LOCAL_GRP gp_buf;
179         struct group *unix_grp;
180
181         if (lp_server_role() == ROLE_DOMAIN_NONE)
182         {
183                 /*
184                  * no domain role, no domain aliases (or domain groups,
185                  * but that's dealt with by groupdb...).
186                  */
187
188                 return NULL;
189         }
190
191         aldb_init_als(&gp_buf);
192
193         fstrcpy(gp_buf.comment, "");
194
195         /* cycle through unix groups */
196         while ((unix_grp = getgrent()) != NULL)
197         {
198                 DOM_SID sid;
199                 if (map_alias_gid(unix_grp->gr_gid, &sid, gp_buf.name, NULL))
200                 {
201                         /*
202                          * find the NT name represented by this UNIX gid.
203                          * then, only accept NT aliases that are in our domain
204                          */
205
206                         sid_split_rid(&sid, &gp_buf.rid);
207                         if (sid_equal(&sid, &global_sam_sid))
208                         {
209                                 break; /* hooray. */
210                         }
211                 }
212                 else if (lp_server_role() == ROLE_DOMAIN_MEMBER)
213                 {
214                         /*
215                          * if we are a member of a domain,
216                          * assume that the UNIX alias is an NT alias with
217                          * the same name.  convert gid to a alias rid.
218                          */
219                         
220                         fstrcpy(gp_buf.name, unix_grp->gr_name);
221                         gp_buf.rid = pwdb_gid_to_alias_rid(unix_grp->gr_gid);
222                 }
223         }
224
225         if (unix_grp == NULL)
226         {
227                 return NULL;
228         }
229
230         /* get the user's domain aliases.  there are a maximum of 32 */
231
232         if (mem != NULL && num_mem != NULL)
233         {
234                 (*mem) = NULL;
235                 (*num_mem) = 0;
236
237                 get_unixalias_members(unix_grp, num_mem, mem);
238         }
239
240         {
241                 pstring linebuf;
242                 make_alias_line(linebuf, sizeof(linebuf), &gp_buf, mem, num_mem);
243                 DEBUG(10,("line: '%s'\n", linebuf));
244         }
245
246         return &gp_buf;
247 }
248
249 /************************************************************************
250  Routine to add an entry to the alspasswd file.
251 *************************************************************************/
252
253 static BOOL add_alsunixgrp_entry(LOCAL_GRP *newals)
254 {
255         DEBUG(0, ("add_alsunixgrp_entry: NOT IMPLEMENTED\n"));
256         return False;
257 }
258
259 /************************************************************************
260  Routine to search the alspasswd file for an entry matching the aliasname.
261  and then modify its alias entry. We can't use the startalspwent()/
262  getalspwent()/endalspwent() interfaces here as we depend on looking
263  in the actual file to decide how much room we have to write data.
264  override = False, normal
265  override = True, override XXXXXXXX'd out alias or NO PASS
266 ************************************************************************/
267
268 static BOOL mod_alsunixgrp_entry(LOCAL_GRP* als)
269 {
270         DEBUG(0, ("mod_alsunixgrp_entry: NOT IMPLEMENTED\n"));
271         return False;
272 }
273
274
275 static struct aliasdb_ops unix_ops =
276 {
277         startalsunixpwent,
278         endalsunixpwent,
279         getalsunixpwpos,
280         setalsunixpwpos,
281
282         iterate_getaliasnam,          /* In aliasdb.c */
283         iterate_getaliasgid,          /* In aliasdb.c */
284         iterate_getaliasrid,          /* In aliasdb.c */
285         getalsunixpwent,
286
287         add_alsunixgrp_entry,
288         mod_alsunixgrp_entry,
289
290         iterate_getuseraliasnam      /* in aliasdb.c */
291 };
292
293 struct aliasdb_ops *unix_initialise_alias_db(void)
294 {    
295         return &unix_ops;
296 }
297
298 #else
299  /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
300  void unix_alspass_dummy_function(void) { } /* stop some compilers complaining */
301 #endif /* USE_SMBPASS_DB */