CVE-2013-4408:s3:Ensure LookupNames replies arrays are range checked.
[Samba.git] / source4 / libnet / userinfo.c
blobc77e9bfa941f1368e4bf9e3eb26d4c2ce479421a
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Rafal Szczesniak 2005
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/>.
21 a composite function for getting user information via samr pipe
24 #include "includes.h"
25 #include "libcli/composite/composite.h"
26 #include "librpc/gen_ndr/security.h"
27 #include "libcli/security/security.h"
28 #include "libnet/libnet.h"
29 #include "librpc/gen_ndr/ndr_samr_c.h"
32 struct userinfo_state {
33 struct dcerpc_pipe *pipe;
34 struct policy_handle domain_handle;
35 struct policy_handle user_handle;
36 uint16_t level;
37 struct samr_LookupNames lookup;
38 struct samr_OpenUser openuser;
39 struct samr_QueryUserInfo queryuserinfo;
40 struct samr_Close samrclose;
41 union samr_UserInfo *info;
43 /* information about the progress */
44 void (*monitor_fn)(struct monitor_msg *);
48 static void continue_userinfo_lookup(struct tevent_req *subreq);
49 static void continue_userinfo_openuser(struct tevent_req *subreq);
50 static void continue_userinfo_getuser(struct tevent_req *subreq);
51 static void continue_userinfo_closeuser(struct tevent_req *subreq);
54 /**
55 * Stage 1 (optional): Look for a username in SAM server.
57 static void continue_userinfo_lookup(struct tevent_req *subreq)
59 struct composite_context *c;
60 struct userinfo_state *s;
61 struct monitor_msg msg;
62 struct msg_rpc_lookup_name *msg_lookup;
64 c = tevent_req_callback_data(subreq, struct composite_context);
65 s = talloc_get_type(c->private_data, struct userinfo_state);
67 /* receive samr_Lookup reply */
68 c->status = dcerpc_samr_LookupNames_r_recv(subreq, s);
69 TALLOC_FREE(subreq);
70 if (!composite_is_ok(c)) return;
72 /* there could be a problem with name resolving itself */
73 if (!NT_STATUS_IS_OK(s->lookup.out.result)) {
74 composite_error(c, s->lookup.out.result);
75 return;
78 /* issue a monitor message */
79 if (s->monitor_fn) {
80 msg.type = mon_SamrLookupName;
81 msg_lookup = talloc(s, struct msg_rpc_lookup_name);
82 msg_lookup->rid = s->lookup.out.rids->ids;
83 msg_lookup->count = s->lookup.out.rids->count;
84 msg.data = (void*)msg_lookup;
85 msg.data_size = sizeof(*msg_lookup);
87 s->monitor_fn(&msg);
91 /* have we actually got name resolved
92 - we're looking for only one at the moment */
93 if (s->lookup.out.rids->count != s->lookup.in.num_names) {
94 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
95 return;
97 if (s->lookup.out.types->count != s->lookup.in.num_names) {
98 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
99 return;
102 /* TODO: find proper status code for more than one rid found */
104 /* prepare parameters for LookupNames */
105 s->openuser.in.domain_handle = &s->domain_handle;
106 s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
107 s->openuser.in.rid = s->lookup.out.rids->ids[0];
108 s->openuser.out.user_handle = &s->user_handle;
110 /* send request */
111 subreq = dcerpc_samr_OpenUser_r_send(s, c->event_ctx,
112 s->pipe->binding_handle,
113 &s->openuser);
114 if (composite_nomem(subreq, c)) return;
116 tevent_req_set_callback(subreq, continue_userinfo_openuser, c);
121 * Stage 2: Open user policy handle.
123 static void continue_userinfo_openuser(struct tevent_req *subreq)
125 struct composite_context *c;
126 struct userinfo_state *s;
127 struct monitor_msg msg;
128 struct msg_rpc_open_user *msg_open;
130 c = tevent_req_callback_data(subreq, struct composite_context);
131 s = talloc_get_type(c->private_data, struct userinfo_state);
133 /* receive samr_OpenUser reply */
134 c->status = dcerpc_samr_OpenUser_r_recv(subreq, s);
135 TALLOC_FREE(subreq);
136 if (!composite_is_ok(c)) return;
138 if (!NT_STATUS_IS_OK(s->queryuserinfo.out.result)) {
139 composite_error(c, s->queryuserinfo.out.result);
140 return;
143 /* issue a monitor message */
144 if (s->monitor_fn) {
145 msg.type = mon_SamrOpenUser;
146 msg_open = talloc(s, struct msg_rpc_open_user);
147 msg_open->rid = s->openuser.in.rid;
148 msg_open->access_mask = s->openuser.in.access_mask;
149 msg.data = (void*)msg_open;
150 msg.data_size = sizeof(*msg_open);
152 s->monitor_fn(&msg);
155 /* prepare parameters for QueryUserInfo call */
156 s->queryuserinfo.in.user_handle = &s->user_handle;
157 s->queryuserinfo.in.level = s->level;
158 s->queryuserinfo.out.info = talloc(s, union samr_UserInfo *);
159 if (composite_nomem(s->queryuserinfo.out.info, c)) return;
161 /* queue rpc call, set event handling and new state */
162 subreq = dcerpc_samr_QueryUserInfo_r_send(s, c->event_ctx,
163 s->pipe->binding_handle,
164 &s->queryuserinfo);
165 if (composite_nomem(subreq, c)) return;
167 tevent_req_set_callback(subreq, continue_userinfo_getuser, c);
172 * Stage 3: Get requested user information.
174 static void continue_userinfo_getuser(struct tevent_req *subreq)
176 struct composite_context *c;
177 struct userinfo_state *s;
178 struct monitor_msg msg;
179 struct msg_rpc_query_user *msg_query;
181 c = tevent_req_callback_data(subreq, struct composite_context);
182 s = talloc_get_type(c->private_data, struct userinfo_state);
184 /* receive samr_QueryUserInfo reply */
185 c->status = dcerpc_samr_QueryUserInfo_r_recv(subreq, s);
186 TALLOC_FREE(subreq);
187 if (!composite_is_ok(c)) return;
189 /* check if queryuser itself went ok */
190 if (!NT_STATUS_IS_OK(s->queryuserinfo.out.result)) {
191 composite_error(c, s->queryuserinfo.out.result);
192 return;
195 s->info = talloc_steal(s, *(s->queryuserinfo.out.info));
197 /* issue a monitor message */
198 if (s->monitor_fn) {
199 msg.type = mon_SamrQueryUser;
200 msg_query = talloc(s, struct msg_rpc_query_user);
201 msg_query->level = s->queryuserinfo.in.level;
202 msg.data = (void*)msg_query;
203 msg.data_size = sizeof(*msg_query);
205 s->monitor_fn(&msg);
208 /* prepare arguments for Close call */
209 s->samrclose.in.handle = &s->user_handle;
210 s->samrclose.out.handle = &s->user_handle;
212 /* queue rpc call, set event handling and new state */
213 subreq = dcerpc_samr_Close_r_send(s, c->event_ctx,
214 s->pipe->binding_handle,
215 &s->samrclose);
216 if (composite_nomem(subreq, c)) return;
218 tevent_req_set_callback(subreq, continue_userinfo_closeuser, c);
223 * Stage 4: Close policy handle associated with opened user.
225 static void continue_userinfo_closeuser(struct tevent_req *subreq)
227 struct composite_context *c;
228 struct userinfo_state *s;
229 struct monitor_msg msg;
230 struct msg_rpc_close_user *msg_close;
232 c = tevent_req_callback_data(subreq, struct composite_context);
233 s = talloc_get_type(c->private_data, struct userinfo_state);
235 /* receive samr_Close reply */
236 c->status = dcerpc_samr_Close_r_recv(subreq, s);
237 TALLOC_FREE(subreq);
238 if (!composite_is_ok(c)) return;
240 if (!NT_STATUS_IS_OK(s->samrclose.out.result)) {
241 composite_error(c, s->samrclose.out.result);
242 return;
245 /* issue a monitor message */
246 if (s->monitor_fn) {
247 msg.type = mon_SamrClose;
248 msg_close = talloc(s, struct msg_rpc_close_user);
249 msg_close->rid = s->openuser.in.rid;
250 msg.data = (void*)msg_close;
251 msg.data_size = sizeof(*msg_close);
253 s->monitor_fn(&msg);
256 composite_done(c);
261 * Sends asynchronous userinfo request
263 * @param p dce/rpc call pipe
264 * @param io arguments and results of the call
266 struct composite_context *libnet_rpc_userinfo_send(struct dcerpc_pipe *p,
267 struct libnet_rpc_userinfo *io,
268 void (*monitor)(struct monitor_msg*))
270 struct composite_context *c;
271 struct userinfo_state *s;
272 struct dom_sid *sid;
273 struct tevent_req *subreq;
275 if (!p || !io) return NULL;
277 c = composite_create(p, dcerpc_event_context(p));
278 if (c == NULL) return c;
280 s = talloc_zero(c, struct userinfo_state);
281 if (composite_nomem(s, c)) return c;
283 c->private_data = s;
285 s->level = io->in.level;
286 s->pipe = p;
287 s->domain_handle = io->in.domain_handle;
288 s->monitor_fn = monitor;
290 if (io->in.sid) {
291 sid = dom_sid_parse_talloc(s, io->in.sid);
292 if (composite_nomem(sid, c)) return c;
294 s->openuser.in.domain_handle = &s->domain_handle;
295 s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
296 s->openuser.in.rid = sid->sub_auths[sid->num_auths - 1];
297 s->openuser.out.user_handle = &s->user_handle;
299 /* send request */
300 subreq = dcerpc_samr_OpenUser_r_send(s, c->event_ctx,
301 p->binding_handle,
302 &s->openuser);
303 if (composite_nomem(subreq, c)) return c;
305 tevent_req_set_callback(subreq, continue_userinfo_openuser, c);
307 } else {
308 /* preparing parameters to send rpc request */
309 s->lookup.in.domain_handle = &s->domain_handle;
310 s->lookup.in.num_names = 1;
311 s->lookup.in.names = talloc_array(s, struct lsa_String, 1);
312 if (composite_nomem(s->lookup.in.names, c)) return c;
313 s->lookup.out.rids = talloc_zero(s, struct samr_Ids);
314 s->lookup.out.types = talloc_zero(s, struct samr_Ids);
315 if (composite_nomem(s->lookup.out.rids, c)) return c;
316 if (composite_nomem(s->lookup.out.types, c)) return c;
318 s->lookup.in.names[0].string = talloc_strdup(s, io->in.username);
319 if (composite_nomem(s->lookup.in.names[0].string, c)) return c;
321 /* send request */
322 subreq = dcerpc_samr_LookupNames_r_send(s, c->event_ctx,
323 p->binding_handle,
324 &s->lookup);
325 if (composite_nomem(subreq, c)) return c;
327 tevent_req_set_callback(subreq, continue_userinfo_lookup, c);
330 return c;
335 * Waits for and receives result of asynchronous userinfo call
337 * @param c composite context returned by asynchronous userinfo call
338 * @param mem_ctx memory context of the call
339 * @param io pointer to results (and arguments) of the call
340 * @return nt status code of execution
343 NTSTATUS libnet_rpc_userinfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
344 struct libnet_rpc_userinfo *io)
346 NTSTATUS status;
347 struct userinfo_state *s;
349 /* wait for results of sending request */
350 status = composite_wait(c);
352 if (NT_STATUS_IS_OK(status) && io) {
353 s = talloc_get_type(c->private_data, struct userinfo_state);
354 talloc_steal(mem_ctx, s->info);
355 io->out.info = *s->info;
358 /* memory context associated to composite context is no longer needed */
359 talloc_free(c);
360 return status;
365 * Synchronous version of userinfo call
367 * @param pipe dce/rpc call pipe
368 * @param mem_ctx memory context for the call
369 * @param io arguments and results of the call
370 * @return nt status code of execution
373 NTSTATUS libnet_rpc_userinfo(struct dcerpc_pipe *p,
374 TALLOC_CTX *mem_ctx,
375 struct libnet_rpc_userinfo *io)
377 struct composite_context *c = libnet_rpc_userinfo_send(p, io, NULL);
378 return libnet_rpc_userinfo_recv(c, mem_ctx, io);