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
26 #include "libcli/composite/composite.h"
27 #include "libnet/composite.h"
28 #include "librpc/gen_ndr/security.h"
29 #include "libcli/security/security.h"
30 #include "libnet/userman.h"
31 #include "libnet/groupinfo.h"
32 #include "librpc/gen_ndr/ndr_samr_c.h"
35 struct groupinfo_state
{
36 struct dcerpc_pipe
*pipe
;
37 struct policy_handle domain_handle
;
38 struct policy_handle group_handle
;
40 struct samr_LookupNames lookup
;
41 struct samr_OpenGroup opengroup
;
42 struct samr_QueryGroupInfo querygroupinfo
;
43 struct samr_Close samrclose
;
44 union samr_GroupInfo
*info
;
46 /* information about the progress */
47 void (*monitor_fn
)(struct monitor_msg
*);
51 static void continue_groupinfo_lookup(struct rpc_request
*req
);
52 static void continue_groupinfo_opengroup(struct rpc_request
*req
);
53 static void continue_groupinfo_getgroup(struct rpc_request
*req
);
54 static void continue_groupinfo_closegroup(struct rpc_request
*req
);
58 * Stage 1 (optional): Look for a group name in SAM server.
60 static void continue_groupinfo_lookup(struct rpc_request
*req
)
62 struct composite_context
*c
;
63 struct groupinfo_state
*s
;
64 struct rpc_request
*opengroup_req
;
65 struct monitor_msg msg
;
66 struct msg_rpc_lookup_name
*msg_lookup
;
68 c
= talloc_get_type(req
->async
.private_data
, struct composite_context
);
69 s
= talloc_get_type(c
->private_data
, struct groupinfo_state
);
71 /* receive samr_Lookup reply */
72 c
->status
= dcerpc_ndr_request_recv(req
);
73 if (!composite_is_ok(c
)) return;
75 /* there could be a problem with name resolving itself */
76 if (!NT_STATUS_IS_OK(s
->lookup
.out
.result
)) {
77 composite_error(c
, s
->lookup
.out
.result
);
81 /* issue a monitor message */
83 msg
.type
= mon_SamrLookupName
;
84 msg_lookup
= talloc(s
, struct msg_rpc_lookup_name
);
85 msg_lookup
->rid
= s
->lookup
.out
.rids
.ids
;
86 msg_lookup
->count
= s
->lookup
.out
.rids
.count
;
87 msg
.data
= (void*)msg_lookup
;
88 msg
.data_size
= sizeof(*msg_lookup
);
94 /* have we actually got name resolved
95 - we're looking for only one at the moment */
96 if (s
->lookup
.out
.rids
.count
== 0) {
97 composite_error(c
, NT_STATUS_NO_SUCH_USER
);
100 /* TODO: find proper status code for more than one rid found */
102 /* prepare parameters for LookupNames */
103 s
->opengroup
.in
.domain_handle
= &s
->domain_handle
;
104 s
->opengroup
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
105 s
->opengroup
.in
.rid
= s
->lookup
.out
.rids
.ids
[0];
106 s
->opengroup
.out
.group_handle
= &s
->group_handle
;
109 opengroup_req
= dcerpc_samr_OpenGroup_send(s
->pipe
, c
, &s
->opengroup
);
110 if (composite_nomem(opengroup_req
, c
)) return;
112 composite_continue_rpc(c
, opengroup_req
, continue_groupinfo_opengroup
, c
);
117 * Stage 2: Open group policy handle.
119 static void continue_groupinfo_opengroup(struct rpc_request
*req
)
121 struct composite_context
*c
;
122 struct groupinfo_state
*s
;
123 struct rpc_request
*querygroup_req
;
124 struct monitor_msg msg
;
125 struct msg_rpc_open_group
*msg_open
;
127 c
= talloc_get_type(req
->async
.private_data
, struct composite_context
);
128 s
= talloc_get_type(c
->private_data
, struct groupinfo_state
);
130 /* receive samr_OpenGroup reply */
131 c
->status
= dcerpc_ndr_request_recv(req
);
132 if (!composite_is_ok(c
)) return;
134 if (!NT_STATUS_IS_OK(s
->querygroupinfo
.out
.result
)) {
135 composite_error(c
, s
->querygroupinfo
.out
.result
);
139 /* issue a monitor message */
141 msg
.type
= mon_SamrOpenGroup
;
142 msg_open
= talloc(s
, struct msg_rpc_open_group
);
143 msg_open
->rid
= s
->opengroup
.in
.rid
;
144 msg_open
->access_mask
= s
->opengroup
.in
.access_mask
;
145 msg
.data
= (void*)msg_open
;
146 msg
.data_size
= sizeof(*msg_open
);
151 /* prepare parameters for QueryGroupInfo call */
152 s
->querygroupinfo
.in
.group_handle
= &s
->group_handle
;
153 s
->querygroupinfo
.in
.level
= s
->level
;
155 /* queue rpc call, set event handling and new state */
156 querygroup_req
= dcerpc_samr_QueryGroupInfo_send(s
->pipe
, c
, &s
->querygroupinfo
);
157 if (composite_nomem(querygroup_req
, c
)) return;
159 composite_continue_rpc(c
, querygroup_req
, continue_groupinfo_getgroup
, c
);
164 * Stage 3: Get requested group information.
166 static void continue_groupinfo_getgroup(struct rpc_request
*req
)
168 struct composite_context
*c
;
169 struct groupinfo_state
*s
;
170 struct rpc_request
*close_req
;
171 struct monitor_msg msg
;
172 struct msg_rpc_query_group
*msg_query
;
174 c
= talloc_get_type(req
->async
.private_data
, struct composite_context
);
175 s
= talloc_get_type(c
->private_data
, struct groupinfo_state
);
177 /* receive samr_QueryGroupInfo reply */
178 c
->status
= dcerpc_ndr_request_recv(req
);
179 if (!composite_is_ok(c
)) return;
181 /* check if querygroup itself went ok */
182 if (!NT_STATUS_IS_OK(s
->querygroupinfo
.out
.result
)) {
183 composite_error(c
, s
->querygroupinfo
.out
.result
);
187 s
->info
= talloc_steal(s
, s
->querygroupinfo
.out
.info
);
189 /* issue a monitor message */
191 msg
.type
= mon_SamrQueryGroup
;
192 msg_query
= talloc(s
, struct msg_rpc_query_group
);
193 msg_query
->level
= s
->querygroupinfo
.in
.level
;
194 msg
.data
= (void*)msg_query
;
195 msg
.data_size
= sizeof(*msg_query
);
200 /* prepare arguments for Close call */
201 s
->samrclose
.in
.handle
= &s
->group_handle
;
202 s
->samrclose
.out
.handle
= &s
->group_handle
;
204 /* queue rpc call, set event handling and new state */
205 close_req
= dcerpc_samr_Close_send(s
->pipe
, c
, &s
->samrclose
);
206 if (composite_nomem(close_req
, c
)) return;
208 composite_continue_rpc(c
, close_req
, continue_groupinfo_closegroup
, c
);
213 * Stage 4: Close policy handle associated with opened group.
215 static void continue_groupinfo_closegroup(struct rpc_request
*req
)
217 struct composite_context
*c
;
218 struct groupinfo_state
*s
;
219 struct monitor_msg msg
;
220 struct msg_rpc_close_group
*msg_close
;
222 c
= talloc_get_type(req
->async
.private_data
, struct composite_context
);
223 s
= talloc_get_type(c
->private_data
, struct groupinfo_state
);
225 /* receive samr_Close reply */
226 c
->status
= dcerpc_ndr_request_recv(req
);
227 if (!composite_is_ok(c
)) return;
229 if (!NT_STATUS_IS_OK(s
->samrclose
.out
.result
)) {
230 composite_error(c
, s
->samrclose
.out
.result
);
234 /* issue a monitor message */
236 msg
.type
= mon_SamrClose
;
237 msg_close
= talloc(s
, struct msg_rpc_close_group
);
238 msg_close
->rid
= s
->opengroup
.in
.rid
;
239 msg
.data
= (void*)msg_close
;
240 msg
.data_size
= sizeof(*msg_close
);
250 * Sends asynchronous groupinfo request
252 * @param p dce/rpc call pipe
253 * @param io arguments and results of the call
255 struct composite_context
*libnet_rpc_groupinfo_send(struct dcerpc_pipe
*p
,
256 struct libnet_rpc_groupinfo
*io
,
257 void (*monitor
)(struct monitor_msg
*))
259 struct composite_context
*c
;
260 struct groupinfo_state
*s
;
262 struct rpc_request
*opengroup_req
, *lookup_req
;
264 if (!p
|| !io
) return NULL
;
266 c
= composite_create(p
, dcerpc_event_context(p
));
267 if (c
== NULL
) return c
;
269 s
= talloc_zero(c
, struct groupinfo_state
);
270 if (composite_nomem(s
, c
)) return c
;
274 s
->level
= io
->in
.level
;
276 s
->domain_handle
= io
->in
.domain_handle
;
277 s
->monitor_fn
= monitor
;
280 sid
= dom_sid_parse_talloc(s
, io
->in
.sid
);
281 if (composite_nomem(sid
, c
)) return c
;
283 s
->opengroup
.in
.domain_handle
= &s
->domain_handle
;
284 s
->opengroup
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
285 s
->opengroup
.in
.rid
= sid
->sub_auths
[sid
->num_auths
- 1];
286 s
->opengroup
.out
.group_handle
= &s
->group_handle
;
289 opengroup_req
= dcerpc_samr_OpenGroup_send(p
, c
, &s
->opengroup
);
290 if (composite_nomem(opengroup_req
, c
)) return c
;
292 composite_continue_rpc(c
, opengroup_req
, continue_groupinfo_opengroup
, c
);
295 /* preparing parameters to send rpc request */
296 s
->lookup
.in
.domain_handle
= &s
->domain_handle
;
297 s
->lookup
.in
.num_names
= 1;
298 s
->lookup
.in
.names
= talloc_array(s
, struct lsa_String
, 1);
299 if (composite_nomem(s
->lookup
.in
.names
, c
)) return c
;
301 s
->lookup
.in
.names
[0].string
= talloc_strdup(s
, io
->in
.groupname
);
302 if (composite_nomem(s
->lookup
.in
.names
[0].string
, c
)) return c
;
305 lookup_req
= dcerpc_samr_LookupNames_send(p
, c
, &s
->lookup
);
306 if (composite_nomem(lookup_req
, c
)) return c
;
308 composite_continue_rpc(c
, lookup_req
, continue_groupinfo_lookup
, c
);
316 * Waits for and receives result of asynchronous groupinfo call
318 * @param c composite context returned by asynchronous groupinfo call
319 * @param mem_ctx memory context of the call
320 * @param io pointer to results (and arguments) of the call
321 * @return nt status code of execution
324 NTSTATUS
libnet_rpc_groupinfo_recv(struct composite_context
*c
, TALLOC_CTX
*mem_ctx
,
325 struct libnet_rpc_groupinfo
*io
)
328 struct groupinfo_state
*s
;
330 /* wait for results of sending request */
331 status
= composite_wait(c
);
333 if (NT_STATUS_IS_OK(status
) && io
) {
334 s
= talloc_get_type(c
->private_data
, struct groupinfo_state
);
335 talloc_steal(mem_ctx
, s
->info
);
336 io
->out
.info
= *s
->info
;
339 /* memory context associated to composite context is no longer needed */
346 * Synchronous version of groupinfo call
348 * @param pipe dce/rpc call pipe
349 * @param mem_ctx memory context for the call
350 * @param io arguments and results of the call
351 * @return nt status code of execution
354 NTSTATUS
libnet_rpc_groupinfo(struct dcerpc_pipe
*p
,
356 struct libnet_rpc_groupinfo
*io
)
358 struct composite_context
*c
= libnet_rpc_groupinfo_send(p
, io
, NULL
);
359 return libnet_rpc_groupinfo_recv(c
, mem_ctx
, io
);