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/raw/libcliraw.h"
27 #include "libcli/composite/composite.h"
28 #include "libcli/composite/monitor.h"
29 #include "librpc/gen_ndr/ndr_samr.h"
30 #include "libnet/composite.h"
31 #include "libnet/userinfo.h"
33 static void userinfo_handler(struct rpc_request *req);
35 enum userinfo_stage { USERINFO_OPENUSER, USERINFO_GETUSER, USERINFO_CLOSEUSER };
37 struct userinfo_state {
38 enum userinfo_stage stage;
39 struct dcerpc_pipe *pipe;
40 struct rpc_request *req;
41 struct policy_handle user_handle;
43 struct samr_OpenUser openuser;
44 struct samr_QueryUserInfo queryuserinfo;
45 struct samr_Close samrclose;
46 union samr_UserInfo *info;
51 * Stage 1: Open user policy handle in SAM server.
53 static NTSTATUS userinfo_openuser(struct composite_context *c,
54 struct userinfo_state *s)
56 /* receive samr_OpenUser reply */
57 c->status = dcerpc_ndr_request_recv(s->req);
58 NT_STATUS_NOT_OK_RETURN(c->status);
60 /* prepare parameters for QueryUserInfo call */
61 s->queryuserinfo.in.user_handle = &s->user_handle;
62 s->queryuserinfo.in.level = s->level;
64 /* queue rpc call, set event handling and new state */
65 s->req = dcerpc_samr_QueryUserInfo_send(s->pipe, c, &s->queryuserinfo);
66 if (s->req == NULL) goto failure;
68 s->req->async.callback = userinfo_handler;
69 s->req->async.private = c;
70 s->stage = USERINFO_GETUSER;
75 return NT_STATUS_UNSUCCESSFUL;
80 * Stage 2: Get requested user information.
82 static NTSTATUS userinfo_getuser(struct composite_context *c,
83 struct userinfo_state *s)
85 /* receive samr_QueryUserInfo reply */
86 c->status = dcerpc_ndr_request_recv(s->req);
87 NT_STATUS_NOT_OK_RETURN(c->status);
89 s->info = talloc_steal(s, s->queryuserinfo.out.info);
91 /* prepare arguments for Close call */
92 s->samrclose.in.handle = &s->user_handle;
93 s->samrclose.out.handle = &s->user_handle;
95 /* queue rpc call, set event handling and new state */
96 s->req = dcerpc_samr_Close_send(s->pipe, c, &s->samrclose);
98 s->req->async.callback = userinfo_handler;
99 s->req->async.private = c;
100 s->stage = USERINFO_CLOSEUSER;
107 * Stage 3: Close policy handle associated with opened user.
109 static NTSTATUS userinfo_closeuser(struct composite_context *c,
110 struct userinfo_state *s)
112 /* receive samr_Close reply */
113 c->status = dcerpc_ndr_request_recv(s->req);
114 NT_STATUS_NOT_OK_RETURN(c->status);
116 c->state = SMBCLI_REQUEST_DONE;
123 * Event handler for asynchronous request. Handles transition through
124 * intermediate stages of the call.
126 * @param req rpc call context
128 static void userinfo_handler(struct rpc_request *req)
130 struct composite_context *c = req->async.private;
131 struct userinfo_state *s = talloc_get_type(c->private, struct userinfo_state);
132 struct monitor_msg msg;
133 struct msg_rpc_open_user *msg_open;
134 struct msg_rpc_query_user *msg_query;
135 struct msg_rpc_close_user *msg_close;
137 /* Stages of the call */
139 case USERINFO_OPENUSER:
140 c->status = userinfo_openuser(c, s);
142 msg.type = rpc_open_user;
143 msg_open = talloc(s, struct msg_rpc_open_user);
144 msg_open->rid = s->openuser.in.rid;
145 msg_open->access_mask = s->openuser.in.access_mask;
146 msg.data = (void*)msg_open;
147 msg.data_size = sizeof(*msg_open);
150 case USERINFO_GETUSER:
151 c->status = userinfo_getuser(c, s);
153 msg.type = rpc_query_user;
154 msg_query = talloc(s, struct msg_rpc_query_user);
155 msg_query->level = s->queryuserinfo.in.level;
156 msg.data = (void*)msg_query;
157 msg.data_size = sizeof(*msg_query);
160 case USERINFO_CLOSEUSER:
161 c->status = userinfo_closeuser(c, s);
163 msg.type = rpc_close_user;
164 msg_close = talloc(s, struct msg_rpc_close_user);
165 msg_close->rid = s->openuser.in.rid;
166 msg.data = (void*)msg_close;
167 msg.data_size = sizeof(*msg_close);
171 if (!NT_STATUS_IS_OK(c->status)) {
172 c->state = SMBCLI_REQUEST_ERROR;
179 if (c->state >= SMBCLI_REQUEST_DONE &&
187 * Sends asynchronous userinfo request
189 * @param p dce/rpc call pipe
190 * @param io arguments and results of the call
192 struct composite_context *libnet_rpc_userinfo_send(struct dcerpc_pipe *p,
193 struct libnet_rpc_userinfo *io,
194 void (*monitor)(struct monitor_msg*))
196 struct composite_context *c;
197 struct userinfo_state *s;
200 if (!p || !io) return NULL;
202 c = talloc_zero(p, struct composite_context);
203 if (c == NULL) goto failure;
205 s = talloc_zero(c, struct userinfo_state);
206 if (s == NULL) goto failure;
208 s->level = io->in.level;
211 sid = dom_sid_parse_talloc(s, io->in.sid);
212 if (sid == NULL) goto failure;
213 c->state = SMBCLI_REQUEST_SEND;
215 c->event_ctx = dcerpc_event_context(p);
216 c->monitor_fn = monitor;
218 /* preparing parameters to send rpc request */
219 s->openuser.in.domain_handle = &io->in.domain_handle;
220 s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
221 s->openuser.in.rid = sid->sub_auths[sid->num_auths - 1];
222 s->openuser.out.user_handle = &s->user_handle;
225 s->req = dcerpc_samr_OpenUser_send(p, c, &s->openuser);
227 /* callback handler */
228 s->req->async.callback = userinfo_handler;
229 s->req->async.private = c;
230 s->stage = USERINFO_OPENUSER;
241 * Waits for and receives result of asynchronous userinfo call
243 * @param c composite context returned by asynchronous userinfo call
244 * @param mem_ctx memory context of the call
245 * @param io pointer to results (and arguments) of the call
246 * @return nt status code of execution
249 NTSTATUS libnet_rpc_userinfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
250 struct libnet_rpc_userinfo *io)
253 struct userinfo_state *s;
255 /* wait for results of sending request */
256 status = composite_wait(c);
258 if (NT_STATUS_IS_OK(status) && io) {
259 s = talloc_get_type(c->private, struct userinfo_state);
260 talloc_steal(mem_ctx, s->info);
261 io->out.info = *s->info;
264 /* memory context associated to composite context is no longer needed */
271 * Synchronous version of userinfo call
273 * @param pipe dce/rpc call pipe
274 * @param mem_ctx memory context for the call
275 * @param io arguments and results of the call
276 * @return nt status code of execution
279 NTSTATUS libnet_rpc_userinfo(struct dcerpc_pipe *pipe,
281 struct libnet_rpc_userinfo *io)
283 struct composite_context *c = libnet_rpc_userinfo_send(pipe, io, NULL);
284 return libnet_rpc_userinfo_recv(c, mem_ctx, io);