Move root check out of smb1_file_se_access_check() in preparation for deleting this...
[Samba.git] / source4 / libnet / userinfo.c
blob8d9c8419971c4ed74f4c9ded8680791e5b976a62
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 == 0) {
94 composite_error(c, NT_STATUS_NO_SUCH_USER);
97 /* TODO: find proper status code for more than one rid found */
99 /* prepare parameters for LookupNames */
100 s->openuser.in.domain_handle = &s->domain_handle;
101 s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
102 s->openuser.in.rid = s->lookup.out.rids->ids[0];
103 s->openuser.out.user_handle = &s->user_handle;
105 /* send request */
106 subreq = dcerpc_samr_OpenUser_r_send(s, c->event_ctx,
107 s->pipe->binding_handle,
108 &s->openuser);
109 if (composite_nomem(subreq, c)) return;
111 tevent_req_set_callback(subreq, continue_userinfo_openuser, c);
116 * Stage 2: Open user policy handle.
118 static void continue_userinfo_openuser(struct tevent_req *subreq)
120 struct composite_context *c;
121 struct userinfo_state *s;
122 struct monitor_msg msg;
123 struct msg_rpc_open_user *msg_open;
125 c = tevent_req_callback_data(subreq, struct composite_context);
126 s = talloc_get_type(c->private_data, struct userinfo_state);
128 /* receive samr_OpenUser reply */
129 c->status = dcerpc_samr_OpenUser_r_recv(subreq, s);
130 TALLOC_FREE(subreq);
131 if (!composite_is_ok(c)) return;
133 if (!NT_STATUS_IS_OK(s->queryuserinfo.out.result)) {
134 composite_error(c, s->queryuserinfo.out.result);
135 return;
138 /* issue a monitor message */
139 if (s->monitor_fn) {
140 msg.type = mon_SamrOpenUser;
141 msg_open = talloc(s, struct msg_rpc_open_user);
142 msg_open->rid = s->openuser.in.rid;
143 msg_open->access_mask = s->openuser.in.access_mask;
144 msg.data = (void*)msg_open;
145 msg.data_size = sizeof(*msg_open);
147 s->monitor_fn(&msg);
150 /* prepare parameters for QueryUserInfo call */
151 s->queryuserinfo.in.user_handle = &s->user_handle;
152 s->queryuserinfo.in.level = s->level;
153 s->queryuserinfo.out.info = talloc(s, union samr_UserInfo *);
154 if (composite_nomem(s->queryuserinfo.out.info, c)) return;
156 /* queue rpc call, set event handling and new state */
157 subreq = dcerpc_samr_QueryUserInfo_r_send(s, c->event_ctx,
158 s->pipe->binding_handle,
159 &s->queryuserinfo);
160 if (composite_nomem(subreq, c)) return;
162 tevent_req_set_callback(subreq, continue_userinfo_getuser, c);
167 * Stage 3: Get requested user information.
169 static void continue_userinfo_getuser(struct tevent_req *subreq)
171 struct composite_context *c;
172 struct userinfo_state *s;
173 struct monitor_msg msg;
174 struct msg_rpc_query_user *msg_query;
176 c = tevent_req_callback_data(subreq, struct composite_context);
177 s = talloc_get_type(c->private_data, struct userinfo_state);
179 /* receive samr_QueryUserInfo reply */
180 c->status = dcerpc_samr_QueryUserInfo_r_recv(subreq, s);
181 TALLOC_FREE(subreq);
182 if (!composite_is_ok(c)) return;
184 /* check if queryuser itself went ok */
185 if (!NT_STATUS_IS_OK(s->queryuserinfo.out.result)) {
186 composite_error(c, s->queryuserinfo.out.result);
187 return;
190 s->info = talloc_steal(s, *(s->queryuserinfo.out.info));
192 /* issue a monitor message */
193 if (s->monitor_fn) {
194 msg.type = mon_SamrQueryUser;
195 msg_query = talloc(s, struct msg_rpc_query_user);
196 msg_query->level = s->queryuserinfo.in.level;
197 msg.data = (void*)msg_query;
198 msg.data_size = sizeof(*msg_query);
200 s->monitor_fn(&msg);
203 /* prepare arguments for Close call */
204 s->samrclose.in.handle = &s->user_handle;
205 s->samrclose.out.handle = &s->user_handle;
207 /* queue rpc call, set event handling and new state */
208 subreq = dcerpc_samr_Close_r_send(s, c->event_ctx,
209 s->pipe->binding_handle,
210 &s->samrclose);
211 if (composite_nomem(subreq, c)) return;
213 tevent_req_set_callback(subreq, continue_userinfo_closeuser, c);
218 * Stage 4: Close policy handle associated with opened user.
220 static void continue_userinfo_closeuser(struct tevent_req *subreq)
222 struct composite_context *c;
223 struct userinfo_state *s;
224 struct monitor_msg msg;
225 struct msg_rpc_close_user *msg_close;
227 c = tevent_req_callback_data(subreq, struct composite_context);
228 s = talloc_get_type(c->private_data, struct userinfo_state);
230 /* receive samr_Close reply */
231 c->status = dcerpc_samr_Close_r_recv(subreq, s);
232 TALLOC_FREE(subreq);
233 if (!composite_is_ok(c)) return;
235 if (!NT_STATUS_IS_OK(s->samrclose.out.result)) {
236 composite_error(c, s->samrclose.out.result);
237 return;
240 /* issue a monitor message */
241 if (s->monitor_fn) {
242 msg.type = mon_SamrClose;
243 msg_close = talloc(s, struct msg_rpc_close_user);
244 msg_close->rid = s->openuser.in.rid;
245 msg.data = (void*)msg_close;
246 msg.data_size = sizeof(*msg_close);
248 s->monitor_fn(&msg);
251 composite_done(c);
256 * Sends asynchronous userinfo request
258 * @param p dce/rpc call pipe
259 * @param io arguments and results of the call
261 struct composite_context *libnet_rpc_userinfo_send(struct dcerpc_pipe *p,
262 struct libnet_rpc_userinfo *io,
263 void (*monitor)(struct monitor_msg*))
265 struct composite_context *c;
266 struct userinfo_state *s;
267 struct dom_sid *sid;
268 struct tevent_req *subreq;
270 if (!p || !io) return NULL;
272 c = composite_create(p, dcerpc_event_context(p));
273 if (c == NULL) return c;
275 s = talloc_zero(c, struct userinfo_state);
276 if (composite_nomem(s, c)) return c;
278 c->private_data = s;
280 s->level = io->in.level;
281 s->pipe = p;
282 s->domain_handle = io->in.domain_handle;
283 s->monitor_fn = monitor;
285 if (io->in.sid) {
286 sid = dom_sid_parse_talloc(s, io->in.sid);
287 if (composite_nomem(sid, c)) return c;
289 s->openuser.in.domain_handle = &s->domain_handle;
290 s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
291 s->openuser.in.rid = sid->sub_auths[sid->num_auths - 1];
292 s->openuser.out.user_handle = &s->user_handle;
294 /* send request */
295 subreq = dcerpc_samr_OpenUser_r_send(s, c->event_ctx,
296 p->binding_handle,
297 &s->openuser);
298 if (composite_nomem(subreq, c)) return c;
300 tevent_req_set_callback(subreq, continue_userinfo_openuser, c);
302 } else {
303 /* preparing parameters to send rpc request */
304 s->lookup.in.domain_handle = &s->domain_handle;
305 s->lookup.in.num_names = 1;
306 s->lookup.in.names = talloc_array(s, struct lsa_String, 1);
307 if (composite_nomem(s->lookup.in.names, c)) return c;
308 s->lookup.out.rids = talloc_zero(s, struct samr_Ids);
309 s->lookup.out.types = talloc_zero(s, struct samr_Ids);
310 if (composite_nomem(s->lookup.out.rids, c)) return c;
311 if (composite_nomem(s->lookup.out.types, c)) return c;
313 s->lookup.in.names[0].string = talloc_strdup(s, io->in.username);
314 if (composite_nomem(s->lookup.in.names[0].string, c)) return c;
316 /* send request */
317 subreq = dcerpc_samr_LookupNames_r_send(s, c->event_ctx,
318 p->binding_handle,
319 &s->lookup);
320 if (composite_nomem(subreq, c)) return c;
322 tevent_req_set_callback(subreq, continue_userinfo_lookup, c);
325 return c;
330 * Waits for and receives result of asynchronous userinfo call
332 * @param c composite context returned by asynchronous userinfo call
333 * @param mem_ctx memory context of the call
334 * @param io pointer to results (and arguments) of the call
335 * @return nt status code of execution
338 NTSTATUS libnet_rpc_userinfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
339 struct libnet_rpc_userinfo *io)
341 NTSTATUS status;
342 struct userinfo_state *s;
344 /* wait for results of sending request */
345 status = composite_wait(c);
347 if (NT_STATUS_IS_OK(status) && io) {
348 s = talloc_get_type(c->private_data, struct userinfo_state);
349 talloc_steal(mem_ctx, s->info);
350 io->out.info = *s->info;
353 /* memory context associated to composite context is no longer needed */
354 talloc_free(c);
355 return status;
360 * Synchronous version of userinfo call
362 * @param pipe dce/rpc call pipe
363 * @param mem_ctx memory context for the call
364 * @param io arguments and results of the call
365 * @return nt status code of execution
368 NTSTATUS libnet_rpc_userinfo(struct dcerpc_pipe *p,
369 TALLOC_CTX *mem_ctx,
370 struct libnet_rpc_userinfo *io)
372 struct composite_context *c = libnet_rpc_userinfo_send(p, io, NULL);
373 return libnet_rpc_userinfo_recv(c, mem_ctx, io);