librpc: Shorten dcerpc_binding_handle_call a bit
[Samba/gebeck_regimport.git] / source4 / winbind / wb_cmd_getgrnam.c
blobd75a460bd065f697521d14944e0a5051dd76c10f
1 /*
2 Unix SMB/CIFS implementation.
4 Command backend for wbinfo --group-info
6 Copyright (C) Kai Blin 2008
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 "winbind/wb_helper.h"
26 #include "smbd/service_task.h"
27 #include "param/param.h"
29 struct cmd_getgrnam_state {
30 struct composite_context *ctx;
31 struct wbsrv_service *service;
32 char *name;
33 char *workgroup_name;
34 struct dom_sid *group_sid;
36 struct winbindd_gr *result;
39 static void cmd_getgrnam_recv_domain(struct composite_context *ctx);
40 static void cmd_getgrnam_recv_group_info(struct composite_context *ctx);
41 static void cmd_getgrnam_recv_gid(struct composite_context *ctx);
43 struct composite_context *wb_cmd_getgrnam_send(TALLOC_CTX *mem_ctx,
44 struct wbsrv_service *service,
45 const char *name)
47 struct composite_context *result, *ctx;
48 struct cmd_getgrnam_state *state;
50 DEBUG(5, ("wb_cmd_getgrnam_send called\n"));
52 result = composite_create(mem_ctx, service->task->event_ctx);
53 if (!result) return NULL;
55 state = talloc(result, struct cmd_getgrnam_state);
56 if (composite_nomem(state, result)) return result;
57 state->ctx = result;
58 result->private_data = state;
59 state->service = service;
60 state->name = talloc_strdup(state, name);
61 if(composite_nomem(state->name, result)) return result;
63 ctx = wb_name2domain_send(state, service, name);
64 if (composite_nomem(ctx, result)) return result;
66 composite_continue(result, ctx, cmd_getgrnam_recv_domain, state);
67 return result;
70 static void cmd_getgrnam_recv_domain(struct composite_context *ctx)
72 struct cmd_getgrnam_state *state = talloc_get_type(
73 ctx->async.private_data, struct cmd_getgrnam_state);
74 struct wbsrv_domain *domain;
75 struct libnet_GroupInfo *group_info;
76 char *group_dom, *group_name;
77 bool ok;
79 state->ctx->status = wb_name2domain_recv(ctx, &domain);
80 if(!composite_is_ok(state->ctx)) return;
82 group_info = talloc(state, struct libnet_GroupInfo);
83 if (composite_nomem(group_info, state->ctx)) return;
85 ok = wb_samba3_split_username(state, state->service->task->lp_ctx,
86 state->name, &group_dom, &group_name);
87 if(!ok){
88 composite_error(state->ctx, NT_STATUS_OBJECT_NAME_INVALID);
89 return;
92 group_info->in.level = GROUP_INFO_BY_NAME;
93 group_info->in.data.group_name = group_name;
94 group_info->in.domain_name = group_dom;
95 state->workgroup_name = talloc_strdup(state, group_dom);
96 if(composite_nomem(state->workgroup_name, state->ctx)) return;
98 ctx = libnet_GroupInfo_send(domain->libnet_ctx, state, group_info,NULL);
100 composite_continue(state->ctx, ctx, cmd_getgrnam_recv_group_info,state);
103 static void cmd_getgrnam_recv_group_info(struct composite_context *ctx)
105 struct cmd_getgrnam_state *state = talloc_get_type(
106 ctx->async.private_data, struct cmd_getgrnam_state);
107 struct libnet_GroupInfo *group_info;
108 struct winbindd_gr *gr;
109 char *group_name_with_domain;
111 DEBUG(5, ("cmd_getgrnam_recv_group_info called\n"));
113 group_info = talloc(state, struct libnet_GroupInfo);
114 if(composite_nomem(group_info, state->ctx)) return;
116 gr = talloc(state, struct winbindd_gr);
117 if(composite_nomem(gr, state->ctx)) return;
119 state->ctx->status = libnet_GroupInfo_recv(ctx, state, group_info);
120 if(!composite_is_ok(state->ctx)) return;
122 group_name_with_domain = talloc_asprintf(gr, "%s%s%s",
123 state->workgroup_name,
124 lpcfg_winbind_separator(state->service->task->lp_ctx),
125 group_info->out.group_name);
126 if (composite_nomem(group_name_with_domain, state->ctx)) {
127 return;
130 WBSRV_SAMBA3_SET_STRING(gr->gr_name, group_name_with_domain);
131 WBSRV_SAMBA3_SET_STRING(gr->gr_passwd, "*");
132 gr->num_gr_mem = group_info->out.num_members;
133 gr->gr_mem_ofs = 0;
135 state->result = gr;
137 ctx = wb_sid2gid_send(state, state->service, group_info->out.group_sid);
138 composite_continue(state->ctx, ctx, cmd_getgrnam_recv_gid, state);
141 static void cmd_getgrnam_recv_gid(struct composite_context *ctx)
143 struct cmd_getgrnam_state *state = talloc_get_type(
144 ctx->async.private_data, struct cmd_getgrnam_state);
145 gid_t gid;
147 DEBUG(5, ("cmd_getgrnam_recv_gid called\n"));
149 state->ctx->status = wb_sid2gid_recv(ctx, &gid);
150 if(!composite_is_ok(state->ctx)) return;
152 state->result->gr_gid = gid;
154 composite_done(state->ctx);
157 NTSTATUS wb_cmd_getgrnam_recv(struct composite_context *ctx,
158 TALLOC_CTX *mem_ctx, struct winbindd_gr **gr)
160 NTSTATUS status = composite_wait(ctx);
162 DEBUG(5, ("wb_cmd_getgrnam_recv called\n"));
164 if (NT_STATUS_IS_OK(status)) {
165 struct cmd_getgrnam_state *state =
166 talloc_get_type(ctx->private_data,
167 struct cmd_getgrnam_state);
168 *gr = talloc_steal(mem_ctx, state->result);
170 talloc_free(ctx);
171 return status;