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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 a composite functions for user management operations (add/del/chg)
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"
35 * Composite user add function
38 static void useradd_handler(struct rpc_request*);
40 enum useradd_stage { USERADD_CREATE };
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;
54 * Stage 1 (and the only one for now): Create user account.
56 static NTSTATUS useradd_create(struct composite_context *c,
57 struct useradd_state *s)
59 c->status = dcerpc_ndr_request_recv(s->req);
60 NT_STATUS_NOT_OK_RETURN(c->status);
62 c->state = SMBCLI_REQUEST_DONE;
68 * Event handler for asynchronous request. Handles transition through
69 * intermediate stages of the call.
71 * @param req rpc call context
73 static void useradd_handler(struct rpc_request *req)
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;
82 c->status = useradd_create(c, s);
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);
92 if (!NT_STATUS_IS_OK(c->status)) {
93 c->state = SMBCLI_REQUEST_ERROR;
100 if (c->state >= SMBCLI_REQUEST_DONE &&
108 * Sends asynchronous useradd request
110 * @param p dce/rpc call pipe
111 * @param io arguments and results of the call
114 struct composite_context *libnet_rpc_useradd_send(struct dcerpc_pipe *p,
115 struct libnet_rpc_useradd *io,
116 void (*monitor)(struct monitor_msg*))
118 struct composite_context *c;
119 struct useradd_state *s;
121 c = talloc_zero(p, struct composite_context);
122 if (c == NULL) goto failure;
124 s = talloc_zero(c, struct useradd_state);
125 if (s == NULL) goto failure;
127 s->domain_handle = io->in.domain_handle;
130 c->state = SMBCLI_REQUEST_SEND;
132 c->event_ctx = dcerpc_event_context(p);
133 c->monitor_fn = monitor;
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;
143 s->req = dcerpc_samr_CreateUser_send(p, c, &s->createuser);
145 /* callback handler */
146 s->req->async.callback = useradd_handler;
147 s->req->async.private = c;
148 s->stage = USERADD_CREATE;
159 * Waits for and receives result of asynchronous useradd call
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
167 NTSTATUS libnet_rpc_useradd_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
168 struct libnet_rpc_useradd *io)
171 struct useradd_state *s;
173 status = composite_wait(c);
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;
187 * Synchronous version of useradd call
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
195 NTSTATUS libnet_rpc_useradd(struct dcerpc_pipe *pipe,
197 struct libnet_rpc_useradd *io)
199 struct composite_context *c = libnet_rpc_useradd_send(pipe, io, NULL);
200 return libnet_rpc_useradd_recv(c, mem_ctx, io);
205 * Composite user delete function
208 static void userdel_handler(struct rpc_request*);
210 enum userdel_stage { USERDEL_LOOKUP, USERDEL_OPEN, USERDEL_DELETE };
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;
225 * Stage 1: Lookup the user name and resolve it to rid
227 static NTSTATUS userdel_lookup(struct composite_context *c,
228 struct userdel_state *s)
230 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
232 c->status = dcerpc_ndr_request_recv(s->req);
233 NT_STATUS_NOT_OK_RETURN(c->status);
235 if (!s->lookupname.out.rids.count) {
236 /* TODO: no such user */
237 status = NT_STATUS_NO_SUCH_USER;
239 } else if (!s->lookupname.out.rids.count > 1) {
240 /* TODO: ambiguous username */
241 status = NT_STATUS_INVALID_ACCOUNT_NAME;
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;
249 s->req = dcerpc_samr_OpenUser_send(s->pipe, c, &s->openuser);
251 s->req->async.callback = userdel_handler;
252 s->req->async.private = c;
253 s->stage = USERDEL_OPEN;
260 * Stage 2: Open user account.
262 static NTSTATUS userdel_open(struct composite_context *c,
263 struct userdel_state *s)
265 c->status = dcerpc_ndr_request_recv(s->req);
266 NT_STATUS_NOT_OK_RETURN(c->status);
268 s->deleteuser.in.user_handle = &s->user_handle;
269 s->deleteuser.out.user_handle = &s->user_handle;
271 s->req = dcerpc_samr_DeleteUser_send(s->pipe, c, &s->deleteuser);
273 s->req->async.callback = userdel_handler;
274 s->req->async.private = c;
275 s->stage = USERDEL_DELETE;
282 * Stage 3: Delete user account
284 static NTSTATUS userdel_delete(struct composite_context *c,
285 struct userdel_state *s)
287 c->status = dcerpc_ndr_request_recv(s->req);
288 NT_STATUS_NOT_OK_RETURN(c->status);
290 c->state = SMBCLI_REQUEST_DONE;
297 * Event handler for asynchronous request. Handles transition through
298 * intermediate stages of the call.
300 * @param req rpc call context
302 static void userdel_handler(struct rpc_request *req)
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;
312 c->status = userdel_lookup(c, s);
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);
323 c->status = userdel_open(c, s);
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);
334 c->status = userdel_delete(c, s);
336 msg.type = rpc_delete_user;
342 if (!NT_STATUS_IS_OK(c->status)) {
343 c->state = SMBCLI_REQUEST_ERROR;
350 if (c->state >= SMBCLI_REQUEST_DONE &&
358 * Sends asynchronous userdel request
360 * @param p dce/rpc call pipe
361 * @param io arguments and results of the call
364 struct composite_context *libnet_rpc_userdel_send(struct dcerpc_pipe *p,
365 struct libnet_rpc_userdel *io)
367 struct composite_context *c;
368 struct userdel_state *s;
370 c = talloc_zero(p, struct composite_context);
371 if (c == NULL) goto failure;
373 s = talloc_zero(c, struct userdel_state);
374 if (s == NULL) goto failure;
376 c->state = SMBCLI_REQUEST_SEND;
378 c->event_ctx = dcerpc_event_context(p);
381 s->domain_handle = io->in.domain_handle;
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;
389 /* send the request */
390 s->req = dcerpc_samr_LookupNames_send(p, c, &s->lookupname);
392 /* callback handler */
393 s->req->async.callback = userdel_handler;
394 s->req->async.private = c;
395 s->stage = USERDEL_LOOKUP;
406 * Waits for and receives results of asynchronous userdel call
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
414 NTSTATUS libnet_rpc_userdel_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
415 struct libnet_rpc_userdel *io)
418 struct userdel_state *s;
420 status = composite_wait(c);
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;
433 * Synchronous version of userdel call
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
441 NTSTATUS libnet_rpc_userdel(struct dcerpc_pipe *pipe,
443 struct libnet_rpc_userdel *io)
445 struct composite_context *c = libnet_rpc_userdel_send(pipe, io);
446 return libnet_rpc_userdel_recv(c, mem_ctx, io);
450 static void usermod_handler(struct rpc_request*);
452 enum usermod_stage { USERMOD_LOOKUP, USERMOD_OPEN, USERMOD_MODIFY };
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;
469 * Step 1: Lookup user name
471 static NTSTATUS usermod_lookup(struct composite_context *c,
472 struct usermod_state *s)
476 c->status = dcerpc_ndr_request_recv(s->req);
477 NT_STATUS_NOT_OK_RETURN(c->status);
479 if (!s->lookupname.out.rids.count) {
480 /* TODO: no such user */
481 status = NT_STATUS_NO_SUCH_USER;
483 } else if (!s->lookupname.out.rids.count > 1) {
484 /* TODO: ambiguous username */
485 status = NT_STATUS_INVALID_ACCOUNT_NAME;
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;
493 s->req = dcerpc_samr_OpenUser_send(s->pipe, c, &s->openuser);
495 s->req->async.callback = usermod_handler;
496 s->req->async.private = c;
497 s->stage = USERMOD_OPEN;
504 * Stage 2: Open user account
506 static NTSTATUS usermod_open(struct composite_context *c,
507 struct usermod_state *s)
509 union samr_UserInfo *i = &s->info;
512 c->status = dcerpc_ndr_request_recv(s->req);
513 NT_STATUS_NOT_OK_RETURN(c->status);
515 s->setuser.in.user_handle = &s->user_handle;
517 /* Prepare UserInfo level and data based on bitmask field */
518 if (s->change.fields) {
519 if (s->change.fields & USERMOD_FIELD_ACCOUNT_NAME) {
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;
525 s->change.fields ^= USERMOD_FIELD_ACCOUNT_NAME;
527 } else if (s->change.fields & USERMOD_FIELD_FULL_NAME) {
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;
533 s->change.fields ^= USERMOD_FIELD_FULL_NAME;
535 } else if (s->change.fields & USERMOD_FIELD_DESCRIPTION) {
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;
541 s->change.fields ^= USERMOD_FIELD_DESCRIPTION;
543 } else if (s->change.fields & USERMOD_FIELD_LOGON_SCRIPT) {
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;
549 s->change.fields ^= USERMOD_FIELD_LOGON_SCRIPT;
551 } else if (s->change.fields & USERMOD_FIELD_PROFILE_PATH) {
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;
557 s->change.fields ^= USERMOD_FIELD_PROFILE_PATH;
559 } else if (s->change.fields & USERMOD_FIELD_ACCT_EXPIRY) {
561 i->info17.acct_expiry = timeval_to_nttime(s->change.acct_expiry);
563 s->change.fields ^= USERMOD_FIELD_ACCT_EXPIRY;
567 s->setuser.in.level = level;
568 s->setuser.in.info = i;
570 s->req = dcerpc_samr_SetUserInfo_send(s->pipe, c, &s->setuser);
572 s->req->async.callback = usermod_handler;
573 s->req->async.private = c;
575 /* Get back here again unless all fields have been set */
576 if (s->change.fields) {
577 s->stage = USERMOD_OPEN;
579 s->stage = USERMOD_MODIFY;
587 * Stage 3: Set new user account data
589 static NTSTATUS usermod_modify(struct composite_context *c,
590 struct usermod_state *s)
592 c->status = dcerpc_ndr_request_recv(s->req);
593 NT_STATUS_NOT_OK_RETURN(c->status);
595 c->state = SMBCLI_REQUEST_DONE;
602 * Event handler for asynchronous request. Handles transition through
603 * intermediate stages of the call.
605 * @param req rpc call context
608 static void usermod_handler(struct rpc_request *req)
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;
616 c->status = usermod_lookup(c, s);
619 c->status = usermod_open(c, s);
622 c->status = usermod_modify(c, s);
626 if (!NT_STATUS_IS_OK(c->status)) {
627 c->state = SMBCLI_REQUEST_ERROR;
634 if (c->state >= SMBCLI_REQUEST_DONE &&
642 * Sends asynchronous usermod request
644 * @param p dce/rpc call pipe
645 * @param io arguments and results of the call
648 struct composite_context *libnet_rpc_usermod_send(struct dcerpc_pipe *p,
649 struct libnet_rpc_usermod *io)
651 struct composite_context *c;
652 struct usermod_state *s;
654 c = talloc_zero(p, struct composite_context);
655 if (c == NULL) goto failure;
657 s = talloc_zero(c, struct usermod_state);
658 if (s == NULL) goto failure;
660 c->state = SMBCLI_REQUEST_SEND;
662 c->event_ctx = dcerpc_event_context(p);
665 s->domain_handle = io->in.domain_handle;
666 s->change = io->in.change;
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;
673 s->req = dcerpc_samr_LookupNames_send(p, c, &s->lookupname);
675 s->req->async.callback = usermod_handler;
676 s->req->async.private = c;
677 s->stage = USERMOD_LOOKUP;
688 * Waits for and receives results of asynchronous usermod call
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
696 NTSTATUS libnet_rpc_usermod_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
697 struct libnet_rpc_usermod *io)
700 struct usermod_state *s;
702 status = composite_wait(c);
710 * Synchronous version of usermod call
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
718 NTSTATUS libnet_rpc_usermod(struct dcerpc_pipe *pipe,
720 struct libnet_rpc_usermod *io)
722 struct composite_context *c = libnet_rpc_usermod_send(pipe, io);
723 return libnet_rpc_usermod_recv(c, mem_ctx, io);