Subversion Repositories wimsdev

Rev

Rev 3840 | Rev 8177 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3840 Rev 7676
Line 40... Line 40...
40
void errorquit(char *msg)
40
void errorquit(char *msg)
41
{
41
{
42
    fprintf(stderr,"%s: %s\n",msg,strerror(errno)); exit(1);
42
    fprintf(stderr,"%s: %s\n",msg,strerror(errno)); exit(1);
43
}
43
}
44
 
44
 
45
        /* Points to the end of the word */
45
/* Points to the end of the word */
46
char *find_word_end(char *p)
46
char *find_word_end(char *p)
47
{
47
{
48
    int i;
48
    int i;
49
    for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
49
    for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
50
    return p;
50
    return p;
51
}
51
}
52
 
52
 
53
        /* Strips leading spaces */
53
/* Strips leading spaces */
54
char *find_word_start(char *p)
54
char *find_word_start(char *p)
55
{
55
{
56
    int i;
56
    int i;
57
    for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++);
57
    for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++);
58
    return p;
58
    return p;
59
}
59
}
60
 
60
 
61
        /* Secured execution */
61
/* Secured execution */
62
void secure(char *host)
62
void secure(char *host)
63
{
63
{
64
    char *p1, *p2, *p3, buf[MAX_LINELEN+1];
64
    char *p1, *p2, *p3, buf[MAX_LINELEN+1];
65
    long int l;
65
    long int l;
66
    FILE *f;
66
    FILE *f;
Line 70... Line 70...
70
    f=fopen("webget.sites","r"); if(f==NULL) return;
70
    f=fopen("webget.sites","r"); if(f==NULL) return;
71
    l=fread(buf,1,MAX_LINELEN,f); fclose(f);
71
    l=fread(buf,1,MAX_LINELEN,f); fclose(f);
72
    if(l<=0 || l>MAX_LINELEN) return;
72
    if(l<=0 || l>MAX_LINELEN) return;
73
    buf[l]=0;
73
    buf[l]=0;
74
    for(p1=find_word_start(buf);*p1;p1=find_word_start(p2)) {
74
    for(p1=find_word_start(buf);*p1;p1=find_word_start(p2)) {
75
        p2=find_word_end(p1); if(*p2) *p2++=0;
75
      p2=find_word_end(p1); if(*p2) *p2++=0;
76
        p3=strstr(host,p1); if(p3==NULL) continue;
76
      p3=strstr(host,p1); if(p3==NULL) continue;
77
        if((p3==host || *(p3-1)=='.') && *(p3+strlen(p1))==0) return;
77
      if((p3==host || *(p3-1)=='.') && *(p3+strlen(p1))==0) return;
78
    }
78
    }
79
    exit(1);  /* unauthorized sites refused. */
79
    exit(1);  /* unauthorized sites refused. */
80
}
80
}
81
 
81
 
82
        /* open a TCP/IP socket with host/port
82
/* open a TCP/IP socket with host/port
83
         * returns the file descriptor for the socket */
83
 * returns the file descriptor for the socket */
84
int net_connect(char *host)
84
int net_connect(char *host)
85
{
85
{
86
    struct hostent *hp;
86
    struct hostent *hp;
87
    struct sockaddr_in sin;
87
    struct sockaddr_in sin;
88
    int soc;
88
    int soc;
Line 93... Line 93...
93
      errorquit("socket() error");
93
      errorquit("socket() error");
94
    memmove(&sin.sin_addr,hp->h_addr,hp->h_length);
94
    memmove(&sin.sin_addr,hp->h_addr,hp->h_length);
95
    sin.sin_port=htons(port);
95
    sin.sin_port=htons(port);
96
    sin.sin_family = hp->h_addrtype;
96
    sin.sin_family = hp->h_addrtype;
97
    if(connect(soc,(struct sockaddr *)&sin,sizeof(sin))<0) {
97
    if(connect(soc,(struct sockaddr *)&sin,sizeof(sin))<0) {
98
        close(soc); errorquit("connect() error");
98
      close(soc); errorquit("connect() error");
99
    }
99
    }
100
    return soc;
100
    return soc;
101
}
101
}
102
 
102
 
103
int gethttps(char *host)
103
int gethttps(char *host)
104
{
104
{
105
    char buf[65536];
105
    char buf[65536];
106
    char *tp;
106
    char *tp;
107
   
107
 
108
    tp=getenv("tmp_dir"); if(tp!=NULL && *tp!=0) tmpdir=tp;    
108
    tp=getenv("tmp_dir"); if(tp!=NULL && *tp!=0) tmpdir=tp;
109
    snprintf(tfname,sizeof(tfname),"%s/https.tmp",tmpdir);
109
    snprintf(tfname,sizeof(tfname),"%s/https.tmp",tmpdir);
110
    snprintf(buf,sizeof(buf),"\
110
    snprintf(buf,sizeof(buf),"\
111
mkdir -p %s\n\
111
mkdir -p %s\n\
112
openssl s_client -connect %s:%d -quiet 2>/dev/null >%s <<@\n\
112
openssl s_client -connect %s:%d -quiet 2>/dev/null >%s <<@\n\
113
%s\n\
113
%s\n\
Line 120... Line 120...
120
int main(int argc, char *argv[])
120
int main(int argc, char *argv[])
121
{
121
{
122
    char *parm, *pt, *p1, *p2, *p3, *p4, *dp, *pre;
122
    char *parm, *pt, *p1, *p2, *p3, *p4, *dp, *pre;
123
    char nbuf[4096], *pp1, *pp2;
123
    char nbuf[4096], *pp1, *pp2;
124
    char c;
124
    char c;
125
   
125
 
126
    parm=getenv("wims_exec_parm");
126
    parm=getenv("wims_exec_parm");
127
    if(parm==NULL || *parm==0) errorquit("no_parameter");
127
    if(parm==NULL || *parm==0) errorquit("no_parameter");
128
    snprintf(pbuf,sizeof(pbuf),"%s",parm);
128
    snprintf(pbuf,sizeof(pbuf),"%s",parm);
129
    p1=find_word_start(pbuf); p2=find_word_end(p1);
129
    p1=find_word_start(pbuf); p2=find_word_end(p1);
130
    if(*p2!=0) *p2++=0; https=0;
130
    if(*p2!=0) *p2++=0; https=0;
131
    outf=stdout; pp1=getenv("w_webget_output");
131
    outf=stdout; pp1=getenv("w_webget_output");
132
    pp2=getenv("session_dir");
132
    pp2=getenv("session_dir");
133
    if(pp1!=NULL && strstr(pp1,"..")==NULL && isalnum(*pp1) && pp2!=NULL) {
133
    if(pp1!=NULL && strstr(pp1,"..")==NULL && isalnum(*pp1) && pp2!=NULL) {
134
        snprintf(nbuf,sizeof(nbuf),"%s/%s",pp2,pp1);
134
      snprintf(nbuf,sizeof(nbuf),"%s/%s",pp2,pp1);
135
        outf=fopen(nbuf,"w"); if(outf==NULL) outf=stdout;
135
      outf=fopen(nbuf,"w"); if(outf==NULL) outf=stdout;
136
    }
136
    }
137
    dp=getenv("w_webget_option");
137
    dp=getenv("w_webget_option");
138
    if(dp!=NULL && strstr(dp,"direct")!=NULL) { /* direct get */
138
    if(dp!=NULL && strstr(dp,"direct")!=NULL) { /* direct get */
139
        p1=getenv("w_webget_host");
139
      p1=getenv("w_webget_host");
140
        p2=getenv("w_webget_port");
140
      p2=getenv("w_webget_port");
141
        if(p1==NULL || p2==NULL) errorquit("incomplete_request");
141
      if(p1==NULL || p2==NULL) errorquit("incomplete_request");
142
        port=atoi(p2);
142
      port=atoi(p2);
143
        soc=net_connect(p1); if(soc==-1) return 1;
143
      soc=net_connect(p1); if(soc==-1) return 1;
144
        c=' '; for(p3=parm; *p3; p3++) {
144
      c=' '; for(p3=parm; *p3; p3++) {
145
            if(*p3=='\n' && c!='\r') (void)write(soc,"\r",1);
145
          if(*p3=='\n' && c!='\r') (void)write(soc,"\r",1);
146
            (void)write(soc,p3,1); c=*p3;
146
          (void)write(soc,p3,1); c=*p3;
147
        }
147
      }
148
        (void)write(soc,"\r\n\r\n",4);
148
      (void)write(soc,"\r\n\r\n",4);
149
        pt=getenv("w_module");
149
      pt=getenv("w_module");
150
        if(pt==NULL || *pt==0 || strncmp(pt,"adm/",4)==0 ) {  /* File to post? */
150
      if(pt==NULL || *pt==0 || strncmp(pt,"adm/",4)==0 ) {  /* File to post? */
151
            pt=getenv("w_webget_post"); if(pt!=NULL && *pt!=0) {
151
          pt=getenv("w_webget_post"); if(pt!=NULL && *pt!=0) {
152
                FILE *f;
152
            FILE *f;
153
                char buf[4096];
153
            char buf[4096];
154
                size_t l;
154
            size_t l;
155
                f=fopen(pt,"r"); if(f!=NULL) {
155
            f=fopen(pt,"r"); if(f!=NULL) {
156
                    do {
156
                do {
157
                        l=fread(buf,1,sizeof(buf),f);
157
                  l=fread(buf,1,sizeof(buf),f);
158
                        if(l>0 && l<=sizeof(buf)) (void)write(soc,buf,l);
158
                  if(l>0 && l<=sizeof(buf)) (void)write(soc,buf,l);
159
                    } while(l==sizeof(buf));
159
                } while(l==sizeof(buf));
160
                    fclose(f);
160
                fclose(f);
161
                }
161
            }
162
            }
162
          }
163
        }
163
      }
164
        if(strstr(dp,"normalread")!=NULL) goto read;
164
      if(strstr(dp,"normalread")!=NULL) goto read;
165
        charcnt=0;
165
      charcnt=0;
166
        while(read(soc,pbuf,1)>0 && charcnt<10240) {
166
      while(read(soc,pbuf,1)>0 && charcnt<10240) {
167
            fputc(pbuf[0],outf); charcnt++;
167
          fputc(pbuf[0],outf); charcnt++;
168
        }
168
      }
169
        close(soc);
169
      close(soc);
170
        return 0;
170
      return 0;
171
    }
171
    }
172
    if(strncasecmp(p1,"http://",strlen("http://"))==0) p1+=strlen("http://");
172
    if(strncasecmp(p1,"http://",strlen("http://"))==0) p1+=strlen("http://");
173
    else if(strncasecmp(p1,"https://",strlen("https://"))==0) {
173
    else if(strncasecmp(p1,"https://",strlen("https://"))==0) {
174
        https=1; p1+=strlen("https://");
174
      https=1; p1+=strlen("https://");
175
    }
175
    }
176
    p3=strchr(p1,'/'); if(p3==NULL) p3="";
176
    p3=strchr(p1,'/'); if(p3==NULL) p3="";
177
    else {*p3++=0; while(*p3=='/') p3++;}
177
    else {*p3++=0; while(*p3=='/') p3++;}
178
    if(strncasecmp(p3,"http://",strlen("http://"))==0 ||
178
    if(strncasecmp(p3,"http://",strlen("http://"))==0 ||
179
       strncasecmp(p3,"https://",strlen("https://"))==0) pre="";
179
       strncasecmp(p3,"https://",strlen("https://"))==0) pre="";
180
    else pre="/";
180
    else pre="/";
181
    snprintf(tbuf,sizeof(tbuf),"GET %s%s HTTP/1.0\r\n%s\r\n\
181
    snprintf(tbuf,sizeof(tbuf),"GET %s%s HTTP/1.0\r\n%s\r\n\
182
Host: %s\r\n\
182
Host: %s\r\n\
183
%s\r\n\r\n",
183
%s\r\n\r\n",
184
             pre,p3,cheater1,p1,cheater2);
184
           pre,p3,cheater1,p1,cheater2);
185
    p4=strchr(p1,':'); if(p4==NULL) {
185
    p4=strchr(p1,':'); if(p4==NULL) {
186
        if(https) port=443; else port=80;
186
      if(https) port=443; else port=80;
187
    }
187
    }
188
    else {*p4++=0; port=atoi(p4);}
188
    else {*p4++=0; port=atoi(p4);}
189
    if(https) {
189
    if(https) {
190
        soc=gethttps(p1); goto read;
190
      soc=gethttps(p1); goto read;
191
    }
191
    }
192
    soc=net_connect(p1);
192
    soc=net_connect(p1);
193
    (void)write(soc,tbuf,strlen(tbuf));
193
    (void)write(soc,tbuf,strlen(tbuf));
194
        /* header */
194
/* header */
195
    read: if(soc==-1) return 1;
195
    read: if(soc==-1) return 1;
196
    c=-1;
196
    c=-1;
197
    while(read(soc,pbuf,1)>0) {
197
    while(read(soc,pbuf,1)>0) {
198
        if(pbuf[0]=='\r') continue;
198
      if(pbuf[0]=='\r') continue;
199
        fputc(pbuf[0],stderr);
199
      fputc(pbuf[0],stderr);
200
        if((c=='\n') && (pbuf[0]=='\n')) break; else c=pbuf[0];
200
      if((c=='\n') && (pbuf[0]=='\n')) break; else c=pbuf[0];
201
    }
201
    }
202
        /* body */
202
/* body */
203
    charcnt=0;
203
    charcnt=0;
204
    while(read(soc,pbuf,1)>0 && charcnt<MAX_WEBGETFLEN) {
204
    while(read(soc,pbuf,1)>0 && charcnt<MAX_WEBGETFLEN) {
205
        fputc(pbuf[0],outf); charcnt++;
205
      fputc(pbuf[0],outf); charcnt++;
206
    }
206
    }
207
    close(soc);
207
    close(soc);
208
    if(outf!=stdout) fclose(outf);
208
    if(outf!=stdout) fclose(outf);
209
    if(https) unlink(tfname);
209
    if(https) unlink(tfname);
210
    return 0;
210
    return 0;