Subversion Repositories wimsdev

Rev

Rev 11124 | Rev 11133 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18.  /* This is a small program used simply to fetch web pages.
  19.   * No fancy functionalities such as link redirection or site sucking is
  20.   * present.
  21.   * Page fetched can only be sent to stdout. */
  22.  
  23. #include <netdb.h>
  24. #include <sys/socket.h>
  25. #include <netinet/in.h>
  26. #include <assert.h>
  27.  
  28. #include "../includes.h"
  29. #include "../wimsdef.h"
  30.  
  31. char *cheater1="User-Agent: WIMS-webget";
  32. char *cheater2="Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*\r\n\
  33. Accept-Encoding: gzip\r\n\
  34. Accept-Language: en, fr, it, de, es\r\n\
  35. Accept-Charset: iso-8859-1,*,utf-8";
  36. char pbuf[4096], tbuf[4096];
  37. char tfname[1024];
  38. char *tmpdir="/tmp";
  39. int soc, port, https;
  40. int charcnt;
  41. FILE *outf;
  42.  
  43. void errorquit(char *msg)
  44. {
  45.     fprintf(stderr,"%s: %s\n",msg,strerror(errno)); exit(1);
  46. }
  47.  
  48. /* Points to the end of the word */
  49. char *find_word_end(char *p)
  50. {
  51.     int i;
  52.     for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
  53.     return p;
  54. }
  55.  
  56. /* Strips leading spaces */
  57. char *find_word_start(char *p)
  58. {
  59.     int i;
  60.     for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++);
  61.     return p;
  62. }
  63.  
  64. /* Secured execution */
  65. void secure(char *host)
  66. {
  67.     char *p1, *p2, *p3, buf[MAX_LINELEN+1];
  68.     long int l;
  69.     FILE *f;
  70.  
  71.     p1=getenv("w_module"); if(p1==NULL || *p1==0) return;
  72.     p1=getenv("untrust"); if(p1==NULL || *p1==0) return;
  73.     f=fopen("webget.sites","r"); if(f==NULL) return;
  74.     l=fread(buf,1,MAX_LINELEN,f); fclose(f);
  75.     if(l<=0 || l>MAX_LINELEN) return;
  76.     buf[l]=0;
  77.     for(p1=find_word_start(buf);*p1;p1=find_word_start(p2)) {
  78.       p2=find_word_end(p1); if(*p2) *p2++=0;
  79.       p3=strstr(host,p1); if(p3==NULL) continue;
  80.       if((p3==host || *(p3-1)=='.') && *(p3+strlen(p1))==0) return;
  81.     }
  82.     exit(1);  /* unauthorized sites refused. */
  83. }
  84.  
  85. /* open a TCP/IP socket with host/port
  86.  * returns the file descriptor for the socket */
  87. int net_connect(char *host)
  88. {
  89.     struct hostent *hp;
  90.     struct sockaddr_in sin;
  91.     int soc;
  92.  
  93.     secure(host);
  94.     if(!(hp = gethostbyname(host))) errorquit("unknown host.");
  95.     if((soc = socket(hp->h_addrtype,SOCK_STREAM,0))<0)
  96.       errorquit("socket() error");
  97.     memmove(&sin.sin_addr,hp->h_addr,hp->h_length);
  98.     sin.sin_port=htons(port);
  99.     sin.sin_family = hp->h_addrtype;
  100.     if(connect(soc,(struct sockaddr *)&sin,sizeof(sin))<0) {
  101.       close(soc); errorquit("connect() error");
  102.     }
  103.     return soc;
  104. }
  105.  
  106. int gethttps(char *host)
  107. {
  108.     char buf[65536];
  109.     char *tp;
  110.  
  111.     tp=getenv("tmp_dir"); if(tp!=NULL && *tp!=0) tmpdir=tp;
  112.     snprintf(tfname,sizeof(tfname),"%s/https.tmp",tmpdir);
  113.     snprintf(buf,sizeof(buf),"\
  114. mkdir -p %s\n\
  115. openssl s_client -connect %s:%d -quiet 2>/dev/null >%s <<@\n\
  116. %s\n\
  117. @\n", tmpdir,host,port,tfname,tbuf);
  118.     if (system(buf))
  119.       errorquit("system() error");
  120.     return open(tfname,O_RDONLY);
  121. }
  122.  
  123. int main(int argc, char *argv[])
  124. {
  125.     char *parm, *pt, *p1, *p2, *p3, *p4, *dp, *pre;
  126.     char nbuf[4096], *pp1, *pp2;
  127.     char c;
  128.  
  129.     parm=getenv("wims_exec_parm");
  130.     if(parm==NULL || *parm==0) errorquit("no_parameter");
  131.     snprintf(pbuf,sizeof(pbuf),"%s",parm);
  132.     p1=find_word_start(pbuf); p2=find_word_end(p1);
  133.     if(*p2!=0) *p2++=0;
  134.     https=0;
  135.     outf=stdout; pp1=getenv("w_webget_output");
  136.     pp2=getenv("session_dir");
  137.     if(pp1!=NULL && strstr(pp1,"..")==NULL && isalnum(*pp1) && pp2!=NULL) {
  138.       snprintf(nbuf,sizeof(nbuf),"%s/%s",pp2,pp1);
  139.       outf=fopen(nbuf,"w"); if(outf==NULL) outf=stdout;
  140.     }
  141.     dp=getenv("w_webget_option");
  142.     if(dp!=NULL && strstr(dp,"direct")!=NULL) { /* direct get */
  143.       p1=getenv("w_webget_host");
  144.       p2=getenv("w_webget_port");
  145.       if(p1==NULL || p2==NULL) errorquit("incomplete_request");
  146.       port=atoi(p2);
  147.       soc=net_connect(p1); if(soc==-1) return 1;
  148.       c=' '; for(p3=parm; *p3; p3++) {
  149.         if(*p3=='\n' && c!='\r') assert(write(soc,"\r",1)==1);
  150.         assert(write(soc,p3,1)==1);
  151.         c=*p3;
  152.       }
  153.       assert(write(soc,"\r\n\r\n",4)==4);
  154.       pt=getenv("w_module");
  155.       if(pt==NULL || *pt==0 || strncmp(pt,"adm/",4)==0 ) {  /* File to post? */
  156.           pt=getenv("w_webget_post"); if(pt!=NULL && *pt!=0) {
  157.             FILE *f;
  158.             char buf[4096];
  159.             size_t l;
  160.             f=fopen(pt,"r"); if(f!=NULL) {
  161.                 do {
  162.                   l=fread(buf,1,sizeof(buf),f);
  163.                   if(l>0 && l<=sizeof(buf)) assert(write(soc,buf,l)==l);
  164.                 } while(l==sizeof(buf));
  165.                 fclose(f);
  166.             }
  167.           }
  168.       }
  169.       if(strstr(dp,"normalread")!=NULL) goto read;
  170.       charcnt=0;
  171.       while(read(soc,pbuf,1)>0 && charcnt<10240) {
  172.           fputc(pbuf[0],outf); charcnt++;
  173.       }
  174.       close(soc);
  175.       return 0;
  176.     }
  177.     if(strncasecmp(p1,"http://",strlen("http://"))==0) p1+=strlen("http://");
  178.     else if(strncasecmp(p1,"https://",strlen("https://"))==0) {
  179.       https=1; p1+=strlen("https://");
  180.     }
  181.     p3=strchr(p1,'/'); if(p3==NULL) p3="";
  182.     else {*p3++=0; while(*p3=='/') p3++;}
  183.     if(strncasecmp(p3,"http://",strlen("http://"))==0 ||
  184.        strncasecmp(p3,"https://",strlen("https://"))==0) pre="";
  185.     else pre="/";
  186.     snprintf(tbuf,sizeof(tbuf),"GET %s%s HTTP/1.0\r\n%s\r\n\
  187. Host: %s\r\n\
  188. %s\r\n\r\n",
  189.            pre,p3,cheater1,p1,cheater2);
  190.     p4=strchr(p1,':'); if(p4==NULL) {
  191.       if(https) port=443; else port=80;
  192.     }
  193.     else {*p4++=0; port=atoi(p4);}
  194.     if(https) {
  195.       soc=gethttps(p1); goto read;
  196.     }
  197.     soc=net_connect(p1);
  198.     assert(write(soc,tbuf,strlen(tbuf))==strlen(tbuf));
  199. /* header */
  200.     read: if(soc==-1) return 1;
  201.     c=-1;
  202.     while(read(soc,pbuf,1)>0) {
  203.       if(pbuf[0]=='\r') continue;
  204.       fputc(pbuf[0],stderr);
  205.       if((c=='\n') && (pbuf[0]=='\n')) break; else c=pbuf[0];
  206.     }
  207. /* body */
  208.     charcnt=0;
  209.     while(read(soc,pbuf,1)>0 && charcnt<MAX_WEBGETFLEN) {
  210.       fputc(pbuf[0],outf); charcnt++;
  211.     }
  212.     close(soc);
  213.     if(outf!=stdout) fclose(outf);
  214.     if(https) unlink(tfname);
  215.     return 0;
  216. }
  217.  
  218.