d0ea9c6032af5f6a2aab9337812eed6964bde40b
[kai/samba-autobuild/.git] / source3 / winbindd / winbindd_uid_to_sid.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_UID_TO_SID
4    Copyright (C) Volker Lendecke 2009
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "libcli/security/dom_sid.h"
23
24 struct winbindd_uid_to_sid_state {
25         struct tevent_context *ev;
26         struct unixid xid;
27         struct dom_sid *sid;
28 };
29
30 static void winbindd_uid_to_sid_done(struct tevent_req *subreq);
31
32 struct tevent_req *winbindd_uid_to_sid_send(TALLOC_CTX *mem_ctx,
33                                             struct tevent_context *ev,
34                                             struct winbindd_cli_state *cli,
35                                             struct winbindd_request *request)
36 {
37         struct tevent_req *req, *subreq;
38         struct winbindd_uid_to_sid_state *state;
39
40         req = tevent_req_create(mem_ctx, &state,
41                                 struct winbindd_uid_to_sid_state);
42         if (req == NULL) {
43                 return NULL;
44         }
45         state->ev = ev;
46
47         DEBUG(3, ("uid_to_sid %d\n", (int)request->data.uid));
48
49         state->xid = (struct unixid) {
50                 .id = request->data.uid, .type = ID_TYPE_UID };
51
52         subreq = wb_xids2sids_send(state, ev, &state->xid, 1);
53         if (tevent_req_nomem(subreq, req)) {
54                 return tevent_req_post(req, ev);
55         }
56         tevent_req_set_callback(subreq, winbindd_uid_to_sid_done, req);
57         return req;
58 }
59
60 static void winbindd_uid_to_sid_done(struct tevent_req *subreq)
61 {
62         struct tevent_req *req = tevent_req_callback_data(
63                 subreq, struct tevent_req);
64         struct winbindd_uid_to_sid_state *state = tevent_req_data(
65                 req, struct winbindd_uid_to_sid_state);
66         NTSTATUS status;
67
68         status = wb_xids2sids_recv(subreq, state, &state->sid);
69         TALLOC_FREE(subreq);
70         if (tevent_req_nterror(req, status)) {
71                 return;
72         }
73         tevent_req_done(req);
74 }
75
76 NTSTATUS winbindd_uid_to_sid_recv(struct tevent_req *req,
77                                   struct winbindd_response *response)
78 {
79         struct winbindd_uid_to_sid_state *state = tevent_req_data(
80                 req, struct winbindd_uid_to_sid_state);
81         NTSTATUS status;
82
83         if (tevent_req_is_nterror(req, &status)) {
84                 DEBUG(5, ("Could not convert sid %s: %s\n",
85                           sid_string_dbg(state->sid), nt_errstr(status)));
86                 return status;
87         }
88         if (is_null_sid(state->sid)) {
89                 return NT_STATUS_NONE_MAPPED;
90         }
91         sid_to_fstring(response->data.sid.sid, state->sid);
92         response->data.sid.type = SID_NAME_USER;
93         return NT_STATUS_OK;
94 }