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_QUERY, 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;
465 struct samr_QueryUserInfo queryuser;
470 * Step 1: Lookup user name
472 static NTSTATUS usermod_lookup(struct composite_context *c,
473 struct usermod_state *s)
477 c->status = dcerpc_ndr_request_recv(s->req);
478 NT_STATUS_NOT_OK_RETURN(c->status);
480 if (!s->lookupname.out.rids.count) {
481 /* TODO: no such user */
482 status = NT_STATUS_NO_SUCH_USER;
484 } else if (!s->lookupname.out.rids.count > 1) {
485 /* TODO: ambiguous username */
486 status = NT_STATUS_INVALID_ACCOUNT_NAME;
489 s->openuser.in.domain_handle = &s->domain_handle;
490 s->openuser.in.rid = s->lookupname.out.rids.ids[0];
491 s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
492 s->openuser.out.user_handle = &s->user_handle;
494 s->req = dcerpc_samr_OpenUser_send(s->pipe, c, &s->openuser);
496 s->req->async.callback = usermod_handler;
497 s->req->async.private = c;
498 s->stage = USERMOD_OPEN;
504 static uint32_t usermod_setfields(struct usermod_state *s, uint16_t *level,
505 union samr_UserInfo *i)
507 if (s->change.fields) {
508 if (s->change.fields & USERMOD_FIELD_ACCOUNT_NAME) {
510 i->info7.account_name.string = s->change.account_name;
512 s->change.fields ^= USERMOD_FIELD_ACCOUNT_NAME;
514 } else if (s->change.fields & USERMOD_FIELD_FULL_NAME) {
516 i->info8.full_name.string = s->change.full_name;
518 s->change.fields ^= USERMOD_FIELD_FULL_NAME;
520 } else if (s->change.fields & USERMOD_FIELD_DESCRIPTION) {
522 i->info13.description.string = s->change.description;
524 s->change.fields ^= USERMOD_FIELD_DESCRIPTION;
526 } else if (s->change.fields & USERMOD_FIELD_COMMENT) {
529 if (s->stage == USERMOD_QUERY) {
530 /* the user info is obtained, so now set the required field */
531 i->info2.comment.string = s->change.comment;
532 s->change.fields ^= USERMOD_FIELD_COMMENT;
535 /* we need to query the user info before setting one field in it */
536 s->stage = USERMOD_QUERY;
537 return s->change.fields;
540 } else if (s->change.fields & USERMOD_FIELD_ALLOW_PASS_CHG) {
543 if (s->stage == USERMOD_QUERY) {
544 i->info3.allow_password_change = timeval_to_nttime(s->change.allow_password_change);
545 s->change.fields ^= USERMOD_FIELD_ALLOW_PASS_CHG;
548 s->stage = USERMOD_QUERY;
549 return s->change.fields;
552 } else if (s->change.fields & USERMOD_FIELD_FORCE_PASS_CHG) {
555 if (s->stage == USERMOD_QUERY) {
556 i->info3.force_password_change = timeval_to_nttime(s->change.force_password_change);
557 s->change.fields ^= USERMOD_FIELD_FORCE_PASS_CHG;
560 s->stage = USERMOD_QUERY;
561 return s->change.fields;
564 } else if (s->change.fields & USERMOD_FIELD_LOGON_SCRIPT) {
566 i->info11.logon_script.string = s->change.logon_script;
568 s->change.fields ^= USERMOD_FIELD_LOGON_SCRIPT;
570 } else if (s->change.fields & USERMOD_FIELD_PROFILE_PATH) {
572 i->info12.profile_path.string = s->change.profile_path;
574 s->change.fields ^= USERMOD_FIELD_PROFILE_PATH;
576 } else if (s->change.fields & USERMOD_FIELD_ACCT_EXPIRY) {
578 i->info17.acct_expiry = timeval_to_nttime(s->change.acct_expiry);
580 s->change.fields ^= USERMOD_FIELD_ACCT_EXPIRY;
582 } else if (s->change.fields & USERMOD_FIELD_ACCT_FLAGS) {
584 i->info16.acct_flags = s->change.acct_flags;
586 s->change.fields ^= USERMOD_FIELD_ACCT_FLAGS;
590 /* We're going to be back here again soon unless all fields have been set */
591 if (s->change.fields) {
592 s->stage = USERMOD_OPEN;
594 s->stage = USERMOD_MODIFY;
597 return s->change.fields;
602 * Stage 2: Open user account
604 static NTSTATUS usermod_open(struct composite_context *c,
605 struct usermod_state *s)
607 union samr_UserInfo *i = &s->info;
610 c->status = dcerpc_ndr_request_recv(s->req);
611 NT_STATUS_NOT_OK_RETURN(c->status);
613 /* Prepare UserInfo level and data based on bitmask field */
614 s->change.fields = usermod_setfields(s, &level, i);
616 if (s->stage == USERMOD_QUERY) {
617 s->queryuser.in.user_handle = &s->user_handle;
618 s->queryuser.in.level = level;
620 s->req = dcerpc_samr_QueryUserInfo_send(s->pipe, c, &s->queryuser);
623 s->setuser.in.user_handle = &s->user_handle;
624 s->setuser.in.level = level;
625 s->setuser.in.info = i;
627 s->req = dcerpc_samr_SetUserInfo_send(s->pipe, c, &s->setuser);
630 s->req->async.callback = usermod_handler;
631 s->req->async.private = c;
638 * Stage 2a (optional): Query the user information
640 static NTSTATUS usermod_query(struct composite_context *c,
641 struct usermod_state *s)
643 union samr_UserInfo *i = &s->info;
646 c->status = dcerpc_ndr_request_recv(s->req);
647 NT_STATUS_NOT_OK_RETURN(c->status);
649 s->info = *s->queryuser.out.info;
651 s->change.fields = usermod_setfields(s, &level, i);
653 s->setuser.in.user_handle = &s->user_handle;
654 s->setuser.in.level = level;
655 s->setuser.in.info = i;
657 s->req = dcerpc_samr_SetUserInfo_send(s->pipe, c, &s->setuser);
659 s->req->async.callback = usermod_handler;
660 s->req->async.private = c;
667 * Stage 3: Set new user account data
669 static NTSTATUS usermod_modify(struct composite_context *c,
670 struct usermod_state *s)
672 c->status = dcerpc_ndr_request_recv(s->req);
673 NT_STATUS_NOT_OK_RETURN(c->status);
675 c->state = SMBCLI_REQUEST_DONE;
682 * Event handler for asynchronous request. Handles transition through
683 * intermediate stages of the call.
685 * @param req rpc call context
688 static void usermod_handler(struct rpc_request *req)
690 struct composite_context *c = req->async.private;
691 struct usermod_state *s = talloc_get_type(c->private, struct usermod_state);
692 struct monitor_msg msg;
696 c->status = usermod_lookup(c, s);
700 c->status = usermod_open(c, s);
704 c->status = usermod_query(c, s);
708 c->status = usermod_modify(c, s);
712 if (!NT_STATUS_IS_OK(c->status)) {
713 c->state = SMBCLI_REQUEST_ERROR;
720 if (c->state >= SMBCLI_REQUEST_DONE &&
728 * Sends asynchronous usermod request
730 * @param p dce/rpc call pipe
731 * @param io arguments and results of the call
734 struct composite_context *libnet_rpc_usermod_send(struct dcerpc_pipe *p,
735 struct libnet_rpc_usermod *io)
737 struct composite_context *c;
738 struct usermod_state *s;
740 c = talloc_zero(p, struct composite_context);
741 if (c == NULL) goto failure;
743 s = talloc_zero(c, struct usermod_state);
744 if (s == NULL) goto failure;
746 c->state = SMBCLI_REQUEST_SEND;
748 c->event_ctx = dcerpc_event_context(p);
751 s->domain_handle = io->in.domain_handle;
752 s->change = io->in.change;
754 s->lookupname.in.domain_handle = &io->in.domain_handle;
755 s->lookupname.in.num_names = 1;
756 s->lookupname.in.names = talloc_zero(s, struct lsa_String);
757 s->lookupname.in.names->string = io->in.username;
759 s->req = dcerpc_samr_LookupNames_send(p, c, &s->lookupname);
761 s->req->async.callback = usermod_handler;
762 s->req->async.private = c;
763 s->stage = USERMOD_LOOKUP;
774 * Waits for and receives results of asynchronous usermod call
776 * @param c composite context returned by asynchronous usermod call
777 * @param mem_ctx memory context of the call
778 * @param io pointer to results (and arguments) of the call
779 * @return nt status code of execution
782 NTSTATUS libnet_rpc_usermod_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
783 struct libnet_rpc_usermod *io)
787 status = composite_wait(c);
795 * Synchronous version of usermod call
797 * @param pipe dce/rpc call pipe
798 * @param mem_ctx memory context for the call
799 * @param io arguments and results of the call
800 * @return nt status code of execution
803 NTSTATUS libnet_rpc_usermod(struct dcerpc_pipe *pipe,
805 struct libnet_rpc_usermod *io)
807 struct composite_context *c = libnet_rpc_usermod_send(pipe, io);
808 return libnet_rpc_usermod_recv(c, mem_ctx, io);