s3: Use any_nt_status_not_ok in winbind
[abartlet/samba.git/.git] / source3 / winbindd / wb_lookupname.c
1 /*
2    Unix SMB/CIFS implementation.
3    async lookupname
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_lookupname_state {
26         struct tevent_context *ev;
27         const char *dom_name;
28         const char *name;
29         uint32_t flags;
30         struct dom_sid sid;
31         enum lsa_SidType type;
32 };
33
34 static void wb_lookupname_done(struct tevent_req *subreq);
35 static void wb_lookupname_root_done(struct tevent_req *subreq);
36
37 struct tevent_req *wb_lookupname_send(TALLOC_CTX *mem_ctx,
38                                       struct tevent_context *ev,
39                                       const char *dom_name, const char *name,
40                                       uint32_t flags)
41 {
42         struct tevent_req *req, *subreq;
43         struct wb_lookupname_state *state;
44         struct winbindd_domain *domain;
45
46         req = tevent_req_create(mem_ctx, &state, struct wb_lookupname_state);
47         if (req == NULL) {
48                 return NULL;
49         }
50         state->ev = ev;
51         state->flags = flags;
52
53         /*
54          * Uppercase domain and name so that we become cache-friendly
55          */
56         state->dom_name = talloc_strdup_upper(state, dom_name);
57         if (tevent_req_nomem(state->dom_name, req)) {
58                 return tevent_req_post(req, ev);
59         }
60         state->name = talloc_strdup_upper(state, name);
61         if (tevent_req_nomem(state->name, req)) {
62                 return tevent_req_post(req, ev);
63         }
64
65         domain = find_lookup_domain_from_name(state->dom_name);
66         if (domain == NULL) {
67                 DEBUG(5, ("Could not find domain for %s\n", state->dom_name));
68                 tevent_req_nterror(req, NT_STATUS_NONE_MAPPED);
69                 return tevent_req_post(req, ev);
70         }
71
72         subreq = dcerpc_wbint_LookupName_send(
73                 state, ev, domain->child.binding_handle, state->dom_name, state->name,
74                 flags, &state->type, &state->sid);
75         if (tevent_req_nomem(subreq, req)) {
76                 return tevent_req_post(req, ev);
77         }
78         tevent_req_set_callback(subreq, wb_lookupname_done, req);
79         return req;
80 }
81
82 static void wb_lookupname_done(struct tevent_req *subreq)
83 {
84         struct tevent_req *req = tevent_req_callback_data(
85                 subreq, struct tevent_req);
86         struct wb_lookupname_state *state = tevent_req_data(
87                 req, struct wb_lookupname_state);
88         struct winbindd_domain *root_domain;
89         NTSTATUS status, result;
90
91         status = dcerpc_wbint_LookupName_recv(subreq, state, &result);
92         TALLOC_FREE(subreq);
93         if (!NT_STATUS_IS_OK(status)) {
94                 tevent_req_nterror(req, status);
95                 return;
96         }
97         if (NT_STATUS_IS_OK(result)) {
98                 tevent_req_done(req);
99                 return;
100         }
101
102         /*
103          * "our" DC did not find it, lets retry with the forest root
104          * domain
105          */
106
107         root_domain = find_root_domain();
108         if (root_domain == NULL) {
109                 tevent_req_nterror(req, result);
110                 return;
111         }
112
113         subreq = dcerpc_wbint_LookupName_send(
114                 state, state->ev, root_domain->child.binding_handle, state->dom_name,
115                 state->name, state->flags, &state->type, &state->sid);
116         if (tevent_req_nomem(subreq, req)) {
117                 return;
118         }
119         tevent_req_set_callback(subreq, wb_lookupname_root_done, req);
120 }
121
122 static void wb_lookupname_root_done(struct tevent_req *subreq)
123 {
124         struct tevent_req *req = tevent_req_callback_data(
125                 subreq, struct tevent_req);
126         struct wb_lookupname_state *state = tevent_req_data(
127                 req, struct wb_lookupname_state);
128         NTSTATUS status, result;
129
130         status = dcerpc_wbint_LookupName_recv(subreq, state, &result);
131         TALLOC_FREE(subreq);
132         if (any_nt_status_not_ok(status, result, &status)) {
133                 tevent_req_nterror(req, status);
134                 return;
135         }
136         tevent_req_done(req);
137 }
138
139 NTSTATUS wb_lookupname_recv(struct tevent_req *req, struct dom_sid *sid,
140                             enum lsa_SidType *type)
141 {
142         struct wb_lookupname_state *state = tevent_req_data(
143                 req, struct wb_lookupname_state);
144         NTSTATUS status;
145
146         if (tevent_req_is_nterror(req, &status)) {
147                 return status;
148         }
149         sid_copy(sid, &state->sid);
150         *type = state->type;
151         return NT_STATUS_OK;
152 }