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