s4:winbind: let wb_lsa_lookupsids_send() take tevent_context/dcerpc_binding_handle
[samba.git] / source4 / winbind / wb_cmd_getgrgid.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Backend for getgrgid
5
6    Copyright (C) Kai Blin 2007
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/composite/composite.h"
24 #include "winbind/wb_server.h"
25 #include "smbd/service_task.h"
26 #include "param/param.h"
27
28 struct cmd_getgrgid_state {
29         struct composite_context *ctx;
30         struct wbsrv_service *service;
31         gid_t gid;
32         struct dom_sid *sid;
33         char *workgroup;
34         struct wbsrv_domain *domain;
35
36         struct winbindd_gr *result;
37 };
38
39 static void cmd_getgrgid_recv_sid(struct composite_context *ctx);
40 static void cmd_getgrgid_recv_domain(struct composite_context *ctx);
41 static void cmd_getgrgid_recv_group_info(struct composite_context *ctx);
42
43 /* Get the SID using the gid */
44
45 struct composite_context *wb_cmd_getgrgid_send(TALLOC_CTX *mem_ctx,
46                                                  struct wbsrv_service *service,
47                                                  gid_t gid)
48 {
49         struct composite_context *ctx, *result;
50         struct cmd_getgrgid_state *state;
51
52         DEBUG(5, ("wb_cmd_getgrgid_send called\n"));
53
54         result = composite_create(mem_ctx, service->task->event_ctx);
55         if (!result) return NULL;
56
57         state = talloc(result, struct cmd_getgrgid_state);
58         if (composite_nomem(state, result)) return result;
59         state->ctx = result;
60         result->private_data = state;
61         state->service = service;
62         state->gid = gid;
63
64         ctx = wb_gid2sid_send(state, service, gid);
65         if (composite_nomem(ctx, state->ctx)) return result;
66
67         composite_continue(result, ctx, cmd_getgrgid_recv_sid, state);
68         return result;
69 }
70
71
72 /* Receive the sid and get the domain structure with it */
73
74 static void cmd_getgrgid_recv_sid(struct composite_context *ctx)
75 {
76         struct cmd_getgrgid_state *state =
77                 talloc_get_type(ctx->async.private_data,
78                                 struct cmd_getgrgid_state);
79
80         DEBUG(5, ("cmd_getgrgid_recv_sid called %p\n", ctx->private_data));
81
82         state->ctx->status = wb_gid2sid_recv(ctx, state, &state->sid);
83         if (!composite_is_ok(state->ctx)) return;
84
85         ctx = wb_sid2domain_send(state, state->service, state->sid);
86
87         composite_continue(state->ctx, ctx, cmd_getgrgid_recv_domain, state);
88 }
89
90 /* Receive the domain struct and call libnet to get the user info struct */
91
92 static void cmd_getgrgid_recv_domain(struct composite_context *ctx)
93 {
94         struct cmd_getgrgid_state *state =
95                 talloc_get_type(ctx->async.private_data,
96                                 struct cmd_getgrgid_state);
97         struct libnet_GroupInfo *group_info;
98
99         DEBUG(5, ("cmd_getgrgid_recv_domain called\n"));
100
101         state->ctx->status = wb_sid2domain_recv(ctx, &state->domain);
102         if (!composite_is_ok(state->ctx)) return;
103
104         group_info = talloc(state, struct libnet_GroupInfo);
105         if (composite_nomem(group_info, state->ctx)) return;
106
107         group_info->in.level = GROUP_INFO_BY_SID;
108         group_info->in.data.group_sid = state->sid;
109         group_info->in.domain_name = state->domain->libnet_ctx->samr.name;
110
111         /* We need the workgroup later, so copy it  */
112         state->workgroup = talloc_strdup(state,
113                         state->domain->libnet_ctx->samr.name);
114         if (composite_nomem(state->workgroup, state->ctx)) return;
115
116         ctx = libnet_GroupInfo_send(state->domain->libnet_ctx, state,group_info,
117                         NULL);
118
119         composite_continue(state->ctx, ctx, cmd_getgrgid_recv_group_info,state);
120 }
121
122 /* Receive the group info struct */
123
124 static void cmd_getgrgid_recv_group_info(struct composite_context *ctx)
125 {
126         struct cmd_getgrgid_state *state =
127                 talloc_get_type(ctx->async.private_data,
128                                 struct cmd_getgrgid_state);
129         struct libnet_GroupInfo *group_info;
130         struct winbindd_gr *gr;
131         char *group_name_with_domain;
132
133         DEBUG(5, ("cmd_getgrgid_recv_group_info called\n"));
134
135         gr = talloc_zero(state, struct winbindd_gr);
136         if (composite_nomem(gr, state->ctx)) return;
137
138         group_info = talloc(state, struct libnet_GroupInfo);
139         if(composite_nomem(group_info, state->ctx)) return;
140
141         state->ctx->status = libnet_GroupInfo_recv(ctx, state, group_info);
142         if (!composite_is_ok(state->ctx)) return;
143
144         group_name_with_domain = talloc_asprintf(gr, "%s%s%s",
145                 state->workgroup,
146                 lpcfg_winbind_separator(state->service->task->lp_ctx),
147                 group_info->out.group_name);
148         if (composite_nomem(group_name_with_domain, state->ctx)) {
149                 return;
150         }
151
152         WBSRV_SAMBA3_SET_STRING(gr->gr_name, group_name_with_domain);
153         WBSRV_SAMBA3_SET_STRING(gr->gr_passwd, "*");
154
155         gr->gr_gid = state->gid;
156
157         state->result = gr;
158
159         composite_done(state->ctx);
160 }
161
162 NTSTATUS wb_cmd_getgrgid_recv(struct composite_context *ctx,
163                 TALLOC_CTX *mem_ctx, struct winbindd_gr **gr)
164 {
165         NTSTATUS status = composite_wait(ctx);
166
167         DEBUG(5, ("wb_cmd_getgrgid_recv called\n"));
168
169         DEBUG(5, ("status is %s\n", nt_errstr(status)));
170
171         if (NT_STATUS_IS_OK(status)) {
172                 struct cmd_getgrgid_state *state =
173                         talloc_get_type(ctx->private_data,
174                                         struct cmd_getgrgid_state);
175                 *gr = talloc_steal(mem_ctx, state->result);
176         }
177         talloc_free(ctx);
178         return status;
179
180 }
181