s4-netlogon: implement dcesrv_netr_DsRAddressToSitenamesExW
[Samba/aatanasov.git] / source3 / winbindd / winbindd_lookuprids.c
blobf2cb8793eb74c4bc6f668e9c977d53ac9aa31bbe
1 /*
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/>.
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "librpc/gen_ndr/cli_wbint.h"
24 struct winbindd_lookuprids_state {
25 struct tevent_context *ev;
26 const char *domain_name;
27 struct wbint_RidArray rids;
28 struct wbint_Principals names;
31 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
32 uint32_t **prids, uint32_t *pnum_rids);
34 static void winbindd_lookuprids_done(struct tevent_req *subreq);
36 struct tevent_req *winbindd_lookuprids_send(TALLOC_CTX *mem_ctx,
37 struct tevent_context *ev,
38 struct winbindd_cli_state *cli,
39 struct winbindd_request *request)
41 struct tevent_req *req, *subreq;
42 struct winbindd_lookuprids_state *state;
43 struct winbindd_domain *domain;
44 struct dom_sid sid;
46 req = tevent_req_create(mem_ctx, &state,
47 struct winbindd_lookuprids_state);
48 if (req == NULL) {
49 return NULL;
51 state->ev = ev;
53 /* Ensure null termination */
54 request->data.sid[sizeof(request->data.sid)-1]='\0';
56 DEBUG(3, ("lookuprids (%s)\n", request->data.sid));
58 if (!string_to_sid(&sid, request->data.sid)) {
59 DEBUG(5, ("%s not a SID\n", request->data.sid));
60 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
61 return tevent_req_post(req, ev);
64 domain = find_domain_from_sid_noinit(&sid);
65 if (domain == NULL) {
66 DEBUG(5, ("Domain for sid %s not found\n",
67 sid_string_dbg(&sid)));
68 tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
69 return tevent_req_post(req, ev);
72 if (request->extra_data.data[request->extra_len-1] != '\0') {
73 DEBUG(5, ("extra_data not 0-terminated\n"));
74 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
75 return tevent_req_post(req, ev);
78 if (!parse_ridlist(state, request->extra_data.data,
79 &state->rids.rids, &state->rids.num_rids)) {
80 DEBUG(5, ("parse_ridlist failed\n"));
81 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
82 return tevent_req_post(req, ev);
85 subreq = rpccli_wbint_LookupRids_send(
86 state, ev, domain->child.rpccli, &state->rids, &state->names);
87 if (tevent_req_nomem(subreq, req)) {
88 return tevent_req_post(req, ev);
90 tevent_req_set_callback(subreq, winbindd_lookuprids_done, req);
91 return req;
94 static void winbindd_lookuprids_done(struct tevent_req *subreq)
96 struct tevent_req *req = tevent_req_callback_data(
97 subreq, struct tevent_req);
98 struct winbindd_lookuprids_state *state = tevent_req_data(
99 req, struct winbindd_lookuprids_state);
100 NTSTATUS status, result;
102 status = rpccli_wbint_LookupRids_recv(subreq, state, &result);
103 TALLOC_FREE(subreq);
104 if (!NT_STATUS_IS_OK(status)) {
105 tevent_req_nterror(req, status);
106 return;
108 if (!NT_STATUS_IS_OK(result)) {
109 tevent_req_nterror(req, result);
110 return;
112 tevent_req_done(req);
115 NTSTATUS winbindd_lookuprids_recv(struct tevent_req *req,
116 struct winbindd_response *response)
118 struct winbindd_lookuprids_state *state = tevent_req_data(
119 req, struct winbindd_lookuprids_state);
120 NTSTATUS status;
121 char *result;
122 int i;
124 if (tevent_req_is_nterror(req, &status)) {
125 DEBUG(5, ("Lookuprids failed: %s\n",nt_errstr(status)));
126 return status;
129 result = talloc_strdup(response, "");
130 if (result == NULL) {
131 return NT_STATUS_NO_MEMORY;
134 for (i=0; i<state->names.num_principals; i++) {
135 struct wbint_Principal *p = &state->names.principals[i];
137 result = talloc_asprintf_append_buffer(
138 result, "%d %s\n", (int)p->type, p->name);
139 if (result == NULL) {
140 return NT_STATUS_NO_MEMORY;
144 fstrcpy(response->data.domain_name, state->domain_name);
145 response->extra_data.data = result;
146 response->length += talloc_get_size(result);
147 return NT_STATUS_OK;
150 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
151 uint32_t **prids, uint32_t *pnum_rids)
153 uint32_t i, num_rids;
154 uint32_t *rids;
155 char *p;
157 if (ridstr == NULL) {
158 return false;
161 p = ridstr;
162 num_rids = 0;
164 /* count rids */
166 while ((p = strchr(p, '\n')) != NULL) {
167 p += 1;
168 num_rids += 1;
171 if (num_rids == 0) {
172 *pnum_rids = 0;
173 *prids = NULL;
174 return true;
177 rids = talloc_array(mem_ctx, uint32_t, num_rids);
178 if (rids == NULL) {
179 return false;
182 p = ridstr;
184 for (i=0; i<num_rids; i++) {
185 char *q;
186 rids[i] = strtoul(p, &q, 10);
187 if (*q != '\n') {
188 DEBUG(0, ("Got invalid ridstr: %s\n", p));
189 return false;
191 p = q+1;
194 *pnum_rids = num_rids;
195 *prids = rids;
196 return true;