summaryrefslogtreecommitdiff
path: root/source3/sam/idmap.c
blob: aa5b923d3ddcb815b6f7dcb2b9a6fe7db521ff5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* 
   Unix SMB/CIFS implementation.
   ID Mapping
   Copyright (C) Tim Potter 2000
   Copyright (C) Jim McDonough <jmcd@us.ibm.com>	2003
   Copyright (C) Simo Sorce 2003
   Copyright (C) Jeremy Allison 2003.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/

#include "includes.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_IDMAP

static_decl_idmap;

struct idmap_function_entry {
	const char *name;
	struct idmap_methods *methods;
	struct idmap_function_entry *prev,*next;
};

static struct idmap_function_entry *backends = NULL;

static struct idmap_methods *cache_map;
static struct idmap_methods *remote_map;

static BOOL proxyonly = False;

/**********************************************************************
 Get idmap methods. Don't allow tdb to be a remote method.
**********************************************************************/

static struct idmap_methods *get_methods(const char *name, BOOL cache_method)
{
	struct idmap_function_entry *entry = backends;

	for(entry = backends; entry; entry = entry->next) {
		if (!cache_method && strequal(entry->name, "tdb"))
			continue; /* tdb is only cache method. */
		if (strequal(entry->name, name))
			return entry->methods;
	}

	return NULL;
}

/**********************************************************************
 Allow a module to register itself as a method.
**********************************************************************/

NTSTATUS smb_register_idmap(int version, const char *name, struct idmap_methods *methods)
{
	struct idmap_function_entry *entry;

 	if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
		DEBUG(0, ("smb_register_idmap: Failed to register idmap module.\n"
		          "The module was compiled against SMB_IDMAP_INTERFACE_VERSION %d,\n"
		          "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
		          "Please recompile against the current version of samba!\n",  
			  version, SMB_IDMAP_INTERFACE_VERSION));
		return NT_STATUS_OBJECT_TYPE_MISMATCH;
  	}

	if (!name || !name[0] || !methods) {
		DEBUG(0,("smb_register_idmap: called with NULL pointer or empty name!\n"));
		return NT_STATUS_INVALID_PARAMETER;
	}

	if (get_methods(name, False)) {
		DEBUG(0,("smb_register_idmap: idmap module %s already registered!\n", name));
		return NT_STATUS_OBJECT_NAME_COLLISION;
	}

	entry = SMB_XMALLOC_P(struct idmap_function_entry);
	entry->name = smb_xstrdup(name);
	entry->methods = methods;

	DLIST_ADD(backends, entry);
	DEBUG(5, ("smb_register_idmap: Successfully added idmap backend '%s'\n", name));
	return NT_STATUS_OK;
}

/**********************************************************************
 Initialise idmap cache and a remote backend (if configured).
**********************************************************************/

BOOL idmap_init(const char **remote_backend)
{
	if (!backends)
		static_init_idmap;

	if (!cache_map) {
		cache_map = get_methods("tdb", True);

		if (!cache_map) {
			DEBUG(0, ("idmap_init: could not find tdb cache backend!\n"));
			return False;
		}
		
		if (!NT_STATUS_IS_OK(cache_map->init( NULL ))) {
			DEBUG(0, ("idmap_init: could not initialise tdb cache backend!\n"));
			return False;
		}
	}
	
	if ((remote_map == NULL) && (remote_backend != NULL) &&
	    (*remote_backend != NULL) && (**remote_backend != '\0'))  {
		char *rem_backend = smb_xstrdup(*remote_backend);
		fstring params = "";
		char *pparams;
		BOOL idmap_prefix_workaround = False;
		
		/* get any mode parameters passed in */
		
		if ( (pparams = strchr( rem_backend, ':' )) != NULL ) {
			*pparams = '\0';
			pparams++;
			fstrcpy( params, pparams );
		}

		/* strip any leading idmap_ prefix of */
		if ( strncmp( rem_backend, "idmap_", 6) == 0 ) {
			rem_backend += 6;
			idmap_prefix_workaround = True;
			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));
		}
		
		DEBUG(3, ("idmap_init: using '%s' as remote backend\n", rem_backend));
		
		if((remote_map = get_methods(rem_backend, False)) ||
		    (NT_STATUS_IS_OK(smb_probe_module("idmap", rem_backend)) && 
		    (remote_map = get_methods(rem_backend, False)))) {
			if (!NT_STATUS_IS_OK(remote_map->init(params))) {
				DEBUG(0, ("idmap_init: failed to initialize remote backend!\n"));
				return False;
			}
		} else {
			DEBUG(0, ("idmap_init: could not load remote backend '%s'\n", rem_backend));
			if (idmap_prefix_workaround)
				rem_backend -= 6;
			SAFE_FREE(rem_backend);
			return False;
		}
		if (idmap_prefix_workaround)
			rem_backend -= 6;
		SAFE_FREE(rem_backend);
	}

	return True;
}

/**************************************************************************
 Don't do id mapping. This is used to make winbind a netlogon proxy only.
**************************************************************************/

void idmap_set_proxyonly(void)
{
	proxyonly = True;
}

BOOL idmap_proxyonly(void)
{
	return proxyonly;
}

/**************************************************************************
 This is a rare operation, designed to allow an explicit mapping to be
 set up for a sid to a POSIX id.
**************************************************************************/

NTSTATUS idmap_set_mapping(const DOM_SID *sid, unid_t id, int id_type)
{
	struct idmap_methods *map = remote_map;
	DOM_SID tmp_sid;

	if (proxyonly)
		return NT_STATUS_UNSUCCESSFUL;

	if (sid_check_is_in_our_domain(sid)) {
		DEBUG(3, ("Refusing to add SID %s to idmap, it's our own "
			  "domain\n", sid_string_static(sid)));
		return NT_STATUS_ACCESS_DENIED;
	}
		
	if (sid_check_is_in_builtin(sid)) {
		DEBUG(3, ("Refusing to add SID %s to idmap, it's our builtin "
			  "domain\n", sid_string_static(sid)));
		return NT_STATUS_ACCESS_DENIED;
	}

	DEBUG(10, ("idmap_set_mapping: Set %s to %s %lu\n",
		   sid_string_static(sid),
		   ((id_type & ID_TYPEMASK) == ID_USERID) ? "UID" : "GID",
		   ((id_type & ID_TYPEMASK) == ID_USERID) ? (unsigned long)id.uid : 
		   (unsigned long)id.gid));

	if ( (NT_STATUS_IS_OK(cache_map->
			      get_sid_from_id(&tmp_sid, id,
					      id_type | ID_QUERY_ONLY))) &&
	     sid_equal(sid, &tmp_sid) ) {
		/* Nothing to do, we already have that mapping */
		DEBUG(10, ("idmap_set_mapping: Mapping already there\n"));
		return NT_STATUS_OK;
	}

	if (map == NULL) {
		/* Ok, we don't have a authoritative remote
			mapping. So update our local cache only. */
		map = cache_map;
	}

	return map->set_mapping(sid, id, id_type);
}

/**************************************************************************
 Get ID from SID. This can create a mapping for a SID to a POSIX id.
**************************************************************************/

NTSTATUS idmap_get_id_from_sid(unid_t *id, int *id_type, const DOM_SID *sid)
{
	NTSTATUS ret;
	int loc_type;
	unid_t loc_id;

	if (proxyonly)
		return NT_STATUS_UNSUCCESSFUL;

	if (sid_check_is_in_our_domain(sid)) {
		DEBUG(9, ("sid %s is in our domain -- go look in passdb\n",
			  sid_string_static(sid)));
		return NT_STATUS_NONE_MAPPED;
	}

	if (sid_check_is_in_builtin(sid)) {
		DEBUG(9, ("sid %s is in builtin domain -- go look in passdb\n",
			  sid_string_static(sid)));
		return NT_STATUS_NONE_MAPPED;
	}

	loc_type = *id_type;

	if (remote_map) {
		/* We have a central remote idmap so only look in
                   cache, don't allocate */
		loc_type |= ID_QUERY_ONLY;
	}

	ret = cache_map->get_id_from_sid(id, &loc_type, sid);

	if (NT_STATUS_IS_OK(ret)) {
		*id_type = loc_type & ID_TYPEMASK;
		return NT_STATUS_OK;
	}

	if ((remote_map == NULL) || (loc_type & ID_CACHE_ONLY)) {
		return ret;
	}

	/* Before forking out to the possibly slow remote map, lets see if we
	 * already have the sid as uid when asking for a gid or vice versa. */

	loc_type = *id_type & ID_TYPEMASK;

	switch (loc_type) {
	case ID_USERID:
		loc_type = ID_GROUPID;
		break;
	case ID_GROUPID:
		loc_type = ID_USERID;
		break;
	default:
		loc_type = ID_EMPTY;
	}

	loc_type |= ID_QUERY_ONLY;

	ret = cache_map->get_id_from_sid(&loc_id, &loc_type, sid);

	if (NT_STATUS_IS_OK(ret)) {
		/* Ok, we have the uid as gid or vice versa. The remote map
		 * would not know anything different, so return here. */
		return NT_STATUS_UNSUCCESSFUL;
	}

	/* Ok, the mapping was not in the cache, give the remote map a
           second try. */

	ret = remote_map->get_id_from_sid(id, id_type, sid);
	
	if (NT_STATUS_IS_OK(ret)) {
		/* The remote backend gave us a valid mapping, cache it. */
		ret = cache_map->set_mapping(sid, *id, *id_type);
	}

	return ret;
}

/**************************************************************************
 Get SID from ID. This must have been created before.
**************************************************************************/

NTSTATUS idmap_get_sid_from_id(DOM_SID *sid, unid_t id, int id_type)
{
	NTSTATUS ret;
	int loc_type;

	if (proxyonly)
		return NT_STATUS_UNSUCCESSFUL;

	loc_type = id_type;
	if (remote_map) {
		loc_type = id_type | ID_QUERY_ONLY;
	}

	ret = cache_map->get_sid_from_id(sid, id, loc_type);

	if (NT_STATUS_IS_OK(ret))
		return ret;

	if ((remote_map == NULL) || (loc_type & ID_CACHE_ONLY))
		return ret;

	/* We have a second chance, ask our authoritative backend */

	ret = remote_map->get_sid_from_id(sid, id, id_type);

	if (NT_STATUS_IS_OK(ret)) {
		/* The remote backend gave us a valid mapping, cache it. */
		ret = cache_map->set_mapping(sid, id, id_type);
	}

	return ret;
}

/**************************************************************************
 Alloocate a new UNIX uid/gid
**************************************************************************/

NTSTATUS idmap_allocate_id(unid_t *id, int id_type)
{
	/* we have to allocate from the authoritative backend */
	
	if (proxyonly)
		return NT_STATUS_UNSUCCESSFUL;

	if ( remote_map )
		return remote_map->allocate_id( id, id_type );

	return cache_map->allocate_id( id, id_type );
}

/**************************************************************************
 Shutdown maps.
**************************************************************************/

NTSTATUS idmap_close(void)
{
	NTSTATUS ret;

	if (proxyonly)
		return NT_STATUS_OK;

	ret = cache_map->close_fn();
	if (!NT_STATUS_IS_OK(ret)) {
		DEBUG(3, ("idmap_close: failed to close local tdb cache!\n"));
	}
	cache_map = NULL;

	if (remote_map) {
		ret = remote_map->close_fn();
		if (!NT_STATUS_IS_OK(ret)) {
			DEBUG(3, ("idmap_close: failed to close remote idmap repository!\n"));
		}
		remote_map = NULL;
	}

	return ret;
}

/**************************************************************************
 Dump backend status.
**************************************************************************/

void idmap_status(void)
{
	cache_map->status();
	if (remote_map)
		remote_map->status();
}