summaryrefslogtreecommitdiff
path: root/source4/libcli/wbclient/wbclient.c
blob: 69d8b439ad9a33f7606d900edb9cf07e86adfe5c (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
/*
   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 "nsswitch/winbind_client.h"
#include "libcli/wbclient/wbclient.h"
#include "libcli/security/dom_sid.h"
#include "nsswitch/libwbclient/wbclient.h"

NTSTATUS wbc_sids_to_xids(struct id_map *ids, uint32_t count)
{
	TALLOC_CTX *mem_ctx;
	uint32_t i;
	struct wbcDomainSid *sids;
	struct wbcUnixId *xids;
	wbcErr result;
	bool wb_off;

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

	sids = talloc_array(mem_ctx, struct wbcDomainSid, count);
	if (sids == NULL) {
		TALLOC_FREE(mem_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	xids = talloc_array(mem_ctx, struct wbcUnixId, count);
	if (xids == NULL) {
		TALLOC_FREE(mem_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	for (i=0; i<count; i++) {
		memcpy(&sids[i], ids[i].sid, sizeof(struct dom_sid));
	}

	wb_off = winbind_env_set();
	if (wb_off) {
		(void)winbind_on();
	}

	result = wbcSidsToUnixIds(sids, count, xids);

	if (wb_off) {
		(void)winbind_off();
	}

	if (!WBC_ERROR_IS_OK(result)) {
		TALLOC_FREE(mem_ctx);
		return NT_STATUS_INTERNAL_ERROR;
	}

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

		switch (xid->type) {
		    case WBC_ID_TYPE_UID:
			id->type = ID_TYPE_UID;
			id->id = xid->id.uid;
			break;
		    case WBC_ID_TYPE_GID:
			id->type = ID_TYPE_GID;
			id->id = xid->id.gid;
			break;
		    case WBC_ID_TYPE_BOTH:
			id->type = ID_TYPE_BOTH;
			id->id = xid->id.uid;
			break;
		    case WBC_ID_TYPE_NOT_SPECIFIED:
			id->type = ID_TYPE_NOT_SPECIFIED;
			id->id = UINT32_MAX;
			break;
		}
		ids[i].status = ID_MAPPED;
	}

	TALLOC_FREE(mem_ctx);

	return NT_STATUS_OK;
}

NTSTATUS wbc_xids_to_sids(struct id_map *ids, uint32_t count)
{
	TALLOC_CTX *mem_ctx;
	uint32_t i;
	struct wbcDomainSid *sids;
	struct wbcUnixId *xids;
	wbcErr result;
	bool wb_off;

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

	sids = talloc_array(mem_ctx, struct wbcDomainSid, count);
	if (sids == NULL) {
		TALLOC_FREE(mem_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	xids = talloc_array(mem_ctx, struct wbcUnixId, count);
	if (xids == NULL) {
		TALLOC_FREE(mem_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	for (i=0; i<count; i++) {
		struct id_map *id = &ids[i];
		struct wbcUnixId *xid = &xids[i];

		switch (id->xid.type) {
		    case ID_TYPE_UID:
			    *xid = (struct wbcUnixId) {
				    .type = WBC_ID_TYPE_UID,
				    .id.uid = id->xid.id
			    };
			    break;
		    case ID_TYPE_GID:
			    *xid = (struct wbcUnixId) {
				    .type = WBC_ID_TYPE_GID,
				    .id.uid = id->xid.id
			    };
			    break;
		    default:
			    TALLOC_FREE(mem_ctx);
			    return NT_STATUS_NOT_FOUND;
		}
	}

	wb_off = winbind_env_set();
	if (wb_off) {
		(void)winbind_on();
	}

	result = wbcUnixIdsToSids(xids, count, sids);

	if (wb_off) {
		(void)winbind_off();
	}

	if (!WBC_ERROR_IS_OK(result)) {
		TALLOC_FREE(mem_ctx);
		return NT_STATUS_INTERNAL_ERROR;
	}

	for (i=0; i<count; i++) {
		struct wbcDomainSid *sid = &sids[i];
		struct wbcDomainSid null_sid = { 0 };
		struct id_map *id = &ids[i];

		if (memcmp(sid, &null_sid, sizeof(*sid)) != 0) {
			struct dom_sid domsid;
			id->status = ID_MAPPED;

			memcpy(&domsid, sid, sizeof(struct dom_sid));
			id->sid = dom_sid_dup(ids, &domsid);
			if (id->sid == NULL) {
				TALLOC_FREE(mem_ctx);
				return NT_STATUS_NO_MEMORY;
			}
		} else {
			id->status = ID_UNMAPPED;
			id->sid = NULL;
		}
	}

	TALLOC_FREE(mem_ctx);
	return NT_STATUS_OK;
}