first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[kai/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_SMBPASS_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 static BOOL make_alias_line(char *p, int max_len,
69                                 LOCAL_GRP *als,
70                                 LOCAL_GRP_MEMBER **mem, int *num_mem)
71 {
72         int i;
73         int len;
74         len = slprintf(p, max_len-1, "%s:%s:%d:", als->name, als->comment, als->rid);
75
76         if (len == -1)
77         {
78                 DEBUG(0,("make_alias_line: cannot create entry\n"));
79                 return False;
80         }
81
82         p += len;
83         max_len -= len;
84
85         if (mem == NULL || num_mem == NULL)
86         {
87                 return True;
88         }
89
90         for (i = 0; i < (*num_mem); i++)
91         {
92                 len = strlen((*mem)[i].name);
93                 p = safe_strcpy(p, (*mem)[i].name, max_len); 
94
95                 if (p == NULL)
96                 {
97                         DEBUG(0, ("make_alias_line: out of space for aliases!\n"));
98                         return False;
99                 }
100
101                 max_len -= len;
102
103                 if (i != (*num_mem)-1)
104                 {
105                         *p = ',';
106                         p++;
107                         max_len--;
108                 }
109         }
110
111         return True;
112 }
113
114 /*************************************************************************
115  Routine to return the next entry in the smbdomainalias list.
116  *************************************************************************/
117 static char *get_alias_members(char *p, int *num_mem, LOCAL_GRP_MEMBER **members)
118 {
119         fstring name;
120
121         if (num_mem == NULL || members == NULL)
122         {
123                 return NULL;
124         }
125
126         (*num_mem) = 0;
127         (*members) = NULL;
128
129         while (next_token(&p, name, ",", sizeof(fstring)))
130         {
131                 DOM_SID sid;
132                 uint8 type;
133
134                 if (lookup_sid(name, &sid, &type))
135                 {
136                         (*members) = Realloc((*members), ((*num_mem)+1) * sizeof(LOCAL_GRP_MEMBER));
137                         (*num_mem)++;
138                 }
139                 else
140                 {
141                         DEBUG(0,("alias database: could not resolve alias named %s\n", name));
142                         continue;
143                 }
144                 if ((*members) == NULL)
145                 {
146                         return NULL;
147                 }
148                 fstrcpy((*members)[(*num_mem)-1].name, name);
149                 (*members)[(*num_mem)-1].sid_use = type;
150                 sid_copy(&(*members)[(*num_mem)-1].sid, &sid);
151         }
152         return p;
153 }
154
155 /*************************************************************************
156  Routine to return the next entry in the smbdomainalias list.
157  *************************************************************************/
158 static LOCAL_GRP *getalsfilepwent(void *vp, LOCAL_GRP_MEMBER **mem, int *num_mem)
159 {
160         /* Static buffers we will return. */
161         static LOCAL_GRP al_buf;
162
163         int gidval;
164
165         pstring linebuf;
166         char  *p;
167         size_t            linebuf_len;
168
169         aldb_init_als(&al_buf);
170
171         /*
172          * Scan the file, a line at a time and check if the name matches.
173          */
174         while ((linebuf_len = getfileline(vp, linebuf, sizeof(linebuf))) > 0)
175         {
176                 /* get alias name */
177
178                 p = strncpyn(al_buf.name, linebuf, sizeof(al_buf.name), ':');
179                 if (p == NULL)
180                 {
181                         DEBUG(0, ("getalsfilepwent: malformed alias entry (no :)\n"));
182                         continue;
183                 }
184
185                 /* Go past ':' */
186                 p++;
187
188                 /* get alias comment */
189
190                 p = strncpyn(al_buf.comment, p, sizeof(al_buf.comment), ':');
191                 if (p == NULL)
192                 {
193                         DEBUG(0, ("getalsfilepwent: malformed alias entry (no :)\n"));
194                         continue;
195                 }
196
197                 /* Go past ':' */
198                 p++;
199
200                 /* Get alias gid. */
201
202                 p = Atoic(p, &gidval, ":");
203
204                 if (p == NULL)
205                 {
206                         DEBUG(0, ("getalsfilepwent: malformed alias entry (no : after uid)\n"));
207                         continue;
208                 }
209
210                 /* Go past ':' */
211                 p++;
212
213                 /* now get the user's aliases.  there are a maximum of 32 */
214
215                 if (mem != NULL && num_mem != NULL)
216                 {
217                         (*mem) = NULL;
218                         (*num_mem) = 0;
219
220                         p = get_alias_members(p, num_mem, mem);
221                         if (p == NULL)
222                         {
223                                 DEBUG(0, ("getalsfilepwent: malformed alias entry (no : after members)\n"));
224                         }
225                 }
226
227                 /* ok, set up the static data structure and return it */
228
229                 al_buf.rid     = pwdb_gid_to_alias_rid((gid_t)gidval);
230
231                 make_alias_line(linebuf, sizeof(linebuf), &al_buf, mem, num_mem);
232                 DEBUG(10,("line: '%s'\n", linebuf));
233
234                 return &al_buf;
235         }
236
237         DEBUG(5,("getalsfilepwent: end of file reached.\n"));
238         return NULL;
239 }
240
241 /************************************************************************
242  Routine to add an entry to the aliasdb file.
243 *************************************************************************/
244
245 static BOOL add_alsfileals_entry(LOCAL_GRP *newals)
246 {
247         DEBUG(0, ("add_alsfileals_entry: NOT IMPLEMENTED\n"));
248         return False;
249 }
250
251 /************************************************************************
252  Routine to search the aliasdb file for an entry matching the aliasname.
253  and then modify its alias entry. We can't use the startalspwent()/
254  getalspwent()/endalspwent() interfaces here as we depend on looking
255  in the actual file to decide how much room we have to write data.
256  override = False, normal
257  override = True, override XXXXXXXX'd out alias or NO PASS
258 ************************************************************************/
259
260 static BOOL mod_alsfileals_entry(LOCAL_GRP* als)
261 {
262         DEBUG(0, ("mod_alsfileals_entry: NOT IMPLEMENTED\n"));
263         return False;
264 }
265
266
267 static struct aliasdb_ops file_ops =
268 {
269         startalsfilepwent,
270         endalsfilepwent,
271         getalsfilepwpos,
272         setalsfilepwpos,
273
274         iterate_getaliasnam,          /* In aliasdb.c */
275         iterate_getaliasgid,          /* In aliasdb.c */
276         iterate_getaliasrid,          /* In aliasdb.c */
277         getalsfilepwent,
278
279         add_alsfileals_entry,
280         mod_alsfileals_entry,
281
282         iterate_getuseraliasnam      /* in aliasdb.c */
283 };
284
285 struct aliasdb_ops *file_initialise_alias_db(void)
286 {    
287         return &file_ops;
288 }
289
290 #else
291  /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
292  void als_dummy_function(void) { } /* stop some compilers complaining */
293 #endif /* USE_SMBPASS_DB */