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
28 #include "smbd/globals.h"
30 #define NERR_notsupported 50
32 static void api_no_reply(connection_struct
*conn
, struct smb_request
*req
);
34 /*******************************************************************
35 copies parameters and data, as needed, into the smb buffer
37 *both* the data and params sections should be aligned. this
38 is fudged in the rpc pipes by
39 at present, only the data section is. this may be a possible
40 cause of some of the ipc problems being experienced. lkcl26dec97
42 ******************************************************************/
44 static void copy_trans_params_and_data(char *outbuf
, int align
,
45 char *rparam
, int param_offset
, int param_len
,
46 char *rdata
, int data_offset
, int data_len
)
48 char *copy_into
= smb_buf(outbuf
);
56 DEBUG(5,("copy_trans_params_and_data: params[%d..%d] data[%d..%d] (align %d)\n",
57 param_offset
, param_offset
+ param_len
,
58 data_offset
, data_offset
+ data_len
,
66 memcpy(copy_into
, &rparam
[param_offset
], param_len
);
68 copy_into
+= param_len
;
70 memset(copy_into
, '\0', align
);
76 memcpy(copy_into
, &rdata
[data_offset
], data_len
);
79 /****************************************************************************
81 ****************************************************************************/
83 void send_trans_reply(connection_struct
*conn
,
84 struct smb_request
*req
,
85 char *rparam
, int rparam_len
,
86 char *rdata
, int rdata_len
,
87 bool buffer_too_large
)
89 int this_ldata
,this_lparam
;
90 int tot_data_sent
= 0;
91 int tot_param_sent
= 0;
94 int ldata
= rdata
? rdata_len
: 0;
95 int lparam
= rparam
? rparam_len
: 0;
98 DEBUG(5,("send_trans_reply: buffer %d too large\n", ldata
));
100 this_lparam
= MIN(lparam
,max_send
- 500); /* hack */
101 this_ldata
= MIN(ldata
,max_send
- (500+this_lparam
));
103 align
= ((this_lparam
)%4);
105 reply_outbuf(req
, 10, 1+align
+this_ldata
+this_lparam
);
108 * We might have SMBtranss in req which was transferred to the outbuf,
111 SCVAL(req
->outbuf
, smb_com
, SMBtrans
);
113 copy_trans_params_and_data((char *)req
->outbuf
, align
,
114 rparam
, tot_param_sent
, this_lparam
,
115 rdata
, tot_data_sent
, this_ldata
);
117 SSVAL(req
->outbuf
,smb_vwv0
,lparam
);
118 SSVAL(req
->outbuf
,smb_vwv1
,ldata
);
119 SSVAL(req
->outbuf
,smb_vwv3
,this_lparam
);
120 SSVAL(req
->outbuf
,smb_vwv4
,
121 smb_offset(smb_buf(req
->outbuf
)+1, req
->outbuf
));
122 SSVAL(req
->outbuf
,smb_vwv5
,0);
123 SSVAL(req
->outbuf
,smb_vwv6
,this_ldata
);
124 SSVAL(req
->outbuf
,smb_vwv7
,
125 smb_offset(smb_buf(req
->outbuf
)+1+this_lparam
+align
,
127 SSVAL(req
->outbuf
,smb_vwv8
,0);
128 SSVAL(req
->outbuf
,smb_vwv9
,0);
130 if (buffer_too_large
) {
131 error_packet_set((char *)req
->outbuf
, ERRDOS
, ERRmoredata
,
132 STATUS_BUFFER_OVERFLOW
, __LINE__
, __FILE__
);
135 show_msg((char *)req
->outbuf
);
136 if (!srv_send_smb(smbd_server_fd(), (char *)req
->outbuf
,
137 IS_CONN_ENCRYPTED(conn
))) {
138 exit_server_cleanly("send_trans_reply: srv_send_smb failed.");
141 TALLOC_FREE(req
->outbuf
);
143 tot_data_sent
= this_ldata
;
144 tot_param_sent
= this_lparam
;
146 while (tot_data_sent
< ldata
|| tot_param_sent
< lparam
)
148 this_lparam
= MIN(lparam
-tot_param_sent
,
149 max_send
- 500); /* hack */
150 this_ldata
= MIN(ldata
-tot_data_sent
,
151 max_send
- (500+this_lparam
));
159 align
= (this_lparam
%4);
161 reply_outbuf(req
, 10, 1+align
+this_ldata
+this_lparam
);
164 * We might have SMBtranss in req which was transferred to the
167 SCVAL(req
->outbuf
, smb_com
, SMBtrans
);
169 copy_trans_params_and_data((char *)req
->outbuf
, align
,
170 rparam
, tot_param_sent
, this_lparam
,
171 rdata
, tot_data_sent
, this_ldata
);
173 SSVAL(req
->outbuf
,smb_vwv3
,this_lparam
);
174 SSVAL(req
->outbuf
,smb_vwv4
,
175 smb_offset(smb_buf(req
->outbuf
)+1,req
->outbuf
));
176 SSVAL(req
->outbuf
,smb_vwv5
,tot_param_sent
);
177 SSVAL(req
->outbuf
,smb_vwv6
,this_ldata
);
178 SSVAL(req
->outbuf
,smb_vwv7
,
179 smb_offset(smb_buf(req
->outbuf
)+1+this_lparam
+align
,
181 SSVAL(req
->outbuf
,smb_vwv8
,tot_data_sent
);
182 SSVAL(req
->outbuf
,smb_vwv9
,0);
184 if (buffer_too_large
) {
185 error_packet_set((char *)req
->outbuf
,
187 STATUS_BUFFER_OVERFLOW
,
191 show_msg((char *)req
->outbuf
);
192 if (!srv_send_smb(smbd_server_fd(), (char *)req
->outbuf
,
193 IS_CONN_ENCRYPTED(conn
)))
194 exit_server_cleanly("send_trans_reply: srv_send_smb "
197 tot_data_sent
+= this_ldata
;
198 tot_param_sent
+= this_lparam
;
199 TALLOC_FREE(req
->outbuf
);
203 /****************************************************************************
204 Start the first part of an RPC reply which began with an SMBtrans request.
205 ****************************************************************************/
207 static void api_rpc_trans_reply(connection_struct
*conn
,
208 struct smb_request
*req
,
212 bool is_data_outstanding
;
213 uint8_t *rdata
= SMB_MALLOC_ARRAY(uint8_t, max_trans_reply
);
218 DEBUG(0,("api_rpc_trans_reply: malloc fail.\n"));
219 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
223 if (!fsp_is_np(fsp
)) {
225 api_no_reply(conn
,req
);
229 status
= np_read(fsp
->fake_file_handle
, rdata
, max_trans_reply
,
230 &data_len
, &is_data_outstanding
);
231 if (!NT_STATUS_IS_OK(status
)) {
233 api_no_reply(conn
,req
);
237 send_trans_reply(conn
, req
, NULL
, 0, (char *)rdata
, data_len
,
238 is_data_outstanding
);
243 /****************************************************************************
244 WaitNamedPipeHandleState
245 ****************************************************************************/
247 static void api_WNPHS(connection_struct
*conn
, struct smb_request
*req
,
248 struct files_struct
*fsp
, char *param
, int param_len
)
250 if (!param
|| param_len
< 2) {
251 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
255 DEBUG(4,("WaitNamedPipeHandleState priority %x\n",
256 (int)SVAL(param
,0)));
258 send_trans_reply(conn
, req
, NULL
, 0, NULL
, 0, False
);
262 /****************************************************************************
263 SetNamedPipeHandleState
264 ****************************************************************************/
266 static void api_SNPHS(connection_struct
*conn
, struct smb_request
*req
,
267 struct files_struct
*fsp
, char *param
, int param_len
)
269 if (!param
|| param_len
< 2) {
270 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
274 DEBUG(4,("SetNamedPipeHandleState to code %x\n", (int)SVAL(param
,0)));
276 send_trans_reply(conn
, req
, NULL
, 0, NULL
, 0, False
);
280 /****************************************************************************
281 When no reply is generated, indicate unsupported.
282 ****************************************************************************/
284 static void api_no_reply(connection_struct
*conn
, struct smb_request
*req
)
289 SSVAL(rparam
,0,NERR_notsupported
);
290 SSVAL(rparam
,2,0); /* converter word */
292 DEBUG(3,("Unsupported API fd command\n"));
294 /* now send the reply */
295 send_trans_reply(conn
, req
, rparam
, 4, NULL
, 0, False
);
300 /****************************************************************************
301 Handle remote api calls delivered to a named pipe already opened.
302 ****************************************************************************/
304 static void api_fd_reply(connection_struct
*conn
, uint16 vuid
,
305 struct smb_request
*req
,
306 uint16
*setup
, uint8_t *data
, char *params
,
307 int suwcnt
, int tdscnt
, int tpscnt
,
308 int mdrcnt
, int mprcnt
)
310 struct files_struct
*fsp
;
315 DEBUG(5,("api_fd_reply\n"));
317 /* First find out the name of this file. */
319 DEBUG(0,("Unexpected named pipe transaction.\n"));
320 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
324 /* Get the file handle and hence the file name. */
326 * NB. The setup array has already been transformed
327 * via SVAL and so is in host byte order.
329 pnum
= ((int)setup
[1]) & 0xFFFF;
330 subcommand
= ((int)setup
[0]) & 0xFFFF;
332 fsp
= file_fsp(req
, pnum
);
334 if (!fsp_is_np(fsp
)) {
335 if (subcommand
== TRANSACT_WAITNAMEDPIPEHANDLESTATE
) {
336 /* Win9x does this call with a unicode pipe name, not a pnum. */
337 /* Just return success for now... */
338 DEBUG(3,("Got TRANSACT_WAITNAMEDPIPEHANDLESTATE on text pipe name\n"));
339 send_trans_reply(conn
, req
, NULL
, 0, NULL
, 0, False
);
343 DEBUG(1,("api_fd_reply: INVALID PIPE HANDLE: %x\n", pnum
));
344 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
348 if (vuid
!= fsp
->vuid
) {
349 DEBUG(1, ("Got pipe request (pnum %x) using invalid VUID %d, "
350 "expected %d\n", pnum
, vuid
, fsp
->vuid
));
351 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
355 DEBUG(3,("Got API command 0x%x on pipe \"%s\" (pnum %x)\n",
356 subcommand
, fsp
->fsp_name
, pnum
));
358 DEBUG(10, ("api_fd_reply: p:%p max_trans_reply: %d\n", fsp
, mdrcnt
));
360 switch (subcommand
) {
361 case TRANSACT_DCERPCCMD
: {
362 /* dce/rpc command */
364 status
= np_write(fsp
->fake_file_handle
, data
, tdscnt
,
366 if (!NT_STATUS_IS_OK(status
)) {
367 api_no_reply(conn
, req
);
370 api_rpc_trans_reply(conn
, req
, fsp
, mdrcnt
);
373 case TRANSACT_WAITNAMEDPIPEHANDLESTATE
:
374 /* Wait Named Pipe Handle state */
375 api_WNPHS(conn
, req
, fsp
, params
, tpscnt
);
377 case TRANSACT_SETNAMEDPIPEHANDLESTATE
:
378 /* Set Named Pipe Handle state */
379 api_SNPHS(conn
, req
, fsp
, params
, tpscnt
);
382 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
387 /****************************************************************************
388 Handle named pipe commands.
389 ****************************************************************************/
391 static void named_pipe(connection_struct
*conn
, uint16 vuid
,
392 struct smb_request
*req
,
393 const char *name
, uint16
*setup
,
394 char *data
, char *params
,
395 int suwcnt
, int tdscnt
,int tpscnt
,
396 int msrcnt
, int mdrcnt
, int mprcnt
)
398 DEBUG(3,("named pipe command on <%s> name\n", name
));
400 if (strequal(name
,"LANMAN")) {
401 api_reply(conn
, vuid
, req
,
408 if (strequal(name
,"WKSSVC") ||
409 strequal(name
,"SRVSVC") ||
410 strequal(name
,"WINREG") ||
411 strequal(name
,"SAMR") ||
412 strequal(name
,"LSARPC")) {
414 DEBUG(4,("named pipe command from Win95 (wow!)\n"));
416 api_fd_reply(conn
, vuid
, req
,
417 setup
, (uint8_t *)data
, params
,
418 suwcnt
, tdscnt
, tpscnt
,
423 if (strlen(name
) < 1) {
424 api_fd_reply(conn
, vuid
, req
,
425 setup
, (uint8_t *)data
,
426 params
, suwcnt
, tdscnt
,
427 tpscnt
, mdrcnt
, mprcnt
);
432 DEBUG(3,("unknown named pipe: setup 0x%X setup1=%d\n",
433 (int)setup
[0],(int)setup
[1]));
435 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
439 static void handle_trans(connection_struct
*conn
, struct smb_request
*req
,
440 struct trans_state
*state
)
442 char *local_machine_name
;
445 DEBUG(3,("trans <%s> data=%u params=%u setup=%u\n",
446 state
->name
,(unsigned int)state
->total_data
,(unsigned int)state
->total_param
,
447 (unsigned int)state
->setup_count
));
450 * WinCE wierdness....
453 local_machine_name
= talloc_asprintf(state
, "\\%s\\",
454 get_local_machine_name());
456 if (local_machine_name
== NULL
) {
457 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
461 if (strnequal(state
->name
, local_machine_name
,
462 strlen(local_machine_name
))) {
463 name_offset
= strlen(local_machine_name
)-1;
466 if (!strnequal(&state
->name
[name_offset
], "\\PIPE",
468 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
472 name_offset
+= strlen("\\PIPE");
474 /* Win9x weirdness. When talking to a unicode server Win9x
475 only sends \PIPE instead of \PIPE\ */
477 if (state
->name
[name_offset
] == '\\')
480 DEBUG(5,("calling named_pipe\n"));
481 named_pipe(conn
, state
->vuid
, req
,
482 state
->name
+name_offset
,
483 state
->setup
,state
->data
,
485 state
->setup_count
,state
->total_data
,
487 state
->max_setup_return
,
488 state
->max_data_return
,
489 state
->max_param_return
);
491 if (state
->close_on_completion
) {
492 close_cnum(conn
,state
->vuid
);
499 /****************************************************************************
501 ****************************************************************************/
503 void reply_trans(struct smb_request
*req
)
505 connection_struct
*conn
= req
->conn
;
510 struct trans_state
*state
;
513 START_PROFILE(SMBtrans
);
516 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
517 END_PROFILE(SMBtrans
);
521 dsoff
= SVAL(req
->vwv
+12, 0);
522 dscnt
= SVAL(req
->vwv
+11, 0);
523 psoff
= SVAL(req
->vwv
+10, 0);
524 pscnt
= SVAL(req
->vwv
+9, 0);
526 result
= allow_new_trans(conn
->pending_trans
, req
->mid
);
527 if (!NT_STATUS_IS_OK(result
)) {
528 DEBUG(2, ("Got invalid trans request: %s\n",
530 reply_nterror(req
, result
);
531 END_PROFILE(SMBtrans
);
535 if ((state
= TALLOC_P(conn
, struct trans_state
)) == NULL
) {
536 DEBUG(0, ("talloc failed\n"));
537 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
538 END_PROFILE(SMBtrans
);
542 state
->cmd
= SMBtrans
;
544 state
->mid
= req
->mid
;
545 state
->vuid
= req
->vuid
;
546 state
->setup_count
= CVAL(req
->vwv
+13, 0);
548 state
->total_param
= SVAL(req
->vwv
+0, 0);
550 state
->total_data
= SVAL(req
->vwv
+1, 0);
552 state
->max_param_return
= SVAL(req
->vwv
+2, 0);
553 state
->max_data_return
= SVAL(req
->vwv
+3, 0);
554 state
->max_setup_return
= CVAL(req
->vwv
+4, 0);
555 state
->close_on_completion
= BITSETW(req
->vwv
+5, 0);
556 state
->one_way
= BITSETW(req
->vwv
+5, 1);
558 srvstr_pull_req_talloc(state
, req
, &state
->name
, req
->buf
,
561 if ((dscnt
> state
->total_data
) || (pscnt
> state
->total_param
) ||
565 if (state
->total_data
) {
567 if (trans_oob(state
->total_data
, 0, dscnt
)
568 || trans_oob(smb_len(req
->inbuf
), dsoff
, dscnt
)) {
572 /* Can't use talloc here, the core routines do realloc on the
573 * params and data. Out of paranoia, 100 bytes too many. */
574 state
->data
= (char *)SMB_MALLOC(state
->total_data
+100);
575 if (state
->data
== NULL
) {
576 DEBUG(0,("reply_trans: data malloc fail for %u "
577 "bytes !\n", (unsigned int)state
->total_data
));
579 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
580 END_PROFILE(SMBtrans
);
583 /* null-terminate the slack space */
584 memset(&state
->data
[state
->total_data
], 0, 100);
586 memcpy(state
->data
,smb_base(req
->inbuf
)+dsoff
,dscnt
);
589 if (state
->total_param
) {
591 if (trans_oob(state
->total_param
, 0, pscnt
)
592 || trans_oob(smb_len(req
->inbuf
), psoff
, pscnt
)) {
596 /* Can't use talloc here, the core routines do realloc on the
597 * params and data. Out of paranoia, 100 bytes too many */
598 state
->param
= (char *)SMB_MALLOC(state
->total_param
+100);
599 if (state
->param
== NULL
) {
600 DEBUG(0,("reply_trans: param malloc fail for %u "
601 "bytes !\n", (unsigned int)state
->total_param
));
602 SAFE_FREE(state
->data
);
604 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
605 END_PROFILE(SMBtrans
);
608 /* null-terminate the slack space */
609 memset(&state
->param
[state
->total_param
], 0, 100);
611 memcpy(state
->param
,smb_base(req
->inbuf
)+psoff
,pscnt
);
614 state
->received_data
= dscnt
;
615 state
->received_param
= pscnt
;
617 if (state
->setup_count
) {
621 * No overflow possible here, state->setup_count is an
622 * unsigned int, being filled by a single byte from
623 * CVAL(req->vwv+13, 0) above. The cast in the comparison
624 * below is not necessary, it's here to clarify things. The
625 * validity of req->vwv and req->wct has been checked in
626 * init_smb_request already.
628 if (state
->setup_count
+ 14 > (unsigned int)req
->wct
) {
632 if((state
->setup
= TALLOC_ARRAY(
633 state
, uint16
, state
->setup_count
)) == NULL
) {
634 DEBUG(0,("reply_trans: setup malloc fail for %u "
635 "bytes !\n", (unsigned int)
636 (state
->setup_count
* sizeof(uint16
))));
637 SAFE_FREE(state
->data
);
638 SAFE_FREE(state
->param
);
640 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
641 END_PROFILE(SMBtrans
);
645 for (i
=0;i
<state
->setup_count
;i
++) {
646 state
->setup
[i
] = SVAL(req
->vwv
+ 14 + i
, 0);
650 state
->received_param
= pscnt
;
652 if ((state
->received_param
!= state
->total_param
) ||
653 (state
->received_data
!= state
->total_data
)) {
654 DLIST_ADD(conn
->pending_trans
, state
);
656 /* We need to send an interim response then receive the rest
657 of the parameter/data bytes */
658 reply_outbuf(req
, 0, 0);
659 show_msg((char *)req
->outbuf
);
660 END_PROFILE(SMBtrans
);
664 handle_trans(conn
, req
, state
);
666 SAFE_FREE(state
->data
);
667 SAFE_FREE(state
->param
);
670 END_PROFILE(SMBtrans
);
675 DEBUG(0,("reply_trans: invalid trans parameters\n"));
676 SAFE_FREE(state
->data
);
677 SAFE_FREE(state
->param
);
679 END_PROFILE(SMBtrans
);
680 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
684 /****************************************************************************
685 Reply to a secondary SMBtrans.
686 ****************************************************************************/
688 void reply_transs(struct smb_request
*req
)
690 connection_struct
*conn
= req
->conn
;
691 unsigned int pcnt
,poff
,dcnt
,doff
,pdisp
,ddisp
;
692 struct trans_state
*state
;
694 START_PROFILE(SMBtranss
);
696 show_msg((char *)req
->inbuf
);
699 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
700 END_PROFILE(SMBtranss
);
704 for (state
= conn
->pending_trans
; state
!= NULL
;
705 state
= state
->next
) {
706 if (state
->mid
== req
->mid
) {
711 if ((state
== NULL
) || (state
->cmd
!= SMBtrans
)) {
712 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
713 END_PROFILE(SMBtranss
);
717 /* Revise total_params and total_data in case they have changed
720 if (SVAL(req
->vwv
+0, 0) < state
->total_param
)
721 state
->total_param
= SVAL(req
->vwv
+0, 0);
722 if (SVAL(req
->vwv
+1, 0) < state
->total_data
)
723 state
->total_data
= SVAL(req
->vwv
+1, 0);
725 pcnt
= SVAL(req
->vwv
+2, 0);
726 poff
= SVAL(req
->vwv
+3, 0);
727 pdisp
= SVAL(req
->vwv
+4, 0);
729 dcnt
= SVAL(req
->vwv
+5, 0);
730 doff
= SVAL(req
->vwv
+6, 0);
731 ddisp
= SVAL(req
->vwv
+7, 0);
733 state
->received_param
+= pcnt
;
734 state
->received_data
+= dcnt
;
736 if ((state
->received_data
> state
->total_data
) ||
737 (state
->received_param
> state
->total_param
))
741 if (trans_oob(state
->total_param
, pdisp
, pcnt
)
742 || trans_oob(smb_len(req
->inbuf
), poff
, pcnt
)) {
745 memcpy(state
->param
+pdisp
,smb_base(req
->inbuf
)+poff
,pcnt
);
749 if (trans_oob(state
->total_data
, ddisp
, dcnt
)
750 || trans_oob(smb_len(req
->inbuf
), doff
, dcnt
)) {
753 memcpy(state
->data
+ddisp
, smb_base(req
->inbuf
)+doff
,dcnt
);
756 if ((state
->received_param
< state
->total_param
) ||
757 (state
->received_data
< state
->total_data
)) {
758 END_PROFILE(SMBtranss
);
762 handle_trans(conn
, req
, state
);
764 DLIST_REMOVE(conn
->pending_trans
, state
);
765 SAFE_FREE(state
->data
);
766 SAFE_FREE(state
->param
);
769 END_PROFILE(SMBtranss
);
774 DEBUG(0,("reply_transs: invalid trans parameters\n"));
775 DLIST_REMOVE(conn
->pending_trans
, state
);
776 SAFE_FREE(state
->data
);
777 SAFE_FREE(state
->param
);
779 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
780 END_PROFILE(SMBtranss
);