torture-krb5: Do not do post-recv checks if the packet recv failed
[Samba.git] / source4 / winbind / wb_cmd_getgrgid.c
blob8ca93db38ee468b24dcb248ef580618e22dcd455
1 /*
2 Unix SMB/CIFS implementation.
4 Backend for getgrgid
6 Copyright (C) Kai Blin 2007
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.
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.
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/>.
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"
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;
36 struct winbindd_gr *result;
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);
43 /* Get the SID using the gid */
45 struct composite_context *wb_cmd_getgrgid_send(TALLOC_CTX *mem_ctx,
46 struct wbsrv_service *service,
47 gid_t gid)
49 struct composite_context *ctx, *result;
50 struct cmd_getgrgid_state *state;
52 DEBUG(5, ("wb_cmd_getgrgid_send called\n"));
54 result = composite_create(mem_ctx, service->task->event_ctx);
55 if (!result) return NULL;
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;
64 ctx = wb_gid2sid_send(state, service, gid);
65 if (composite_nomem(ctx, state->ctx)) return result;
67 composite_continue(result, ctx, cmd_getgrgid_recv_sid, state);
68 return result;
72 /* Receive the sid and get the domain structure with it */
74 static void cmd_getgrgid_recv_sid(struct composite_context *ctx)
76 struct cmd_getgrgid_state *state =
77 talloc_get_type(ctx->async.private_data,
78 struct cmd_getgrgid_state);
80 DEBUG(5, ("cmd_getgrgid_recv_sid called %p\n", ctx->private_data));
82 state->ctx->status = wb_gid2sid_recv(ctx, state, &state->sid);
83 if (!composite_is_ok(state->ctx)) return;
85 ctx = wb_sid2domain_send(state, state->service, state->sid);
87 composite_continue(state->ctx, ctx, cmd_getgrgid_recv_domain, state);
90 /* Receive the domain struct and call libnet to get the user info struct */
92 static void cmd_getgrgid_recv_domain(struct composite_context *ctx)
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;
99 DEBUG(5, ("cmd_getgrgid_recv_domain called\n"));
101 state->ctx->status = wb_sid2domain_recv(ctx, &state->domain);
102 if (!composite_is_ok(state->ctx)) return;
104 group_info = talloc(state, struct libnet_GroupInfo);
105 if (composite_nomem(group_info, state->ctx)) return;
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;
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;
116 ctx = libnet_GroupInfo_send(state->domain->libnet_ctx, state,group_info,
117 NULL);
119 composite_continue(state->ctx, ctx, cmd_getgrgid_recv_group_info,state);
122 /* Receive the group info struct */
124 static void cmd_getgrgid_recv_group_info(struct composite_context *ctx)
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;
133 DEBUG(5, ("cmd_getgrgid_recv_group_info called\n"));
135 gr = talloc_zero(state, struct winbindd_gr);
136 if (composite_nomem(gr, state->ctx)) return;
138 group_info = talloc(state, struct libnet_GroupInfo);
139 if(composite_nomem(group_info, state->ctx)) return;
141 state->ctx->status = libnet_GroupInfo_recv(ctx, state, group_info);
142 if (!composite_is_ok(state->ctx)) return;
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;
152 WBSRV_SAMBA3_SET_STRING(gr->gr_name, group_name_with_domain);
153 WBSRV_SAMBA3_SET_STRING(gr->gr_passwd, "*");
155 gr->gr_gid = state->gid;
157 state->result = gr;
159 composite_done(state->ctx);
162 NTSTATUS wb_cmd_getgrgid_recv(struct composite_context *ctx,
163 TALLOC_CTX *mem_ctx, struct winbindd_gr **gr)
165 NTSTATUS status = composite_wait(ctx);
167 DEBUG(5, ("wb_cmd_getgrgid_recv called\n"));
169 DEBUG(5, ("status is %s\n", nt_errstr(status)));
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);
177 talloc_free(ctx);
178 return status;