s4-idmap: Add mapping using uidNumber and gidNumber like idmap_ad
[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
27 struct cmd_getgrgid_state {
28         struct composite_context *ctx;
29         struct wbsrv_service *service;
30         gid_t gid;
31         struct dom_sid *sid;
32         char *workgroup;
33         struct wbsrv_domain *domain;
34
35         struct winbindd_gr *result;
36 };
37
38 static void cmd_getgrgid_recv_sid(struct composite_context *ctx);
39 static void cmd_getgrgid_recv_domain(struct composite_context *ctx);
40 static void cmd_getgrgid_recv_group_info(struct composite_context *ctx);
41
42 /* Get the SID using the gid */
43
44 struct composite_context *wb_cmd_getgrgid_send(TALLOC_CTX *mem_ctx,
45                                                  struct wbsrv_service *service,
46                                                  gid_t gid)
47 {
48         struct composite_context *ctx, *result;
49         struct cmd_getgrgid_state *state;
50
51         DEBUG(5, ("wb_cmd_getgrgid_send called\n"));
52
53         result = composite_create(mem_ctx, service->task->event_ctx);
54         if (!result) return NULL;
55
56         state = talloc(result, struct cmd_getgrgid_state);
57         if (composite_nomem(state, result)) return result;
58         state->ctx = result;
59         result->private_data = state;
60         state->service = service;
61         state->gid = gid;
62
63         ctx = wb_gid2sid_send(state, service, gid);
64         if (composite_nomem(ctx, state->ctx)) return result;
65
66         composite_continue(result, ctx, cmd_getgrgid_recv_sid, state);
67         return result;
68 }
69
70
71 /* Receive the sid and get the domain structure with it */
72
73 static void cmd_getgrgid_recv_sid(struct composite_context *ctx)
74 {
75         struct cmd_getgrgid_state *state =
76                 talloc_get_type(ctx->async.private_data,
77                                 struct cmd_getgrgid_state);
78
79         DEBUG(5, ("cmd_getgrgid_recv_sid called %p\n", ctx->private_data));
80
81         state->ctx->status = wb_gid2sid_recv(ctx, state, &state->sid);
82         if (!composite_is_ok(state->ctx)) return;
83
84         ctx = wb_sid2domain_send(state, state->service, state->sid);
85
86         composite_continue(state->ctx, ctx, cmd_getgrgid_recv_domain, state);
87 }
88
89 /* Receive the domain struct and call libnet to get the user info struct */
90
91 static void cmd_getgrgid_recv_domain(struct composite_context *ctx)
92 {
93         struct cmd_getgrgid_state *state =
94                 talloc_get_type(ctx->async.private_data,
95                                 struct cmd_getgrgid_state);
96         struct libnet_GroupInfo *group_info;
97
98         DEBUG(5, ("cmd_getgrgid_recv_domain called\n"));
99
100         state->ctx->status = wb_sid2domain_recv(ctx, &state->domain);
101         if (!composite_is_ok(state->ctx)) return;
102
103         group_info = talloc(state, struct libnet_GroupInfo);
104         if (composite_nomem(group_info, state->ctx)) return;
105
106         group_info->in.level = GROUP_INFO_BY_SID;
107         group_info->in.data.group_sid = state->sid;
108         group_info->in.domain_name = state->domain->libnet_ctx->samr.name;
109
110         /* We need the workgroup later, so copy it  */
111         state->workgroup = talloc_strdup(state,
112                         state->domain->libnet_ctx->samr.name);
113         if (composite_nomem(state->workgroup, state->ctx)) return;
114
115         ctx = libnet_GroupInfo_send(state->domain->libnet_ctx, state,group_info,
116                         NULL);
117
118         composite_continue(state->ctx, ctx, cmd_getgrgid_recv_group_info,state);
119 }
120
121 /* Receive the group info struct */
122
123 static void cmd_getgrgid_recv_group_info(struct composite_context *ctx)
124 {
125         struct cmd_getgrgid_state *state =
126                 talloc_get_type(ctx->async.private_data,
127                                 struct cmd_getgrgid_state);
128         struct libnet_GroupInfo *group_info;
129         struct winbindd_gr *gr;
130
131         DEBUG(5, ("cmd_getgrgid_recv_group_info called\n"));
132
133         gr = talloc_zero(state, struct winbindd_gr);
134         if (composite_nomem(gr, state->ctx)) return;
135
136         group_info = talloc(state, struct libnet_GroupInfo);
137         if(composite_nomem(group_info, state->ctx)) return;
138
139         state->ctx->status = libnet_GroupInfo_recv(ctx, state, group_info);
140         if (!composite_is_ok(state->ctx)) return;
141
142         WBSRV_SAMBA3_SET_STRING(gr->gr_name, group_info->out.group_name);
143         WBSRV_SAMBA3_SET_STRING(gr->gr_passwd, "*");
144
145         gr->gr_gid = state->gid;
146
147         state->result = gr;
148
149         composite_done(state->ctx);
150 }
151
152 NTSTATUS wb_cmd_getgrgid_recv(struct composite_context *ctx,
153                 TALLOC_CTX *mem_ctx, struct winbindd_gr **gr)
154 {
155         NTSTATUS status = composite_wait(ctx);
156
157         DEBUG(5, ("wb_cmd_getgrgid_recv called\n"));
158
159         DEBUG(5, ("status is %s\n", nt_errstr(status)));
160
161         if (NT_STATUS_IS_OK(status)) {
162                 struct cmd_getgrgid_state *state =
163                         talloc_get_type(ctx->private_data,
164                                         struct cmd_getgrgid_state);
165                 *gr = talloc_steal(mem_ctx, state->result);
166         }
167         talloc_free(ctx);
168         return status;
169
170 }
171