Fix proxy module
[Samba.git] / source3 / smbd / pipes.c
blobb148cff04571383a5e1d527c13ea390ee561717e
1 /*
2 Unix SMB/CIFS implementation.
3 Pipe SMB reply routines
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Luke Kenneth Casson Leighton 1996-1998
6 Copyright (C) Paul Ashton 1997-1998.
7 Copyright (C) Jeremy Allison 2005.
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 reply_ calls on named pipes that the server
24 makes to handle specific protocols
28 #include "includes.h"
30 #define PIPE "\\PIPE\\"
31 #define PIPELEN strlen(PIPE)
33 #define MAX_PIPE_NAME_LEN 24
35 NTSTATUS open_np_file(struct smb_request *smb_req, const char *name,
36 struct files_struct **pfsp)
38 struct connection_struct *conn = smb_req->conn;
39 struct files_struct *fsp;
40 NTSTATUS status;
42 status = file_new(smb_req, conn, &fsp);
43 if (!NT_STATUS_IS_OK(status)) {
44 DEBUG(0, ("file_new failed: %s\n", nt_errstr(status)));
45 return status;
48 fsp->conn = conn;
49 fsp->fh->fd = -1;
50 fsp->vuid = smb_req->vuid;
51 fsp->can_lock = false;
52 fsp->access_mask = FILE_READ_DATA | FILE_WRITE_DATA;
53 string_set(&fsp->fsp_name, name);
55 status = np_open(NULL, name, conn->client_address,
56 conn->server_info, &fsp->fake_file_handle);
57 if (!NT_STATUS_IS_OK(status)) {
58 DEBUG(10, ("np_open(%s) returned %s\n", name,
59 nt_errstr(status)));
60 file_free(smb_req, fsp);
61 return status;
64 *pfsp = fsp;
66 return NT_STATUS_OK;
69 /****************************************************************************
70 Reply to an open and X on a named pipe.
71 This code is basically stolen from reply_open_and_X with some
72 wrinkles to handle pipes.
73 ****************************************************************************/
75 void reply_open_pipe_and_X(connection_struct *conn, struct smb_request *req)
77 const char *fname = NULL;
78 char *pipe_name = NULL;
79 files_struct *fsp;
80 TALLOC_CTX *ctx = talloc_tos();
81 NTSTATUS status;
83 /* XXXX we need to handle passed times, sattr and flags */
84 srvstr_pull_req_talloc(ctx, req, &pipe_name, req->buf, STR_TERMINATE);
85 if (!pipe_name) {
86 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
87 ERRDOS, ERRbadpipe);
88 return;
91 /* If the name doesn't start \PIPE\ then this is directed */
92 /* at a mailslot or something we really, really don't understand, */
93 /* not just something we really don't understand. */
94 if ( strncmp(pipe_name,PIPE,PIPELEN) != 0 ) {
95 reply_doserror(req, ERRSRV, ERRaccess);
96 return;
99 DEBUG(4,("Opening pipe %s.\n", pipe_name));
101 /* Strip \PIPE\ off the name. */
102 fname = pipe_name + PIPELEN;
104 #if 0
106 * Hack for NT printers... JRA.
108 if(should_fail_next_srvsvc_open(fname)) {
109 reply_doserror(req, ERRSRV, ERRaccess);
110 return;
112 #endif
114 status = open_np_file(req, fname, &fsp);
115 if (!NT_STATUS_IS_OK(status)) {
116 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
117 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
118 ERRDOS, ERRbadpipe);
119 return;
121 reply_nterror(req, status);
122 return;
125 /* Prepare the reply */
126 reply_outbuf(req, 15, 0);
128 /* Mark the opened file as an existing named pipe in message mode. */
129 SSVAL(req->outbuf,smb_vwv9,2);
130 SSVAL(req->outbuf,smb_vwv10,0xc700);
132 SSVAL(req->outbuf, smb_vwv2, fsp->fnum);
133 SSVAL(req->outbuf, smb_vwv3, 0); /* fmode */
134 srv_put_dos_date3((char *)req->outbuf, smb_vwv4, 0); /* mtime */
135 SIVAL(req->outbuf, smb_vwv6, 0); /* size */
136 SSVAL(req->outbuf, smb_vwv8, 0); /* rmode */
137 SSVAL(req->outbuf, smb_vwv11, 0x0001);
139 chain_reply(req);
140 return;
143 /****************************************************************************
144 Reply to a write on a pipe.
145 ****************************************************************************/
147 void reply_pipe_write(struct smb_request *req)
149 files_struct *fsp = file_fsp(req, SVAL(req->vwv+0, 0));
150 size_t numtowrite = SVAL(req->vwv+1, 0);
151 ssize_t nwritten;
152 const uint8_t *data;
154 if (!fsp_is_np(fsp)) {
155 reply_doserror(req, ERRDOS, ERRbadfid);
156 return;
159 if (fsp->vuid != req->vuid) {
160 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
161 return;
164 data = req->buf + 3;
166 if (numtowrite == 0) {
167 nwritten = 0;
168 } else {
169 NTSTATUS status;
170 if (!fsp_is_np(fsp)) {
171 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
172 return;
174 DEBUG(6, ("reply_pipe_write: %x name: %s len: %d\n",
175 (int)fsp->fnum, fsp->fsp_name, (int)numtowrite));
176 status = np_write(fsp->fake_file_handle, data, numtowrite,
177 &nwritten);
178 if (!NT_STATUS_IS_OK(status)) {
179 reply_nterror(req, status);
180 return;
184 if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) {
185 reply_unixerror(req, ERRDOS, ERRnoaccess);
186 return;
189 reply_outbuf(req, 1, 0);
191 SSVAL(req->outbuf,smb_vwv0,nwritten);
193 DEBUG(3,("write-IPC pnum=%04x nwritten=%d\n", fsp->fnum,
194 (int)nwritten));
196 return;
199 /****************************************************************************
200 Reply to a write and X.
202 This code is basically stolen from reply_write_and_X with some
203 wrinkles to handle pipes.
204 ****************************************************************************/
206 void reply_pipe_write_and_X(struct smb_request *req)
208 files_struct *fsp = file_fsp(req, SVAL(req->vwv+2, 0));
209 size_t numtowrite = SVAL(req->vwv+10, 0);
210 ssize_t nwritten;
211 int smb_doff = SVAL(req->vwv+11, 0);
212 bool pipe_start_message_raw =
213 ((SVAL(req->vwv+7, 0) & (PIPE_START_MESSAGE|PIPE_RAW_MODE))
214 == (PIPE_START_MESSAGE|PIPE_RAW_MODE));
215 uint8_t *data;
217 if (!fsp_is_np(fsp)) {
218 reply_doserror(req, ERRDOS, ERRbadfid);
219 return;
222 if (fsp->vuid != req->vuid) {
223 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
224 return;
227 DEBUG(6, ("reply_pipe_write_and_X: %x name: %s len: %d\n",
228 (int)fsp->fnum, fsp->fsp_name, (int)numtowrite));
230 data = (uint8_t *)smb_base(req->inbuf) + smb_doff;
232 if (numtowrite == 0) {
233 nwritten = 0;
234 } else {
235 NTSTATUS status;
237 if(pipe_start_message_raw) {
239 * For the start of a message in named pipe byte mode,
240 * the first two bytes are a length-of-pdu field. Ignore
241 * them (we don't trust the client). JRA.
243 if(numtowrite < 2) {
244 DEBUG(0,("reply_pipe_write_and_X: start of "
245 "message set and not enough data "
246 "sent.(%u)\n",
247 (unsigned int)numtowrite ));
248 reply_unixerror(req, ERRDOS, ERRnoaccess);
249 return;
252 data += 2;
253 numtowrite -= 2;
255 status = np_write(fsp->fake_file_handle, data, numtowrite,
256 &nwritten);
257 if (!NT_STATUS_IS_OK(status)) {
258 reply_nterror(req, status);
259 return;
263 if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) {
264 reply_unixerror(req, ERRDOS,ERRnoaccess);
265 return;
268 reply_outbuf(req, 6, 0);
270 nwritten = (pipe_start_message_raw ? nwritten + 2 : nwritten);
271 SSVAL(req->outbuf,smb_vwv2,nwritten);
273 DEBUG(3,("writeX-IPC pnum=%04x nwritten=%d\n", fsp->fnum,
274 (int)nwritten));
276 chain_reply(req);
279 /****************************************************************************
280 Reply to a read and X.
281 This code is basically stolen from reply_read_and_X with some
282 wrinkles to handle pipes.
283 ****************************************************************************/
285 void reply_pipe_read_and_X(struct smb_request *req)
287 files_struct *fsp = file_fsp(req, SVAL(req->vwv+0, 0));
288 int smb_maxcnt = SVAL(req->vwv+5, 0);
289 int smb_mincnt = SVAL(req->vwv+6, 0);
290 ssize_t nread;
291 uint8_t *data;
292 bool unused;
293 NTSTATUS status;
295 /* we don't use the offset given to use for pipe reads. This
296 is deliberate, instead we always return the next lump of
297 data on the pipe */
298 #if 0
299 uint32 smb_offs = IVAL(req->vwv+3, 0);
300 #endif
302 if (!fsp_is_np(fsp)) {
303 reply_doserror(req, ERRDOS, ERRbadfid);
304 return;
307 if (fsp->vuid != req->vuid) {
308 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
309 return;
312 reply_outbuf(req, 12, smb_maxcnt);
314 data = (uint8_t *)smb_buf(req->outbuf);
316 status = np_read(fsp->fake_file_handle, data, smb_maxcnt, &nread,
317 &unused);
319 if (!NT_STATUS_IS_OK(status)) {
320 reply_doserror(req, ERRDOS, ERRnoaccess);
321 return;
324 srv_set_message((char *)req->outbuf, 12, nread, False);
326 SSVAL(req->outbuf,smb_vwv5,nread);
327 SSVAL(req->outbuf,smb_vwv6,
328 req_wct_ofs(req)
329 + 1 /* the wct field */
330 + 12 * sizeof(uint16_t) /* vwv */
331 + 2); /* the buflen field */
332 SSVAL(req->outbuf,smb_vwv11,smb_maxcnt);
334 DEBUG(3,("readX-IPC pnum=%04x min=%d max=%d nread=%d\n",
335 fsp->fnum, smb_mincnt, smb_maxcnt, (int)nread));
337 chain_reply(req);