r6381: Started working on user account management functions.
[ira/wip.git] / source4 / libnet / userman.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Rafal Szczesniak 2005
5    
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.
10    
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.
15    
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.
19 */
20
21 /*
22   a composite functions for user management operations (add/del/chg)
23 */
24
25 #include "includes.h"
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"
30
31 static void useradd_handler(struct rpc_request *req);
32
33 enum useradd_stage { USERADD_CREATE };
34
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;
42 };
43
44
45 /**
46  * Stage 1 (and the only one for now): Create user account.
47  */
48 static NTSTATUS useradd_create(struct composite_context *c,
49                                struct useradd_state *s)
50 {
51         c->status = dcerpc_ndr_request_recv(s->req);
52         NT_STATUS_NOT_OK_RETURN(c->status);
53         
54         c->state = SMBCLI_REQUEST_DONE;
55         return NT_STATUS_OK;
56 }
57
58
59 /**
60  * Event handler for asynchronous request. Handles transition through
61  * intermediate stages of the call.
62  *
63  * @param req rpc call context
64  */
65 static void useradd_handler(struct rpc_request *req)
66 {
67         struct composite_context *c = req->async.private;
68         struct useradd_state *s = talloc_get_type(c->private, struct useradd_state);
69         
70         switch (s->stage) {
71         case USERADD_CREATE:
72                 c->status = useradd_create(c, s);
73                 break;
74         }
75
76         if (!NT_STATUS_IS_OK(c->status)) {
77                 c->state = SMBCLI_REQUEST_ERROR;
78         }
79
80         if (c->state >= SMBCLI_REQUEST_DONE &&
81             c->async.fn) {
82                 c->async.fn(c);
83         }
84 }
85
86
87 /**
88  * Sends asynchronous useradd request
89  *
90  * @param p dce/rpc call pipe 
91  * @param io arguments and results of the call
92  */
93
94 struct composite_context *rpc_composite_useradd_send(struct dcerpc_pipe *p,
95                                                      struct rpc_composite_useradd *io)
96 {
97         struct composite_context *c;
98         struct useradd_state *s;
99         struct dom_sid *sid;
100         
101         c = talloc_zero(p, struct composite_context);
102         if (c == NULL) goto failure;
103         
104         s = talloc_zero(c, struct useradd_state);
105         if (s == NULL) goto failure;
106
107         s->domain_handle = io->in.domain_handle;
108         s->pipe          = p;
109         
110         c->state     = SMBCLI_REQUEST_SEND;
111         c->private   = s;
112         c->event_ctx = dcerpc_event_context(p);
113
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);
118
119         /* send request */
120         s->req = dcerpc_samr_CreateUser_send(p, c, &s->createuser);
121
122         /* callback handler */
123         s->req->async.callback = useradd_handler;
124         s->req->async.private  = c;
125         s->stage = USERADD_CREATE;
126
127         return c;
128         
129 failure:
130         talloc_free(c);
131         return NULL;
132 }
133
134
135 /**
136  * Waits for and receives result of asynchronous useradd call
137  * 
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
142  */
143
144 NTSTATUS rpc_composite_useradd_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
145                                     struct rpc_composite_useradd *io)
146 {
147         NTSTATUS status;
148         struct useradd_state *s;
149         
150         status = composite_wait(c);
151         
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;
156         }
157
158         talloc_free(c);
159         return status;
160 }
161
162
163 /**
164  * Synchronous version of useradd call
165  *
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
170  */
171
172 NTSTATUS rpc_composite_useradd(struct dcerpc_pipe *pipe,
173                                TALLOC_CTX *mem_ctx,
174                                struct rpc_composite_useradd *io)
175 {
176         struct composite_context *c = rpc_composite_useradd_send(pipe, io);
177         return rpc_composite_useradd_recv(c, mem_ctx, io);
178 }