r8808: More comments.
[abartlet/samba.git/.git] / source4 / libnet / userman.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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
22   a composite functions for user management operations (add/del/chg)
23 */
24
25 #include "includes.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "libcli/composite/composite.h"
28 #include "libcli/composite/monitor.h"
29 #include "librpc/gen_ndr/ndr_samr.h"
30 #include "libnet/composite.h"
31 #include "libnet/userman.h"
32 #include "libnet/userinfo.h"
33
34 /*
35  * Composite user add function
36  */
37
38 static void useradd_handler(struct rpc_request*);
39
40 enum useradd_stage { USERADD_CREATE };
41
42 struct useradd_state {
43         enum useradd_stage       stage;
44         struct dcerpc_pipe       *pipe;
45         struct rpc_request       *req;
46         struct policy_handle     domain_handle;
47         struct samr_CreateUser   createuser;
48         struct policy_handle     user_handle;
49         uint32_t                 user_rid;
50 };
51
52
53 /**
54  * Stage 1 (and the only one for now): Create user account.
55  */
56 static NTSTATUS useradd_create(struct composite_context *c,
57                                struct useradd_state *s)
58 {
59         c->status = dcerpc_ndr_request_recv(s->req);
60         NT_STATUS_NOT_OK_RETURN(c->status);
61         
62         c->state = SMBCLI_REQUEST_DONE;
63         return NT_STATUS_OK;
64 }
65
66
67 /**
68  * Event handler for asynchronous request. Handles transition through
69  * intermediate stages of the call.
70  *
71  * @param req rpc call context
72  */
73 static void useradd_handler(struct rpc_request *req)
74 {
75         struct composite_context *c = req->async.private;
76         struct useradd_state *s = talloc_get_type(c->private, struct useradd_state);
77         struct monitor_msg msg;
78         struct msg_rpc_create_user *rpc_create;
79         
80         switch (s->stage) {
81         case USERADD_CREATE:
82                 c->status = useradd_create(c, s);
83
84                 msg.type = rpc_create_user;
85                 rpc_create = talloc(s, struct msg_rpc_create_user);
86                 rpc_create->rid = *s->createuser.out.rid;
87                 msg.data = (void*)rpc_create;
88                 msg.data_size = sizeof(*rpc_create);
89                 break;
90         }
91
92         if (!NT_STATUS_IS_OK(c->status)) {
93                 c->state = SMBCLI_REQUEST_ERROR;
94         }
95
96         if (c->monitor_fn) {
97                 c->monitor_fn(&msg);
98         }
99
100         if (c->state >= SMBCLI_REQUEST_DONE &&
101             c->async.fn) {
102                 c->async.fn(c);
103         }
104 }
105
106
107 /**
108  * Sends asynchronous useradd request
109  *
110  * @param p dce/rpc call pipe 
111  * @param io arguments and results of the call
112  */
113
114 struct composite_context *libnet_rpc_useradd_send(struct dcerpc_pipe *p,
115                                                   struct libnet_rpc_useradd *io,
116                                                   void (*monitor)(struct monitor_msg*))
117 {
118         struct composite_context *c;
119         struct useradd_state *s;
120         
121         c = talloc_zero(p, struct composite_context);
122         if (c == NULL) goto failure;
123         
124         s = talloc_zero(c, struct useradd_state);
125         if (s == NULL) goto failure;
126         
127         s->domain_handle = io->in.domain_handle;
128         s->pipe          = p;
129         
130         c->state       = SMBCLI_REQUEST_SEND;
131         c->private     = s;
132         c->event_ctx   = dcerpc_event_context(p);
133         c->monitor_fn  = monitor;
134
135         /* preparing parameters to send rpc request */
136         s->createuser.in.domain_handle         = &io->in.domain_handle;
137         s->createuser.in.account_name          = talloc_zero(c, struct lsa_String);
138         s->createuser.in.account_name->string  = talloc_strdup(c, io->in.username);
139         s->createuser.out.user_handle          = &s->user_handle;
140         s->createuser.out.rid                  = &s->user_rid;
141
142         /* send request */
143         s->req = dcerpc_samr_CreateUser_send(p, c, &s->createuser);
144
145         /* callback handler */
146         s->req->async.callback = useradd_handler;
147         s->req->async.private  = c;
148         s->stage = USERADD_CREATE;
149
150         return c;
151         
152 failure:
153         talloc_free(c);
154         return NULL;
155 }
156
157
158 /**
159  * Waits for and receives result of asynchronous useradd call
160  * 
161  * @param c composite context returned by asynchronous useradd call
162  * @param mem_ctx memory context of the call
163  * @param io pointer to results (and arguments) of the call
164  * @return nt status code of execution
165  */
166
167 NTSTATUS libnet_rpc_useradd_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
168                                     struct libnet_rpc_useradd *io)
169 {
170         NTSTATUS status;
171         struct useradd_state *s;
172         
173         status = composite_wait(c);
174         
175         if (NT_STATUS_IS_OK(status) && io) {
176                 /* get and return result of the call */
177                 s = talloc_get_type(c->private, struct useradd_state);
178                 io->out.user_handle = s->user_handle;
179         }
180
181         talloc_free(c);
182         return status;
183 }
184
185
186 /**
187  * Synchronous version of useradd call
188  *
189  * @param pipe dce/rpc call pipe
190  * @param mem_ctx memory context for the call
191  * @param io arguments and results of the call
192  * @return nt status code of execution
193  */
194
195 NTSTATUS libnet_rpc_useradd(struct dcerpc_pipe *pipe,
196                                TALLOC_CTX *mem_ctx,
197                                struct libnet_rpc_useradd *io)
198 {
199         struct composite_context *c = libnet_rpc_useradd_send(pipe, io, NULL);
200         return libnet_rpc_useradd_recv(c, mem_ctx, io);
201 }
202
203
204 /*
205  * Composite user delete function
206  */
207
208 static void userdel_handler(struct rpc_request*);
209
210 enum userdel_stage { USERDEL_LOOKUP, USERDEL_OPEN, USERDEL_DELETE };
211
212 struct userdel_state {
213         enum userdel_stage        stage;
214         struct dcerpc_pipe        *pipe;
215         struct rpc_request        *req;
216         struct policy_handle      domain_handle;
217         struct policy_handle      user_handle;
218         struct samr_LookupNames   lookupname;
219         struct samr_OpenUser      openuser;
220         struct samr_DeleteUser    deleteuser;
221 };
222
223
224 /**
225  * Stage 1: Lookup the user name and resolve it to rid
226  */
227 static NTSTATUS userdel_lookup(struct composite_context *c,
228                                struct userdel_state *s)
229 {
230         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
231
232         c->status = dcerpc_ndr_request_recv(s->req);
233         NT_STATUS_NOT_OK_RETURN(c->status);
234         
235         if (!s->lookupname.out.rids.count) {
236                 /* TODO: no such user */
237                 status = NT_STATUS_NO_SUCH_USER;
238
239         } else if (!s->lookupname.out.rids.count > 1) {
240                 /* TODO: ambiguous username */
241                 status = NT_STATUS_INVALID_ACCOUNT_NAME;
242         }
243         
244         s->openuser.in.domain_handle = &s->domain_handle;
245         s->openuser.in.rid           = s->lookupname.out.rids.ids[0];
246         s->openuser.in.access_mask   = SEC_FLAG_MAXIMUM_ALLOWED;
247         s->openuser.out.user_handle  = &s->user_handle;
248
249         s->req = dcerpc_samr_OpenUser_send(s->pipe, c, &s->openuser);
250         
251         s->req->async.callback = userdel_handler;
252         s->req->async.private  = c;
253         s->stage = USERDEL_OPEN;
254         
255         return NT_STATUS_OK;
256 }
257
258
259 /**
260  * Stage 2: Open user account.
261  */
262 static NTSTATUS userdel_open(struct composite_context *c,
263                              struct userdel_state *s)
264 {
265         c->status = dcerpc_ndr_request_recv(s->req);
266         NT_STATUS_NOT_OK_RETURN(c->status);
267         
268         s->deleteuser.in.user_handle   = &s->user_handle;
269         s->deleteuser.out.user_handle  = &s->user_handle;
270         
271         s->req = dcerpc_samr_DeleteUser_send(s->pipe, c, &s->deleteuser);
272         
273         s->req->async.callback = userdel_handler;
274         s->req->async.private  = c;
275         s->stage = USERDEL_DELETE;
276         
277         return NT_STATUS_OK;
278 }
279
280
281 /**
282  * Stage 3: Delete user account
283  */
284 static NTSTATUS userdel_delete(struct composite_context *c,
285                                struct userdel_state *s)
286 {
287         c->status = dcerpc_ndr_request_recv(s->req);
288         NT_STATUS_NOT_OK_RETURN(c->status);
289         
290         c->state = SMBCLI_REQUEST_DONE;
291
292         return NT_STATUS_OK;
293 }
294
295
296 /**
297  * Event handler for asynchronous request. Handles transition through
298  * intermediate stages of the call.
299  *
300  * @param req rpc call context
301  */
302 static void userdel_handler(struct rpc_request *req)
303 {
304         struct composite_context *c = req->async.private;
305         struct userdel_state *s = talloc_get_type(c->private, struct userdel_state);
306         struct monitor_msg msg;
307         struct msg_rpc_lookup_name *msg_lookup;
308         struct msg_rpc_open_user *msg_open;
309         
310         switch (s->stage) {
311         case USERDEL_LOOKUP:
312                 c->status = userdel_lookup(c, s);
313
314                 msg.type = rpc_lookup_name;
315                 msg_lookup = talloc(s, struct msg_rpc_lookup_name);
316                 msg_lookup->rid = s->lookupname.out.rids.ids;
317                 msg_lookup->count = s->lookupname.out.rids.count;
318                 msg.data = (void*)msg_lookup;
319                 msg.data_size = sizeof(*msg_lookup);
320                 break;
321
322         case USERDEL_OPEN:
323                 c->status = userdel_open(c, s);
324
325                 msg.type = rpc_open_user;
326                 msg_open = talloc(s, struct msg_rpc_open_user);
327                 msg_open->rid = s->openuser.in.rid;
328                 msg_open->access_mask = s->openuser.in.rid;
329                 msg.data = (void*)msg_open;
330                 msg.data_size = sizeof(*msg_open);
331                 break;
332
333         case USERDEL_DELETE:
334                 c->status = userdel_delete(c, s);
335                 
336                 msg.type = rpc_delete_user;
337                 msg.data = NULL;
338                 msg.data_size = 0;
339                 break;
340         }
341
342         if (!NT_STATUS_IS_OK(c->status)) {
343                 c->state = SMBCLI_REQUEST_ERROR;
344         }
345
346         if (c->monitor_fn) {
347                 c->monitor_fn(&msg);
348         }
349
350         if (c->state >= SMBCLI_REQUEST_DONE &&
351             c->async.fn) {
352                 c->async.fn(c);
353         }
354 }
355
356
357 /**
358  * Sends asynchronous userdel request
359  *
360  * @param p dce/rpc call pipe
361  * @param io arguments and results of the call
362  */
363
364 struct composite_context *libnet_rpc_userdel_send(struct dcerpc_pipe *p,
365                                                   struct libnet_rpc_userdel *io)
366 {
367         struct composite_context *c;
368         struct userdel_state *s;
369         
370         c = talloc_zero(p, struct composite_context);
371         if (c == NULL) goto failure;
372
373         s = talloc_zero(c, struct userdel_state);
374         if (s == NULL) goto failure;
375
376         c->state      = SMBCLI_REQUEST_SEND;
377         c->private    = s;
378         c->event_ctx  = dcerpc_event_context(p);
379
380         s->pipe          = p;
381         s->domain_handle = io->in.domain_handle;
382         
383         /* preparing parameters to send rpc request */
384         s->lookupname.in.domain_handle = &io->in.domain_handle;
385         s->lookupname.in.num_names     = 1;
386         s->lookupname.in.names         = talloc_zero(s, struct lsa_String);
387         s->lookupname.in.names->string = io->in.username;
388
389         /* send the request */
390         s->req = dcerpc_samr_LookupNames_send(p, c, &s->lookupname);
391
392         /* callback handler */
393         s->req->async.callback = userdel_handler;
394         s->req->async.private  = c;
395         s->stage = USERDEL_LOOKUP;
396
397         return c;
398
399 failure:
400         talloc_free(c);
401         return NULL;
402 }
403
404
405 /**
406  * Waits for and receives results of asynchronous userdel call
407  *
408  * @param c composite context returned by asynchronous userdel call
409  * @param mem_ctx memory context of the call
410  * @param io pointer to results (and arguments) of the call
411  * @return nt status code of execution
412  */
413
414 NTSTATUS libnet_rpc_userdel_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
415                                  struct libnet_rpc_userdel *io)
416 {
417         NTSTATUS status;
418         struct userdel_state *s;
419         
420         status = composite_wait(c);
421
422         if (NT_STATUS_IS_OK(status) && io) {
423                 s  = talloc_get_type(c->private, struct userdel_state);
424                 io->out.user_handle = s->user_handle;
425         }
426
427         talloc_free(c);
428         return status;
429 }
430
431
432 /**
433  * Synchronous version of userdel call
434  *
435  * @param pipe dce/rpc call pipe
436  * @param mem_ctx memory context for the call
437  * @param io arguments and results of the call
438  * @return nt status code of execution
439  */
440
441 NTSTATUS libnet_rpc_userdel(struct dcerpc_pipe *pipe,
442                             TALLOC_CTX *mem_ctx,
443                             struct libnet_rpc_userdel *io)
444 {
445         struct composite_context *c = libnet_rpc_userdel_send(pipe, io);
446         return libnet_rpc_userdel_recv(c, mem_ctx, io);
447 }
448
449
450 static void usermod_handler(struct rpc_request*);
451
452 enum usermod_stage { USERMOD_LOOKUP, USERMOD_OPEN, USERMOD_MODIFY };
453
454 struct usermod_state {
455         enum usermod_stage        stage;
456         struct dcerpc_pipe        *pipe;
457         struct rpc_request        *req;
458         struct policy_handle      domain_handle;
459         struct policy_handle      user_handle;
460         struct usermod_change     change;
461         union  samr_UserInfo      info;
462         struct samr_LookupNames   lookupname;
463         struct samr_OpenUser      openuser;
464         struct samr_SetUserInfo   setuser;
465 };
466
467
468 /**
469  * Step 1: Lookup user name
470  */
471 static NTSTATUS usermod_lookup(struct composite_context *c,
472                                struct usermod_state *s)
473 {
474         NTSTATUS status;
475
476         c->status = dcerpc_ndr_request_recv(s->req);
477         NT_STATUS_NOT_OK_RETURN(c->status);
478
479         if (!s->lookupname.out.rids.count) {
480                 /* TODO: no such user */
481                 status = NT_STATUS_NO_SUCH_USER;
482
483         } else if (!s->lookupname.out.rids.count > 1) {
484                 /* TODO: ambiguous username */
485                 status = NT_STATUS_INVALID_ACCOUNT_NAME;
486         }
487
488         s->openuser.in.domain_handle = &s->domain_handle;
489         s->openuser.in.rid           = s->lookupname.out.rids.ids[0];
490         s->openuser.in.access_mask   = SEC_FLAG_MAXIMUM_ALLOWED;
491         s->openuser.out.user_handle  = &s->user_handle;
492
493         s->req = dcerpc_samr_OpenUser_send(s->pipe, c, &s->openuser);
494
495         s->req->async.callback = usermod_handler;
496         s->req->async.private  = c;
497         s->stage = USERMOD_OPEN;
498         
499         return NT_STATUS_OK;
500 }
501
502
503 /**
504  * Stage 2: Open user account
505  */
506 static NTSTATUS usermod_open(struct composite_context *c,
507                              struct usermod_state *s)
508 {
509         union samr_UserInfo *i = &s->info;
510         uint16_t level;
511
512         c->status = dcerpc_ndr_request_recv(s->req);
513         NT_STATUS_NOT_OK_RETURN(c->status);
514
515         s->setuser.in.user_handle  = &s->user_handle;
516
517         /* Prepare UserInfo level and data based on bitmask field */
518         if (s->change.fields) {
519                 if (s->change.fields & USERMOD_FIELD_ACCOUNT_NAME) {
520                         level = 7;
521                         i->info7.account_name.length = 2*strlen_m(s->change.account_name);
522                         i->info7.account_name.size   = 2*strlen_m(s->change.account_name);
523                         i->info7.account_name.string = s->change.account_name;
524
525                         s->change.fields ^= USERMOD_FIELD_ACCOUNT_NAME;
526
527                 } else if (s->change.fields & USERMOD_FIELD_FULL_NAME) {
528                         level = 8;
529                         i->info8.full_name.length = 2*strlen_m(s->change.full_name);
530                         i->info8.full_name.size   = 2*strlen_m(s->change.full_name);
531                         i->info8.full_name.string = s->change.full_name;
532                         
533                         s->change.fields ^= USERMOD_FIELD_FULL_NAME;
534
535                 } else if (s->change.fields & USERMOD_FIELD_DESCRIPTION) {
536                         level = 13;
537                         i->info13.description.length = 2*strlen_m(s->change.description);
538                         i->info13.description.size   = 2*strlen_m(s->change.description);
539                         i->info13.description.string = s->change.description;
540                         
541                         s->change.fields ^= USERMOD_FIELD_DESCRIPTION;
542
543                 } else if (s->change.fields & USERMOD_FIELD_LOGON_SCRIPT) {
544                         level = 11;
545                         i->info11.logon_script.length = 2*strlen_m(s->change.logon_script);
546                         i->info11.logon_script.size   = 2*strlen_m(s->change.logon_script);
547                         i->info11.logon_script.string = s->change.logon_script;
548                         
549                         s->change.fields ^= USERMOD_FIELD_LOGON_SCRIPT;
550
551                 } else if (s->change.fields & USERMOD_FIELD_PROFILE_PATH) {
552                         level = 12;
553                         i->info12.profile_path.length = 2*strlen_m(s->change.profile_path);
554                         i->info12.profile_path.size   = 2*strlen_m(s->change.profile_path);
555                         i->info12.profile_path.string = s->change.profile_path;
556
557                         s->change.fields ^= USERMOD_FIELD_PROFILE_PATH;
558
559                 } else if (s->change.fields & USERMOD_FIELD_ACCT_EXPIRY) {
560                         level = 17;
561                         i->info17.acct_expiry = timeval_to_nttime(s->change.acct_expiry);
562
563                         s->change.fields ^= USERMOD_FIELD_ACCT_EXPIRY;
564                 }
565         }
566
567         s->setuser.in.level        = level;
568         s->setuser.in.info         = i;
569
570         s->req = dcerpc_samr_SetUserInfo_send(s->pipe, c, &s->setuser);
571
572         s->req->async.callback = usermod_handler;
573         s->req->async.private  = c;
574
575         /* Get back here again unless all fields have been set */
576         if (s->change.fields) {
577                 s->stage = USERMOD_OPEN;
578         } else {
579                 s->stage = USERMOD_MODIFY;
580         }
581
582         return NT_STATUS_OK;
583 }
584
585
586 /**
587  * Stage 3: Set new user account data
588  */
589 static NTSTATUS usermod_modify(struct composite_context *c,
590                                struct usermod_state *s)
591 {
592         c->status = dcerpc_ndr_request_recv(s->req);
593         NT_STATUS_NOT_OK_RETURN(c->status);
594
595         c->state = SMBCLI_REQUEST_DONE;
596
597         return NT_STATUS_OK;
598 }
599
600
601 /**
602  * Event handler for asynchronous request. Handles transition through
603  * intermediate stages of the call.
604  *
605  * @param req rpc call context
606  */
607
608 static void usermod_handler(struct rpc_request *req)
609 {
610         struct composite_context *c = req->async.private;
611         struct usermod_state *s = talloc_get_type(c->private, struct usermod_state);
612         struct monitor_msg msg;
613
614         switch (s->stage) {
615         case USERMOD_LOOKUP:
616                 c->status = usermod_lookup(c, s);
617                 break;
618         case USERMOD_OPEN:
619                 c->status = usermod_open(c, s);
620                 break;
621         case USERMOD_MODIFY:
622                 c->status = usermod_modify(c, s);
623                 break;
624         }
625
626         if (!NT_STATUS_IS_OK(c->status)) {
627                 c->state = SMBCLI_REQUEST_ERROR;
628         }
629
630         if (c->monitor_fn) {
631                 c->monitor_fn(&msg);
632         }
633
634         if (c->state >= SMBCLI_REQUEST_DONE &&
635             c->async.fn) {
636                 c->async.fn(c);
637         }
638 }
639
640
641 /**
642  * Sends asynchronous usermod request
643  *
644  * @param p dce/rpc call pipe
645  * @param io arguments and results of the call
646  */
647
648 struct composite_context *libnet_rpc_usermod_send(struct dcerpc_pipe *p,
649                                                   struct libnet_rpc_usermod *io)
650 {
651         struct composite_context *c;
652         struct usermod_state *s;
653         
654         c = talloc_zero(p, struct composite_context);
655         if (c == NULL) goto failure;
656
657         s = talloc_zero(c, struct usermod_state);
658         if (s == NULL) goto failure;
659
660         c->state      = SMBCLI_REQUEST_SEND;
661         c->private    = s;
662         c->event_ctx  = dcerpc_event_context(p);
663
664         s->pipe          = p;
665         s->domain_handle = io->in.domain_handle;
666         s->change        = io->in.change;
667         
668         s->lookupname.in.domain_handle = &io->in.domain_handle;
669         s->lookupname.in.num_names     = 1;
670         s->lookupname.in.names         = talloc_zero(s, struct lsa_String);
671         s->lookupname.in.names->string = io->in.username;
672         
673         s->req = dcerpc_samr_LookupNames_send(p, c, &s->lookupname);
674         
675         s->req->async.callback = usermod_handler;
676         s->req->async.private  = c;
677         s->stage = USERMOD_LOOKUP;
678
679         return c;
680
681 failure:
682         talloc_free(c);
683         return NULL;
684 }
685
686
687 /**
688  * Waits for and receives results of asynchronous usermod call
689  *
690  * @param c composite context returned by asynchronous usermod call
691  * @param mem_ctx memory context of the call
692  * @param io pointer to results (and arguments) of the call
693  * @return nt status code of execution
694  */
695
696 NTSTATUS libnet_rpc_usermod_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
697                                  struct libnet_rpc_usermod *io)
698 {
699         NTSTATUS status;
700         struct usermod_state *s;
701         
702         status = composite_wait(c);
703
704         talloc_free(c);
705         return status;
706 }
707
708
709 /**
710  * Synchronous version of usermod call
711  *
712  * @param pipe dce/rpc call pipe
713  * @param mem_ctx memory context for the call
714  * @param io arguments and results of the call
715  * @return nt status code of execution
716  */
717
718 NTSTATUS libnet_rpc_usermod(struct dcerpc_pipe *pipe,
719                             TALLOC_CTX *mem_ctx,
720                             struct libnet_rpc_usermod *io)
721 {
722         struct composite_context *c = libnet_rpc_usermod_send(pipe, io);
723         return libnet_rpc_usermod_recv(c, mem_ctx, io);
724 }