tdb: allow reads after prepare commit
[Samba/aatanasov.git] / source4 / libnet / userinfo.c
bloba718ab9e851a1e43a5658d87782829720f15f59e
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 rpc_request *req);
49 static void continue_userinfo_openuser(struct rpc_request *req);
50 static void continue_userinfo_getuser(struct rpc_request *req);
51 static void continue_userinfo_closeuser(struct rpc_request *req);
54 /**
55 * Stage 1 (optional): Look for a username in SAM server.
57 static void continue_userinfo_lookup(struct rpc_request *req)
59 struct composite_context *c;
60 struct userinfo_state *s;
61 struct rpc_request *openuser_req;
62 struct monitor_msg msg;
63 struct msg_rpc_lookup_name *msg_lookup;
65 c = talloc_get_type(req->async.private_data, struct composite_context);
66 s = talloc_get_type(c->private_data, struct userinfo_state);
68 /* receive samr_Lookup reply */
69 c->status = dcerpc_ndr_request_recv(req);
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 == 0) {
94 composite_error(c, NT_STATUS_NO_SUCH_USER);
97 /* TODO: find proper status code for more than one rid found */
99 /* prepare parameters for LookupNames */
100 s->openuser.in.domain_handle = &s->domain_handle;
101 s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
102 s->openuser.in.rid = s->lookup.out.rids->ids[0];
103 s->openuser.out.user_handle = &s->user_handle;
105 /* send request */
106 openuser_req = dcerpc_samr_OpenUser_send(s->pipe, c, &s->openuser);
107 if (composite_nomem(openuser_req, c)) return;
109 composite_continue_rpc(c, openuser_req, continue_userinfo_openuser, c);
114 * Stage 2: Open user policy handle.
116 static void continue_userinfo_openuser(struct rpc_request *req)
118 struct composite_context *c;
119 struct userinfo_state *s;
120 struct rpc_request *queryuser_req;
121 struct monitor_msg msg;
122 struct msg_rpc_open_user *msg_open;
124 c = talloc_get_type(req->async.private_data, struct composite_context);
125 s = talloc_get_type(c->private_data, struct userinfo_state);
127 /* receive samr_OpenUser reply */
128 c->status = dcerpc_ndr_request_recv(req);
129 if (!composite_is_ok(c)) return;
131 if (!NT_STATUS_IS_OK(s->queryuserinfo.out.result)) {
132 composite_error(c, s->queryuserinfo.out.result);
133 return;
136 /* issue a monitor message */
137 if (s->monitor_fn) {
138 msg.type = mon_SamrOpenUser;
139 msg_open = talloc(s, struct msg_rpc_open_user);
140 msg_open->rid = s->openuser.in.rid;
141 msg_open->access_mask = s->openuser.in.access_mask;
142 msg.data = (void*)msg_open;
143 msg.data_size = sizeof(*msg_open);
145 s->monitor_fn(&msg);
148 /* prepare parameters for QueryUserInfo call */
149 s->queryuserinfo.in.user_handle = &s->user_handle;
150 s->queryuserinfo.in.level = s->level;
151 s->queryuserinfo.out.info = talloc(s, union samr_UserInfo *);
152 if (composite_nomem(s->queryuserinfo.out.info, c)) return;
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;
299 s->lookup.out.rids = talloc_zero(s, struct samr_Ids);
300 s->lookup.out.types = talloc_zero(s, struct samr_Ids);
301 if (composite_nomem(s->lookup.out.rids, c)) return c;
302 if (composite_nomem(s->lookup.out.types, c)) return c;
304 s->lookup.in.names[0].string = talloc_strdup(s, io->in.username);
305 if (composite_nomem(s->lookup.in.names[0].string, c)) return c;
307 /* send request */
308 lookup_req = dcerpc_samr_LookupNames_send(p, c, &s->lookup);
309 if (composite_nomem(lookup_req, c)) return c;
311 composite_continue_rpc(c, lookup_req, continue_userinfo_lookup, c);
314 return c;
319 * Waits for and receives result of asynchronous userinfo call
321 * @param c composite context returned by asynchronous userinfo call
322 * @param mem_ctx memory context of the call
323 * @param io pointer to results (and arguments) of the call
324 * @return nt status code of execution
327 NTSTATUS libnet_rpc_userinfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
328 struct libnet_rpc_userinfo *io)
330 NTSTATUS status;
331 struct userinfo_state *s;
333 /* wait for results of sending request */
334 status = composite_wait(c);
336 if (NT_STATUS_IS_OK(status) && io) {
337 s = talloc_get_type(c->private_data, struct userinfo_state);
338 talloc_steal(mem_ctx, s->info);
339 io->out.info = *s->info;
342 /* memory context associated to composite context is no longer needed */
343 talloc_free(c);
344 return status;
349 * Synchronous version of userinfo call
351 * @param pipe dce/rpc call pipe
352 * @param mem_ctx memory context for the call
353 * @param io arguments and results of the call
354 * @return nt status code of execution
357 NTSTATUS libnet_rpc_userinfo(struct dcerpc_pipe *p,
358 TALLOC_CTX *mem_ctx,
359 struct libnet_rpc_userinfo *io)
361 struct composite_context *c = libnet_rpc_userinfo_send(p, io, NULL);
362 return libnet_rpc_userinfo_recv(c, mem_ctx, io);