winbindd: Use dom_sid_str_buf
[samba.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 #include "libcli/security/dom_sid.h"
23
24 struct winbindd_getgrnam_state {
25         struct tevent_context *ev;
26         fstring name_namespace, name_domain, name_group;
27         struct dom_sid sid;
28         const char *domname;
29         const char *name;
30         gid_t gid;
31         struct db_context *members;
32 };
33
34 static void winbindd_getgrnam_lookupname_done(struct tevent_req *subreq);
35 static void winbindd_getgrnam_done(struct tevent_req *subreq);
36
37 struct tevent_req *winbindd_getgrnam_send(TALLOC_CTX *mem_ctx,
38                                           struct tevent_context *ev,
39                                           struct winbindd_cli_state *cli,
40                                           struct winbindd_request *request)
41 {
42         struct tevent_req *req, *subreq;
43         struct winbindd_getgrnam_state *state;
44         char *tmp;
45         NTSTATUS nt_status;
46         bool ok;
47
48         req = tevent_req_create(mem_ctx, &state,
49                                 struct winbindd_getgrnam_state);
50         if (req == NULL) {
51                 return NULL;
52         }
53         state->ev = ev;
54
55         /* Ensure null termination */
56         request->data.groupname[sizeof(request->data.groupname)-1]='\0';
57
58         DBG_NOTICE("[%s (%u)] getgrnam %s\n",
59                    cli->client_name,
60                    (unsigned int)cli->pid,
61                    request->data.groupname);
62
63         nt_status = normalize_name_unmap(state, request->data.groupname, &tmp);
64         /* If we didn't map anything in the above call, just reset the
65            tmp pointer to the original string */
66         if (!NT_STATUS_IS_OK(nt_status) &&
67             !NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED))
68         {
69                 tmp = request->data.groupname;
70         }
71
72         /* Parse domain and groupname */
73
74         ok = parse_domain_user(tmp,
75                                state->name_namespace,
76                                state->name_domain,
77                                state->name_group);
78         if (!ok) {
79                 DBG_INFO("Could not parse domain user: %s\n", tmp);
80                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
81                 return tevent_req_post(req, ev);
82         }
83
84         /* if no domain or our local domain and no local tdb group, default to
85          * our local domain for aliases */
86
87         if ( !*(state->name_domain) || strequal(state->name_domain,
88                                                 get_global_sam_name()) ) {
89                 fstrcpy(state->name_domain, get_global_sam_name());
90         }
91
92         subreq = wb_lookupname_send(state, ev,
93                                     state->name_namespace,
94                                     state->name_domain,
95                                     state->name_group,
96                                     0);
97         if (tevent_req_nomem(subreq, req)) {
98                 return tevent_req_post(req, ev);
99         }
100         tevent_req_set_callback(subreq, winbindd_getgrnam_lookupname_done,
101                                 req);
102         return req;
103 }
104
105 static void winbindd_getgrnam_lookupname_done(struct tevent_req *subreq)
106 {
107         struct tevent_req *req = tevent_req_callback_data(
108                 subreq, struct tevent_req);
109         struct winbindd_getgrnam_state *state = tevent_req_data(
110                 req, struct winbindd_getgrnam_state);
111         enum lsa_SidType type;
112         NTSTATUS status;
113
114         status = wb_lookupname_recv(subreq, &state->sid, &type);
115         TALLOC_FREE(subreq);
116         if (tevent_req_nterror(req, status)) {
117                 return;
118         }
119
120         switch (type) {
121         case SID_NAME_DOM_GRP:
122         case SID_NAME_ALIAS:
123         case SID_NAME_WKN_GRP:
124         /*
125          * Also give user types a chance:
126          * These might be user sids mapped to the ID_TYPE_BOTH,
127          * and in that case we should construct a group struct.
128          */
129         case SID_NAME_USER:
130         case SID_NAME_COMPUTER:
131                 break;
132         default:
133                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
134                 return;
135         }
136
137         subreq = wb_getgrsid_send(state, state->ev, &state->sid,
138                                   lp_winbind_expand_groups());
139         if (tevent_req_nomem(subreq, req)) {
140                 return;
141         }
142         tevent_req_set_callback(subreq, winbindd_getgrnam_done, req);
143 }
144
145 static void winbindd_getgrnam_done(struct tevent_req *subreq)
146 {
147         struct tevent_req *req = tevent_req_callback_data(
148                 subreq, struct tevent_req);
149         struct winbindd_getgrnam_state *state = tevent_req_data(
150                 req, struct winbindd_getgrnam_state);
151         NTSTATUS status;
152
153         status = wb_getgrsid_recv(subreq, state, &state->domname, &state->name,
154                                   &state->gid, &state->members);
155         TALLOC_FREE(subreq);
156         if (tevent_req_nterror(req, status)) {
157                 return;
158         }
159         tevent_req_done(req);
160 }
161
162 NTSTATUS winbindd_getgrnam_recv(struct tevent_req *req,
163                                 struct winbindd_response *response)
164 {
165         struct winbindd_getgrnam_state *state = tevent_req_data(
166                 req, struct winbindd_getgrnam_state);
167         NTSTATUS status;
168         int num_members;
169         char *buf;
170
171         if (tevent_req_is_nterror(req, &status)) {
172                 struct dom_sid_buf sidbuf;
173                 DEBUG(5, ("Could not convert sid %s: %s\n",
174                           dom_sid_str_buf(&state->sid, &sidbuf),
175                           nt_errstr(status)));
176                 return status;
177         }
178
179         if (!fill_grent(talloc_tos(), &response->data.gr, state->domname,
180                         state->name, state->gid)) {
181                 DEBUG(5, ("fill_grent failed\n"));
182                 return NT_STATUS_NO_MEMORY;
183         }
184
185         status = winbindd_print_groupmembers(state->members, response,
186                                              &num_members, &buf);
187         if (!NT_STATUS_IS_OK(status)) {
188                 return status;
189         }
190
191         response->data.gr.num_gr_mem = (uint32_t)num_members;
192
193         /* Group membership lives at start of extra data */
194
195         response->data.gr.gr_mem_ofs = 0;
196         response->extra_data.data = buf;
197         response->length += talloc_get_size(response->extra_data.data);
198
199         return NT_STATUS_OK;
200 }