s3:winbindd: use dcerpc_wbint_* functions instead of rpccli_wbint_*
[metze/samba/wip.git] / source3 / winbindd / winbindd_lookuprids.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_LOOKUPRIDS
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
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;
29 };
30
31 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
32                           uint32_t **prids, uint32_t *pnum_rids);
33
34 static void winbindd_lookuprids_done(struct tevent_req *subreq);
35
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)
40 {
41         struct tevent_req *req, *subreq;
42         struct winbindd_lookuprids_state *state;
43         struct winbindd_domain *domain;
44         struct dom_sid sid;
45
46         req = tevent_req_create(mem_ctx, &state,
47                                 struct winbindd_lookuprids_state);
48         if (req == NULL) {
49                 return NULL;
50         }
51         state->ev = ev;
52
53         /* Ensure null termination */
54         request->data.sid[sizeof(request->data.sid)-1]='\0';
55
56         DEBUG(3, ("lookuprids (%s)\n", request->data.sid));
57
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);
62         }
63
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);
70         }
71
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);
76         }
77
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);
83         }
84
85         subreq = dcerpc_wbint_LookupRids_send(
86                 state, ev, domain->child.binding_handle, &state->rids, &state->names);
87         if (tevent_req_nomem(subreq, req)) {
88                 return tevent_req_post(req, ev);
89         }
90         tevent_req_set_callback(subreq, winbindd_lookuprids_done, req);
91         return req;
92 }
93
94 static void winbindd_lookuprids_done(struct tevent_req *subreq)
95 {
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;
101
102         status = dcerpc_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;
107         }
108         if (!NT_STATUS_IS_OK(result)) {
109                 tevent_req_nterror(req, result);
110                 return;
111         }
112         tevent_req_done(req);
113 }
114
115 NTSTATUS winbindd_lookuprids_recv(struct tevent_req *req,
116                                   struct winbindd_response *response)
117 {
118         struct winbindd_lookuprids_state *state = tevent_req_data(
119                 req, struct winbindd_lookuprids_state);
120         NTSTATUS status;
121         char *result;
122         int i;
123
124         if (tevent_req_is_nterror(req, &status)) {
125                 DEBUG(5, ("Lookuprids failed: %s\n",nt_errstr(status)));
126                 return status;
127         }
128
129         result = talloc_strdup(response, "");
130         if (result == NULL) {
131                 return NT_STATUS_NO_MEMORY;
132         }
133
134         for (i=0; i<state->names.num_principals; i++) {
135                 struct wbint_Principal *p = &state->names.principals[i];
136
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;
141                 }
142         }
143
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;
148 }
149
150 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
151                           uint32_t **prids, uint32_t *pnum_rids)
152 {
153         uint32_t i, num_rids;
154         uint32_t *rids;
155         char *p;
156
157         if (ridstr == NULL) {
158                 return false;
159         }
160
161         p = ridstr;
162         num_rids = 0;
163
164         /* count rids */
165
166         while ((p = strchr(p, '\n')) != NULL) {
167                 p += 1;
168                 num_rids += 1;
169         }
170
171         if (num_rids == 0) {
172                 *pnum_rids = 0;
173                 *prids = NULL;
174                 return true;
175         }
176
177         rids = talloc_array(mem_ctx, uint32_t, num_rids);
178         if (rids == NULL) {
179                 return false;
180         }
181
182         p = ridstr;
183
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;
190                 }
191                 p = q+1;
192         }
193
194         *pnum_rids = num_rids;
195         *prids = rids;
196         return true;
197 }