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/composite/composite.h"
27 #include "librpc/gen_ndr/ndr_samr.h"
28 #include "libnet/composite.h"
29 #include "libnet/userman.h"
30 #include "libnet/userinfo.h"
33 * Composite user add function
36 static void useradd_handler(struct rpc_request*);
38 enum useradd_stage { USERADD_CREATE };
40 struct useradd_state {
41 enum useradd_stage stage;
42 struct dcerpc_pipe *pipe;
43 struct rpc_request *req;
44 struct policy_handle domain_handle;
45 struct samr_CreateUser createuser;
46 struct policy_handle user_handle;
48 /* information about the progress */
49 void (*monitor_fn)(struct monitor_msg *);
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 = COMPOSITE_STATE_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_data, 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 = COMPOSITE_STATE_ERROR;
100 if (c->state >= COMPOSITE_STATE_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;
129 s->monitor_fn = monitor;
131 c->state = COMPOSITE_STATE_IN_PROGRESS;
133 c->event_ctx = dcerpc_event_context(p);
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_data, 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;
221 /* information about the progress */
222 void (*monitor_fn)(struct monitor_msg *);
227 * Stage 1: Lookup the user name and resolve it to rid
229 static NTSTATUS userdel_lookup(struct composite_context *c,
230 struct userdel_state *s)
232 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
234 c->status = dcerpc_ndr_request_recv(s->req);
235 NT_STATUS_NOT_OK_RETURN(c->status);
237 if (!s->lookupname.out.rids.count) {
238 /* TODO: no such user */
239 status = NT_STATUS_NO_SUCH_USER;
241 } else if (!s->lookupname.out.rids.count > 1) {
242 /* TODO: ambiguous username */
243 status = NT_STATUS_INVALID_ACCOUNT_NAME;
246 s->openuser.in.domain_handle = &s->domain_handle;
247 s->openuser.in.rid = s->lookupname.out.rids.ids[0];
248 s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
249 s->openuser.out.user_handle = &s->user_handle;
251 s->req = dcerpc_samr_OpenUser_send(s->pipe, c, &s->openuser);
253 s->req->async.callback = userdel_handler;
254 s->req->async.private = c;
255 s->stage = USERDEL_OPEN;
262 * Stage 2: Open user account.
264 static NTSTATUS userdel_open(struct composite_context *c,
265 struct userdel_state *s)
267 c->status = dcerpc_ndr_request_recv(s->req);
268 NT_STATUS_NOT_OK_RETURN(c->status);
270 s->deleteuser.in.user_handle = &s->user_handle;
271 s->deleteuser.out.user_handle = &s->user_handle;
273 s->req = dcerpc_samr_DeleteUser_send(s->pipe, c, &s->deleteuser);
275 s->req->async.callback = userdel_handler;
276 s->req->async.private = c;
277 s->stage = USERDEL_DELETE;
284 * Stage 3: Delete user account
286 static NTSTATUS userdel_delete(struct composite_context *c,
287 struct userdel_state *s)
289 c->status = dcerpc_ndr_request_recv(s->req);
290 NT_STATUS_NOT_OK_RETURN(c->status);
292 c->state = COMPOSITE_STATE_DONE;
299 * Event handler for asynchronous request. Handles transition through
300 * intermediate stages of the call.
302 * @param req rpc call context
304 static void userdel_handler(struct rpc_request *req)
306 struct composite_context *c = req->async.private;
307 struct userdel_state *s = talloc_get_type(c->private_data, struct userdel_state);
308 struct monitor_msg msg;
309 struct msg_rpc_lookup_name *msg_lookup;
310 struct msg_rpc_open_user *msg_open;
314 c->status = userdel_lookup(c, s);
316 msg.type = rpc_lookup_name;
317 msg_lookup = talloc(s, struct msg_rpc_lookup_name);
318 msg_lookup->rid = s->lookupname.out.rids.ids;
319 msg_lookup->count = s->lookupname.out.rids.count;
320 msg.data = (void*)msg_lookup;
321 msg.data_size = sizeof(*msg_lookup);
325 c->status = userdel_open(c, s);
327 msg.type = rpc_open_user;
328 msg_open = talloc(s, struct msg_rpc_open_user);
329 msg_open->rid = s->openuser.in.rid;
330 msg_open->access_mask = s->openuser.in.rid;
331 msg.data = (void*)msg_open;
332 msg.data_size = sizeof(*msg_open);
336 c->status = userdel_delete(c, s);
338 msg.type = rpc_delete_user;
344 if (!NT_STATUS_IS_OK(c->status)) {
345 c->state = COMPOSITE_STATE_ERROR;
352 if (c->state >= COMPOSITE_STATE_DONE &&
360 * Sends asynchronous userdel request
362 * @param p dce/rpc call pipe
363 * @param io arguments and results of the call
366 struct composite_context *libnet_rpc_userdel_send(struct dcerpc_pipe *p,
367 struct libnet_rpc_userdel *io)
369 struct composite_context *c;
370 struct userdel_state *s;
372 c = talloc_zero(p, struct composite_context);
373 if (c == NULL) goto failure;
375 s = talloc_zero(c, struct userdel_state);
376 if (s == NULL) goto failure;
378 c->state = COMPOSITE_STATE_IN_PROGRESS;
380 c->event_ctx = dcerpc_event_context(p);
383 s->domain_handle = io->in.domain_handle;
385 /* preparing parameters to send rpc request */
386 s->lookupname.in.domain_handle = &io->in.domain_handle;
387 s->lookupname.in.num_names = 1;
388 s->lookupname.in.names = talloc_zero(s, struct lsa_String);
389 s->lookupname.in.names->string = io->in.username;
391 /* send the request */
392 s->req = dcerpc_samr_LookupNames_send(p, c, &s->lookupname);
394 /* callback handler */
395 s->req->async.callback = userdel_handler;
396 s->req->async.private = c;
397 s->stage = USERDEL_LOOKUP;
408 1 * Waits for and receives results of asynchronous userdel call
410 * @param c composite context returned by asynchronous userdel call
411 * @param mem_ctx memory context of the call
412 * @param io pointer to results (and arguments) of the call
413 * @return nt status code of execution
416 NTSTATUS libnet_rpc_userdel_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
417 struct libnet_rpc_userdel *io)
420 struct userdel_state *s;
422 status = composite_wait(c);
424 if (NT_STATUS_IS_OK(status) && io) {
425 s = talloc_get_type(c->private_data, struct userdel_state);
426 io->out.user_handle = s->user_handle;
435 * Synchronous version of userdel call
437 * @param pipe dce/rpc call pipe
438 * @param mem_ctx memory context for the call
439 * @param io arguments and results of the call
440 * @return nt status code of execution
443 NTSTATUS libnet_rpc_userdel(struct dcerpc_pipe *pipe,
445 struct libnet_rpc_userdel *io)
447 struct composite_context *c = libnet_rpc_userdel_send(pipe, io);
448 return libnet_rpc_userdel_recv(c, mem_ctx, io);
452 static void usermod_handler(struct rpc_request*);
454 enum usermod_stage { USERMOD_LOOKUP, USERMOD_OPEN, USERMOD_QUERY, USERMOD_MODIFY };
456 struct usermod_state {
457 enum usermod_stage stage;
458 struct dcerpc_pipe *pipe;
459 struct rpc_request *req;
460 struct policy_handle domain_handle;
461 struct policy_handle user_handle;
462 struct usermod_change change;
463 union samr_UserInfo info;
464 struct samr_LookupNames lookupname;
465 struct samr_OpenUser openuser;
466 struct samr_SetUserInfo setuser;
467 struct samr_QueryUserInfo queryuser;
472 * Step 1: Lookup user name
474 static NTSTATUS usermod_lookup(struct composite_context *c,
475 struct usermod_state *s)
479 c->status = dcerpc_ndr_request_recv(s->req);
480 NT_STATUS_NOT_OK_RETURN(c->status);
482 if (!s->lookupname.out.rids.count) {
483 /* TODO: no such user */
484 status = NT_STATUS_NO_SUCH_USER;
486 } else if (!s->lookupname.out.rids.count > 1) {
487 /* TODO: ambiguous username */
488 status = NT_STATUS_INVALID_ACCOUNT_NAME;
491 s->openuser.in.domain_handle = &s->domain_handle;
492 s->openuser.in.rid = s->lookupname.out.rids.ids[0];
493 s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
494 s->openuser.out.user_handle = &s->user_handle;
496 s->req = dcerpc_samr_OpenUser_send(s->pipe, c, &s->openuser);
498 s->req->async.callback = usermod_handler;
499 s->req->async.private = c;
500 s->stage = USERMOD_OPEN;
506 static uint32_t usermod_setfields(struct usermod_state *s, uint16_t *level,
507 union samr_UserInfo *i)
509 if (s->change.fields) {
510 if (s->change.fields & USERMOD_FIELD_ACCOUNT_NAME) {
512 i->info7.account_name.string = s->change.account_name;
514 s->change.fields ^= USERMOD_FIELD_ACCOUNT_NAME;
516 } else if (s->change.fields & USERMOD_FIELD_FULL_NAME) {
518 i->info8.full_name.string = s->change.full_name;
520 s->change.fields ^= USERMOD_FIELD_FULL_NAME;
522 } else if (s->change.fields & USERMOD_FIELD_DESCRIPTION) {
524 i->info13.description.string = s->change.description;
526 s->change.fields ^= USERMOD_FIELD_DESCRIPTION;
528 } else if (s->change.fields & USERMOD_FIELD_COMMENT) {
531 if (s->stage == USERMOD_QUERY) {
532 /* the user info is obtained, so now set the required field */
533 i->info2.comment.string = s->change.comment;
534 s->change.fields ^= USERMOD_FIELD_COMMENT;
537 /* we need to query the user info before setting one field in it */
538 s->stage = USERMOD_QUERY;
539 return s->change.fields;
542 } else if (s->change.fields & USERMOD_FIELD_ALLOW_PASS_CHG) {
545 if (s->stage == USERMOD_QUERY) {
546 i->info3.allow_password_change = timeval_to_nttime(s->change.allow_password_change);
547 s->change.fields ^= USERMOD_FIELD_ALLOW_PASS_CHG;
550 s->stage = USERMOD_QUERY;
551 return s->change.fields;
554 } else if (s->change.fields & USERMOD_FIELD_FORCE_PASS_CHG) {
557 if (s->stage == USERMOD_QUERY) {
558 i->info3.force_password_change = timeval_to_nttime(s->change.force_password_change);
559 s->change.fields ^= USERMOD_FIELD_FORCE_PASS_CHG;
562 s->stage = USERMOD_QUERY;
563 return s->change.fields;
566 } else if (s->change.fields & USERMOD_FIELD_LOGON_SCRIPT) {
568 i->info11.logon_script.string = s->change.logon_script;
570 s->change.fields ^= USERMOD_FIELD_LOGON_SCRIPT;
572 } else if (s->change.fields & USERMOD_FIELD_PROFILE_PATH) {
574 i->info12.profile_path.string = s->change.profile_path;
576 s->change.fields ^= USERMOD_FIELD_PROFILE_PATH;
578 } else if (s->change.fields & USERMOD_FIELD_ACCT_EXPIRY) {
580 i->info17.acct_expiry = timeval_to_nttime(s->change.acct_expiry);
582 s->change.fields ^= USERMOD_FIELD_ACCT_EXPIRY;
584 } else if (s->change.fields & USERMOD_FIELD_ACCT_FLAGS) {
586 i->info16.acct_flags = s->change.acct_flags;
588 s->change.fields ^= USERMOD_FIELD_ACCT_FLAGS;
592 /* We're going to be back here again soon unless all fields have been set */
593 if (s->change.fields) {
594 s->stage = USERMOD_OPEN;
596 s->stage = USERMOD_MODIFY;
599 return s->change.fields;
604 * Stage 2: Open user account
606 static NTSTATUS usermod_open(struct composite_context *c,
607 struct usermod_state *s)
609 union samr_UserInfo *i = &s->info;
612 c->status = dcerpc_ndr_request_recv(s->req);
613 NT_STATUS_NOT_OK_RETURN(c->status);
615 /* Prepare UserInfo level and data based on bitmask field */
616 s->change.fields = usermod_setfields(s, &level, i);
618 if (s->stage == USERMOD_QUERY) {
619 s->queryuser.in.user_handle = &s->user_handle;
620 s->queryuser.in.level = level;
622 s->req = dcerpc_samr_QueryUserInfo_send(s->pipe, c, &s->queryuser);
625 s->setuser.in.user_handle = &s->user_handle;
626 s->setuser.in.level = level;
627 s->setuser.in.info = i;
629 s->req = dcerpc_samr_SetUserInfo_send(s->pipe, c, &s->setuser);
632 s->req->async.callback = usermod_handler;
633 s->req->async.private = c;
640 * Stage 2a (optional): Query the user information
642 static NTSTATUS usermod_query(struct composite_context *c,
643 struct usermod_state *s)
645 union samr_UserInfo *i = &s->info;
648 c->status = dcerpc_ndr_request_recv(s->req);
649 NT_STATUS_NOT_OK_RETURN(c->status);
651 s->info = *s->queryuser.out.info;
653 s->change.fields = usermod_setfields(s, &level, i);
655 s->setuser.in.user_handle = &s->user_handle;
656 s->setuser.in.level = level;
657 s->setuser.in.info = i;
659 s->req = dcerpc_samr_SetUserInfo_send(s->pipe, c, &s->setuser);
661 s->req->async.callback = usermod_handler;
662 s->req->async.private = c;
669 * Stage 3: Set new user account data
671 static NTSTATUS usermod_modify(struct composite_context *c,
672 struct usermod_state *s)
674 c->status = dcerpc_ndr_request_recv(s->req);
675 NT_STATUS_NOT_OK_RETURN(c->status);
677 c->state = COMPOSITE_STATE_DONE;
684 * Event handler for asynchronous request. Handles transition through
685 * intermediate stages of the call.
687 * @param req rpc call context
690 static void usermod_handler(struct rpc_request *req)
692 struct composite_context *c = req->async.private;
693 struct usermod_state *s = talloc_get_type(c->private_data, struct usermod_state);
697 c->status = usermod_lookup(c, s);
701 c->status = usermod_open(c, s);
705 c->status = usermod_query(c, s);
709 c->status = usermod_modify(c, s);
713 if (!NT_STATUS_IS_OK(c->status)) {
714 c->state = COMPOSITE_STATE_ERROR;
717 if (c->state >= COMPOSITE_STATE_DONE &&
725 * Sends asynchronous usermod request
727 * @param p dce/rpc call pipe
728 * @param io arguments and results of the call
731 struct composite_context *libnet_rpc_usermod_send(struct dcerpc_pipe *p,
732 struct libnet_rpc_usermod *io)
734 struct composite_context *c;
735 struct usermod_state *s;
737 c = talloc_zero(p, struct composite_context);
738 if (c == NULL) goto failure;
740 s = talloc_zero(c, struct usermod_state);
741 if (s == NULL) goto failure;
743 c->state = COMPOSITE_STATE_IN_PROGRESS;
745 c->event_ctx = dcerpc_event_context(p);
748 s->domain_handle = io->in.domain_handle;
749 s->change = io->in.change;
751 s->lookupname.in.domain_handle = &io->in.domain_handle;
752 s->lookupname.in.num_names = 1;
753 s->lookupname.in.names = talloc_zero(s, struct lsa_String);
754 s->lookupname.in.names->string = io->in.username;
756 s->req = dcerpc_samr_LookupNames_send(p, c, &s->lookupname);
758 s->req->async.callback = usermod_handler;
759 s->req->async.private = c;
760 s->stage = USERMOD_LOOKUP;
771 * Waits for and receives results of asynchronous usermod call
773 * @param c composite context returned by asynchronous usermod call
774 * @param mem_ctx memory context of the call
775 * @param io pointer to results (and arguments) of the call
776 * @return nt status code of execution
779 NTSTATUS libnet_rpc_usermod_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
780 struct libnet_rpc_usermod *io)
784 status = composite_wait(c);
792 * Synchronous version of usermod call
794 * @param pipe dce/rpc call pipe
795 * @param mem_ctx memory context for the call
796 * @param io arguments and results of the call
797 * @return nt status code of execution
800 NTSTATUS libnet_rpc_usermod(struct dcerpc_pipe *pipe,
802 struct libnet_rpc_usermod *io)
804 struct composite_context *c = libnet_rpc_usermod_send(pipe, io);
805 return libnet_rpc_usermod_recv(c, mem_ctx, io);