s4-winbindd: fixed handling of extra_data in s3 requests
[Samba/bjacke.git] / source4 / winbind / wb_samba3_protocol.c
blob6c89f340631cbe8427f7a4368a5742acf03b8fa1
1 /*
2 Unix SMB/CIFS implementation.
3 Main winbindd samba3 server routines
5 Copyright (C) Stefan Metzmacher 2005
6 Copyright (C) Volker Lendecke 2005
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.
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.
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/>.
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"
29 work out if a packet is complete for protocols that use a 32 bit host byte
30 order length
32 NTSTATUS wbsrv_samba3_packet_full_request(void *private_data, DATA_BLOB blob, size_t *size)
34 uint32_t *len;
35 struct winbindd_request *req;
37 if (blob.length < 4) {
38 return STATUS_MORE_ENTRIES;
40 len = (uint32_t *)blob.data;
41 *size = (*len);
42 if (*size > blob.length) {
43 return STATUS_MORE_ENTRIES;
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;
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;
60 return NT_STATUS_OK;
64 NTSTATUS wbsrv_samba3_pull_request(struct wbsrv_samba3_call *call)
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;
73 call->request = talloc_zero(call, struct winbindd_request);
74 NT_STATUS_HAVE_NO_MEMORY(call->request);
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));
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;
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;
98 return NT_STATUS_OK;
101 NTSTATUS wbsrv_samba3_handle_call(struct wbsrv_samba3_call *s3call)
103 DEBUG(10, ("Got winbind samba3 request %d\n", s3call->request->cmd));
105 s3call->response = talloc_zero(s3call, struct winbindd_response);
106 NT_STATUS_HAVE_NO_MEMORY(s3call->request);
108 s3call->response->length = sizeof(*s3call->response);
110 switch(s3call->request->cmd) {
111 case WINBINDD_INTERFACE_VERSION:
112 return wbsrv_samba3_interface_version(s3call);
114 case WINBINDD_CHECK_MACHACC:
115 return wbsrv_samba3_check_machacc(s3call);
117 case WINBINDD_PING:
118 return wbsrv_samba3_ping(s3call);
120 case WINBINDD_INFO:
121 return wbsrv_samba3_info(s3call);
123 case WINBINDD_DOMAIN_NAME:
124 return wbsrv_samba3_domain_name(s3call);
126 case WINBINDD_NETBIOS_NAME:
127 return wbsrv_samba3_netbios_name(s3call);
129 case WINBINDD_PRIV_PIPE_DIR:
130 return wbsrv_samba3_priv_pipe_dir(s3call);
132 case WINBINDD_LOOKUPNAME:
133 return wbsrv_samba3_lookupname(s3call);
135 case WINBINDD_LOOKUPSID:
136 return wbsrv_samba3_lookupsid(s3call);
138 case WINBINDD_PAM_AUTH:
139 return wbsrv_samba3_pam_auth(s3call);
141 case WINBINDD_PAM_AUTH_CRAP:
142 return wbsrv_samba3_pam_auth_crap(s3call);
144 case WINBINDD_GETDCNAME:
145 return wbsrv_samba3_getdcname(s3call);
147 case WINBINDD_GETUSERDOMGROUPS:
148 return wbsrv_samba3_userdomgroups(s3call);
150 case WINBINDD_GETUSERSIDS:
151 return wbsrv_samba3_usersids(s3call);
153 case WINBINDD_LIST_GROUPS:
154 return wbsrv_samba3_list_groups(s3call);
156 case WINBINDD_LIST_TRUSTDOM:
157 return wbsrv_samba3_list_trustdom(s3call);
159 case WINBINDD_LIST_USERS:
160 return wbsrv_samba3_list_users(s3call);
162 case WINBINDD_GETPWNAM:
163 return wbsrv_samba3_getpwnam(s3call);
165 case WINBINDD_GETPWUID:
166 return wbsrv_samba3_getpwuid(s3call);
168 case WINBINDD_SETPWENT:
169 return wbsrv_samba3_setpwent(s3call);
171 case WINBINDD_GETPWENT:
172 return wbsrv_samba3_getpwent(s3call);
174 case WINBINDD_ENDPWENT:
175 return wbsrv_samba3_endpwent(s3call);
177 case WINBINDD_GETGRNAM:
178 return wbsrv_samba3_getgrnam(s3call);
180 case WINBINDD_GETGRGID:
181 return wbsrv_samba3_getgrgid(s3call);
183 case WINBINDD_GETGROUPS:
184 return wbsrv_samba3_getgroups(s3call);
186 case WINBINDD_SETGRENT:
187 return wbsrv_samba3_setgrent(s3call);
189 case WINBINDD_GETGRENT:
190 return wbsrv_samba3_getgrent(s3call);
192 case WINBINDD_ENDGRENT:
193 return wbsrv_samba3_endgrent(s3call);
195 case WINBINDD_SID_TO_UID:
196 case WINBINDD_DUAL_SID2UID:
197 return wbsrv_samba3_sid2uid(s3call);
199 case WINBINDD_SID_TO_GID:
200 case WINBINDD_DUAL_SID2GID:
201 return wbsrv_samba3_sid2gid(s3call);
203 case WINBINDD_UID_TO_SID:
204 case WINBINDD_DUAL_UID2SID:
205 return wbsrv_samba3_uid2sid(s3call);
207 case WINBINDD_GID_TO_SID:
208 case WINBINDD_DUAL_GID2SID:
209 return wbsrv_samba3_gid2sid(s3call);
211 case WINBINDD_DOMAIN_INFO:
212 return wbsrv_samba3_domain_info(s3call);
214 case WINBINDD_PAM_LOGOFF:
215 return wbsrv_samba3_pam_logoff(s3call);
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;
248 s3call->response->result = WINBINDD_ERROR;
249 return NT_STATUS_OK;
252 static NTSTATUS wbsrv_samba3_push_reply(struct wbsrv_samba3_call *call)
254 uint8_t *extra_data;
255 size_t extra_data_len = 0;
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);
263 call->out = data_blob_talloc(call, NULL, call->response->length);
264 NT_STATUS_HAVE_NO_MEMORY(call->out.data);
266 /* don't push real pointer values into sockets */
267 if (extra_data) {
268 call->response->extra_data.data = (void *)0xFFFFFFFF;
271 memcpy(call->out.data, call->response, sizeof(*call->response));
272 /* set back the pointer */
273 call->response->extra_data.data = extra_data;
275 if (extra_data) {
276 memcpy(call->out.data + sizeof(*call->response),
277 extra_data,
278 extra_data_len);
281 return NT_STATUS_OK;
284 static void wbsrv_samba3_send_reply_done(struct tevent_req *subreq);
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
292 NTSTATUS wbsrv_samba3_send_reply(struct wbsrv_samba3_call *call)
294 struct wbsrv_connection *wbsrv_conn = call->wbconn;
295 struct tevent_req *subreq;
296 NTSTATUS status;
298 status = wbsrv_samba3_push_reply(call);
299 NT_STATUS_NOT_OK_RETURN(status);
301 call->out_iov[0].iov_base = (char *) call->out.data;
302 call->out_iov[0].iov_len = call->out.length;
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;
314 tevent_req_set_callback(subreq, wbsrv_samba3_send_reply_done, call);
316 return status;
319 static void wbsrv_samba3_send_reply_done(struct tevent_req *subreq)
321 struct wbsrv_samba3_call *call = tevent_req_callback_data(subreq,
322 struct wbsrv_samba3_call);
323 int sys_errno;
324 int rc;
326 rc = tstream_writev_queue_recv(subreq, &sys_errno);
327 TALLOC_FREE(subreq);
328 if (rc == -1) {
329 const char *reason;
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";
339 wbsrv_terminate_connection(call->wbconn, reason);
340 return;
343 talloc_free(call);
346 NTSTATUS wbsrv_samba3_process(struct wbsrv_samba3_call *call)
348 NTSTATUS status;
350 status = wbsrv_samba3_pull_request(call);
352 if (!NT_STATUS_IS_OK(status)) {
353 return status;
356 status = wbsrv_samba3_handle_call(call);
358 if (!NT_STATUS_IS_OK(status)) {
359 talloc_free(call);
360 return status;
363 if (call->flags & WBSRV_CALL_FLAGS_REPLY_ASYNC) {
364 return NT_STATUS_OK;
367 status = wbsrv_samba3_send_reply(call);
368 return status;