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/>.
22 #include "libnet/libnet.h"
23 #include "libcli/composite/composite.h"
24 #include "librpc/gen_ndr/lsa.h"
27 struct group_info_state {
28 struct libnet_context *ctx;
29 const char *domain_name;
30 const char *group_name;
31 struct libnet_LookupName lookup;
32 struct libnet_DomainOpen domopen;
33 struct libnet_rpc_groupinfo info;
35 /* information about the progress */
36 void (*monitor_fn)(struct monitor_msg *);
40 static void continue_domain_open_info(struct composite_context *ctx);
41 static void continue_name_found(struct composite_context *ctx);
42 static void continue_group_info(struct composite_context *ctx);
45 * Sends request to get group information
47 * @param ctx initialised libnet context
48 * @param mem_ctx memory context of this call
49 * @param io pointer to structure containing arguments the call
50 * @param monitor function pointer for receiving monitor messages
51 * @return composite context of this request
53 struct composite_context* libnet_GroupInfo_send(struct libnet_context *ctx,
55 struct libnet_GroupInfo *io,
56 void (*monitor)(struct monitor_msg*))
58 struct composite_context *c;
59 struct group_info_state *s;
60 BOOL prereq_met = False;
61 struct composite_context *lookup_req;
63 /* composite context allocation and setup */
64 c = composite_create(mem_ctx, ctx->event_ctx);
65 if (c == NULL) return NULL;
67 s = talloc_zero(c, struct group_info_state);
68 if (composite_nomem(s, c)) return c;
72 /* store arguments in the state structure */
73 s->monitor_fn = monitor;
75 s->domain_name = talloc_strdup(c, io->in.domain_name);
76 s->group_name = talloc_strdup(c, io->in.group_name);
78 /* prerequisite: make sure the domain is opened */
79 prereq_met = samr_domain_opened(ctx, s->domain_name, &c, &s->domopen,
80 continue_domain_open_info, monitor);
81 if (!prereq_met) return c;
83 /* prepare arguments for LookupName call */
84 s->lookup.in.name = s->group_name;
85 s->lookup.in.domain_name = s->domain_name;
87 /* send the request */
88 lookup_req = libnet_LookupName_send(s->ctx, c, &s->lookup, s->monitor_fn);
89 if (composite_nomem(lookup_req, c)) return c;
91 /* set the next stage */
92 composite_continue(c, lookup_req, continue_name_found, c);
98 * Stage 0.5 (optional): receive opened domain and send lookup name request
100 static void continue_domain_open_info(struct composite_context *ctx)
102 struct composite_context *c;
103 struct group_info_state *s;
104 struct composite_context *lookup_req;
106 c = talloc_get_type(ctx->async.private_data, struct composite_context);
107 s = talloc_get_type(c->private_data, struct group_info_state);
109 /* receive domain handle */
110 c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
111 if (!composite_is_ok(c)) return;
113 /* prepare arguments for LookupName call */
114 s->lookup.in.name = s->group_name;
115 s->lookup.in.domain_name = s->domain_name;
117 /* send the request */
118 lookup_req = libnet_LookupName_send(s->ctx, c, &s->lookup, s->monitor_fn);
119 if (composite_nomem(lookup_req, c)) return;
121 /* set the next stage */
122 composite_continue(c, lookup_req, continue_name_found, c);
127 * Stage 1: Receive SID found and send request for group info
129 static void continue_name_found(struct composite_context *ctx)
131 struct composite_context *c;
132 struct group_info_state *s;
133 struct composite_context *info_req;
135 c = talloc_get_type(ctx->async.private_data, struct composite_context);
136 s = talloc_get_type(c->private_data, struct group_info_state);
138 /* receive SID assiociated with name found */
139 c->status = libnet_LookupName_recv(ctx, c, &s->lookup);
140 if (!composite_is_ok(c)) return;
142 /* Is is a group SID actually ? */
143 if (s->lookup.out.sid_type != SID_NAME_DOM_GRP &&
144 s->lookup.out.sid_type != SID_NAME_ALIAS) {
145 composite_error(c, NT_STATUS_NO_SUCH_GROUP);
148 /* prepare arguments for groupinfo call */
149 s->info.in.domain_handle = s->ctx->samr.handle;
150 s->info.in.groupname = s->group_name;
151 s->info.in.sid = s->lookup.out.sidstr;
152 /* we're looking for all information available */
153 s->info.in.level = GROUPINFOALL;
155 /* send the request */
156 info_req = libnet_rpc_groupinfo_send(s->ctx->samr.pipe, &s->info, s->monitor_fn);
157 if (composite_nomem(info_req, c)) return;
159 /* set the next stage */
160 composite_continue(c, info_req, continue_group_info, c);
165 * Stage 2: Receive group information
167 static void continue_group_info(struct composite_context *ctx)
169 struct composite_context *c;
170 struct group_info_state *s;
172 c = talloc_get_type(ctx->async.private_data, struct composite_context);
173 s = talloc_get_type(c->private_data, struct group_info_state);
175 /* receive group information */
176 c->status = libnet_rpc_groupinfo_recv(ctx, c, &s->info);
177 if (!composite_is_ok(c)) return;
185 * Receive group information
187 * @param c composite context returned by libnet_GroupInfo_send
188 * @param mem_ctx memory context of this call
189 * @param io pointer to a structure receiving results of the call
192 NTSTATUS libnet_GroupInfo_recv(struct composite_context* c, TALLOC_CTX *mem_ctx,
193 struct libnet_GroupInfo *io)
196 struct group_info_state *s;
198 status = composite_wait(c);
199 if (NT_STATUS_IS_OK(status)) {
200 /* put the results into io structure if everything went fine */
201 s = talloc_get_type(c->private_data, struct group_info_state);
203 io->out.group_sid = talloc_steal(mem_ctx, s->lookup.out.sid);
204 io->out.num_members = s->info.out.info.all.num_members;
205 io->out.description = talloc_steal(mem_ctx, s->info.out.info.all.description.string);
207 io->out.error_string = talloc_strdup(mem_ctx, "Success");
210 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
220 * Obtains specified group information
222 * @param ctx initialised libnet context
223 * @param mem_ctx memory context of the call
224 * @param io pointer to a structure containing arguments and results of the call
226 NTSTATUS libnet_GroupInfo(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
227 struct libnet_GroupInfo *io)
229 struct composite_context *c = libnet_GroupInfo_send(ctx, mem_ctx,
231 return libnet_GroupInfo_recv(c, mem_ctx, io);