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/>.
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_remove_mapping_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) = (void (*)(void *, bool))c;
55 DEBUG(5, ("Could not trigger idmap_remove_mapping\n"));
56 cont(private_data, False);
60 if (response->result != WINBINDD_OK) {
61 DEBUG(5, ("idmap_remove_mapping returned an error\n"));
62 cont(private_data, False);
66 cont(private_data, True);
69 void winbindd_remove_mapping_async(TALLOC_CTX *mem_ctx,
70 const struct id_map *map,
71 void (*cont)(void *private_data, bool success),
74 struct winbindd_request request;
76 request.cmd = WINBINDD_DUAL_REMOVE_MAPPING;
77 request.data.dual_idmapset.id = map->xid.id;
78 request.data.dual_idmapset.type = map->xid.type;
79 sid_to_fstring(request.data.dual_idmapset.sid, map->sid);
81 do_async(mem_ctx, idmap_child(), &request, winbindd_remove_mapping_recv,
82 (void *)cont, private_data);
85 enum winbindd_result winbindd_dual_remove_mapping(
86 struct winbindd_domain *domain,
87 struct winbindd_cli_state *state)
93 DEBUG(3, ("[%5lu]: dual_idmapremove\n", (unsigned long)state->pid));
95 if (!string_to_sid(&sid, state->request->data.dual_idmapset.sid))
96 return WINBINDD_ERROR;
99 map.xid.id = state->request->data.dual_idmapset.id;
100 map.xid.type = state->request->data.dual_idmapset.type;
101 map.status = ID_MAPPED;
103 result = idmap_remove_mapping(&map);
104 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
107 static void winbindd_set_hwm_recv(TALLOC_CTX *mem_ctx, bool success,
108 struct winbindd_response *response,
109 void *c, void *private_data)
111 void (*cont)(void *priv, bool succ) = (void (*)(void *, bool))c;
114 DEBUG(5, ("Could not trigger idmap_set_hwm\n"));
115 cont(private_data, False);
119 if (response->result != WINBINDD_OK) {
120 DEBUG(5, ("idmap_set_hwm returned an error\n"));
121 cont(private_data, False);
125 cont(private_data, True);
128 void winbindd_set_hwm_async(TALLOC_CTX *mem_ctx, const struct unixid *xid,
129 void (*cont)(void *private_data, bool success),
132 struct winbindd_request request;
133 ZERO_STRUCT(request);
134 request.cmd = WINBINDD_DUAL_SET_HWM;
135 request.data.dual_idmapset.id = xid->id;
136 request.data.dual_idmapset.type = xid->type;
138 do_async(mem_ctx, idmap_child(), &request, winbindd_set_hwm_recv,
139 (void *)cont, private_data);
142 enum winbindd_result winbindd_dual_set_hwm(struct winbindd_domain *domain,
143 struct winbindd_cli_state *state)
148 DEBUG(3, ("[%5lu]: dual_set_hwm\n", (unsigned long)state->pid));
150 xid.id = state->request->data.dual_idmapset.id;
151 xid.type = state->request->data.dual_idmapset.type;
155 result = idmap_set_uid_hwm(&xid);
158 result = idmap_set_gid_hwm(&xid);
161 return WINBINDD_ERROR;
163 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
166 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, bool success,
167 struct winbindd_response *response,
168 void *c, void *private_data)
170 void (*cont)(void *priv, bool succ, uid_t uid) =
171 (void (*)(void *, bool, uid_t))c;
174 DEBUG(5, ("Could not trigger sid2uid\n"));
175 cont(private_data, False, 0);
179 if (response->result != WINBINDD_OK) {
180 DEBUG(5, ("sid2uid returned an error\n"));
181 cont(private_data, False, 0);
185 cont(private_data, True, response->data.uid);
188 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
189 void (*cont)(void *private_data, bool success, uid_t uid),
192 struct winbindd_request request;
193 struct winbindd_domain *domain;
195 ZERO_STRUCT(request);
196 request.cmd = WINBINDD_DUAL_SID2UID;
198 domain = find_domain_from_sid(sid);
200 if (domain != NULL) {
201 DEBUG(10, ("winbindd_sid2uid_async found domain %s, "
202 "have_idmap_config = %d\n", domain->name,
203 (int)domain->have_idmap_config));
207 DEBUG(10, ("winbindd_sid2uid_async did not find a domain for "
208 "%s\n", sid_string_dbg(sid)));
211 if ((domain != NULL) && (domain->have_idmap_config)) {
212 fstrcpy(request.domain_name, domain->name);
215 sid_to_fstring(request.data.dual_sid2id.sid, sid);
216 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2uid_recv,
217 (void *)cont, private_data);
220 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
221 struct winbindd_cli_state *state)
226 DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
227 state->request->data.dual_sid2id.sid));
229 if (!string_to_sid(&sid, state->request->data.dual_sid2id.sid)) {
230 DEBUG(1, ("Could not get convert sid %s from string\n",
231 state->request->data.dual_sid2id.sid));
232 return WINBINDD_ERROR;
235 result = idmap_sid_to_uid(state->request->domain_name, &sid,
236 &state->response->data.uid);
238 DEBUG(10, ("winbindd_dual_sid2uid: 0x%08x - %s - %u\n",
239 NT_STATUS_V(result), sid_string_dbg(&sid),
240 (unsigned int)state->response->data.uid));
242 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
245 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, bool success,
246 struct winbindd_response *response,
247 void *c, void *private_data)
249 void (*cont)(void *priv, bool succ, gid_t gid) =
250 (void (*)(void *, bool, gid_t))c;
253 DEBUG(5, ("Could not trigger sid2gid\n"));
254 cont(private_data, False, 0);
258 if (response->result != WINBINDD_OK) {
259 DEBUG(5, ("sid2gid returned an error\n"));
260 cont(private_data, False, 0);
264 cont(private_data, True, response->data.gid);
267 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
268 void (*cont)(void *private_data, bool success, gid_t gid),
271 struct winbindd_request request;
272 struct winbindd_domain *domain;
274 ZERO_STRUCT(request);
275 request.cmd = WINBINDD_DUAL_SID2GID;
277 domain = find_domain_from_sid(sid);
278 if ((domain != NULL) && (domain->have_idmap_config)) {
279 fstrcpy(request.domain_name, domain->name);
282 sid_to_fstring(request.data.dual_sid2id.sid, sid);
284 DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
285 request.data.dual_sid2id.sid));
287 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
288 (void *)cont, private_data);
291 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
292 struct winbindd_cli_state *state)
297 DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
298 state->request->data.dual_sid2id.sid));
300 if (!string_to_sid(&sid, state->request->data.dual_sid2id.sid)) {
301 DEBUG(1, ("Could not get convert sid %s from string\n",
302 state->request->data.dual_sid2id.sid));
303 return WINBINDD_ERROR;
306 /* Find gid for this sid and return it, possibly ask the slow remote idmap */
308 result = idmap_sid_to_gid(state->request->domain_name, &sid,
309 &state->response->data.gid);
311 DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n",
312 NT_STATUS_V(result), sid_string_dbg(&sid),
313 (unsigned int)state->response->data.gid));
315 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
318 /* The following uid2sid/gid2sid functions has been contributed by
319 * Keith Reynolds <Keith.Reynolds@centrify.com> */
321 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
322 struct winbindd_response *response,
323 void *c, void *private_data)
325 void (*cont)(void *priv, bool succ, const char *sid) =
326 (void (*)(void *, bool, const char *))c;
329 DEBUG(5, ("Could not trigger uid2sid\n"));
330 cont(private_data, False, NULL);
334 if (response->result != WINBINDD_OK) {
335 DEBUG(5, ("uid2sid returned an error\n"));
336 cont(private_data, False, NULL);
340 cont(private_data, True, response->data.sid.sid);
343 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
344 void (*cont)(void *private_data, bool success, const char *sid),
347 struct winbindd_domain *domain;
348 struct winbindd_request request;
350 ZERO_STRUCT(request);
351 request.cmd = WINBINDD_DUAL_UID2SID;
352 request.data.uid = uid;
354 for (domain = domain_list(); domain != NULL; domain = domain->next) {
355 if (domain->have_idmap_config
356 && (uid >= domain->id_range_low)
357 && (uid <= domain->id_range_high)) {
358 fstrcpy(request.domain_name, domain->name);
362 do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
363 (void *)cont, private_data);
366 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
367 struct winbindd_cli_state *state)
372 DEBUG(3,("[%5lu]: uid to sid %lu\n",
373 (unsigned long)state->pid,
374 (unsigned long) state->request->data.uid));
376 /* Find sid for this uid and return it, possibly ask the slow remote idmap */
377 result = idmap_uid_to_sid(state->request->domain_name, &sid,
378 state->request->data.uid);
380 if (NT_STATUS_IS_OK(result)) {
381 sid_to_fstring(state->response->data.sid.sid, &sid);
382 state->response->data.sid.type = SID_NAME_USER;
386 return WINBINDD_ERROR;
389 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
390 struct winbindd_response *response,
391 void *c, void *private_data)
393 void (*cont)(void *priv, bool succ, const char *sid) =
394 (void (*)(void *, bool, const char *))c;
397 DEBUG(5, ("Could not trigger gid2sid\n"));
398 cont(private_data, False, NULL);
402 if (response->result != WINBINDD_OK) {
403 DEBUG(5, ("gid2sid returned an error\n"));
404 cont(private_data, False, NULL);
408 cont(private_data, True, response->data.sid.sid);
411 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
412 void (*cont)(void *private_data, bool success, const char *sid),
415 struct winbindd_domain *domain;
416 struct winbindd_request request;
418 ZERO_STRUCT(request);
419 request.cmd = WINBINDD_DUAL_GID2SID;
420 request.data.gid = gid;
422 for (domain = domain_list(); domain != NULL; domain = domain->next) {
423 if (domain->have_idmap_config
424 && (gid >= domain->id_range_low)
425 && (gid <= domain->id_range_high)) {
426 fstrcpy(request.domain_name, domain->name);
430 do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
431 (void *)cont, private_data);
434 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
435 struct winbindd_cli_state *state)
440 DEBUG(3,("[%5lu]: gid %lu to sid\n",
441 (unsigned long)state->pid,
442 (unsigned long) state->request->data.gid));
444 /* Find sid for this gid and return it, possibly ask the slow remote idmap */
445 result = idmap_gid_to_sid(state->request->domain_name, &sid,
446 state->request->data.gid);
448 if (NT_STATUS_IS_OK(result)) {
449 sid_to_fstring(state->response->data.sid.sid, &sid);
450 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
451 (unsigned long)state->pid,
452 state->response->data.sid.sid));
453 state->response->data.sid.type = SID_NAME_DOM_GRP;
457 return WINBINDD_ERROR;
460 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
463 .struct_cmd = WINBINDD_PING,
464 .struct_fn = winbindd_dual_ping,
466 .name = "DUAL_SID2UID",
467 .struct_cmd = WINBINDD_DUAL_SID2UID,
468 .struct_fn = winbindd_dual_sid2uid,
470 .name = "DUAL_SID2GID",
471 .struct_cmd = WINBINDD_DUAL_SID2GID,
472 .struct_fn = winbindd_dual_sid2gid,
474 .name = "DUAL_UID2SID",
475 .struct_cmd = WINBINDD_DUAL_UID2SID,
476 .struct_fn = winbindd_dual_uid2sid,
478 .name = "DUAL_GID2SID",
479 .struct_cmd = WINBINDD_DUAL_GID2SID,
480 .struct_fn = winbindd_dual_gid2sid,
482 .name = "DUAL_REMOVE_MAPPING",
483 .struct_cmd = WINBINDD_DUAL_REMOVE_MAPPING,
484 .struct_fn = winbindd_dual_remove_mapping,
486 .name = "DUAL_SET_HWMS",
487 .struct_cmd = WINBINDD_DUAL_SET_HWM,
488 .struct_fn = winbindd_dual_set_hwm,
491 .struct_cmd = WINBINDD_DUAL_NDRCMD,
492 .struct_fn = winbindd_dual_ndrcmd,
498 void init_idmap_child(void)
500 setup_child(NULL, &static_idmap_child,
501 idmap_dispatch_table,
502 "log.winbindd", "idmap");