Subversion Repositories wimsdev

Rev

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