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