1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/********************************************************************
* $Author: lindner $
* $Revision: 3.22 $
* $Date: 1995/09/25 22:09:25 $
* $Source: /home/arcwelder/GopherSrc/CVS/gopher+/gopherd/special.c,v $
* $State: Exp $
*
* Paul Lindner, University of Minnesota CIS.
*
* Copyright 1991, 1992 by the Regents of the University of Minnesota
* see the file "Copyright" in the distribution for conditions of use.
*********************************************************************
* MODULE: special.c
* routines to deal with special types of files, compressed, scripts, etc.
*********************************************************************
* Revision History:
* $Log: special.c,v $
* Revision 3.22 1995/09/25 22:09:25 lindner
* Remove static buf..
*
* Revision 3.21 1995/09/25 05:02:40 lindner
* Convert to ANSI C
*
* Revision 3.20 1995/05/02 06:07:38 lindner
* Fix for OSF again and again
*
* Revision 3.19 1995/05/02 04:30:00 lindner
* Fix for weirdness with ask blocks on OSF
*
* Revision 3.18 1994/07/21 04:08:29 lindner
* Bulletproofing for shell scripts
*
* Revision 3.17 1994/04/25 20:49:11 lindner
* Fix for debug code
*
* Revision 3.16 1994/04/14 15:48:54 lindner
* Fix for files with single quotes
*
* Revision 3.15 1993/11/03 03:32:52 lindner
* Test shell scripts for exec bit
*
* Revision 3.14 1993/08/19 20:52:30 lindner
* Mitra comments
*
* Revision 3.13 1993/08/11 22:47:11 lindner
* Don't let the security stuff trap ask blocks
*
* Revision 3.12 1993/08/06 14:30:49 lindner
* Fixes for better security logging
*
* Revision 3.11 1993/08/04 22:14:54 lindner
* Mods to use Gpopen
*
* Revision 3.10 1993/08/04 22:12:51 lindner
* Mods to use Gpopen
*
* Revision 3.9 1993/07/27 05:27:59 lindner
* Mondo Debug overhaul from Mitra
*
* Revision 3.8 1993/07/25 02:56:51 lindner
* Fixed iscompressed() to return NULL
*
* Revision 3.7 1993/07/23 03:18:13 lindner
* Mods for using decoder:'s
*
* Revision 3.6 1993/07/07 19:35:36 lindner
* removed extra args to popen()
*
* Revision 3.5 1993/06/22 06:58:53 lindner
* Added a little debug code..
*
* Revision 3.4 1993/06/11 16:46:50 lindner
* Support for gzipped files
*
* Revision 3.3 1993/04/09 15:00:19 lindner
* Fixes for ask shell scripts, ensure that they're run in the current
* directory of the script.
*
* Revision 3.2 1993/03/24 20:24:23 lindner
* Addition for compressed file support rfopenz()
*
* Revision 3.1.1.1 1993/02/11 18:02:53 lindner
* Gopher+1.2beta release
*
* Revision 1.3 1993/02/09 22:15:36 lindner
* additions for askfile
*
* Revision 1.2 1993/01/30 23:57:44 lindner
* Additions for ASK block support.
*
* Revision 1.1 1992/12/10 23:13:27 lindner
* gopher 1.1 release
*
*
*********************************************************************/
#include "gopherd.h"
#include "special.h"
#include "ext.h"
#include "Debug.h"
static char *iscompressed(char *pathname);
static int isshellscript(char *s);
static int isexec(int fd);
/* Check to see if this file needs special treatment before heading
* back to the client... We will check for:
* Encoded files execute decoder...
* Shellscript if so, "do it"
* (add ask block params if exists..)
* Note: it would be somewhat non-portable to check of a binary
* (we'd need to check for so many different magic numbers; the
* shell script designation should be sufficient, since the script
* can call an executable anyway
* Recognized elsewhere:
* .snd needs special processing on client
* uuencoded needs special processing on client
* Other filetypes we could check for:
* GIF -> Bring up GIF previewer
* Postscript -> Bring up Postscript previewer
*/
static int ispipe;
FILE *
Specialfile(int sockfd, FILE *fp, char *pathname)
{
FILE *pp;
char buf[256];
char s[256], *cp;
long i;
char *decoder;
int c;
ispipe = 0;
/* Keep track of where we are */
i = ftell(fp);
rewind(fp);
/* Grab the first line or 254 bytes, and rewind */
if (fgets(s, 255, fp) == NULL)
return (FILE *)0;
fseek(fp, i, 0);
/* Compressed? */
if ((decoder = iscompressed(pathname)) != NULL) {
dequote1(pathname);
if (dochroot)
sprintf(buf, "%s '%s'", decoder, pathname);
else
sprintf(buf, "%s '%s/%s'", decoder, Data_Dir, pathname);
Debug("Executing decoder %s\n",buf);
pp = popen(buf, "r");
if (!pp)
return (FILE *)0;
ispipe = 1;
/* dgg - wait for some data ! else DEC OSF/1 loses data
sporadically here this might apply to other systems
missing ASK data & other special file failures */
for (i=0; i<1000; i++)
if ((c = fgetc(pp)) > 0) {
ungetc(c,pp);
break;
}
return pp;
}
/* Script? */
if (isshellscript(s) && isexec(fileno(fp))) {
dequote1(pathname);
s[strlen(s)-1] = '\0';
if (dochroot)
sprintf(buf, "\"%s\" %s", pathname, (EXECargs == NULL) ? "" : EXECargs);
else
sprintf(buf, "\"%s/%s\" %s", Data_Dir, pathname, (EXECargs == NULL) ? "" : EXECargs);
if (ASKfile != NULL) { /* Ick this is a global*/
strcat(buf, " < ");
strcat(buf, ASKfile);
}
else
strcat(buf, " < /dev/null ");
/*
* Okay, let's change our working directory for the benefit of
* shell script writers everywhere :-)
*/
strcpy(s, pathname);
cp = strrchr(s, '/');
if (cp != NULL && cp > s) {
*cp = '\0';
;
} else
strcpy(s, "/");
rchdir(s);
Debug("Executing %s\n", buf);
if (EXECargs) {
if (! (pp = Gpopen(sockfd, buf, "r")))
return (FILE *)0;
}
else {
if (! (pp = popen(buf, "r")))
return (FILE *)0;
}
ispipe = 1;
Debugmsg("Zcat/popen is okay\n");
return pp;
}
return (FILE *)0;
}
static char *
iscompressed(char *pathname)
{
static Extobj *ext = NULL;
if (pathname == NULL || strlen(pathname)==0)
return(NULL);
if (ext == NULL)
ext = EXnew();
if (EXAcasedSearch(Config->Extensions, ext, pathname, EXT_DECODER))
return(EXgetDecoder(ext));
return(NULL);
}
/*
* Is this a shell script?
*/
static int
isshellscript(char *s)
{
if (! strncmp(s, "#!", 2))
return 1;
else
return 0;
}
/*
* Are the exec bits set?
*/
static int
isexec(int fd)
{
STATSTR sb;
if (fstat(fd, &sb) == -1)
return 0;
if (sb.st_mode & S_IXUSR)
return 1;
else
return 0;
}
int
Specialclose(FILE *fp)
{
if (ASKfile != NULL) /* Ick this is a global*/
unlink(ASKfile);
if (ispipe)
return(pclose(fp));
else
return(fclose(fp));
}
|