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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 a composite function for getting user information via samr pipe
26 #include "libcli/composite/composite.h"
27 #include "libcli/security/proto.h"
28 #include "libnet/composite.h"
29 #include "libnet/userinfo.h"
30 #include "librpc/gen_ndr/ndr_samr_c.h"
32 static void userinfo_handler(struct rpc_request *req);
34 enum userinfo_stage { USERINFO_OPENUSER, USERINFO_GETUSER, USERINFO_CLOSEUSER };
36 struct userinfo_state {
37 enum userinfo_stage stage;
38 struct dcerpc_pipe *pipe;
39 struct rpc_request *req;
40 struct policy_handle user_handle;
42 struct samr_OpenUser openuser;
43 struct samr_QueryUserInfo queryuserinfo;
44 struct samr_Close samrclose;
45 union samr_UserInfo *info;
47 /* information about the progress */
48 void (*monitor_fn)(struct monitor_msg *);
53 * Stage 1: Open user policy handle in SAM server.
55 static NTSTATUS userinfo_openuser(struct composite_context *c,
56 struct userinfo_state *s)
58 /* receive samr_OpenUser reply */
59 c->status = dcerpc_ndr_request_recv(s->req);
60 NT_STATUS_NOT_OK_RETURN(c->status);
62 /* prepare parameters for QueryUserInfo call */
63 s->queryuserinfo.in.user_handle = &s->user_handle;
64 s->queryuserinfo.in.level = s->level;
66 /* queue rpc call, set event handling and new state */
67 s->req = dcerpc_samr_QueryUserInfo_send(s->pipe, c, &s->queryuserinfo);
68 if (s->req == NULL) goto failure;
70 s->req->async.callback = userinfo_handler;
71 s->req->async.private = c;
72 s->stage = USERINFO_GETUSER;
77 return NT_STATUS_UNSUCCESSFUL;
82 * Stage 2: Get requested user information.
84 static NTSTATUS userinfo_getuser(struct composite_context *c,
85 struct userinfo_state *s)
87 /* receive samr_QueryUserInfo reply */
88 c->status = dcerpc_ndr_request_recv(s->req);
89 NT_STATUS_NOT_OK_RETURN(c->status);
91 s->info = talloc_steal(s, s->queryuserinfo.out.info);
93 /* prepare arguments for Close call */
94 s->samrclose.in.handle = &s->user_handle;
95 s->samrclose.out.handle = &s->user_handle;
97 /* queue rpc call, set event handling and new state */
98 s->req = dcerpc_samr_Close_send(s->pipe, c, &s->samrclose);
100 s->req->async.callback = userinfo_handler;
101 s->req->async.private = c;
102 s->stage = USERINFO_CLOSEUSER;
109 * Stage 3: Close policy handle associated with opened user.
111 static NTSTATUS userinfo_closeuser(struct composite_context *c,
112 struct userinfo_state *s)
114 /* receive samr_Close reply */
115 c->status = dcerpc_ndr_request_recv(s->req);
116 NT_STATUS_NOT_OK_RETURN(c->status);
118 c->state = COMPOSITE_STATE_DONE;
125 * Event handler for asynchronous request. Handles transition through
126 * intermediate stages of the call.
128 * @param req rpc call context
130 static void userinfo_handler(struct rpc_request *req)
132 struct composite_context *c = req->async.private;
133 struct userinfo_state *s = talloc_get_type(c->private_data, struct userinfo_state);
134 struct monitor_msg msg;
135 struct msg_rpc_open_user *msg_open;
136 struct msg_rpc_query_user *msg_query;
137 struct msg_rpc_close_user *msg_close;
139 /* Stages of the call */
141 case USERINFO_OPENUSER:
142 c->status = userinfo_openuser(c, s);
144 msg.type = rpc_open_user;
145 msg_open = talloc(s, struct msg_rpc_open_user);
146 msg_open->rid = s->openuser.in.rid;
147 msg_open->access_mask = s->openuser.in.access_mask;
148 msg.data = (void*)msg_open;
149 msg.data_size = sizeof(*msg_open);
152 case USERINFO_GETUSER:
153 c->status = userinfo_getuser(c, s);
155 msg.type = rpc_query_user;
156 msg_query = talloc(s, struct msg_rpc_query_user);
157 msg_query->level = s->queryuserinfo.in.level;
158 msg.data = (void*)msg_query;
159 msg.data_size = sizeof(*msg_query);
162 case USERINFO_CLOSEUSER:
163 c->status = userinfo_closeuser(c, s);
165 msg.type = rpc_close_user;
166 msg_close = talloc(s, struct msg_rpc_close_user);
167 msg_close->rid = s->openuser.in.rid;
168 msg.data = (void*)msg_close;
169 msg.data_size = sizeof(*msg_close);
173 if (!NT_STATUS_IS_OK(c->status)) {
174 c->state = COMPOSITE_STATE_ERROR;
181 if (c->state >= COMPOSITE_STATE_DONE &&
189 * Sends asynchronous userinfo request
191 * @param p dce/rpc call pipe
192 * @param io arguments and results of the call
194 struct composite_context *libnet_rpc_userinfo_send(struct dcerpc_pipe *p,
195 struct libnet_rpc_userinfo *io,
196 void (*monitor)(struct monitor_msg*))
198 struct composite_context *c;
199 struct userinfo_state *s;
202 if (!p || !io) return NULL;
204 c = talloc_zero(p, struct composite_context);
205 if (c == NULL) goto failure;
207 s = talloc_zero(c, struct userinfo_state);
208 if (s == NULL) goto failure;
210 s->level = io->in.level;
212 s->monitor_fn = monitor;
214 sid = dom_sid_parse_talloc(s, io->in.sid);
215 if (sid == NULL) goto failure;
216 c->state = COMPOSITE_STATE_IN_PROGRESS;
218 c->event_ctx = dcerpc_event_context(p);
220 /* preparing parameters to send rpc request */
221 s->openuser.in.domain_handle = &io->in.domain_handle;
222 s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
223 s->openuser.in.rid = sid->sub_auths[sid->num_auths - 1];
224 s->openuser.out.user_handle = &s->user_handle;
227 s->req = dcerpc_samr_OpenUser_send(p, c, &s->openuser);
229 /* callback handler */
230 s->req->async.callback = userinfo_handler;
231 s->req->async.private = c;
232 s->stage = USERINFO_OPENUSER;
243 * Waits for and receives result of asynchronous userinfo call
245 * @param c composite context returned by asynchronous userinfo call
246 * @param mem_ctx memory context of the call
247 * @param io pointer to results (and arguments) of the call
248 * @return nt status code of execution
251 NTSTATUS libnet_rpc_userinfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
252 struct libnet_rpc_userinfo *io)
255 struct userinfo_state *s;
257 /* wait for results of sending request */
258 status = composite_wait(c);
260 if (NT_STATUS_IS_OK(status) && io) {
261 s = talloc_get_type(c->private_data, struct userinfo_state);
262 talloc_steal(mem_ctx, s->info);
263 io->out.info = *s->info;
266 /* memory context associated to composite context is no longer needed */
273 * Synchronous version of userinfo call
275 * @param pipe dce/rpc call pipe
276 * @param mem_ctx memory context for the call
277 * @param io arguments and results of the call
278 * @return nt status code of execution
281 NTSTATUS libnet_rpc_userinfo(struct dcerpc_pipe *p,
283 struct libnet_rpc_userinfo *io)
285 struct composite_context *c = libnet_rpc_userinfo_send(p, io, NULL);
286 return libnet_rpc_userinfo_recv(c, mem_ctx, io);