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

   idmap NSS backend

   Copyright (C) Simo Sorce 2006

   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 "system/passwd.h"
#include "winbindd.h"
#include "nsswitch/winbind_client.h"
#include "idmap.h"
#include "lib/winbind_util.h"
#include "libcli/security/dom_sid.h"
#include "lib/global_contexts.h"
#include "messages.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_IDMAP

struct idmap_nss_context {
	struct idmap_domain *dom;
	bool use_upn;
};

static int idmap_nss_context_destructor(struct idmap_nss_context *ctx)
{
	if ((ctx->dom != NULL) && (ctx->dom->private_data == ctx)) {
		ctx->dom->private_data = NULL;
	}
	return 0;
}

static NTSTATUS idmap_nss_context_create(TALLOC_CTX *mem_ctx,
					 struct idmap_domain *dom,
					 struct idmap_nss_context **pctx)
{
	struct idmap_nss_context *ctx = NULL;

	ctx = talloc_zero(mem_ctx, struct idmap_nss_context);
	if (ctx == NULL) {
		return NT_STATUS_NO_MEMORY;
	}
	ctx->dom = dom;

	talloc_set_destructor(ctx, idmap_nss_context_destructor);

	ctx->use_upn = idmap_config_bool(dom->name, "use_upn", false);

	*pctx = ctx;
	return NT_STATUS_OK;
}

static NTSTATUS idmap_nss_get_context(struct idmap_domain *dom,
				      struct idmap_nss_context **pctx)
{
	struct idmap_nss_context *ctx = NULL;
	NTSTATUS status;

	if (dom->private_data != NULL) {
		*pctx = talloc_get_type_abort(dom->private_data,
					      struct idmap_nss_context);
		return NT_STATUS_OK;
	}

	status = idmap_nss_context_create(dom, dom, &ctx);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_WARNING("idmap_nss_context_create failed: %s\n",
			    nt_errstr(status));
		return status;
	}

	dom->private_data = ctx;
	*pctx = ctx;
	return NT_STATUS_OK;
}

static bool idmap_nss_msg_filter(struct messaging_rec *rec, void *private_data)
{
	struct idmap_domain *dom = talloc_get_type_abort(private_data,
		       struct idmap_domain);
	struct idmap_nss_context *ctx = NULL;
	NTSTATUS status;
	bool ret;

	if (rec->msg_type == MSG_SMB_CONF_UPDATED) {
		ret = lp_load_global(get_dyn_CONFIGFILE());
		if (!ret) {
			DBG_WARNING("Failed to reload configuration\n");
			return false;
		}

		status = idmap_nss_get_context(dom, &ctx);
		if (NT_STATUS_IS_ERR(status)) {
			DBG_WARNING("Failed to get idmap nss context: %s\n",
					nt_errstr(status));
			return false;
		}

		ctx->use_upn = idmap_config_bool(dom->name, "use_upn", false);
	}

	return false;
}

/*****************************
 Initialise idmap database.
*****************************/

static NTSTATUS idmap_nss_int_init(struct idmap_domain *dom)
{
	struct idmap_nss_context *ctx = NULL;
	NTSTATUS status;
	struct messaging_context *msg_ctx = global_messaging_context();
	struct tevent_req *req = NULL;

	status = idmap_nss_context_create(dom, dom, &ctx);
	if (NT_STATUS_IS_ERR(status)) {
		return status;
	}

	dom->private_data = ctx;

	req = messaging_filtered_read_send(
			dom,
			messaging_tevent_context(msg_ctx),
			msg_ctx,
			idmap_nss_msg_filter,
			dom);
	if (req == NULL) {
		DBG_WARNING("messaging_filtered_read_send failed\n");
		return NT_STATUS_UNSUCCESSFUL;
	}

	return status;
}

static NTSTATUS idmap_nss_lookup_name(const char *namespace,
				      const char *username,
				      struct dom_sid *sid,
				      enum lsa_SidType *type)
{
	bool ret;

	/*
	 * By default calls to winbindd are disabled
	 * the following call will not recurse so this is safe
	 */
	(void)winbind_on();
	ret = winbind_lookup_name(namespace, username, sid, type);
	(void)winbind_off();

	if (!ret) {
		DBG_NOTICE("Failed to lookup name [%s] in namespace [%s]\n",
			   username, namespace);
		return NT_STATUS_NOT_FOUND;
	}

	return NT_STATUS_OK;
}

/**********************************
 lookup a set of unix ids.
**********************************/

static NTSTATUS idmap_nss_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
{
	struct idmap_nss_context *ctx = NULL;
	NTSTATUS status;
	int i;

	status = idmap_nss_get_context(dom, &ctx);
	if (NT_STATUS_IS_ERR(status)) {
		DBG_WARNING("Failed to get idmap nss context: %s\n",
			    nt_errstr(status));
		return status;
	}

	/* initialize the status to avoid surprise */
	for (i = 0; ids[i]; i++) {
		ids[i]->status = ID_UNKNOWN;
	}

	for (i = 0; ids[i]; i++) {
		struct passwd *pw;
		struct group *gr;
		const char *name;
		struct dom_sid sid;
		enum lsa_SidType type;

		switch (ids[i]->xid.type) {
		case ID_TYPE_UID:
			errno = 0;
			pw = getpwuid((uid_t)ids[i]->xid.id);
			if (!pw) {
				DBG_DEBUG("getpwuid(%lu) failed: %s\n",
					  (unsigned long)ids[i]->xid.id,
					  errno != 0
					  ? strerror(errno)
					  : "not found");
				ids[i]->status = ID_UNMAPPED;
				continue;
			}
			name = pw->pw_name;
			break;
		case ID_TYPE_GID:
			errno = 0;
			gr = getgrgid((gid_t)ids[i]->xid.id);
			if (!gr) {
				DBG_DEBUG("getgrgid(%lu) failed: %s\n",
					  (unsigned long)ids[i]->xid.id,
					  errno != 0
					  ? strerror(errno)
					  : "not found");
				ids[i]->status = ID_UNMAPPED;
				continue;
			}
			name = gr->gr_name;
			break;
		default: /* ?? */
			DBG_WARNING("Unexpected xid type %d\n",
				    ids[i]->xid.type);
			ids[i]->status = ID_UNKNOWN;
			continue;
		}

		/* Lookup name from PDC using lsa_lookup_names() */
		if (ctx->use_upn) {
			char *p = NULL;
			const char *namespace = NULL;
			const char *domname = NULL;
			const char *domuser = NULL;

			p = strstr(name, lp_winbind_separator());
			if (p != NULL) {
				*p = '\0';
				domname = name;
				namespace = domname;
				domuser = p + 1;
			} else {
				p = strchr(name, '@');
				if (p != NULL) {
					*p = '\0';
					namespace = p + 1;
					domname = "";
					domuser = name;
				} else {
					namespace = dom->name;
					domuser = name;
				}
			}

			DBG_DEBUG("Using namespace [%s] from UPN instead "
				  "of [%s] to lookup the name [%s]\n",
				  namespace, dom->name, domuser);

			status = idmap_nss_lookup_name(namespace,
						       domuser,
						       &sid,
						       &type);
		} else {
			status = idmap_nss_lookup_name(dom->name,
						       name,
						       &sid,
						       &type);
                }

		if (NT_STATUS_IS_ERR(status)) {
			/*
			 * TODO: how do we know if the name is really
			 * not mapped, or something just failed ?
			 */
			ids[i]->status = ID_UNMAPPED;
			continue;
		}

		switch (type) {
		case SID_NAME_USER:
			if (ids[i]->xid.type == ID_TYPE_UID) {
				sid_copy(ids[i]->sid, &sid);
				ids[i]->status = ID_MAPPED;
			}
			break;

		case SID_NAME_DOM_GRP:
		case SID_NAME_ALIAS:
		case SID_NAME_WKN_GRP:
			if (ids[i]->xid.type == ID_TYPE_GID) {
				sid_copy(ids[i]->sid, &sid);
				ids[i]->status = ID_MAPPED;
			}
			break;

		default:
			ids[i]->status = ID_UNKNOWN;
			break;
		}
	}
	return NT_STATUS_OK;
}

/**********************************
 lookup a set of sids.
**********************************/

static NTSTATUS idmap_nss_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
{
	struct idmap_nss_context *ctx = NULL;
	NTSTATUS status;
	int i;

	status = idmap_nss_get_context(dom, &ctx);
	if (NT_STATUS_IS_ERR(status)) {
		DBG_WARNING("Failed to get idmap nss context: %s\n",
			    nt_errstr(status));
		return status;
	}

	/* initialize the status to avoid surprise */
	for (i = 0; ids[i]; i++) {
		ids[i]->status = ID_UNKNOWN;
	}

	for (i = 0; ids[i]; i++) {
		struct group *gr;
		enum lsa_SidType type;
		const char *_domain = NULL;
		const char *_name = NULL;
		char *domain = NULL;
		char *name = NULL;
		char *fqdn = NULL;
		char *sname = NULL;
		bool ret;

		/* by default calls to winbindd are disabled
		   the following call will not recurse so this is safe */
		(void)winbind_on();
		ret = winbind_lookup_sid(talloc_tos(),
					 ids[i]->sid,
					 &_domain,
					 &_name,
					 &type);
		(void)winbind_off();
		if (!ret) {
			/* TODO: how do we know if the name is really not mapped,
			 * or something just failed ? */
			ids[i]->status = ID_UNMAPPED;
			continue;
		}

		domain = discard_const_p(char, _domain);
		name = discard_const_p(char, _name);

		if (!strequal(domain, dom->name)) {
			struct dom_sid_buf buf;
			DBG_ERR("DOMAIN[%s] ignoring SID[%s] belongs to %s [%s\\%s]\n",
			        dom->name, dom_sid_str_buf(ids[i]->sid, &buf),
				sid_type_lookup(type), domain, name);
			ids[i]->status = ID_UNMAPPED;
			continue;
		}

		if (ctx->use_upn) {
			fqdn = talloc_asprintf(talloc_tos(),
					       "%s%s%s",
					       domain,
					       lp_winbind_separator(),
					       name);
			if (fqdn == NULL) {
				DBG_ERR("No memory\n");
				ids[i]->status = ID_UNMAPPED;
				continue;
			}
			DBG_DEBUG("Using UPN [%s] instead of plain name [%s]\n",
				  fqdn, name);
			sname = fqdn;
		} else {
			sname = name;
		}

		switch (type) {
		case SID_NAME_USER: {
			struct passwd *pw;

			/* this will find also all lower case name and use username level */
			pw = Get_Pwnam_alloc(talloc_tos(), sname);
			if (pw) {
				ids[i]->xid.id = pw->pw_uid;
				ids[i]->xid.type = ID_TYPE_UID;
				ids[i]->status = ID_MAPPED;
			}
			TALLOC_FREE(pw);
			break;
		}

		case SID_NAME_DOM_GRP:
		case SID_NAME_ALIAS:
		case SID_NAME_WKN_GRP:

			gr = getgrnam(sname);
			if (gr) {
				ids[i]->xid.id = gr->gr_gid;
				ids[i]->xid.type = ID_TYPE_GID;
				ids[i]->status = ID_MAPPED;
			}
			break;

		default:
			ids[i]->status = ID_UNKNOWN;
			break;
		}
		TALLOC_FREE(domain);
		TALLOC_FREE(name);
		TALLOC_FREE(fqdn);
	}
	return NT_STATUS_OK;
}

/**********************************
 Close the idmap tdb instance
**********************************/

static const struct idmap_methods nss_methods = {
	.init = idmap_nss_int_init,
	.unixids_to_sids = idmap_nss_unixids_to_sids,
	.sids_to_unixids = idmap_nss_sids_to_unixids,
};

NTSTATUS idmap_nss_init(TALLOC_CTX *mem_ctx)
{
	return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "nss", &nss_methods);
}