s4:descriptor - cosmetic
[Samba/aatanasov.git] / source3 / winbindd / winbindd_idmap.c
blob1d275014ce0874618a339d4d6de15a24a83375b7
1 /*
2 Unix SMB/CIFS implementation.
4 Async helpers for blocking functions
6 Copyright (C) Volker Lendecke 2005
7 Copyright (C) Gerald Carter 2006
8 Copyright (C) Simo Sorce 2007
10 The helpers always consist of three functions:
12 * A request setup function that takes the necessary parameters together
13 with a continuation function that is to be called upon completion
15 * A private continuation function that is internal only. This is to be
16 called by the lower-level functions in do_async(). Its only task is to
17 properly call the continuation function named above.
19 * A worker function that is called inside the appropriate child process.
21 This program is free software; you can redistribute it and/or modify
22 it under the terms of the GNU General Public License as published by
23 the Free Software Foundation; either version 3 of the License, or
24 (at your option) any later version.
26 This program is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 GNU General Public License for more details.
31 You should have received a copy of the GNU General Public License
32 along with this program. If not, see <http://www.gnu.org/licenses/>.
35 #include "includes.h"
36 #include "winbindd.h"
38 #undef DBGC_CLASS
39 #define DBGC_CLASS DBGC_WINBIND
41 static struct winbindd_child static_idmap_child;
43 struct winbindd_child *idmap_child(void)
45 return &static_idmap_child;
48 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, bool success,
49 struct winbindd_response *response,
50 void *c, void *private_data)
52 void (*cont)(void *priv, bool succ, uid_t uid) =
53 (void (*)(void *, bool, uid_t))c;
55 if (!success) {
56 DEBUG(5, ("Could not trigger sid2uid\n"));
57 cont(private_data, False, 0);
58 return;
61 if (response->result != WINBINDD_OK) {
62 DEBUG(5, ("sid2uid returned an error\n"));
63 cont(private_data, False, 0);
64 return;
67 cont(private_data, True, response->data.uid);
70 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
71 void (*cont)(void *private_data, bool success, uid_t uid),
72 void *private_data)
74 struct winbindd_request request;
75 struct winbindd_domain *domain;
77 ZERO_STRUCT(request);
78 request.cmd = WINBINDD_DUAL_SID2UID;
80 domain = find_domain_from_sid(sid);
82 if (domain != NULL) {
83 DEBUG(10, ("winbindd_sid2uid_async found domain %s, "
84 "have_idmap_config = %d\n", domain->name,
85 (int)domain->have_idmap_config));
88 else {
89 DEBUG(10, ("winbindd_sid2uid_async did not find a domain for "
90 "%s\n", sid_string_dbg(sid)));
93 if ((domain != NULL) && (domain->have_idmap_config)) {
94 fstrcpy(request.domain_name, domain->name);
97 sid_to_fstring(request.data.dual_sid2id.sid, sid);
98 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2uid_recv,
99 (void *)cont, private_data);
102 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
103 struct winbindd_cli_state *state)
105 DOM_SID sid;
106 NTSTATUS result;
108 DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
109 state->request->data.dual_sid2id.sid));
111 if (!string_to_sid(&sid, state->request->data.dual_sid2id.sid)) {
112 DEBUG(1, ("Could not get convert sid %s from string\n",
113 state->request->data.dual_sid2id.sid));
114 return WINBINDD_ERROR;
117 result = idmap_sid_to_uid(state->request->domain_name, &sid,
118 &state->response->data.uid);
120 DEBUG(10, ("winbindd_dual_sid2uid: 0x%08x - %s - %u\n",
121 NT_STATUS_V(result), sid_string_dbg(&sid),
122 (unsigned int)state->response->data.uid));
124 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
127 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, bool success,
128 struct winbindd_response *response,
129 void *c, void *private_data)
131 void (*cont)(void *priv, bool succ, gid_t gid) =
132 (void (*)(void *, bool, gid_t))c;
134 if (!success) {
135 DEBUG(5, ("Could not trigger sid2gid\n"));
136 cont(private_data, False, 0);
137 return;
140 if (response->result != WINBINDD_OK) {
141 DEBUG(5, ("sid2gid returned an error\n"));
142 cont(private_data, False, 0);
143 return;
146 cont(private_data, True, response->data.gid);
149 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
150 void (*cont)(void *private_data, bool success, gid_t gid),
151 void *private_data)
153 struct winbindd_request request;
154 struct winbindd_domain *domain;
156 ZERO_STRUCT(request);
157 request.cmd = WINBINDD_DUAL_SID2GID;
159 domain = find_domain_from_sid(sid);
160 if ((domain != NULL) && (domain->have_idmap_config)) {
161 fstrcpy(request.domain_name, domain->name);
164 sid_to_fstring(request.data.dual_sid2id.sid, sid);
166 DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
167 request.data.dual_sid2id.sid));
169 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
170 (void *)cont, private_data);
173 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
174 struct winbindd_cli_state *state)
176 DOM_SID sid;
177 NTSTATUS result;
179 DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
180 state->request->data.dual_sid2id.sid));
182 if (!string_to_sid(&sid, state->request->data.dual_sid2id.sid)) {
183 DEBUG(1, ("Could not get convert sid %s from string\n",
184 state->request->data.dual_sid2id.sid));
185 return WINBINDD_ERROR;
188 /* Find gid for this sid and return it, possibly ask the slow remote idmap */
190 result = idmap_sid_to_gid(state->request->domain_name, &sid,
191 &state->response->data.gid);
193 DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n",
194 NT_STATUS_V(result), sid_string_dbg(&sid),
195 (unsigned int)state->response->data.gid));
197 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
200 /* The following uid2sid/gid2sid functions has been contributed by
201 * Keith Reynolds <Keith.Reynolds@centrify.com> */
203 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
204 struct winbindd_response *response,
205 void *c, void *private_data)
207 void (*cont)(void *priv, bool succ, const char *sid) =
208 (void (*)(void *, bool, const char *))c;
210 if (!success) {
211 DEBUG(5, ("Could not trigger uid2sid\n"));
212 cont(private_data, False, NULL);
213 return;
216 if (response->result != WINBINDD_OK) {
217 DEBUG(5, ("uid2sid returned an error\n"));
218 cont(private_data, False, NULL);
219 return;
222 cont(private_data, True, response->data.sid.sid);
225 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
226 void (*cont)(void *private_data, bool success, const char *sid),
227 void *private_data)
229 struct winbindd_domain *domain;
230 struct winbindd_request request;
232 ZERO_STRUCT(request);
233 request.cmd = WINBINDD_DUAL_UID2SID;
234 request.data.uid = uid;
236 for (domain = domain_list(); domain != NULL; domain = domain->next) {
237 if (domain->have_idmap_config
238 && (uid >= domain->id_range_low)
239 && (uid <= domain->id_range_high)) {
240 fstrcpy(request.domain_name, domain->name);
244 do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
245 (void *)cont, private_data);
248 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
249 struct winbindd_cli_state *state)
251 DOM_SID sid;
252 NTSTATUS result;
254 DEBUG(3,("[%5lu]: uid to sid %lu\n",
255 (unsigned long)state->pid,
256 (unsigned long) state->request->data.uid));
258 /* Find sid for this uid and return it, possibly ask the slow remote idmap */
259 result = idmap_uid_to_sid(state->request->domain_name, &sid,
260 state->request->data.uid);
262 if (NT_STATUS_IS_OK(result)) {
263 sid_to_fstring(state->response->data.sid.sid, &sid);
264 state->response->data.sid.type = SID_NAME_USER;
265 return WINBINDD_OK;
268 return WINBINDD_ERROR;
271 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
272 struct winbindd_response *response,
273 void *c, void *private_data)
275 void (*cont)(void *priv, bool succ, const char *sid) =
276 (void (*)(void *, bool, const char *))c;
278 if (!success) {
279 DEBUG(5, ("Could not trigger gid2sid\n"));
280 cont(private_data, False, NULL);
281 return;
284 if (response->result != WINBINDD_OK) {
285 DEBUG(5, ("gid2sid returned an error\n"));
286 cont(private_data, False, NULL);
287 return;
290 cont(private_data, True, response->data.sid.sid);
293 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
294 void (*cont)(void *private_data, bool success, const char *sid),
295 void *private_data)
297 struct winbindd_domain *domain;
298 struct winbindd_request request;
300 ZERO_STRUCT(request);
301 request.cmd = WINBINDD_DUAL_GID2SID;
302 request.data.gid = gid;
304 for (domain = domain_list(); domain != NULL; domain = domain->next) {
305 if (domain->have_idmap_config
306 && (gid >= domain->id_range_low)
307 && (gid <= domain->id_range_high)) {
308 fstrcpy(request.domain_name, domain->name);
312 do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
313 (void *)cont, private_data);
316 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
317 struct winbindd_cli_state *state)
319 DOM_SID sid;
320 NTSTATUS result;
322 DEBUG(3,("[%5lu]: gid %lu to sid\n",
323 (unsigned long)state->pid,
324 (unsigned long) state->request->data.gid));
326 /* Find sid for this gid and return it, possibly ask the slow remote idmap */
327 result = idmap_gid_to_sid(state->request->domain_name, &sid,
328 state->request->data.gid);
330 if (NT_STATUS_IS_OK(result)) {
331 sid_to_fstring(state->response->data.sid.sid, &sid);
332 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
333 (unsigned long)state->pid,
334 state->response->data.sid.sid));
335 state->response->data.sid.type = SID_NAME_DOM_GRP;
336 return WINBINDD_OK;
339 return WINBINDD_ERROR;
342 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
344 .name = "PING",
345 .struct_cmd = WINBINDD_PING,
346 .struct_fn = winbindd_dual_ping,
348 .name = "DUAL_SID2UID",
349 .struct_cmd = WINBINDD_DUAL_SID2UID,
350 .struct_fn = winbindd_dual_sid2uid,
352 .name = "DUAL_SID2GID",
353 .struct_cmd = WINBINDD_DUAL_SID2GID,
354 .struct_fn = winbindd_dual_sid2gid,
356 .name = "DUAL_UID2SID",
357 .struct_cmd = WINBINDD_DUAL_UID2SID,
358 .struct_fn = winbindd_dual_uid2sid,
360 .name = "DUAL_GID2SID",
361 .struct_cmd = WINBINDD_DUAL_GID2SID,
362 .struct_fn = winbindd_dual_gid2sid,
364 .name = "NDRCMD",
365 .struct_cmd = WINBINDD_DUAL_NDRCMD,
366 .struct_fn = winbindd_dual_ndrcmd,
368 .name = NULL,
372 void init_idmap_child(void)
374 setup_child(NULL, &static_idmap_child,
375 idmap_dispatch_table,
376 "log.winbindd", "idmap");