WHATSNEW: Update changes since 3.3.0pre2.
[Samba.git] / source / winbindd / winbindd_idmap.c
blobd8c67dc21c7cff49f29cc90c14bf29aa23a99916
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_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;
120 if (!success) {
121 DEBUG(5, ("Could not trigger idmap_set_hwm\n"));
122 cont(private_data, False);
123 return;
126 if (response->result != WINBINDD_OK) {
127 DEBUG(5, ("idmap_set_hwm returned an error\n"));
128 cont(private_data, False);
129 return;
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),
137 void *private_data)
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)
152 struct unixid xid;
153 NTSTATUS result;
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;
160 switch (xid.type) {
161 case ID_TYPE_UID:
162 result = idmap_set_uid_hwm(&xid);
163 break;
164 case ID_TYPE_GID:
165 result = idmap_set_gid_hwm(&xid);
166 break;
167 default:
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;
180 if (!success) {
181 DEBUG(5, ("Could not trigger sid2uid\n"));
182 cont(private_data, False, 0);
183 return;
186 if (response->result != WINBINDD_OK) {
187 DEBUG(5, ("sid2uid returned an error\n"));
188 cont(private_data, False, 0);
189 return;
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),
197 void *private_data)
199 struct winbindd_request request;
200 struct winbindd_domain *domain;
202 ZERO_STRUCT(request);
203 request.cmd = WINBINDD_DUAL_SID2UID;
205 domain = find_domain_from_sid(sid);
207 if (domain != NULL) {
208 DEBUG(10, ("winbindd_sid2uid_async found domain %s, "
209 "have_idmap_config = %d\n", domain->name,
210 (int)domain->have_idmap_config));
213 else {
214 DEBUG(10, ("winbindd_sid2uid_async did not find a domain for "
215 "%s\n", sid_string_dbg(sid)));
218 if ((domain != NULL) && (domain->have_idmap_config)) {
219 fstrcpy(request.domain_name, domain->name);
222 sid_to_fstring(request.data.dual_sid2id.sid, sid);
223 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2uid_recv,
224 (void *)cont, private_data);
227 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
228 struct winbindd_cli_state *state)
230 DOM_SID sid;
231 NTSTATUS result;
233 DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
234 state->request.data.dual_sid2id.sid));
236 if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
237 DEBUG(1, ("Could not get convert sid %s from string\n",
238 state->request.data.dual_sid2id.sid));
239 return WINBINDD_ERROR;
242 result = idmap_sid_to_uid(state->request.domain_name, &sid,
243 &state->response.data.uid);
245 DEBUG(10, ("winbindd_dual_sid2uid: 0x%08x - %s - %u\n",
246 NT_STATUS_V(result), sid_string_dbg(&sid),
247 state->response.data.uid));
249 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
252 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, bool success,
253 struct winbindd_response *response,
254 void *c, void *private_data)
256 void (*cont)(void *priv, bool succ, gid_t gid) =
257 (void (*)(void *, bool, gid_t))c;
259 if (!success) {
260 DEBUG(5, ("Could not trigger sid2gid\n"));
261 cont(private_data, False, 0);
262 return;
265 if (response->result != WINBINDD_OK) {
266 DEBUG(5, ("sid2gid returned an error\n"));
267 cont(private_data, False, 0);
268 return;
271 cont(private_data, True, response->data.gid);
274 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
275 void (*cont)(void *private_data, bool success, gid_t gid),
276 void *private_data)
278 struct winbindd_request request;
279 struct winbindd_domain *domain;
281 ZERO_STRUCT(request);
282 request.cmd = WINBINDD_DUAL_SID2GID;
284 domain = find_domain_from_sid(sid);
285 if ((domain != NULL) && (domain->have_idmap_config)) {
286 fstrcpy(request.domain_name, domain->name);
289 sid_to_fstring(request.data.dual_sid2id.sid, sid);
291 DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
292 request.data.dual_sid2id.sid));
294 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
295 (void *)cont, private_data);
298 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
299 struct winbindd_cli_state *state)
301 DOM_SID sid;
302 NTSTATUS result;
304 DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
305 state->request.data.dual_sid2id.sid));
307 if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
308 DEBUG(1, ("Could not get convert sid %s from string\n",
309 state->request.data.dual_sid2id.sid));
310 return WINBINDD_ERROR;
313 /* Find gid for this sid and return it, possibly ask the slow remote idmap */
315 result = idmap_sid_to_gid(state->request.domain_name, &sid,
316 &state->response.data.gid);
318 DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n",
319 NT_STATUS_V(result), sid_string_dbg(&sid),
320 state->response.data.gid));
322 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
325 /* The following uid2sid/gid2sid functions has been contributed by
326 * Keith Reynolds <Keith.Reynolds@centrify.com> */
328 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
329 struct winbindd_response *response,
330 void *c, void *private_data)
332 void (*cont)(void *priv, bool succ, const char *sid) =
333 (void (*)(void *, bool, const char *))c;
335 if (!success) {
336 DEBUG(5, ("Could not trigger uid2sid\n"));
337 cont(private_data, False, NULL);
338 return;
341 if (response->result != WINBINDD_OK) {
342 DEBUG(5, ("uid2sid returned an error\n"));
343 cont(private_data, False, NULL);
344 return;
347 cont(private_data, True, response->data.sid.sid);
350 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
351 void (*cont)(void *private_data, bool success, const char *sid),
352 void *private_data)
354 struct winbindd_domain *domain;
355 struct winbindd_request request;
357 ZERO_STRUCT(request);
358 request.cmd = WINBINDD_DUAL_UID2SID;
359 request.data.uid = uid;
361 for (domain = domain_list(); domain != NULL; domain = domain->next) {
362 if (domain->have_idmap_config
363 && (uid >= domain->id_range_low)
364 && (uid <= domain->id_range_high)) {
365 fstrcpy(request.domain_name, domain->name);
369 do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
370 (void *)cont, private_data);
373 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
374 struct winbindd_cli_state *state)
376 DOM_SID sid;
377 NTSTATUS result;
379 DEBUG(3,("[%5lu]: uid to sid %lu\n",
380 (unsigned long)state->pid,
381 (unsigned long) state->request.data.uid));
383 /* Find sid for this uid and return it, possibly ask the slow remote idmap */
384 result = idmap_uid_to_sid(state->request.domain_name, &sid,
385 state->request.data.uid);
387 if (NT_STATUS_IS_OK(result)) {
388 sid_to_fstring(state->response.data.sid.sid, &sid);
389 state->response.data.sid.type = SID_NAME_USER;
390 return WINBINDD_OK;
393 return WINBINDD_ERROR;
396 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
397 struct winbindd_response *response,
398 void *c, void *private_data)
400 void (*cont)(void *priv, bool succ, const char *sid) =
401 (void (*)(void *, bool, const char *))c;
403 if (!success) {
404 DEBUG(5, ("Could not trigger gid2sid\n"));
405 cont(private_data, False, NULL);
406 return;
409 if (response->result != WINBINDD_OK) {
410 DEBUG(5, ("gid2sid returned an error\n"));
411 cont(private_data, False, NULL);
412 return;
415 cont(private_data, True, response->data.sid.sid);
418 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
419 void (*cont)(void *private_data, bool success, const char *sid),
420 void *private_data)
422 struct winbindd_domain *domain;
423 struct winbindd_request request;
425 ZERO_STRUCT(request);
426 request.cmd = WINBINDD_DUAL_GID2SID;
427 request.data.gid = gid;
429 for (domain = domain_list(); domain != NULL; domain = domain->next) {
430 if (domain->have_idmap_config
431 && (gid >= domain->id_range_low)
432 && (gid <= domain->id_range_high)) {
433 fstrcpy(request.domain_name, domain->name);
437 do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
438 (void *)cont, private_data);
441 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
442 struct winbindd_cli_state *state)
444 DOM_SID sid;
445 NTSTATUS result;
447 DEBUG(3,("[%5lu]: gid %lu to sid\n",
448 (unsigned long)state->pid,
449 (unsigned long) state->request.data.gid));
451 /* Find sid for this gid and return it, possibly ask the slow remote idmap */
452 result = idmap_gid_to_sid(state->request.domain_name, &sid,
453 state->request.data.gid);
455 if (NT_STATUS_IS_OK(result)) {
456 sid_to_fstring(state->response.data.sid.sid, &sid);
457 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
458 (unsigned long)state->pid,
459 state->response.data.sid.sid));
460 state->response.data.sid.type = SID_NAME_DOM_GRP;
461 return WINBINDD_OK;
464 return WINBINDD_ERROR;
467 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
469 .name = "DUAL_SID2UID",
470 .struct_cmd = WINBINDD_DUAL_SID2UID,
471 .struct_fn = winbindd_dual_sid2uid,
473 .name = "DUAL_SID2GID",
474 .struct_cmd = WINBINDD_DUAL_SID2GID,
475 .struct_fn = winbindd_dual_sid2gid,
477 .name = "DUAL_UID2SID",
478 .struct_cmd = WINBINDD_DUAL_UID2SID,
479 .struct_fn = winbindd_dual_uid2sid,
481 .name = "DUAL_GID2SID",
482 .struct_cmd = WINBINDD_DUAL_GID2SID,
483 .struct_fn = winbindd_dual_gid2sid,
485 .name = "DUAL_SET_MAPPING",
486 .struct_cmd = WINBINDD_DUAL_SET_MAPPING,
487 .struct_fn = winbindd_dual_set_mapping,
489 .name = "DUAL_SET_HWMS",
490 .struct_cmd = WINBINDD_DUAL_SET_HWM,
491 .struct_fn = winbindd_dual_set_hwm,
493 .name = "ALLOCATE_UID",
494 .struct_cmd = WINBINDD_ALLOCATE_UID,
495 .struct_fn = winbindd_dual_allocate_uid,
497 .name = "ALLOCATE_GID",
498 .struct_cmd = WINBINDD_ALLOCATE_GID,
499 .struct_fn = winbindd_dual_allocate_gid,
501 .name = NULL,