this checkin gets rid of the global Files[] array and makes it local
[samba.git] / source / smbd / pipes.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Pipe SMB reply routines
5    Copyright (C) Andrew Tridgell 1992-1998
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1998
7    Copyright (C) Paul Ashton  1997-1998.
8    
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 2 of the License, or
12    (at your option) any later version.
13    
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.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23 /*
24    This file handles reply_ calls on named pipes that the server
25    makes to handle specific protocols
26 */
27
28
29 #include "includes.h"
30 #include "trans2.h"
31
32 #define PIPE            "\\PIPE\\"
33 #define PIPELEN         strlen(PIPE)
34
35 #define REALLOC(ptr,size) Realloc(ptr,MAX((size),4*1024))
36
37 /* look in server.c for some explanation of these variables */
38 extern int Protocol;
39 extern int DEBUGLEVEL;
40 extern char magic_char;
41 extern BOOL case_sensitive;
42 extern pstring sesssetup_user;
43 extern int Client;
44
45 #define VALID_PNUM(pnum)   (((pnum) >= 0) && ((pnum) < MAX_OPEN_PIPES))
46 #define OPEN_PNUM(pnum)    (VALID_PNUM(pnum) && Pipes[pnum].open)
47
48 /* this macro should always be used to extract an pnum (smb_fid) from
49    a packet to ensure chaining works correctly */
50 #define GETPNUM(buf,where) (chain_pnum!= -1?chain_pnum:SVAL(buf,where))
51
52 extern struct pipe_id_info pipe_names[];
53
54 /****************************************************************************
55   reply to an open and X on a named pipe
56
57   This code is basically stolen from reply_open_and_X with some
58   wrinkles to handle pipes.
59 ****************************************************************************/
60 int reply_open_pipe_and_X(connection_struct *conn,
61                           char *inbuf,char *outbuf,int length,int bufsize)
62 {
63   pstring fname;
64   uint16 vuid = SVAL(inbuf, smb_uid);
65   int pnum = -1;
66   int smb_ofun = SVAL(inbuf,smb_vwv8);
67   int size=0,fmode=0,mtime=0,rmode=0;
68   int i;
69
70   /* XXXX we need to handle passed times, sattr and flags */
71   pstrcpy(fname,smb_buf(inbuf));
72
73   /* If the name doesn't start \PIPE\ then this is directed */
74   /* at a mailslot or something we really, really don't understand, */
75   /* not just something we really don't understand. */
76   if ( strncmp(fname,PIPE,PIPELEN) != 0 )
77     return(ERROR(ERRSRV,ERRaccess));
78
79   DEBUG(4,("Opening pipe %s.\n", fname));
80
81   /* See if it is one we want to handle. */
82   for( i = 0; pipe_names[i].client_pipe ; i++ )
83     if( strequal(fname,pipe_names[i].client_pipe) )
84       break;
85
86   if (pipe_names[i].client_pipe == NULL)
87     return(ERROR(ERRSRV,ERRaccess));
88
89   /* Strip \PIPE\ off the name. */
90   pstrcpy(fname,smb_buf(inbuf) + PIPELEN);
91
92   /* Known pipes arrive with DIR attribs. Remove it so a regular file */
93   /* can be opened and add it in after the open. */
94   DEBUG(3,("Known pipe %s opening.\n",fname));
95   smb_ofun |= 0x10;             /* Add Create it not exists flag */
96
97   pnum = open_rpc_pipe_hnd(fname, conn, vuid);
98   if (pnum < 0) return(ERROR(ERRSRV,ERRnofids));
99
100   /* Prepare the reply */
101   set_message(outbuf,15,0,True);
102
103   /* Mark the opened file as an existing named pipe in message mode. */
104   SSVAL(outbuf,smb_vwv9,2);
105   SSVAL(outbuf,smb_vwv10,0xc700);
106
107   if (rmode == 2)
108   {
109     DEBUG(4,("Resetting open result to open from create.\n"));
110     rmode = 1;
111   }
112
113   SSVAL(outbuf,smb_vwv2, pnum + PIPE_HANDLE_OFFSET); /* mark file
114                                                         handle up into
115                                                         high range */
116   SSVAL(outbuf,smb_vwv3,fmode);
117   put_dos_date3(outbuf,smb_vwv4,mtime);
118   SIVAL(outbuf,smb_vwv6,size);
119   SSVAL(outbuf,smb_vwv8,rmode);
120   SSVAL(outbuf,smb_vwv11,0);
121
122   return chain_reply(inbuf,outbuf,length,bufsize);
123 }
124
125
126 /****************************************************************************
127   reply to a read and X
128
129   This code is basically stolen from reply_read_and_X with some
130   wrinkles to handle pipes.
131 ****************************************************************************/
132 int reply_pipe_read_and_X(char *inbuf,char *outbuf,int length,int bufsize)
133 {
134   int pnum = get_rpc_pipe_num(inbuf,smb_vwv2);
135   uint32 smb_offs = IVAL(inbuf,smb_vwv3);
136   int smb_maxcnt = SVAL(inbuf,smb_vwv5);
137   int smb_mincnt = SVAL(inbuf,smb_vwv6);
138   int nread = -1;
139   char *data;
140   BOOL ok = False;
141
142   set_message(outbuf,12,0,True);
143   data = smb_buf(outbuf);
144
145   nread = read_pipe(pnum, data, smb_offs, smb_maxcnt);
146
147   ok = True;
148   
149   if (nread < 0)
150     return(UNIXERROR(ERRDOS,ERRnoaccess));
151   
152   SSVAL(outbuf,smb_vwv5,nread);
153   SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
154   SSVAL(smb_buf(outbuf),-2,nread);
155   
156   DEBUG(3,("readX pnum=%04x min=%d max=%d nread=%d\n",
157             pnum, smb_mincnt, smb_maxcnt, nread));
158
159   set_chain_pnum(pnum);
160
161   return chain_reply(inbuf,outbuf,length,bufsize);
162 }
163 /****************************************************************************
164   reply to a close
165 ****************************************************************************/
166 int reply_pipe_close(connection_struct *conn, char *inbuf,char *outbuf)
167 {
168   int pnum = get_rpc_pipe_num(inbuf,smb_vwv0);
169   int outsize = set_message(outbuf,0,0,True);
170
171   DEBUG(5,("reply_pipe_close: pnum:%x\n", pnum));
172
173   if (!close_rpc_pipe_hnd(pnum, conn)) return(ERROR(ERRDOS,ERRbadfid));
174
175   return(outsize);
176 }
177