As per a Andrew's message, I went through and removed the timestring()
[samba.git] / source3 / 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 #define PNUM_OK(pnum,c) (OPEN_PNUM(pnum) && (c)==Pipes[pnum].cnum)
48
49 /* this macro should always be used to extract an pnum (smb_fid) from
50    a packet to ensure chaining works correctly */
51 #define GETPNUM(buf,where) (chain_pnum!= -1?chain_pnum:SVAL(buf,where))
52
53 extern struct pipe_id_info pipe_names[];
54
55 /****************************************************************************
56   reply to an open and X on a named pipe
57
58   This code is basically stolen from reply_open_and_X with some
59   wrinkles to handle pipes.
60 ****************************************************************************/
61 int reply_open_pipe_and_X(char *inbuf,char *outbuf,int length,int bufsize)
62 {
63   pstring fname;
64   uint16 cnum = SVAL(inbuf, smb_tid);
65   uint16 vuid = SVAL(inbuf, smb_uid);
66   int pnum = -1;
67   int smb_ofun = SVAL(inbuf,smb_vwv8);
68   int size=0,fmode=0,mtime=0,rmode=0;
69   int i;
70
71   /* XXXX we need to handle passed times, sattr and flags */
72   pstrcpy(fname,smb_buf(inbuf));
73
74   /* If the name doesn't start \PIPE\ then this is directed */
75   /* at a mailslot or something we really, really don't understand, */
76   /* not just something we really don't understand. */
77   if ( strncmp(fname,PIPE,PIPELEN) != 0 )
78     return(ERROR(ERRSRV,ERRaccess));
79
80   DEBUG(4,("Opening pipe %s.\n", fname));
81
82   /* See if it is one we want to handle. */
83   for( i = 0; pipe_names[i].client_pipe ; i++ )
84     if( strequal(fname,pipe_names[i].client_pipe) )
85       break;
86
87   if ( pipe_names[i].client_pipe == NULL )
88     return(ERROR(ERRSRV,ERRaccess));
89
90   /* Strip \PIPE\ off the name. */
91   pstrcpy(fname,smb_buf(inbuf) + PIPELEN);
92
93   /* Known pipes arrive with DIR attribs. Remove it so a regular file */
94   /* can be opened and add it in after the open. */
95   DEBUG(3,("Known pipe %s opening.\n",fname));
96   smb_ofun |= 0x10;             /* Add Create it not exists flag */
97
98   pnum = open_rpc_pipe_hnd(fname, cnum, vuid);
99   if (pnum < 0) return(ERROR(ERRSRV,ERRnofids));
100
101   /* Prepare the reply */
102   set_message(outbuf,15,0,True);
103
104   /* Mark the opened file as an existing named pipe in message mode. */
105   SSVAL(outbuf,smb_vwv9,2);
106   SSVAL(outbuf,smb_vwv10,0xc700);
107
108   if (rmode == 2)
109   {
110     DEBUG(4,("Resetting open result to open from create.\n"));
111     rmode = 1;
112   }
113
114   SSVAL(outbuf,smb_vwv2, pnum + 0x800); /* mark file handle up into high range */
115   SSVAL(outbuf,smb_vwv3,fmode);
116   put_dos_date3(outbuf,smb_vwv4,mtime);
117   SIVAL(outbuf,smb_vwv6,size);
118   SSVAL(outbuf,smb_vwv8,rmode);
119   SSVAL(outbuf,smb_vwv11,0);
120
121   return chain_reply(inbuf,outbuf,length,bufsize);
122 }
123
124
125 /****************************************************************************
126   reply to a read and X
127
128   This code is basically stolen from reply_read_and_X with some
129   wrinkles to handle pipes.
130 ****************************************************************************/
131 int reply_pipe_read_and_X(char *inbuf,char *outbuf,int length,int bufsize)
132 {
133   int pnum = get_rpc_pipe_num(inbuf,smb_vwv2);
134   uint32 smb_offs = IVAL(inbuf,smb_vwv3);
135   int smb_maxcnt = SVAL(inbuf,smb_vwv5);
136   int smb_mincnt = SVAL(inbuf,smb_vwv6);
137   int cnum;
138   int nread = -1;
139   char *data;
140   BOOL ok = False;
141
142   cnum = SVAL(inbuf,smb_tid);
143
144 /*
145   CHECK_FNUM(fnum,cnum);
146   CHECK_READ(fnum);
147   CHECK_ERROR(fnum);
148 */
149
150   set_message(outbuf,12,0,True);
151   data = smb_buf(outbuf);
152
153   nread = read_pipe(pnum, data, smb_offs, smb_maxcnt);
154
155   ok = True;
156   
157   if (nread < 0)
158     return(UNIXERROR(ERRDOS,ERRnoaccess));
159   
160   SSVAL(outbuf,smb_vwv5,nread);
161   SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
162   SSVAL(smb_buf(outbuf),-2,nread);
163   
164   DEBUG( 3, ( "readX pnum=%04x cnum=%d min=%d max=%d nread=%d\n",
165               pnum, cnum, smb_mincnt, smb_maxcnt, nread ) );
166
167   set_chain_pnum(pnum);
168
169   return chain_reply(inbuf,outbuf,length,bufsize);
170 }
171 /****************************************************************************
172   reply to a close
173 ****************************************************************************/
174 int reply_pipe_close(char *inbuf,char *outbuf)
175 {
176   int pnum = get_rpc_pipe_num(inbuf,smb_vwv0);
177   int cnum = SVAL(inbuf,smb_tid);
178   int outsize = set_message(outbuf,0,0,True);
179
180   DEBUG(5,("reply_pipe_close: pnum:%x cnum:%x\n", pnum, cnum));
181
182   if (!close_rpc_pipe_hnd(pnum, cnum)) return(ERROR(ERRDOS,ERRbadfid));
183
184   return(outsize);
185 }
186