s3:pylibsmb: Make .list() work for SMBv2
[samba.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/ndr_winbind_c.h"
23 #include "../libcli/security/security.h"
24
25 struct winbindd_lookuprids_state {
26         struct tevent_context *ev;
27         struct dom_sid domain_sid;
28         const char *domain_name;
29         struct wbint_RidArray rids;
30         struct wbint_Principals names;
31 };
32
33 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
34                           uint32_t **prids, uint32_t *pnum_rids);
35
36 static void winbindd_lookuprids_done(struct tevent_req *subreq);
37
38 struct tevent_req *winbindd_lookuprids_send(TALLOC_CTX *mem_ctx,
39                                             struct tevent_context *ev,
40                                             struct winbindd_cli_state *cli,
41                                             struct winbindd_request *request)
42 {
43         struct tevent_req *req, *subreq;
44         struct winbindd_lookuprids_state *state;
45         struct winbindd_domain *domain;
46
47         req = tevent_req_create(mem_ctx, &state,
48                                 struct winbindd_lookuprids_state);
49         if (req == NULL) {
50                 return NULL;
51         }
52         state->ev = ev;
53
54         /* Ensure null termination */
55         request->data.sid[sizeof(request->data.sid)-1]='\0';
56
57         DEBUG(3, ("lookuprids (%s)\n", request->data.sid));
58
59         if (!string_to_sid(&state->domain_sid, request->data.sid)) {
60                 DEBUG(5, ("%s not a SID\n", request->data.sid));
61                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
62                 return tevent_req_post(req, ev);
63         }
64
65         domain = find_lookup_domain_from_sid(&state->domain_sid);
66         if (domain == NULL) {
67                 struct dom_sid_buf buf;
68                 DEBUG(5, ("Domain for sid %s not found\n",
69                           dom_sid_str_buf(&state->domain_sid, &buf)));
70                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
71                 return tevent_req_post(req, ev);
72         }
73
74         if (request->extra_data.data[request->extra_len-1] != '\0') {
75                 DEBUG(5, ("extra_data not 0-terminated\n"));
76                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
77                 return tevent_req_post(req, ev);
78         }
79
80         if (!parse_ridlist(state, request->extra_data.data,
81                            &state->rids.rids, &state->rids.num_rids)) {
82                 DEBUG(5, ("parse_ridlist failed\n"));
83                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
84                 return tevent_req_post(req, ev);
85         }
86
87         subreq = dcerpc_wbint_LookupRids_send(
88                 state, ev, dom_child_handle(domain), &state->domain_sid,
89                 &state->rids, &state->domain_name, &state->names);
90         if (tevent_req_nomem(subreq, req)) {
91                 return tevent_req_post(req, ev);
92         }
93         tevent_req_set_callback(subreq, winbindd_lookuprids_done, req);
94         return req;
95 }
96
97 static void winbindd_lookuprids_done(struct tevent_req *subreq)
98 {
99         struct tevent_req *req = tevent_req_callback_data(
100                 subreq, struct tevent_req);
101         struct winbindd_lookuprids_state *state = tevent_req_data(
102                 req, struct winbindd_lookuprids_state);
103         NTSTATUS status, result;
104
105         status = dcerpc_wbint_LookupRids_recv(subreq, state, &result);
106         TALLOC_FREE(subreq);
107         if (any_nt_status_not_ok(status, result, &status)) {
108                 tevent_req_nterror(req, status);
109                 return;
110         }
111         tevent_req_done(req);
112 }
113
114 NTSTATUS winbindd_lookuprids_recv(struct tevent_req *req,
115                                   struct winbindd_response *response)
116 {
117         struct winbindd_lookuprids_state *state = tevent_req_data(
118                 req, struct winbindd_lookuprids_state);
119         NTSTATUS status;
120         char *result;
121         uint32_t i;
122
123         if (tevent_req_is_nterror(req, &status)) {
124                 DEBUG(5, ("Lookuprids failed: %s\n",nt_errstr(status)));
125                 return status;
126         }
127
128         result = talloc_strdup(response, "");
129         if (result == NULL) {
130                 return NT_STATUS_NO_MEMORY;
131         }
132
133         for (i=0; i<state->names.num_principals; i++) {
134                 struct wbint_Principal *p = &state->names.principals[i];
135
136                 result = talloc_asprintf_append_buffer(
137                         result, "%d %s\n", (int)p->type, p->name);
138                 if (result == NULL) {
139                         return NT_STATUS_NO_MEMORY;
140                 }
141         }
142
143         fstrcpy(response->data.domain_name, state->domain_name);
144         response->extra_data.data = result;
145         response->length += talloc_get_size(result);
146         return NT_STATUS_OK;
147 }
148
149 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
150                           uint32_t **prids, uint32_t *pnum_rids)
151 {
152         uint32_t i, num_rids;
153         uint32_t *rids;
154         char *p;
155
156         if (ridstr == NULL) {
157                 return false;
158         }
159
160         p = ridstr;
161         num_rids = 0;
162
163         /* count rids */
164
165         while ((p = strchr(p, '\n')) != NULL) {
166                 p += 1;
167                 num_rids += 1;
168         }
169
170         if (num_rids == 0) {
171                 *pnum_rids = 0;
172                 *prids = NULL;
173                 return true;
174         }
175
176         rids = talloc_array(mem_ctx, uint32_t, num_rids);
177         if (rids == NULL) {
178                 return false;
179         }
180
181         p = ridstr;
182
183         for (i=0; i<num_rids; i++) {
184                 char *q;
185                 rids[i] = strtoul(p, &q, 10);
186                 if (*q != '\n') {
187                         DEBUG(0, ("Got invalid ridstr: %s\n", p));
188                         return false;
189                 }
190                 p = q+1;
191         }
192
193         *pnum_rids = num_rids;
194         *prids = rids;
195         return true;
196 }