r23792: convert Samba4 to GPLv3
[ira/wip.git] / source / libnet / userinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Rafal Szczesniak 2005
5    
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.
10    
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.
15    
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/>.
18 */
19
20 /*
21   a composite function for getting user information via samr pipe
22 */
23
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"
32
33
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;
44
45         /* information about the progress */
46         void (*monitor_fn)(struct monitor_msg *);
47 };
48
49
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);
54
55
56 /**
57  * Stage 1 (optional): Look for a username in SAM server.
58  */
59 static void continue_userinfo_lookup(struct rpc_request *req)
60 {
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;
66
67         c = talloc_get_type(req->async.private_data, struct composite_context);
68         s = talloc_get_type(c->private_data, struct userinfo_state);
69
70         /* receive samr_Lookup reply */
71         c->status = dcerpc_ndr_request_recv(req);
72         if (!composite_is_ok(c)) return;
73         
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;
78         }
79
80         /* issue a monitor message */
81         if (s->monitor_fn) {
82                 msg.type = rpc_lookup_name;
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);
88                 
89                 s->monitor_fn(&msg);
90         }
91         
92
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);
97         }
98
99         /* TODO: find proper status code for more than one rid found */
100
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;
106
107         /* send request */
108         openuser_req = dcerpc_samr_OpenUser_send(s->pipe, c, &s->openuser);
109         if (composite_nomem(openuser_req, c)) return;
110
111         composite_continue_rpc(c, openuser_req, continue_userinfo_openuser, c);
112 }
113
114
115 /**
116  * Stage 2: Open user policy handle.
117  */
118 static void continue_userinfo_openuser(struct rpc_request *req)
119 {
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;
125
126         c = talloc_get_type(req->async.private_data, struct composite_context);
127         s = talloc_get_type(c->private_data, struct userinfo_state);
128
129         /* receive samr_OpenUser reply */
130         c->status = dcerpc_ndr_request_recv(req);
131         if (!composite_is_ok(c)) return;
132
133         if (!NT_STATUS_IS_OK(s->queryuserinfo.out.result)) {
134                 composite_error(c, s->queryuserinfo.out.result);
135                 return;
136         }
137
138         /* issue a monitor message */
139         if (s->monitor_fn) {
140                 msg.type = rpc_open_user;
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);
146                 
147                 s->monitor_fn(&msg);
148         }
149         
150         /* prepare parameters for QueryUserInfo call */
151         s->queryuserinfo.in.user_handle = &s->user_handle;
152         s->queryuserinfo.in.level       = s->level;
153         
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;
157         
158         composite_continue_rpc(c, queryuser_req, continue_userinfo_getuser, c);
159 }
160
161
162 /**
163  * Stage 3: Get requested user information.
164  */
165 static void continue_userinfo_getuser(struct rpc_request *req)
166 {
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;
172
173         c = talloc_get_type(req->async.private_data, struct composite_context);
174         s = talloc_get_type(c->private_data, struct userinfo_state);
175
176         /* receive samr_QueryUserInfo reply */
177         c->status = dcerpc_ndr_request_recv(req);
178         if (!composite_is_ok(c)) return;
179
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;
184         }
185
186         s->info = talloc_steal(s, s->queryuserinfo.out.info);
187
188         /* issue a monitor message */
189         if (s->monitor_fn) {
190                 msg.type = rpc_query_user;
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);
195                 
196                 s->monitor_fn(&msg);
197         }
198         
199         /* prepare arguments for Close call */
200         s->samrclose.in.handle  = &s->user_handle;
201         s->samrclose.out.handle = &s->user_handle;
202         
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;
206         
207         composite_continue_rpc(c, close_req, continue_userinfo_closeuser, c);
208 }
209
210
211 /**
212  * Stage 4: Close policy handle associated with opened user.
213  */
214 static void continue_userinfo_closeuser(struct rpc_request *req)
215 {
216         struct composite_context *c;
217         struct userinfo_state *s;
218         struct monitor_msg msg;
219         struct msg_rpc_close_user *msg_close;
220
221         c = talloc_get_type(req->async.private_data, struct composite_context);
222         s = talloc_get_type(c->private_data, struct userinfo_state);
223
224         /* receive samr_Close reply */
225         c->status = dcerpc_ndr_request_recv(req);
226         if (!composite_is_ok(c)) return;
227
228         if (!NT_STATUS_IS_OK(s->samrclose.out.result)) {
229                 composite_error(c, s->samrclose.out.result);
230                 return;
231         }
232
233         /* issue a monitor message */
234         if (s->monitor_fn) {
235                 msg.type = rpc_close_user;
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);
240
241                 s->monitor_fn(&msg);
242         }
243
244         composite_done(c);
245 }
246
247
248 /**
249  * Sends asynchronous userinfo request
250  *
251  * @param p dce/rpc call pipe 
252  * @param io arguments and results of the call
253  */
254 struct composite_context *libnet_rpc_userinfo_send(struct dcerpc_pipe *p,
255                                                    struct libnet_rpc_userinfo *io,
256                                                    void (*monitor)(struct monitor_msg*))
257 {
258         struct composite_context *c;
259         struct userinfo_state *s;
260         struct dom_sid *sid;
261         struct rpc_request *openuser_req, *lookup_req;
262
263         if (!p || !io) return NULL;
264         
265         c = composite_create(p, dcerpc_event_context(p));
266         if (c == NULL) return c;
267         
268         s = talloc_zero(c, struct userinfo_state);
269         if (composite_nomem(s, c)) return c;
270
271         c->private_data = s;
272
273         s->level         = io->in.level;
274         s->pipe          = p;
275         s->domain_handle = io->in.domain_handle;
276         s->monitor_fn    = monitor;
277
278         if (io->in.sid) {
279                 sid = dom_sid_parse_talloc(s, io->in.sid);
280                 if (composite_nomem(sid, c)) return c;
281
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;
286                 
287                 /* send request */
288                 openuser_req = dcerpc_samr_OpenUser_send(p, c, &s->openuser);
289                 if (composite_nomem(openuser_req, c)) return c;
290
291                 composite_continue_rpc(c, openuser_req, continue_userinfo_openuser, c);
292
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
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;
302                 
303                 /* send request */
304                 lookup_req = dcerpc_samr_LookupNames_send(p, c, &s->lookup);
305                 if (composite_nomem(lookup_req, c)) return c;
306                 
307                 composite_continue_rpc(c, lookup_req, continue_userinfo_lookup, c);
308         }
309
310         return c;
311 }
312
313
314 /**
315  * Waits for and receives result of asynchronous userinfo call
316  * 
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
321  */
322
323 NTSTATUS libnet_rpc_userinfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
324                                   struct libnet_rpc_userinfo *io)
325 {
326         NTSTATUS status;
327         struct userinfo_state *s;
328         
329         /* wait for results of sending request */
330         status = composite_wait(c);
331         
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;
336         }
337         
338         /* memory context associated to composite context is no longer needed */
339         talloc_free(c);
340         return status;
341 }
342
343
344 /**
345  * Synchronous version of userinfo call
346  *
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
351  */
352
353 NTSTATUS libnet_rpc_userinfo(struct dcerpc_pipe *p,
354                              TALLOC_CTX *mem_ctx,
355                              struct libnet_rpc_userinfo *io)
356 {
357         struct composite_context *c = libnet_rpc_userinfo_send(p, io, NULL);
358         return libnet_rpc_userinfo_recv(c, mem_ctx, io);
359 }