2 Unix SMB/CIFS implementation.
3 Inter-process communication and named pipe handling
4 Copyright (C) Andrew Tridgell 1992-1998
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
31 #define NERR_notsupported 50
33 static void api_no_reply(connection_struct
*conn
, struct smb_request
*req
);
35 /*******************************************************************
36 copies parameters and data, as needed, into the smb buffer
38 *both* the data and params sections should be aligned. this
39 is fudged in the rpc pipes by
40 at present, only the data section is. this may be a possible
41 cause of some of the ipc problems being experienced. lkcl26dec97
43 ******************************************************************/
45 static void copy_trans_params_and_data(char *outbuf
, int align
,
46 char *rparam
, int param_offset
, int param_len
,
47 char *rdata
, int data_offset
, int data_len
)
49 char *copy_into
= smb_buf(outbuf
);
57 DEBUG(5,("copy_trans_params_and_data: params[%d..%d] data[%d..%d] (align %d)\n",
58 param_offset
, param_offset
+ param_len
,
59 data_offset
, data_offset
+ data_len
,
67 memcpy(copy_into
, &rparam
[param_offset
], param_len
);
69 copy_into
+= param_len
;
71 memset(copy_into
, '\0', align
);
77 memcpy(copy_into
, &rdata
[data_offset
], data_len
);
80 /****************************************************************************
82 ****************************************************************************/
84 void send_trans_reply(connection_struct
*conn
,
85 struct smb_request
*req
,
86 char *rparam
, int rparam_len
,
87 char *rdata
, int rdata_len
,
88 bool buffer_too_large
)
90 int this_ldata
,this_lparam
;
91 int tot_data_sent
= 0;
92 int tot_param_sent
= 0;
95 int ldata
= rdata
? rdata_len
: 0;
96 int lparam
= rparam
? rparam_len
: 0;
99 DEBUG(5,("send_trans_reply: buffer %d too large\n", ldata
));
101 this_lparam
= MIN(lparam
,max_send
- 500); /* hack */
102 this_ldata
= MIN(ldata
,max_send
- (500+this_lparam
));
104 align
= ((this_lparam
)%4);
106 reply_outbuf(req
, 10, 1+align
+this_ldata
+this_lparam
);
109 * We might have SMBtranss in req which was transferred to the outbuf,
112 SCVAL(req
->outbuf
, smb_com
, SMBtrans
);
114 copy_trans_params_and_data((char *)req
->outbuf
, align
,
115 rparam
, tot_param_sent
, this_lparam
,
116 rdata
, tot_data_sent
, this_ldata
);
118 SSVAL(req
->outbuf
,smb_vwv0
,lparam
);
119 SSVAL(req
->outbuf
,smb_vwv1
,ldata
);
120 SSVAL(req
->outbuf
,smb_vwv3
,this_lparam
);
121 SSVAL(req
->outbuf
,smb_vwv4
,
122 smb_offset(smb_buf(req
->outbuf
)+1, req
->outbuf
));
123 SSVAL(req
->outbuf
,smb_vwv5
,0);
124 SSVAL(req
->outbuf
,smb_vwv6
,this_ldata
);
125 SSVAL(req
->outbuf
,smb_vwv7
,
126 smb_offset(smb_buf(req
->outbuf
)+1+this_lparam
+align
,
128 SSVAL(req
->outbuf
,smb_vwv8
,0);
129 SSVAL(req
->outbuf
,smb_vwv9
,0);
131 if (buffer_too_large
) {
132 error_packet_set((char *)req
->outbuf
, ERRDOS
, ERRmoredata
,
133 STATUS_BUFFER_OVERFLOW
, __LINE__
, __FILE__
);
136 show_msg((char *)req
->outbuf
);
137 if (!srv_send_smb(smbd_server_fd(), (char *)req
->outbuf
,
138 IS_CONN_ENCRYPTED(conn
))) {
139 exit_server_cleanly("send_trans_reply: srv_send_smb failed.");
142 TALLOC_FREE(req
->outbuf
);
144 tot_data_sent
= this_ldata
;
145 tot_param_sent
= this_lparam
;
147 while (tot_data_sent
< ldata
|| tot_param_sent
< lparam
)
149 this_lparam
= MIN(lparam
-tot_param_sent
,
150 max_send
- 500); /* hack */
151 this_ldata
= MIN(ldata
-tot_data_sent
,
152 max_send
- (500+this_lparam
));
160 align
= (this_lparam
%4);
162 reply_outbuf(req
, 10, 1+align
+this_ldata
+this_lparam
);
165 * We might have SMBtranss in req which was transferred to the
168 SCVAL(req
->outbuf
, smb_com
, SMBtrans
);
170 copy_trans_params_and_data((char *)req
->outbuf
, align
,
171 rparam
, tot_param_sent
, this_lparam
,
172 rdata
, tot_data_sent
, this_ldata
);
174 SSVAL(req
->outbuf
,smb_vwv3
,this_lparam
);
175 SSVAL(req
->outbuf
,smb_vwv4
,
176 smb_offset(smb_buf(req
->outbuf
)+1,req
->outbuf
));
177 SSVAL(req
->outbuf
,smb_vwv5
,tot_param_sent
);
178 SSVAL(req
->outbuf
,smb_vwv6
,this_ldata
);
179 SSVAL(req
->outbuf
,smb_vwv7
,
180 smb_offset(smb_buf(req
->outbuf
)+1+this_lparam
+align
,
182 SSVAL(req
->outbuf
,smb_vwv8
,tot_data_sent
);
183 SSVAL(req
->outbuf
,smb_vwv9
,0);
185 if (buffer_too_large
) {
186 error_packet_set((char *)req
->outbuf
,
188 STATUS_BUFFER_OVERFLOW
,
192 show_msg((char *)req
->outbuf
);
193 if (!srv_send_smb(smbd_server_fd(), (char *)req
->outbuf
,
194 IS_CONN_ENCRYPTED(conn
)))
195 exit_server_cleanly("send_trans_reply: srv_send_smb "
198 tot_data_sent
+= this_ldata
;
199 tot_param_sent
+= this_lparam
;
200 TALLOC_FREE(req
->outbuf
);
204 /****************************************************************************
205 Start the first part of an RPC reply which began with an SMBtrans request.
206 ****************************************************************************/
208 static void api_rpc_trans_reply(connection_struct
*conn
,
209 struct smb_request
*req
,
213 bool is_data_outstanding
;
214 uint8_t *rdata
= SMB_MALLOC_ARRAY(uint8_t, max_trans_reply
);
219 DEBUG(0,("api_rpc_trans_reply: malloc fail.\n"));
220 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
224 status
= np_read(fsp
, rdata
, max_trans_reply
, &data_len
,
225 &is_data_outstanding
);
226 if (!NT_STATUS_IS_OK(status
)) {
228 api_no_reply(conn
,req
);
232 send_trans_reply(conn
, req
, NULL
, 0, (char *)rdata
, data_len
,
233 is_data_outstanding
);
238 /****************************************************************************
239 WaitNamedPipeHandleState
240 ****************************************************************************/
242 static void api_WNPHS(connection_struct
*conn
, struct smb_request
*req
,
243 struct files_struct
*fsp
, char *param
, int param_len
)
245 if (!param
|| param_len
< 2) {
246 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
250 DEBUG(4,("WaitNamedPipeHandleState priority %x\n",
251 (int)SVAL(param
,0)));
253 send_trans_reply(conn
, req
, NULL
, 0, NULL
, 0, False
);
257 /****************************************************************************
258 SetNamedPipeHandleState
259 ****************************************************************************/
261 static void api_SNPHS(connection_struct
*conn
, struct smb_request
*req
,
262 struct files_struct
*fsp
, char *param
, int param_len
)
264 if (!param
|| param_len
< 2) {
265 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
269 DEBUG(4,("SetNamedPipeHandleState to code %x\n", (int)SVAL(param
,0)));
271 send_trans_reply(conn
, req
, NULL
, 0, NULL
, 0, False
);
275 /****************************************************************************
276 When no reply is generated, indicate unsupported.
277 ****************************************************************************/
279 static void api_no_reply(connection_struct
*conn
, struct smb_request
*req
)
284 SSVAL(rparam
,0,NERR_notsupported
);
285 SSVAL(rparam
,2,0); /* converter word */
287 DEBUG(3,("Unsupported API fd command\n"));
289 /* now send the reply */
290 send_trans_reply(conn
, req
, rparam
, 4, NULL
, 0, False
);
295 /****************************************************************************
296 Handle remote api calls delivered to a named pipe already opened.
297 ****************************************************************************/
299 static void api_fd_reply(connection_struct
*conn
, uint16 vuid
,
300 struct smb_request
*req
,
301 uint16
*setup
, uint8_t *data
, char *params
,
302 int suwcnt
, int tdscnt
, int tpscnt
,
303 int mdrcnt
, int mprcnt
)
305 struct files_struct
*fsp
;
310 DEBUG(5,("api_fd_reply\n"));
312 /* First find out the name of this file. */
314 DEBUG(0,("Unexpected named pipe transaction.\n"));
315 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
319 /* Get the file handle and hence the file name. */
321 * NB. The setup array has already been transformed
322 * via SVAL and so is in host byte order.
324 pnum
= ((int)setup
[1]) & 0xFFFF;
325 subcommand
= ((int)setup
[0]) & 0xFFFF;
327 fsp
= file_fsp(req
, pnum
);
329 if (!fsp_is_np(fsp
)) {
330 if (subcommand
== TRANSACT_WAITNAMEDPIPEHANDLESTATE
) {
331 /* Win9x does this call with a unicode pipe name, not a pnum. */
332 /* Just return success for now... */
333 DEBUG(3,("Got TRANSACT_WAITNAMEDPIPEHANDLESTATE on text pipe name\n"));
334 send_trans_reply(conn
, req
, NULL
, 0, NULL
, 0, False
);
338 DEBUG(1,("api_fd_reply: INVALID PIPE HANDLE: %x\n", pnum
));
339 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
343 if (vuid
!= fsp
->vuid
) {
344 DEBUG(1, ("Got pipe request (pnum %x) using invalid VUID %d, "
345 "expected %d\n", pnum
, vuid
, fsp
->vuid
));
346 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
350 DEBUG(3,("Got API command 0x%x on pipe \"%s\" (pnum %x)\n",
351 subcommand
, fsp
->fsp_name
, pnum
));
353 DEBUG(10, ("api_fd_reply: p:%p max_trans_reply: %d\n", fsp
, mdrcnt
));
355 switch (subcommand
) {
356 case TRANSACT_DCERPCCMD
: {
357 /* dce/rpc command */
359 status
= np_write(fsp
, data
, tdscnt
, &nwritten
);
360 if (!NT_STATUS_IS_OK(status
)) {
361 api_no_reply(conn
, req
);
364 api_rpc_trans_reply(conn
, req
, fsp
, mdrcnt
);
367 case TRANSACT_WAITNAMEDPIPEHANDLESTATE
:
368 /* Wait Named Pipe Handle state */
369 api_WNPHS(conn
, req
, fsp
, params
, tpscnt
);
371 case TRANSACT_SETNAMEDPIPEHANDLESTATE
:
372 /* Set Named Pipe Handle state */
373 api_SNPHS(conn
, req
, fsp
, params
, tpscnt
);
376 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
381 /****************************************************************************
382 Handle named pipe commands.
383 ****************************************************************************/
385 static void named_pipe(connection_struct
*conn
, uint16 vuid
,
386 struct smb_request
*req
,
387 const char *name
, uint16
*setup
,
388 char *data
, char *params
,
389 int suwcnt
, int tdscnt
,int tpscnt
,
390 int msrcnt
, int mdrcnt
, int mprcnt
)
392 DEBUG(3,("named pipe command on <%s> name\n", name
));
394 if (strequal(name
,"LANMAN")) {
395 api_reply(conn
, vuid
, req
,
402 if (strequal(name
,"WKSSVC") ||
403 strequal(name
,"SRVSVC") ||
404 strequal(name
,"WINREG") ||
405 strequal(name
,"SAMR") ||
406 strequal(name
,"LSARPC")) {
408 DEBUG(4,("named pipe command from Win95 (wow!)\n"));
410 api_fd_reply(conn
, vuid
, req
,
411 setup
, (uint8_t *)data
, params
,
412 suwcnt
, tdscnt
, tpscnt
,
417 if (strlen(name
) < 1) {
418 api_fd_reply(conn
, vuid
, req
,
419 setup
, (uint8_t *)data
,
420 params
, suwcnt
, tdscnt
,
421 tpscnt
, mdrcnt
, mprcnt
);
426 DEBUG(3,("unknown named pipe: setup 0x%X setup1=%d\n",
427 (int)setup
[0],(int)setup
[1]));
429 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
433 static void handle_trans(connection_struct
*conn
, struct smb_request
*req
,
434 struct trans_state
*state
)
436 char *local_machine_name
;
439 DEBUG(3,("trans <%s> data=%u params=%u setup=%u\n",
440 state
->name
,(unsigned int)state
->total_data
,(unsigned int)state
->total_param
,
441 (unsigned int)state
->setup_count
));
444 * WinCE wierdness....
447 local_machine_name
= talloc_asprintf(state
, "\\%s\\",
448 get_local_machine_name());
450 if (local_machine_name
== NULL
) {
451 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
455 if (strnequal(state
->name
, local_machine_name
,
456 strlen(local_machine_name
))) {
457 name_offset
= strlen(local_machine_name
)-1;
460 if (!strnequal(&state
->name
[name_offset
], "\\PIPE",
462 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
466 name_offset
+= strlen("\\PIPE");
468 /* Win9x weirdness. When talking to a unicode server Win9x
469 only sends \PIPE instead of \PIPE\ */
471 if (state
->name
[name_offset
] == '\\')
474 DEBUG(5,("calling named_pipe\n"));
475 named_pipe(conn
, state
->vuid
, req
,
476 state
->name
+name_offset
,
477 state
->setup
,state
->data
,
479 state
->setup_count
,state
->total_data
,
481 state
->max_setup_return
,
482 state
->max_data_return
,
483 state
->max_param_return
);
485 if (state
->close_on_completion
) {
486 close_cnum(conn
,state
->vuid
);
493 /****************************************************************************
495 ****************************************************************************/
497 void reply_trans(struct smb_request
*req
)
499 connection_struct
*conn
= req
->conn
;
504 struct trans_state
*state
;
507 START_PROFILE(SMBtrans
);
510 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
511 END_PROFILE(SMBtrans
);
515 dsoff
= SVAL(req
->vwv
+12, 0);
516 dscnt
= SVAL(req
->vwv
+11, 0);
517 psoff
= SVAL(req
->vwv
+10, 0);
518 pscnt
= SVAL(req
->vwv
+9, 0);
520 result
= allow_new_trans(conn
->pending_trans
, req
->mid
);
521 if (!NT_STATUS_IS_OK(result
)) {
522 DEBUG(2, ("Got invalid trans request: %s\n",
524 reply_nterror(req
, result
);
525 END_PROFILE(SMBtrans
);
529 if ((state
= TALLOC_P(conn
, struct trans_state
)) == NULL
) {
530 DEBUG(0, ("talloc failed\n"));
531 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
532 END_PROFILE(SMBtrans
);
536 state
->cmd
= SMBtrans
;
538 state
->mid
= req
->mid
;
539 state
->vuid
= req
->vuid
;
540 state
->setup_count
= CVAL(req
->vwv
+13, 0);
542 state
->total_param
= SVAL(req
->vwv
+0, 0);
544 state
->total_data
= SVAL(req
->vwv
+1, 0);
546 state
->max_param_return
= SVAL(req
->vwv
+2, 0);
547 state
->max_data_return
= SVAL(req
->vwv
+3, 0);
548 state
->max_setup_return
= CVAL(req
->vwv
+4, 0);
549 state
->close_on_completion
= BITSETW(req
->vwv
+5, 0);
550 state
->one_way
= BITSETW(req
->vwv
+5, 1);
552 srvstr_pull_req_talloc(state
, req
, &state
->name
, req
->buf
,
555 if ((dscnt
> state
->total_data
) || (pscnt
> state
->total_param
) ||
559 if (state
->total_data
) {
561 if (trans_oob(state
->total_data
, 0, dscnt
)
562 || trans_oob(smb_len(req
->inbuf
), dsoff
, dscnt
)) {
566 /* Can't use talloc here, the core routines do realloc on the
567 * params and data. Out of paranoia, 100 bytes too many. */
568 state
->data
= (char *)SMB_MALLOC(state
->total_data
+100);
569 if (state
->data
== NULL
) {
570 DEBUG(0,("reply_trans: data malloc fail for %u "
571 "bytes !\n", (unsigned int)state
->total_data
));
573 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
574 END_PROFILE(SMBtrans
);
577 /* null-terminate the slack space */
578 memset(&state
->data
[state
->total_data
], 0, 100);
580 memcpy(state
->data
,smb_base(req
->inbuf
)+dsoff
,dscnt
);
583 if (state
->total_param
) {
585 if (trans_oob(state
->total_param
, 0, pscnt
)
586 || trans_oob(smb_len(req
->inbuf
), psoff
, pscnt
)) {
590 /* Can't use talloc here, the core routines do realloc on the
591 * params and data. Out of paranoia, 100 bytes too many */
592 state
->param
= (char *)SMB_MALLOC(state
->total_param
+100);
593 if (state
->param
== NULL
) {
594 DEBUG(0,("reply_trans: param malloc fail for %u "
595 "bytes !\n", (unsigned int)state
->total_param
));
596 SAFE_FREE(state
->data
);
598 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
599 END_PROFILE(SMBtrans
);
602 /* null-terminate the slack space */
603 memset(&state
->param
[state
->total_param
], 0, 100);
605 memcpy(state
->param
,smb_base(req
->inbuf
)+psoff
,pscnt
);
608 state
->received_data
= dscnt
;
609 state
->received_param
= pscnt
;
611 if (state
->setup_count
) {
615 * No overflow possible here, state->setup_count is an
616 * unsigned int, being filled by a single byte from
617 * CVAL(req->vwv+13, 0) above. The cast in the comparison
618 * below is not necessary, it's here to clarify things. The
619 * validity of req->vwv and req->wct has been checked in
620 * init_smb_request already.
622 if (state
->setup_count
+ 14 > (unsigned int)req
->wct
) {
626 if((state
->setup
= TALLOC_ARRAY(
627 state
, uint16
, state
->setup_count
)) == NULL
) {
628 DEBUG(0,("reply_trans: setup malloc fail for %u "
629 "bytes !\n", (unsigned int)
630 (state
->setup_count
* sizeof(uint16
))));
631 SAFE_FREE(state
->data
);
632 SAFE_FREE(state
->param
);
634 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
635 END_PROFILE(SMBtrans
);
639 for (i
=0;i
<state
->setup_count
;i
++) {
640 state
->setup
[i
] = SVAL(req
->vwv
+ 14 + i
, 0);
644 state
->received_param
= pscnt
;
646 if ((state
->received_param
!= state
->total_param
) ||
647 (state
->received_data
!= state
->total_data
)) {
648 DLIST_ADD(conn
->pending_trans
, state
);
650 /* We need to send an interim response then receive the rest
651 of the parameter/data bytes */
652 reply_outbuf(req
, 0, 0);
653 show_msg((char *)req
->outbuf
);
654 END_PROFILE(SMBtrans
);
658 handle_trans(conn
, req
, state
);
660 SAFE_FREE(state
->data
);
661 SAFE_FREE(state
->param
);
664 END_PROFILE(SMBtrans
);
669 DEBUG(0,("reply_trans: invalid trans parameters\n"));
670 SAFE_FREE(state
->data
);
671 SAFE_FREE(state
->param
);
673 END_PROFILE(SMBtrans
);
674 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
678 /****************************************************************************
679 Reply to a secondary SMBtrans.
680 ****************************************************************************/
682 void reply_transs(struct smb_request
*req
)
684 connection_struct
*conn
= req
->conn
;
685 unsigned int pcnt
,poff
,dcnt
,doff
,pdisp
,ddisp
;
686 struct trans_state
*state
;
688 START_PROFILE(SMBtranss
);
690 show_msg((char *)req
->inbuf
);
693 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
694 END_PROFILE(SMBtranss
);
698 for (state
= conn
->pending_trans
; state
!= NULL
;
699 state
= state
->next
) {
700 if (state
->mid
== req
->mid
) {
705 if ((state
== NULL
) || (state
->cmd
!= SMBtrans
)) {
706 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
707 END_PROFILE(SMBtranss
);
711 /* Revise total_params and total_data in case they have changed
714 if (SVAL(req
->vwv
+0, 0) < state
->total_param
)
715 state
->total_param
= SVAL(req
->vwv
+0, 0);
716 if (SVAL(req
->vwv
+1, 0) < state
->total_data
)
717 state
->total_data
= SVAL(req
->vwv
+1, 0);
719 pcnt
= SVAL(req
->vwv
+2, 0);
720 poff
= SVAL(req
->vwv
+3, 0);
721 pdisp
= SVAL(req
->vwv
+4, 0);
723 dcnt
= SVAL(req
->vwv
+5, 0);
724 doff
= SVAL(req
->vwv
+6, 0);
725 ddisp
= SVAL(req
->vwv
+7, 0);
727 state
->received_param
+= pcnt
;
728 state
->received_data
+= dcnt
;
730 if ((state
->received_data
> state
->total_data
) ||
731 (state
->received_param
> state
->total_param
))
735 if (trans_oob(state
->total_param
, pdisp
, pcnt
)
736 || trans_oob(smb_len(req
->inbuf
), poff
, pcnt
)) {
739 memcpy(state
->param
+pdisp
,smb_base(req
->inbuf
)+poff
,pcnt
);
743 if (trans_oob(state
->total_data
, ddisp
, dcnt
)
744 || trans_oob(smb_len(req
->inbuf
), doff
, dcnt
)) {
747 memcpy(state
->data
+ddisp
, smb_base(req
->inbuf
)+doff
,dcnt
);
750 if ((state
->received_param
< state
->total_param
) ||
751 (state
->received_data
< state
->total_data
)) {
752 END_PROFILE(SMBtranss
);
756 handle_trans(conn
, req
, state
);
758 DLIST_REMOVE(conn
->pending_trans
, state
);
759 SAFE_FREE(state
->data
);
760 SAFE_FREE(state
->param
);
763 END_PROFILE(SMBtranss
);
768 DEBUG(0,("reply_transs: invalid trans parameters\n"));
769 DLIST_REMOVE(conn
->pending_trans
, state
);
770 SAFE_FREE(state
->data
);
771 SAFE_FREE(state
->param
);
773 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
774 END_PROFILE(SMBtranss
);