Improve the Gemand pam_winbind translation.
[Samba/bb.git] / source4 / libnet / groupman.c
blob4dfb2d8aabc3a2a071c7f09d5f64116200a0eae3
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Rafal Szczesniak 2007
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 3 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, see <http://www.gnu.org/licenses/>.
21 a composite function for manipulating (add/edit/del) groups via samr pipe
24 #include "includes.h"
25 #include "libcli/composite/composite.h"
26 #include "libnet/composite.h"
27 #include "libnet/groupman.h"
28 #include "librpc/gen_ndr/ndr_samr_c.h"
29 #include "libnet/libnet_proto.h"
32 struct groupadd_state {
33 struct dcerpc_pipe *pipe;
34 struct policy_handle domain_handle;
35 struct samr_CreateDomainGroup creategroup;
36 struct policy_handle group_handle;
37 uint32_t group_rid;
39 void (*monitor_fn)(struct monitor_msg*);
43 static void continue_groupadd_created(struct rpc_request *req);
46 struct composite_context* libnet_rpc_groupadd_send(struct dcerpc_pipe *p,
47 struct libnet_rpc_groupadd *io,
48 void (*monitor)(struct monitor_msg*))
50 struct composite_context *c;
51 struct groupadd_state *s;
52 struct rpc_request *create_req;
54 if (!p || !io) return NULL;
56 c = composite_create(p, dcerpc_event_context(p));
57 if (c == NULL) return NULL;
59 s = talloc_zero(c, struct groupadd_state);
60 if (composite_nomem(s, c)) return c;
62 c->private_data = s;
64 s->domain_handle = io->in.domain_handle;
65 s->pipe = p;
66 s->monitor_fn = monitor;
68 s->creategroup.in.domain_handle = &s->domain_handle;
70 s->creategroup.in.name = talloc_zero(c, struct lsa_String);
71 if (composite_nomem(s->creategroup.in.name, c)) return c;
73 s->creategroup.in.name->string = talloc_strdup(c, io->in.groupname);
74 if (composite_nomem(s->creategroup.in.name->string, c)) return c;
76 s->creategroup.in.access_mask = 0;
78 s->creategroup.out.group_handle = &s->group_handle;
79 s->creategroup.out.rid = &s->group_rid;
81 create_req = dcerpc_samr_CreateDomainGroup_send(s->pipe, c, &s->creategroup);
82 if (composite_nomem(create_req, c)) return c;
84 composite_continue_rpc(c, create_req, continue_groupadd_created, c);
85 return c;
89 NTSTATUS libnet_rpc_groupadd_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
90 struct libnet_rpc_groupadd *io)
92 NTSTATUS status;
93 struct groupadd_state *s;
95 status = composite_wait(c);
96 if (NT_STATUS_IS_OK(status)) {
97 s = talloc_get_type(c, struct groupadd_state);
100 return status;
104 static void continue_groupadd_created(struct rpc_request *req)
106 struct composite_context *c;
107 struct groupadd_state *s;
109 c = talloc_get_type(req->async.private_data, struct composite_context);
110 s = talloc_get_type(c->private_data, struct groupadd_state);
112 c->status = dcerpc_ndr_request_recv(req);
113 if (!composite_is_ok(c)) return;
115 c->status = s->creategroup.out.result;
116 if (!composite_is_ok(c)) return;
118 composite_done(c);
122 NTSTATUS libnet_rpc_groupadd(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
123 struct libnet_rpc_groupadd *io)
125 struct composite_context *c;
127 c = libnet_rpc_groupadd_send(p, io, NULL);
128 return libnet_rpc_groupadd_recv(c, mem_ctx, io);
132 struct groupdel_state {
133 struct dcerpc_pipe *pipe;
134 struct policy_handle domain_handle;
135 struct policy_handle group_handle;
136 struct samr_LookupNames lookupname;
137 struct samr_OpenGroup opengroup;
138 struct samr_DeleteDomainGroup deletegroup;
140 /* information about the progress */
141 void (*monitor_fn)(struct monitor_msg *);
145 static void continue_groupdel_name_found(struct rpc_request *req);
146 static void continue_groupdel_group_opened(struct rpc_request *req);
147 static void continue_groupdel_deleted(struct rpc_request *req);
150 struct composite_context* libnet_rpc_groupdel_send(struct dcerpc_pipe *p,
151 struct libnet_rpc_groupdel *io,
152 void (*monitor)(struct monitor_msg*))
154 struct composite_context *c;
155 struct groupdel_state *s;
156 struct rpc_request *lookup_req;
158 /* composite context allocation and setup */
159 c = composite_create(p, dcerpc_event_context(p));
160 if (c == NULL) return NULL;
162 s = talloc_zero(c, struct groupdel_state);
163 if (composite_nomem(s, c)) return c;
165 c->private_data = s;
167 /* store function parameters in the state structure */
168 s->pipe = p;
169 s->domain_handle = io->in.domain_handle;
170 s->monitor_fn = monitor;
172 /* prepare parameters to send rpc request */
173 s->lookupname.in.domain_handle = &io->in.domain_handle;
174 s->lookupname.in.num_names = 1;
175 s->lookupname.in.names = talloc_zero(s, struct lsa_String);
176 s->lookupname.in.names->string = io->in.groupname;
177 s->lookupname.out.rids = talloc_zero(s, struct samr_Ids);
178 s->lookupname.out.types = talloc_zero(s, struct samr_Ids);
179 if (composite_nomem(s->lookupname.out.rids, c)) return c;
180 if (composite_nomem(s->lookupname.out.types, c)) return c;
182 /* send the request */
183 lookup_req = dcerpc_samr_LookupNames_send(p, c, &s->lookupname);
184 if (composite_nomem(lookup_req, c)) return c;
186 composite_continue_rpc(c, lookup_req, continue_groupdel_name_found, c);
187 return c;
191 static void continue_groupdel_name_found(struct rpc_request *req)
193 struct composite_context *c;
194 struct groupdel_state *s;
195 struct rpc_request *opengroup_req;
197 c = talloc_get_type(req->async.private_data, struct composite_context);
198 s = talloc_get_type(c->private_data, struct groupdel_state);
200 /* receive samr_LookupNames result */
201 c->status = dcerpc_ndr_request_recv(req);
202 if (!composite_is_ok(c)) return;
204 c->status = s->lookupname.out.result;
205 if (!NT_STATUS_IS_OK(c->status)) {
206 composite_error(c, c->status);
207 return;
210 /* what to do when there's no group account to delete
211 and what if there's more than one rid resolved */
212 if (!s->lookupname.out.rids->count) {
213 c->status = NT_STATUS_NO_SUCH_GROUP;
214 composite_error(c, c->status);
215 return;
217 } else if (!s->lookupname.out.rids->count > 1) {
218 c->status = NT_STATUS_INVALID_ACCOUNT_NAME;
219 composite_error(c, c->status);
220 return;
223 /* prepare the arguments for rpc call */
224 s->opengroup.in.domain_handle = &s->domain_handle;
225 s->opengroup.in.rid = s->lookupname.out.rids->ids[0];
226 s->opengroup.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
227 s->opengroup.out.group_handle = &s->group_handle;
229 /* send rpc request */
230 opengroup_req = dcerpc_samr_OpenGroup_send(s->pipe, c, &s->opengroup);
231 if (composite_nomem(opengroup_req, c)) return;
233 composite_continue_rpc(c, opengroup_req, continue_groupdel_group_opened, c);
237 static void continue_groupdel_group_opened(struct rpc_request *req)
239 struct composite_context *c;
240 struct groupdel_state *s;
241 struct rpc_request *delgroup_req;
243 c = talloc_get_type(req->async.private_data, struct composite_context);
244 s = talloc_get_type(c->private_data, struct groupdel_state);
246 /* receive samr_OpenGroup result */
247 c->status = dcerpc_ndr_request_recv(req);
248 if (!composite_is_ok(c)) return;
250 c->status = s->opengroup.out.result;
251 if (!NT_STATUS_IS_OK(c->status)) {
252 composite_error(c, c->status);
253 return;
256 /* prepare the final rpc call arguments */
257 s->deletegroup.in.group_handle = &s->group_handle;
258 s->deletegroup.out.group_handle = &s->group_handle;
260 /* send rpc request */
261 delgroup_req = dcerpc_samr_DeleteDomainGroup_send(s->pipe, c, &s->deletegroup);
262 if (composite_nomem(delgroup_req, c)) return;
264 /* callback handler setup */
265 composite_continue_rpc(c, delgroup_req, continue_groupdel_deleted, c);
269 static void continue_groupdel_deleted(struct rpc_request *req)
271 struct composite_context *c;
272 struct groupdel_state *s;
274 c = talloc_get_type(req->async.private_data, struct composite_context);
275 s = talloc_get_type(c->private_data, struct groupdel_state);
277 /* receive samr_DeleteGroup result */
278 c->status = dcerpc_ndr_request_recv(req);
279 if (!composite_is_ok(c)) return;
281 /* return the actual function call status */
282 c->status = s->deletegroup.out.result;
283 if (!NT_STATUS_IS_OK(c->status)) {
284 composite_error(c, c->status);
285 return;
288 composite_done(c);
292 NTSTATUS libnet_rpc_groupdel_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
293 struct libnet_rpc_groupdel *io)
295 NTSTATUS status;
296 struct groupdel_state *s;
298 status = composite_wait(c);
299 if (NT_STATUS_IS_OK(status) && io) {
300 s = talloc_get_type(c->private_data, struct groupdel_state);
301 io->out.group_handle = s->group_handle;
304 talloc_free(c);
305 return status;
309 NTSTATUS libnet_rpc_groupdel(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
310 struct libnet_rpc_groupdel *io)
312 struct composite_context *c;
314 c = libnet_rpc_groupdel_send(p, io, NULL);
315 return libnet_rpc_groupdel_recv(c, mem_ctx, io);