s3:winbindd: remove unused server implementation of wbint_Sid2Gid()
[Samba/gbeck.git] / source3 / smbd / ipc.c
blob91d5047c4c67a20dccc48a0f545cd71b50fb2c97
1 /*
2 Unix SMB/CIFS implementation.
3 Inter-process communication and named pipe handling
4 Copyright (C) Andrew Tridgell 1992-1998
6 SMB Version handling
7 Copyright (C) John H Terpstra 1995-1998
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 This file handles the named pipe and mailslot calls
24 in the SMBtrans protocol
27 #include "includes.h"
28 #include "smbd/smbd.h"
29 #include "smbd/globals.h"
30 #include "smbprofile.h"
31 #include "rpc_server/srv_pipe_hnd.h"
33 #define NERR_notsupported 50
35 static void api_no_reply(connection_struct *conn, struct smb_request *req);
37 /*******************************************************************
38 copies parameters and data, as needed, into the smb buffer
40 *both* the data and params sections should be aligned. this
41 is fudged in the rpc pipes by
42 at present, only the data section is. this may be a possible
43 cause of some of the ipc problems being experienced. lkcl26dec97
45 ******************************************************************/
47 static void copy_trans_params_and_data(char *outbuf, int align,
48 char *rparam, int param_offset, int param_len,
49 char *rdata, int data_offset, int data_len)
51 char *copy_into = smb_buf(outbuf);
53 if(param_len < 0)
54 param_len = 0;
56 if(data_len < 0)
57 data_len = 0;
59 DEBUG(5,("copy_trans_params_and_data: params[%d..%d] data[%d..%d] (align %d)\n",
60 param_offset, param_offset + param_len,
61 data_offset , data_offset + data_len,
62 align));
64 *copy_into = '\0';
66 copy_into += 1;
68 if (param_len)
69 memcpy(copy_into, &rparam[param_offset], param_len);
71 copy_into += param_len;
72 if (align) {
73 memset(copy_into, '\0', align);
76 copy_into += align;
78 if (data_len )
79 memcpy(copy_into, &rdata[data_offset], data_len);
82 NTSTATUS nt_status_np_pipe(NTSTATUS status)
84 if (NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_DISCONNECTED)) {
85 status = NT_STATUS_PIPE_DISCONNECTED;
86 } else if (NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_RESET)) {
87 status = NT_STATUS_PIPE_BROKEN;
90 return status;
93 /****************************************************************************
94 Send a trans reply.
95 ****************************************************************************/
97 void send_trans_reply(connection_struct *conn,
98 struct smb_request *req,
99 char *rparam, int rparam_len,
100 char *rdata, int rdata_len,
101 bool buffer_too_large)
103 int this_ldata,this_lparam;
104 int tot_data_sent = 0;
105 int tot_param_sent = 0;
106 int align;
108 int ldata = rdata ? rdata_len : 0;
109 int lparam = rparam ? rparam_len : 0;
110 struct smbd_server_connection *sconn = req->sconn;
111 int max_send = sconn->smb1.sessions.max_send;
113 if (buffer_too_large)
114 DEBUG(5,("send_trans_reply: buffer %d too large\n", ldata ));
116 this_lparam = MIN(lparam,max_send - 500); /* hack */
117 this_ldata = MIN(ldata,max_send - (500+this_lparam));
119 align = ((this_lparam)%4);
121 reply_outbuf(req, 10, 1+align+this_ldata+this_lparam);
124 * We might have SMBtranss in req which was transferred to the outbuf,
125 * fix that.
127 SCVAL(req->outbuf, smb_com, SMBtrans);
129 copy_trans_params_and_data((char *)req->outbuf, align,
130 rparam, tot_param_sent, this_lparam,
131 rdata, tot_data_sent, this_ldata);
133 SSVAL(req->outbuf,smb_vwv0,lparam);
134 SSVAL(req->outbuf,smb_vwv1,ldata);
135 SSVAL(req->outbuf,smb_vwv3,this_lparam);
136 SSVAL(req->outbuf,smb_vwv4,
137 smb_offset(smb_buf(req->outbuf)+1, req->outbuf));
138 SSVAL(req->outbuf,smb_vwv5,0);
139 SSVAL(req->outbuf,smb_vwv6,this_ldata);
140 SSVAL(req->outbuf,smb_vwv7,
141 smb_offset(smb_buf(req->outbuf)+1+this_lparam+align,
142 req->outbuf));
143 SSVAL(req->outbuf,smb_vwv8,0);
144 SSVAL(req->outbuf,smb_vwv9,0);
146 if (buffer_too_large) {
147 error_packet_set((char *)req->outbuf, ERRDOS, ERRmoredata,
148 STATUS_BUFFER_OVERFLOW, __LINE__, __FILE__);
151 show_msg((char *)req->outbuf);
152 if (!srv_send_smb(sconn, (char *)req->outbuf,
153 true, req->seqnum+1,
154 IS_CONN_ENCRYPTED(conn), &req->pcd)) {
155 exit_server_cleanly("send_trans_reply: srv_send_smb failed.");
158 TALLOC_FREE(req->outbuf);
160 tot_data_sent = this_ldata;
161 tot_param_sent = this_lparam;
163 while (tot_data_sent < ldata || tot_param_sent < lparam)
165 this_lparam = MIN(lparam-tot_param_sent,
166 max_send - 500); /* hack */
167 this_ldata = MIN(ldata -tot_data_sent,
168 max_send - (500+this_lparam));
170 if(this_lparam < 0)
171 this_lparam = 0;
173 if(this_ldata < 0)
174 this_ldata = 0;
176 align = (this_lparam%4);
178 reply_outbuf(req, 10, 1+align+this_ldata+this_lparam);
181 * We might have SMBtranss in req which was transferred to the
182 * outbuf, fix that.
184 SCVAL(req->outbuf, smb_com, SMBtrans);
186 copy_trans_params_and_data((char *)req->outbuf, align,
187 rparam, tot_param_sent, this_lparam,
188 rdata, tot_data_sent, this_ldata);
190 SSVAL(req->outbuf,smb_vwv0,lparam);
191 SSVAL(req->outbuf,smb_vwv1,ldata);
193 SSVAL(req->outbuf,smb_vwv3,this_lparam);
194 SSVAL(req->outbuf,smb_vwv4,
195 smb_offset(smb_buf(req->outbuf)+1,req->outbuf));
196 SSVAL(req->outbuf,smb_vwv5,tot_param_sent);
197 SSVAL(req->outbuf,smb_vwv6,this_ldata);
198 SSVAL(req->outbuf,smb_vwv7,
199 smb_offset(smb_buf(req->outbuf)+1+this_lparam+align,
200 req->outbuf));
201 SSVAL(req->outbuf,smb_vwv8,tot_data_sent);
202 SSVAL(req->outbuf,smb_vwv9,0);
204 if (buffer_too_large) {
205 error_packet_set((char *)req->outbuf,
206 ERRDOS, ERRmoredata,
207 STATUS_BUFFER_OVERFLOW,
208 __LINE__, __FILE__);
211 show_msg((char *)req->outbuf);
212 if (!srv_send_smb(sconn, (char *)req->outbuf,
213 true, req->seqnum+1,
214 IS_CONN_ENCRYPTED(conn), &req->pcd))
215 exit_server_cleanly("send_trans_reply: srv_send_smb "
216 "failed.");
218 tot_data_sent += this_ldata;
219 tot_param_sent += this_lparam;
220 TALLOC_FREE(req->outbuf);
224 /****************************************************************************
225 Start the first part of an RPC reply which began with an SMBtrans request.
226 ****************************************************************************/
228 struct dcerpc_cmd_state {
229 struct fake_file_handle *handle;
230 uint8_t *data;
231 size_t num_data;
232 size_t max_read;
235 static void api_dcerpc_cmd_write_done(struct tevent_req *subreq);
236 static void api_dcerpc_cmd_read_done(struct tevent_req *subreq);
238 static void api_dcerpc_cmd(connection_struct *conn, struct smb_request *req,
239 files_struct *fsp, uint8_t *data, size_t length,
240 size_t max_read)
242 struct tevent_req *subreq;
243 struct dcerpc_cmd_state *state;
244 bool busy;
246 if (!fsp_is_np(fsp)) {
247 api_no_reply(conn, req);
248 return;
252 * Trans requests are only allowed
253 * if no other Trans or Read is active
255 busy = np_read_in_progress(fsp->fake_file_handle);
256 if (busy) {
257 reply_nterror(req, NT_STATUS_PIPE_BUSY);
258 return;
261 state = talloc(req, struct dcerpc_cmd_state);
262 if (state == NULL) {
263 reply_nterror(req, NT_STATUS_NO_MEMORY);
264 return;
266 req->async_priv = state;
268 state->handle = fsp->fake_file_handle;
271 * This memdup severely sucks. But doing it properly essentially means
272 * to rewrite lanman.c, something which I don't really want to do now.
274 state->data = (uint8_t *)talloc_memdup(state, data, length);
275 if (state->data == NULL) {
276 reply_nterror(req, NT_STATUS_NO_MEMORY);
277 return;
279 state->num_data = length;
280 state->max_read = max_read;
282 subreq = np_write_send(state, req->sconn->ev_ctx, state->handle,
283 state->data, length);
284 if (subreq == NULL) {
285 TALLOC_FREE(state);
286 reply_nterror(req, NT_STATUS_NO_MEMORY);
287 return;
289 tevent_req_set_callback(subreq, api_dcerpc_cmd_write_done,
290 talloc_move(conn, &req));
293 static void api_dcerpc_cmd_write_done(struct tevent_req *subreq)
295 struct smb_request *req = tevent_req_callback_data(
296 subreq, struct smb_request);
297 struct dcerpc_cmd_state *state = talloc_get_type_abort(
298 req->async_priv, struct dcerpc_cmd_state);
299 NTSTATUS status;
300 ssize_t nwritten = -1;
302 status = np_write_recv(subreq, &nwritten);
303 TALLOC_FREE(subreq);
304 if (!NT_STATUS_IS_OK(status)) {
305 NTSTATUS old = status;
306 status = nt_status_np_pipe(old);
308 DEBUG(10, ("Could not write to pipe: %s%s%s\n",
309 nt_errstr(old),
310 NT_STATUS_EQUAL(old, status)?"":" => ",
311 NT_STATUS_EQUAL(old, status)?"":nt_errstr(status)));
312 reply_nterror(req, status);
313 goto send;
315 if (nwritten != state->num_data) {
316 status = NT_STATUS_PIPE_NOT_AVAILABLE;
317 DEBUG(10, ("Could not write to pipe: (%d/%d) => %s\n",
318 (int)state->num_data,
319 (int)nwritten, nt_errstr(status)));
320 reply_nterror(req, status);
321 goto send;
324 state->data = talloc_realloc(state, state->data, uint8_t,
325 state->max_read);
326 if (state->data == NULL) {
327 reply_nterror(req, NT_STATUS_NO_MEMORY);
328 goto send;
331 subreq = np_read_send(state, req->sconn->ev_ctx,
332 state->handle, state->data, state->max_read);
333 if (subreq == NULL) {
334 reply_nterror(req, NT_STATUS_NO_MEMORY);
335 goto send;
337 tevent_req_set_callback(subreq, api_dcerpc_cmd_read_done, req);
338 return;
340 send:
341 if (!srv_send_smb(
342 req->sconn, (char *)req->outbuf,
343 true, req->seqnum+1,
344 IS_CONN_ENCRYPTED(req->conn) || req->encrypted,
345 &req->pcd)) {
346 exit_server_cleanly("api_dcerpc_cmd_write_done: "
347 "srv_send_smb failed.");
349 TALLOC_FREE(req);
352 static void api_dcerpc_cmd_read_done(struct tevent_req *subreq)
354 struct smb_request *req = tevent_req_callback_data(
355 subreq, struct smb_request);
356 struct dcerpc_cmd_state *state = talloc_get_type_abort(
357 req->async_priv, struct dcerpc_cmd_state);
358 NTSTATUS status;
359 ssize_t nread;
360 bool is_data_outstanding;
362 status = np_read_recv(subreq, &nread, &is_data_outstanding);
363 TALLOC_FREE(subreq);
365 if (!NT_STATUS_IS_OK(status)) {
366 NTSTATUS old = status;
367 status = nt_status_np_pipe(old);
369 DEBUG(10, ("Could not read from to pipe: %s%s%s\n",
370 nt_errstr(old),
371 NT_STATUS_EQUAL(old, status)?"":" => ",
372 NT_STATUS_EQUAL(old, status)?"":nt_errstr(status)));
373 reply_nterror(req, status);
375 if (!srv_send_smb(req->sconn, (char *)req->outbuf,
376 true, req->seqnum+1,
377 IS_CONN_ENCRYPTED(req->conn)
378 ||req->encrypted, &req->pcd)) {
379 exit_server_cleanly("api_dcerpc_cmd_read_done: "
380 "srv_send_smb failed.");
382 TALLOC_FREE(req);
383 return;
386 send_trans_reply(req->conn, req, NULL, 0, (char *)state->data, nread,
387 is_data_outstanding);
388 TALLOC_FREE(req);
391 /****************************************************************************
392 WaitNamedPipeHandleState
393 ****************************************************************************/
395 static void api_WNPHS(connection_struct *conn, struct smb_request *req,
396 struct files_struct *fsp, char *param, int param_len)
398 if (!param || param_len < 2) {
399 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
400 return;
403 DEBUG(4,("WaitNamedPipeHandleState priority %x\n",
404 (int)SVAL(param,0)));
406 send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
410 /****************************************************************************
411 SetNamedPipeHandleState
412 ****************************************************************************/
414 static void api_SNPHS(connection_struct *conn, struct smb_request *req,
415 struct files_struct *fsp, char *param, int param_len)
417 if (!param || param_len < 2) {
418 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
419 return;
422 DEBUG(4,("SetNamedPipeHandleState to code %x\n", (int)SVAL(param,0)));
424 send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
428 /****************************************************************************
429 When no reply is generated, indicate unsupported.
430 ****************************************************************************/
432 static void api_no_reply(connection_struct *conn, struct smb_request *req)
434 char rparam[4];
436 /* unsupported */
437 SSVAL(rparam,0,NERR_notsupported);
438 SSVAL(rparam,2,0); /* converter word */
440 DEBUG(3,("Unsupported API fd command\n"));
442 /* now send the reply */
443 send_trans_reply(conn, req, rparam, 4, NULL, 0, False);
445 return;
448 /****************************************************************************
449 Handle remote api calls delivered to a named pipe already opened.
450 ****************************************************************************/
452 static void api_fd_reply(connection_struct *conn, uint64_t vuid,
453 struct smb_request *req,
454 uint16 *setup, uint8_t *data, char *params,
455 int suwcnt, int tdscnt, int tpscnt,
456 int mdrcnt, int mprcnt)
458 struct files_struct *fsp;
459 int pnum;
460 int subcommand;
462 DEBUG(5,("api_fd_reply\n"));
464 /* First find out the name of this file. */
465 if (suwcnt != 2) {
466 DEBUG(0,("Unexpected named pipe transaction.\n"));
467 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
468 return;
471 /* Get the file handle and hence the file name. */
473 * NB. The setup array has already been transformed
474 * via SVAL and so is in host byte order.
476 pnum = ((int)setup[1]) & 0xFFFF;
477 subcommand = ((int)setup[0]) & 0xFFFF;
479 fsp = file_fsp(req, pnum);
481 if (!fsp_is_np(fsp)) {
482 if (subcommand == TRANSACT_WAITNAMEDPIPEHANDLESTATE) {
483 /* Win9x does this call with a unicode pipe name, not a pnum. */
484 /* Just return success for now... */
485 DEBUG(3,("Got TRANSACT_WAITNAMEDPIPEHANDLESTATE on text pipe name\n"));
486 send_trans_reply(conn, req, NULL, 0, NULL, 0, False);
487 return;
490 DEBUG(1,("api_fd_reply: INVALID PIPE HANDLE: %x\n", pnum));
491 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
492 return;
495 if (vuid != fsp->vuid) {
496 DEBUG(1, ("Got pipe request (pnum %x) using invalid VUID %llu, "
497 "expected %llu\n", pnum, (unsigned long long)vuid,
498 (unsigned long long)fsp->vuid));
499 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
500 return;
503 DEBUG(3,("Got API command 0x%x on pipe \"%s\" (pnum %x)\n",
504 subcommand, fsp_str_dbg(fsp), pnum));
506 DEBUG(10, ("api_fd_reply: p:%p max_trans_reply: %d\n", fsp, mdrcnt));
508 switch (subcommand) {
509 case TRANSACT_DCERPCCMD: {
510 /* dce/rpc command */
511 api_dcerpc_cmd(conn, req, fsp, (uint8_t *)data, tdscnt,
512 mdrcnt);
513 break;
515 case TRANSACT_WAITNAMEDPIPEHANDLESTATE:
516 /* Wait Named Pipe Handle state */
517 api_WNPHS(conn, req, fsp, params, tpscnt);
518 break;
519 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
520 /* Set Named Pipe Handle state */
521 api_SNPHS(conn, req, fsp, params, tpscnt);
522 break;
523 default:
524 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
525 return;
529 /****************************************************************************
530 Handle named pipe commands.
531 ****************************************************************************/
533 static void named_pipe(connection_struct *conn, uint64_t vuid,
534 struct smb_request *req,
535 const char *name, uint16 *setup,
536 char *data, char *params,
537 int suwcnt, int tdscnt,int tpscnt,
538 int msrcnt, int mdrcnt, int mprcnt)
540 DEBUG(3,("named pipe command on <%s> name\n", name));
542 if (strequal(name,"LANMAN")) {
543 api_reply(conn, vuid, req,
544 data, params,
545 tdscnt, tpscnt,
546 mdrcnt, mprcnt);
547 return;
550 if (strequal(name,"WKSSVC") ||
551 strequal(name,"SRVSVC") ||
552 strequal(name,"WINREG") ||
553 strequal(name,"SAMR") ||
554 strequal(name,"LSARPC")) {
556 DEBUG(4,("named pipe command from Win95 (wow!)\n"));
558 api_fd_reply(conn, vuid, req,
559 setup, (uint8_t *)data, params,
560 suwcnt, tdscnt, tpscnt,
561 mdrcnt, mprcnt);
562 return;
565 if (strlen(name) < 1) {
566 api_fd_reply(conn, vuid, req,
567 setup, (uint8_t *)data,
568 params, suwcnt, tdscnt,
569 tpscnt, mdrcnt, mprcnt);
570 return;
573 if (setup)
574 DEBUG(3,("unknown named pipe: setup 0x%X setup1=%d\n",
575 (int)setup[0],(int)setup[1]));
577 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
578 return;
581 static void handle_trans(connection_struct *conn, struct smb_request *req,
582 struct trans_state *state)
584 char *local_machine_name;
585 int name_offset = 0;
587 DEBUG(3,("trans <%s> data=%u params=%u setup=%u\n",
588 state->name,(unsigned int)state->total_data,(unsigned int)state->total_param,
589 (unsigned int)state->setup_count));
592 * WinCE wierdness....
595 local_machine_name = talloc_asprintf(state, "\\%s\\",
596 get_local_machine_name());
598 if (local_machine_name == NULL) {
599 reply_nterror(req, NT_STATUS_NO_MEMORY);
600 return;
603 if (strnequal(state->name, local_machine_name,
604 strlen(local_machine_name))) {
605 name_offset = strlen(local_machine_name)-1;
608 if (!strnequal(&state->name[name_offset], "\\PIPE",
609 strlen("\\PIPE"))) {
610 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
611 return;
614 name_offset += strlen("\\PIPE");
616 /* Win9x weirdness. When talking to a unicode server Win9x
617 only sends \PIPE instead of \PIPE\ */
619 if (state->name[name_offset] == '\\')
620 name_offset++;
622 DEBUG(5,("calling named_pipe\n"));
623 named_pipe(conn, state->vuid, req,
624 state->name+name_offset,
625 state->setup,state->data,
626 state->param,
627 state->setup_count,state->total_data,
628 state->total_param,
629 state->max_setup_return,
630 state->max_data_return,
631 state->max_param_return);
633 if (state->close_on_completion) {
634 struct smbXsrv_tcon *tcon;
635 NTSTATUS status;
637 tcon = conn->tcon;
638 req->conn = NULL;
639 conn = NULL;
642 * TODO: cancel all outstanding requests on the tcon
644 status = smbXsrv_tcon_disconnect(tcon, state->vuid);
645 if (!NT_STATUS_IS_OK(status)) {
646 DEBUG(0, ("handle_trans: "
647 "smbXsrv_tcon_disconnect() failed: %s\n",
648 nt_errstr(status)));
650 * If we hit this case, there is something completely
651 * wrong, so we better disconnect the transport connection.
653 exit_server(__location__ ": smbXsrv_tcon_disconnect failed");
654 return;
657 TALLOC_FREE(tcon);
660 return;
663 /****************************************************************************
664 Reply to a SMBtrans.
665 ****************************************************************************/
667 void reply_trans(struct smb_request *req)
669 connection_struct *conn = req->conn;
670 unsigned int dsoff;
671 unsigned int dscnt;
672 unsigned int psoff;
673 unsigned int pscnt;
674 struct trans_state *state;
675 NTSTATUS result;
677 START_PROFILE(SMBtrans);
679 if (req->wct < 14) {
680 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
681 END_PROFILE(SMBtrans);
682 return;
685 dsoff = SVAL(req->vwv+12, 0);
686 dscnt = SVAL(req->vwv+11, 0);
687 psoff = SVAL(req->vwv+10, 0);
688 pscnt = SVAL(req->vwv+9, 0);
690 result = allow_new_trans(conn->pending_trans, req->mid);
691 if (!NT_STATUS_IS_OK(result)) {
692 DEBUG(2, ("Got invalid trans request: %s\n",
693 nt_errstr(result)));
694 reply_nterror(req, result);
695 END_PROFILE(SMBtrans);
696 return;
699 if ((state = talloc(conn, struct trans_state)) == NULL) {
700 DEBUG(0, ("talloc failed\n"));
701 reply_nterror(req, NT_STATUS_NO_MEMORY);
702 END_PROFILE(SMBtrans);
703 return;
706 state->cmd = SMBtrans;
708 state->mid = req->mid;
709 state->vuid = req->vuid;
710 state->setup_count = CVAL(req->vwv+13, 0);
711 state->setup = NULL;
712 state->total_param = SVAL(req->vwv+0, 0);
713 state->param = NULL;
714 state->total_data = SVAL(req->vwv+1, 0);
715 state->data = NULL;
716 state->max_param_return = SVAL(req->vwv+2, 0);
717 state->max_data_return = SVAL(req->vwv+3, 0);
718 state->max_setup_return = CVAL(req->vwv+4, 0);
719 state->close_on_completion = BITSETW(req->vwv+5, 0);
720 state->one_way = BITSETW(req->vwv+5, 1);
722 srvstr_pull_req_talloc(state, req, &state->name, req->buf,
723 STR_TERMINATE);
725 if ((dscnt > state->total_data) || (pscnt > state->total_param) ||
726 !state->name)
727 goto bad_param;
729 if (state->total_data) {
731 if (trans_oob(state->total_data, 0, dscnt)
732 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
733 goto bad_param;
736 /* Can't use talloc here, the core routines do realloc on the
737 * params and data. Out of paranoia, 100 bytes too many. */
738 state->data = (char *)SMB_MALLOC(state->total_data+100);
739 if (state->data == NULL) {
740 DEBUG(0,("reply_trans: data malloc fail for %u "
741 "bytes !\n", (unsigned int)state->total_data));
742 TALLOC_FREE(state);
743 reply_nterror(req, NT_STATUS_NO_MEMORY);
744 END_PROFILE(SMBtrans);
745 return;
747 /* null-terminate the slack space */
748 memset(&state->data[state->total_data], 0, 100);
750 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
753 if (state->total_param) {
755 if (trans_oob(state->total_param, 0, pscnt)
756 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
757 goto bad_param;
760 /* Can't use talloc here, the core routines do realloc on the
761 * params and data. Out of paranoia, 100 bytes too many */
762 state->param = (char *)SMB_MALLOC(state->total_param+100);
763 if (state->param == NULL) {
764 DEBUG(0,("reply_trans: param malloc fail for %u "
765 "bytes !\n", (unsigned int)state->total_param));
766 SAFE_FREE(state->data);
767 TALLOC_FREE(state);
768 reply_nterror(req, NT_STATUS_NO_MEMORY);
769 END_PROFILE(SMBtrans);
770 return;
772 /* null-terminate the slack space */
773 memset(&state->param[state->total_param], 0, 100);
775 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
778 state->received_data = dscnt;
779 state->received_param = pscnt;
781 if (state->setup_count) {
782 unsigned int i;
785 * No overflow possible here, state->setup_count is an
786 * unsigned int, being filled by a single byte from
787 * CVAL(req->vwv+13, 0) above. The cast in the comparison
788 * below is not necessary, it's here to clarify things. The
789 * validity of req->vwv and req->wct has been checked in
790 * init_smb_request already.
792 if (state->setup_count + 14 > (unsigned int)req->wct) {
793 goto bad_param;
796 if((state->setup = talloc_array(
797 state, uint16, state->setup_count)) == NULL) {
798 DEBUG(0,("reply_trans: setup malloc fail for %u "
799 "bytes !\n", (unsigned int)
800 (state->setup_count * sizeof(uint16))));
801 SAFE_FREE(state->data);
802 SAFE_FREE(state->param);
803 TALLOC_FREE(state);
804 reply_nterror(req, NT_STATUS_NO_MEMORY);
805 END_PROFILE(SMBtrans);
806 return;
809 for (i=0;i<state->setup_count;i++) {
810 state->setup[i] = SVAL(req->vwv + 14 + i, 0);
814 state->received_param = pscnt;
816 if ((state->received_param != state->total_param) ||
817 (state->received_data != state->total_data)) {
818 DLIST_ADD(conn->pending_trans, state);
820 /* We need to send an interim response then receive the rest
821 of the parameter/data bytes */
822 reply_outbuf(req, 0, 0);
823 show_msg((char *)req->outbuf);
824 END_PROFILE(SMBtrans);
825 return;
828 talloc_steal(talloc_tos(), state);
830 handle_trans(conn, req, state);
832 SAFE_FREE(state->data);
833 SAFE_FREE(state->param);
834 TALLOC_FREE(state);
836 END_PROFILE(SMBtrans);
837 return;
839 bad_param:
841 DEBUG(0,("reply_trans: invalid trans parameters\n"));
842 SAFE_FREE(state->data);
843 SAFE_FREE(state->param);
844 TALLOC_FREE(state);
845 END_PROFILE(SMBtrans);
846 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
847 return;
850 /****************************************************************************
851 Reply to a secondary SMBtrans.
852 ****************************************************************************/
854 void reply_transs(struct smb_request *req)
856 connection_struct *conn = req->conn;
857 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
858 struct trans_state *state;
860 START_PROFILE(SMBtranss);
862 show_msg((const char *)req->inbuf);
864 if (req->wct < 8) {
865 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
866 END_PROFILE(SMBtranss);
867 return;
870 for (state = conn->pending_trans; state != NULL;
871 state = state->next) {
872 if (state->mid == req->mid) {
873 break;
877 if ((state == NULL) || (state->cmd != SMBtrans)) {
878 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
879 END_PROFILE(SMBtranss);
880 return;
883 /* Revise total_params and total_data in case they have changed
884 * downwards */
886 if (SVAL(req->vwv+0, 0) < state->total_param)
887 state->total_param = SVAL(req->vwv+0, 0);
888 if (SVAL(req->vwv+1, 0) < state->total_data)
889 state->total_data = SVAL(req->vwv+1, 0);
891 pcnt = SVAL(req->vwv+2, 0);
892 poff = SVAL(req->vwv+3, 0);
893 pdisp = SVAL(req->vwv+4, 0);
895 dcnt = SVAL(req->vwv+5, 0);
896 doff = SVAL(req->vwv+6, 0);
897 ddisp = SVAL(req->vwv+7, 0);
899 state->received_param += pcnt;
900 state->received_data += dcnt;
902 if ((state->received_data > state->total_data) ||
903 (state->received_param > state->total_param))
904 goto bad_param;
906 if (pcnt) {
907 if (trans_oob(state->total_param, pdisp, pcnt)
908 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
909 goto bad_param;
911 memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,pcnt);
914 if (dcnt) {
915 if (trans_oob(state->total_data, ddisp, dcnt)
916 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
917 goto bad_param;
919 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
922 if ((state->received_param < state->total_param) ||
923 (state->received_data < state->total_data)) {
924 END_PROFILE(SMBtranss);
925 return;
928 talloc_steal(talloc_tos(), state);
930 handle_trans(conn, req, state);
932 DLIST_REMOVE(conn->pending_trans, state);
933 SAFE_FREE(state->data);
934 SAFE_FREE(state->param);
935 TALLOC_FREE(state);
937 END_PROFILE(SMBtranss);
938 return;
940 bad_param:
942 DEBUG(0,("reply_transs: invalid trans parameters\n"));
943 DLIST_REMOVE(conn->pending_trans, state);
944 SAFE_FREE(state->data);
945 SAFE_FREE(state->param);
946 TALLOC_FREE(state);
947 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
948 END_PROFILE(SMBtranss);
949 return;