151f39906bcdcfe9ba763f3db23cb037f7808f1d
[kai/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) 2007-2008 Kai Blin
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/security.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 static void sid2uid_recv_uid(struct composite_context *ctx);
37
38 struct composite_context *wb_sid2uid_send(TALLOC_CTX *mem_ctx,
39                 struct wbsrv_service *service, const struct dom_sid *sid)
40 {
41         struct composite_context *result, *ctx;
42         struct sid2uid_state *state;
43         struct id_mapping *ids;
44
45         DEBUG(5, ("wb_sid2uid_send called\n"));
46
47         result = composite_create(mem_ctx, service->task->event_ctx);
48         if (!result) return NULL;
49
50         state = talloc(result, struct sid2uid_state);
51         if (composite_nomem(state, result)) return result;
52
53         state->ctx = result;
54         result->private_data = state;
55         state->service = service;
56
57         ids = talloc(result, struct id_mapping);
58         if (composite_nomem(ids, result)) return result;
59
60         ids->sid = dom_sid_dup(result, sid);
61         if (composite_nomem(ids->sid, result)) return result;
62
63         ctx = wb_sids2xids_send(result, service, 1, ids);
64         if (composite_nomem(ctx, result)) return result;
65
66         composite_continue(result, ctx, sid2uid_recv_uid, state);
67         return result;
68 }
69
70 static void sid2uid_recv_uid(struct composite_context *ctx)
71 {
72         struct sid2uid_state *state = talloc_get_type(ctx->async.private_data,
73                                                       struct sid2uid_state);
74
75         struct id_mapping *ids = NULL;
76
77         state->ctx->status = wb_sids2xids_recv(ctx, &ids);
78         if (!composite_is_ok(state->ctx)) return;
79
80         if (!NT_STATUS_IS_OK(ids->status)) {
81                 composite_error(state->ctx, ids->status);
82                 return;
83         }
84
85         if (ids->unixid->type == ID_TYPE_BOTH ||
86             ids->unixid->type == ID_TYPE_UID) {
87                 state->uid = ids->unixid->id;
88                 composite_done(state->ctx);
89         } else {
90                 composite_error(state->ctx, NT_STATUS_INVALID_SID);
91         }
92 }
93
94 NTSTATUS wb_sid2uid_recv(struct composite_context *ctx, uid_t *uid)
95 {
96         NTSTATUS status = composite_wait(ctx);
97
98         DEBUG(5, ("wb_sid2uid_recv called\n"));
99
100         if (NT_STATUS_IS_OK(status)) {
101                 struct sid2uid_state *state =
102                         talloc_get_type(ctx->private_data,
103                                 struct sid2uid_state);
104                 *uid = state->uid;
105         }
106         talloc_free(ctx);
107         return status;
108 }
109