summaryrefslogtreecommitdiff
path: root/source3/lib/idmap_cache.c
blob: 011a017c483959addd2688f2da6064174ec42c81 (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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
   Unix SMB/CIFS implementation.
   ID Mapping Cache

   Copyright (C) Volker Lendecke	2008

   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 3 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, see <http://www.gnu.org/licenses/>.*/

#include "includes.h"
#include "idmap_cache.h"
#include "../libcli/security/security.h"
#include "../librpc/gen_ndr/idmap.h"

/**
 * Find a sid2xid mapping
 * @param[in] sid		the sid to map
 * @param[out] id		where to put the result
 * @param[out] expired		is the cache entry expired?
 * @retval Was anything in the cache at all?
 *
 * If id->id == -1 this was a negative mapping.
 */

bool idmap_cache_find_sid2unixid(const struct dom_sid *sid, struct unixid *id,
				 bool *expired)
{
	fstring sidstr;
	char *key;
	char *value = NULL;
	char *endptr;
	time_t timeout;
	bool ret;
	struct unixid tmp_id;

	key = talloc_asprintf(talloc_tos(), "IDMAP/SID2XID/%s",
			      sid_to_fstring(sidstr, sid));
	if (key == NULL) {
		return false;
	}
	ret = gencache_get(key, &value, &timeout);
	if (!ret) {
		goto done;
	}

	DEBUG(10, ("Parsing value for key [%s]: value=[%s]\n", key, value));

	if (value[0] == '\0') {
		DEBUG(0, ("Failed to parse value for key [%s]: "
			  "value is empty\n", key));
		ret = false;
		goto done;
	}

	tmp_id.id = strtol(value, &endptr, 10);

	if ((value == endptr) && (tmp_id.id == 0)) {
		DEBUG(0, ("Failed to parse value for key [%s]: value[%s] does "
			  "not start with a number\n", key, value));
		ret = false;
		goto done;
	}

	DEBUG(10, ("Parsing value for key [%s]: id=[%llu], endptr=[%s]\n",
		   key, (unsigned long long)tmp_id.id, endptr));

	ret = (*endptr == ':');
	if (ret) {
		switch (endptr[1]) {
		case 'U':
			tmp_id.type = ID_TYPE_UID;
			break;

		case 'G':
			tmp_id.type = ID_TYPE_GID;
			break;

		case 'B':
			tmp_id.type = ID_TYPE_BOTH;
			break;

		case '\0':
			DEBUG(0, ("FAILED to parse value for key [%s] "
				  "(id=[%llu], endptr=[%s]): "
				  "no type character after colon\n",
				  key, (unsigned long long)tmp_id.id, endptr));
			ret = false;
			goto done;
		default:
			DEBUG(0, ("FAILED to parse value for key [%s] "
				  "(id=[%llu], endptr=[%s]): "
				  "illegal type character '%c'\n",
				  key, (unsigned long long)tmp_id.id, endptr,
				  endptr[1]));
			ret = false;
			goto done;
		}
		if (endptr[2] != '\0') {
			DEBUG(0, ("FAILED to parse value for key [%s] "
				  "(id=[%llu], endptr=[%s]): "
				  "more than 1 type character after colon\n",
				  key, (unsigned long long)tmp_id.id, endptr));
			ret = false;
			goto done;
		}

		*id = tmp_id;
		*expired = (timeout <= time(NULL));
	} else {
		DEBUG(0, ("FAILED to parse value for key [%s] (value=[%s]): "
			  "colon missing after id=[%llu]\n",
			  key, value, (unsigned long long)tmp_id.id));
	}

done:
	TALLOC_FREE(key);
	SAFE_FREE(value);
	return ret;
}

/**
 * Find a sid2uid mapping
 * @param[in] sid		the sid to map
 * @param[out] puid		where to put the result
 * @param[out] expired		is the cache entry expired?
 * @retval Was anything in the cache at all?
 *
 * If *puid == -1 this was a negative mapping.
 */

bool idmap_cache_find_sid2uid(const struct dom_sid *sid, uid_t *puid,
			      bool *expired)
{
	bool ret;
	struct unixid id;
	ret = idmap_cache_find_sid2unixid(sid, &id, expired);
	if (!ret) {
		return false;
	}

	if (id.type == ID_TYPE_BOTH || id.type == ID_TYPE_UID) {
		*puid = id.id;
	} else {
		*puid = -1;
	}
	return true;
}

/**
 * Find a sid2gid mapping
 * @param[in] sid		the sid to map
 * @param[out] pgid		where to put the result
 * @param[out] expired		is the cache entry expired?
 * @retval Was anything in the cache at all?
 *
 * If *pgid == -1 this was a negative mapping.
 */

bool idmap_cache_find_sid2gid(const struct dom_sid *sid, gid_t *pgid,
			      bool *expired)
{
	bool ret;
	struct unixid id;
	ret = idmap_cache_find_sid2unixid(sid, &id, expired);
	if (!ret) {
		return false;
	}

	if (id.type == ID_TYPE_BOTH || id.type == ID_TYPE_GID) {
		*pgid = id.id;
	} else {
		*pgid = -1;
	}
	return true;
}

/**
 * Find a uid2sid mapping
 * @param[in] uid		the uid to map
 * @param[out] sid		where to put the result
 * @param[out] expired		is the cache entry expired?
 * @retval Was anything in the cache at all?
 *
 * If "is_null_sid(sid)", this was a negative mapping.
 */

bool idmap_cache_find_uid2sid(uid_t uid, struct dom_sid *sid, bool *expired)
{
	char *key;
	char *value;
	time_t timeout;
	bool ret = true;

	key = talloc_asprintf(talloc_tos(), "IDMAP/UID2SID/%d", (int)uid);
	if (key == NULL) {
		return false;
	}
	ret = gencache_get(key, &value, &timeout);
	TALLOC_FREE(key);
	if (!ret) {
		return false;
	}
	ZERO_STRUCTP(sid);
	if (value[0] != '-') {
		ret = string_to_sid(sid, value);
	}
	SAFE_FREE(value);
	if (ret) {
		*expired = (timeout <= time(NULL));
	}
	return ret;
}

/**
 * Find a gid2sid mapping
 * @param[in] gid		the gid to map
 * @param[out] sid		where to put the result
 * @param[out] expired		is the cache entry expired?
 * @retval Was anything in the cache at all?
 *
 * If "is_null_sid(sid)", this was a negative mapping.
 */

bool idmap_cache_find_gid2sid(gid_t gid, struct dom_sid *sid, bool *expired)
{
	char *key;
	char *value;
	time_t timeout;
	bool ret = true;

	key = talloc_asprintf(talloc_tos(), "IDMAP/GID2SID/%d", (int)gid);
	if (key == NULL) {
		return false;
	}
	ret = gencache_get(key, &value, &timeout);
	TALLOC_FREE(key);
	if (!ret) {
		return false;
	}
	ZERO_STRUCTP(sid);
	if (value[0] != '-') {
		ret = string_to_sid(sid, value);
	}
	SAFE_FREE(value);
	if (ret) {
		*expired = (timeout <= time(NULL));
	}
	return ret;
}

/**
 * Store a mapping in the idmap cache
 * @param[in] sid		the sid to map
 * @param[in] gid		the gid to map
 *
 * If both parameters are valid values, then a positive mapping in both
 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
 * negative mapping of gid, we want to cache that for this gid we could not
 * find anything. Likewise if "gid==-1", then we want to cache that we did not
 * find a mapping for the sid passed here.
 */

void idmap_cache_set_sid2unixid(const struct dom_sid *sid, struct unixid *unix_id)
{
	time_t now = time(NULL);
	time_t timeout;
	fstring sidstr, key, value;

	if (!is_null_sid(sid)) {
		fstr_sprintf(key, "IDMAP/SID2XID/%s",
			     sid_to_fstring(sidstr, sid));
		switch (unix_id->type) {
		case ID_TYPE_UID:
			fstr_sprintf(value, "%d:U", (int)unix_id->id);
			break;
		case ID_TYPE_GID:
			fstr_sprintf(value, "%d:G", (int)unix_id->id);
			break;
		case ID_TYPE_BOTH:
			fstr_sprintf(value, "%d:B", (int)unix_id->id);
			break;
		default:
			return;
		}
		timeout = (unix_id->id == -1)
			? lp_idmap_negative_cache_time()
			: lp_idmap_cache_time();
		gencache_set(key, value, now + timeout);
	}
	if (unix_id->id != -1) {
		if (is_null_sid(sid)) {
			/* negative gid mapping */
			fstrcpy(value, "-");
			timeout = lp_idmap_negative_cache_time();
		}
		else {
			sid_to_fstring(value, sid);
			timeout = lp_idmap_cache_time();
		}
		switch (unix_id->type) {
		case ID_TYPE_BOTH:
			fstr_sprintf(key, "IDMAP/UID2SID/%d", (int)unix_id->id);
			gencache_set(key, value, now + timeout);
			fstr_sprintf(key, "IDMAP/GID2SID/%d", (int)unix_id->id);
			gencache_set(key, value, now + timeout);
			return;

		case ID_TYPE_UID:
			fstr_sprintf(key, "IDMAP/UID2SID/%d", (int)unix_id->id);
			break;

		case ID_TYPE_GID:
			fstr_sprintf(key, "IDMAP/GID2SID/%d", (int)unix_id->id);
			break;

		default:
			return;
		}
		gencache_set(key, value, now + timeout);
	}
}

/**
 * Store a mapping in the idmap cache
 * @param[in] sid		the sid to map
 * @param[in] uid		the uid to map
 *
 * If both parameters are valid values, then a positive mapping in both
 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
 * negative mapping of uid, we want to cache that for this uid we could not
 * find anything. Likewise if "uid==-1", then we want to cache that we did not
 * find a mapping for the sid passed here.
 */

void idmap_cache_set_sid2uid(const struct dom_sid *sid, uid_t uid)
{
	struct unixid id;
	id.type = ID_TYPE_UID;
	id.id = uid;

	if (uid == -1) {
		uid_t tmp_gid;
		bool expired;
		/* If we were asked to invalidate this SID -> UID
		 * mapping, it was because we found out that this was
		 * not a UID at all.  Do not overwrite a valid GID or
		 * BOTH mapping */
		if (idmap_cache_find_sid2gid(sid, &tmp_gid, &expired)) {
			if (!expired) {
				return;
			}
		}
	}

	idmap_cache_set_sid2unixid(sid, &id);
	return;
}

/**
 * Store a mapping in the idmap cache
 * @param[in] sid		the sid to map
 * @param[in] gid		the gid to map
 *
 * If both parameters are valid values, then a positive mapping in both
 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
 * negative mapping of gid, we want to cache that for this gid we could not
 * find anything. Likewise if "gid==-1", then we want to cache that we did not
 * find a mapping for the sid passed here.
 */

void idmap_cache_set_sid2gid(const struct dom_sid *sid, gid_t gid)
{
	struct unixid id;
	id.type = ID_TYPE_GID;
	id.id = gid;

	if (gid == -1) {
		uid_t tmp_uid;
		bool expired;
		/* If we were asked to invalidate this SID -> GID
		 * mapping, it was because we found out that this was
		 * not a GID at all.  Do not overwrite a valid UID or
		 * BOTH mapping */
		if (idmap_cache_find_sid2uid(sid, &tmp_uid, &expired)) {
			if (!expired) {
				return;
			}
		}
	}

	idmap_cache_set_sid2unixid(sid, &id);
	return;
}

static char* key_xid2sid_str(TALLOC_CTX* mem_ctx, char t, const char* id) {
	return talloc_asprintf(mem_ctx, "IDMAP/%cID2SID/%s", t, id);
}

static char* key_xid2sid(TALLOC_CTX* mem_ctx, char t, int id) {
	char str[32];
	snprintf(str, sizeof(str), "%d", id);
	return key_xid2sid_str(mem_ctx, t, str);
}

static char* key_sid2xid_str(TALLOC_CTX* mem_ctx, const char* id) {
	return talloc_asprintf(mem_ctx, "IDMAP/SID2XID/%s", id);
}

static bool idmap_cache_del_xid(char t, int xid)
{
	TALLOC_CTX* mem_ctx = talloc_stackframe();
	const char* key = key_xid2sid(mem_ctx, t, xid);
	char* sid_str = NULL;
	time_t timeout;
	bool ret = true;

	if (!gencache_get(key, &sid_str, &timeout)) {
		DEBUG(3, ("no entry: %s\n", key));
		ret = false;
		goto done;
	}

	if (sid_str[0] != '-') {
		const char* sid_key = key_sid2xid_str(mem_ctx, sid_str);
		if (!gencache_del(sid_key)) {
			DEBUG(2, ("failed to delete: %s\n", sid_key));
			ret = false;
		} else {
			DEBUG(5, ("delete: %s\n", sid_key));
		}

	}

	if (!gencache_del(key)) {
		DEBUG(1, ("failed to delete: %s\n", key));
		ret = false;
	} else {
		DEBUG(5, ("delete: %s\n", key));
	}

done:
	talloc_free(mem_ctx);
	return ret;
}

bool idmap_cache_del_uid(uid_t uid) {
	return idmap_cache_del_xid('U', uid);
}

bool idmap_cache_del_gid(gid_t gid) {
	return idmap_cache_del_xid('G', gid);
}

bool idmap_cache_del_sid(const struct dom_sid *sid)
{
	TALLOC_CTX* mem_ctx = talloc_stackframe();
	bool ret = true;
	bool expired;
	struct unixid id;
	const char *sid_key;

	if (!idmap_cache_find_sid2unixid(sid, &id, &expired)) {
		ret = false;
		goto done;
	}

	if (id.id != -1) {
		switch (id.type) {
		case ID_TYPE_BOTH:
			idmap_cache_del_xid('U', id.id);
			idmap_cache_del_xid('G', id.id);
			break;
		case ID_TYPE_UID:
			idmap_cache_del_xid('U', id.id);
			break;
		case ID_TYPE_GID:
			idmap_cache_del_xid('G', id.id);
			break;
		default:
			break;
		}
	}

	sid_key = key_sid2xid_str(mem_ctx, dom_sid_string(mem_ctx, sid));
	if (sid_key == NULL) {
		return false;
	}
	/* If the mapping was symmetric, then this should fail */
	gencache_del(sid_key);
done:
	talloc_free(mem_ctx);
	return ret;
}