50615f3946e12831d71ec25989c50c92feaa05ac
[kai/samba-autobuild/.git] / source4 / libnet / libnet_group.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Copyright (C) Rafal Szczesniak  2007
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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 #include "includes.h"
22 #include "libnet/libnet.h"
23 #include "libcli/composite/composite.h"
24 #include "librpc/gen_ndr/lsa.h"
25
26
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;
34         
35         /* information about the progress */
36         void (*monitor_fn)(struct monitor_msg *);
37 };
38
39
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);
43
44 /**
45  * Sends request to get group information
46  *
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
52  */
53 struct composite_context* libnet_GroupInfo_send(struct libnet_context *ctx,
54                                                 TALLOC_CTX *mem_ctx,
55                                                 struct libnet_GroupInfo *io,
56                                                 void (*monitor)(struct monitor_msg*))
57 {
58         struct composite_context *c;
59         struct group_info_state *s;
60         BOOL prereq_met = False;
61         struct composite_context *lookup_req;
62
63         /* composite context allocation and setup */
64         c = composite_create(mem_ctx, ctx->event_ctx);
65         if (c == NULL) return NULL;
66
67         s = talloc_zero(c, struct group_info_state);
68         if (composite_nomem(s, c)) return c;
69
70         c->private_data = s;
71
72         /* store arguments in the state structure */
73         s->monitor_fn = monitor;
74         s->ctx = ctx;   
75         s->domain_name = talloc_strdup(c, io->in.domain_name);
76         s->group_name  = talloc_strdup(c, io->in.group_name);
77
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;
82         
83         /* prepare arguments for LookupName call */
84         s->lookup.in.name        = s->group_name;
85         s->lookup.in.domain_name = s->domain_name;
86
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;
90
91         /* set the next stage */
92         composite_continue(c, lookup_req, continue_name_found, c);
93         return c;
94 }
95
96
97 /*
98  * Stage 0.5 (optional): receive opened domain and send lookup name request
99  */
100 static void continue_domain_open_info(struct composite_context *ctx)
101 {
102         struct composite_context *c;
103         struct group_info_state *s;
104         struct composite_context *lookup_req;
105         
106         c = talloc_get_type(ctx->async.private_data, struct composite_context);
107         s = talloc_get_type(c->private_data, struct group_info_state);
108         
109         /* receive domain handle */
110         c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
111         if (!composite_is_ok(c)) return;
112
113         /* prepare arguments for LookupName call */
114         s->lookup.in.name        = s->group_name;
115         s->lookup.in.domain_name = s->domain_name;
116
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;
120         
121         /* set the next stage */
122         composite_continue(c, lookup_req, continue_name_found, c);
123 }
124
125
126 /*
127  * Stage 1: Receive SID found and send request for group info
128  */
129 static void continue_name_found(struct composite_context *ctx)
130 {
131         struct composite_context *c;
132         struct group_info_state *s;
133         struct composite_context *info_req;
134
135         c = talloc_get_type(ctx->async.private_data, struct composite_context);
136         s = talloc_get_type(c->private_data, struct group_info_state);
137
138         /* receive SID assiociated with name found */
139         c->status = libnet_LookupName_recv(ctx, c, &s->lookup);
140         if (!composite_is_ok(c)) return;
141         
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);
146         }
147
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;
154
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;
158
159         /* set the next stage */
160         composite_continue(c, info_req, continue_group_info, c);
161 }
162
163
164 /*
165  * Stage 2: Receive group information
166  */
167 static void continue_group_info(struct composite_context *ctx)
168 {
169         struct composite_context *c;
170         struct group_info_state *s;
171
172         c = talloc_get_type(ctx->async.private_data, struct composite_context);
173         s = talloc_get_type(c->private_data, struct group_info_state);
174
175         /* receive group information */
176         c->status = libnet_rpc_groupinfo_recv(ctx, c, &s->info);
177         if (!composite_is_ok(c)) return;
178
179         /* we're done */
180         composite_done(c);
181 }
182
183
184 /*
185  * Receive group information
186  *
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
190  * @result nt status
191  */
192 NTSTATUS libnet_GroupInfo_recv(struct composite_context* c, TALLOC_CTX *mem_ctx,
193                                struct libnet_GroupInfo *io)
194 {
195         NTSTATUS status;
196         struct group_info_state *s;
197         
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);
202                 
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);
206
207                 io->out.error_string = talloc_strdup(mem_ctx, "Success");
208
209         } else {
210                 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
211         }
212
213         talloc_free(c);
214
215         return status;
216 }
217
218
219 /**
220  * Obtains specified group information
221  * 
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
225  */
226 NTSTATUS libnet_GroupInfo(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
227                           struct libnet_GroupInfo *io)
228 {
229         struct composite_context *c = libnet_GroupInfo_send(ctx, mem_ctx,
230                                                             io, NULL);
231         return libnet_GroupInfo_recv(c, mem_ctx, io);
232 }