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 "librpc/gen_ndr/ndr_samr.h"
29 #include "libnet/composite.h"
31 static void useradd_handler(struct rpc_request *req);
33 enum useradd_stage { USERADD_CREATE };
35 struct useradd_state {
36 enum useradd_stage stage;
37 struct dcerpc_pipe *pipe;
38 struct rpc_request *req;
39 struct policy_handle domain_handle;
40 struct samr_CreateUser createuser;
41 struct policy_handle *user_handle;
46 * Stage 1 (and the only one for now): Create user account.
48 static NTSTATUS useradd_create(struct composite_context *c,
49 struct useradd_state *s)
51 c->status = dcerpc_ndr_request_recv(s->req);
52 NT_STATUS_NOT_OK_RETURN(c->status);
54 c->state = SMBCLI_REQUEST_DONE;
60 * Event handler for asynchronous request. Handles transition through
61 * intermediate stages of the call.
63 * @param req rpc call context
65 static void useradd_handler(struct rpc_request *req)
67 struct composite_context *c = req->async.private;
68 struct useradd_state *s = talloc_get_type(c->private, struct useradd_state);
72 c->status = useradd_create(c, s);
76 if (!NT_STATUS_IS_OK(c->status)) {
77 c->state = SMBCLI_REQUEST_ERROR;
80 if (c->state >= SMBCLI_REQUEST_DONE &&
88 * Sends asynchronous useradd request
90 * @param p dce/rpc call pipe
91 * @param io arguments and results of the call
94 struct composite_context *rpc_composite_useradd_send(struct dcerpc_pipe *p,
95 struct rpc_composite_useradd *io)
97 struct composite_context *c;
98 struct useradd_state *s;
101 c = talloc_zero(p, struct composite_context);
102 if (c == NULL) goto failure;
104 s = talloc_zero(c, struct useradd_state);
105 if (s == NULL) goto failure;
107 s->domain_handle = io->in.domain_handle;
110 c->state = SMBCLI_REQUEST_SEND;
112 c->event_ctx = dcerpc_event_context(p);
114 /* preparing parameters to send rpc request */
115 s->createuser.in.domain_handle = &io->in.domain_handle;
116 s->createuser.in.account_name = talloc_zero(c, struct samr_String);
117 s->createuser.in.account_name->string = talloc_strdup(c, io->in.username);
120 s->req = dcerpc_samr_CreateUser_send(p, c, &s->createuser);
122 /* callback handler */
123 s->req->async.callback = useradd_handler;
124 s->req->async.private = c;
125 s->stage = USERADD_CREATE;
136 * Waits for and receives result of asynchronous useradd call
138 * @param c composite context returned by asynchronous userinfo call
139 * @param mem_ctx memory context of the call
140 * @param io pointer to results (and arguments) of the call
141 * @return nt status code of execution
144 NTSTATUS rpc_composite_useradd_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
145 struct rpc_composite_useradd *io)
148 struct useradd_state *s;
150 status = composite_wait(c);
152 if (NT_STATUS_IS_OK(status) && io) {
153 s = talloc_get_type(c->private, struct useradd_state);
154 talloc_steal(mem_ctx, s->user_handle);
155 io->out.user_handle = *s->user_handle;
164 * Synchronous version of useradd call
166 * @param pipe dce/rpc call pipe
167 * @param mem_ctx memory context for the call
168 * @param io arguments and results of the call
169 * @return nt status code of execution
172 NTSTATUS rpc_composite_useradd(struct dcerpc_pipe *pipe,
174 struct rpc_composite_useradd *io)
176 struct composite_context *c = rpc_composite_useradd_send(pipe, io);
177 return rpc_composite_useradd_recv(c, mem_ctx, io);