s3:libsmb: add function cli_qpathinfo3()
[Samba.git] / source4 / libnet / userinfo.c
blob9530f9efdb5c457ad4c69083bc94352cf919f037
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_abort(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_abort(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->openuser.out.result)) {
139 composite_error(c, s->openuser.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_abort(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_abort(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 TALLOC_CTX *mem_ctx,
268 struct libnet_rpc_userinfo *io,
269 void (*monitor)(struct monitor_msg*))
271 struct composite_context *c;
272 struct userinfo_state *s;
273 struct dom_sid *sid;
274 struct tevent_req *subreq;
276 if (!p || !io) return NULL;
278 c = composite_create(mem_ctx, dcerpc_event_context(p));
279 if (c == NULL) return c;
281 s = talloc_zero(c, struct userinfo_state);
282 if (composite_nomem(s, c)) return c;
284 c->private_data = s;
286 s->level = io->in.level;
287 s->pipe = p;
288 s->domain_handle = io->in.domain_handle;
289 s->monitor_fn = monitor;
291 if (io->in.sid) {
292 sid = dom_sid_parse_talloc(s, io->in.sid);
293 if (composite_nomem(sid, c)) return c;
295 s->openuser.in.domain_handle = &s->domain_handle;
296 s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
297 s->openuser.in.rid = sid->sub_auths[sid->num_auths - 1];
298 s->openuser.out.user_handle = &s->user_handle;
300 /* send request */
301 subreq = dcerpc_samr_OpenUser_r_send(s, c->event_ctx,
302 p->binding_handle,
303 &s->openuser);
304 if (composite_nomem(subreq, c)) return c;
306 tevent_req_set_callback(subreq, continue_userinfo_openuser, c);
308 } else {
309 /* preparing parameters to send rpc request */
310 s->lookup.in.domain_handle = &s->domain_handle;
311 s->lookup.in.num_names = 1;
312 s->lookup.in.names = talloc_array(s, struct lsa_String, 1);
313 if (composite_nomem(s->lookup.in.names, c)) return c;
314 s->lookup.out.rids = talloc_zero(s, struct samr_Ids);
315 s->lookup.out.types = talloc_zero(s, struct samr_Ids);
316 if (composite_nomem(s->lookup.out.rids, c)) return c;
317 if (composite_nomem(s->lookup.out.types, c)) return c;
319 s->lookup.in.names[0].string = talloc_strdup(s, io->in.username);
320 if (composite_nomem(s->lookup.in.names[0].string, c)) return c;
322 /* send request */
323 subreq = dcerpc_samr_LookupNames_r_send(s, c->event_ctx,
324 p->binding_handle,
325 &s->lookup);
326 if (composite_nomem(subreq, c)) return c;
328 tevent_req_set_callback(subreq, continue_userinfo_lookup, c);
331 return c;
336 * Waits for and receives result of asynchronous userinfo call
338 * @param c composite context returned by asynchronous userinfo call
339 * @param mem_ctx memory context of the call
340 * @param io pointer to results (and arguments) of the call
341 * @return nt status code of execution
344 NTSTATUS libnet_rpc_userinfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
345 struct libnet_rpc_userinfo *io)
347 NTSTATUS status;
348 struct userinfo_state *s;
350 /* wait for results of sending request */
351 status = composite_wait(c);
353 if (NT_STATUS_IS_OK(status) && io) {
354 s = talloc_get_type_abort(c->private_data, struct userinfo_state);
355 talloc_steal(mem_ctx, s->info);
356 io->out.info = *s->info;
359 /* memory context associated to composite context is no longer needed */
360 talloc_free(c);
361 return status;
366 * Synchronous version of userinfo call
368 * @param pipe dce/rpc call pipe
369 * @param mem_ctx memory context for the call
370 * @param io arguments and results of the call
371 * @return nt status code of execution
374 NTSTATUS libnet_rpc_userinfo(struct dcerpc_pipe *p,
375 TALLOC_CTX *mem_ctx,
376 struct libnet_rpc_userinfo *io)
378 struct composite_context *c = libnet_rpc_userinfo_send(p, mem_ctx, io, NULL);
379 return libnet_rpc_userinfo_recv(c, mem_ctx, io);