Subversion Repositories wimsdev

Rev

Rev 5505 | Rev 8067 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 reyssat 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
 */
5505 bpr 17
      /* line input / output / translation routines
18
       * and error routines */
10 reyssat 19
 
20
int is_class_module=0;
21
 
22
int trusted_module(void);
23
void phtml_put_base(char *fname,int cache);
24
void module_error(char msg[]);
25
void user_error(char msg[]);
26
void accessfile(char *content, char *type, char *s,...);
27
int remove_tree(char *p);
28
void output(char *s,...);
29
char *_getvar(char *vname);
30
int checkhost(char *hlist);
31
 
32
char *lastdata;
33
char lastdatafile[MAX_FNAME+1];
34
char *datacache[DATAFIELD_LIMIT];
35
int  nextdatacache;
36
struct stat ftst;
37
char lastftest[MAX_FNAME+1];
38
int lastftype;
39
char outbuf[8192];
40
char *outptr;
41
 
42
  /* These modules can execute private programs.
43
   * adm/ modules are always trusted, so need no definition here. */
44
char *trusted_modules="";
5505 bpr 45
            /* bit 0: module is not trusted.
46
             * bit 1: file in wimshome.
47
             * bit 2: readdef or file in writable directory.
48
             */
10 reyssat 49
int untrust=0;  /* non-zero if user detrusts the module. */
50
 
51
int error_status=0;
52
char pidbuf[32];
53
#include <utime.h>
54
 
55
void delete_pid(void);
56
 
5505 bpr 57
      /* Internal use only */
10 reyssat 58
void _debug(char *s,...)
59
{
60
    va_list vp;
61
    char buf[MAX_LINELEN+1];
62
 
63
    va_start(vp,s);
64
    vsnprintf(buf,sizeof(buf),s,vp);
65
    va_end(vp);
66
    setvar("debug",buf); module_error("debug");
67
    exit(1);
68
}
69
 
70
 
5505 bpr 71
      /* HTTP response header for non-processed CGI interface */
10 reyssat 72
void nph_header(int code)
73
{
74
    char *cstr;
75
    switch(code) {
5505 bpr 76
      case 200: cstr="OK"; break;
77
      case 301: cstr="Moved Permanently"; break;
78
      case 302: cstr="Moved Temporarily"; break;
79
      case 420: cstr="WIMS Nested Error"; break;
80
      case 450: cstr="WIMS User Error"; break;
81
      case 500: cstr="WIMS Internal Error"; break;
7646 bpr 82
      case 550: cstr="WIMS Module Error"; break;
5505 bpr 83
      default: cstr="ERROR"; break;
10 reyssat 84
    }
85
    if(httpd_type!=httpd_wims) printf("Status: %d %s\r\n",code,cstr);
86
    else {
5505 bpr 87
      char *p, tbuf[256];
88
      mystrncpy(tbuf,ctime(&nowtime),sizeof(tbuf));
89
      for(p=tbuf+strlen(tbuf);p>=tbuf && (*(p-1)=='\n' || *(p-1)=='\r'); p--);
90
      *p=0;
91
      printf("HTTP/1.0 %d %s\r\n\
10 reyssat 92
Date: %s\r\n\
93
Connection: close\r\n\
94
",code,cstr,tbuf);
95
    }
96
}
97
 
98
void flushoutput(void)
99
{
100
    int l2;
101
    if(outptr<=outbuf) return;
102
    l2=outptr-outbuf;
3843 kbelabas 103
    if(lastout_file!=-1) (void)write(lastout_file,outbuf,l2);
10 reyssat 104
    else fwrite(outbuf,l2,1,stdout);
105
    outptr=outbuf;
106
}
107
 
108
void internal_warn(char msg[])
109
{
110
    char buf[1024];
111
    fprintf(stderr,"wims: %s\n%s\n",msg,strerror(errno));
112
    snprintf(buf,sizeof(buf),"%s: %s\n",nowstr,msg);
113
    accessfile(buf,"a","%s/internal_error.log",log_dir);
114
}
115
 
116
  /* Internal error: panic and forget about requester. */
117
void internal_error(char msg[])
118
{
119
    if(error_status<2) {
5505 bpr 120
      nph_header(500);
121
      printf("Cache-Control: no-cache\nPragma: no-cache\r\n\
10 reyssat 122
Content-type: text/plain\r\n\r\n\r\n\
123
WIMS panick! %s\n%s\n",msg,strerror(errno));
5505 bpr 124
      error_status=2; internal_warn(msg);
10 reyssat 125
    }
126
    delete_pid(); exit(1);
127
}
128
 
129
void tex_nospace(char *p)
130
{
131
    char *p1, *p2;
132
    char buf[MAX_LINELEN+1];
133
    for(p1=buf,p2=p;*p2 && p1-buf<MAX_LINELEN-4;p2++) {
5505 bpr 134
      if(!isspace(*p2)) {*p1++=*p2;continue;}
135
      while(isspace(*p2)) p2++;
7646 bpr 136
      if(*(p1-1)=='\\' ||
5505 bpr 137
         (p1>buf && myisalnum(*(p1-1)) && myisalnum(*p2)))
138
        *p1++=' ';
139
      *p1++=*p2;
10 reyssat 140
    }
141
    *p1++=0; memmove(p,buf,p1-buf);
142
}
143
 
144
void nametoolong(char *p)
145
{
146
    char buf[MAX_FNAME+17];
147
    snprintf(buf,sizeof(buf),"%s...",p);
148
    force_setvar("wims_error_data",buf);
149
    module_error("file_name_too_long");
150
}
151
 
152
enum{is_file, is_dir, is_exec, is_fifo, is_socket, is_unknown};
153
off_t ftest_size;
154
 
5505 bpr 155
      /* A simple front-end of stat(). */
10 reyssat 156
int ftest(char *fname)
157
{
158
    if(strcmp(fname,lastftest)==0) return lastftype;
159
/* if(fname[0]=='/' || fname[0]=='.') fprintf(stderr,"ftest: %s\n",fname); */
160
    mystrncpy(lastftest,fname,sizeof(lastftest));
161
    if(stat(fname,&ftst)) return lastftype=-1;
162
    ftest_size=ftst.st_size;
163
    if(S_ISREG(ftst.st_mode)) {
5505 bpr 164
      if((ftst.st_mode&(S_IXUSR|S_IXGRP|S_IXOTH))!=0) return lastftype=is_exec;
165
      else return lastftype=is_file;
10 reyssat 166
    }
167
    if(S_ISDIR(ftst.st_mode)) return lastftype=is_dir;
168
    if(S_ISFIFO(ftst.st_mode)) return lastftype=is_fifo;
169
    if(S_ISSOCK(ftst.st_mode)) return lastftype=is_socket;
170
    return lastftype=is_unknown;
171
}
172
 
173
char fnbuf[MAX_FNAME+1];
174
 
5505 bpr 175
      /* make a filename and check length */
10 reyssat 176
char *mkfname(char buf[], char *s,...)
177
{
178
    va_list vp;
179
    char *p;
180
 
181
    if(buf==NULL) p=fnbuf; else p=buf;
182
    va_start(vp,s);
183
    vsnprintf(p,MAX_FNAME,s,vp);
184
    va_end(vp);
185
    if(strlen(p)>=MAX_FNAME-1) nametoolong(p);
186
    return p;
187
}
188
 
189
void sysmask_trigger(char *s)
190
{
191
    char buf[MAX_FNAME+1];
192
    struct stat st;
193
    mkfname(buf,"%s/%s",sysmask_trigger_dir,s);
194
    stat(buf,&st);
195
}
196
 
5505 bpr 197
      /* read-in a file into buffer. Use open() and read().
198
       * Return buffer address which will be malloc'ed if buf=NULL. */
10 reyssat 199
char *readfile(char *fname, char buf[], long int buflen)
200
{
201
    int fd, t, st;
202
    long int l, lc;
203
    char *bf;
204
    t=0; if(buf) buf[0]=0;
205
    st=ftest(fname); if(st!=is_file) {
5505 bpr 206
      if(st==is_exec) { /* refuse to open executable file */
207
          setvar("executable",fname); module_error("executable");
208
      }
209
      return NULL;
10 reyssat 210
    }
211
    l=ftst.st_size; if(l<0) return NULL;
212
    if(l>=buflen) {
5505 bpr 213
      if(buflen<MAX_LINELEN) l=buflen-1;
214
      else {
215
          if(strncmp(fname,"modules/",strlen("modules/"))==0) {
216
            setvar(error_data_string,fname); module_error("file_too_long");
217
          }
218
          else user_error("cmd_output_too_long");
219
      }
10 reyssat 220
    }
221
    fd=open(fname,O_RDONLY); if(fd==-1) return NULL;
222
    if(buf==NULL) bf=xmalloc(l+8); else {bf=buf;if(l==0) {t=1; l=buflen-1;}}
223
    lc=read(fd,bf,l); close(fd);
7646 bpr 224
    if(lc<0 || lc>l || (lc!=l && t==0))
5505 bpr 225
      {if(buf==NULL) free(bf); else buf[0]=0; return NULL;}
10 reyssat 226
    bf[lc]=0; _tolinux(bf); return bf;
227
}
228
 
7646 bpr 229
      /* Get a line in a stored working file.
5505 bpr 230
       * Buffer length is always MAX_LINELEN. */
10 reyssat 231
int wgetline(char buf[], size_t buflen, WORKING_FILE *f)
232
{
233
    int i,j; unsigned int n;
234
    i=f->linepointer; buf[0]=0;
235
    if(i>=f->linecnt || f->textbuf==NULL) return EOF;
236
    n=f->lines[i].llen;
237
    if(n>=buflen) n=buflen-1;
238
    if(n>0) memmove(buf,f->lines[i].address,n); buf[n]=0;
239
    for(j=i+1;j<f->linecnt && f->lines[j].isstart==0;j++);
240
    f->l=i; f->linepointer=j;
241
    if(j>=f->linecnt && n==0) return EOF; else return n;
242
}
243
 
244
int get_cached_file(char *name)
245
{
246
    int i,l,flag;
247
    l=strlen(name); if(l>=127) return -1;
248
    if(strncmp(module_prefix,module_dir,strlen(module_dir))!=0) flag=8; else flag=0;
249
    for(i=0;i<mcachecnt;i++) {
5505 bpr 250
      if(flag==mcache[i].nocache && strcmp(name,mcache[i].name)==0) {
251
          memmove(&m_file,mcache+i,sizeof(WORKING_FILE));
252
          m_file.nocache|=4;
253
          return i;
254
      }
10 reyssat 255
    }
256
    return -1;
257
}
258
 
5505 bpr 259
      /* Open a work file. Returns 0 if OK. */
10 reyssat 260
int open_working_file(WORKING_FILE *f, char *fname)
261
{
262
    char *p, *q;
263
    void *vp;
264
    int i,j,k,laststart,lc[LINE_LIMIT];
265
 
266
    f->linecnt=f->linepointer=0;
267
    f->for_idx=f->nocache=0;
268
    f->l=-1; f->lines=NULL;
269
    f->textbuf=readfile(fname,NULL,WORKFILE_LIMIT);
270
    if(f->textbuf==NULL) return -1;
271
    p=f->textbuf; if(*p) {
5505 bpr 272
      for(i=0,p--; i<LINE_LIMIT-1 && p!=NULL;p=strchr(p,'\n')) {
273
          if(i>0) *p=0;
274
          lc[i++]=(++p-f->textbuf);
275
      }
10 reyssat 276
    }
277
    else i=0;
278
    if(i>=LINE_LIMIT-1) module_error("file_too_long");
279
    lc[i]=lc[i-1]+strlen(f->textbuf+lc[i-1])+1;
280
    f->textbuf[lc[i]]=0;
281
    f->linecnt=i; laststart=0;
282
    f->for_stack=xmalloc(MAX_FOR_LEVEL*sizeof(FOR_STACK)+(i+1)*sizeof(LINE_STRUCT));
283
    vp=f->for_stack+MAX_FOR_LEVEL; f->lines=vp;
284
    for(j=0;j<i;j++) {
5505 bpr 285
      f->lines[j].address=p=(f->textbuf)+lc[j];
286
      f->lines[j].llen=lc[j+1]-lc[j]-1;
287
      (f->lines[j]).execcode=(f->lines[j]).varcode=-1;
288
      if(j==0) goto normal;
289
      q=f->lines[laststart].address+f->lines[laststart].llen-1;
290
      if(q>=f->textbuf && *q=='\\') {
291
          f->lines[laststart].llen+=f->lines[j].llen; *q='\n';
292
          f->lines[j].isstart=f->lines[j].llen=0;
293
          memmove(q+1,p,lc[j+1]-lc[j]);
294
      }
295
      else {
296
          normal: for(q=p;myislspace(*q);q++);
297
          f->lines[j].isstart=1; laststart=j; k=0;
298
          switch(*q) {
299
                  /* isstart: bit 1 = start.
300
                   * bit 2 = exec
301
                   * bit 3 (4) = label
302
                   * bit 4 (8) = hard comment (!!)
303
                   * bit 5 (16) = soft comment (#) */
304
            case exec_prefix_char: {
305
                if(myisalpha(q[1])) f->lines[j].isstart=3;
306
                else f->lines[j].isstart=9;
307
                k=1; break;
308
            }
309
            case label_prefix_char: {f->lines[j].isstart=5; k=1; break;}
310
            case comment_prefix_char: {f->lines[j].isstart=17; break;}
311
            default: {f->lines[j].isstart=1; break;}
312
          }
313
          if(k && q>p) {f->lines[j].address=q;f->lines[j].llen-=q-p;}
314
      }
10 reyssat 315
    }
316
    f->lines[i].isstart=1; f->lines[i].llen=0;
317
    f->lines[i].address=(f->textbuf)+lc[i];
318
    mfilecnt++; return 0;
319
}
320
 
5505 bpr 321
      /* close an earlier opened working file */
10 reyssat 322
void close_working_file(WORKING_FILE *f, int cache)
323
{
324
    f->linepointer=f->l=0;
325
    if(cache && untrust==0 && mcachecnt<MAX_MCACHE && (f->nocache&7)==0) {
5505 bpr 326
      memmove(mcache+mcachecnt,f,sizeof(WORKING_FILE));
327
      mcachecnt++;
10 reyssat 328
    }
329
    else if((f->nocache&4)==0) {
5505 bpr 330
      if(f->for_stack!=NULL) free(f->for_stack);
331
      if(f->textbuf!=NULL) free(f->textbuf);
10 reyssat 332
    }
333
    f->for_stack=NULL; f->textbuf=NULL; f->linecnt=0;
334
}
335
 
336
void free_mcache(void)
337
{
338
    int i;
339
    untrust=0;
340
    for(i=mcachecnt-1;i>=0;i--) close_working_file(mcache+i,0);
341
    mcachecnt=0;
342
}
343
 
344
void cleantmpdir(void);
345
void user_error_log(char msg[]);
346
 
347
void nested_error(char msg[])
348
{
349
    fprintf(stderr,"\nNested error! %s\n",msg);
350
    nph_header(420);
351
    printf("\r\n\r\nWIMS error processing aborted on nested error.\r\n\r\n%s\r\n",msg);
352
    delete_pid(); exit(1);
353
}
354
 
5505 bpr 355
      /* Send an error message to requester and exit.
356
       * This is for user errors, language-sensitive. */
10 reyssat 357
void user_error(char msg[])
358
{
359
    char erfname[MAX_FNAME+1];
360
 
361
    if(error_status) nested_error(msg);
362
    error_status=1;
363
    mkfname(erfname,"%s.%s",user_error_msg_file,lang);
364
    if(ftest(erfname)!=is_file) internal_error("user_error(): error message file not found.\n\
365
Bad installation.");
366
    force_setvar("wims_user_error",msg);
7646 bpr 367
    if(strcmp(msg,"threshold")!=0) user_error_log(msg);
368
    memmove(module_prefix,".",2);
10 reyssat 369
    if(lastout_file!=-1 && outputing) {
5505 bpr 370
      flushoutput(); close(lastout_file); lastout_file=-1;
10 reyssat 371
    }
372
    nph_header(450);
7646 bpr 373
    phtml_put_base(erfname,0);
374
    if(strcmp(msg,"double_click")!=0) delete_pid();
10 reyssat 375
    else {
5505 bpr 376
      cleantmpdir(); flushlog();
10 reyssat 377
    }
378
    flushoutput(); exit(0);
379
}
380
 
381
void module_error_log(char msg[]);
382
 
5505 bpr 383
      /* Messages for module errors. English only. */
384
      /* This is really rudimentary for the time being. */
10 reyssat 385
void module_error(char msg[])
386
{
387
    int send=0;
388
    char *p;
389
    WORKING_FILE mf;
390
 
391
    if(error_status) nested_error(msg);
392
    error_status=1; untrust=0;
393
    module_error_log(msg);
394
    nph_header(550);
395
    printf("Server: %s %s (%s)\r\n", SHORTSWNAME,wims_version,LONGSWNAME);
396
    p=getvar("wims_main_font");
397
    if(p!=NULL && *p!=0) printf("Content-type: text/plain; charset=%s\r\n\r\n",p);
398
    else printf("Content-type: text/plain\r\n\r\n");
399
    p=getvar(ro_name[ro_module]); if(p==NULL) p="???";
400
    printf("ERROR.\n\nwims has detected an error in the module '%s'.",p);
401
    if(m_file.l>=0) printf("\n\nIn file '%s', line %d:",
5505 bpr 402
                     m_file.name,m_file.l+1);
10 reyssat 403
    printf(" %s.\n\n",msg);
404
    if(open_working_file(&mf,mkfname(NULL,"%s.%s",module_error_msg_file,lang))!=0)
405
      internal_error("module_error(): error message file not found.");
406
    while(wgetline(tmplbuf,MAX_LINELEN,&mf)!=EOF) {
5505 bpr 407
      if(tmplbuf[0]!=tag_prefix_char) {
408
          if(send) {substit(tmplbuf); puts(tmplbuf);}
409
          continue;
410
      }
411
      strip_trailing_spaces(tmplbuf);
412
      if(tmplbuf[1]==0 || strcmp(msg,tmplbuf+1)==0) send=1;
413
      else send=0;
10 reyssat 414
    }
415
    close_working_file(&mf,0);
416
    outptr=outbuf; delete_pid(); exit(1);
417
}
418
 
5505 bpr 419
/* Output kernel routine */
10 reyssat 420
void _output_(char *s)
421
{
422
    int l,l2;
423
    l=strlen(s); output_length+=l;
424
    if(output_length>=OUTPUT_LENGTH_LIMIT) {
5505 bpr 425
      module_error("output_too_long"); return;
10 reyssat 426
    }
427
    l2=sizeof(outbuf)-(outptr-outbuf);
428
    put: if(l<=l2) {
5505 bpr 429
      memmove(outptr,s,l); outptr+=l; return;
10 reyssat 430
    }
431
    memmove(outptr,s,l2); s+=l2; l-=l2;
3843 kbelabas 432
    if(lastout_file!=-1) (void)write(lastout_file,outbuf,sizeof(outbuf));
10 reyssat 433
    else fwrite(outbuf,sizeof(outbuf),1,stdout);
434
    outptr=outbuf; l2=sizeof(outbuf); goto put;
435
}
436
 
5505 bpr 437
/* Output with no var. */
10 reyssat 438
void output0(char buf[])
439
{
440
    char *p1, *p2, *ps;
441
    int dynsave;
442
    if(backslash_insmath) {
5505 bpr 443
     ps=buf; dynsave=instex_usedynamic;
444
     for(p1=strstr(buf,"\\("); p1; p1=strstr(p2,"\\(")) {
445
         p2=find_matching(p1+2,')');
446
         if(p2==NULL) break;
447
         if(p1>buf && *(p1-1)=='\\') continue;
448
         *p1=0; if(*(p2-1)=='\\') *(p2-1)=0; *p2++=0; _output_(ps); ps=p2;
449
         instex_usedynamic=1; insmath(p1+2);
450
     }
451
     if(*ps) _output_(ps); instex_usedynamic=dynsave;
10 reyssat 452
    }
453
    else _output_(buf);
454
}
455
 
5505 bpr 456
/* Output routine */
10 reyssat 457
void output(char *s,...)
458
{
459
    va_list vp;
460
    char buf[4*MAX_LINELEN+1];
461
 
462
    va_start(vp,s);
463
    vsnprintf(buf,sizeof(buf),s,vp);
464
    va_end(vp);
465
    output0(buf);
466
}
467
 
5505 bpr 468
     /* read in tmpf in tmp directory, and places in p.
469
      * Maximal length: MAX_LINELEN. */
10 reyssat 470
void read_tmp_file(char *p, const char *fname)
471
{
472
    char *name, *pp;
473
    name=mkfname(NULL,"%s/%s",tmp_dir,fname);
474
    if(!exec_is_module || !outputing || !direct_exec
475
       || strcmp(fname,"exec.out")!=0) {
5505 bpr 476
      readfile(name,p,MAX_LINELEN);
477
      pp=getvar("wims_exec_dollar_double");
478
      if(pp && strcmp(pp,"yes")==0) {
479
          for(pp=strchr(p,'$'); pp; pp=strchr(pp+2,'$'))
480
            string_modify(p,pp,pp+1,"$$");
481
      }
10 reyssat 482
    }
483
    else {
5505 bpr 484
      char *s;
485
      s=readfile(name,NULL,OUTPUT_LENGTH_LIMIT);
486
      if(s==NULL) {*p=0; return;}
487
      if(memcmp(s,"Error: ", strlen("Error: "))==0) mystrncpy(p,s,MAX_LINELEN);
488
      output_length+=strlen(s);
489
      if(output_length>=OUTPUT_LENGTH_LIMIT) module_error("output_too_long");
490
      else _output_(s);
491
      free(s); *p=0;
492
      chmod(name,S_IRUSR|S_IWUSR);
10 reyssat 493
    }
494
}
495
 
5505 bpr 496
      /* verify whether the module is trusted.
497
       * Returns 1 if yes, 0 if no. -1 for error. */
10 reyssat 498
int trusted_module(void)
499
{
500
    char *modname, *w, buf[MAX_LINELEN+1];
501
    int i,n;
5505 bpr 502
    static int _trusted=-1;      /* avoid repeated computations */
7646 bpr 503
 
10 reyssat 504
    if(untrust&255) return 0;
505
    if(_trusted>=0) return _trusted;
506
    modname=getvar(ro_name[ro_module]);
507
    if(modname==NULL || *modname==0) return 0;
7646 bpr 508
    if(memcmp(modname,"adm/",strlen("adm/"))==0 ||
509
       memcmp(modname,"classes/",strlen("classes/"))==0 ||
10 reyssat 510
       strcmp(modname,home_module)==0 ||
511
       memcmp(modname,"help/",strlen("help/"))==0) {
5505 bpr 512
      tr:
513
      if(memcmp(modname,"classes/",strlen("classes/"))==0)
514
        is_class_module=1;
515
      setenv("trusted_module","yes",1);
516
      return _trusted=1;
10 reyssat 517
    }
518
    n=wordnum(trusted_modules); for(i=0;i<n;i++) {
5505 bpr 519
      w=fnd_word(trusted_modules,i+1,buf);
520
      if(strcmp(w,modname)==0) goto tr;
10 reyssat 521
    }
522
    return _trusted=0;
523
}
524
 
7646 bpr 525
      /* file should be in the module directory, but
5505 bpr 526
       * it may also be somewhere else.
527
       * buf[] requires MAX_FNAME+1 length.
528
       * Returns 0 if found. */
10 reyssat 529
int find_module_file(char *fname, char buf[], int mode)
530
{
531
    char *p, dtest[32];
7646 bpr 532
 
10 reyssat 533
    fname=find_word_start(fname);
534
    if(*fname==0) return -1;
5505 bpr 535
      /* Name checking: no directory backtracing. */
10 reyssat 536
    if(strstr(fname,parent_dir_string)!=NULL) {
5505 bpr 537
      setvar(error_data_string,fname); module_error("illegal_fname");
538
      return -1;
10 reyssat 539
    }
540
    p=strchr(fname,'/'); if(p==NULL || p>fname+10) goto openit;
541
    memmove(dtest,fname,p-fname); dtest[p-fname]=0;
542
    if(strcmp(dtest,"datamodule")==0) {
5505 bpr 543
      mkfname(buf,"modules/data%s",p); goto lastopen;
10 reyssat 544
    }
545
    if(strcmp(dtest,"wimshome")==0 && trusted_module()) {
5505 bpr 546
      mkfname(buf,"%s%s",getvar("wims_home"),p); goto lastopen;
10 reyssat 547
    }
548
    if(strcmp(dtest,"writable")==0) {
5505 bpr 549
      if(strncmp(p+1,"TEMP_",5)==0 && strchr(p+1,'/')==NULL) {
550
          mkfname(buf,"%s/%s",tmp_dir,p+1);
551
      }
552
      else {
553
          mkfname(buf,"w/%s/%s",module_prefix,p+1);
554
      }
7646 bpr 555
      untrust|=4;
10 reyssat 556
    }
557
    else {
5505 bpr 558
      openit: mkfname(buf,"%s/%s",module_prefix,fname);
10 reyssat 559
    }
560
    if(mode) return 0;
561
    if(ftest(buf)!=is_file) {
5505 bpr 562
      if(lastftype==is_exec) {
10 reyssat 563
isexec:
5505 bpr 564
          setvar("executable",fname); module_error("executable");
565
          return -1;
566
      }
7646 bpr 567
      if(strncmp(fname,"adm/",4)==0 &&
5505 bpr 568
         (!trusted_module() || is_class_module)) return -1;
569
      mkfname(buf,"scripts/%s",fname);
570
      lastopen:
571
      if(mode) return 0;
572
      if(ftest(buf)!=is_file) {
573
          if(lastftype==is_exec) goto isexec;
574
          else return -1;
575
      }
10 reyssat 576
    }
577
    return 0;
578
}
579
 
5505 bpr 580
      /* check whether a file is user-submitted */
581
      /* This is deprecated because of the wimshome/ method. */
10 reyssat 582
/* int user_file(char *name) {
583
    if(name[0]=='/' || name[0]=='.' ||
7646 bpr 584
       strstr(name,"classes/")!=NULL ||
10 reyssat 585
       strstr(name,"forums/")!=NULL ||
586
       strstr(name,"sessions/")!=NULL ||
587
       strstr(name,"doc/")!=NULL) return 1; else return 0;
588
} */
589
 
5505 bpr 590
      /* returns 1 if violation */
10 reyssat 591
int datafile_check(char *name) {
592
    if((untrust&255)==0) return 0;
593
    if(strncmp(name,"data/",strlen("data/"))==0) return 0;
594
    if(strncmp(name,"authors/",strlen("authors/"))==0) return 0;
5333 bpr 595
    if(strncmp(name,"datamodule/",strlen("datamodule/"))==0) return 0;
10 reyssat 596
    return 1;
597
}
598
 
5505 bpr 599
      /* returns 0 if success */
10 reyssat 600
void readdatafile(char *name)
601
{
602
    char *pp;
603
    if(strcmp(name,lastdatafile)==0) return;
604
    lastdata[0]=0; readfile(name,lastdata,WORKFILE_LIMIT);
605
    mystrncpy(lastdatafile,name,sizeof(lastdatafile));
606
    datacache[0]=lastdata; nextdatacache=1;
607
    if(lastdata[0]==tag_string[1]) {
5505 bpr 608
      datacache[1]=lastdata; nextdatacache++;
10 reyssat 609
    }
610
    pp=strstr(lastdata,tag_string);
611
    if(pp) datacache[nextdatacache]=pp;
612
    else datacache[nextdatacache]=lastdata+strlen(lastdata);
613
}
614
 
615
char *_nextdata(char *p)
616
{
617
    char *pp;
618
    if(!*p) return p;
619
    pp=strstr(p,tag_string);
620
    if(pp) return pp;
621
    else return p+strlen(p);
622
}
623
 
5505 bpr 624
      /* datafile structure: number of records.
625
       * tag=1 if direct access */
10 reyssat 626
unsigned int datafile_recordnum(char *p)
627
{
628
    char nbuf[MAX_LINELEN+1], *pp;
629
    int i, t, ret;
630
 
631
    t=untrust; ret=0;
632
    if(direct_datafile) mystrncpy(nbuf,p,sizeof(nbuf));
633
    else if(datafile_check(p)!=0 || find_module_file(p,nbuf,0)) goto ret;
7646 bpr 634
    readdatafile(nbuf);
10 reyssat 635
    for(i=nextdatacache, pp=datacache[i]; *pp;) {
5505 bpr 636
      pp=_nextdata(pp+1); i++;
637
      if(i<DATAFIELD_LIMIT) {
638
          datacache[i]=pp; nextdatacache=i;
639
      }
10 reyssat 640
    }
641
    ret=i-1;
642
    ret:
643
    untrust=t;
644
    return ret;
645
}
646
 
5505 bpr 647
      /* datafile structure: find record n, starting from 1 */
10 reyssat 648
char *datafile_fnd_record(char *p, int n, char bf[])
649
{
650
    char nbuf[MAX_LINELEN+1], *pp, *p2;
651
    int i, t;
652
 
653
    bf[0]=0; t=untrust;
654
    if(n<0) goto ret;
655
    if(direct_datafile) mystrncpy(nbuf,p,sizeof(nbuf));
656
    else if(datafile_check(p)!=0 || find_module_file(p,nbuf,0)) goto ret;
657
    readdatafile(nbuf); if(*lastdata==0) goto ret;
658
    if(n>nextdatacache) {
5505 bpr 659
      for(i=nextdatacache, pp=datacache[i]; i<n && *pp;) {
660
          pp=_nextdata(pp+1); i++;
661
          if(i<DATAFIELD_LIMIT) {
662
            datacache[i]=pp; nextdatacache=i;
663
          }
664
      }
10 reyssat 665
    }
666
    else pp=datacache[n];
667
    if(!*pp) goto ret;
668
    if(n>1 || (n==1 && *pp!=tag_string[1])) pp+=strlen(tag_string);
669
    else if(n==1) pp+=strlen(tag_string)-1;
670
    if(n<nextdatacache) p2=datacache[n+1];
671
    else {
5505 bpr 672
      p2=strstr(pp,tag_string); if(p2==NULL) p2=pp+strlen(pp);
673
      if(n<DATAFIELD_LIMIT-1 && n==nextdatacache) {
674
          nextdatacache++; datacache[nextdatacache]=p2;
675
      }
10 reyssat 676
    }
677
    if(p2-pp>=MAX_LINELEN) p2=pp+MAX_LINELEN-1;
678
    if(p2<pp) p2=pp;
679
    memmove(bf,pp,p2-pp); bf[p2-pp]=0;
680
    ret:
681
    untrust=t; return bf;
682
}
683
 
684
char hex2char(char c1, char c2)
685
{
686
    char tbuf[16];
687
    if(c1<'0' || c1>'f' || c2<'0' || c2>'f') {
688
invl:
5505 bpr 689
      snprintf(tbuf,sizeof(tbuf),"%%%c%c",c1,c2);
690
      setvar(error_data_string,tbuf);
691
      user_error("invalid_char_in_query_string");
10 reyssat 692
    }
693
    c1=toupper(c1);c2=toupper(c2);
694
    if(c1>'9' && c1<'A') goto invl;
695
    if(c2>'9' && c2<'A') goto invl;
696
    if(c1>'F' || c2>'F') goto invl;
697
    if(c1>='A') c1=c1-'A'+'9'+1;
698
    if(c2>='A') c2=c2-'A'+'9'+1;
699
    return (c1-'0')*16+c2-'0';
700
}
701
 
5505 bpr 702
      /* Converts back http escaped chars, slight. Does not check buffer length.
703
       * Returns converted string length. */
10 reyssat 704
int _http2env(char outs[], char ins[])
705
{
706
    int j,k,l;
707
    l=strlen(ins);
708
    for(j=k=0;j<l && !isspace(ins[j]);j++,k++) {
5505 bpr 709
      if(isspace(ins[j])) {  /* skip space characters in query string */
710
          k--;continue;
711
      }
712
      if(ins[j]=='%') {
713
            /* skip Carriage-Return. */
714
          if(ins[j+1]=='0' && (ins[j+2]=='d' || ins[j+2]=='D')) {
715
            j+=2; k--; continue;
716
          }
717
          outs[k]=hex2char(ins[j+1],ins[j+2]);
718
          j+=2; continue;
719
      }
720
      outs[k]=ins[j];
10 reyssat 721
    }
7646 bpr 722
    outs[k]=0;
10 reyssat 723
    return k;
724
}
725
 
5505 bpr 726
      /* Converts back http escaped chars. Does not check buffer length.
727
       * Returns converted string length. */
10 reyssat 728
int http2env(char outs[], char ins[])
729
{
730
    int j,k,l;
731
    l=strlen(ins);
732
    for(j=k=0;j<l && !isspace(ins[j]);j++,k++) {
5505 bpr 733
      if(isspace(ins[j])) {  /* skip space characters in query string */
734
          k--;continue;
735
      }
736
      if(ins[j]=='%') {
737
            /* skip Carriage-Return. */
738
          if(ins[j+1]=='0' && (ins[j+2]=='d' || ins[j+2]=='D')) {
739
            j+=2; k--; continue;
740
          }
741
          outs[k]=hex2char(ins[j+1],ins[j+2]);
742
          j+=2; continue;
743
      }
744
      if(ins[j]=='+') {
745
          outs[k]=' '; continue;
746
      }
747
      if(ins[j]=='?' || ins[j]=='&') {
748
          outs[k]=0; continue;
749
      }
750
      outs[k]=ins[j];
10 reyssat 751
    }
7646 bpr 752
    outs[k]=0;
10 reyssat 753
    return k;
754
}
755
 
5505 bpr 756
      /* translate a string to http querystring style.
757
       * '&' is not translated.
758
       * Buffer p must be at least MAX_LINELEN. */
10 reyssat 759
void tohttpquery(char *p)
760
{
761
    char trlist[]="     ()[]{}+-*^|/\"\'!:;,<>\n";
762
    char *pp;
763
    for(pp=p;*pp;pp++) {
5505 bpr 764
      if(*pp==' ') {
765
          *pp='+'; continue;
766
      }
767
      if(strchr(trlist,*pp)==NULL) continue;
768
      if(*pp=='+' && pp>p && *(pp-1)=='&') continue;
769
      if(pp>p && *(pp-1)=='\\') {
770
          ovlstrcpy(pp-1,pp);pp--;continue;
771
      }
772
      if(*pp=='\n') {
773
          string_modify(p,pp,pp+1,"%%0D%%0A");pp+=5;
774
      }
775
      else {
776
          string_modify(p,pp,pp+1,"%%%02X",*pp);pp+=2;
777
      }
10 reyssat 778
    }
779
}
780
 
5505 bpr 781
      /* substitute backslash parameters. Internal use only. */
10 reyssat 782
void slashsubst(char *p)
783
{
784
    char *p1, *p2, *pt, *pp, namebuf[128];
785
    int n;
786
 
787
    n=strlen(mathfont_prefix); memmove(namebuf,mathfont_prefix,n+1);
788
    for(p1=strchr(p,'\\'); p1!=NULL; p1=strchr(p1,'\\')) {
5505 bpr 789
      p1++; for(p2=p1; myisalnum(*p2) || *p2=='_'; p2++);
790
      if(p2<=p1 || p2>p1+100) continue;
791
      memmove(namebuf+n,p1,p2-p1); namebuf[p2-p1+n]=0;
792
      pt=_getvar(namebuf); if(pt==NULL) continue;
793
      if(*p2=='[' && (pp=find_matching(p2+1,']'))!=NULL) {
794
          string_modify(p,pp+1,pp+1,")");
795
          string_modify(p,p1-1,p1,"$(%s",mathfont_prefix);
796
      }
797
      else string_modify(p,p1-1,p1,"$%s",mathfont_prefix);
10 reyssat 798
    }
799
}
800
 
5505 bpr 801
      /* two alarm handlers. */
10 reyssat 802
void alarm1(int s)
803
{
804
    if(killpid>0 && kill(killpid,SIGKILL)) module_error("timeup");
805
    killpid=0;
806
}
807
 
808
void alarm2(int s)
809
{
810
    cleantmpdir();
811
    alarm1(s); module_error("timeup");
812
}
813
 
814
void finalalarm(void)
815
{
816
    time_t curr;
817
    curr=time(0);
818
    if(curr>=limtime) alarm2(SIGALRM);
819
    errno=0;
820
    if(signal(SIGALRM,alarm2)==SIG_ERR)
821
      internal_error(strerror(errno));
822
    alarm(limtime-curr+1);
823
}
824
 
825
void initalarm(void)
826
{
827
    limtimex=nowtime+4*rlimit_cpu/3;
828
    limtime=limtimex+2; finalalarm();
829
}
830
 
831
void forkalarm(void)
832
{
833
    time_t curr;
834
    curr=time(0);
835
    if(curr>=limtimex) {alarm1(SIGALRM); return;}
836
    if(signal(SIGALRM,alarm1)==SIG_ERR)
837
      internal_error(strerror(errno));
838
    alarm(limtimex-curr+1);
839
}
840
 
5505 bpr 841
      /* create pid tag */
10 reyssat 842
void create_pid(void)
843
{
844
    char buf[MAX_FNAME+1], pbuf[256], obuf[MAX_FNAME+1];
845
    struct stat dst;
846
    struct utimbuf ub;
7646 bpr 847
 
10 reyssat 848
    if(robot_access || *session_prefix==0) return;
849
    if(cmd_type==cmd_getframe) return;
850
    mkfname(buf,"%s/.pid",s2_prefix);
5505 bpr 851
            /* another process running? */
10 reyssat 852
    if(readfile(buf,pbuf,sizeof(pbuf))!=NULL) {
5505 bpr 853
      mkfname(obuf,"/proc/%s",pbuf);
854
      if(stat(obuf,&dst)==0) user_error("double_click");
10 reyssat 855
    }
856
    snprintf(pidbuf,sizeof(pidbuf),"%u",getpid());
857
    accessfile(pidbuf,"w","%s",buf);
5505 bpr 858
      /* Touch session time */
10 reyssat 859
    if(strstr(session_prefix,"sessions/")==NULL) return;
860
    ub.actime=ub.modtime=nowtime;
861
    utime(session_prefix,&ub);
862
    if(strchr(session_prefix,'_')!=NULL) { /* touch parent too */
5505 bpr 863
      char sbuf[MAX_FNAME+1], *p;
864
      mystrncpy(sbuf,session_prefix,sizeof(sbuf));
865
      p=strchr(sbuf,'_'); if(p!=NULL) *p=0;
866
      utime(sbuf,&ub);
10 reyssat 867
    }
868
}
869
 
870
struct {
871
    char cmd[MAX_EXEC_NAME+1];
872
    unsigned int fd1, fd2;
873
    int pipe_stdin[2];
874
    int pipe_stdout[2];
875
    int pipe_stderr[2];
876
} mxtab[MAX_MULTIEXEC];
877
int mxno=0;
878
 
879
int execredirected(char *cmdf, char *inf, char *outf, char *errf, char *arg[])
880
{
881
    pid_t pid;
882
    int status, t;
883
 
884
    if(robot_access) return 0;
885
    if(time(0)>=limtimex) {
5505 bpr 886
      if(errf!=NULL)
887
        accessfile("No time left to execute subprograms.\n","w","%s",errf);
888
      return -100;
10 reyssat 889
    }
890
    lastdatafile[0]=lastftest[0]=0;
5505 bpr 891
    fflush(NULL);      /* flush all output streams before forking
892
                   * otherwise they will be doubled */
10 reyssat 893
    pid=fork(); if(pid==-1) return -1;
5505 bpr 894
    if(!pid) {      /* child */
895
      char buf[MAX_LINELEN+1]; int k;
896
      (void)nice(10);      /* lower priority for children */
897
      if(is_multiexec) {
898
          dup2(mxtab[multiexec_index].pipe_stdin[0],0);
899
          dup2(mxtab[multiexec_index].pipe_stdout[1],1);
900
          dup2(mxtab[multiexec_index].pipe_stderr[1],2);
901
      }
902
      else {
903
          if(inf!=NULL) (void)freopen(inf,"r",stdin);
904
          if(outf!=NULL) (void)freopen(outf,"w",stdout);
905
          if(errf!=NULL) (void)freopen(errf,"w",stderr);
906
      }
7646 bpr 907
            /* This is to patch LinuxPPC uid wrapping
5505 bpr 908
             * for scripts */
909
      t=0; if(strchr(cmdf,'/')) {
910
          int tf;
911
          char tbuf[16];
912
          tf=open(cmdf,O_RDONLY); (void)read(tf,tbuf,8); close(tf);
913
          if(memcmp(tbuf+1,"ELF",3)!=0) t=1;
914
      }
915
      if(wrapexec==-1) {
916
          setreuid(getuid(),getuid());setregid(getgid(),getgid());
917
      }
918
      if(wrapexec==1 || (t==1 && wrapexec==0)) {
919
          setreuid(geteuid(),geteuid());setregid(getegid(),getegid());
920
      }
921
      errno=0;
922
      if(strchr(cmdf,'/')) execve(cmdf,arg,environ);
923
      else execvp(cmdf,arg);
924
      snprintf(buf,sizeof(buf),"Failed to execute");
925
      for(k=0;arg[k];k++) {
926
          t=strlen(buf);
927
          snprintf(buf+t,sizeof(buf)-t," %s",arg[k]);
928
      }
929
      t=strlen(buf);
930
      snprintf(buf+t,sizeof(buf)-t,"\n  %s\n",strerror(errno));
931
      accessfile(buf,"a","%s/exec.fail",tmp_dir);
932
      exit(127);
10 reyssat 933
    }
5505 bpr 934
    else {      /* parent */
7646 bpr 935
      wrapexec=0; status=0;
5505 bpr 936
      if(exec_wait && !is_multiexec) {
937
          killpid=pid; forkalarm();
938
          waitpid(pid,&status,0); killpid=0; finalalarm();
939
      }
940
      return WEXITSTATUS(status);
10 reyssat 941
    }
942
}
943
 
5505 bpr 944
      /* preparation for resident execution.
945
       * Returns 1 if already up, otherwise 0. */
10 reyssat 946
int multiexec(char *cmd, char **abuf)
947
{
948
    char *p;
949
    int i;
7646 bpr 950
 
10 reyssat 951
    if(robot_access) return 0;
952
    if(strstr(tmp_dir,"sessions/")==NULL) return 0;
7646 bpr 953
    if(strstr(tmp_debug,"yes")!=NULL && checkhost(manager_site)>=1)
10 reyssat 954
      setenv("multiexec_debug","yes",1);
955
    p=getvar("wims_multiexec");
956
    if(p==NULL || wordchr(p,cmd)==NULL) return 0; /* not allowed */
957
    if(!multiexec_random[0]) {
5505 bpr 958
      snprintf(multiexec_random, sizeof(multiexec_random),
959
             "%lX%lX%lX%lX%lX%lX%lX%lX",
960
             random(),random(),random(),random(),
961
             random(),random(),random(),random());
962
      setenv("multiexec_random",multiexec_random,1);
10 reyssat 963
    }
964
    for(i=0;i<mxno && strcmp(cmd,mxtab[i].cmd)!=0; i++);
965
    multiexec_index=i;
966
    if(i==mxno) {
5505 bpr 967
      if(mxno>=MAX_MULTIEXEC) return 0;
968
      if(pipe(mxtab[i].pipe_stdin)<0) return 0;
969
      if(pipe(mxtab[i].pipe_stdout)<0) return 0;
970
      if(pipe(mxtab[i].pipe_stderr)<0) return 0;
971
      mystrncpy(mxtab[i].cmd,cmd,sizeof(mxtab[i].cmd));
972
      mxno++;      is_multiexec=1;
973
      exportall(); setenv("wims_exec_parm",multiexec_random,1);
974
      execredirected(abuf[0],NULL,NULL,NULL,abuf);
10 reyssat 975
    }
976
    is_multiexec=0;
977
    return 1;
978
}
979
 
5505 bpr 980
      /* my system(), but with variable parms
981
       * More secure than system(), and direct fork. */
10 reyssat 982
int call_ssh(char *s,...)
983
{
984
    va_list vp;
985
    char buf[MAX_LINELEN+1];
986
    char *arg[1024];
987
    char *inf=NULL, *outf=NULL, *errf=NULL;
988
    char *cmdf, *p, *p2;
989
    int i, d;
990
 
991
    if(robot_access) return 0;
992
    va_start(vp,s);
993
    vsnprintf(buf,sizeof(buf),s,vp);
994
    va_end(vp);
995
    p=find_word_start(buf); if(*p==0) return 0;
996
    cmdf=p;
997
    for(i=0;*p!=0 && i<1000; p=find_word_start(p2)) {
5505 bpr 998
      switch(*p) {
999
          case '\'': {
1000
            p++; p2=strchr(p,'\''); if(p2==NULL) p2=p+strlen(p);
1001
            d=0; break;
1002
          }
1003
          case '"': {
1004
            p++; p2=strchr(p,'"'); if(p2==NULL) p2=p+strlen(p);
1005
            d=0; break;
1006
          }
1007
          default: d=1; p2=find_word_end(p); break;
1008
      }
1009
      if(*p2) *p2++=0;
1010
      if(!d) {arg[i++]=p; continue;}
1011
      switch(*p) {
1012
          case '<': inf=++p; break;
1013
          case '>': {
1014
            p++; if(*p=='&') {
1015
                merge: p++; errf=outf=p; break;
1016
            }
1017
            else outf=p;
1018
            break;
1019
          }
1020
          case '&': {
1021
            p++; if(*p=='>') goto merge;
1022
            else break;
1023
          }
1024
          case '2': {
1025
            if(*(p+1)=='>') {errf=p+2; break;}
1026
          }
1027
          default: arg[i++]=p; break;
1028
      }
10 reyssat 1029
    }
1030
    arg[i]=NULL;
1031
    return execredirected(cmdf,inf,outf,errf,arg);
1032
}
1033
 
5505 bpr 1034
      /* Read/write to a file with variable parms to print filename */
10 reyssat 1035
void accessfile(char *content, char *type, char *s,...)
1036
{
1037
    va_list vp;
1038
    char buf[MAX_FNAME+1];
1039
    int fd;
1040
 
1041
    if(robot_access) return;
1042
    va_start(vp,s);
1043
    vsnprintf(buf,sizeof(buf),s,vp);
1044
    va_end(vp);
1045
    if(strlen(buf)>=MAX_FNAME-1) nametoolong(buf);
1046
    switch(*type) {
5505 bpr 1047
      case 'r': readfile(buf,content,MAX_LINELEN); return;
1048
      case 'e': readfile(buf,content,MAX_LINELEN/4); return; /* limited read */
1049
      case 'w': fd=creat(buf,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); break;
1050
      case 'a': fd=open(buf,O_WRONLY|O_CREAT|O_APPEND,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); break;
1051
      default : return;
10 reyssat 1052
    }
1053
    lastdatafile[0]=lastftest[0]=0;
1054
    if(fd==-1) return;
3843 kbelabas 1055
    (void)write(fd,content,strlen(content)); close(fd);
10 reyssat 1056
}
1057
 
5505 bpr 1058
      /* system(), but with variable parms
1059
       * Uses sh to execute command. */
10 reyssat 1060
int call_sh(char *s,...)
1061
{
1062
    va_list vp;
1063
    char buf[MAX_LINELEN+1];
1064
    char *abuf[8];
1065
 
1066
    if(robot_access) return 0;
1067
    va_start(vp,s);
1068
    vsnprintf(buf,sizeof(buf),s,vp);
1069
    va_end(vp);
1070
    abuf[0]="sh"; abuf[1]="-c"; abuf[2]=buf; abuf[3]=NULL;
1071
    return execredirected(abuf[0],NULL,NULL,NULL,abuf);
1072
}
1073
 
1074
void _getdef(char buf[], char *name, char value[])
1075
{
1076
    char *p1, *p2, *p3, *p4;
1077
 
5505 bpr 1078
    if(*name==0) goto nothing;      /* this would create segfault. */
10 reyssat 1079
    for(p1=strstr(buf,name); p1!=NULL; p1=strstr(p1+1,name)) {
5505 bpr 1080
      p2=find_word_start(p1+strlen(name));
1081
      if((p1>buf && !isspace(*(p1-1))) || *p2!='=') continue;
1082
      p3=p1; while(p3>buf && *(p3-1)!='\n') p3--;
1083
      p3=find_word_start(p3);
1084
      if(p3<p1 && *p3!='!') continue;
1085
      if(p3<p1) {
1086
          p3++; p4=find_word_end(p3);
1087
          if(find_word_start(p4)!=p1) continue;
1088
          if(p4-p3!=3 || (strncmp(p3,"set",3)!=0 &&
1089
             strncmp(p3,"let",3)!=0 &&
1090
             strncmp(p3,"def",3)!=0)) {
1091
            if(p4-p3!=6 || strncmp(p3,"define",6)!=0) continue;
1092
          }
1093
      }
1094
      p2++;p3=strchr(p2,'\n'); if(p3==NULL) p3=p2+strlen(p2);
1095
      p2=find_word_start(p2);
1096
      if(p2>p3) goto nothing;
1097
      if(p3-p2>=MAX_LINELEN) user_error("cmd_output_too_long");
1098
      memmove(value,p2,p3-p2); value[p3-p2]=0;
1099
      strip_trailing_spaces(value); return;
10 reyssat 1100
    }
1101
nothing:
1102
    value[0]=0; return;
1103
}
1104
 
5505 bpr 1105
      /* Get variable definition from a file.
1106
       * Result stored in buffer value of length MAX_LINELEN. */
10 reyssat 1107
void getdef(char *fname, char *name, char value[])
1108
{
1109
    char buf[MAX_LINELEN+1], tbuf[MAX_LINELEN+1], nbuf[MAX_NAMELEN+1];
1110
    char *p1, *p2;
1111
 
1112
    value[0]=0; if(readfile(fname,buf,sizeof(buf)-16)==NULL) return;
1113
    mystrncpy(value,name,MAX_LINELEN);
1114
    for(p1=value; *p1; p1=p2) {
5505 bpr 1115
      while(*p1 && !myisalnum(*p1) && *p1!='_') p1++;
1116
      if(*p1==0) break;
1117
      for(p2=p1; myisalnum(*p2) || *p2=='_'; p2++);
1118
      if(p2-p1>MAX_NAMELEN) continue;
1119
      memmove(nbuf,p1,p2-p1); nbuf[p2-p1]=0;
1120
      _getdef(buf,nbuf,tbuf);
1121
      string_modify(value,p1,p2,"%s",tbuf);
1122
      p2=p1+strlen(tbuf);
10 reyssat 1123
    }
1124
}
1125
 
1126
int _setdef_changed;
1127
 
1128
void _setdef(char buf[], char *name, char *value)
1129
{
1130
    char *p1, *p2, *p3;
1131
    int n;
7646 bpr 1132
 
10 reyssat 1133
    for(p1=strstr(buf,name); p1!=NULL; p1=strstr(p1+1,name)) {
5505 bpr 1134
      p2=find_word_start(p1+strlen(name));
1135
      if((p1>buf && !isspace(*(p1-1))) || *p2!='=') continue;
1136
      p3=p1; while(p3>buf && *(p3-1)==' ') p3--;
1137
      if(p3>buf && *(p3-1)!='\n') continue;
1138
      p2++;p3=strchr(p2,'\n'); if(p3==NULL) p3=p2+strlen(p2);
1139
      if(strlen(value)!=p3-p2 || strncmp(value,p2,p3-p2)!=0) {
1140
          string_modify(buf,p2,p3,"%s",value);
1141
          _setdef_changed++;
1142
      }
1143
      return;
10 reyssat 1144
    }
1145
    n=strlen(buf);
7646 bpr 1146
    if(n>0 && buf[n-1]!='\n')
10 reyssat 1147
      snprintf(buf+n,MAX_LINELEN-n,"\n%s=%s\n",name,value);
1148
    else
1149
      snprintf(buf+n,MAX_LINELEN-n,"%s=%s\n",name,value);
1150
    _setdef_changed++;
1151
}
1152
 
5505 bpr 1153
      /* Set variable definition to a file. */
10 reyssat 1154
void setdef(char *fname, char *name)
1155
{
1156
    char buf[MAX_LINELEN+1];
1157
    char *p1, *p2, *p3;
1158
 
1159
    _setdef_changed=0;
1160
    if(strchr(name,'=')==NULL) return;
1161
    for(p1=name;*p1;p1++) {
5505 bpr 1162
      if(isspace(*p1) && *p1!=' ' && *p1!='\n') *p1=' ';
1163
      if(*p1==' ') {
1164
          for(p2=p1+1; isspace(*p2) && *p2!='\n'; p2++);
1165
          if(p2>p1+1) ovlstrcpy(p1+1,p2);
1166
          p2=p1+1; if(*p2=='=' || *p2=='\n') ovlstrcpy(p1,p2);
1167
      }
10 reyssat 1168
    }
1169
    if(readfile(fname,buf,sizeof(buf))==NULL) buf[0]=0;
1170
    for(p1=find_word_start(name); p1!=NULL; p1=p2) {
5505 bpr 1171
      p2=strchr(p1,'\n'); if(p2!=NULL) *p2++=0;
1172
      p1=find_word_start(p1);
1173
      p3=strchr(p1,'='); if(p3==NULL) continue;
1174
      *p3++=0; p3=find_word_start(p3);
1175
      _setdef(buf,p1,p3);
10 reyssat 1176
    }
1177
    if(_setdef_changed) accessfile(buf,"w","%s",fname);
1178
}
1179
 
5505 bpr 1180
      /* check whether connecting host is part of given list.
1181
       * Returns 0 if no, 1 if yes. */
10 reyssat 1182
int checkhost(char *hlist)
1183
{
1184
    char buf[MAX_LINELEN+1];
1185
    char lbuf[1024], hbuf1[256], hbuf2[256];
1186
    char *p1, *p2, *pb, *pe, *pp;
7646 bpr 1187
 
10 reyssat 1188
    if(*remote_addr==0) return 0;
1189
    snprintf(hbuf1,sizeof(hbuf1),"+%s+",remote_addr);
1190
    if(*remote_host!=0) {
5505 bpr 1191
      snprintf(hbuf2,sizeof(hbuf2),"+%s+",remote_host);
1192
      for(p1=hbuf2; *p1; p1++) *p1=tolower(*p1);
10 reyssat 1193
    }
1194
    else hbuf2[0]=0;
1195
    mystrncpy(buf,find_word_start(hlist),sizeof(buf)); strip_trailing_spaces(buf);
1196
    for(p1=buf; *p1; p1++) {
5505 bpr 1197
      *p1=tolower(*p1);
1198
      if(!myisalnum(*p1) && strchr(".-_",*p1)==NULL) *p1=' ';
10 reyssat 1199
    }
5505 bpr 1200
    if(strcmp(buf,"all")==0) return 1;      /* all is all */
10 reyssat 1201
    for(p1=find_word_start(buf); *p1; p1=find_word_start(p2)) {
5505 bpr 1202
      p2=find_word_end(p1); if(*p2) *p2++=0;
1203
      if(p2-p1<3) continue;
1204
      if(myisalnum(*p1)) pb="+"; else pb="";
1205
      if(myisalnum(*(p2-1))) pe="+"; else pe="";
1206
      snprintf(lbuf,sizeof(lbuf),"%s%s%s",pb,p1,pe);
7646 bpr 1207
      for(pp=p1; *pp && (myisdigit(*pp) || *pp=='.'); pp++);
5505 bpr 1208
      if(*pp) pp=hbuf2;      /* host name */
1209
      else pp=hbuf1;         /* ip number */
1210
      if(strstr(pp,lbuf)!=NULL) return 1;      /* found */
10 reyssat 1211
    }
1212
    return 0;
1213
}
1214
 
5505 bpr 1215
      /* return 1 if a word of bf2 is a substring of host.
1216
       * Like checkhost, but with time check.
1217
       * The content of bf2[] is destroyed. */
10 reyssat 1218
int checkhostt(char bf2[])
1219
{
1220
    char *p1, *p2, *p3;
5505 bpr 1221
      /* compare with starting time */
10 reyssat 1222
    for(p1=strchr(bf2,'>'); p1!=NULL; p1=strchr(p1+1,'>')) {
5505 bpr 1223
      if(p1>bf2 && !isspace(*(p1-1))) continue;
1224
      p3=find_word_start(++p1); p2=find_word_end(p3);
1225
      if(p2-p3!=14) continue;
1226
      p3[8]='.'; p3[11]=':'; if(*p2) *p2++=0;
1227
      if(strncmp(nowstr,p3,14)<0) return 0;
1228
      ovlstrcpy(p1-1,p2); p1-=2;
10 reyssat 1229
    }
5505 bpr 1230
      /* compare with ending time */
10 reyssat 1231
    for(p1=strchr(bf2,'<'); p1!=NULL; p1=strchr(p1+1,'<')) {
5505 bpr 1232
      if(p1>bf2 && !isspace(*(p1-1))) continue;
1233
      p3=find_word_start(++p1); p2=find_word_end(p3);
1234
      if(p2-p3!=14) continue;
1235
      p3[8]='.'; p3[11]=':'; if(*p2) *p2++=0;
1236
      if(strncmp(nowstr,p3,14)>0) return 0;
1237
      ovlstrcpy(p1-1,p2); p1-=2;
10 reyssat 1238
    }
1239
    p1=find_word_start(bf2); if(*p1==0) return 1;
1240
    return checkhost(p1);
1241
}
1242
 
5505 bpr 1243
      /* bad identification */
10 reyssat 1244
void bad_ident(void)
1245
{
1246
    if(cookiegot[0]!=0) {
1247
    }
1248
    user_error("bad_ident");
1249
}
1250
 
1251
void instex_flush(void)
1252
{
1253
    char *p;
1254
    setenv("texgif_style","",1);
1255
    setenv("texgif_tmpdir",tmp_dir,1);
1256
    setenv("texgif_src",instex_src,1);
1257
    setenv("texgif_outfile",instex_fname,1);
1258
    unsetenv("w_instex_color");
1259
    getwimstexsize=0; fix_tex_size(); getwimstexsize=1;
1260
    for(p=instex_fname;*p;p++) if(*p=='\n') *p=' ';
1261
    wrapexec=0; call_ssh("%s/%s >%s/ins.Out 2>%s/ins.Err",
5505 bpr 1262
                   bin_dir,instex_processor,
1263
                   tmp_dir,tmp_dir);
10 reyssat 1264
    call_ssh("mv %s %s >/dev/null 2>/dev/null", instex_fname,s2_prefix);
1265
    instex_src[0]=instex_fname[0]=0; instex_cnt=0;
1266
}
1267
 
5505 bpr 1268
      /* put last.phtml */
10 reyssat 1269
void putlastout(void)
1270
{
1271
    int t;
1272
    if(instex_cnt>0) instex_flush();
1273
    t=catfile(stdout,"%s/%s",s2_prefix,lastout);
1274
    if(t==0) printf("Content-type: text/plain\r\n\r\n");
1275
}
1276
 
1277
struct sockaddr_un sun;
1278
 
5505 bpr 1279
      /* returns >=0 if OK. */
7646 bpr 1280
 
10 reyssat 1281
int kerneld(char *p, int bufsize)
1282
{
1283
    int sock, s, t, t1, l, *ip;
1284
    struct timeval tv;
1285
    fd_set rset;
1286
    sock=socket(PF_UNIX,SOCK_STREAM,0);
1287
    s=connect(sock,(const struct sockaddr *)&sun,sizeof(sun));
1288
    if(s) {bad: close(sock); return -1;}
1289
    ip=(int *) p;
1290
    l=strlen(p+sizeof(int)); *ip=l;
1291
    s=write(sock,p,l+sizeof(int)); if(s!=l+sizeof(int)) goto bad;
1292
    for(t=0, l=bufsize-1; t<l+sizeof(int);) {
5505 bpr 1293
      tv.tv_sec=2; tv.tv_usec=0;
1294
      FD_ZERO(&rset); FD_SET(sock,&rset);
1295
      if(select(sock+1,&rset,NULL,NULL,&tv)<=0) goto bad;
7646 bpr 1296
      t1=read(sock,p+t,l+sizeof(int)-t);
5505 bpr 1297
      if(t1+t<sizeof(int)) goto bad;
7646 bpr 1298
      if (t1 < 0) goto bad;
5505 bpr 1299
      l=*ip; if(l<=0) goto bad;
1300
      if(l>=bufsize-sizeof(int)-4) user_error("cmd_output_too_long");
1301
      t+=t1;
10 reyssat 1302
    }
1303
    p[l+sizeof(int)]=0;
1304
    close(sock);
1305
    return l+sizeof(int);
1306
}
1307
 
1308
void _daemoncmd(char *p)
1309
{
1310
    char buf[MAX_LINELEN+1+sizeof(int)];
1311
    char *p1, *p2, *p3;
1312
    mystrncpy(buf+sizeof(int),p,sizeof(buf)-sizeof(int));
7646 bpr 1313
    if(kerneld(buf,sizeof(buf))<0)
10 reyssat 1314
      internal_error("Daemon communication error.");
1315
    p1=find_word_start(buf+sizeof(int));
1316
    p2=find_word_end(p1); if(*p2) *p2++=0;
1317
    if(strcmp(p1,"OK")==0) {
5505 bpr 1318
      mystrncpy(p,p2,MAX_LINELEN); return;
10 reyssat 1319
    }
1320
    p1=find_word_start(p2); p2=find_word_end(p1); if(*p2) *p2++=0;
7646 bpr 1321
    p2=find_word_start(p2); p3=find_word_end(p2);
10 reyssat 1322
    if(*p3) {
5505 bpr 1323
      *p3++=0; p3=find_word_start(p3); strip_trailing_spaces(p3);
1324
      setvar("wims_error_data",p3);
10 reyssat 1325
    }
1326
    switch(*p1) {
5505 bpr 1327
      case '1': user_error(p2);
1328
      case '2': module_error(p2);
1329
      case '3':
1330
      default: internal_error(p2);
10 reyssat 1331
    }
1332
    *p=0;
1333
}
1334