summaryrefslogtreecommitdiff
path: root/source4/libcli/wbclient/wbclient.c
blob: 8cfe117d0b1694b6cd84359c773062bc32cbc8d5 (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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
   Unix SMB/CIFS implementation.

   Winbind client library.

   Copyright (C) 2008 Kai Blin  <kai@samba.org>

   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 <tevent.h>
#include "lib/util/tevent_unix.h"
#include "libcli/wbclient/wbclient.h"
#include "nsswitch/wb_reqtrans.h"
#include "system/network.h"
#include "libcli/util/error.h"
#include "libcli/security/dom_sid.h"

/**
 * Initialize the wbclient context, talloc_free() when done.
 *
 * \param mem_ctx talloc context to allocate memory from
 * \param msg_ctx message context to use
 * \param
 */
struct wbc_context *wbc_init(TALLOC_CTX *mem_ctx,
			     struct imessaging_context *msg_ctx,
			     struct tevent_context *event_ctx)
{
	struct wbc_context *ctx;

	ctx = talloc(mem_ctx, struct wbc_context);
	if (ctx == NULL) return NULL;

	ctx->event_ctx = event_ctx;

	ctx->irpc_handle = irpc_binding_handle_by_name(ctx, msg_ctx,
						       "winbind_server",
						       &ndr_table_winbind);
	if (ctx->irpc_handle == NULL) {
		talloc_free(ctx);
		return NULL;
	}

	return ctx;
}

struct wbc_idmap_state {
	struct composite_context *ctx;
	struct winbind_get_idmap *req;
	struct id_map *ids;
};

static void sids_to_xids_recv_ids(struct tevent_req *subreq);

struct composite_context *wbc_sids_to_xids_send(struct wbc_context *wbc_ctx,
						TALLOC_CTX *mem_ctx,
						uint32_t count,
						struct id_map *ids)
{
	struct composite_context *ctx;
	struct wbc_idmap_state *state;
	struct tevent_req *subreq;

	DEBUG(5, ("wbc_sids_to_xids called\n"));

	ctx = composite_create(mem_ctx, wbc_ctx->event_ctx);
	if (ctx == NULL) return NULL;

	state = talloc(ctx, struct wbc_idmap_state);
	if (composite_nomem(state, ctx)) return ctx;
	ctx->private_data = state;

	state->req = talloc(state, struct winbind_get_idmap);
	if (composite_nomem(state->req, ctx)) return ctx;

	state->req->in.count = count;
	state->req->in.level = WINBIND_IDMAP_LEVEL_SIDS_TO_XIDS;
	state->req->in.ids = ids;
	state->ctx = ctx;

	subreq = dcerpc_winbind_get_idmap_r_send(state,
						 wbc_ctx->event_ctx,
						 wbc_ctx->irpc_handle,
						 state->req);
	if (composite_nomem(subreq, ctx)) return ctx;

	tevent_req_set_callback(subreq, sids_to_xids_recv_ids, state);

	return ctx;
}

static void sids_to_xids_recv_ids(struct tevent_req *subreq)
{
	struct wbc_idmap_state *state =
		tevent_req_callback_data(subreq,
		struct wbc_idmap_state);

	state->ctx->status = dcerpc_winbind_get_idmap_r_recv(subreq, state);
	TALLOC_FREE(subreq);
	if (!composite_is_ok(state->ctx)) return;

	state->ids = state->req->out.ids;
	composite_done(state->ctx);
}

NTSTATUS wbc_sids_to_xids_recv(struct composite_context *ctx,
			       struct id_map **ids)
{
	NTSTATUS status = composite_wait(ctx);
		DEBUG(5, ("wbc_sids_to_xids_recv called\n"));
	if (NT_STATUS_IS_OK(status)) {
		struct wbc_idmap_state *state =	talloc_get_type_abort(
							ctx->private_data,
							struct wbc_idmap_state);
		*ids = state->ids;
	}

	return status;
}

static void xids_to_sids_recv_ids(struct tevent_req *subreq);

struct composite_context *wbc_xids_to_sids_send(struct wbc_context *wbc_ctx,
						TALLOC_CTX *mem_ctx,
						uint32_t count,
						struct id_map *ids)
{
	struct composite_context *ctx;
	struct wbc_idmap_state *state;
	struct tevent_req *subreq;

	DEBUG(5, ("wbc_xids_to_sids called\n"));

	ctx = composite_create(mem_ctx, wbc_ctx->event_ctx);
	if (ctx == NULL) return NULL;

	state = talloc(ctx, struct wbc_idmap_state);
	if (composite_nomem(state, ctx)) return ctx;
	ctx->private_data = state;

	state->req = talloc(state, struct winbind_get_idmap);
	if (composite_nomem(state->req, ctx)) return ctx;

	state->req->in.count = count;
	state->req->in.level = WINBIND_IDMAP_LEVEL_XIDS_TO_SIDS;
	state->req->in.ids = ids;
	state->ctx = ctx;

	subreq = dcerpc_winbind_get_idmap_r_send(state,
						 wbc_ctx->event_ctx,
						 wbc_ctx->irpc_handle,
						 state->req);
	if (composite_nomem(subreq, ctx)) return ctx;

	tevent_req_set_callback(subreq, xids_to_sids_recv_ids, state);

	return ctx;
}

static void xids_to_sids_recv_ids(struct tevent_req *subreq)
{
	struct wbc_idmap_state *state =
		tevent_req_callback_data(subreq,
		struct wbc_idmap_state);

	state->ctx->status = dcerpc_winbind_get_idmap_r_recv(subreq, state);
	TALLOC_FREE(subreq);
	if (!composite_is_ok(state->ctx)) return;

	state->ids = state->req->out.ids;
	composite_done(state->ctx);
}

NTSTATUS wbc_xids_to_sids_recv(struct composite_context *ctx,
			       struct id_map **ids)
{
	NTSTATUS status = composite_wait(ctx);
		DEBUG(5, ("wbc_xids_to_sids_recv called\n"));
	if (NT_STATUS_IS_OK(status)) {
		struct wbc_idmap_state *state =	talloc_get_type_abort(
							ctx->private_data,
							struct wbc_idmap_state);
		*ids = state->ids;
	}

	return status;
}

static int wb_simple_trans(struct tevent_context *ev, int fd,
			   struct winbindd_request *wb_req,
			   TALLOC_CTX *mem_ctx,
			   struct winbindd_response **resp, int *err)
{
	struct tevent_req *req;
	bool polled;
	int ret;

	req = wb_simple_trans_send(ev, ev, NULL, fd, wb_req);
	if (req == NULL) {
		*err = ENOMEM;
		return -1;
	}

	polled = tevent_req_poll(req, ev);
	if (!polled) {
		*err = errno;
		DEBUG(10, ("tevent_req_poll returned %s\n",
			   strerror(*err)));
		return -1;
	}

	ret = wb_simple_trans_recv(req, mem_ctx, resp, err);
	TALLOC_FREE(req);
	return ret;
}

static const char *winbindd_socket_dir(void)
{
#ifdef SOCKET_WRAPPER
	const char *env_dir;

	env_dir = getenv(WINBINDD_SOCKET_DIR_ENVVAR);
	if (env_dir) {
		return env_dir;
	}
#endif

	return WINBINDD_SOCKET_DIR;
}

static int winbindd_pipe_sock(void)
{
	struct sockaddr_un sunaddr = {};
	int ret, fd;
	char *path;

	ret = asprintf(&path, "%s/%s", winbindd_socket_dir(),
		       WINBINDD_SOCKET_NAME);
	if (ret == -1) {
		errno = ENOMEM;
		return -1;
	}
	sunaddr.sun_family = AF_UNIX;
	strlcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path));
	free(path);

	fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (fd == -1) {
		return -1;
	}

	ret = connect(fd, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
	if (ret == -1) {
		int err = errno;
		close(fd);
		errno = err;
		return -1;
	}

	return fd;
}

NTSTATUS wbc_sids_to_xids(struct tevent_context *ev, struct id_map *ids,
			  uint32_t count)
{
	TALLOC_CTX *mem_ctx;
	struct winbindd_request req = {};
	struct winbindd_response *resp;
	uint32_t i;
	int fd, ret, err;
	char *sids, *p;
	size_t sidslen;

	fd = winbindd_pipe_sock();
	if (fd == -1) {
		return map_nt_error_from_unix_common(errno);
	}

	mem_ctx = talloc_new(NULL);
	if (mem_ctx == NULL) {
		close(fd);
		return NT_STATUS_NO_MEMORY;
	}

	sidslen = count * (DOM_SID_STR_BUFLEN + 1);

	sids = talloc_array(mem_ctx, char, sidslen);
	if (sids == NULL) {
		close(fd);
		TALLOC_FREE(mem_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	p = sids;
	for (i=0; i<count; i++) {
		p += dom_sid_string_buf(ids[i].sid, p, sidslen - (p - sids));
		*p++ = '\n';
	}
	*p++ = '\0';

	DEBUG(10, ("sids=\n%s", sids));

	req.length = sizeof(struct winbindd_request);
	req.cmd = WINBINDD_SIDS_TO_XIDS;
	req.pid = getpid();
	req.extra_data.data = sids;
	req.extra_len = sidslen;

	ret = wb_simple_trans(ev, fd, &req, mem_ctx, &resp, &err);
	if (ret == -1) {
		return map_nt_error_from_unix_common(err);
	}

	close(fd);

	p = resp->extra_data.data;

	for (i=0; i<count; i++) {
		struct unixid *id = &ids[i].xid;
		char *q;

		switch (p[0]) {
		case 'U':
			id->type = ID_TYPE_UID;
			id->id = strtoul(p+1, &q, 10);
			break;
		case 'G':
			id->type = ID_TYPE_GID;
			id->id = strtoul(p+1, &q, 10);
			break;
		case 'B':
			id->type = ID_TYPE_BOTH;
			id->id = strtoul(p+1, &q, 10);
			break;
		default:
			id->type = ID_TYPE_NOT_SPECIFIED;
			id->id = UINT32_MAX;
			q = strchr(p, '\n');
			break;
		};
		ids[i].status = ID_MAPPED;

		if (q == NULL || q[0] != '\n') {
			TALLOC_FREE(mem_ctx);
			return NT_STATUS_INTERNAL_ERROR;
		}
		p = q+1;
	}

	return NT_STATUS_OK;
}

struct wbc_id_to_sid_state {
	struct winbindd_request wbreq;
	struct dom_sid sid;
};

static void wbc_id_to_sid_done(struct tevent_req *subreq);

static struct tevent_req *wbc_id_to_sid_send(TALLOC_CTX *mem_ctx,
					     struct tevent_context *ev,
					     int fd, const struct unixid *id)
{
	struct tevent_req *req, *subreq;
	struct wbc_id_to_sid_state *state;

	req = tevent_req_create(mem_ctx, &state, struct wbc_id_to_sid_state);
	if (req == NULL) {
		return NULL;
	}

	switch(id->type) {
	case ID_TYPE_UID:
		state->wbreq.cmd = WINBINDD_UID_TO_SID;
		state->wbreq.data.uid = id->id;
		break;
	case ID_TYPE_GID:
		state->wbreq.cmd = WINBINDD_GID_TO_SID;
		state->wbreq.data.gid = id->id;
		break;
	default:
		tevent_req_error(req, ENOENT);
		return tevent_req_post(req, ev);
	}

	subreq = wb_simple_trans_send(state, ev, NULL, fd, &state->wbreq);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, wbc_id_to_sid_done, req);
	return req;
}

static void wbc_id_to_sid_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct wbc_id_to_sid_state *state = tevent_req_data(
		req, struct wbc_id_to_sid_state);
	struct winbindd_response *wbresp;
	int ret, err;

	ret = wb_simple_trans_recv(subreq, state, &wbresp, &err);
	TALLOC_FREE(subreq);
	if (ret == -1) {
		tevent_req_error(req, err);
		return;
	}
	if ((wbresp->result != WINBINDD_OK) ||
	    !dom_sid_parse(wbresp->data.sid.sid, &state->sid)) {
		tevent_req_error(req, ENOENT);
		return;
	}
	tevent_req_done(req);
}

static int wbc_id_to_sid_recv(struct tevent_req *req, struct dom_sid *sid)
{
	struct wbc_id_to_sid_state *state = tevent_req_data(
		req, struct wbc_id_to_sid_state);
	int err;

	if (tevent_req_is_unix_error(req, &err)) {
		return err;
	}
	sid_copy(sid, &state->sid);
	return 0;
}

struct wbc_ids_to_sids_state {
	struct tevent_context *ev;
	int fd;
	struct id_map *ids;
	uint32_t count;
	uint32_t idx;
};

static void wbc_ids_to_sids_done(struct tevent_req *subreq);

static struct tevent_req *wbc_ids_to_sids_send(
	TALLOC_CTX *mem_ctx, struct tevent_context *ev,
	int fd, struct id_map *ids, uint32_t count)
{
	struct tevent_req *req, *subreq;
	struct wbc_ids_to_sids_state *state;

	req = tevent_req_create(mem_ctx, &state,
				struct wbc_ids_to_sids_state);
	if (req == NULL) {
		return NULL;
	}
	state->ev = ev;
	state->fd = fd;
	state->ids = ids;
	state->count = count;

	if (count == 0) {
		tevent_req_done(req);
		return tevent_req_post(req, ev);
	}

	subreq = wbc_id_to_sid_send(state, state->ev, state->fd,
				    &state->ids[state->idx].xid);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, wbc_ids_to_sids_done, req);
	return req;
}

static void wbc_ids_to_sids_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct wbc_ids_to_sids_state *state = tevent_req_data(
		req, struct wbc_ids_to_sids_state);
	struct id_map *id;
	struct dom_sid sid;
	int ret;

	ret = wbc_id_to_sid_recv(subreq, &sid);
	TALLOC_FREE(subreq);

	id = &state->ids[state->idx];
	if (ret == 0) {
		id->status = ID_MAPPED;
		id->sid = dom_sid_dup(state->ids, &sid);
		if (id->sid == NULL) {
			tevent_req_error(req, ENOMEM);
			return;
		}
	} else {
		id->status = ID_UNMAPPED;
		id->sid = NULL;
	}

	state->idx += 1;
	if (state->idx == state->count) {
		tevent_req_done(req);
		return;
	}

	subreq = wbc_id_to_sid_send(state, state->ev, state->fd,
				    &state->ids[state->idx].xid);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, wbc_ids_to_sids_done, req);
}

static int wbc_ids_to_sids_recv(struct tevent_req *req)
{
	int err;
	if (tevent_req_is_unix_error(req, &err)) {
		return err;
	}
	return 0;
}

NTSTATUS wbc_xids_to_sids(struct tevent_context *ev, struct id_map *ids,
			  uint32_t count)
{
	struct tevent_req *req;
	NTSTATUS status;
	bool polled;
	int ret, fd;

	DEBUG(5, ("wbc_xids_to_sids called: %u ids\n", (unsigned)count));

	fd = winbindd_pipe_sock();
	if (fd == -1) {
		status = map_nt_error_from_unix_common(errno);
		DEBUG(10, ("winbindd_pipe_sock returned %s\n",
			   strerror(errno)));
		return status;
	}

	req = wbc_ids_to_sids_send(ev, ev, fd, ids, count);
	if (req == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto done;
	}

	polled = tevent_req_poll(req, ev);
	if (!polled) {
		status = map_nt_error_from_unix_common(errno);
		DEBUG(10, ("tevent_req_poll returned %s\n",
			   strerror(errno)));
		goto done;
	}

	ret = wbc_ids_to_sids_recv(req);
	TALLOC_FREE(req);
	if (ret != 0) {
		status = map_nt_error_from_unix_common(ret);
		DEBUG(10, ("tevent_req_poll returned %s\n",
			   strerror(ret)));
	} else {
		status = NT_STATUS_OK;
	}

done:
	close(fd);
	return status;
}