85e60a27578f03ead8209d750460bf057b752b7b
[metze/samba/wip.git] / source3 / winbindd / winbindd_getgrnam.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_GETGRNAM
4    Copyright (C) Volker Lendecke 2009
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "winbindd.h"
22
23 struct winbindd_getgrnam_state {
24         struct tevent_context *ev;
25         fstring name_domain, name_group;
26         struct dom_sid sid;
27         const char *domname;
28         const char *name;
29         gid_t gid;
30         struct talloc_dict *members;
31 };
32
33 static void winbindd_getgrnam_lookupsid_done(struct tevent_req *subreq);
34 static void winbindd_getgrnam_done(struct tevent_req *subreq);
35
36 struct tevent_req *winbindd_getgrnam_send(TALLOC_CTX *mem_ctx,
37                                           struct tevent_context *ev,
38                                           struct winbindd_request *request)
39 {
40         struct tevent_req *req, *subreq;
41         struct winbindd_getgrnam_state *state;
42         struct winbindd_domain *domain;
43         char *tmp;
44         NTSTATUS nt_status;
45
46         req = tevent_req_create(mem_ctx, &state,
47                                 struct winbindd_getgrnam_state);
48         if (req == NULL) {
49                 return NULL;
50         }
51         state->ev = ev;
52
53         /* Ensure null termination */
54         request->data.groupname[sizeof(request->data.groupname)-1]='\0';
55
56         DEBUG(3, ("getgrnam %s\n", request->data.groupname));
57
58         nt_status = normalize_name_unmap(state, request->data.groupname, &tmp);
59         /* If we didn't map anything in the above call, just reset the
60            tmp pointer to the original string */
61         if (!NT_STATUS_IS_OK(nt_status) &&
62             !NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED))
63         {
64                 tmp = request->data.groupname;
65         }
66
67         /* Parse domain and groupname */
68
69         parse_domain_user(tmp, state->name_domain, state->name_group);
70
71         /* if no domain or our local domain and no local tdb group, default to
72          * our local domain for aliases */
73
74         if ( !*(state->name_domain) || strequal(state->name_domain,
75                                                 get_global_sam_name()) ) {
76                 fstrcpy(state->name_domain, get_global_sam_name());
77         }
78
79         /* Get info for the domain */
80
81         domain = find_domain_from_name_noinit(state->name_domain);
82         if (domain == NULL) {
83                 DEBUG(3, ("could not get domain sid for domain %s\n",
84                           state->name_domain));
85                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
86                 return tevent_req_post(req, ev);
87         }
88
89         /* should we deal with users for our domain? */
90
91         if ( lp_winbind_trusted_domains_only() && domain->primary) {
92                 DEBUG(7,("winbindd_getgrnam: My domain -- rejecting "
93                          "getgrnam() for %s\\%s.\n", state->name_domain,
94                          state->name_group));
95                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
96                 return tevent_req_post(req, ev);
97         }
98
99         subreq = wb_lookupname_send(state, ev, domain->name, state->name_group,
100                                     0);
101         if (tevent_req_nomem(subreq, req)) {
102                 return tevent_req_post(req, ev);
103         }
104         tevent_req_set_callback(subreq, winbindd_getgrnam_lookupsid_done,
105                                 req);
106         return req;
107 }
108
109 static void winbindd_getgrnam_lookupsid_done(struct tevent_req *subreq)
110 {
111         struct tevent_req *req = tevent_req_callback_data(
112                 subreq, struct tevent_req);
113         struct winbindd_getgrnam_state *state = tevent_req_data(
114                 req, struct winbindd_getgrnam_state);
115         enum lsa_SidType type;
116         NTSTATUS status;
117
118         status = wb_lookupname_recv(subreq, &state->sid, &type);
119         TALLOC_FREE(subreq);
120         if (!NT_STATUS_IS_OK(status)) {
121                 tevent_req_nterror(req, status);
122                 return;
123         }
124
125         if ( (type != SID_NAME_DOM_GRP) && (type != SID_NAME_ALIAS) ) {
126                 DEBUG(5,("getgrnam_recv: not a group!\n"));
127                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
128                 return;
129         }
130
131         subreq = wb_getgrsid_send(state, state->ev, &state->sid,
132                                   lp_winbind_expand_groups());
133         if (tevent_req_nomem(subreq, req)) {
134                 return;
135         }
136         tevent_req_set_callback(subreq, winbindd_getgrnam_done, req);
137 }
138
139 static void winbindd_getgrnam_done(struct tevent_req *subreq)
140 {
141         struct tevent_req *req = tevent_req_callback_data(
142                 subreq, struct tevent_req);
143         struct winbindd_getgrnam_state *state = tevent_req_data(
144                 req, struct winbindd_getgrnam_state);
145         NTSTATUS status;
146
147         status = wb_getgrsid_recv(subreq, state, &state->domname, &state->name,
148                                   &state->gid, &state->members);
149         TALLOC_FREE(subreq);
150         if (!NT_STATUS_IS_OK(status)) {
151                 tevent_req_nterror(req, status);
152                 return;
153         }
154         tevent_req_done(req);
155 }
156
157 NTSTATUS winbindd_getgrnam_recv(struct tevent_req *req,
158                                 struct winbindd_response *response)
159 {
160         struct winbindd_getgrnam_state *state = tevent_req_data(
161                 req, struct winbindd_getgrnam_state);
162         NTSTATUS status;
163         int num_members;
164         char *buf;
165
166         if (tevent_req_is_nterror(req, &status)) {
167                 DEBUG(5, ("Could not convert sid %s: %s\n",
168                           sid_string_dbg(&state->sid), nt_errstr(status)));
169                 return status;
170         }
171
172         if (!fill_grent(talloc_tos(), &response->data.gr, state->domname,
173                         state->name, state->gid)) {
174                 DEBUG(5, ("fill_grent failed\n"));
175                 return NT_STATUS_NO_MEMORY;
176         }
177
178         status = winbindd_print_groupmembers(state->members, response,
179                                              &num_members, &buf);
180
181         response->data.gr.num_gr_mem = (uint32)num_members;
182
183         /* Group membership lives at start of extra data */
184
185         response->data.gr.gr_mem_ofs = 0;
186         response->extra_data.data = buf;
187         response->length += talloc_get_size(response->extra_data.data);
188
189         return NT_STATUS_OK;
190 }