source: branches/samba-3.5.x/source3/winbindd/winbindd_lookuprids.c

Last change on this file was 620, checked in by Herwig Bauernfeind, 14 years ago

Samba 3.5: Update trunk to 3.5.11

File size: 5.2 KB
Line 
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
24struct winbindd_lookuprids_state {
25 struct tevent_context *ev;
26 struct dom_sid domain_sid;
27 const char *domain_name;
28 struct wbint_RidArray rids;
29 struct wbint_Principals names;
30};
31
32static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
33 uint32_t **prids, uint32_t *pnum_rids);
34
35static void winbindd_lookuprids_done(struct tevent_req *subreq);
36
37struct tevent_req *winbindd_lookuprids_send(TALLOC_CTX *mem_ctx,
38 struct tevent_context *ev,
39 struct winbindd_cli_state *cli,
40 struct winbindd_request *request)
41{
42 struct tevent_req *req, *subreq;
43 struct winbindd_lookuprids_state *state;
44 struct winbindd_domain *domain;
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(&state->domain_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_lookup_domain_from_sid(&state->domain_sid);
65 if (domain == NULL) {
66 DEBUG(5, ("Domain for sid %s not found\n",
67 sid_string_dbg(&state->domain_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 = rpccli_wbint_LookupRids_send(
86 state, ev, domain->child.rpccli, &state->domain_sid,
87 &state->rids, &state->domain_name, &state->names);
88 if (tevent_req_nomem(subreq, req)) {
89 return tevent_req_post(req, ev);
90 }
91 tevent_req_set_callback(subreq, winbindd_lookuprids_done, req);
92 return req;
93}
94
95static void winbindd_lookuprids_done(struct tevent_req *subreq)
96{
97 struct tevent_req *req = tevent_req_callback_data(
98 subreq, struct tevent_req);
99 struct winbindd_lookuprids_state *state = tevent_req_data(
100 req, struct winbindd_lookuprids_state);
101 NTSTATUS status, result;
102
103 status = rpccli_wbint_LookupRids_recv(subreq, state, &result);
104 TALLOC_FREE(subreq);
105 if (!NT_STATUS_IS_OK(status)) {
106 tevent_req_nterror(req, status);
107 return;
108 }
109 if (!NT_STATUS_IS_OK(result)) {
110 tevent_req_nterror(req, result);
111 return;
112 }
113 tevent_req_done(req);
114}
115
116NTSTATUS winbindd_lookuprids_recv(struct tevent_req *req,
117 struct winbindd_response *response)
118{
119 struct winbindd_lookuprids_state *state = tevent_req_data(
120 req, struct winbindd_lookuprids_state);
121 NTSTATUS status;
122 char *result;
123 int i;
124
125 if (tevent_req_is_nterror(req, &status)) {
126 DEBUG(5, ("Lookuprids failed: %s\n",nt_errstr(status)));
127 return status;
128 }
129
130 result = talloc_strdup(response, "");
131 if (result == NULL) {
132 return NT_STATUS_NO_MEMORY;
133 }
134
135 for (i=0; i<state->names.num_principals; i++) {
136 struct wbint_Principal *p = &state->names.principals[i];
137
138 result = talloc_asprintf_append_buffer(
139 result, "%d %s\n", (int)p->type, p->name);
140 if (result == NULL) {
141 return NT_STATUS_NO_MEMORY;
142 }
143 }
144
145 fstrcpy(response->data.domain_name, state->domain_name);
146 response->extra_data.data = result;
147 response->length += talloc_get_size(result);
148 return NT_STATUS_OK;
149}
150
151static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
152 uint32_t **prids, uint32_t *pnum_rids)
153{
154 uint32_t i, num_rids;
155 uint32_t *rids;
156 char *p;
157
158 if (ridstr == NULL) {
159 return false;
160 }
161
162 p = ridstr;
163 num_rids = 0;
164
165 /* count rids */
166
167 while ((p = strchr(p, '\n')) != NULL) {
168 p += 1;
169 num_rids += 1;
170 }
171
172 if (num_rids == 0) {
173 *pnum_rids = 0;
174 *prids = NULL;
175 return true;
176 }
177
178 rids = talloc_array(mem_ctx, uint32_t, num_rids);
179 if (rids == NULL) {
180 return false;
181 }
182
183 p = ridstr;
184
185 for (i=0; i<num_rids; i++) {
186 char *q;
187 rids[i] = strtoul(p, &q, 10);
188 if (*q != '\n') {
189 DEBUG(0, ("Got invalid ridstr: %s\n", p));
190 return false;
191 }
192 p = q+1;
193 }
194
195 *pnum_rids = num_rids;
196 *prids = rids;
197 return true;
198}
Note: See TracBrowser for help on using the repository browser.