s4 winbind: Add implementation for WINBINDD_GETGRNAM
[kai/samba.git] / source4 / winbind / wb_cmd_getgrnam.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Command backend for wbinfo --group-info
5
6    Copyright (C) Kai Blin 2008
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 "winbind/wb_async_helpers.h"
26 #include "param/param.h"
27 #include "winbind/wb_helper.h"
28 #include "smbd/service_task.h"
29 #include "libnet/libnet_proto.h"
30 #include "libcli/security/proto.h"
31
32 struct cmd_getgrnam_state {
33         struct composite_context *ctx;
34         struct wbsrv_service *service;
35         char *name;
36         char *workgroup_name;
37         struct dom_sid *group_sid;
38
39         struct winbindd_gr *result;
40 };
41
42 static void cmd_getgrnam_recv_domain(struct composite_context *ctx);
43 static void cmd_getgrnam_recv_group_info(struct composite_context *ctx);
44 static void cmd_getgrnam_recv_gid(struct composite_context *ctx);
45
46 struct composite_context *wb_cmd_getgrnam_send(TALLOC_CTX *mem_ctx,
47                                                  struct wbsrv_service *service,
48                                                  const char *name)
49 {
50         struct composite_context *result, *ctx;
51         struct cmd_getgrnam_state *state;
52
53         DEBUG(5, ("wb_cmd_getgrnam_send called\n"));
54
55         result = composite_create(mem_ctx, service->task->event_ctx);
56         if (!result) return NULL;
57
58         state = talloc(result, struct cmd_getgrnam_state);
59         if (composite_nomem(state, result)) return result;
60         state->ctx = result;
61         result->private_data = state;
62         state->service = service;
63         state->name = talloc_strdup(state, name);
64         if(composite_nomem(state->name, result)) return result;
65
66         ctx = wb_name2domain_send(state, service, name);
67         if (composite_nomem(ctx, result)) return result;
68
69         composite_continue(result, ctx, cmd_getgrnam_recv_domain, state);
70         return result;
71 }
72
73 static void cmd_getgrnam_recv_domain(struct composite_context *ctx)
74 {
75         struct cmd_getgrnam_state *state = talloc_get_type(
76                         ctx->async.private_data, struct cmd_getgrnam_state);
77         struct wbsrv_domain *domain;
78         struct libnet_GroupInfo *group_info;
79         char *group_dom, *group_name;
80         bool ok;
81
82         state->ctx->status = wb_name2domain_recv(ctx, &domain);
83         if(!composite_is_ok(state->ctx)) return;
84
85         group_info = talloc(state, struct libnet_GroupInfo);
86         if (composite_nomem(group_info, state->ctx)) return;
87
88         ok = wb_samba3_split_username(state, state->service->task->lp_ctx,
89                                       state->name, &group_dom, &group_name);
90         if(!ok){
91                 composite_error(state->ctx, NT_STATUS_OBJECT_NAME_INVALID);
92                 return;
93         }
94
95         group_info->in.group_name = group_name;
96         group_info->in.domain_name = group_dom;
97         state->workgroup_name = talloc_strdup(state, group_dom);
98         if(composite_nomem(state->workgroup_name, state->ctx)) return;
99
100         ctx = libnet_GroupInfo_send(domain->libnet_ctx, state, group_info,NULL);
101
102         composite_continue(state->ctx, ctx, cmd_getgrnam_recv_group_info,state);
103 }
104
105 static void cmd_getgrnam_recv_group_info(struct composite_context *ctx)
106 {
107         struct cmd_getgrnam_state *state = talloc_get_type(
108                         ctx->async.private_data, struct cmd_getgrnam_state);
109         struct libnet_GroupInfo *group_info;
110         struct winbindd_gr *gr;
111
112         DEBUG(5, ("cmd_getgrnam_recv_group_info called\n"));
113
114         group_info = talloc(state, struct libnet_GroupInfo);
115         if(composite_nomem(group_info, state->ctx)) return;
116
117         gr = talloc(state, struct winbindd_gr);
118         if(composite_nomem(gr, state->ctx)) return;
119
120         state->ctx->status = libnet_GroupInfo_recv(ctx, state, group_info);
121         if(!composite_is_ok(state->ctx)) return;
122
123         WBSRV_SAMBA3_SET_STRING(gr->gr_name, group_info->out.group_name);
124         WBSRV_SAMBA3_SET_STRING(gr->gr_passwd, "*");
125         gr->num_gr_mem = group_info->out.num_members;
126         gr->gr_mem_ofs = 0;
127
128         state->result = gr;
129
130         ctx = wb_sid2gid_send(state, state->service, group_info->out.group_sid);
131         composite_continue(state->ctx, ctx, cmd_getgrnam_recv_gid, state);
132 }
133
134 static void cmd_getgrnam_recv_gid(struct composite_context *ctx)
135 {
136         struct cmd_getgrnam_state *state = talloc_get_type(
137                         ctx->async.private_data, struct cmd_getgrnam_state);
138         gid_t gid;
139
140         DEBUG(5, ("cmd_getgrnam_recv_gid called\n"));
141
142         state->ctx->status = wb_sid2gid_recv(ctx, &gid);
143         if(!composite_is_ok(state->ctx)) return;
144
145         state->result->gr_gid = gid;
146
147         composite_done(state->ctx);
148 }
149
150 NTSTATUS wb_cmd_getgrnam_recv(struct composite_context *ctx,
151                 TALLOC_CTX *mem_ctx, struct winbindd_gr **gr)
152 {
153         NTSTATUS status = composite_wait(ctx);
154
155         DEBUG(5, ("wb_cmd_getgrnam_recv called\n"));
156
157         if (NT_STATUS_IS_OK(status)) {
158                 struct cmd_getgrnam_state *state =
159                         talloc_get_type(ctx->private_data,
160                                         struct cmd_getgrnam_state);
161                 *gr = talloc_steal(mem_ctx, state->result);
162         }
163         talloc_free(ctx);
164         return status;
165
166 }
167