Initial import
[samba] / source / sam / idmap.c
1 /* 
2    Unix SMB/CIFS implementation.
3    ID Mapping
4    Copyright (C) Tim Potter 2000
5    Copyright (C) Jim McDonough <jmcd@us.ibm.com>        2003
6    Copyright (C) Simo Sorce 2003
7    Copyright (C) Jeremy Allison 2003.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_IDMAP
27
28 struct idmap_function_entry {
29         const char *name;
30         struct idmap_methods *methods;
31         struct idmap_function_entry *prev,*next;
32 };
33
34 static struct idmap_function_entry *backends = NULL;
35
36 static struct idmap_methods *cache_map;
37 static struct idmap_methods *remote_map;
38
39 static BOOL proxyonly = False;
40
41 /**********************************************************************
42  Get idmap methods. Don't allow tdb to be a remote method.
43 **********************************************************************/
44
45 static struct idmap_methods *get_methods(const char *name, BOOL cache_method)
46 {
47         struct idmap_function_entry *entry = backends;
48
49         for(entry = backends; entry; entry = entry->next) {
50                 if (!cache_method && strequal(entry->name, "tdb"))
51                         continue; /* tdb is only cache method. */
52                 if (strequal(entry->name, name))
53                         return entry->methods;
54         }
55
56         return NULL;
57 }
58
59 /**********************************************************************
60  Allow a module to register itself as a method.
61 **********************************************************************/
62
63 NTSTATUS smb_register_idmap(int version, const char *name, struct idmap_methods *methods)
64 {
65         struct idmap_function_entry *entry;
66
67         if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
68                 DEBUG(0, ("smb_register_idmap: Failed to register idmap module.\n"
69                           "The module was compiled against SMB_IDMAP_INTERFACE_VERSION %d,\n"
70                           "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
71                           "Please recompile against the current version of samba!\n",  
72                           version, SMB_IDMAP_INTERFACE_VERSION));
73                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
74         }
75
76         if (!name || !name[0] || !methods) {
77                 DEBUG(0,("smb_register_idmap: called with NULL pointer or empty name!\n"));
78                 return NT_STATUS_INVALID_PARAMETER;
79         }
80
81         if (get_methods(name, False)) {
82                 DEBUG(0,("smb_register_idmap: idmap module %s already registered!\n", name));
83                 return NT_STATUS_OBJECT_NAME_COLLISION;
84         }
85
86         entry = SMB_XMALLOC_P(struct idmap_function_entry);
87         entry->name = smb_xstrdup(name);
88         entry->methods = methods;
89
90         DLIST_ADD(backends, entry);
91         DEBUG(5, ("smb_register_idmap: Successfully added idmap backend '%s'\n", name));
92         return NT_STATUS_OK;
93 }
94
95 /**********************************************************************
96  Initialise idmap cache and a remote backend (if configured).
97 **********************************************************************/
98
99 BOOL idmap_init(const char **remote_backend)
100 {
101         if (!backends)
102                 static_init_idmap;
103
104         if (!cache_map) {
105                 cache_map = get_methods("tdb", True);
106
107                 if (!cache_map) {
108                         DEBUG(0, ("idmap_init: could not find tdb cache backend!\n"));
109                         return False;
110                 }
111                 
112                 if (!NT_STATUS_IS_OK(cache_map->init( NULL ))) {
113                         DEBUG(0, ("idmap_init: could not initialise tdb cache backend!\n"));
114                         return False;
115                 }
116         }
117         
118         if ((remote_map == NULL) && (remote_backend != NULL) &&
119             (*remote_backend != NULL) && (**remote_backend != '\0'))  {
120                 char *rem_backend = smb_xstrdup(*remote_backend);
121                 fstring params = "";
122                 char *pparams;
123                 BOOL idmap_prefix_workaround = False;
124                 
125                 /* get any mode parameters passed in */
126                 
127                 if ( (pparams = strchr( rem_backend, ':' )) != NULL ) {
128                         *pparams = '\0';
129                         pparams++;
130                         fstrcpy( params, pparams );
131                 }
132
133                 /* strip any leading idmap_ prefix of */
134                 if ( strncmp( rem_backend, "idmap_", 6) == 0 ) {
135                         rem_backend += 6;
136                         idmap_prefix_workaround = True;
137                         DEBUG(0, ("idmap_init: idmap backend uses deprecated 'idmap_' prefix.  Please replace 'idmap_%s' by '%s' in %s\n", rem_backend, rem_backend, dyn_CONFIGFILE));
138                 }
139                 
140                 DEBUG(3, ("idmap_init: using '%s' as remote backend\n", rem_backend));
141                 
142                 if((remote_map = get_methods(rem_backend, False)) ||
143                     (NT_STATUS_IS_OK(smb_probe_module("idmap", rem_backend)) && 
144                     (remote_map = get_methods(rem_backend, False)))) {
145                         if (!NT_STATUS_IS_OK(remote_map->init(params))) {
146                                 DEBUG(0, ("idmap_init: failed to initialize remote backend!\n"));
147                                 return False;
148                         }
149                 } else {
150                         DEBUG(0, ("idmap_init: could not load remote backend '%s'\n", rem_backend));
151                         if (idmap_prefix_workaround)
152                                 rem_backend -= 6;
153                         SAFE_FREE(rem_backend);
154                         return False;
155                 }
156                 if (idmap_prefix_workaround)
157                         rem_backend -= 6;
158                 SAFE_FREE(rem_backend);
159         }
160
161         return True;
162 }
163
164 /**************************************************************************
165  Don't do id mapping. This is used to make winbind a netlogon proxy only.
166 **************************************************************************/
167
168 void idmap_set_proxyonly(void)
169 {
170         proxyonly = True;
171 }
172
173 BOOL idmap_proxyonly(void)
174 {
175         return proxyonly;
176 }
177
178 /**************************************************************************
179  This is a rare operation, designed to allow an explicit mapping to be
180  set up for a sid to a POSIX id.
181 **************************************************************************/
182
183 NTSTATUS idmap_set_mapping(const DOM_SID *sid, unid_t id, int id_type)
184 {
185         struct idmap_methods *map = remote_map;
186         DOM_SID tmp_sid;
187
188         if (proxyonly)
189                 return NT_STATUS_UNSUCCESSFUL;
190
191         DEBUG(10, ("idmap_set_mapping: Set %s to %s %lu\n",
192                    sid_string_static(sid),
193                    ((id_type & ID_TYPEMASK) == ID_USERID) ? "UID" : "GID",
194                    ((id_type & ID_TYPEMASK) == ID_USERID) ? (unsigned long)id.uid : 
195                    (unsigned long)id.gid));
196
197         if ( (NT_STATUS_IS_OK(cache_map->
198                               get_sid_from_id(&tmp_sid, id,
199                                               id_type | ID_QUERY_ONLY))) &&
200              sid_equal(sid, &tmp_sid) ) {
201                 /* Nothing to do, we already have that mapping */
202                 DEBUG(10, ("idmap_set_mapping: Mapping already there\n"));
203                 return NT_STATUS_OK;
204         }
205
206         if (map == NULL) {
207                 /* Ok, we don't have a authoritative remote
208                         mapping. So update our local cache only. */
209                 map = cache_map;
210         }
211
212         return map->set_mapping(sid, id, id_type);
213 }
214
215 /**************************************************************************
216  Get ID from SID. This can create a mapping for a SID to a POSIX id.
217 **************************************************************************/
218
219 NTSTATUS idmap_get_id_from_sid(unid_t *id, int *id_type, const DOM_SID *sid)
220 {
221         NTSTATUS ret;
222         int loc_type;
223         unid_t loc_id;
224
225         if (proxyonly)
226                 return NT_STATUS_UNSUCCESSFUL;
227
228         loc_type = *id_type;
229
230         if (remote_map) {
231                 /* We have a central remote idmap so only look in
232                    cache, don't allocate */
233                 loc_type |= ID_QUERY_ONLY;
234         }
235
236         ret = cache_map->get_id_from_sid(id, &loc_type, sid);
237
238         if (NT_STATUS_IS_OK(ret)) {
239                 *id_type = loc_type & ID_TYPEMASK;
240                 return NT_STATUS_OK;
241         }
242
243         if ((remote_map == NULL) || (loc_type & ID_CACHE_ONLY)) {
244                 return ret;
245         }
246
247         /* Before forking out to the possibly slow remote map, lets see if we
248          * already have the sid as uid when asking for a gid or vice versa. */
249
250         loc_type = *id_type & ID_TYPEMASK;
251
252         switch (loc_type) {
253         case ID_USERID:
254                 loc_type = ID_GROUPID;
255                 break;
256         case ID_GROUPID:
257                 loc_type = ID_USERID;
258                 break;
259         default:
260                 loc_type = ID_EMPTY;
261         }
262
263         loc_type |= ID_QUERY_ONLY;
264
265         ret = cache_map->get_id_from_sid(&loc_id, &loc_type, sid);
266
267         if (NT_STATUS_IS_OK(ret)) {
268                 /* Ok, we have the uid as gid or vice versa. The remote map
269                  * would not know anything different, so return here. */
270                 return NT_STATUS_UNSUCCESSFUL;
271         }
272
273         /* Ok, the mapping was not in the cache, give the remote map a
274            second try. */
275
276         ret = remote_map->get_id_from_sid(id, id_type, sid);
277         
278         if (NT_STATUS_IS_OK(ret)) {
279                 /* The remote backend gave us a valid mapping, cache it. */
280                 ret = cache_map->set_mapping(sid, *id, *id_type);
281         }
282
283         return ret;
284 }
285
286 /**************************************************************************
287  Get SID from ID. This must have been created before.
288 **************************************************************************/
289
290 NTSTATUS idmap_get_sid_from_id(DOM_SID *sid, unid_t id, int id_type)
291 {
292         NTSTATUS ret;
293         int loc_type;
294
295         if (proxyonly)
296                 return NT_STATUS_UNSUCCESSFUL;
297
298         loc_type = id_type;
299         if (remote_map) {
300                 loc_type = id_type | ID_QUERY_ONLY;
301         }
302
303         ret = cache_map->get_sid_from_id(sid, id, loc_type);
304
305         if (NT_STATUS_IS_OK(ret))
306                 return ret;
307
308         if ((remote_map == NULL) || (loc_type & ID_CACHE_ONLY))
309                 return ret;
310
311         /* We have a second chance, ask our authoritative backend */
312
313         ret = remote_map->get_sid_from_id(sid, id, id_type);
314
315         if (NT_STATUS_IS_OK(ret)) {
316                 /* The remote backend gave us a valid mapping, cache it. */
317                 ret = cache_map->set_mapping(sid, id, id_type);
318         }
319
320         return ret;
321 }
322
323 /**************************************************************************
324  Alloocate a new UNIX uid/gid
325 **************************************************************************/
326
327 NTSTATUS idmap_allocate_id(unid_t *id, int id_type)
328 {
329         /* we have to allocate from the authoritative backend */
330         
331         if (proxyonly)
332                 return NT_STATUS_UNSUCCESSFUL;
333
334         if ( remote_map )
335                 return remote_map->allocate_id( id, id_type );
336
337         return cache_map->allocate_id( id, id_type );
338 }
339
340 /**************************************************************************
341  Alloocate a new RID
342 **************************************************************************/
343
344 NTSTATUS idmap_allocate_rid(uint32 *rid, int type)
345 {
346         /* we have to allocate from the authoritative backend */
347         
348         if (proxyonly)
349                 return NT_STATUS_UNSUCCESSFUL;
350
351         if ( remote_map )
352                 return remote_map->allocate_rid( rid, type );
353
354         return cache_map->allocate_rid( rid, type );
355 }
356
357 /**************************************************************************
358  Shutdown maps.
359 **************************************************************************/
360
361 NTSTATUS idmap_close(void)
362 {
363         NTSTATUS ret;
364
365         if (proxyonly)
366                 return NT_STATUS_OK;
367
368         ret = cache_map->close_fn();
369         if (!NT_STATUS_IS_OK(ret)) {
370                 DEBUG(3, ("idmap_close: failed to close local tdb cache!\n"));
371         }
372         cache_map = NULL;
373
374         if (remote_map) {
375                 ret = remote_map->close_fn();
376                 if (!NT_STATUS_IS_OK(ret)) {
377                         DEBUG(3, ("idmap_close: failed to close remote idmap repository!\n"));
378                 }
379                 remote_map = NULL;
380         }
381
382         return ret;
383 }
384
385 /**************************************************************************
386  Dump backend status.
387 **************************************************************************/
388
389 void idmap_status(void)
390 {
391         cache_map->status();
392         if (remote_map)
393                 remote_map->status();
394 }