summaryrefslogtreecommitdiff
path: root/source4/winbind/wb_sid2gid.c
blob: e1061544d24ca29154e1edb0a0f5a0a70c9b52cf (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
/*
   Unix SMB/CIFS implementation.

   Map a SID to a gid

   Copyright (C) 2007-2008  Kai Blin

   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 "libcli/composite/composite.h"
#include "winbind/wb_server.h"
#include "smbd/service_task.h"
#include "libcli/security/security.h"

struct sid2gid_state {
	struct composite_context *ctx;
	struct wbsrv_service *service;
	gid_t gid;
};

static void sid2gid_recv_gid(struct composite_context *ctx);

struct composite_context *wb_sid2gid_send(TALLOC_CTX *mem_ctx,
		struct wbsrv_service *service, const struct dom_sid *sid)
{
	struct composite_context *result, *ctx;
	struct sid2gid_state *state;
	struct id_map *ids;

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

	result = composite_create(mem_ctx, service->task->event_ctx);
	if (!result) return NULL;

	state = talloc(result, struct sid2gid_state);
	if(composite_nomem(state, result)) return result;

	state->ctx = result;
	result->private_data = state;
	state->service = service;

	ids = talloc(result, struct id_map);
	if (composite_nomem(ids, result)) return result;

	ids->sid = dom_sid_dup(result, sid);
	if (composite_nomem(ids->sid, result)) return result;

	ctx = wb_sids2xids_send(result, service, 1, ids);
	if (composite_nomem(ctx, result)) return result;

	composite_continue(result, ctx, sid2gid_recv_gid, state);
	return result;
}

static void sid2gid_recv_gid(struct composite_context *ctx)
{
	struct sid2gid_state *state = talloc_get_type(ctx->async.private_data,
						      struct sid2gid_state);

	struct id_map *ids = NULL;

	state->ctx->status = wb_sids2xids_recv(ctx, &ids, NULL);
	if (!composite_is_ok(state->ctx)) return;

	if (ids->status != ID_MAPPED) {
		composite_error(state->ctx, NT_STATUS_UNSUCCESSFUL);
		return;
	}

	if (ids->xid.type == ID_TYPE_BOTH ||
	    ids->xid.type == ID_TYPE_GID) {
		state->gid = ids->xid.id;
		composite_done(state->ctx);
		return;
	} else {
		composite_error(state->ctx, NT_STATUS_INVALID_SID);
		return;
	}
}

NTSTATUS wb_sid2gid_recv(struct composite_context *ctx, gid_t *gid)
{
	NTSTATUS status = composite_wait(ctx);

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

	if (NT_STATUS_IS_OK(status)) {
		struct sid2gid_state *state =
			talloc_get_type(ctx->private_data,
				struct sid2gid_state);
		*gid = state->gid;
	}
	talloc_free(ctx);
	return status;
}