s3/packaging: pam_winbind has been moved to section 8.
[Samba/gebeck_regimport.git] / source3 / winbindd / winbindd_idmap.c
blob6e24a9c2127dfdc9cc1c5cbe367431d7539e191d
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 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,
48 idmap_dispatch_table,
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;
63 if (!success) {
64 DEBUG(5, ("Could not trigger idmap_set_mapping\n"));
65 cont(private_data, False);
66 return;
69 if (response->result != WINBINDD_OK) {
70 DEBUG(5, ("idmap_set_mapping returned an error\n"));
71 cont(private_data, False);
72 return;
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),
80 void *private_data)
82 struct winbindd_request request;
83 ZERO_STRUCT(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)
96 struct id_map map;
97 DOM_SID sid;
98 NTSTATUS result;
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;
105 map.sid = &sid;
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_remove_mapping_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;
120 if (!success) {
121 DEBUG(5, ("Could not trigger idmap_remove_mapping\n"));
122 cont(private_data, False);
123 return;
126 if (response->result != WINBINDD_OK) {
127 DEBUG(5, ("idmap_remove_mapping returned an error\n"));
128 cont(private_data, False);
129 return;
132 cont(private_data, True);
135 void winbindd_remove_mapping_async(TALLOC_CTX *mem_ctx,
136 const struct id_map *map,
137 void (*cont)(void *private_data, bool success),
138 void *private_data)
140 struct winbindd_request request;
141 ZERO_STRUCT(request);
142 request.cmd = WINBINDD_DUAL_REMOVE_MAPPING;
143 request.data.dual_idmapset.id = map->xid.id;
144 request.data.dual_idmapset.type = map->xid.type;
145 sid_to_fstring(request.data.dual_idmapset.sid, map->sid);
147 do_async(mem_ctx, idmap_child(), &request, winbindd_remove_mapping_recv,
148 (void *)cont, private_data);
151 enum winbindd_result winbindd_dual_remove_mapping(
152 struct winbindd_domain *domain,
153 struct winbindd_cli_state *state)
155 struct id_map map;
156 DOM_SID sid;
157 NTSTATUS result;
159 DEBUG(3, ("[%5lu]: dual_idmapremove\n", (unsigned long)state->pid));
161 if (!string_to_sid(&sid, state->request->data.dual_idmapset.sid))
162 return WINBINDD_ERROR;
164 map.sid = &sid;
165 map.xid.id = state->request->data.dual_idmapset.id;
166 map.xid.type = state->request->data.dual_idmapset.type;
167 map.status = ID_MAPPED;
169 result = idmap_remove_mapping(&map);
170 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
173 static void winbindd_set_hwm_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) = (void (*)(void *, bool))c;
179 if (!success) {
180 DEBUG(5, ("Could not trigger idmap_set_hwm\n"));
181 cont(private_data, False);
182 return;
185 if (response->result != WINBINDD_OK) {
186 DEBUG(5, ("idmap_set_hwm returned an error\n"));
187 cont(private_data, False);
188 return;
191 cont(private_data, True);
194 void winbindd_set_hwm_async(TALLOC_CTX *mem_ctx, const struct unixid *xid,
195 void (*cont)(void *private_data, bool success),
196 void *private_data)
198 struct winbindd_request request;
199 ZERO_STRUCT(request);
200 request.cmd = WINBINDD_DUAL_SET_HWM;
201 request.data.dual_idmapset.id = xid->id;
202 request.data.dual_idmapset.type = xid->type;
204 do_async(mem_ctx, idmap_child(), &request, winbindd_set_hwm_recv,
205 (void *)cont, private_data);
208 enum winbindd_result winbindd_dual_set_hwm(struct winbindd_domain *domain,
209 struct winbindd_cli_state *state)
211 struct unixid xid;
212 NTSTATUS result;
214 DEBUG(3, ("[%5lu]: dual_set_hwm\n", (unsigned long)state->pid));
216 xid.id = state->request->data.dual_idmapset.id;
217 xid.type = state->request->data.dual_idmapset.type;
219 switch (xid.type) {
220 case ID_TYPE_UID:
221 result = idmap_set_uid_hwm(&xid);
222 break;
223 case ID_TYPE_GID:
224 result = idmap_set_gid_hwm(&xid);
225 break;
226 default:
227 return WINBINDD_ERROR;
229 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
232 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, bool success,
233 struct winbindd_response *response,
234 void *c, void *private_data)
236 void (*cont)(void *priv, bool succ, uid_t uid) =
237 (void (*)(void *, bool, uid_t))c;
239 if (!success) {
240 DEBUG(5, ("Could not trigger sid2uid\n"));
241 cont(private_data, False, 0);
242 return;
245 if (response->result != WINBINDD_OK) {
246 DEBUG(5, ("sid2uid returned an error\n"));
247 cont(private_data, False, 0);
248 return;
251 cont(private_data, True, response->data.uid);
254 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
255 void (*cont)(void *private_data, bool success, uid_t uid),
256 void *private_data)
258 struct winbindd_request request;
259 struct winbindd_domain *domain;
261 ZERO_STRUCT(request);
262 request.cmd = WINBINDD_DUAL_SID2UID;
264 domain = find_domain_from_sid(sid);
266 if (domain != NULL) {
267 DEBUG(10, ("winbindd_sid2uid_async found domain %s, "
268 "have_idmap_config = %d\n", domain->name,
269 (int)domain->have_idmap_config));
272 else {
273 DEBUG(10, ("winbindd_sid2uid_async did not find a domain for "
274 "%s\n", sid_string_dbg(sid)));
277 if ((domain != NULL) && (domain->have_idmap_config)) {
278 fstrcpy(request.domain_name, domain->name);
281 sid_to_fstring(request.data.dual_sid2id.sid, sid);
282 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2uid_recv,
283 (void *)cont, private_data);
286 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
287 struct winbindd_cli_state *state)
289 DOM_SID sid;
290 NTSTATUS result;
292 DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
293 state->request->data.dual_sid2id.sid));
295 if (!string_to_sid(&sid, state->request->data.dual_sid2id.sid)) {
296 DEBUG(1, ("Could not get convert sid %s from string\n",
297 state->request->data.dual_sid2id.sid));
298 return WINBINDD_ERROR;
301 result = idmap_sid_to_uid(state->request->domain_name, &sid,
302 &state->response->data.uid);
304 DEBUG(10, ("winbindd_dual_sid2uid: 0x%08x - %s - %u\n",
305 NT_STATUS_V(result), sid_string_dbg(&sid),
306 (unsigned int)state->response->data.uid));
308 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
311 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, bool success,
312 struct winbindd_response *response,
313 void *c, void *private_data)
315 void (*cont)(void *priv, bool succ, gid_t gid) =
316 (void (*)(void *, bool, gid_t))c;
318 if (!success) {
319 DEBUG(5, ("Could not trigger sid2gid\n"));
320 cont(private_data, False, 0);
321 return;
324 if (response->result != WINBINDD_OK) {
325 DEBUG(5, ("sid2gid returned an error\n"));
326 cont(private_data, False, 0);
327 return;
330 cont(private_data, True, response->data.gid);
333 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
334 void (*cont)(void *private_data, bool success, gid_t gid),
335 void *private_data)
337 struct winbindd_request request;
338 struct winbindd_domain *domain;
340 ZERO_STRUCT(request);
341 request.cmd = WINBINDD_DUAL_SID2GID;
343 domain = find_domain_from_sid(sid);
344 if ((domain != NULL) && (domain->have_idmap_config)) {
345 fstrcpy(request.domain_name, domain->name);
348 sid_to_fstring(request.data.dual_sid2id.sid, sid);
350 DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
351 request.data.dual_sid2id.sid));
353 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
354 (void *)cont, private_data);
357 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
358 struct winbindd_cli_state *state)
360 DOM_SID sid;
361 NTSTATUS result;
363 DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
364 state->request->data.dual_sid2id.sid));
366 if (!string_to_sid(&sid, state->request->data.dual_sid2id.sid)) {
367 DEBUG(1, ("Could not get convert sid %s from string\n",
368 state->request->data.dual_sid2id.sid));
369 return WINBINDD_ERROR;
372 /* Find gid for this sid and return it, possibly ask the slow remote idmap */
374 result = idmap_sid_to_gid(state->request->domain_name, &sid,
375 &state->response->data.gid);
377 DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n",
378 NT_STATUS_V(result), sid_string_dbg(&sid),
379 (unsigned int)state->response->data.gid));
381 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
384 /* The following uid2sid/gid2sid functions has been contributed by
385 * Keith Reynolds <Keith.Reynolds@centrify.com> */
387 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
388 struct winbindd_response *response,
389 void *c, void *private_data)
391 void (*cont)(void *priv, bool succ, const char *sid) =
392 (void (*)(void *, bool, const char *))c;
394 if (!success) {
395 DEBUG(5, ("Could not trigger uid2sid\n"));
396 cont(private_data, False, NULL);
397 return;
400 if (response->result != WINBINDD_OK) {
401 DEBUG(5, ("uid2sid returned an error\n"));
402 cont(private_data, False, NULL);
403 return;
406 cont(private_data, True, response->data.sid.sid);
409 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
410 void (*cont)(void *private_data, bool success, const char *sid),
411 void *private_data)
413 struct winbindd_domain *domain;
414 struct winbindd_request request;
416 ZERO_STRUCT(request);
417 request.cmd = WINBINDD_DUAL_UID2SID;
418 request.data.uid = uid;
420 for (domain = domain_list(); domain != NULL; domain = domain->next) {
421 if (domain->have_idmap_config
422 && (uid >= domain->id_range_low)
423 && (uid <= domain->id_range_high)) {
424 fstrcpy(request.domain_name, domain->name);
428 do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
429 (void *)cont, private_data);
432 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
433 struct winbindd_cli_state *state)
435 DOM_SID sid;
436 NTSTATUS result;
438 DEBUG(3,("[%5lu]: uid to sid %lu\n",
439 (unsigned long)state->pid,
440 (unsigned long) state->request->data.uid));
442 /* Find sid for this uid and return it, possibly ask the slow remote idmap */
443 result = idmap_uid_to_sid(state->request->domain_name, &sid,
444 state->request->data.uid);
446 if (NT_STATUS_IS_OK(result)) {
447 sid_to_fstring(state->response->data.sid.sid, &sid);
448 state->response->data.sid.type = SID_NAME_USER;
449 return WINBINDD_OK;
452 return WINBINDD_ERROR;
455 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
456 struct winbindd_response *response,
457 void *c, void *private_data)
459 void (*cont)(void *priv, bool succ, const char *sid) =
460 (void (*)(void *, bool, const char *))c;
462 if (!success) {
463 DEBUG(5, ("Could not trigger gid2sid\n"));
464 cont(private_data, False, NULL);
465 return;
468 if (response->result != WINBINDD_OK) {
469 DEBUG(5, ("gid2sid returned an error\n"));
470 cont(private_data, False, NULL);
471 return;
474 cont(private_data, True, response->data.sid.sid);
477 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
478 void (*cont)(void *private_data, bool success, const char *sid),
479 void *private_data)
481 struct winbindd_domain *domain;
482 struct winbindd_request request;
484 ZERO_STRUCT(request);
485 request.cmd = WINBINDD_DUAL_GID2SID;
486 request.data.gid = gid;
488 for (domain = domain_list(); domain != NULL; domain = domain->next) {
489 if (domain->have_idmap_config
490 && (gid >= domain->id_range_low)
491 && (gid <= domain->id_range_high)) {
492 fstrcpy(request.domain_name, domain->name);
496 do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
497 (void *)cont, private_data);
500 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
501 struct winbindd_cli_state *state)
503 DOM_SID sid;
504 NTSTATUS result;
506 DEBUG(3,("[%5lu]: gid %lu to sid\n",
507 (unsigned long)state->pid,
508 (unsigned long) state->request->data.gid));
510 /* Find sid for this gid and return it, possibly ask the slow remote idmap */
511 result = idmap_gid_to_sid(state->request->domain_name, &sid,
512 state->request->data.gid);
514 if (NT_STATUS_IS_OK(result)) {
515 sid_to_fstring(state->response->data.sid.sid, &sid);
516 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
517 (unsigned long)state->pid,
518 state->response->data.sid.sid));
519 state->response->data.sid.type = SID_NAME_DOM_GRP;
520 return WINBINDD_OK;
523 return WINBINDD_ERROR;
526 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
528 .name = "PING",
529 .struct_cmd = WINBINDD_PING,
530 .struct_fn = winbindd_dual_ping,
532 .name = "DUAL_SID2UID",
533 .struct_cmd = WINBINDD_DUAL_SID2UID,
534 .struct_fn = winbindd_dual_sid2uid,
536 .name = "DUAL_SID2GID",
537 .struct_cmd = WINBINDD_DUAL_SID2GID,
538 .struct_fn = winbindd_dual_sid2gid,
540 .name = "DUAL_UID2SID",
541 .struct_cmd = WINBINDD_DUAL_UID2SID,
542 .struct_fn = winbindd_dual_uid2sid,
544 .name = "DUAL_GID2SID",
545 .struct_cmd = WINBINDD_DUAL_GID2SID,
546 .struct_fn = winbindd_dual_gid2sid,
548 .name = "DUAL_SET_MAPPING",
549 .struct_cmd = WINBINDD_DUAL_SET_MAPPING,
550 .struct_fn = winbindd_dual_set_mapping,
552 .name = "DUAL_REMOVE_MAPPING",
553 .struct_cmd = WINBINDD_DUAL_REMOVE_MAPPING,
554 .struct_fn = winbindd_dual_remove_mapping,
556 .name = "DUAL_SET_HWMS",
557 .struct_cmd = WINBINDD_DUAL_SET_HWM,
558 .struct_fn = winbindd_dual_set_hwm,
560 .name = "ALLOCATE_UID",
561 .struct_cmd = WINBINDD_ALLOCATE_UID,
562 .struct_fn = winbindd_dual_allocate_uid,
564 .name = "ALLOCATE_GID",
565 .struct_cmd = WINBINDD_ALLOCATE_GID,
566 .struct_fn = winbindd_dual_allocate_gid,
568 .name = NULL,