dsdb-acl: fix the order of special and system checks
[Samba/gebeck_regimport.git] / source4 / libnet / userinfo.c
blob75c46e477d784f6aaa1a085707d1a1216c8a3224
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 tevent_req *subreq);
49 static void continue_userinfo_openuser(struct tevent_req *subreq);
50 static void continue_userinfo_getuser(struct tevent_req *subreq);
51 static void continue_userinfo_closeuser(struct tevent_req *subreq);
54 /**
55 * Stage 1 (optional): Look for a username in SAM server.
57 static void continue_userinfo_lookup(struct tevent_req *subreq)
59 struct composite_context *c;
60 struct userinfo_state *s;
61 struct monitor_msg msg;
62 struct msg_rpc_lookup_name *msg_lookup;
64 c = tevent_req_callback_data(subreq, struct composite_context);
65 s = talloc_get_type_abort(c->private_data, struct userinfo_state);
67 /* receive samr_Lookup reply */
68 c->status = dcerpc_samr_LookupNames_r_recv(subreq, s);
69 TALLOC_FREE(subreq);
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);
95 return;
98 /* TODO: find proper status code for more than one rid found */
100 /* prepare parameters for LookupNames */
101 s->openuser.in.domain_handle = &s->domain_handle;
102 s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
103 s->openuser.in.rid = s->lookup.out.rids->ids[0];
104 s->openuser.out.user_handle = &s->user_handle;
106 /* send request */
107 subreq = dcerpc_samr_OpenUser_r_send(s, c->event_ctx,
108 s->pipe->binding_handle,
109 &s->openuser);
110 if (composite_nomem(subreq, c)) return;
112 tevent_req_set_callback(subreq, continue_userinfo_openuser, c);
117 * Stage 2: Open user policy handle.
119 static void continue_userinfo_openuser(struct tevent_req *subreq)
121 struct composite_context *c;
122 struct userinfo_state *s;
123 struct monitor_msg msg;
124 struct msg_rpc_open_user *msg_open;
126 c = tevent_req_callback_data(subreq, struct composite_context);
127 s = talloc_get_type_abort(c->private_data, struct userinfo_state);
129 /* receive samr_OpenUser reply */
130 c->status = dcerpc_samr_OpenUser_r_recv(subreq, s);
131 TALLOC_FREE(subreq);
132 if (!composite_is_ok(c)) return;
134 if (!NT_STATUS_IS_OK(s->openuser.out.result)) {
135 composite_error(c, s->openuser.out.result);
136 return;
139 /* issue a monitor message */
140 if (s->monitor_fn) {
141 msg.type = mon_SamrOpenUser;
142 msg_open = talloc(s, struct msg_rpc_open_user);
143 msg_open->rid = s->openuser.in.rid;
144 msg_open->access_mask = s->openuser.in.access_mask;
145 msg.data = (void*)msg_open;
146 msg.data_size = sizeof(*msg_open);
148 s->monitor_fn(&msg);
151 /* prepare parameters for QueryUserInfo call */
152 s->queryuserinfo.in.user_handle = &s->user_handle;
153 s->queryuserinfo.in.level = s->level;
154 s->queryuserinfo.out.info = talloc(s, union samr_UserInfo *);
155 if (composite_nomem(s->queryuserinfo.out.info, c)) return;
157 /* queue rpc call, set event handling and new state */
158 subreq = dcerpc_samr_QueryUserInfo_r_send(s, c->event_ctx,
159 s->pipe->binding_handle,
160 &s->queryuserinfo);
161 if (composite_nomem(subreq, c)) return;
163 tevent_req_set_callback(subreq, continue_userinfo_getuser, c);
168 * Stage 3: Get requested user information.
170 static void continue_userinfo_getuser(struct tevent_req *subreq)
172 struct composite_context *c;
173 struct userinfo_state *s;
174 struct monitor_msg msg;
175 struct msg_rpc_query_user *msg_query;
177 c = tevent_req_callback_data(subreq, struct composite_context);
178 s = talloc_get_type_abort(c->private_data, struct userinfo_state);
180 /* receive samr_QueryUserInfo reply */
181 c->status = dcerpc_samr_QueryUserInfo_r_recv(subreq, s);
182 TALLOC_FREE(subreq);
183 if (!composite_is_ok(c)) return;
185 /* check if queryuser itself went ok */
186 if (!NT_STATUS_IS_OK(s->queryuserinfo.out.result)) {
187 composite_error(c, s->queryuserinfo.out.result);
188 return;
191 s->info = talloc_steal(s, *(s->queryuserinfo.out.info));
193 /* issue a monitor message */
194 if (s->monitor_fn) {
195 msg.type = mon_SamrQueryUser;
196 msg_query = talloc(s, struct msg_rpc_query_user);
197 msg_query->level = s->queryuserinfo.in.level;
198 msg.data = (void*)msg_query;
199 msg.data_size = sizeof(*msg_query);
201 s->monitor_fn(&msg);
204 /* prepare arguments for Close call */
205 s->samrclose.in.handle = &s->user_handle;
206 s->samrclose.out.handle = &s->user_handle;
208 /* queue rpc call, set event handling and new state */
209 subreq = dcerpc_samr_Close_r_send(s, c->event_ctx,
210 s->pipe->binding_handle,
211 &s->samrclose);
212 if (composite_nomem(subreq, c)) return;
214 tevent_req_set_callback(subreq, continue_userinfo_closeuser, c);
219 * Stage 4: Close policy handle associated with opened user.
221 static void continue_userinfo_closeuser(struct tevent_req *subreq)
223 struct composite_context *c;
224 struct userinfo_state *s;
225 struct monitor_msg msg;
226 struct msg_rpc_close_user *msg_close;
228 c = tevent_req_callback_data(subreq, struct composite_context);
229 s = talloc_get_type_abort(c->private_data, struct userinfo_state);
231 /* receive samr_Close reply */
232 c->status = dcerpc_samr_Close_r_recv(subreq, s);
233 TALLOC_FREE(subreq);
234 if (!composite_is_ok(c)) return;
236 if (!NT_STATUS_IS_OK(s->samrclose.out.result)) {
237 composite_error(c, s->samrclose.out.result);
238 return;
241 /* issue a monitor message */
242 if (s->monitor_fn) {
243 msg.type = mon_SamrClose;
244 msg_close = talloc(s, struct msg_rpc_close_user);
245 msg_close->rid = s->openuser.in.rid;
246 msg.data = (void*)msg_close;
247 msg.data_size = sizeof(*msg_close);
249 s->monitor_fn(&msg);
252 composite_done(c);
257 * Sends asynchronous userinfo request
259 * @param p dce/rpc call pipe
260 * @param io arguments and results of the call
262 struct composite_context *libnet_rpc_userinfo_send(struct dcerpc_pipe *p,
263 TALLOC_CTX *mem_ctx,
264 struct libnet_rpc_userinfo *io,
265 void (*monitor)(struct monitor_msg*))
267 struct composite_context *c;
268 struct userinfo_state *s;
269 struct dom_sid *sid;
270 struct tevent_req *subreq;
272 if (!p || !io) return NULL;
274 c = composite_create(mem_ctx, dcerpc_event_context(p));
275 if (c == NULL) return c;
277 s = talloc_zero(c, struct userinfo_state);
278 if (composite_nomem(s, c)) return c;
280 c->private_data = s;
282 s->level = io->in.level;
283 s->pipe = p;
284 s->domain_handle = io->in.domain_handle;
285 s->monitor_fn = monitor;
287 if (io->in.sid) {
288 sid = dom_sid_parse_talloc(s, io->in.sid);
289 if (composite_nomem(sid, c)) return c;
291 s->openuser.in.domain_handle = &s->domain_handle;
292 s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
293 s->openuser.in.rid = sid->sub_auths[sid->num_auths - 1];
294 s->openuser.out.user_handle = &s->user_handle;
296 /* send request */
297 subreq = dcerpc_samr_OpenUser_r_send(s, c->event_ctx,
298 p->binding_handle,
299 &s->openuser);
300 if (composite_nomem(subreq, c)) return c;
302 tevent_req_set_callback(subreq, continue_userinfo_openuser, c);
304 } else {
305 /* preparing parameters to send rpc request */
306 s->lookup.in.domain_handle = &s->domain_handle;
307 s->lookup.in.num_names = 1;
308 s->lookup.in.names = talloc_array(s, struct lsa_String, 1);
309 if (composite_nomem(s->lookup.in.names, c)) return c;
310 s->lookup.out.rids = talloc_zero(s, struct samr_Ids);
311 s->lookup.out.types = talloc_zero(s, struct samr_Ids);
312 if (composite_nomem(s->lookup.out.rids, c)) return c;
313 if (composite_nomem(s->lookup.out.types, c)) return c;
315 s->lookup.in.names[0].string = talloc_strdup(s, io->in.username);
316 if (composite_nomem(s->lookup.in.names[0].string, c)) return c;
318 /* send request */
319 subreq = dcerpc_samr_LookupNames_r_send(s, c->event_ctx,
320 p->binding_handle,
321 &s->lookup);
322 if (composite_nomem(subreq, c)) return c;
324 tevent_req_set_callback(subreq, continue_userinfo_lookup, c);
327 return c;
332 * Waits for and receives result of asynchronous userinfo call
334 * @param c composite context returned by asynchronous userinfo call
335 * @param mem_ctx memory context of the call
336 * @param io pointer to results (and arguments) of the call
337 * @return nt status code of execution
340 NTSTATUS libnet_rpc_userinfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
341 struct libnet_rpc_userinfo *io)
343 NTSTATUS status;
344 struct userinfo_state *s;
346 /* wait for results of sending request */
347 status = composite_wait(c);
349 if (NT_STATUS_IS_OK(status) && io) {
350 s = talloc_get_type_abort(c->private_data, struct userinfo_state);
351 talloc_steal(mem_ctx, s->info);
352 io->out.info = *s->info;
355 /* memory context associated to composite context is no longer needed */
356 talloc_free(c);
357 return status;
362 * Synchronous version of userinfo call
364 * @param pipe dce/rpc call pipe
365 * @param mem_ctx memory context for the call
366 * @param io arguments and results of the call
367 * @return nt status code of execution
370 NTSTATUS libnet_rpc_userinfo(struct dcerpc_pipe *p,
371 TALLOC_CTX *mem_ctx,
372 struct libnet_rpc_userinfo *io)
374 struct composite_context *c = libnet_rpc_userinfo_send(p, mem_ctx, io, NULL);
375 return libnet_rpc_userinfo_recv(c, mem_ctx, io);