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