added rid and sid_name_use to DOMAIN_GRP_MEMBER, for use in group member
[samba.git] / source3 / groupdb / aliasfile.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_SMBGROUP_DB
23
24 static int al_file_lock_depth = 0;
25 extern int DEBUGLEVEL;
26
27 static char s_readbuf[1024];
28
29 /***************************************************************
30  Start to enumerate the aliasdb list. Returns a void pointer
31  to ensure no modification outside this module.
32 ****************************************************************/
33
34 static void *startalsfilepwent(BOOL update)
35 {
36         return startfilepwent(lp_smb_alias_file(),
37                               s_readbuf, sizeof(s_readbuf),
38                               &al_file_lock_depth, update);
39 }
40
41 /***************************************************************
42  End enumeration of the aliasdb list.
43 ****************************************************************/
44
45 static void endalsfilepwent(void *vp)
46 {
47         endfilepwent(vp, &al_file_lock_depth);
48 }
49
50 /*************************************************************************
51  Return the current position in the aliasdb list as an SMB_BIG_UINT.
52  This must be treated as an opaque token.
53 *************************************************************************/
54 static SMB_BIG_UINT getalsfilepwpos(void *vp)
55 {
56         return getfilepwpos(vp);
57 }
58
59 /*************************************************************************
60  Set the current position in the aliasdb list from an SMB_BIG_UINT.
61  This must be treated as an opaque token.
62 *************************************************************************/
63 static BOOL setalsfilepwpos(void *vp, SMB_BIG_UINT tok)
64 {
65         return setfilepwpos(vp, tok);
66 }
67
68
69 /*************************************************************************
70  Routine to return the next entry in the smbdomainalias list.
71  *************************************************************************/
72 static char *get_alias_members(char *p, int *num_mem, LOCAL_GRP_MEMBER **members)
73 {
74         fstring name;
75
76         if (num_mem == NULL || members == NULL)
77         {
78                 return NULL;
79         }
80
81         (*num_mem) = 0;
82         (*members) = NULL;
83
84         while (next_token(&p, name, ",", sizeof(fstring)))
85         {
86                 DOM_SID sid;
87                 uint8 type;
88                 BOOL found = False;
89
90                 if (strnequal(name, "S-", 2))
91                 {
92                         /* sid entered directly */
93                         string_to_sid(&sid, name);
94                         found = lookup_sid(&sid, name, &type) == 0x0;
95                 }
96                 else
97                 {
98                         found = lookup_name(name, &sid, &type) == 0x0;
99                 }
100
101                 if (!found)
102                 {
103                         DEBUG(0,("alias database: could not resolve alias named %s\n", name));
104                         continue;
105                 }
106
107                 (*members) = Realloc((*members), ((*num_mem)+1) * sizeof(LOCAL_GRP_MEMBER));
108
109                 if ((*members) == NULL)
110                 {
111                         return NULL;
112                 }
113
114                 fstrcpy((*members)[*num_mem].name, name);
115                 (*members)[*num_mem].sid_use = type;
116                 sid_copy(&(*members)[*num_mem].sid, &sid);
117                 (*num_mem)++;
118         }
119         return p;
120 }
121
122 /*************************************************************************
123  Routine to return the next entry in the smbdomainalias list.
124  *************************************************************************/
125 static LOCAL_GRP *getalsfilepwent(void *vp, LOCAL_GRP_MEMBER **mem, int *num_mem)
126 {
127         /* Static buffers we will return. */
128         static LOCAL_GRP al_buf;
129
130         int gidval;
131
132         pstring linebuf;
133         char  *p;
134         uint8 type;
135
136         aldb_init_als(&al_buf);
137
138         /*
139          * Scan the file, a line at a time and check if the name matches.
140          */
141         while (getfileline(vp, linebuf, sizeof(linebuf)) > 0)
142         {
143                 DOM_NAME_MAP gmep;
144
145                 /* get alias name */
146
147                 p = strncpyn(al_buf.name, linebuf, sizeof(al_buf.name), ':');
148                 if (p == NULL)
149                 {
150                         DEBUG(0, ("getalsfilepwent: malformed alias entry (no :)\n"));
151                         continue;
152                 }
153
154                 /* Go past ':' */
155                 p++;
156
157                 /* get alias comment */
158
159                 p = strncpyn(al_buf.comment, p, sizeof(al_buf.comment), ':');
160                 if (p == NULL)
161                 {
162                         DEBUG(0, ("getalsfilepwent: malformed alias entry (no :)\n"));
163                         continue;
164                 }
165
166                 /* Go past ':' */
167                 p++;
168
169                 /* Get alias gid. */
170
171                 p = Atoic(p, &gidval, ":");
172
173                 if (p == NULL)
174                 {
175                         DEBUG(0, ("getalsfilepwent: malformed alias entry (no : after uid)\n"));
176                         continue;
177                 }
178
179                 /* Go past ':' */
180                 p++;
181
182                 /* now get the user's aliases.  there are a maximum of 32 */
183
184                 if (mem != NULL && num_mem != NULL)
185                 {
186                         (*mem) = NULL;
187                         (*num_mem) = 0;
188
189                         p = get_alias_members(p, num_mem, mem);
190                         if (p == NULL)
191                         {
192                                 DEBUG(0, ("getalsfilepwent: malformed alias entry (no : after members)\n"));
193                         }
194                 }
195
196                 /*
197                  * look up the gid, turn it into a rid.  the _correct_ type of rid */
198                  */
199
200                 if (!lookupsmbgrpgid((gid_t)gidval, &gmep))
201                 {
202                         continue;
203                 }
204                 if (gmep.type != SID_NAME_DOM_GRP &&
205                     gmep.type != SID_NAME_WKN_GRP))
206                 {
207                         continue;
208                 }
209
210                 sid_split_rid(&gmep.sid, &gp_buf.rid);
211                 if (!sid_equal(&gmep.sid, &global_sam_sid))
212                 {
213                         continue;
214                 }
215
216                 make_alias_line(linebuf, sizeof(linebuf), &al_buf, mem, num_mem);
217                 DEBUG(10,("line: '%s'\n", linebuf));
218
219                 return &al_buf;
220         }
221
222         DEBUG(5,("getalsfilepwent: end of file reached.\n"));
223         return NULL;
224 }
225
226 /************************************************************************
227  Routine to add an entry to the aliasdb file.
228 *************************************************************************/
229
230 static BOOL add_alsfileals_entry(LOCAL_GRP *newals)
231 {
232         DEBUG(0, ("add_alsfileals_entry: NOT IMPLEMENTED\n"));
233         return False;
234 }
235
236 /************************************************************************
237  Routine to search the aliasdb file for an entry matching the aliasname.
238  and then modify its alias entry. We can't use the startalspwent()/
239  getalspwent()/endalspwent() interfaces here as we depend on looking
240  in the actual file to decide how much room we have to write data.
241  override = False, normal
242  override = True, override XXXXXXXX'd out alias or NO PASS
243 ************************************************************************/
244
245 static BOOL mod_alsfileals_entry(LOCAL_GRP* als)
246 {
247         DEBUG(0, ("mod_alsfileals_entry: NOT IMPLEMENTED\n"));
248         return False;
249 }
250
251
252 static struct aliasdb_ops file_ops =
253 {
254         startalsfilepwent,
255         endalsfilepwent,
256         getalsfilepwpos,
257         setalsfilepwpos,
258
259         iterate_getaliasntnam,          /* In aliasdb.c */
260         iterate_getaliasgid,          /* In aliasdb.c */
261         iterate_getaliasrid,          /* In aliasdb.c */
262         getalsfilepwent,
263
264         add_alsfileals_entry,
265         mod_alsfileals_entry,
266
267         iterate_getuseraliasntnam      /* in aliasdb.c */
268 };
269
270 struct aliasdb_ops *file_initialise_alias_db(void)
271 {    
272         return &file_ops;
273 }
274
275 #else
276  /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
277  void als_dummy_function(void) { } /* stop some compilers complaining */
278 #endif /* USE_SMBPASS_DB */