Revert "Tiny logic simplification -- remove an else branch"
[Samba/gebeck_regimport.git] / source3 / winbindd / winbindd_idmap.c
blob98f8548083dd2e19cce7b2c6e77bf9b2b87c978c
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_sids2xids_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 *, int) =
178 (void (*)(void *, bool, void *, int))c;
180 if (!success) {
181 DEBUG(5, ("Could not trigger sids2xids\n"));
182 cont(private_data, False, NULL, 0);
183 return;
186 if (response->result != WINBINDD_OK) {
187 DEBUG(5, ("sids2xids returned an error\n"));
188 cont(private_data, False, NULL, 0);
189 return;
192 cont(private_data, True, response->extra_data.data, response->length - sizeof(response));
195 void winbindd_sids2xids_async(TALLOC_CTX *mem_ctx, void *sids, int size,
196 void (*cont)(void *private_data, bool success, void *data, int len),
197 void *private_data)
199 struct winbindd_request request;
200 ZERO_STRUCT(request);
201 request.cmd = WINBINDD_DUAL_SIDS2XIDS;
202 request.extra_data.data = (char *)sids;
203 request.extra_len = size;
204 do_async(mem_ctx, idmap_child(), &request, winbindd_sids2xids_recv,
205 (void *)cont, private_data);
208 enum winbindd_result winbindd_dual_sids2xids(struct winbindd_domain *domain,
209 struct winbindd_cli_state *state)
211 DOM_SID *sids;
212 struct unixid *xids;
213 struct id_map **ids;
214 NTSTATUS result;
215 int num, i;
217 DEBUG(3, ("[%5lu]: sids to unix ids\n", (unsigned long)state->pid));
219 if (state->request.extra_len == 0) {
220 DEBUG(0, ("Invalid buffer size!\n"));
221 return WINBINDD_ERROR;
224 sids = (DOM_SID *)state->request.extra_data.data;
225 num = state->request.extra_len / sizeof(DOM_SID);
227 ids = TALLOC_ARRAY(state->mem_ctx, struct id_map *, num);
228 if ( ! ids) {
229 DEBUG(0, ("Out of memory!\n"));
230 return WINBINDD_ERROR;
232 for (i = 0; i < num; i++) {
233 ids[i] = TALLOC_P(ids, struct id_map);
234 if ( ! ids[i]) {
235 DEBUG(0, ("Out of memory!\n"));
236 talloc_free(ids);
237 return WINBINDD_ERROR;
239 ids[i]->sid = &sids[i];
242 result = idmap_sids_to_unixids(ids, num);
244 if (NT_STATUS_IS_OK(result)) {
246 xids = SMB_MALLOC_ARRAY(struct unixid, num);
247 if ( ! xids) {
248 DEBUG(0, ("Out of memory!\n"));
249 talloc_free(ids);
250 return WINBINDD_ERROR;
253 for (i = 0; i < num; i++) {
254 if (ids[i]->status == ID_MAPPED) {
255 xids[i].type = ids[i]->xid.type;
256 xids[i].id = ids[i]->xid.id;
257 } else {
258 xids[i].type = -1;
262 state->response.length = sizeof(state->response) + (sizeof(struct unixid) * num);
263 state->response.extra_data.data = xids;
265 } else {
266 DEBUG (2, ("idmap_sids_to_unixids returned an error: 0x%08x\n", NT_STATUS_V(result)));
267 talloc_free(ids);
268 return WINBINDD_ERROR;
271 talloc_free(ids);
272 return WINBINDD_OK;
275 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, bool success,
276 struct winbindd_response *response,
277 void *c, void *private_data)
279 void (*cont)(void *priv, bool succ, uid_t uid) =
280 (void (*)(void *, bool, uid_t))c;
282 if (!success) {
283 DEBUG(5, ("Could not trigger sid2uid\n"));
284 cont(private_data, False, 0);
285 return;
288 if (response->result != WINBINDD_OK) {
289 DEBUG(5, ("sid2uid returned an error\n"));
290 cont(private_data, False, 0);
291 return;
294 cont(private_data, True, response->data.uid);
297 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
298 void (*cont)(void *private_data, bool success, uid_t uid),
299 void *private_data)
301 struct winbindd_request request;
302 ZERO_STRUCT(request);
303 request.cmd = WINBINDD_DUAL_SID2UID;
304 sid_to_fstring(request.data.dual_sid2id.sid, sid);
305 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2uid_recv,
306 (void *)cont, private_data);
309 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
310 struct winbindd_cli_state *state)
312 DOM_SID sid;
313 NTSTATUS result;
315 DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
316 state->request.data.dual_sid2id.sid));
318 if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
319 DEBUG(1, ("Could not get convert sid %s from string\n",
320 state->request.data.dual_sid2id.sid));
321 return WINBINDD_ERROR;
324 /* Find uid for this sid and return it, possibly ask the slow remote idmap */
326 result = idmap_sid_to_uid(&sid, &(state->response.data.uid));
328 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
331 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, bool success,
332 struct winbindd_response *response,
333 void *c, void *private_data)
335 void (*cont)(void *priv, bool succ, gid_t gid) =
336 (void (*)(void *, bool, gid_t))c;
338 if (!success) {
339 DEBUG(5, ("Could not trigger sid2gid\n"));
340 cont(private_data, False, 0);
341 return;
344 if (response->result != WINBINDD_OK) {
345 DEBUG(5, ("sid2gid returned an error\n"));
346 cont(private_data, False, 0);
347 return;
350 cont(private_data, True, response->data.gid);
353 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
354 void (*cont)(void *private_data, bool success, gid_t gid),
355 void *private_data)
357 struct winbindd_request request;
358 ZERO_STRUCT(request);
359 request.cmd = WINBINDD_DUAL_SID2GID;
360 sid_to_fstring(request.data.dual_sid2id.sid, sid);
362 DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
363 request.data.dual_sid2id.sid));
365 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
366 (void *)cont, private_data);
369 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
370 struct winbindd_cli_state *state)
372 DOM_SID sid;
373 NTSTATUS result;
375 DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
376 state->request.data.dual_sid2id.sid));
378 if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
379 DEBUG(1, ("Could not get convert sid %s from string\n",
380 state->request.data.dual_sid2id.sid));
381 return WINBINDD_ERROR;
384 /* Find gid for this sid and return it, possibly ask the slow remote idmap */
386 result = idmap_sid_to_gid(&sid, &state->response.data.gid);
388 DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n",
389 NT_STATUS_V(result), sid_string_dbg(&sid),
390 state->response.data.gid));
392 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
395 /* The following uid2sid/gid2sid functions has been contributed by
396 * Keith Reynolds <Keith.Reynolds@centrify.com> */
398 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
399 struct winbindd_response *response,
400 void *c, void *private_data)
402 void (*cont)(void *priv, bool succ, const char *sid) =
403 (void (*)(void *, bool, const char *))c;
405 if (!success) {
406 DEBUG(5, ("Could not trigger uid2sid\n"));
407 cont(private_data, False, NULL);
408 return;
411 if (response->result != WINBINDD_OK) {
412 DEBUG(5, ("uid2sid returned an error\n"));
413 cont(private_data, False, NULL);
414 return;
417 cont(private_data, True, response->data.sid.sid);
420 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
421 void (*cont)(void *private_data, bool success, const char *sid),
422 void *private_data)
424 struct winbindd_request request;
426 ZERO_STRUCT(request);
427 request.cmd = WINBINDD_DUAL_UID2SID;
428 request.data.uid = uid;
429 do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
430 (void *)cont, private_data);
433 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
434 struct winbindd_cli_state *state)
436 DOM_SID sid;
437 NTSTATUS result;
439 DEBUG(3,("[%5lu]: uid to sid %lu\n",
440 (unsigned long)state->pid,
441 (unsigned long) state->request.data.uid));
443 /* Find sid for this uid and return it, possibly ask the slow remote idmap */
444 result = idmap_uid_to_sid(&sid, 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_request request;
483 ZERO_STRUCT(request);
484 request.cmd = WINBINDD_DUAL_GID2SID;
485 request.data.gid = gid;
486 do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
487 (void *)cont, private_data);
490 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
491 struct winbindd_cli_state *state)
493 DOM_SID sid;
494 NTSTATUS result;
496 DEBUG(3,("[%5lu]: gid %lu to sid\n",
497 (unsigned long)state->pid,
498 (unsigned long) state->request.data.gid));
500 /* Find sid for this gid and return it, possibly ask the slow remote idmap */
501 result = idmap_gid_to_sid(&sid, state->request.data.gid);
503 if (NT_STATUS_IS_OK(result)) {
504 sid_to_fstring(state->response.data.sid.sid, &sid);
505 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
506 (unsigned long)state->pid,
507 state->response.data.sid.sid));
508 state->response.data.sid.type = SID_NAME_DOM_GRP;
509 return WINBINDD_OK;
512 return WINBINDD_ERROR;
515 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
517 .name = "DUAL_SID2UID",
518 .struct_cmd = WINBINDD_DUAL_SID2UID,
519 .struct_fn = winbindd_dual_sid2uid,
521 .name = "DUAL_SID2GID",
522 .struct_cmd = WINBINDD_DUAL_SID2GID,
523 .struct_fn = winbindd_dual_sid2gid,
524 #if 0 /* DISABLED until we fix the interface in Samba 3.0.26 --jerry */
526 .name = "DUAL_SIDS2XIDS",
527 .struct_cmd = WINBINDD_DUAL_SIDS2XIDS,
528 .struct_fn = winbindd_dual_sids2xids,
529 #endif /* end DISABLED */
531 .name = "DUAL_UID2SID",
532 .struct_cmd = WINBINDD_DUAL_UID2SID,
533 .struct_fn = winbindd_dual_uid2sid,
535 .name = "DUAL_GID2SID",
536 .struct_cmd = WINBINDD_DUAL_GID2SID,
537 .struct_fn = winbindd_dual_gid2sid,
539 .name = "DUAL_SET_MAPPING",
540 .struct_cmd = WINBINDD_DUAL_SET_MAPPING,
541 .struct_fn = winbindd_dual_set_mapping,
543 .name = "DUAL_SET_HWMS",
544 .struct_cmd = WINBINDD_DUAL_SET_HWM,
545 .struct_fn = winbindd_dual_set_hwm,
547 .name = "ALLOCATE_UID",
548 .struct_cmd = WINBINDD_ALLOCATE_UID,
549 .struct_fn = winbindd_dual_allocate_uid,
551 .name = "ALLOCATE_GID",
552 .struct_cmd = WINBINDD_ALLOCATE_GID,
553 .struct_fn = winbindd_dual_allocate_gid,
555 .name = NULL,