s4-dsdb: Use samdb_reference_dn_is_our_ntdsa()
[Samba.git] / source4 / libnet / groupinfo.c
blob9060973e02ef04361cdb92b35d361033be95364e
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 getting group information via samr pipe
25 #include "includes.h"
26 #include "libcli/composite/composite.h"
27 #include "librpc/gen_ndr/security.h"
28 #include "libcli/security/security.h"
29 #include "libnet/libnet.h"
30 #include "librpc/gen_ndr/ndr_samr_c.h"
33 struct groupinfo_state {
34 struct dcerpc_pipe *pipe;
35 struct policy_handle domain_handle;
36 struct policy_handle group_handle;
37 uint16_t level;
38 struct samr_LookupNames lookup;
39 struct samr_OpenGroup opengroup;
40 struct samr_QueryGroupInfo querygroupinfo;
41 struct samr_Close samrclose;
42 union samr_GroupInfo *info;
44 /* information about the progress */
45 void (*monitor_fn)(struct monitor_msg *);
49 static void continue_groupinfo_lookup(struct tevent_req *subreq);
50 static void continue_groupinfo_opengroup(struct tevent_req *subreq);
51 static void continue_groupinfo_getgroup(struct tevent_req *subreq);
52 static void continue_groupinfo_closegroup(struct tevent_req *subreq);
55 /**
56 * Stage 1 (optional): Look for a group name in SAM server.
58 static void continue_groupinfo_lookup(struct tevent_req *subreq)
60 struct composite_context *c;
61 struct groupinfo_state *s;
62 struct monitor_msg msg;
63 struct msg_rpc_lookup_name *msg_lookup;
65 c = tevent_req_callback_data(subreq, struct composite_context);
66 s = talloc_get_type(c->private_data, struct groupinfo_state);
68 /* receive samr_Lookup reply */
69 c->status = dcerpc_samr_LookupNames_r_recv(subreq, s);
70 TALLOC_FREE(subreq);
71 if (!composite_is_ok(c)) return;
73 /* there could be a problem with name resolving itself */
74 if (!NT_STATUS_IS_OK(s->lookup.out.result)) {
75 composite_error(c, s->lookup.out.result);
76 return;
79 /* issue a monitor message */
80 if (s->monitor_fn) {
81 msg.type = mon_SamrLookupName;
82 msg_lookup = talloc(s, struct msg_rpc_lookup_name);
83 msg_lookup->rid = s->lookup.out.rids->ids;
84 msg_lookup->count = s->lookup.out.rids->count;
85 msg.data = (void*)msg_lookup;
86 msg.data_size = sizeof(*msg_lookup);
88 s->monitor_fn(&msg);
92 /* have we actually got name resolved
93 - we're looking for only one at the moment */
94 if (s->lookup.out.rids->count == 0) {
95 composite_error(c, NT_STATUS_NO_SUCH_USER);
96 return;
99 /* TODO: find proper status code for more than one rid found */
101 /* prepare parameters for LookupNames */
102 s->opengroup.in.domain_handle = &s->domain_handle;
103 s->opengroup.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
104 s->opengroup.in.rid = s->lookup.out.rids->ids[0];
105 s->opengroup.out.group_handle = &s->group_handle;
107 /* send request */
108 subreq = dcerpc_samr_OpenGroup_r_send(s, c->event_ctx,
109 s->pipe->binding_handle,
110 &s->opengroup);
111 if (composite_nomem(subreq, c)) return;
113 tevent_req_set_callback(subreq, continue_groupinfo_opengroup, c);
118 * Stage 2: Open group policy handle.
120 static void continue_groupinfo_opengroup(struct tevent_req *subreq)
122 struct composite_context *c;
123 struct groupinfo_state *s;
124 struct monitor_msg msg;
125 struct msg_rpc_open_group *msg_open;
127 c = tevent_req_callback_data(subreq, struct composite_context);
128 s = talloc_get_type(c->private_data, struct groupinfo_state);
130 /* receive samr_OpenGroup reply */
131 c->status = dcerpc_samr_OpenGroup_r_recv(subreq, s);
132 TALLOC_FREE(subreq);
133 if (!composite_is_ok(c)) return;
135 if (!NT_STATUS_IS_OK(s->opengroup.out.result)) {
136 composite_error(c, s->opengroup.out.result);
137 return;
140 /* issue a monitor message */
141 if (s->monitor_fn) {
142 msg.type = mon_SamrOpenGroup;
143 msg_open = talloc(s, struct msg_rpc_open_group);
144 msg_open->rid = s->opengroup.in.rid;
145 msg_open->access_mask = s->opengroup.in.access_mask;
146 msg.data = (void*)msg_open;
147 msg.data_size = sizeof(*msg_open);
149 s->monitor_fn(&msg);
152 /* prepare parameters for QueryGroupInfo call */
153 s->querygroupinfo.in.group_handle = &s->group_handle;
154 s->querygroupinfo.in.level = s->level;
155 s->querygroupinfo.out.info = talloc(s, union samr_GroupInfo *);
156 if (composite_nomem(s->querygroupinfo.out.info, c)) return;
158 /* queue rpc call, set event handling and new state */
159 subreq = dcerpc_samr_QueryGroupInfo_r_send(s,
160 c->event_ctx,
161 s->pipe->binding_handle,
162 &s->querygroupinfo);
163 if (composite_nomem(subreq, c)) return;
165 tevent_req_set_callback(subreq, continue_groupinfo_getgroup, c);
170 * Stage 3: Get requested group information.
172 static void continue_groupinfo_getgroup(struct tevent_req *subreq)
174 struct composite_context *c;
175 struct groupinfo_state *s;
176 struct monitor_msg msg;
177 struct msg_rpc_query_group *msg_query;
179 c = tevent_req_callback_data(subreq, struct composite_context);
180 s = talloc_get_type(c->private_data, struct groupinfo_state);
182 /* receive samr_QueryGroupInfo reply */
183 c->status = dcerpc_samr_QueryGroupInfo_r_recv(subreq, s);
184 TALLOC_FREE(subreq);
185 if (!composite_is_ok(c)) return;
187 /* check if querygroup itself went ok */
188 if (!NT_STATUS_IS_OK(s->querygroupinfo.out.result)) {
189 composite_error(c, s->querygroupinfo.out.result);
190 return;
193 s->info = talloc_steal(s, *s->querygroupinfo.out.info);
195 /* issue a monitor message */
196 if (s->monitor_fn) {
197 msg.type = mon_SamrQueryGroup;
198 msg_query = talloc(s, struct msg_rpc_query_group);
199 msg_query->level = s->querygroupinfo.in.level;
200 msg.data = (void*)msg_query;
201 msg.data_size = sizeof(*msg_query);
203 s->monitor_fn(&msg);
206 /* prepare arguments for Close call */
207 s->samrclose.in.handle = &s->group_handle;
208 s->samrclose.out.handle = &s->group_handle;
210 /* queue rpc call, set event handling and new state */
211 subreq = dcerpc_samr_Close_r_send(s, c->event_ctx,
212 s->pipe->binding_handle,
213 &s->samrclose);
214 if (composite_nomem(subreq, c)) return;
216 tevent_req_set_callback(subreq, continue_groupinfo_closegroup, c);
221 * Stage 4: Close policy handle associated with opened group.
223 static void continue_groupinfo_closegroup(struct tevent_req *subreq)
225 struct composite_context *c;
226 struct groupinfo_state *s;
227 struct monitor_msg msg;
228 struct msg_rpc_close_group *msg_close;
230 c = tevent_req_callback_data(subreq, struct composite_context);
231 s = talloc_get_type(c->private_data, struct groupinfo_state);
233 /* receive samr_Close reply */
234 c->status = dcerpc_samr_Close_r_recv(subreq, s);
235 TALLOC_FREE(subreq);
236 if (!composite_is_ok(c)) return;
238 if (!NT_STATUS_IS_OK(s->samrclose.out.result)) {
239 composite_error(c, s->samrclose.out.result);
240 return;
243 /* issue a monitor message */
244 if (s->monitor_fn) {
245 msg.type = mon_SamrClose;
246 msg_close = talloc(s, struct msg_rpc_close_group);
247 msg_close->rid = s->opengroup.in.rid;
248 msg.data = (void*)msg_close;
249 msg.data_size = sizeof(*msg_close);
251 s->monitor_fn(&msg);
254 composite_done(c);
259 * Sends asynchronous groupinfo request
261 * @param p dce/rpc call pipe
262 * @param io arguments and results of the call
264 struct composite_context *libnet_rpc_groupinfo_send(struct dcerpc_pipe *p,
265 TALLOC_CTX *mem_ctx,
266 struct libnet_rpc_groupinfo *io,
267 void (*monitor)(struct monitor_msg*))
269 struct composite_context *c;
270 struct groupinfo_state *s;
271 struct dom_sid *sid;
272 struct tevent_req *subreq;
274 if (!p || !io) return NULL;
276 c = composite_create(mem_ctx, dcerpc_event_context(p));
277 if (c == NULL) return c;
279 s = talloc_zero(c, struct groupinfo_state);
280 if (composite_nomem(s, c)) return c;
282 c->private_data = s;
284 s->level = io->in.level;
285 s->pipe = p;
286 s->domain_handle = io->in.domain_handle;
287 s->monitor_fn = monitor;
289 if (io->in.sid) {
290 sid = dom_sid_parse_talloc(s, io->in.sid);
291 if (composite_nomem(sid, c)) return c;
293 s->opengroup.in.domain_handle = &s->domain_handle;
294 s->opengroup.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
295 s->opengroup.in.rid = sid->sub_auths[sid->num_auths - 1];
296 s->opengroup.out.group_handle = &s->group_handle;
298 /* send request */
299 subreq = dcerpc_samr_OpenGroup_r_send(s, c->event_ctx,
300 p->binding_handle,
301 &s->opengroup);
302 if (composite_nomem(subreq, c)) return c;
304 tevent_req_set_callback(subreq, continue_groupinfo_opengroup, c);
306 } else {
307 /* preparing parameters to send rpc request */
308 s->lookup.in.domain_handle = &s->domain_handle;
309 s->lookup.in.num_names = 1;
310 s->lookup.in.names = talloc_array(s, struct lsa_String, 1);
311 if (composite_nomem(s->lookup.in.names, c)) return c;
313 s->lookup.in.names[0].string = talloc_strdup(s, io->in.groupname);
314 if (composite_nomem(s->lookup.in.names[0].string, c)) return c;
315 s->lookup.out.rids = talloc_zero(s, struct samr_Ids);
316 s->lookup.out.types = talloc_zero(s, struct samr_Ids);
317 if (composite_nomem(s->lookup.out.rids, c)) return c;
318 if (composite_nomem(s->lookup.out.types, c)) return c;
320 /* send request */
321 subreq = dcerpc_samr_LookupNames_r_send(s, c->event_ctx,
322 p->binding_handle,
323 &s->lookup);
324 if (composite_nomem(subreq, c)) return c;
326 tevent_req_set_callback(subreq, continue_groupinfo_lookup, c);
329 return c;
334 * Waits for and receives result of asynchronous groupinfo call
336 * @param c composite context returned by asynchronous groupinfo call
337 * @param mem_ctx memory context of the call
338 * @param io pointer to results (and arguments) of the call
339 * @return nt status code of execution
342 NTSTATUS libnet_rpc_groupinfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
343 struct libnet_rpc_groupinfo *io)
345 NTSTATUS status;
346 struct groupinfo_state *s;
348 /* wait for results of sending request */
349 status = composite_wait(c);
351 if (NT_STATUS_IS_OK(status) && io) {
352 s = talloc_get_type(c->private_data, struct groupinfo_state);
353 talloc_steal(mem_ctx, s->info);
354 io->out.info = *s->info;
357 /* memory context associated to composite context is no longer needed */
358 talloc_free(c);
359 return status;
364 * Synchronous version of groupinfo call
366 * @param pipe dce/rpc call pipe
367 * @param mem_ctx memory context for the call
368 * @param io arguments and results of the call
369 * @return nt status code of execution
372 NTSTATUS libnet_rpc_groupinfo(struct dcerpc_pipe *p,
373 TALLOC_CTX *mem_ctx,
374 struct libnet_rpc_groupinfo *io)
376 struct composite_context *c = libnet_rpc_groupinfo_send(p, mem_ctx, io, NULL);
377 return libnet_rpc_groupinfo_recv(c, mem_ctx, io);