summaryrefslogtreecommitdiffstats
path: root/source3/winbindd/idmap_script.c
blob: 3a0d685507ba4cbcf01d29ada90007ea89dcaf6c (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
/*
   Unix SMB/CIFS implementation.

   idmap script backend, used for Samba setups where you need to map SIDs to
   specific UIDs/GIDs.

   Copyright (C) Richard Sharpe 2014.

   This is heavily based upon idmap_tdb2.c, which is:

   Copyright (C) Tim Potter 2000
   Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
   Copyright (C) Jeremy Allison 2006
   Copyright (C) Simo Sorce 2003-2006
   Copyright (C) Michael Adam 2009-2010

   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"
#include "system/filesys.h"
#include "winbindd.h"
#include "idmap.h"
#include "idmap_rw.h"
#include "../libcli/security/dom_sid.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_IDMAP

struct idmap_script_context {
	const char *script; /* script to provide idmaps */
};

/*
  run a script to perform a mapping

  The script should accept the following command lines:

      SIDTOID S-1-xxxx -> XID:<id> | ERR:<str>
      SIDTOID S-1-xxxx -> UID:<id> | ERR:<str>
      SIDTOID S-1-xxxx -> GID:<id> | ERR:<str>
      IDTOSID XID xxxx -> SID:<sid> | ERR:<str>
      IDTOSID UID xxxx -> SID:<sid> | ERR:<str>
      IDTOSID GID xxxx -> SID:<sid> | ERR:<str>

  where XID means both a UID and a GID. This is the case for ID_TYPE_BOTH.

  TODO: Needs more validation ... like that we got a UID when we asked for one.
 */
static NTSTATUS idmap_script_script(struct idmap_script_context *ctx,
				    struct id_map *map, const char *fmt, ...)
{
	va_list ap;
	char *cmd, **lines;
	int numlines = 0;
	unsigned long v;

	cmd = talloc_asprintf(ctx, "%s ", ctx->script);
	if (!cmd) {
		DEBUG(10, ("Unable to allocate memory for the script command!\n"));
		return NT_STATUS_NO_MEMORY;
	}

	va_start(ap, fmt);
	cmd = talloc_vasprintf_append(cmd, fmt, ap);
	va_end(ap);
	if (!cmd) {
		DEBUG(10, ("Unable to allocate memory for the script command!\n"));
		return NT_STATUS_NO_MEMORY;
	}

	lines = file_lines_pload(cmd, &numlines);
	talloc_free(cmd);
	if (!lines) {
		return NT_STATUS_NONE_MAPPED;
	}

	DEBUG(10,("idmap script gave %d lines, first: %s\n", numlines,
		lines[0]));

	if (sscanf(lines[0], "XID:%lu", &v) == 1) {
		map->xid.id   = v;
		map->xid.type = ID_TYPE_BOTH;
	} else if (sscanf(lines[0], "UID:%lu", &v) == 1) {
		map->xid.id   = v;
		map->xid.type = ID_TYPE_UID;
	} else if (sscanf(lines[0], "GID:%lu", &v) == 1) {
		map->xid.id   = v;
		map->xid.type = ID_TYPE_GID;
	} else if (strncmp(lines[0], "SID:S-", 6) == 0) {
		if (!string_to_sid(map->sid, &lines[0][4])) {
			DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
				 lines[0], ctx->script));
			talloc_free(lines);
			return NT_STATUS_NONE_MAPPED;
		}
	} else {
		DEBUG(0,("Bad reply '%s' from idmap script %s\n",
			 lines[0], ctx->script));
		talloc_free(lines);
		return NT_STATUS_NONE_MAPPED;
	}

	talloc_free(lines);
	return NT_STATUS_OK;
}

/*
  Single id to sid lookup function.
*/
static NTSTATUS idmap_script_id_to_sid(struct idmap_domain *dom,
				       struct id_map *map)
{
	NTSTATUS ret;
	char *keystr;
	char *sidstr;
	struct idmap_script_context *ctx = dom->private_data;

	if (!dom || !map) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	/* apply filters before checking */
	if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
		DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
				map->xid.id, dom->low_id, dom->high_id));
		return NT_STATUS_NONE_MAPPED;
	}

	switch (map->xid.type) {

	case ID_TYPE_UID:
		keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
		break;

	case ID_TYPE_GID:
		keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
		break;

	case ID_TYPE_BOTH:
		keystr = talloc_asprintf(ctx, "XID %lu", (unsigned long)map->xid.id);
		break;

	default:
		DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
		return NT_STATUS_INVALID_PARAMETER;
	}

	if (keystr == NULL) {
		DEBUG(0, ("Out of memory!\n"));
		ret = NT_STATUS_NO_MEMORY;
		goto done;
	}

	DEBUG(10,("Running script to fetch mapping %s\n", keystr));

	ret = idmap_script_script(ctx, map, "IDTOSID %s", keystr);
	if (!NT_STATUS_IS_OK(ret)) {
		goto done;
	}

	sidstr = sid_string_talloc(keystr, map->sid);
	if (!sidstr) {
		ret = NT_STATUS_NO_MEMORY;
		goto done;
	}

	DEBUG(10,("Found id %s:%d -> %s\n", keystr, map->xid.id,
		  (const char *)sidstr));
	ret = NT_STATUS_OK;

done:
	talloc_free(keystr);
	return ret;
}

/*
 Single sid to id lookup function.
*/
static NTSTATUS idmap_script_sid_to_id(struct idmap_domain *dom,
				       struct id_map *map)
{
	NTSTATUS ret;
	char *keystr;
	struct idmap_script_context *ctx = dom->private_data;
	TALLOC_CTX *tmp_ctx = talloc_stackframe();

	keystr = sid_string_talloc(tmp_ctx, map->sid);
	if (keystr == NULL) {
		DEBUG(0, ("Out of memory!\n"));
		ret = NT_STATUS_NO_MEMORY;
		goto done;
	}

	DEBUG(10,("Fetching record %s\n", keystr));

	if (ctx->script == NULL) {
		ret = NT_STATUS_NONE_MAPPED;
		goto done;
	}

	ret = idmap_script_script(ctx, map, "SIDTOID %s", keystr);
	if (!NT_STATUS_IS_OK(ret)) {
		goto done;
	}

	/* apply filters before returning result */
	if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
		DEBUG(5, ("Script returned id (%u) out of range (%u - %u)."
			  " Filtered!\n",
			  map->xid.id, dom->low_id, dom->high_id));
		ret = NT_STATUS_NONE_MAPPED;
		goto done;
	}

done:
	talloc_free(tmp_ctx);
	return ret;
}

static NTSTATUS idmap_script_unixids_to_sids(struct idmap_domain *dom,
				      struct id_map **ids)
{
	NTSTATUS ret;
	int i, num_mapped = 0;

	DEBUG(10, ("%s called ...\n", __func__));
	/* Init status to avoid surprise ... */
	for (i = 0; ids[i]; i++) {
		ids[i]->status = ID_UNKNOWN;
	}

	for (i = 0; ids[i]; i++) {
		ret = idmap_script_id_to_sid(dom, ids[i]);
		if (!NT_STATUS_IS_OK(ret)) {
			if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
				ids[i]->status = ID_UNMAPPED;
				continue;
			}

			/*
			 * We cannot keep going if it is other than mapping
			 * failed.
			 */
			goto done;
		}

		ids[i]->status = ID_MAPPED;
		num_mapped++;
	}

	ret = NT_STATUS_OK;

done:
	if (NT_STATUS_IS_OK(ret)) {
		if (i == 0 || num_mapped == 0) {
			ret = NT_STATUS_NONE_MAPPED;
		}
		else if (num_mapped < i) {
			ret = STATUS_SOME_UNMAPPED;
		} else {
			DEBUG(10, ("Returning NT_STATUS_OK\n"));
			ret = NT_STATUS_OK;
		}
	}

	return ret;
}

static NTSTATUS idmap_script_sids_to_unixids(struct idmap_domain *dom,
				      struct id_map **ids)
{
	NTSTATUS ret;
	int i, num_mapped = 0;

	DEBUG(10, ("%s called ...\n", __func__));
	/* Init status to avoid surprise ... */
	for (i = 0; ids[i]; i++) {
		ids[i]->status = ID_UNKNOWN;
	}

	for (i = 0; ids[i]; i++) {
		ret = idmap_script_sid_to_id(dom, ids[i]);
		if (!NT_STATUS_IS_OK(ret)) {
			if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
				ids[i]->status = ID_UNMAPPED;
				continue;
			}

			/*
			 * We cannot keep going if it is other than mapping
			 * failed.
			 */
			goto done;
		}

		ids[i]->status = ID_MAPPED;
		num_mapped++;
	}

	ret = NT_STATUS_OK;

done:
	if (NT_STATUS_IS_OK(ret)) {
		if (i == 0 || num_mapped == 0) {
			ret = NT_STATUS_NONE_MAPPED;
		}
		else if (num_mapped < i) {
			ret = STATUS_SOME_UNMAPPED;
		} else {
			DEBUG(10, ("Returning NT_STATUS_OK\n"));
			ret = NT_STATUS_OK;
		}
	}

	return ret;
}

/*
 *   Initialise idmap_script database.
 */
static NTSTATUS idmap_script_db_init(struct idmap_domain *dom)
{
	NTSTATUS ret;
	struct idmap_script_context *ctx;
	char *config_option = NULL;
	const char * idmap_script = NULL;

	DEBUG(10, ("%s called ...\n", __func__));

	ctx = talloc_zero(dom, struct idmap_script_context);
	if (!ctx) {
		DEBUG(0, ("Out of memory!\n"));
		ret = NT_STATUS_NO_MEMORY;
		goto failed;
	}

	config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
	if (config_option == NULL) {
		DEBUG(0, ("Out of memory!\n"));
		ret = NT_STATUS_NO_MEMORY;
		goto failed;
	}
	ctx->script = lp_parm_const_string(-1, config_option, "script", NULL);
	talloc_free(config_option);

	/* Do we even need to handle this? */
	idmap_script = lp_parm_const_string(-1, "idmap", "script", NULL);
	if (idmap_script != NULL) {
		DEBUG(0, ("Warning: 'idmap:script' is deprecated. "
			  " Please use 'idmap config * : script' instead!\n"));
	}

	if (strequal(dom->name, "*") && ctx->script == NULL) {
		/* fall back to idmap:script for backwards compatibility */
		ctx->script = idmap_script;
	}

	if (ctx->script) {
		DEBUG(1, ("using idmap script '%s'\n", ctx->script));
	}

	dom->private_data = ctx;
	dom->read_only = true; /* We do not allocate!*/

	return NT_STATUS_OK;

failed:
	talloc_free(ctx);
	return ret;
}

static struct idmap_methods db_methods = {
	.init            = idmap_script_db_init,
	.unixids_to_sids = idmap_script_unixids_to_sids,
	.sids_to_unixids = idmap_script_sids_to_unixids,
};

NTSTATUS idmap_script_init(void)
{
	return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "script", &db_methods);
}