libcli/security Provide a common, top level libcli/security/security.h
[sfrench/samba-autobuild/.git] / source3 / winbindd / wb_lookupsid.c
1 /*
2    Unix SMB/CIFS implementation.
3    async lookupsid
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 "librpc/gen_ndr/cli_wbint.h"
23 #include "../libcli/security/security.h"
24
25 struct wb_lookupsid_state {
26         struct tevent_context *ev;
27         struct winbindd_domain *lookup_domain;
28         struct dom_sid sid;
29         enum lsa_SidType type;
30         const char *domname;
31         const char *name;
32 };
33
34 static void wb_lookupsid_done(struct tevent_req *subreq);
35
36 struct tevent_req *wb_lookupsid_send(TALLOC_CTX *mem_ctx,
37                                      struct tevent_context *ev,
38                                      const struct dom_sid *sid)
39 {
40         struct tevent_req *req, *subreq;
41         struct wb_lookupsid_state *state;
42
43         req = tevent_req_create(mem_ctx, &state, struct wb_lookupsid_state);
44         if (req == NULL) {
45                 return NULL;
46         }
47         sid_copy(&state->sid, sid);
48         state->ev = ev;
49
50         state->lookup_domain = find_lookup_domain_from_sid(sid);
51         if (state->lookup_domain == NULL) {
52                 DEBUG(5, ("Could not find domain for sid %s\n",
53                           sid_string_dbg(sid)));
54                 tevent_req_nterror(req, NT_STATUS_NONE_MAPPED);
55                 return tevent_req_post(req, ev);
56         }
57
58         subreq = dcerpc_wbint_LookupSid_send(
59                 state, ev, state->lookup_domain->child.binding_handle,
60                 &state->sid, &state->type, &state->domname, &state->name);
61         if (tevent_req_nomem(subreq, req)) {
62                 return tevent_req_post(req, ev);
63         }
64         tevent_req_set_callback(subreq, wb_lookupsid_done, req);
65         return req;
66 }
67
68 static void wb_lookupsid_done(struct tevent_req *subreq)
69 {
70         struct tevent_req *req = tevent_req_callback_data(
71                 subreq, struct tevent_req);
72         struct wb_lookupsid_state *state = tevent_req_data(
73                 req, struct wb_lookupsid_state);
74         struct winbindd_domain *forest_root;
75         NTSTATUS status, result;
76
77         status = dcerpc_wbint_LookupSid_recv(subreq, state, &result);
78         TALLOC_FREE(subreq);
79         if (!NT_STATUS_IS_OK(status)) {
80                 tevent_req_nterror(req, status);
81                 return;
82         }
83         if (NT_STATUS_IS_OK(result)) {
84                 tevent_req_done(req);
85                 return;
86         }
87
88         /*
89          * Let's try the forest root
90          */
91         forest_root = find_root_domain();
92         if ((forest_root == NULL) || (forest_root == state->lookup_domain)) {
93                 tevent_req_nterror(req, result);
94                 return;
95         }
96         state->lookup_domain = forest_root;
97
98         subreq = dcerpc_wbint_LookupSid_send(
99                 state, state->ev, state->lookup_domain->child.binding_handle,
100                 &state->sid, &state->type, &state->domname, &state->name);
101         if (tevent_req_nomem(subreq, req)) {
102                 return;
103         }
104         tevent_req_set_callback(subreq, wb_lookupsid_done, req);
105 }
106
107 NTSTATUS wb_lookupsid_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
108                            enum lsa_SidType *type, const char **domain,
109                            const char **name)
110 {
111         struct wb_lookupsid_state *state = tevent_req_data(
112                 req, struct wb_lookupsid_state);
113         NTSTATUS status;
114
115         if (tevent_req_is_nterror(req, &status)) {
116                 return status;
117         }
118         *type = state->type;
119         *domain = talloc_move(mem_ctx, &state->domname);
120         *name = talloc_move(mem_ctx, &state->name);
121         return NT_STATUS_OK;
122 }