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"
31 static void userinfo_handler(struct rpc_request *req);
33 enum userinfo_stage { USERINFO_OPENUSER, USERINFO_GETUSER, USERINFO_CLOSEUSER };
35 struct userinfo_state {
36 enum userinfo_stage stage;
37 struct dcerpc_pipe *pipe;
38 struct rpc_request *req;
39 struct policy_handle user_handle;
41 struct samr_OpenUser openuser;
42 struct samr_QueryUserInfo queryuserinfo;
43 struct samr_Close samrclose;
44 union samr_UserInfo *info;
46 /* information about the progress */
47 void (*monitor_fn)(struct monitor_msg *);
52 * Stage 1: Open user policy handle in SAM server.
54 static NTSTATUS userinfo_openuser(struct composite_context *c,
55 struct userinfo_state *s)
57 /* receive samr_OpenUser reply */
58 c->status = dcerpc_ndr_request_recv(s->req);
59 NT_STATUS_NOT_OK_RETURN(c->status);
61 /* prepare parameters for QueryUserInfo call */
62 s->queryuserinfo.in.user_handle = &s->user_handle;
63 s->queryuserinfo.in.level = s->level;
65 /* queue rpc call, set event handling and new state */
66 s->req = dcerpc_samr_QueryUserInfo_send(s->pipe, c, &s->queryuserinfo);
67 if (s->req == NULL) goto failure;
69 s->req->async.callback = userinfo_handler;
70 s->req->async.private = c;
71 s->stage = USERINFO_GETUSER;
76 return NT_STATUS_UNSUCCESSFUL;
81 * Stage 2: Get requested user information.
83 static NTSTATUS userinfo_getuser(struct composite_context *c,
84 struct userinfo_state *s)
86 /* receive samr_QueryUserInfo reply */
87 c->status = dcerpc_ndr_request_recv(s->req);
88 NT_STATUS_NOT_OK_RETURN(c->status);
90 s->info = talloc_steal(s, s->queryuserinfo.out.info);
92 /* prepare arguments for Close call */
93 s->samrclose.in.handle = &s->user_handle;
94 s->samrclose.out.handle = &s->user_handle;
96 /* queue rpc call, set event handling and new state */
97 s->req = dcerpc_samr_Close_send(s->pipe, c, &s->samrclose);
99 s->req->async.callback = userinfo_handler;
100 s->req->async.private = c;
101 s->stage = USERINFO_CLOSEUSER;
108 * Stage 3: Close policy handle associated with opened user.
110 static NTSTATUS userinfo_closeuser(struct composite_context *c,
111 struct userinfo_state *s)
113 /* receive samr_Close reply */
114 c->status = dcerpc_ndr_request_recv(s->req);
115 NT_STATUS_NOT_OK_RETURN(c->status);
117 c->state = COMPOSITE_STATE_DONE;
124 * Event handler for asynchronous request. Handles transition through
125 * intermediate stages of the call.
127 * @param req rpc call context
129 static void userinfo_handler(struct rpc_request *req)
131 struct composite_context *c = req->async.private;
132 struct userinfo_state *s = talloc_get_type(c->private_data, struct userinfo_state);
133 struct monitor_msg msg;
134 struct msg_rpc_open_user *msg_open;
135 struct msg_rpc_query_user *msg_query;
136 struct msg_rpc_close_user *msg_close;
138 /* Stages of the call */
140 case USERINFO_OPENUSER:
141 c->status = userinfo_openuser(c, s);
143 msg.type = rpc_open_user;
144 msg_open = talloc(s, struct msg_rpc_open_user);
145 msg_open->rid = s->openuser.in.rid;
146 msg_open->access_mask = s->openuser.in.access_mask;
147 msg.data = (void*)msg_open;
148 msg.data_size = sizeof(*msg_open);
151 case USERINFO_GETUSER:
152 c->status = userinfo_getuser(c, s);
154 msg.type = rpc_query_user;
155 msg_query = talloc(s, struct msg_rpc_query_user);
156 msg_query->level = s->queryuserinfo.in.level;
157 msg.data = (void*)msg_query;
158 msg.data_size = sizeof(*msg_query);
161 case USERINFO_CLOSEUSER:
162 c->status = userinfo_closeuser(c, s);
164 msg.type = rpc_close_user;
165 msg_close = talloc(s, struct msg_rpc_close_user);
166 msg_close->rid = s->openuser.in.rid;
167 msg.data = (void*)msg_close;
168 msg.data_size = sizeof(*msg_close);
172 if (!NT_STATUS_IS_OK(c->status)) {
173 c->state = COMPOSITE_STATE_ERROR;
180 if (c->state >= COMPOSITE_STATE_DONE &&
188 * Sends asynchronous userinfo request
190 * @param p dce/rpc call pipe
191 * @param io arguments and results of the call
193 struct composite_context *libnet_rpc_userinfo_send(struct dcerpc_pipe *p,
194 struct libnet_rpc_userinfo *io,
195 void (*monitor)(struct monitor_msg*))
197 struct composite_context *c;
198 struct userinfo_state *s;
201 if (!p || !io) return NULL;
203 c = talloc_zero(p, struct composite_context);
204 if (c == NULL) goto failure;
206 s = talloc_zero(c, struct userinfo_state);
207 if (s == NULL) goto failure;
209 s->level = io->in.level;
211 s->monitor_fn = monitor;
213 sid = dom_sid_parse_talloc(s, io->in.sid);
214 if (sid == NULL) goto failure;
215 c->state = COMPOSITE_STATE_IN_PROGRESS;
217 c->event_ctx = dcerpc_event_context(p);
219 /* preparing parameters to send rpc request */
220 s->openuser.in.domain_handle = &io->in.domain_handle;
221 s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
222 s->openuser.in.rid = sid->sub_auths[sid->num_auths - 1];
223 s->openuser.out.user_handle = &s->user_handle;
226 s->req = dcerpc_samr_OpenUser_send(p, c, &s->openuser);
228 /* callback handler */
229 s->req->async.callback = userinfo_handler;
230 s->req->async.private = c;
231 s->stage = USERINFO_OPENUSER;
242 * Waits for and receives result of asynchronous userinfo call
244 * @param c composite context returned by asynchronous userinfo call
245 * @param mem_ctx memory context of the call
246 * @param io pointer to results (and arguments) of the call
247 * @return nt status code of execution
250 NTSTATUS libnet_rpc_userinfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
251 struct libnet_rpc_userinfo *io)
254 struct userinfo_state *s;
256 /* wait for results of sending request */
257 status = composite_wait(c);
259 if (NT_STATUS_IS_OK(status) && io) {
260 s = talloc_get_type(c->private_data, struct userinfo_state);
261 talloc_steal(mem_ctx, s->info);
262 io->out.info = *s->info;
265 /* memory context associated to composite context is no longer needed */
272 * Synchronous version of userinfo call
274 * @param pipe dce/rpc call pipe
275 * @param mem_ctx memory context for the call
276 * @param io arguments and results of the call
277 * @return nt status code of execution
280 NTSTATUS libnet_rpc_userinfo(struct dcerpc_pipe *p,
282 struct libnet_rpc_userinfo *io)
284 struct composite_context *c = libnet_rpc_userinfo_send(p, io, NULL);
285 return libnet_rpc_userinfo_recv(c, mem_ctx, io);