s4-winbindd: fixed handling of extra_data in s3 requests
[samba.git] / source4 / winbind / wb_samba3_protocol.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Main winbindd samba3 server routines
4
5    Copyright (C) Stefan Metzmacher      2005
6    Copyright (C) Volker Lendecke        2005
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 "winbind/wb_server.h"
24 #include "smbd/service_stream.h"
25 #include "lib/stream/packet.h"
26 #include "lib/tsocket/tsocket.h"
27
28 /*
29   work out if a packet is complete for protocols that use a 32 bit host byte
30   order length
31 */
32 NTSTATUS wbsrv_samba3_packet_full_request(void *private_data, DATA_BLOB blob, size_t *size)
33 {
34         uint32_t *len;
35         struct winbindd_request *req;
36
37         if (blob.length < 4) {
38                 return STATUS_MORE_ENTRIES;
39         }
40         len = (uint32_t *)blob.data;
41         *size = (*len);
42         if (*size > blob.length) {
43                 return STATUS_MORE_ENTRIES;
44         }
45         if (*size < sizeof(req)) {
46                 /* its not a valid winbind packet. We need to accept
47                    it here, and wbsrv_samba3_pull_request() will throw
48                    it away */
49                 return NT_STATUS_OK;
50         }
51
52         /* now we need to cope with possible extra_data, which is
53            stuck on the end with no length prefix! This is a very very
54            stupid protocol */
55         req = (struct winbindd_request *)blob.data;
56         *size = (*len) + req->extra_len;
57         if (*size > blob.length) {
58                 return STATUS_MORE_ENTRIES;
59         }
60         return NT_STATUS_OK;
61 }
62
63
64 NTSTATUS wbsrv_samba3_pull_request(struct wbsrv_samba3_call *call)
65 {
66         if (call->in.length < sizeof(*call->request)) {
67                 DEBUG(0,("wbsrv_samba3_pull_request: invalid blob length %lu should be %lu\n"
68                          " make sure you use the correct winbind client tools!\n",
69                          (long)call->in.length, (long)sizeof(*call->request)));
70                 return NT_STATUS_INVALID_PARAMETER;
71         }
72
73         call->request = talloc_zero(call, struct winbindd_request);
74         NT_STATUS_HAVE_NO_MEMORY(call->request);
75
76         /* the packet layout is the same as the in memory layout of the request, so just copy it */
77         memcpy(call->request, call->in.data, sizeof(*call->request));
78
79         if (call->in.length != sizeof(*call->request) + call->request->extra_len) {
80                 DEBUG(0,(__location__ " : invalid extra_len %u should be %u\n",
81                          call->request->extra_len, (unsigned)(call->in.length - sizeof(*call->request))));
82                 return NT_STATUS_INVALID_PARAMETER;
83         }
84
85         /* there may be extra data */
86         if (call->request->extra_len != 0) {
87                 call->request->extra_data.data = talloc_size(call->request, call->request->extra_len+1);
88                 NT_STATUS_HAVE_NO_MEMORY(call->request->extra_data.data);
89                 /* guarantee a nul termination, as many of the uses of
90                    this field is for strings */
91                 memcpy(call->request->extra_data.data, call->in.data + sizeof(*call->request),
92                        call->request->extra_len);
93                 call->request->extra_data.data[call->request->extra_len] = 0;
94         } else {
95                 call->request->extra_data.data = NULL;
96         }
97
98         return NT_STATUS_OK;
99 }
100
101 NTSTATUS wbsrv_samba3_handle_call(struct wbsrv_samba3_call *s3call)
102 {
103         DEBUG(10, ("Got winbind samba3 request %d\n", s3call->request->cmd));
104
105         s3call->response = talloc_zero(s3call, struct winbindd_response);
106         NT_STATUS_HAVE_NO_MEMORY(s3call->request);
107
108         s3call->response->length = sizeof(*s3call->response);
109
110         switch(s3call->request->cmd) {
111         case WINBINDD_INTERFACE_VERSION:
112                 return wbsrv_samba3_interface_version(s3call);
113
114         case WINBINDD_CHECK_MACHACC:
115                 return wbsrv_samba3_check_machacc(s3call);
116
117         case WINBINDD_PING:
118                 return wbsrv_samba3_ping(s3call);
119
120         case WINBINDD_INFO:
121                 return wbsrv_samba3_info(s3call);
122
123         case WINBINDD_DOMAIN_NAME:
124                 return wbsrv_samba3_domain_name(s3call);
125
126         case WINBINDD_NETBIOS_NAME:
127                 return wbsrv_samba3_netbios_name(s3call);
128
129         case WINBINDD_PRIV_PIPE_DIR:
130                 return wbsrv_samba3_priv_pipe_dir(s3call);
131
132         case WINBINDD_LOOKUPNAME:
133                 return wbsrv_samba3_lookupname(s3call);
134
135         case WINBINDD_LOOKUPSID:
136                 return wbsrv_samba3_lookupsid(s3call);
137
138         case WINBINDD_PAM_AUTH:
139                 return wbsrv_samba3_pam_auth(s3call);
140
141         case WINBINDD_PAM_AUTH_CRAP:
142                 return wbsrv_samba3_pam_auth_crap(s3call);
143
144         case WINBINDD_GETDCNAME:
145                 return wbsrv_samba3_getdcname(s3call);
146
147         case WINBINDD_GETUSERDOMGROUPS:
148                 return wbsrv_samba3_userdomgroups(s3call);
149
150         case WINBINDD_GETUSERSIDS:
151                 return wbsrv_samba3_usersids(s3call);
152
153         case WINBINDD_LIST_GROUPS:
154                 return wbsrv_samba3_list_groups(s3call);
155
156         case WINBINDD_LIST_TRUSTDOM:
157                 return wbsrv_samba3_list_trustdom(s3call);
158
159         case WINBINDD_LIST_USERS:
160                 return wbsrv_samba3_list_users(s3call);
161
162         case WINBINDD_GETPWNAM:
163                 return wbsrv_samba3_getpwnam(s3call);
164
165         case WINBINDD_GETPWUID:
166                 return wbsrv_samba3_getpwuid(s3call);
167
168         case WINBINDD_SETPWENT:
169                 return wbsrv_samba3_setpwent(s3call);
170
171         case WINBINDD_GETPWENT:
172                 return wbsrv_samba3_getpwent(s3call);
173
174         case WINBINDD_ENDPWENT:
175                 return wbsrv_samba3_endpwent(s3call);
176
177         case WINBINDD_GETGRNAM:
178                 return wbsrv_samba3_getgrnam(s3call);
179
180         case WINBINDD_GETGRGID:
181                 return wbsrv_samba3_getgrgid(s3call);
182
183         case WINBINDD_GETGROUPS:
184                 return wbsrv_samba3_getgroups(s3call);
185
186         case WINBINDD_SETGRENT:
187                 return wbsrv_samba3_setgrent(s3call);
188
189         case WINBINDD_GETGRENT:
190                 return wbsrv_samba3_getgrent(s3call);
191
192         case WINBINDD_ENDGRENT:
193                 return wbsrv_samba3_endgrent(s3call);
194
195         case WINBINDD_SID_TO_UID:
196         case WINBINDD_DUAL_SID2UID:
197                 return wbsrv_samba3_sid2uid(s3call);
198
199         case WINBINDD_SID_TO_GID:
200         case WINBINDD_DUAL_SID2GID:
201                 return wbsrv_samba3_sid2gid(s3call);
202
203         case WINBINDD_UID_TO_SID:
204         case WINBINDD_DUAL_UID2SID:
205                 return wbsrv_samba3_uid2sid(s3call);
206
207         case WINBINDD_GID_TO_SID:
208         case WINBINDD_DUAL_GID2SID:
209                 return wbsrv_samba3_gid2sid(s3call);
210
211         case WINBINDD_DOMAIN_INFO:
212                 return wbsrv_samba3_domain_info(s3call);
213
214         case WINBINDD_PAM_LOGOFF:
215                 return wbsrv_samba3_pam_logoff(s3call);
216
217         /* Unimplemented commands */
218         case WINBINDD_GETPWSID:
219         case WINBINDD_PAM_CHAUTHTOK:
220         case WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP:
221         case WINBINDD_LOOKUPRIDS:
222         case WINBINDD_LOOKUPSIDS:
223         case WINBINDD_SIDS_TO_XIDS:
224         case WINBINDD_ALLOCATE_UID:
225         case WINBINDD_ALLOCATE_GID:
226         case WINBINDD_SHOW_SEQUENCE:
227         case WINBINDD_WINS_BYIP:
228         case WINBINDD_WINS_BYNAME:
229         case WINBINDD_GETGRLST:
230         case WINBINDD_GETSIDALIASES:
231         case WINBINDD_DSGETDCNAME:
232         case WINBINDD_INIT_CONNECTION:
233         case WINBINDD_DUAL_SIDS2XIDS:
234         case WINBINDD_DUAL_USERINFO:
235         case WINBINDD_DUAL_GETSIDALIASES:
236         case WINBINDD_DUAL_NDRCMD:
237         case WINBINDD_CCACHE_NTLMAUTH:
238         case WINBINDD_NUM_CMDS:
239         case WINBINDD_CHANGE_MACHACC:
240         case WINBINDD_PING_DC:
241         case WINBINDD_DC_INFO:
242         case WINBINDD_CCACHE_SAVE:
243                 DEBUG(10, ("Unimplemented winbind samba3 request %d\n",
244                            s3call->request->cmd));
245                 break;
246         }
247
248         s3call->response->result = WINBINDD_ERROR;
249         return NT_STATUS_OK;
250 }
251
252 static NTSTATUS wbsrv_samba3_push_reply(struct wbsrv_samba3_call *call)
253 {
254         uint8_t *extra_data;
255         size_t extra_data_len = 0;
256
257         extra_data = (uint8_t *)call->response->extra_data.data;
258         if (extra_data != NULL) {
259                 extra_data_len = call->response->length -
260                         sizeof(*call->response);
261         }
262
263         call->out = data_blob_talloc(call, NULL, call->response->length);
264         NT_STATUS_HAVE_NO_MEMORY(call->out.data);
265
266         /* don't push real pointer values into sockets */
267         if (extra_data) {
268                 call->response->extra_data.data = (void *)0xFFFFFFFF;
269         }
270
271         memcpy(call->out.data, call->response, sizeof(*call->response));
272         /* set back the pointer */
273         call->response->extra_data.data = extra_data;
274
275         if (extra_data) {
276                 memcpy(call->out.data + sizeof(*call->response),
277                        extra_data,
278                        extra_data_len);
279         }
280
281         return NT_STATUS_OK;
282 }
283
284 static void wbsrv_samba3_send_reply_done(struct tevent_req *subreq);
285
286 /*
287  * queue a wbsrv_call reply on a wbsrv_connection
288  * NOTE: that this implies talloc_free(call),
289  *       use talloc_reference(call) if you need it after
290  *       calling wbsrv_queue_reply
291  */
292 NTSTATUS wbsrv_samba3_send_reply(struct wbsrv_samba3_call *call)
293 {
294         struct wbsrv_connection *wbsrv_conn = call->wbconn;
295         struct tevent_req *subreq;
296         NTSTATUS status;
297
298         status = wbsrv_samba3_push_reply(call);
299         NT_STATUS_NOT_OK_RETURN(status);
300
301         call->out_iov[0].iov_base = (char *) call->out.data;
302         call->out_iov[0].iov_len = call->out.length;
303
304         subreq = tstream_writev_queue_send(call,
305                                            wbsrv_conn->conn->event.ctx,
306                                            wbsrv_conn->tstream,
307                                            wbsrv_conn->send_queue,
308                                            call->out_iov, 1);
309         if (subreq == NULL) {
310                 wbsrv_terminate_connection(wbsrv_conn, "wbsrv_call_loop: "
311                                 "no memory for tstream_writev_queue_send");
312                 return NT_STATUS_NO_MEMORY;
313         }
314         tevent_req_set_callback(subreq, wbsrv_samba3_send_reply_done, call);
315
316         return status;
317 }
318
319 static void wbsrv_samba3_send_reply_done(struct tevent_req *subreq)
320 {
321         struct wbsrv_samba3_call *call = tevent_req_callback_data(subreq,
322                         struct wbsrv_samba3_call);
323         int sys_errno;
324         int rc;
325
326         rc = tstream_writev_queue_recv(subreq, &sys_errno);
327         TALLOC_FREE(subreq);
328         if (rc == -1) {
329                 const char *reason;
330
331                 reason = talloc_asprintf(call, "wbsrv_samba3_send_reply_done: "
332                                          "tstream_writev_queue_recv() - %d:%s",
333                                          sys_errno, strerror(sys_errno));
334                 if (reason == NULL) {
335                         reason = "wbsrv_samba3_send_reply_done: "
336                                  "tstream_writev_queue_recv() failed";
337                 }
338
339                 wbsrv_terminate_connection(call->wbconn, reason);
340                 return;
341         }
342
343         talloc_free(call);
344 }
345
346 NTSTATUS wbsrv_samba3_process(struct wbsrv_samba3_call *call)
347 {
348         NTSTATUS status;
349
350         status = wbsrv_samba3_pull_request(call);
351         
352         if (!NT_STATUS_IS_OK(status)) {
353                 return status;
354         }
355
356         status = wbsrv_samba3_handle_call(call);
357
358         if (!NT_STATUS_IS_OK(status)) {
359                 talloc_free(call);
360                 return status;
361         }
362
363         if (call->flags & WBSRV_CALL_FLAGS_REPLY_ASYNC) {
364                 return NT_STATUS_OK;
365         }
366
367         status = wbsrv_samba3_send_reply(call);
368         return status;
369 }
370