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 const struct winbindd_child_dispatch_table idmap_dispatch_table[];
43 static struct winbindd_child static_idmap_child;
45 void init_idmap_child(void)
47 setup_child(&static_idmap_child,
49 "log.winbindd", "idmap");
52 struct winbindd_child *idmap_child(void)
54 return &static_idmap_child;
57 static void winbindd_set_mapping_recv(TALLOC_CTX *mem_ctx, bool success,
58 struct winbindd_response *response,
59 void *c, void *private_data)
61 void (*cont)(void *priv, bool succ) = (void (*)(void *, bool))c;
64 DEBUG(5, ("Could not trigger idmap_set_mapping\n"));
65 cont(private_data, False);
69 if (response->result != WINBINDD_OK) {
70 DEBUG(5, ("idmap_set_mapping returned an error\n"));
71 cont(private_data, False);
75 cont(private_data, True);
78 void winbindd_set_mapping_async(TALLOC_CTX *mem_ctx, const struct id_map *map,
79 void (*cont)(void *private_data, bool success),
82 struct winbindd_request request;
84 request.cmd = WINBINDD_DUAL_SET_MAPPING;
85 request.data.dual_idmapset.id = map->xid.id;
86 request.data.dual_idmapset.type = map->xid.type;
87 sid_to_fstring(request.data.dual_idmapset.sid, map->sid);
89 do_async(mem_ctx, idmap_child(), &request, winbindd_set_mapping_recv,
90 (void *)cont, private_data);
93 enum winbindd_result winbindd_dual_set_mapping(struct winbindd_domain *domain,
94 struct winbindd_cli_state *state)
100 DEBUG(3, ("[%5lu]: dual_idmapset\n", (unsigned long)state->pid));
102 if (!string_to_sid(&sid, state->request.data.dual_idmapset.sid))
103 return WINBINDD_ERROR;
106 map.xid.id = state->request.data.dual_idmapset.id;
107 map.xid.type = state->request.data.dual_idmapset.type;
108 map.status = ID_MAPPED;
110 result = idmap_set_mapping(&map);
111 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
114 static void winbindd_set_hwm_recv(TALLOC_CTX *mem_ctx, bool success,
115 struct winbindd_response *response,
116 void *c, void *private_data)
118 void (*cont)(void *priv, bool succ) = (void (*)(void *, bool))c;
121 DEBUG(5, ("Could not trigger idmap_set_hwm\n"));
122 cont(private_data, False);
126 if (response->result != WINBINDD_OK) {
127 DEBUG(5, ("idmap_set_hwm returned an error\n"));
128 cont(private_data, False);
132 cont(private_data, True);
135 void winbindd_set_hwm_async(TALLOC_CTX *mem_ctx, const struct unixid *xid,
136 void (*cont)(void *private_data, bool success),
139 struct winbindd_request request;
140 ZERO_STRUCT(request);
141 request.cmd = WINBINDD_DUAL_SET_HWM;
142 request.data.dual_idmapset.id = xid->id;
143 request.data.dual_idmapset.type = xid->type;
145 do_async(mem_ctx, idmap_child(), &request, winbindd_set_hwm_recv,
146 (void *)cont, private_data);
149 enum winbindd_result winbindd_dual_set_hwm(struct winbindd_domain *domain,
150 struct winbindd_cli_state *state)
155 DEBUG(3, ("[%5lu]: dual_set_hwm\n", (unsigned long)state->pid));
157 xid.id = state->request.data.dual_idmapset.id;
158 xid.type = state->request.data.dual_idmapset.type;
162 result = idmap_set_uid_hwm(&xid);
165 result = idmap_set_gid_hwm(&xid);
168 return WINBINDD_ERROR;
170 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
173 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, bool success,
174 struct winbindd_response *response,
175 void *c, void *private_data)
177 void (*cont)(void *priv, bool succ, uid_t uid) =
178 (void (*)(void *, bool, uid_t))c;
181 DEBUG(5, ("Could not trigger sid2uid\n"));
182 cont(private_data, False, 0);
186 if (response->result != WINBINDD_OK) {
187 DEBUG(5, ("sid2uid returned an error\n"));
188 cont(private_data, False, 0);
192 cont(private_data, True, response->data.uid);
195 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
196 void (*cont)(void *private_data, bool success, uid_t uid),
199 struct winbindd_request request;
200 ZERO_STRUCT(request);
201 request.cmd = WINBINDD_DUAL_SID2UID;
202 sid_to_fstring(request.data.dual_sid2id.sid, sid);
203 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2uid_recv,
204 (void *)cont, private_data);
207 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
208 struct winbindd_cli_state *state)
213 DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
214 state->request.data.dual_sid2id.sid));
216 if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
217 DEBUG(1, ("Could not get convert sid %s from string\n",
218 state->request.data.dual_sid2id.sid));
219 return WINBINDD_ERROR;
222 /* Find uid for this sid and return it, possibly ask the slow remote idmap */
224 result = idmap_sid_to_uid(&sid, &(state->response.data.uid));
226 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
229 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, bool success,
230 struct winbindd_response *response,
231 void *c, void *private_data)
233 void (*cont)(void *priv, bool succ, gid_t gid) =
234 (void (*)(void *, bool, gid_t))c;
237 DEBUG(5, ("Could not trigger sid2gid\n"));
238 cont(private_data, False, 0);
242 if (response->result != WINBINDD_OK) {
243 DEBUG(5, ("sid2gid returned an error\n"));
244 cont(private_data, False, 0);
248 cont(private_data, True, response->data.gid);
251 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
252 void (*cont)(void *private_data, bool success, gid_t gid),
255 struct winbindd_request request;
256 ZERO_STRUCT(request);
257 request.cmd = WINBINDD_DUAL_SID2GID;
258 sid_to_fstring(request.data.dual_sid2id.sid, sid);
260 DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
261 request.data.dual_sid2id.sid));
263 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
264 (void *)cont, private_data);
267 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
268 struct winbindd_cli_state *state)
273 DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
274 state->request.data.dual_sid2id.sid));
276 if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
277 DEBUG(1, ("Could not get convert sid %s from string\n",
278 state->request.data.dual_sid2id.sid));
279 return WINBINDD_ERROR;
282 /* Find gid for this sid and return it, possibly ask the slow remote idmap */
284 result = idmap_sid_to_gid(&sid, &state->response.data.gid);
286 DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n",
287 NT_STATUS_V(result), sid_string_dbg(&sid),
288 state->response.data.gid));
290 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
293 /* The following uid2sid/gid2sid functions has been contributed by
294 * Keith Reynolds <Keith.Reynolds@centrify.com> */
296 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
297 struct winbindd_response *response,
298 void *c, void *private_data)
300 void (*cont)(void *priv, bool succ, const char *sid) =
301 (void (*)(void *, bool, const char *))c;
304 DEBUG(5, ("Could not trigger uid2sid\n"));
305 cont(private_data, False, NULL);
309 if (response->result != WINBINDD_OK) {
310 DEBUG(5, ("uid2sid returned an error\n"));
311 cont(private_data, False, NULL);
315 cont(private_data, True, response->data.sid.sid);
318 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
319 void (*cont)(void *private_data, bool success, const char *sid),
322 struct winbindd_request request;
324 ZERO_STRUCT(request);
325 request.cmd = WINBINDD_DUAL_UID2SID;
326 request.data.uid = uid;
327 do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
328 (void *)cont, private_data);
331 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
332 struct winbindd_cli_state *state)
337 DEBUG(3,("[%5lu]: uid to sid %lu\n",
338 (unsigned long)state->pid,
339 (unsigned long) state->request.data.uid));
341 /* Find sid for this uid and return it, possibly ask the slow remote idmap */
342 result = idmap_uid_to_sid(&sid, state->request.data.uid);
344 if (NT_STATUS_IS_OK(result)) {
345 sid_to_fstring(state->response.data.sid.sid, &sid);
346 state->response.data.sid.type = SID_NAME_USER;
350 return WINBINDD_ERROR;
353 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
354 struct winbindd_response *response,
355 void *c, void *private_data)
357 void (*cont)(void *priv, bool succ, const char *sid) =
358 (void (*)(void *, bool, const char *))c;
361 DEBUG(5, ("Could not trigger gid2sid\n"));
362 cont(private_data, False, NULL);
366 if (response->result != WINBINDD_OK) {
367 DEBUG(5, ("gid2sid returned an error\n"));
368 cont(private_data, False, NULL);
372 cont(private_data, True, response->data.sid.sid);
375 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
376 void (*cont)(void *private_data, bool success, const char *sid),
379 struct winbindd_request request;
381 ZERO_STRUCT(request);
382 request.cmd = WINBINDD_DUAL_GID2SID;
383 request.data.gid = gid;
384 do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
385 (void *)cont, private_data);
388 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
389 struct winbindd_cli_state *state)
394 DEBUG(3,("[%5lu]: gid %lu to sid\n",
395 (unsigned long)state->pid,
396 (unsigned long) state->request.data.gid));
398 /* Find sid for this gid and return it, possibly ask the slow remote idmap */
399 result = idmap_gid_to_sid(&sid, state->request.data.gid);
401 if (NT_STATUS_IS_OK(result)) {
402 sid_to_fstring(state->response.data.sid.sid, &sid);
403 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
404 (unsigned long)state->pid,
405 state->response.data.sid.sid));
406 state->response.data.sid.type = SID_NAME_DOM_GRP;
410 return WINBINDD_ERROR;
413 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
415 .name = "DUAL_SID2UID",
416 .struct_cmd = WINBINDD_DUAL_SID2UID,
417 .struct_fn = winbindd_dual_sid2uid,
419 .name = "DUAL_SID2GID",
420 .struct_cmd = WINBINDD_DUAL_SID2GID,
421 .struct_fn = winbindd_dual_sid2gid,
423 .name = "DUAL_UID2SID",
424 .struct_cmd = WINBINDD_DUAL_UID2SID,
425 .struct_fn = winbindd_dual_uid2sid,
427 .name = "DUAL_GID2SID",
428 .struct_cmd = WINBINDD_DUAL_GID2SID,
429 .struct_fn = winbindd_dual_gid2sid,
431 .name = "DUAL_SET_MAPPING",
432 .struct_cmd = WINBINDD_DUAL_SET_MAPPING,
433 .struct_fn = winbindd_dual_set_mapping,
435 .name = "DUAL_SET_HWMS",
436 .struct_cmd = WINBINDD_DUAL_SET_HWM,
437 .struct_fn = winbindd_dual_set_hwm,
439 .name = "ALLOCATE_UID",
440 .struct_cmd = WINBINDD_ALLOCATE_UID,
441 .struct_fn = winbindd_dual_allocate_uid,
443 .name = "ALLOCATE_GID",
444 .struct_cmd = WINBINDD_ALLOCATE_GID,
445 .struct_fn = winbindd_dual_allocate_gid,