s4-libnet: Move to talloc_get_type_abort()
[kai/samba-autobuild/.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 "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"
30
31
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;
42
43         /* information about the progress */
44         void (*monitor_fn)(struct monitor_msg *);
45 };
46
47
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);
52
53
54 /**
55  * Stage 1 (optional): Look for a username in SAM server.
56  */
57 static void continue_userinfo_lookup(struct tevent_req *subreq)
58 {
59         struct composite_context *c;
60         struct userinfo_state *s;
61         struct monitor_msg msg;
62         struct msg_rpc_lookup_name *msg_lookup;
63
64         c = tevent_req_callback_data(subreq, struct composite_context);
65         s = talloc_get_type_abort(c->private_data, struct userinfo_state);
66
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;
71         
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;
76         }
77
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);
86                 
87                 s->monitor_fn(&msg);
88         }
89         
90
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         }
96
97         /* TODO: find proper status code for more than one rid found */
98
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;
104
105         /* send request */
106         subreq = dcerpc_samr_OpenUser_r_send(s, c->event_ctx,
107                                              s->pipe->binding_handle,
108                                              &s->openuser);
109         if (composite_nomem(subreq, c)) return;
110
111         tevent_req_set_callback(subreq, continue_userinfo_openuser, c);
112 }
113
114
115 /**
116  * Stage 2: Open user policy handle.
117  */
118 static void continue_userinfo_openuser(struct tevent_req *subreq)
119 {
120         struct composite_context *c;
121         struct userinfo_state *s;
122         struct monitor_msg msg;
123         struct msg_rpc_open_user *msg_open;
124
125         c = tevent_req_callback_data(subreq, struct composite_context);
126         s = talloc_get_type_abort(c->private_data, struct userinfo_state);
127
128         /* receive samr_OpenUser reply */
129         c->status = dcerpc_samr_OpenUser_r_recv(subreq, s);
130         TALLOC_FREE(subreq);
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 = 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);
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         s->queryuserinfo.out.info       = talloc(s, union samr_UserInfo *);
154         if (composite_nomem(s->queryuserinfo.out.info, c)) return;
155         
156         /* queue rpc call, set event handling and new state */
157         subreq = dcerpc_samr_QueryUserInfo_r_send(s, c->event_ctx,
158                                                   s->pipe->binding_handle,
159                                                   &s->queryuserinfo);
160         if (composite_nomem(subreq, c)) return;
161         
162         tevent_req_set_callback(subreq, continue_userinfo_getuser, c);
163 }
164
165
166 /**
167  * Stage 3: Get requested user information.
168  */
169 static void continue_userinfo_getuser(struct tevent_req *subreq)
170 {
171         struct composite_context *c;
172         struct userinfo_state *s;
173         struct monitor_msg msg;
174         struct msg_rpc_query_user *msg_query;
175
176         c = tevent_req_callback_data(subreq, struct composite_context);
177         s = talloc_get_type_abort(c->private_data, struct userinfo_state);
178
179         /* receive samr_QueryUserInfo reply */
180         c->status = dcerpc_samr_QueryUserInfo_r_recv(subreq, s);
181         TALLOC_FREE(subreq);
182         if (!composite_is_ok(c)) return;
183
184         /* check if queryuser itself went ok */
185         if (!NT_STATUS_IS_OK(s->queryuserinfo.out.result)) {
186                 composite_error(c, s->queryuserinfo.out.result);
187                 return;
188         }
189
190         s->info = talloc_steal(s, *(s->queryuserinfo.out.info));
191
192         /* issue a monitor message */
193         if (s->monitor_fn) {
194                 msg.type = mon_SamrQueryUser;
195                 msg_query = talloc(s, struct msg_rpc_query_user);
196                 msg_query->level = s->queryuserinfo.in.level;
197                 msg.data = (void*)msg_query;
198                 msg.data_size = sizeof(*msg_query);
199                 
200                 s->monitor_fn(&msg);
201         }
202         
203         /* prepare arguments for Close call */
204         s->samrclose.in.handle  = &s->user_handle;
205         s->samrclose.out.handle = &s->user_handle;
206         
207         /* queue rpc call, set event handling and new state */
208         subreq = dcerpc_samr_Close_r_send(s, c->event_ctx,
209                                           s->pipe->binding_handle,
210                                           &s->samrclose);
211         if (composite_nomem(subreq, c)) return;
212         
213         tevent_req_set_callback(subreq, continue_userinfo_closeuser, c);
214 }
215
216
217 /**
218  * Stage 4: Close policy handle associated with opened user.
219  */
220 static void continue_userinfo_closeuser(struct tevent_req *subreq)
221 {
222         struct composite_context *c;
223         struct userinfo_state *s;
224         struct monitor_msg msg;
225         struct msg_rpc_close_user *msg_close;
226
227         c = tevent_req_callback_data(subreq, struct composite_context);
228         s = talloc_get_type_abort(c->private_data, struct userinfo_state);
229
230         /* receive samr_Close reply */
231         c->status = dcerpc_samr_Close_r_recv(subreq, s);
232         TALLOC_FREE(subreq);
233         if (!composite_is_ok(c)) return;
234
235         if (!NT_STATUS_IS_OK(s->samrclose.out.result)) {
236                 composite_error(c, s->samrclose.out.result);
237                 return;
238         }
239
240         /* issue a monitor message */
241         if (s->monitor_fn) {
242                 msg.type = mon_SamrClose;
243                 msg_close = talloc(s, struct msg_rpc_close_user);
244                 msg_close->rid = s->openuser.in.rid;
245                 msg.data = (void*)msg_close;
246                 msg.data_size = sizeof(*msg_close);
247
248                 s->monitor_fn(&msg);
249         }
250
251         composite_done(c);
252 }
253
254
255 /**
256  * Sends asynchronous userinfo request
257  *
258  * @param p dce/rpc call pipe 
259  * @param io arguments and results of the call
260  */
261 struct composite_context *libnet_rpc_userinfo_send(struct dcerpc_pipe *p,
262                                                    struct libnet_rpc_userinfo *io,
263                                                    void (*monitor)(struct monitor_msg*))
264 {
265         struct composite_context *c;
266         struct userinfo_state *s;
267         struct dom_sid *sid;
268         struct tevent_req *subreq;
269
270         if (!p || !io) return NULL;
271         
272         c = composite_create(p, dcerpc_event_context(p));
273         if (c == NULL) return c;
274         
275         s = talloc_zero(c, struct userinfo_state);
276         if (composite_nomem(s, c)) return c;
277
278         c->private_data = s;
279
280         s->level         = io->in.level;
281         s->pipe          = p;
282         s->domain_handle = io->in.domain_handle;
283         s->monitor_fn    = monitor;
284
285         if (io->in.sid) {
286                 sid = dom_sid_parse_talloc(s, io->in.sid);
287                 if (composite_nomem(sid, c)) return c;
288
289                 s->openuser.in.domain_handle  = &s->domain_handle;
290                 s->openuser.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
291                 s->openuser.in.rid            = sid->sub_auths[sid->num_auths - 1];
292                 s->openuser.out.user_handle   = &s->user_handle;
293                 
294                 /* send request */
295                 subreq = dcerpc_samr_OpenUser_r_send(s, c->event_ctx,
296                                                      p->binding_handle,
297                                                      &s->openuser);
298                 if (composite_nomem(subreq, c)) return c;
299
300                 tevent_req_set_callback(subreq, continue_userinfo_openuser, c);
301
302         } else {
303                 /* preparing parameters to send rpc request */
304                 s->lookup.in.domain_handle    = &s->domain_handle;
305                 s->lookup.in.num_names        = 1;
306                 s->lookup.in.names            = talloc_array(s, struct lsa_String, 1);
307                 if (composite_nomem(s->lookup.in.names, c)) return c;
308                 s->lookup.out.rids         = talloc_zero(s, struct samr_Ids);
309                 s->lookup.out.types        = talloc_zero(s, struct samr_Ids);
310                 if (composite_nomem(s->lookup.out.rids, c)) return c;
311                 if (composite_nomem(s->lookup.out.types, c)) return c;
312
313                 s->lookup.in.names[0].string  = talloc_strdup(s, io->in.username);
314                 if (composite_nomem(s->lookup.in.names[0].string, c)) return c;
315                 
316                 /* send request */
317                 subreq = dcerpc_samr_LookupNames_r_send(s, c->event_ctx,
318                                                         p->binding_handle,
319                                                         &s->lookup);
320                 if (composite_nomem(subreq, c)) return c;
321                 
322                 tevent_req_set_callback(subreq, continue_userinfo_lookup, c);
323         }
324
325         return c;
326 }
327
328
329 /**
330  * Waits for and receives result of asynchronous userinfo call
331  * 
332  * @param c composite context returned by asynchronous userinfo call
333  * @param mem_ctx memory context of the call
334  * @param io pointer to results (and arguments) of the call
335  * @return nt status code of execution
336  */
337
338 NTSTATUS libnet_rpc_userinfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
339                                   struct libnet_rpc_userinfo *io)
340 {
341         NTSTATUS status;
342         struct userinfo_state *s;
343         
344         /* wait for results of sending request */
345         status = composite_wait(c);
346         
347         if (NT_STATUS_IS_OK(status) && io) {
348                 s = talloc_get_type_abort(c->private_data, struct userinfo_state);
349                 talloc_steal(mem_ctx, s->info);
350                 io->out.info = *s->info;
351         }
352         
353         /* memory context associated to composite context is no longer needed */
354         talloc_free(c);
355         return status;
356 }
357
358
359 /**
360  * Synchronous version of userinfo call
361  *
362  * @param pipe dce/rpc call pipe
363  * @param mem_ctx memory context for the call
364  * @param io arguments and results of the call
365  * @return nt status code of execution
366  */
367
368 NTSTATUS libnet_rpc_userinfo(struct dcerpc_pipe *p,
369                              TALLOC_CTX *mem_ctx,
370                              struct libnet_rpc_userinfo *io)
371 {
372         struct composite_context *c = libnet_rpc_userinfo_send(p, io, NULL);
373         return libnet_rpc_userinfo_recv(c, mem_ctx, io);
374 }