Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-trivial
[samba.git] / source4 / winbind / wb_sid2uid.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Map a SID to a uid
5
6    Copyright (C) Kai Blin 2007-2008
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/composite/composite.h"
24 #include "winbind/wb_server.h"
25 #include "smbd/service_task.h"
26 #include "winbind/wb_helper.h"
27 #include "libcli/security/proto.h"
28 #include "winbind/idmap.h"
29
30 struct sid2uid_state {
31         struct composite_context *ctx;
32         struct wbsrv_service *service;
33         uid_t uid;
34 };
35
36 struct composite_context *wb_sid2uid_send(TALLOC_CTX *mem_ctx,
37                 struct wbsrv_service *service, const struct dom_sid *sid)
38 {
39         struct composite_context *result;
40         struct sid2uid_state *state;
41
42         DEBUG(5, ("wb_sid2uid_send called\n"));
43
44         result = composite_create(mem_ctx, service->task->event_ctx);
45         if (!result) return NULL;
46
47         state = talloc(result, struct sid2uid_state);
48         if(composite_nomem(state, result)) return result;
49
50         state->ctx = result;
51         result->private_data = state;
52         state->service = service;
53
54         state->ctx->status = idmap_sid_to_uid(service->idmap_ctx, state, sid,
55                                               &state->uid);
56         if (NT_STATUS_EQUAL(state->ctx->status, NT_STATUS_RETRY)) {
57                 state->ctx->status = idmap_sid_to_uid(service->idmap_ctx, state,
58                                                       sid, &state->uid);
59         }
60         if (!composite_is_ok(state->ctx)) return result;
61
62         composite_done(state->ctx);
63         return result;
64 }
65
66 NTSTATUS wb_sid2uid_recv(struct composite_context *ctx, uid_t *uid)
67 {
68         NTSTATUS status = composite_wait(ctx);
69
70         DEBUG(5, ("wb_sid2uid_recv called\n"));
71
72         if (NT_STATUS_IS_OK(status)) {
73                 struct sid2uid_state *state =
74                         talloc_get_type(ctx->private_data,
75                                 struct sid2uid_state);
76                 *uid = state->uid;
77         }
78         talloc_free(ctx);
79         return status;
80 }
81