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 "libnet/composite.h"
28 #include "libnet/userman.h"
29 #include "libnet/userinfo.h"
30 #include "librpc/gen_ndr/ndr_samr_c.h"
33 * Composite USER ADD functionality
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
;
49 /* information about the progress */
50 void (*monitor_fn
)(struct monitor_msg
*);
55 * Stage 1 (and the only one for now): Create user account.
57 static NTSTATUS
useradd_create(struct composite_context
*c
,
58 struct useradd_state
*s
)
60 c
->status
= dcerpc_ndr_request_recv(s
->req
);
61 NT_STATUS_NOT_OK_RETURN(c
->status
);
63 c
->state
= COMPOSITE_STATE_DONE
;
69 * Event handler for asynchronous request. Handles transition through
70 * intermediate stages of the call.
72 * @param req rpc call context
74 static void useradd_handler(struct rpc_request
*req
)
76 struct composite_context
*c
= req
->async
.private;
77 struct useradd_state
*s
= talloc_get_type(c
->private_data
, struct useradd_state
);
78 struct monitor_msg msg
;
79 struct msg_rpc_create_user
*rpc_create
;
83 c
->status
= useradd_create(c
, s
);
85 /* prepare a message to pass to monitor function */
86 msg
.type
= rpc_create_user
;
87 rpc_create
= talloc(s
, struct msg_rpc_create_user
);
88 rpc_create
->rid
= *s
->createuser
.out
.rid
;
89 msg
.data
= (void*)rpc_create
;
90 msg
.data_size
= sizeof(*rpc_create
);
94 /* are we ok so far ? */
95 if (!NT_STATUS_IS_OK(c
->status
)) {
96 c
->state
= COMPOSITE_STATE_ERROR
;
99 /* call monitor function provided the pointer has been passed */
104 /* are we done yet ? */
105 if (c
->state
>= COMPOSITE_STATE_DONE
&&
113 * Sends asynchronous useradd request
115 * @param p dce/rpc call pipe
116 * @param io arguments and results of the call
117 * @param monitor monitor function for providing information about the progress
120 struct composite_context
*libnet_rpc_useradd_send(struct dcerpc_pipe
*p
,
121 struct libnet_rpc_useradd
*io
,
122 void (*monitor
)(struct monitor_msg
*))
124 struct composite_context
*c
;
125 struct useradd_state
*s
;
127 /* composite allocation and setup */
128 c
= talloc_zero(p
, struct composite_context
);
129 if (c
== NULL
) return NULL
;
131 s
= talloc_zero(c
, struct useradd_state
);
132 if (composite_nomem(s
, c
)) return c
;
134 c
->state
= COMPOSITE_STATE_IN_PROGRESS
;
136 c
->event_ctx
= dcerpc_event_context(p
);
138 /* put passed arguments to the state structure */
139 s
->domain_handle
= io
->in
.domain_handle
;
141 s
->monitor_fn
= monitor
;
143 /* preparing parameters to send rpc request */
144 s
->createuser
.in
.domain_handle
= &io
->in
.domain_handle
;
145 s
->createuser
.in
.account_name
= talloc_zero(c
, struct lsa_String
);
146 s
->createuser
.in
.account_name
->string
= talloc_strdup(c
, io
->in
.username
);
147 s
->createuser
.out
.user_handle
= &s
->user_handle
;
148 s
->createuser
.out
.rid
= &s
->user_rid
;
150 /* send the request */
151 s
->req
= dcerpc_samr_CreateUser_send(p
, c
, &s
->createuser
);
152 if (composite_nomem(s
->req
, c
)) return c
;
154 /* callback handler for continuation */
155 s
->req
->async
.callback
= useradd_handler
;
156 s
->req
->async
.private = c
;
157 s
->stage
= USERADD_CREATE
;
164 * Waits for and receives result of asynchronous useradd call
166 * @param c composite context returned by asynchronous useradd call
167 * @param mem_ctx memory context of the call
168 * @param io pointer to results (and arguments) of the call
169 * @return nt status code of execution
172 NTSTATUS
libnet_rpc_useradd_recv(struct composite_context
*c
, TALLOC_CTX
*mem_ctx
,
173 struct libnet_rpc_useradd
*io
)
176 struct useradd_state
*s
;
178 status
= composite_wait(c
);
180 if (NT_STATUS_IS_OK(status
) && io
) {
181 /* get and return result of the call */
182 s
= talloc_get_type(c
->private_data
, struct useradd_state
);
183 io
->out
.user_handle
= s
->user_handle
;
192 * Synchronous version of useradd call
194 * @param pipe dce/rpc call pipe
195 * @param mem_ctx memory context for the call
196 * @param io arguments and results of the call
197 * @return nt status code of execution
200 NTSTATUS
libnet_rpc_useradd(struct dcerpc_pipe
*p
,
202 struct libnet_rpc_useradd
*io
)
204 struct composite_context
*c
= libnet_rpc_useradd_send(p
, io
, NULL
);
205 return libnet_rpc_useradd_recv(c
, mem_ctx
, io
);
211 * Composite USER DELETE functionality
214 static void userdel_handler(struct rpc_request
*);
216 enum userdel_stage
{ USERDEL_LOOKUP
, USERDEL_OPEN
, USERDEL_DELETE
};
218 struct userdel_state
{
219 enum userdel_stage stage
;
220 struct dcerpc_pipe
*pipe
;
221 struct rpc_request
*req
;
222 struct policy_handle domain_handle
;
223 struct policy_handle user_handle
;
224 struct samr_LookupNames lookupname
;
225 struct samr_OpenUser openuser
;
226 struct samr_DeleteUser deleteuser
;
228 /* information about the progress */
229 void (*monitor_fn
)(struct monitor_msg
*);
234 * Stage 1: Lookup the user name and resolve it to rid
236 static NTSTATUS
userdel_lookup(struct composite_context
*c
,
237 struct userdel_state
*s
)
239 /* receive samr_LookupNames result */
240 c
->status
= dcerpc_ndr_request_recv(s
->req
);
241 NT_STATUS_NOT_OK_RETURN(c
->status
);
243 /* what to do when there's no user account to delete
244 and what if there's more than one rid resolved */
245 if (!s
->lookupname
.out
.rids
.count
) {
246 c
->status
= NT_STATUS_NO_SUCH_USER
;
247 composite_error(c
, c
->status
);
249 } else if (!s
->lookupname
.out
.rids
.count
> 1) {
250 c
->status
= NT_STATUS_INVALID_ACCOUNT_NAME
;
251 composite_error(c
, c
->status
);
254 /* prepare the next rpc call arguments */
255 s
->openuser
.in
.domain_handle
= &s
->domain_handle
;
256 s
->openuser
.in
.rid
= s
->lookupname
.out
.rids
.ids
[0];
257 s
->openuser
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
258 s
->openuser
.out
.user_handle
= &s
->user_handle
;
260 /* send rpc request */
261 s
->req
= dcerpc_samr_OpenUser_send(s
->pipe
, c
, &s
->openuser
);
262 if (s
->req
== NULL
) return NT_STATUS_NO_MEMORY
;
264 /* callback handler setup */
265 s
->req
->async
.callback
= userdel_handler
;
266 s
->req
->async
.private = c
;
267 s
->stage
= USERDEL_OPEN
;
274 * Stage 2: Open user account.
276 static NTSTATUS
userdel_open(struct composite_context
*c
,
277 struct userdel_state
*s
)
279 /* receive samr_OpenUser result */
280 c
->status
= dcerpc_ndr_request_recv(s
->req
);
281 NT_STATUS_NOT_OK_RETURN(c
->status
);
283 /* prepare the final rpc call arguments */
284 s
->deleteuser
.in
.user_handle
= &s
->user_handle
;
285 s
->deleteuser
.out
.user_handle
= &s
->user_handle
;
287 /* send rpc request */
288 s
->req
= dcerpc_samr_DeleteUser_send(s
->pipe
, c
, &s
->deleteuser
);
289 if (s
->req
== NULL
) return NT_STATUS_NO_MEMORY
;
291 /* callback handler setup */
292 s
->req
->async
.callback
= userdel_handler
;
293 s
->req
->async
.private = c
;
294 s
->stage
= USERDEL_DELETE
;
301 * Stage 3: Delete user account
303 static NTSTATUS
userdel_delete(struct composite_context
*c
,
304 struct userdel_state
*s
)
306 /* receive samr_DeleteUser result */
307 c
->status
= dcerpc_ndr_request_recv(s
->req
);
308 NT_STATUS_NOT_OK_RETURN(c
->status
);
310 c
->state
= COMPOSITE_STATE_DONE
;
317 * Event handler for asynchronous request. Handles transition through
318 * intermediate stages of the call.
320 * @param req rpc call context
322 static void userdel_handler(struct rpc_request
*req
)
324 struct composite_context
*c
;
325 struct userdel_state
*s
;
326 struct monitor_msg msg
;
327 struct msg_rpc_lookup_name
*msg_lookup
;
328 struct msg_rpc_open_user
*msg_open
;
330 c
= talloc_get_type(req
->async
.private, struct composite_context
);
331 s
= talloc_get_type(c
->private_data
, struct userdel_state
);
335 c
->status
= userdel_lookup(c
, s
);
337 /* monitor message */
338 msg
.type
= rpc_lookup_name
;
339 msg_lookup
= talloc(s
, struct msg_rpc_lookup_name
);
341 msg_lookup
->rid
= s
->lookupname
.out
.rids
.ids
;
342 msg_lookup
->count
= s
->lookupname
.out
.rids
.count
;
343 msg
.data
= (void*)msg_lookup
;
344 msg
.data_size
= sizeof(*msg_lookup
);
348 c
->status
= userdel_open(c
, s
);
350 /* monitor message */
351 msg
.type
= rpc_open_user
;
352 msg_open
= talloc(s
, struct msg_rpc_open_user
);
354 msg_open
->rid
= s
->openuser
.in
.rid
;
355 msg_open
->access_mask
= s
->openuser
.in
.rid
;
356 msg
.data
= (void*)msg_open
;
357 msg
.data_size
= sizeof(*msg_open
);
361 c
->status
= userdel_delete(c
, s
);
363 /* monitor message */
364 msg
.type
= rpc_delete_user
;
370 /* are we ok, so far ? */
371 if (!NT_STATUS_IS_OK(c
->status
)) {
372 c
->state
= COMPOSITE_STATE_ERROR
;
375 /* call monitor function provided the pointer has been passed */
380 /* are we done yet */
381 if (c
->state
>= COMPOSITE_STATE_DONE
&&
389 * Sends asynchronous userdel request
391 * @param p dce/rpc call pipe
392 * @param io arguments and results of the call
393 * @param monitor monitor function for providing information about the progress
396 struct composite_context
*libnet_rpc_userdel_send(struct dcerpc_pipe
*p
,
397 struct libnet_rpc_userdel
*io
,
398 void (*monitor
)(struct monitor_msg
*))
400 struct composite_context
*c
;
401 struct userdel_state
*s
;
403 /* composite context allocation and setup */
404 c
= talloc_zero(p
, struct composite_context
);
405 if (c
== NULL
) return NULL
;
407 s
= talloc_zero(c
, struct userdel_state
);
408 if (composite_nomem(s
, c
)) return c
;
410 c
->state
= COMPOSITE_STATE_IN_PROGRESS
;
412 c
->event_ctx
= dcerpc_event_context(p
);
414 /* store function parameters in the state structure */
416 s
->domain_handle
= io
->in
.domain_handle
;
417 s
->monitor_fn
= monitor
;
419 /* preparing parameters to send rpc request */
420 s
->lookupname
.in
.domain_handle
= &io
->in
.domain_handle
;
421 s
->lookupname
.in
.num_names
= 1;
422 s
->lookupname
.in
.names
= talloc_zero(s
, struct lsa_String
);
423 s
->lookupname
.in
.names
->string
= io
->in
.username
;
425 /* send the request */
426 s
->req
= dcerpc_samr_LookupNames_send(p
, c
, &s
->lookupname
);
428 /* callback handler setup */
429 s
->req
->async
.callback
= userdel_handler
;
430 s
->req
->async
.private = c
;
431 s
->stage
= USERDEL_LOOKUP
;
438 * Waits for and receives results of asynchronous userdel call
440 * @param c composite context returned by asynchronous userdel call
441 * @param mem_ctx memory context of the call
442 * @param io pointer to results (and arguments) of the call
443 * @return nt status code of execution
446 NTSTATUS
libnet_rpc_userdel_recv(struct composite_context
*c
, TALLOC_CTX
*mem_ctx
,
447 struct libnet_rpc_userdel
*io
)
450 struct userdel_state
*s
;
452 status
= composite_wait(c
);
454 if (NT_STATUS_IS_OK(status
) && io
) {
455 s
= talloc_get_type(c
->private_data
, struct userdel_state
);
456 io
->out
.user_handle
= s
->user_handle
;
465 * Synchronous version of userdel call
467 * @param pipe dce/rpc call pipe
468 * @param mem_ctx memory context for the call
469 * @param io arguments and results of the call
470 * @return nt status code of execution
473 NTSTATUS
libnet_rpc_userdel(struct dcerpc_pipe
*p
,
475 struct libnet_rpc_userdel
*io
)
477 struct composite_context
*c
= libnet_rpc_userdel_send(p
, io
, NULL
);
478 return libnet_rpc_userdel_recv(c
, mem_ctx
, io
);
483 * USER MODIFY functionality
486 static void usermod_handler(struct rpc_request
*);
488 enum usermod_stage
{ USERMOD_LOOKUP
, USERMOD_OPEN
, USERMOD_QUERY
, USERMOD_MODIFY
};
490 struct usermod_state
{
491 enum usermod_stage stage
;
492 struct dcerpc_pipe
*pipe
;
493 struct rpc_request
*req
;
494 struct policy_handle domain_handle
;
495 struct policy_handle user_handle
;
496 struct usermod_change change
;
497 union samr_UserInfo info
;
498 struct samr_LookupNames lookupname
;
499 struct samr_OpenUser openuser
;
500 struct samr_SetUserInfo setuser
;
501 struct samr_QueryUserInfo queryuser
;
503 /* information about the progress */
504 void (*monitor_fn
)(struct monitor_msg
*);
509 * Step 1: Lookup user name
511 static NTSTATUS
usermod_lookup(struct composite_context
*c
,
512 struct usermod_state
*s
)
514 /* receive samr_LookupNames result */
515 c
->status
= dcerpc_ndr_request_recv(s
->req
);
516 NT_STATUS_NOT_OK_RETURN(c
->status
);
518 /* what to do when there's no user account to delete
519 and what if there's more than one rid resolved */
520 if (!s
->lookupname
.out
.rids
.count
) {
521 c
->status
= NT_STATUS_NO_SUCH_USER
;
522 c
->state
= COMPOSITE_STATE_ERROR
;
525 } else if (!s
->lookupname
.out
.rids
.count
> 1) {
526 c
->status
= NT_STATUS_INVALID_ACCOUNT_NAME
;
527 c
->state
= COMPOSITE_STATE_ERROR
;
531 /* prepare the next rpc call */
532 s
->openuser
.in
.domain_handle
= &s
->domain_handle
;
533 s
->openuser
.in
.rid
= s
->lookupname
.out
.rids
.ids
[0];
534 s
->openuser
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
535 s
->openuser
.out
.user_handle
= &s
->user_handle
;
537 /* send the rpc request */
538 s
->req
= dcerpc_samr_OpenUser_send(s
->pipe
, c
, &s
->openuser
);
540 /* callback handler setup */
541 s
->req
->async
.callback
= usermod_handler
;
542 s
->req
->async
.private = c
;
543 s
->stage
= USERMOD_OPEN
;
550 * Choose a proper level of samr_UserInfo structure depending on required
551 * change specified by means of flags field. Subsequent calls of this
552 * function are made until there's no flags set meaning that all of the
553 * changes have been made.
555 static uint32_t usermod_setfields(struct usermod_state
*s
, uint16_t *level
,
556 union samr_UserInfo
*i
)
558 if (s
->change
.fields
== 0) return s
->change
.fields
;
562 if ((s
->change
.fields
& USERMOD_FIELD_ACCOUNT_NAME
) &&
563 (*level
== 0 || *level
== 7)) {
565 i
->info7
.account_name
.string
= s
->change
.account_name
;
567 s
->change
.fields
^= USERMOD_FIELD_ACCOUNT_NAME
;
570 if ((s
->change
.fields
& USERMOD_FIELD_FULL_NAME
) &&
571 (*level
== 0 || *level
== 8)) {
573 i
->info8
.full_name
.string
= s
->change
.full_name
;
575 s
->change
.fields
^= USERMOD_FIELD_FULL_NAME
;
578 if ((s
->change
.fields
& USERMOD_FIELD_DESCRIPTION
) &&
579 (*level
== 0 || *level
== 13)) {
581 i
->info13
.description
.string
= s
->change
.description
;
583 s
->change
.fields
^= USERMOD_FIELD_DESCRIPTION
;
586 if ((s
->change
.fields
& USERMOD_FIELD_COMMENT
) &&
587 (*level
== 0 || *level
== 2)) {
590 if (s
->stage
== USERMOD_QUERY
) {
591 /* the user info is obtained, so now set the required field */
592 i
->info2
.comment
.string
= s
->change
.comment
;
593 s
->change
.fields
^= USERMOD_FIELD_COMMENT
;
596 /* we need to query the user info before setting one field in it */
597 s
->stage
= USERMOD_QUERY
;
598 return s
->change
.fields
;
602 if ((s
->change
.fields
& USERMOD_FIELD_LOGON_SCRIPT
) &&
603 (*level
== 0 || *level
== 11)) {
605 i
->info11
.logon_script
.string
= s
->change
.logon_script
;
607 s
->change
.fields
^= USERMOD_FIELD_LOGON_SCRIPT
;
610 if ((s
->change
.fields
& USERMOD_FIELD_PROFILE_PATH
) &&
611 (*level
== 0 || *level
== 12)) {
613 i
->info12
.profile_path
.string
= s
->change
.profile_path
;
615 s
->change
.fields
^= USERMOD_FIELD_PROFILE_PATH
;
618 if ((s
->change
.fields
& USERMOD_FIELD_HOME_DIRECTORY
) &&
619 (*level
== 0 || *level
== 10)) {
622 if (s
->stage
== USERMOD_QUERY
) {
623 i
->info10
.home_directory
.string
= s
->change
.home_directory
;
624 s
->change
.fields
^= USERMOD_FIELD_HOME_DIRECTORY
;
626 s
->stage
= USERMOD_QUERY
;
627 return s
->change
.fields
;
631 if ((s
->change
.fields
& USERMOD_FIELD_HOME_DRIVE
) &&
632 (*level
== 0 || *level
== 10)) {
635 if (s
->stage
== USERMOD_QUERY
) {
636 i
->info10
.home_drive
.string
= s
->change
.home_drive
;
637 s
->change
.fields
^= USERMOD_FIELD_HOME_DRIVE
;
639 s
->stage
= USERMOD_QUERY
;
640 return s
->change
.fields
;
644 if ((s
->change
.fields
& USERMOD_FIELD_ACCT_EXPIRY
) &&
645 (*level
== 0 || *level
== 17)) {
647 i
->info17
.acct_expiry
= timeval_to_nttime(s
->change
.acct_expiry
);
649 s
->change
.fields
^= USERMOD_FIELD_ACCT_EXPIRY
;
652 if ((s
->change
.fields
& USERMOD_FIELD_ACCT_FLAGS
) &&
653 (*level
== 0 || *level
== 16)) {
655 i
->info16
.acct_flags
= s
->change
.acct_flags
;
657 s
->change
.fields
^= USERMOD_FIELD_ACCT_FLAGS
;
660 /* We're going to be here back again soon unless all fields have been set */
661 if (s
->change
.fields
) {
662 s
->stage
= USERMOD_OPEN
;
664 s
->stage
= USERMOD_MODIFY
;
667 return s
->change
.fields
;
671 static NTSTATUS
usermod_change(struct composite_context
*c
,
672 struct usermod_state
*s
)
674 union samr_UserInfo
*i
= &s
->info
;
676 /* set the level to invalid value, so that unless setfields routine
677 gives it a valid value we report the error correctly */
680 /* prepare UserInfo level and data based on bitmask field */
681 s
->change
.fields
= usermod_setfields(s
, &level
, i
);
683 if (level
< 1 || level
> 26) {
684 /* apparently there's a field that the setfields routine
685 does not know how to set */
686 c
->state
= COMPOSITE_STATE_ERROR
;
687 return NT_STATUS_INVALID_PARAMETER
;
690 /* If some specific level is used to set user account data and the change
691 itself does not cover all fields then we need to query the user info
692 first, right before changing the data. Otherwise we could set required
693 fields and accidentally reset the others.
695 if (s
->stage
== USERMOD_QUERY
) {
696 s
->queryuser
.in
.user_handle
= &s
->user_handle
;
697 s
->queryuser
.in
.level
= level
;
699 /* send query user info request to retrieve complete data of
700 a particular info level */
701 s
->req
= dcerpc_samr_QueryUserInfo_send(s
->pipe
, c
, &s
->queryuser
);
704 s
->setuser
.in
.user_handle
= &s
->user_handle
;
705 s
->setuser
.in
.level
= level
;
706 s
->setuser
.in
.info
= i
;
708 /* send set user info request after making required change */
709 s
->req
= dcerpc_samr_SetUserInfo_send(s
->pipe
, c
, &s
->setuser
);
712 /* callback handler setup */
713 s
->req
->async
.callback
= usermod_handler
;
714 s
->req
->async
.private = c
;
721 * Stage 2: Open user account
723 static NTSTATUS
usermod_open(struct composite_context
*c
,
724 struct usermod_state
*s
)
726 c
->status
= dcerpc_ndr_request_recv(s
->req
);
727 NT_STATUS_NOT_OK_RETURN(c
->status
);
729 return usermod_change(c
, s
);
734 * Stage 2a (optional): Query the user information
736 static NTSTATUS
usermod_query(struct composite_context
*c
,
737 struct usermod_state
*s
)
739 union samr_UserInfo
*i
= &s
->info
;
742 /* receive samr_QueryUserInfo result */
743 c
->status
= dcerpc_ndr_request_recv(s
->req
);
744 NT_STATUS_NOT_OK_RETURN(c
->status
);
746 /* get returned user data and make a change (potentially one
748 s
->info
= *s
->queryuser
.out
.info
;
750 s
->change
.fields
= usermod_setfields(s
, &level
, i
);
752 /* prepare rpc call arguments */
753 s
->setuser
.in
.user_handle
= &s
->user_handle
;
754 s
->setuser
.in
.level
= level
;
755 s
->setuser
.in
.info
= i
;
757 /* send the rpc request */
758 s
->req
= dcerpc_samr_SetUserInfo_send(s
->pipe
, c
, &s
->setuser
);
760 /* callback handler setup */
761 s
->req
->async
.callback
= usermod_handler
;
762 s
->req
->async
.private = c
;
769 * Stage 3: Set new user account data
771 static NTSTATUS
usermod_modify(struct composite_context
*c
,
772 struct usermod_state
*s
)
774 /* receive samr_SetUserInfo result */
775 c
->status
= dcerpc_ndr_request_recv(s
->req
);
776 NT_STATUS_NOT_OK_RETURN(c
->status
);
778 NT_STATUS_NOT_OK_RETURN(s
->setuser
.out
.result
);
780 if (s
->change
.fields
== 0) {
781 /* all fields have been set - we're done */
782 c
->state
= COMPOSITE_STATE_DONE
;
784 /* something's still not changed - repeat the procedure */
785 return usermod_change(c
, s
);
793 * Event handler for asynchronous request. Handles transition through
794 * intermediate stages of the call.
796 * @param req rpc call context
799 static void usermod_handler(struct rpc_request
*req
)
801 struct composite_context
*c
;
802 struct usermod_state
*s
;
803 struct monitor_msg msg
;
804 struct msg_rpc_lookup_name
*msg_lookup
;
805 struct msg_rpc_open_user
*msg_open
;
807 c
= talloc_get_type(req
->async
.private, struct composite_context
);
808 s
= talloc_get_type(c
->private_data
, struct usermod_state
);
812 c
->status
= usermod_lookup(c
, s
);
814 if (NT_STATUS_IS_OK(c
->status
)) {
815 /* monitor message */
816 msg
.type
= rpc_lookup_name
;
817 msg_lookup
= talloc(s
, struct msg_rpc_lookup_name
);
819 msg_lookup
->rid
= s
->lookupname
.out
.rids
.ids
;
820 msg_lookup
->count
= s
->lookupname
.out
.rids
.count
;
821 msg
.data
= (void*)msg_lookup
;
822 msg
.data_size
= sizeof(*msg_lookup
);
827 c
->status
= usermod_open(c
, s
);
829 if (NT_STATUS_IS_OK(c
->status
)) {
830 /* monitor message */
831 msg
.type
= rpc_open_user
;
832 msg_open
= talloc(s
, struct msg_rpc_open_user
);
834 msg_open
->rid
= s
->openuser
.in
.rid
;
835 msg_open
->access_mask
= s
->openuser
.in
.rid
;
836 msg
.data
= (void*)msg_open
;
837 msg
.data_size
= sizeof(*msg_open
);
842 c
->status
= usermod_query(c
, s
);
844 if (NT_STATUS_IS_OK(c
->status
)) {
845 /* monitor message */
846 msg
.type
= rpc_query_user
;
853 c
->status
= usermod_modify(c
, s
);
855 if (NT_STATUS_IS_OK(c
->status
)) {
856 /* monitor message */
857 msg
.type
= rpc_set_user
;
864 /* are we ok, so far ? */
865 if (!NT_STATUS_IS_OK(c
->status
)) {
866 c
->state
= COMPOSITE_STATE_ERROR
;
869 /* call monitor function provided the pointer has been passed */
874 /* are we done yet ? */
875 if (c
->state
>= COMPOSITE_STATE_DONE
&&
883 * Sends asynchronous usermod request
885 * @param p dce/rpc call pipe
886 * @param io arguments and results of the call
887 * @param monitor monitor function for providing information about the progress
890 struct composite_context
*libnet_rpc_usermod_send(struct dcerpc_pipe
*p
,
891 struct libnet_rpc_usermod
*io
,
892 void (*monitor
)(struct monitor_msg
*))
894 struct composite_context
*c
;
895 struct usermod_state
*s
;
897 /* composite context allocation and setup */
898 c
= talloc_zero(p
, struct composite_context
);
899 if (c
== NULL
) return NULL
;
901 s
= talloc_zero(c
, struct usermod_state
);
902 if (composite_nomem(s
, c
)) return c
;
904 c
->state
= COMPOSITE_STATE_IN_PROGRESS
;
906 c
->event_ctx
= dcerpc_event_context(p
);
908 /* store parameters in the call structure */
910 s
->domain_handle
= io
->in
.domain_handle
;
911 s
->change
= io
->in
.change
;
912 s
->monitor_fn
= monitor
;
914 /* prepare rpc call arguments */
915 s
->lookupname
.in
.domain_handle
= &io
->in
.domain_handle
;
916 s
->lookupname
.in
.num_names
= 1;
917 s
->lookupname
.in
.names
= talloc_zero(s
, struct lsa_String
);
918 s
->lookupname
.in
.names
->string
= io
->in
.username
;
920 /* send the rpc request */
921 s
->req
= dcerpc_samr_LookupNames_send(p
, c
, &s
->lookupname
);
923 /* callback handler setup */
924 s
->req
->async
.callback
= usermod_handler
;
925 s
->req
->async
.private = c
;
926 s
->stage
= USERMOD_LOOKUP
;
933 * Waits for and receives results of asynchronous usermod call
935 * @param c composite context returned by asynchronous usermod call
936 * @param mem_ctx memory context of the call
937 * @param io pointer to results (and arguments) of the call
938 * @return nt status code of execution
941 NTSTATUS
libnet_rpc_usermod_recv(struct composite_context
*c
, TALLOC_CTX
*mem_ctx
,
942 struct libnet_rpc_usermod
*io
)
946 status
= composite_wait(c
);
954 * Synchronous version of usermod call
956 * @param pipe dce/rpc call pipe
957 * @param mem_ctx memory context for the call
958 * @param io arguments and results of the call
959 * @return nt status code of execution
962 NTSTATUS
libnet_rpc_usermod(struct dcerpc_pipe
*p
,
964 struct libnet_rpc_usermod
*io
)
966 struct composite_context
*c
= libnet_rpc_usermod_send(p
, io
, NULL
);
967 return libnet_rpc_usermod_recv(c
, mem_ctx
, io
);