Use shared copy of hmac5 implementation.
[Samba/gebeck_regimport.git] / source4 / libnet / userinfo.c
blob847997fc12f6b6faab8eb2bc3c38b01106284723
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 "libnet/composite.h"
27 #include "librpc/gen_ndr/security.h"
28 #include "libcli/security/security.h"
29 #include "libnet/userman.h"
30 #include "libnet/userinfo.h"
31 #include "librpc/gen_ndr/ndr_samr_c.h"
34 struct userinfo_state {
35 struct dcerpc_pipe *pipe;
36 struct policy_handle domain_handle;
37 struct policy_handle user_handle;
38 uint16_t level;
39 struct samr_LookupNames lookup;
40 struct samr_OpenUser openuser;
41 struct samr_QueryUserInfo queryuserinfo;
42 struct samr_Close samrclose;
43 union samr_UserInfo *info;
45 /* information about the progress */
46 void (*monitor_fn)(struct monitor_msg *);
50 static void continue_userinfo_lookup(struct rpc_request *req);
51 static void continue_userinfo_openuser(struct rpc_request *req);
52 static void continue_userinfo_getuser(struct rpc_request *req);
53 static void continue_userinfo_closeuser(struct rpc_request *req);
56 /**
57 * Stage 1 (optional): Look for a username in SAM server.
59 static void continue_userinfo_lookup(struct rpc_request *req)
61 struct composite_context *c;
62 struct userinfo_state *s;
63 struct rpc_request *openuser_req;
64 struct monitor_msg msg;
65 struct msg_rpc_lookup_name *msg_lookup;
67 c = talloc_get_type(req->async.private_data, struct composite_context);
68 s = talloc_get_type(c->private_data, struct userinfo_state);
70 /* receive samr_Lookup reply */
71 c->status = dcerpc_ndr_request_recv(req);
72 if (!composite_is_ok(c)) return;
74 /* there could be a problem with name resolving itself */
75 if (!NT_STATUS_IS_OK(s->lookup.out.result)) {
76 composite_error(c, s->lookup.out.result);
77 return;
80 /* issue a monitor message */
81 if (s->monitor_fn) {
82 msg.type = mon_SamrLookupName;
83 msg_lookup = talloc(s, struct msg_rpc_lookup_name);
84 msg_lookup->rid = s->lookup.out.rids.ids;
85 msg_lookup->count = s->lookup.out.rids.count;
86 msg.data = (void*)msg_lookup;
87 msg.data_size = sizeof(*msg_lookup);
89 s->monitor_fn(&msg);
93 /* have we actually got name resolved
94 - we're looking for only one at the moment */
95 if (s->lookup.out.rids.count == 0) {
96 composite_error(c, NT_STATUS_NO_SUCH_USER);
99 /* TODO: find proper status code for more than one rid found */
101 /* prepare parameters for LookupNames */
102 s->openuser.in.domain_handle = &s->domain_handle;
103 s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
104 s->openuser.in.rid = s->lookup.out.rids.ids[0];
105 s->openuser.out.user_handle = &s->user_handle;
107 /* send request */
108 openuser_req = dcerpc_samr_OpenUser_send(s->pipe, c, &s->openuser);
109 if (composite_nomem(openuser_req, c)) return;
111 composite_continue_rpc(c, openuser_req, continue_userinfo_openuser, c);
116 * Stage 2: Open user policy handle.
118 static void continue_userinfo_openuser(struct rpc_request *req)
120 struct composite_context *c;
121 struct userinfo_state *s;
122 struct rpc_request *queryuser_req;
123 struct monitor_msg msg;
124 struct msg_rpc_open_user *msg_open;
126 c = talloc_get_type(req->async.private_data, struct composite_context);
127 s = talloc_get_type(c->private_data, struct userinfo_state);
129 /* receive samr_OpenUser reply */
130 c->status = dcerpc_ndr_request_recv(req);
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;
154 /* queue rpc call, set event handling and new state */
155 queryuser_req = dcerpc_samr_QueryUserInfo_send(s->pipe, c, &s->queryuserinfo);
156 if (composite_nomem(queryuser_req, c)) return;
158 composite_continue_rpc(c, queryuser_req, continue_userinfo_getuser, c);
163 * Stage 3: Get requested user information.
165 static void continue_userinfo_getuser(struct rpc_request *req)
167 struct composite_context *c;
168 struct userinfo_state *s;
169 struct rpc_request *close_req;
170 struct monitor_msg msg;
171 struct msg_rpc_query_user *msg_query;
173 c = talloc_get_type(req->async.private_data, struct composite_context);
174 s = talloc_get_type(c->private_data, struct userinfo_state);
176 /* receive samr_QueryUserInfo reply */
177 c->status = dcerpc_ndr_request_recv(req);
178 if (!composite_is_ok(c)) return;
180 /* check if queryuser itself went ok */
181 if (!NT_STATUS_IS_OK(s->queryuserinfo.out.result)) {
182 composite_error(c, s->queryuserinfo.out.result);
183 return;
186 s->info = talloc_steal(s, s->queryuserinfo.out.info);
188 /* issue a monitor message */
189 if (s->monitor_fn) {
190 msg.type = mon_SamrQueryUser;
191 msg_query = talloc(s, struct msg_rpc_query_user);
192 msg_query->level = s->queryuserinfo.in.level;
193 msg.data = (void*)msg_query;
194 msg.data_size = sizeof(*msg_query);
196 s->monitor_fn(&msg);
199 /* prepare arguments for Close call */
200 s->samrclose.in.handle = &s->user_handle;
201 s->samrclose.out.handle = &s->user_handle;
203 /* queue rpc call, set event handling and new state */
204 close_req = dcerpc_samr_Close_send(s->pipe, c, &s->samrclose);
205 if (composite_nomem(close_req, c)) return;
207 composite_continue_rpc(c, close_req, continue_userinfo_closeuser, c);
212 * Stage 4: Close policy handle associated with opened user.
214 static void continue_userinfo_closeuser(struct rpc_request *req)
216 struct composite_context *c;
217 struct userinfo_state *s;
218 struct monitor_msg msg;
219 struct msg_rpc_close_user *msg_close;
221 c = talloc_get_type(req->async.private_data, struct composite_context);
222 s = talloc_get_type(c->private_data, struct userinfo_state);
224 /* receive samr_Close reply */
225 c->status = dcerpc_ndr_request_recv(req);
226 if (!composite_is_ok(c)) return;
228 if (!NT_STATUS_IS_OK(s->samrclose.out.result)) {
229 composite_error(c, s->samrclose.out.result);
230 return;
233 /* issue a monitor message */
234 if (s->monitor_fn) {
235 msg.type = mon_SamrClose;
236 msg_close = talloc(s, struct msg_rpc_close_user);
237 msg_close->rid = s->openuser.in.rid;
238 msg.data = (void*)msg_close;
239 msg.data_size = sizeof(*msg_close);
241 s->monitor_fn(&msg);
244 composite_done(c);
249 * Sends asynchronous userinfo request
251 * @param p dce/rpc call pipe
252 * @param io arguments and results of the call
254 struct composite_context *libnet_rpc_userinfo_send(struct dcerpc_pipe *p,
255 struct libnet_rpc_userinfo *io,
256 void (*monitor)(struct monitor_msg*))
258 struct composite_context *c;
259 struct userinfo_state *s;
260 struct dom_sid *sid;
261 struct rpc_request *openuser_req, *lookup_req;
263 if (!p || !io) return NULL;
265 c = composite_create(p, dcerpc_event_context(p));
266 if (c == NULL) return c;
268 s = talloc_zero(c, struct userinfo_state);
269 if (composite_nomem(s, c)) return c;
271 c->private_data = s;
273 s->level = io->in.level;
274 s->pipe = p;
275 s->domain_handle = io->in.domain_handle;
276 s->monitor_fn = monitor;
278 if (io->in.sid) {
279 sid = dom_sid_parse_talloc(s, io->in.sid);
280 if (composite_nomem(sid, c)) return c;
282 s->openuser.in.domain_handle = &s->domain_handle;
283 s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
284 s->openuser.in.rid = sid->sub_auths[sid->num_auths - 1];
285 s->openuser.out.user_handle = &s->user_handle;
287 /* send request */
288 openuser_req = dcerpc_samr_OpenUser_send(p, c, &s->openuser);
289 if (composite_nomem(openuser_req, c)) return c;
291 composite_continue_rpc(c, openuser_req, continue_userinfo_openuser, c);
293 } else {
294 /* preparing parameters to send rpc request */
295 s->lookup.in.domain_handle = &s->domain_handle;
296 s->lookup.in.num_names = 1;
297 s->lookup.in.names = talloc_array(s, struct lsa_String, 1);
298 if (composite_nomem(s->lookup.in.names, c)) return c;
300 s->lookup.in.names[0].string = talloc_strdup(s, io->in.username);
301 if (composite_nomem(s->lookup.in.names[0].string, c)) return c;
303 /* send request */
304 lookup_req = dcerpc_samr_LookupNames_send(p, c, &s->lookup);
305 if (composite_nomem(lookup_req, c)) return c;
307 composite_continue_rpc(c, lookup_req, continue_userinfo_lookup, c);
310 return c;
315 * Waits for and receives result of asynchronous userinfo call
317 * @param c composite context returned by asynchronous userinfo call
318 * @param mem_ctx memory context of the call
319 * @param io pointer to results (and arguments) of the call
320 * @return nt status code of execution
323 NTSTATUS libnet_rpc_userinfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
324 struct libnet_rpc_userinfo *io)
326 NTSTATUS status;
327 struct userinfo_state *s;
329 /* wait for results of sending request */
330 status = composite_wait(c);
332 if (NT_STATUS_IS_OK(status) && io) {
333 s = talloc_get_type(c->private_data, struct userinfo_state);
334 talloc_steal(mem_ctx, s->info);
335 io->out.info = *s->info;
338 /* memory context associated to composite context is no longer needed */
339 talloc_free(c);
340 return status;
345 * Synchronous version of userinfo call
347 * @param pipe dce/rpc call pipe
348 * @param mem_ctx memory context for the call
349 * @param io arguments and results of the call
350 * @return nt status code of execution
353 NTSTATUS libnet_rpc_userinfo(struct dcerpc_pipe *p,
354 TALLOC_CTX *mem_ctx,
355 struct libnet_rpc_userinfo *io)
357 struct composite_context *c = libnet_rpc_userinfo_send(p, io, NULL);
358 return libnet_rpc_userinfo_recv(c, mem_ctx, io);