710154d41e195eddd5e3983dbdeb0725d19227c1
[metze/samba/wip.git] / source4 / 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 #include "libnet/libnet_proto.h"
33
34
35 struct userinfo_state {
36         struct dcerpc_pipe        *pipe;
37         struct policy_handle      domain_handle;
38         struct policy_handle      user_handle;
39         uint16_t                  level;
40         struct samr_LookupNames   lookup;
41         struct samr_OpenUser      openuser;
42         struct samr_QueryUserInfo queryuserinfo;
43         struct samr_Close         samrclose;    
44         union  samr_UserInfo      *info;
45
46         /* information about the progress */
47         void (*monitor_fn)(struct monitor_msg *);
48 };
49
50
51 static void continue_userinfo_lookup(struct rpc_request *req);
52 static void continue_userinfo_openuser(struct rpc_request *req);
53 static void continue_userinfo_getuser(struct rpc_request *req);
54 static void continue_userinfo_closeuser(struct rpc_request *req);
55
56
57 /**
58  * Stage 1 (optional): Look for a username in SAM server.
59  */
60 static void continue_userinfo_lookup(struct rpc_request *req)
61 {
62         struct composite_context *c;
63         struct userinfo_state *s;
64         struct rpc_request *openuser_req;
65         struct monitor_msg msg;
66         struct msg_rpc_lookup_name *msg_lookup;
67
68         c = talloc_get_type(req->async.private_data, struct composite_context);
69         s = talloc_get_type(c->private_data, struct userinfo_state);
70
71         /* receive samr_Lookup reply */
72         c->status = dcerpc_ndr_request_recv(req);
73         if (!composite_is_ok(c)) return;
74         
75         /* there could be a problem with name resolving itself */
76         if (!NT_STATUS_IS_OK(s->lookup.out.result)) {
77                 composite_error(c, s->lookup.out.result);
78                 return;
79         }
80
81         /* issue a monitor message */
82         if (s->monitor_fn) {
83                 msg.type = mon_SamrLookupName;
84                 msg_lookup = talloc(s, struct msg_rpc_lookup_name);
85                 msg_lookup->rid = s->lookup.out.rids->ids;
86                 msg_lookup->count = s->lookup.out.rids->count;
87                 msg.data = (void*)msg_lookup;
88                 msg.data_size = sizeof(*msg_lookup);
89                 
90                 s->monitor_fn(&msg);
91         }
92         
93
94         /* have we actually got name resolved
95            - we're looking for only one at the moment */
96         if (s->lookup.out.rids->count == 0) {
97                 composite_error(c, NT_STATUS_NO_SUCH_USER);
98         }
99
100         /* TODO: find proper status code for more than one rid found */
101
102         /* prepare parameters for LookupNames */
103         s->openuser.in.domain_handle  = &s->domain_handle;
104         s->openuser.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
105         s->openuser.in.rid            = s->lookup.out.rids->ids[0];
106         s->openuser.out.user_handle   = &s->user_handle;
107
108         /* send request */
109         openuser_req = dcerpc_samr_OpenUser_send(s->pipe, c, &s->openuser);
110         if (composite_nomem(openuser_req, c)) return;
111
112         composite_continue_rpc(c, openuser_req, continue_userinfo_openuser, c);
113 }
114
115
116 /**
117  * Stage 2: Open user policy handle.
118  */
119 static void continue_userinfo_openuser(struct rpc_request *req)
120 {
121         struct composite_context *c;
122         struct userinfo_state *s;
123         struct rpc_request *queryuser_req;
124         struct monitor_msg msg;
125         struct msg_rpc_open_user *msg_open;
126
127         c = talloc_get_type(req->async.private_data, struct composite_context);
128         s = talloc_get_type(c->private_data, struct userinfo_state);
129
130         /* receive samr_OpenUser reply */
131         c->status = dcerpc_ndr_request_recv(req);
132         if (!composite_is_ok(c)) return;
133
134         if (!NT_STATUS_IS_OK(s->queryuserinfo.out.result)) {
135                 composite_error(c, s->queryuserinfo.out.result);
136                 return;
137         }
138
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);
147                 
148                 s->monitor_fn(&msg);
149         }
150         
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;
156         
157         /* queue rpc call, set event handling and new state */
158         queryuser_req = dcerpc_samr_QueryUserInfo_send(s->pipe, c, &s->queryuserinfo);
159         if (composite_nomem(queryuser_req, c)) return;
160         
161         composite_continue_rpc(c, queryuser_req, continue_userinfo_getuser, c);
162 }
163
164
165 /**
166  * Stage 3: Get requested user information.
167  */
168 static void continue_userinfo_getuser(struct rpc_request *req)
169 {
170         struct composite_context *c;
171         struct userinfo_state *s;
172         struct rpc_request *close_req;
173         struct monitor_msg msg;
174         struct msg_rpc_query_user *msg_query;
175
176         c = talloc_get_type(req->async.private_data, struct composite_context);
177         s = talloc_get_type(c->private_data, struct userinfo_state);
178
179         /* receive samr_QueryUserInfo reply */
180         c->status = dcerpc_ndr_request_recv(req);
181         if (!composite_is_ok(c)) return;
182
183         /* check if queryuser itself went ok */
184         if (!NT_STATUS_IS_OK(s->queryuserinfo.out.result)) {
185                 composite_error(c, s->queryuserinfo.out.result);
186                 return;
187         }
188
189         s->info = talloc_steal(s, *(s->queryuserinfo.out.info));
190
191         /* issue a monitor message */
192         if (s->monitor_fn) {
193                 msg.type = mon_SamrQueryUser;
194                 msg_query = talloc(s, struct msg_rpc_query_user);
195                 msg_query->level = s->queryuserinfo.in.level;
196                 msg.data = (void*)msg_query;
197                 msg.data_size = sizeof(*msg_query);
198                 
199                 s->monitor_fn(&msg);
200         }
201         
202         /* prepare arguments for Close call */
203         s->samrclose.in.handle  = &s->user_handle;
204         s->samrclose.out.handle = &s->user_handle;
205         
206         /* queue rpc call, set event handling and new state */
207         close_req = dcerpc_samr_Close_send(s->pipe, c, &s->samrclose);
208         if (composite_nomem(close_req, c)) return;
209         
210         composite_continue_rpc(c, close_req, continue_userinfo_closeuser, c);
211 }
212
213
214 /**
215  * Stage 4: Close policy handle associated with opened user.
216  */
217 static void continue_userinfo_closeuser(struct rpc_request *req)
218 {
219         struct composite_context *c;
220         struct userinfo_state *s;
221         struct monitor_msg msg;
222         struct msg_rpc_close_user *msg_close;
223
224         c = talloc_get_type(req->async.private_data, struct composite_context);
225         s = talloc_get_type(c->private_data, struct userinfo_state);
226
227         /* receive samr_Close reply */
228         c->status = dcerpc_ndr_request_recv(req);
229         if (!composite_is_ok(c)) return;
230
231         if (!NT_STATUS_IS_OK(s->samrclose.out.result)) {
232                 composite_error(c, s->samrclose.out.result);
233                 return;
234         }
235
236         /* issue a monitor message */
237         if (s->monitor_fn) {
238                 msg.type = mon_SamrClose;
239                 msg_close = talloc(s, struct msg_rpc_close_user);
240                 msg_close->rid = s->openuser.in.rid;
241                 msg.data = (void*)msg_close;
242                 msg.data_size = sizeof(*msg_close);
243
244                 s->monitor_fn(&msg);
245         }
246
247         composite_done(c);
248 }
249
250
251 /**
252  * Sends asynchronous userinfo request
253  *
254  * @param p dce/rpc call pipe 
255  * @param io arguments and results of the call
256  */
257 struct composite_context *libnet_rpc_userinfo_send(struct dcerpc_pipe *p,
258                                                    struct libnet_rpc_userinfo *io,
259                                                    void (*monitor)(struct monitor_msg*))
260 {
261         struct composite_context *c;
262         struct userinfo_state *s;
263         struct dom_sid *sid;
264         struct rpc_request *openuser_req, *lookup_req;
265
266         if (!p || !io) return NULL;
267         
268         c = composite_create(p, dcerpc_event_context(p));
269         if (c == NULL) return c;
270         
271         s = talloc_zero(c, struct userinfo_state);
272         if (composite_nomem(s, c)) return c;
273
274         c->private_data = s;
275
276         s->level         = io->in.level;
277         s->pipe          = p;
278         s->domain_handle = io->in.domain_handle;
279         s->monitor_fn    = monitor;
280
281         if (io->in.sid) {
282                 sid = dom_sid_parse_talloc(s, io->in.sid);
283                 if (composite_nomem(sid, c)) return c;
284
285                 s->openuser.in.domain_handle  = &s->domain_handle;
286                 s->openuser.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
287                 s->openuser.in.rid            = sid->sub_auths[sid->num_auths - 1];
288                 s->openuser.out.user_handle   = &s->user_handle;
289                 
290                 /* send request */
291                 openuser_req = dcerpc_samr_OpenUser_send(p, c, &s->openuser);
292                 if (composite_nomem(openuser_req, c)) return c;
293
294                 composite_continue_rpc(c, openuser_req, continue_userinfo_openuser, c);
295
296         } else {
297                 /* preparing parameters to send rpc request */
298                 s->lookup.in.domain_handle    = &s->domain_handle;
299                 s->lookup.in.num_names        = 1;
300                 s->lookup.in.names            = talloc_array(s, struct lsa_String, 1);
301                 if (composite_nomem(s->lookup.in.names, c)) return c;
302                 s->lookup.out.rids         = talloc_zero(s, struct samr_Ids);
303                 s->lookup.out.types        = talloc_zero(s, struct samr_Ids);
304                 if (composite_nomem(s->lookup.out.rids, c)) return c;
305                 if (composite_nomem(s->lookup.out.types, c)) return c;
306
307                 s->lookup.in.names[0].string  = talloc_strdup(s, io->in.username);
308                 if (composite_nomem(s->lookup.in.names[0].string, c)) return c;
309                 
310                 /* send request */
311                 lookup_req = dcerpc_samr_LookupNames_send(p, c, &s->lookup);
312                 if (composite_nomem(lookup_req, c)) return c;
313                 
314                 composite_continue_rpc(c, lookup_req, continue_userinfo_lookup, c);
315         }
316
317         return c;
318 }
319
320
321 /**
322  * Waits for and receives result of asynchronous userinfo call
323  * 
324  * @param c composite context returned by asynchronous userinfo call
325  * @param mem_ctx memory context of the call
326  * @param io pointer to results (and arguments) of the call
327  * @return nt status code of execution
328  */
329
330 NTSTATUS libnet_rpc_userinfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
331                                   struct libnet_rpc_userinfo *io)
332 {
333         NTSTATUS status;
334         struct userinfo_state *s;
335         
336         /* wait for results of sending request */
337         status = composite_wait(c);
338         
339         if (NT_STATUS_IS_OK(status) && io) {
340                 s = talloc_get_type(c->private_data, struct userinfo_state);
341                 talloc_steal(mem_ctx, s->info);
342                 io->out.info = *s->info;
343         }
344         
345         /* memory context associated to composite context is no longer needed */
346         talloc_free(c);
347         return status;
348 }
349
350
351 /**
352  * Synchronous version of userinfo call
353  *
354  * @param pipe dce/rpc call pipe
355  * @param mem_ctx memory context for the call
356  * @param io arguments and results of the call
357  * @return nt status code of execution
358  */
359
360 NTSTATUS libnet_rpc_userinfo(struct dcerpc_pipe *p,
361                              TALLOC_CTX *mem_ctx,
362                              struct libnet_rpc_userinfo *io)
363 {
364         struct composite_context *c = libnet_rpc_userinfo_send(p, io, NULL);
365         return libnet_rpc_userinfo_recv(c, mem_ctx, io);
366 }