2 Unix SMB/CIFS implementation.
3 async implementation of WINBINDD_LOOKUPRIDS
4 Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
22 #include "librpc/gen_ndr/ndr_wbint_c.h"
23 #include "../libcli/security/security.h"
24 #include "passdb/machine_sid.h"
26 struct winbindd_lookuprids_state {
27 struct tevent_context *ev;
28 const char *domain_name;
29 struct wbint_RidArray rids;
30 struct wbint_Principals names;
32 struct lsa_SidArray tmp_sids;
33 struct lsa_RefDomainList tmp_domains;
34 struct lsa_TransNameArray tmp_names;
37 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
38 uint32_t **prids, uint32_t *pnum_rids);
40 static void winbindd_lookuprids_rids_done(struct tevent_req *subreq);
41 static void winbindd_lookuprids_sids_done(struct tevent_req *subreq);
43 struct tevent_req *winbindd_lookuprids_send(TALLOC_CTX *mem_ctx,
44 struct tevent_context *ev,
45 struct winbindd_cli_state *cli,
46 struct winbindd_request *request)
48 struct tevent_req *req, *subreq;
49 struct winbindd_lookuprids_state *state;
50 struct winbindd_domain *domain;
54 req = tevent_req_create(mem_ctx, &state,
55 struct winbindd_lookuprids_state);
61 /* Ensure null termination */
62 request->data.sid[sizeof(request->data.sid)-1]='\0';
64 DEBUG(3, ("lookuprids (%s)\n", request->data.sid));
66 if (!string_to_sid(&sid, request->data.sid)) {
67 DEBUG(5, ("%s not a SID\n", request->data.sid));
68 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
69 return tevent_req_post(req, ev);
72 domain = find_lookup_domain_from_sid(&sid);
74 DEBUG(5, ("Domain for sid %s not found\n",
75 sid_string_dbg(&sid)));
76 tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
77 return tevent_req_post(req, ev);
80 if (request->extra_data.data[request->extra_len-1] != '\0') {
81 DEBUG(5, ("extra_data not 0-terminated\n"));
82 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
83 return tevent_req_post(req, ev);
86 if (!parse_ridlist(state, request->extra_data.data,
87 &state->rids.rids, &state->rids.num_rids)) {
88 DEBUG(5, ("parse_ridlist failed\n"));
89 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
90 return tevent_req_post(req, ev);
93 if (sid_check_is_domain(&sid)) {
94 subreq = dcerpc_wbint_LookupRids_send(
95 state, ev, dom_child_handle(domain), &state->rids,
96 &state->domain_name, &state->names);
97 if (tevent_req_nomem(subreq, req)) {
98 return tevent_req_post(req, ev);
100 tevent_req_set_callback(subreq, winbindd_lookuprids_rids_done, req);
104 state->tmp_sids.num_sids = state->rids.num_rids;
105 state->tmp_sids.sids = talloc_array(state, struct lsa_SidPtr,
106 state->tmp_sids.num_sids);
107 if (tevent_req_nomem(state->tmp_sids.sids, req)) {
108 return tevent_req_post(req, ev);
111 for (i=0; i<state->rids.num_rids; i++) {
112 struct lsa_SidPtr *cur = &state->tmp_sids.sids[i];
113 cur->sid = dom_sid_add_rid(state->tmp_sids.sids,
114 &sid, state->rids.rids[i]);
115 if (tevent_req_nomem(cur->sid, req)) {
116 return tevent_req_post(req, ev);
120 state->names.num_principals = state->rids.num_rids;
121 state->names.principals = talloc_array(state,
122 struct wbint_Principal,
123 state->names.num_principals);
124 if (tevent_req_nomem(state->names.principals, req)) {
125 return tevent_req_post(req, ev);
128 subreq = dcerpc_wbint_LookupSids_send(
129 state, state->ev, dom_child_handle(domain),
130 &state->tmp_sids, &state->tmp_domains, &state->tmp_names);
131 if (tevent_req_nomem(subreq, req)) {
132 return tevent_req_post(req, ev);
134 tevent_req_set_callback(subreq, winbindd_lookuprids_sids_done, req);
138 static void winbindd_lookuprids_rids_done(struct tevent_req *subreq)
140 struct tevent_req *req = tevent_req_callback_data(
141 subreq, struct tevent_req);
142 struct winbindd_lookuprids_state *state = tevent_req_data(
143 req, struct winbindd_lookuprids_state);
144 NTSTATUS status, result;
146 status = dcerpc_wbint_LookupRids_recv(subreq, state, &result);
148 if (any_nt_status_not_ok(status, result, &status)) {
149 tevent_req_nterror(req, status);
152 tevent_req_done(req);
155 static void winbindd_lookuprids_sids_done(struct tevent_req *subreq)
157 struct tevent_req *req = tevent_req_callback_data(
158 subreq, struct tevent_req);
159 struct winbindd_lookuprids_state *state = tevent_req_data(
160 req, struct winbindd_lookuprids_state);
161 NTSTATUS status, result;
164 status = dcerpc_wbint_LookupSids_recv(subreq, state, &result);
166 if (any_nt_status_not_ok(status, result, &status)) {
167 tevent_req_nterror(req, status);
171 if (state->tmp_names.count != state->names.num_principals) {
172 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
176 if (state->tmp_domains.count != 1) {
177 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
181 state->domain_name = talloc_move(state,
182 &state->tmp_domains.domains[0].name.string);
184 for (i=0; i < state->tmp_names.count; i++) {
185 struct lsa_TranslatedName *n = &state->tmp_names.names[i];
186 struct wbint_Principal *p = &state->names.principals[i];
188 if (n->sid_index != 0) {
189 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
193 p->name = talloc_move(state->names.principals, &n->name.string);
194 p->sid = *state->tmp_sids.sids[i].sid;
195 p->type = n->sid_type;
198 tevent_req_done(req);
201 NTSTATUS winbindd_lookuprids_recv(struct tevent_req *req,
202 struct winbindd_response *response)
204 struct winbindd_lookuprids_state *state = tevent_req_data(
205 req, struct winbindd_lookuprids_state);
210 if (tevent_req_is_nterror(req, &status)) {
211 DEBUG(5, ("Lookuprids failed: %s\n",nt_errstr(status)));
215 result = talloc_strdup(response, "");
216 if (result == NULL) {
217 return NT_STATUS_NO_MEMORY;
220 for (i=0; i<state->names.num_principals; i++) {
221 struct wbint_Principal *p = &state->names.principals[i];
223 result = talloc_asprintf_append_buffer(
224 result, "%d %s\n", (int)p->type, p->name);
225 if (result == NULL) {
226 return NT_STATUS_NO_MEMORY;
230 fstrcpy(response->data.domain_name, state->domain_name);
231 response->extra_data.data = result;
232 response->length += talloc_get_size(result);
236 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
237 uint32_t **prids, uint32_t *pnum_rids)
239 uint32_t i, num_rids;
243 if (ridstr == NULL) {
252 while ((p = strchr(p, '\n')) != NULL) {
263 rids = talloc_array(mem_ctx, uint32_t, num_rids);
270 for (i=0; i<num_rids; i++) {
272 rids[i] = strtoul(p, &q, 10);
274 DEBUG(0, ("Got invalid ridstr: %s\n", p));
280 *pnum_rids = num_rids;