Subversion Repositories wimsdev

Rev

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

  1. /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18. /* Web exerciser */
  19.  
  20. #define WEBMATH 1
  21. #include "Lib/libwims.h"
  22. #include "wims.h"
  23. #include <sys/socket.h>
  24. #include <sys/un.h>
  25.  
  26. struct {
  27.     char *name;
  28.     char *font;
  29. } charname[]={
  30.     {"en","iso-8859-1"},
  31.       {"fr","iso-8859-1"},
  32.       {"es","iso-8859-1"},
  33.       {"cn","gb2312"},
  34.       {"de","iso-8859-1"},
  35.       {"it","iso-8859-1"},
  36.       {"nl","iso-8859-1"},
  37.       {"si","iso-8859-2"},
  38.       {"ar","iso-8859-6"},
  39.       {"tw","big5"},
  40.       {"pt","iso-8859-1"},
  41.       {"ca","iso-8859-1"},
  42.       {"pt","iso-8859-1"},
  43.       {"ru","iso-8859-5"},
  44.       {"ty","iso-8859-13"}
  45. };
  46.  
  47. #define charname_no (sizeof(charname)/sizeof(charname[0]))
  48.  
  49. /* left to right or right to left writing */
  50. struct {
  51.     char *name;
  52.     char *dirn;
  53. } dirnname[]={
  54.     {"en","ltr"},
  55.       {"fr","ltr"},
  56.       {"es","ltr"},
  57.       {"cn","ltr"},
  58.       {"de","ltr"},
  59.       {"it","ltr"},
  60.       {"nl","ltr"},
  61.       {"si","ltr"},
  62.       {"ar","rtl"},
  63.       {"tw","ltr"},
  64.       {"pt","ltr"},
  65.       {"ca","ltr"},
  66.       {"pt","ltr"},
  67.       {"ru","ltr"},
  68.       {"ty","ltr"}
  69.  
  70. };
  71.  
  72. #define dirnname_no (sizeof(dirnname)/sizeof(dirnname[0]))
  73.  
  74.  
  75. #define evalue strevalue
  76. char *robot_session="../tmp/robot";
  77. int robot_access=0,human_access=0;
  78. int user_error_nolog=0;
  79. char *good_agent[]={
  80.       "Mozilla","Netscape","Opera","WIMS",
  81.       "MSIE","Konqueror","Java"
  82. };
  83. #define good_agent_no (sizeof(good_agent)/sizeof(good_agent[0]))
  84.  
  85. char *bad_agent[]={ /* These are really bad agents: prohibited. */
  86.       "HTTrack","MemoWeb","Teleport","Offline","Wget","eCatch",
  87.       "Powermarks","EmailSiphon", "WebCopier"
  88. };
  89. #define bad_agent_no (sizeof(bad_agent)/sizeof(bad_agent[0]))
  90.  
  91. /* used for debugging */
  92. int debug=0;
  93.  
  94. char class_dir[MAX_FNAME+1]; /* directory name of this class */
  95.  
  96. struct {
  97.     char *name;
  98.     char *value;
  99. } user_variable[MAX_VAR_NUM];
  100. int user_var_no;
  101.  
  102. struct VAR_DEF {
  103.     char *name;
  104.     short int beg,end;
  105.     char allow, log_num, defined_in_parm, unused_padding;
  106. } var_def[MAX_VAR_NUM];
  107. int defined_var_total;
  108.  
  109. /* Destinated to module error messages */
  110. WORKING_FILE m_file,svar_file,mcache[MAX_MCACHE];
  111. int mcachecnt;
  112.  
  113. /* Limit for any data working files. */
  114. int WORKFILE_LIMIT=2048*1024;
  115.  
  116. /* whether the user has defined language */
  117. int user_lang=0;
  118.  
  119. /* for instex grouping */
  120. int instex_cnt=0, getwimstexsize=1;
  121. char instex_src[MAX_LINELEN+1], instex_fname[MAX_LINELEN+1];
  122. char *instex_processor="tex..gif";
  123.  
  124. /* Cookie management */
  125. char cookiegot[256], cookieset[256], cookieheader[64]="WIMSUSER=";
  126.  
  127. /* multipart boundary */
  128. char mpboundary[1024];
  129. int  deplen=0; /* length of deposit */
  130.  
  131. int confset=0; /* set to 1 if setvar for config */
  132.  
  133. /* Operating mode: default, popup, raw, etc. */
  134. enum {mode_default, mode_popup, mode_raw} NAME_MODES;
  135. int mode=mode_default;
  136.  
  137. /* Switch; notice subroutines wherether we are outputing. */
  138. int outputing;
  139.  
  140. char *home_module="home"; /* name of home module */
  141. extern char **environ;      /* table of environment variables */
  142. int module_defined=0;
  143.  
  144.         /* directory prefix buffers */
  145. char session_prefix[MAX_FNAME+1], s2_prefix[MAX_FNAME+1], module_prefix[MAX_FNAME+1];
  146.  
  147. char *stdinbuf;
  148. int sesrandomtab[MAX_SESRANDOM]; /* session random values */
  149. char multiexec_random[64];
  150. int executed_gotos; /* number of goto's executed. to check with GOTO_LIMIT. */
  151. int insert_no; /* number of instex, insplot, insPLOT. */
  152. int output_length; /* length of the output. */
  153. int isexam=0; /* non-zero if request is exam */
  154. int simuxam=0; /* exam is simulation */
  155. int is_multiexec=0; /* for execredirected */
  156. int multiexec_index;
  157. int direct_datafile=0;
  158. int exec_is_module=0;
  159.   /* root directory of modules */
  160. char *module_dir="modules";
  161. int new_session=0; /* =1 if new session created */
  162. int good_httpd=0; /* Whether the http server is intelligent */
  163. /* int internal_sql=0; */ /* for internal sql use */
  164. int direct_exec=0; /* calc routine is exected by exec if 1 */
  165. int print_precision=8; /* precision when printing real numbers */
  166. int session_serial; /* request serial for session control */
  167. int form_access=0; /* identifies form access, for robot identification */
  168. int lastout_file=-1; /* file to hold last output */
  169. char *instex_style=""; /* "": text "$": math "$$": displaymath */
  170. int instex_usedynamic=0; /* always dynamic if 1 */
  171. int wrapexec=0; /* if set to 1, change uid (nobody) to euid (wims).
  172.                  * if set to -1, change euid to uid when exec(). */
  173. int parm_restore=0; /* Restoring saved parameters? */
  174. int exec_wait=1; /* whether to wait for fork return */
  175. int execnt=0; /* count executions */
  176. int readnest; /* nested read count */
  177. int mfilecnt=0; /* count working files */
  178. int forceresume=0; /* force user to resume old request */
  179. int manageable=0; /* whether the connection may be site manager
  180.                    * 0: no; 1: maybe; 2: sure */
  181. int ismhelp=0;  /* 1 if session is in mhelp. */
  182. int getvar_len; /* length of the last-got variable. */
  183. int noout=0; /* if set to 1 then output is skipped */
  184. char tmp_dir[MAX_FNAME+1]; /* temporary directory */
  185. char *bin_dir="bin"; /* directory containing executable scripts and programs. */
  186. char cwdbuf[MAX_FNAME+1]; /* store current working directory */
  187. char var_hacking=0; /* Trying to hack a variable? */
  188. char *tmp_debug="no";
  189. char ins_alt[MAX_LINELEN+1]; /* dynamic insertion alternative text */
  190. char *devel_modules="close"; /* whether to open devel modules */
  191. int isclassmodule=0; /* 1 if the module is class module */
  192. int isdevelmodule=0; /* development module? */
  193. int setcookie=0; /* 1 if need to set cookie */
  194. int killpid=0; /* pid of process to kill by alarm */
  195. char *mathalign_sup1, *mathalign_sup2; /* see mathalign_base */
  196. int substnest=0; /* nesting level of substit() */
  197. int exodepOK=1;
  198. long int startmtime; /* start time in milliseconds */
  199. long int startmtime2; /* start time in microseconds */
  200. int backslash_insmath=0; /* \(...) substitution? */
  201. char examlogf[MAX_FNAME+1]; /* examlog file name */
  202. char examlogd[MAX_FNAME+1]; /* examlog file name */
  203. char exam_sheetexo[32]; /* sheet data of an exam */
  204. char loadavg[64];
  205. /* user file variable access control. */
  206. char *var_readable, *var_writable, *var_nr, *var_nw, *var_pfx;
  207. int hostcquota;
  208. int var_noexport; /* do not export variable */
  209.  
  210. char tmplbuf[MAX_LINELEN+1]; /* for temporary uses not thru subroutines. */
  211.  
  212. struct tm *now, Now; /* time of request */
  213. time_t nowtime, limtime, limtimex;
  214. char nowstr[32];
  215.  
  216. /* Resource limits. Capital names are reserved by system. */
  217. int rlimit_cpu=20;  /* cpu time in seconds */
  218. int rlimit_fsize=8388608;/* file size */
  219. int rlimit_as=614457600;/* virtual memory size */
  220. int rlimit_data=204857600;/* data segment size; maxima requires a lot (must be lower than rlimit_as)*/
  221. int rlimit_stack=2097152;/* stack size */
  222. int rlimit_core=0; /* core dump size */
  223. int rlimit_rss=16777216; /* resident size */
  224. int rlimit_nproc=1024; /* number of processes */
  225. int rlimit_nofile=512; /* number of open files */
  226. int rlimit_memlock=2097152;/* locked-in-memory address space */
  227.  
  228. char *var_str; /* malloc'ed buffer to hold translated query_string */
  229.  
  230. /* buffer to hold module's variable definition file, malloc'ed. */
  231. char *var_def_buf;
  232.  
  233.  /* job_identifier is even a reserved variable name */
  234. char job_identifier[32];
  235.  
  236.   /* site manager definition IPv4 IPv6*/
  237. char *manager_site="127.0.0.1 ::1";
  238. int   manager_https=0;
  239.  
  240.  /* sheet and exercise information */
  241. int wims_sheet=0,wims_exo=0;
  242.  
  243.  /* Form method: get or post */
  244. char *default_form_method="post";
  245.  
  246.  /* Je suis maintenant oblige de passer a l'anglais
  247.   * pour la langue de defaut.
  248.   */
  249. char lang[16]="en";
  250. char available_lang[MAX_LANGUAGES][4]={"en","fr"};
  251. int available_lang_no=2;
  252. char pre_language[4]="";
  253. char *protocol="http"; /* http or https */
  254.  
  255.  /* check for coordinate input. This will mean that
  256.   * the request is manual, but not robot.
  257.   */
  258. int coord_input=0;
  259.  
  260. /* These are readonly environment variable names
  261.  * special parm used for special cmds (getins, etc).
  262.  */
  263. char *ro_name[]={
  264.       "cmd" ,
  265.       "empty",
  266.       "lang" ,
  267.       "module" ,
  268.       "session" ,
  269.       "special_parm",
  270.       "special_parm2",
  271.       "special_parm3",
  272.       "special_parm4",
  273.       "useropts" ,
  274.       "wims_session",
  275.       "wims_subsession",
  276.       "wims_window",
  277.       "worksheet"
  278. };
  279. enum {
  280.     ro_cmd, ro_empty, ro_lang, ro_module, ro_session, ro_special_parm,
  281.       ro_special_parm2, ro_special_parm3, ro_special_parm4, ro_useropts, ro_wsession, ro_subsession, ro_win, ro_worksheet
  282. } RO_NAMES;
  283. #define RO_NAME_NO (sizeof(ro_name)/sizeof(ro_name[0]))
  284.  
  285. int cmd_type;
  286. char *commands[]={
  287.     "intro" , "new" , "renew" , "reply" , "config" , "hint" , "help" ,
  288.       "resume", "next", "getins", "getframe", "getfile", "close", "ref"
  289. };
  290. enum {
  291.     cmd_intro, cmd_new, cmd_renew, cmd_reply, cmd_config, cmd_hint, cmd_help,
  292.       cmd_resume, cmd_next, cmd_getins, cmd_getframe, cmd_getfile, cmd_close,
  293.       cmd_ref
  294. }COMMANDS;
  295. #define CMD_NO (sizeof(commands)/sizeof(commands[0]))
  296.  
  297. /* stat=0: saved variables
  298.  * all names starting with wims_priv_ are also internal.
  299.  */
  300. struct {
  301.     char *name; int stat;
  302. } internal_name[]={
  303.       {"accessright", 1}, /* right to access commercial resources */
  304.       {"caller", 1}, /* caller session */
  305.       {"check", 1}, /* for exam check use */
  306.       {"class", 1},
  307.       {"class_examlog", 1},
  308.       {"class_exolog", 1},
  309.       {"class_limit", 1},
  310.       {"class_quota", 1},
  311.       {"class_regpass", 1},
  312.       {"class_user_limit", 1},
  313.       {"classdir", 1},
  314.       {"classname", 1},
  315.       {"devel_modules", 1},
  316.       {"developer", 1},
  317.       {"doc_quota", 1},
  318.       {"doc_regpass", 1},
  319.       {"email", 1},
  320.       {"exo", 0}, /* exercise number */
  321.       {"exoption", 1}, /* exercise option */
  322.       {"firstname", 1},
  323.       {"forum_limit", 1},
  324.       {"home", 1},
  325.       {"institutionname", 1},
  326.       {"isexam", 0}, /* whether the sheet is an exam sheet */
  327.       {"ismanager", 0},
  328.       {"lastname", 1},
  329.       {"mode", 0}, /* operating mode */
  330.       {"module_start_time", 0},
  331.       {"now", 1}, /* date and time, yyyymmdd.hh:mm:ss */
  332.       {"nowseconds", 1}, /* date and time, seconds since EPOCH */
  333.       {"nr", 1}, /* non-readable variables in user file, words */
  334.       {"nw", 1}, /* non-writable variables in user file, words */
  335.       {"otherclass", 1}, /* Remember other logins */
  336.       {"participate", 1}, /* superclass definition */
  337.       {"prefix", 1}, /* user file prefix */
  338.       {"protocol", 0}, /* http protocol */
  339.       {"rafale", 0}, /* rapidfire request information */
  340.       {"readable", 1}, /* readable variables in user file, words */
  341.       {"realuser", 1}, /* real user for supervisor in gateway */
  342.       {"req_time", 0}, /* time of the request */
  343.       {"sclassdir", 1},
  344.       {"scorereg", 0}, /* score registration flag */
  345.       {"sequence", 0}, /*sequence number */
  346.       {"sescookie", 1}, /* session cookie */
  347.       {"sesdir", 1},
  348.       {"session_serial", 0}, /* request serial in the session */
  349.       {"session_start_time", 0},
  350.       {"sheet", 0}, /* sheet number */
  351.       {"sup_secure", 1}, /* secure level of supervisor */
  352.       {"superclass", 1}, /* superclass code */
  353.       {"superclass_quota", 1},
  354.       {"supertype", 1}, /* superclass type */
  355.       {"supervise", 1}, /* superclass definition */
  356.       {"supervisor", 1}, /* real name of the supervisor */
  357.       {"supervisormail",1}, /* email of supervisor */
  358.       {"trustfile", 1}, /* trusted files in special adm modules */
  359.       {"useropts", 1}, /* user options */
  360.       {"writable", 1}, /* writable variables in user file, words */
  361. };
  362. #define INTERNAL_NAME_NO (sizeof(internal_name)/sizeof(internal_name[0]))
  363.  
  364. char *httpd_vars[]={
  365.       "HTTP_ACCEPT",
  366.       "HTTP_ACCEPT_CHARSET",
  367.       "HTTP_ACCEPT_LANGUAGE",
  368.       "HTTP_COOKIE",
  369.       "HTTP_HOST",
  370.       "HTTP_USER_AGENT",
  371.       "HTTPS",
  372.       "QUERY_STRING",
  373.       "REMOTE_HOST",
  374.       "REMOTE_ADDR",
  375.       "REMOTE_PORT",
  376.       "REQUEST_METHOD",
  377.       "SCRIPT_NAME",
  378.       "SERVER_NAME",
  379.       "SERVER_SOFTWARE",
  380.       "SERVER_PROTOCOL"
  381. };
  382. #define HTTPD_VAR_NO (sizeof(httpd_vars)/sizeof(httpd_vars[0]))
  383.  
  384. /* security: these variables will not be visible to child processes */
  385. char *unsetvars[]={
  386.  "DOCUMENT_ROOT","SERVER_SIGNATURE","SERVER_SOFTWARE",
  387.       "UNIQUE_ID","HTTP_KEEP_ALIVE","SSL_SESSION_ID"
  388. };
  389. #define unsetvarcnt (sizeof(unsetvars)/sizeof(unsetvars[0]))
  390.  
  391. enum {httpd_apache, httpd_wims};
  392. int httpd_type=httpd_apache;
  393.  
  394. char *remote_addr=""; /* storing for performance */
  395. char *remote_host="";
  396.  
  397. char ref_name[2048], ref_base[2048];
  398.  
  399. void put_special_page(char *pname);
  400. void useropts(void);
  401.  
  402. #include "lines.c"
  403. #include "var.c"
  404. #include "config.c"
  405. #include "cleaning.c"
  406. #include "files.c"
  407. #include "pedia.c"
  408. #include "evalue.c"
  409. #include "test.c"
  410. #include "compare.c"
  411. #include "html.c"
  412. #include "mathml.c"
  413. #include "texmath.c"
  414. #include "rawmath.c"
  415. #include "insmath.c"
  416. #include "matrix.c"
  417. #include "score.c"
  418. #include "calc.c"
  419. #include "exec.c"
  420. #include "auth.c"
  421. #include "variables.c"
  422. #include "exam.c"
  423. #include "log.c"
  424.  
  425. /* Make certain httpd variables readable by modules */
  426. void take_httpd_vars(void)
  427. {
  428.     int i;
  429.     char *p, buf[MAX_NAMELEN+1];
  430.     var_noexport=1;
  431.     for(i=0;i<HTTPD_VAR_NO;i++) {
  432.      snprintf(buf,sizeof(buf),"httpd_%s",httpd_vars[i]);
  433.      if((p=getenv(httpd_vars[i]))!=NULL) setvar(buf,p);
  434.     }
  435.     var_noexport=0;
  436.  
  437.     for(i=0;i<unsetvarcnt;i++) unsetenv(unsetvars[i]);
  438.      /* IPv4 IPv6*/
  439.     p=getenv("REMOTE_ADDR");if(p!=NULL && (strcmp(p,"127.0.0.1")==0 || strcmp(p,"::1")==0)) human_access=1;
  440.     p=getenv("HTTP_REFERER"); if(p!=NULL && *p!=0) setvar("wims_referer",p);
  441. }
  442.  
  443. void cookie2session(void)
  444. {
  445.     char cksession[64], psession[32], *ckey, *p;
  446.     char nbuf[MAX_FNAME+1];
  447.  
  448.     if(mode==mode_popup) return;
  449.     if(cookiegot[0]==0) {
  450.      ckset: cookiegot[0]=0; setcookie=1; return;
  451.     }
  452.     p=getvar("special_parm");
  453.     if(p!=NULL && strcmp(p,"ignorecookie")==0) return;
  454.     mystrncpy(cksession,cookiegot,sizeof(cksession));
  455.     ckey=strchr(cksession,'-');
  456.     if(ckey==NULL) goto ckset; else *ckey++=0;
  457.     p=getvar("wims_session"); if(p==NULL) p="";
  458.     if(strstr(p,"new")!=NULL) goto ckset;
  459.     mystrncpy(psession,p,sizeof(psession));
  460.     p=strchr(psession,'_'); if(p!=NULL) *p=0;
  461.     if(psession[0]!=0) {
  462.       if(strcmp(psession,cksession)==0) return;
  463.       if(session_exists(psession)) goto ckset;
  464.       if(session_exists(cksession)) goto change;
  465.     }
  466.     else {
  467.       if(!session_exists(cksession)) return;
  468.       change:
  469.       p=getenv("HTTPS");
  470.       if(p!=NULL && strcasecmp(p,"on")==0) goto ckset;
  471.       mkfname(nbuf,"%s/%s/var",session_dir,cksession);
  472.       getdef(nbuf,"w_wims_ismanager",tmplbuf);
  473.       if(tmplbuf[0]!=0 && tmplbuf[0]!='0') goto ckset;
  474.       getdef(nbuf,"w_wims_protocol",tmplbuf);
  475.       if(strcasecmp(tmplbuf,"https")==0) goto ckset;
  476.       mkfname(nbuf,"%s/%s/var.stat",session_dir,cksession);
  477.       getdef(nbuf,"wims_user",tmplbuf);
  478.       if(tmplbuf[0]!=0) goto ckset;
  479.       force_setvar(ro_name[ro_session],cksession);
  480.       setsesdir(cksession);
  481.       force_setvar("wims_subsession","");
  482.       session_serial=0;
  483.     }
  484. }
  485.  
  486. void determine_font(char *l)
  487. {
  488.     int i;
  489.  
  490.     if(l==NULL || *l==0) return;
  491.     for(i=0;i<charname_no && memcmp(charname[i].name,l,2);i++);
  492.     if(i<charname_no) setvar("wims_main_font",charname[i].font);
  493. }
  494.  
  495. void determine_dirn(char *l)
  496. {
  497.     int i;
  498.  
  499.     if(l==NULL || *l==0) return;
  500.     for(i=0;i<dirnname_no && memcmp(dirnname[i].name,l,2);i++);
  501.     if(i<dirnname_no) setvar("wims_main_dirn",dirnname[i].dirn);
  502. }
  503.  
  504. void predetermine_language(void)
  505. {
  506.     char *p;
  507.     int i,n;
  508.  
  509.     if(pre_language[0]!=0) p=pre_language;
  510.     else p=getenv("HTTP_ACCEPT_LANGUAGE");
  511.     if(p!=NULL && strlen(p)>=2) {
  512.       for(i=0;i<available_lang_no && memcmp(p,available_lang[i],2)!=0;i++);
  513.       if(i<available_lang_no) goto lend;
  514.     }
  515.     p=getenv("HTTP_USER_AGENT");
  516.     if(p!=NULL && strlen(p)>=5) {
  517.       char *q;
  518.       if((q=strchr(p,'['))!=NULL && islower(*(q+1)) && islower(*(q+2)) && *(q+3)==']') {
  519.           char bb[4];
  520.           bb[0]=*(q+1);bb[1]=*(q+2);bb[2]=0;
  521.           for(i=0;i<available_lang_no && memcmp(bb,available_lang[i],2)!=0;i++);
  522.           if(i<available_lang_no) {
  523.             memmove(lang,bb,2); lang[2]=0;
  524.             goto lend2;
  525.           }
  526.       }
  527.     }
  528.     p=getenv("HTTP_HOST"); if(p==NULL) goto lend2;
  529.     n=strlen(p); if(n<=3 || *(p+n-3)!='.') goto lend2;
  530.     p=p+n-2;
  531.     for(i=0;i<available_lang_no && memcmp(p,available_lang[i],2)!=0;i++);
  532.     if(i<available_lang_no) {
  533.       lend: memmove(lang,p,2); lang[2]=0;
  534.       lend2: determine_font(lang);determine_dirn(lang);
  535.     }
  536. }
  537.  
  538. /* print a special page */
  539. void put_special_page(char *pname)
  540. {
  541.     determine_font(lang);
  542.     determine_dirn(lang);
  543.     phtml_put_base(mkfname(NULL,"%s.phtml.%s",pname,lang),0);
  544.     write_logs();free(var_str);
  545. }
  546.  
  547. /* check whether the connection is a site manager. */
  548. void manager_check(void)
  549. {
  550.     char *p, *pp, buf[16];
  551.     struct stat confstat;
  552.     int i;
  553.  
  554.     manageable=0;
  555.     if(robot_access || *manager_site==0 || checkhost(manager_site)<1)
  556.       goto mend;
  557.     if(manager_https) {
  558.       p=getenv("HTTPS");
  559.       if(p==NULL || strcmp(p,"on")!=0) goto mend;
  560.     }
  561.  /* IPv4 IPv6*/
  562.     if(strcmp(remote_addr,"127.0.0.1")==0 || strcmp(remote_addr,"::1")==0) {
  563.       int port, port2;
  564.       char tester[128];
  565.       p=getenv("REMOTE_PORT"); if(p==NULL) goto mend;
  566.       port=atoi(p); if(port<1024 || port>65535) goto mend;
  567.       p=getenv("SERVER_PORT"); if(p==NULL) goto mend;
  568.       port2=atoi(p); if(port2>=10000 || port2<=0) goto mend;
  569. /* this is very non-portable */
  570.       manageable=1;
  571.       accessfile(tmplbuf,"r","/proc/net/tcp");
  572.       snprintf(tester,sizeof(tester)," 0100007F:%04X 0100007F:%04X ",
  573.              port,port2);
  574.       p=strstr(tmplbuf,tester);
  575.       if(p!=NULL) {
  576.           pp=strchr(p,'\n'); if(pp!=NULL) *pp=0;
  577.           if(strlen(p)>=75) {
  578.             p=find_word_start(p+70); *find_word_end(p)=0;
  579.             if(atoi(p)==geteuid()) manageable=2;
  580.           }
  581.       }
  582.     }
  583.     else manageable=1;
  584.     i=stat(config_file,&confstat);
  585.     if(i==0 && manageable>0 && (confstat.st_mode&(S_IRWXO|S_IRWXG))!=0) manageable=-1;
  586.     if(manageable>0 && !trusted_module()) manageable=0;
  587.     if(manageable==1) {
  588.       accessfile(tmplbuf,"r","%s/.manager",session_prefix);
  589.       if(strstr(tmplbuf,"yes")!=NULL) manageable=2;
  590.     }
  591.     if(manageable==1) {
  592.       p=getvar(ro_name[ro_module]);
  593.       if(p!=NULL && strncmp(p,"adm/manage",strlen("adm/manage"))==0) {
  594.           struct stat pstat;
  595.           if(stat("../log/.wimspass",&pstat)==0) {
  596.             if((S_IFMT&pstat.st_mode)!=S_IFREG ||
  597.                ((S_IRWXO|S_IRWXG)&pstat.st_mode)!=0)
  598.               manageable=-2;
  599.           }
  600.       }
  601.     }
  602.     mend:
  603.     mystrncpy(buf,int2str(manageable),sizeof(buf));
  604.     force_setvar("wims_ismanager",buf);
  605.     if(manageable>=2) {
  606.       struct rlimit rlim;
  607.       rlimit_cpu*=10;
  608.       rlim.rlim_cur=rlim.rlim_max=rlimit_cpu;
  609.       setrlimit(RLIMIT_CPU,&rlim);
  610.       mystrncpy(buf,int2str(rlimit_cpu),sizeof(buf));
  611.       setvar("wims_cpu_limit",buf);
  612.       initalarm();
  613.     }
  614. }
  615.  
  616. /* check for robot access */
  617. void robot_check(void)
  618. {
  619.     char *ua, *p, *ses, *c, *mod;
  620.     int i;
  621.  
  622.     if(human_access) return;
  623.     mod=getvar(ro_name[ro_module]);
  624.     if(mod!=NULL && strcmp(mod,"adm/raw")==0) return;
  625.     ses=getvar(ro_name[ro_session]);
  626. /* user has valid session; OK */
  627.     if(ses!=NULL && strncmp(ses,robot_session,strlen(robot_session))!=0
  628.        && strchr(ses,'/')==NULL
  629.        && ftest(mkfname(NULL,"%s/%s",s2_dir,ses))==is_dir)
  630.       return;
  631.     ua=getenv("HTTP_USER_AGENT"); if(ua==NULL) ua="";
  632.     ua=find_word_start(ua);
  633.     if(strncasecmp(ua,"Mozilla",strlen("Mozilla"))==0 &&
  634.        (p=strstr(ua,"compatible"))!=NULL)
  635.       ua=find_word_start(find_word_end(p));
  636.     if(*ua) {
  637.       for(i=0;i<good_agent_no
  638.           && strncasecmp(ua,good_agent[i],strlen(good_agent[i]));i++);
  639.       if(i<good_agent_no) return;
  640.       for(i=0;i<bad_agent_no
  641.           && strstr(ua,bad_agent[i])==NULL;i++);
  642.       if(i<bad_agent_no) user_error("trapped");
  643.     }
  644.     force_setvar(ro_name[ro_session],robot_session);
  645.     setsesdir(robot_session);
  646.     c=getvar(ro_name[ro_cmd]);
  647.     robot_access=1;
  648.     if(c!=NULL && strcmp(c,"new") && strcmp(c,"intro")) {
  649.       force_setvar(ro_name[ro_cmd],"robot_error");
  650.       nph_header(450); put_special_page("robot");
  651.       flushoutput(); flushlog(); exit(0);
  652.     }
  653. }
  654.  
  655. /* type=0: ordinary; type=1: multipart/form-data */
  656. void parse_query_string(int len, int type)
  657. {
  658.     int i,j,l,v,cmd_defined;
  659.     int parenth=-1, ll, lb, dlen;
  660.     char *start, *p, *p1, *pt, *b1="";
  661.  
  662.     cmd_defined=0;
  663.     setvar("wims_subsession","");
  664.     ll=lb=0;
  665.     if(type) {
  666.       ll=strlen(mpboundary);
  667.       start=strstr(var_str,mpboundary);
  668.       if(start==NULL) start=var_str+strlen(var_str);
  669.       if(strstr(var_str,"\r\n\r\n")!=NULL) b1="\r\n\r\n";
  670.       else b1="\n\n";
  671.       lb=strlen(b1);
  672.     }
  673.     else start=var_str;
  674.     for(v=0, p1=start;p1<var_str+len;p1+=l) {
  675.       if(type) {
  676.           char *p2, *p3, *p4, *p5;
  677.           p2=p1+ll; p3=memstr(p2,mpboundary,var_str+len-p2); l=p3-p1;
  678.           p=memstr(p2,b1,var_str+len-p2); if(p>=p3) continue;
  679.           p+=lb; if(p3<var_str+len) {
  680.             while(*p3!='\n' && p3>p2) p3--; *p3=0;
  681.             p3--; if(*p3=='\r') *p3=0;
  682.           }
  683.           dlen=p3-p;
  684.           p2=memstr(p2,"name=",p3-p2); if(p2>=p3) continue;
  685.           p2+=strlen("name="); if(*p2=='"') p2++;
  686.           for(p3=p2; myisalnum(*p3) || strchr("._",*p3)!=NULL; p3++);
  687.           if(p3==p2) continue;
  688.           if(p3-p2==strlen("wims_deposit") &&
  689.              strncmp(p2,"wims_deposit",p3-p2)==0) {
  690.             p4=memstr(p1,"filename=",p-p1); if(p4<p) {
  691.                 p4+=strlen("filename="); if(*p4=='"') {
  692.                   p4++; p5=strchr(p4,'"');
  693.                   if(p5==NULL || p5-p4>=MAX_FNAME) goto emptyquote;
  694.                 }
  695.                 else {
  696.                   emptyquote:
  697.                   for(p5=p4; p5<p && !isspace(*p5) &&
  698.                       strchr(";\"~#*?=,'",*p5)==NULL; p5++);
  699.                 }
  700.                 if(p5>p4) {
  701.                   *p5=0;
  702.                   for(p5--;
  703.                       p5>=p4 && !isspace(*p5) && strchr("/\\:",*p5)==NULL;
  704.                       p5--);
  705.                   if(p5>=p4) p4=p5+1; if(*p4==0) goto noname;
  706.                   if(strstr(p4,"..")!=NULL || *p4=='.')
  707.                     p4="noname.file";
  708.                   setvar("wims_deposit",p4);
  709.                 }
  710.                 else {
  711.                   noname: setvar("wims_deposit","noname.file");
  712.                 }
  713.             }
  714.             deplen=dlen;
  715.           }
  716.           *p3=0; l-=p2-p1; p1=p2;
  717.       }
  718.       else {
  719.           p1=find_word_start(p1);
  720.           l=strlen(p1)+1; p=strchr(p1,'=');
  721.           if(p==NULL) p=p1+strlen(p1);
  722.           if(*p==0 && l>1) {
  723.             user_variable[v].name="no_name";
  724.             user_variable[v].value=p1;
  725.             coord_input=1;
  726.             goto nnext;
  727.           }
  728.           *p++=0;
  729.       }
  730. /* empty name or empty value: ignore */
  731.         if(*p1==0 || *p==0) continue;
  732. /* We do not treat names containing '.' */
  733.       for(pt=strchr(p1,'.'); pt; pt=strchr(++pt,'.')) *pt='_';
  734. /* Restrictions on variable names */
  735.       for(pt=p1; myisalnum(*pt) || *pt=='_'; pt++);
  736.       if(*pt) continue;
  737.       if(strcmp(p1,"wims_deposit")!=0) _tolinux(p);
  738. /* This is a restriction:
  739.  * Every parameter must have matching parentheses.
  740.  */
  741.       if(parenth==-1 && strncmp(p1,"freepar_",strlen("freepar_"))!=0
  742.          && strcmp(p1,"wims_deposit")!=0
  743.          && check_parentheses(p,1)) parenth=v;
  744.       if(strcmp(p1,"special_parm")==0 && strcmp(p,"wims")==0)
  745.         human_access=1;
  746.       j=search_list(ro_name,RO_NAME_NO,sizeof(ro_name[0]),p1);
  747.       if(j>=0) {
  748.           if(j==ro_session) {
  749.             p=find_word_start(p); *find_word_end(p)=0;
  750.             if(strlen(p)>MAX_SESSIONLEN) continue;
  751.             if(strcmp(p,robot_session)==0) p="";
  752.             if(strcasecmp(p,"popup")==0) {
  753.                 mode=mode_popup;
  754.                 force_setvar("wims_mode","popup");
  755.                 force_setvar("session","");
  756.                 continue;
  757.             }
  758.           }
  759.           if(j==ro_module) module_defined=1;
  760.           if(j==ro_cmd) {
  761.             p=find_word_start(p); *find_word_end(p)=0;
  762.             if(strlen(p)>16) continue;
  763.             cmd_defined=1;
  764.           }
  765.           if(j==ro_lang) {
  766.             if(strlen(p)!=2) continue;
  767.             for(i=0;i<available_lang_no && strcmp(available_lang[i],p)!=0;i++);
  768.             if(i<available_lang_no) {user_lang=1; ovlstrcpy(lang,p);}
  769.             else continue;
  770.           }
  771. /* strip leading and trailing '/'s in module name */
  772.           if(j==ro_module) {
  773.             p=find_word_start(p); *find_word_end(p)=0;
  774.             while(*p=='/') p++;
  775.             while(*p!=0 && *(p+strlen(p)-1)=='/') *(p+strlen(p)-1)=0;
  776.             if(strlen(p)>MAX_MODULELEN) continue;
  777.           }
  778.           setvar(p1,p);
  779.           if(j==ro_session && mode!=mode_popup) {
  780.             char *pp, *pr;
  781.             char buf[1024];
  782.             mystrncpy(buf,p,sizeof(buf));
  783.             if((pp=strchr(buf,'.'))!=NULL) {
  784.                 *pp++=0; session_serial=atoi(pp);
  785.                 if(pp<0) pp=0;
  786.             }
  787.             else session_serial=0;
  788.             pp=strchr(buf,'_');
  789.             if(pp!=NULL && (pr=strstr(pp,"_mhelp"))!=NULL) {
  790.                 *pr=0; ismhelp=1; lastout_file=-1;
  791.                 setvar("wims_inssub","mh");
  792.             }
  793.             force_setvar("wims_session",buf);
  794.             if(pp!=NULL) force_setvar("wims_subsession",pp);
  795.           }
  796.           continue;
  797.       }
  798.       user_variable[v].name=p1;
  799.       user_variable[v].value=p;
  800. nnext:v++; if(v>=MAX_VAR_NUM) user_error("too_many_variables");
  801.     }
  802.     user_var_no=v;
  803.     if(parenth>=0) {
  804.       char buf[16];
  805.       mystrncpy(buf,int2str(user_var_no),sizeof(buf));
  806.       setvar("user_var_no",buf);
  807.       for(i=0;i<user_var_no;i++) {
  808.           snprintf(buf,sizeof(buf),"name%d",i);
  809.           setvar(buf,user_variable[i].name);
  810.           snprintf(buf,sizeof(buf),"value%d",i);
  811.           setvar(buf,user_variable[i].value);
  812.       }
  813.       mystrncpy(buf,int2str(parenth),sizeof(buf));
  814.       setvar("bad_parentheses",buf);
  815.       user_error("unmatched_parentheses");
  816.     }
  817.     p=getenv("SCRIPT_NAME");
  818.     if(p!=NULL && (p=strstr(p,"/getfile/"))!=NULL) {
  819.       p+=strlen("/getfile/");
  820.       force_setvar(ro_name[ro_cmd],commands[cmd_getfile]);
  821.       force_setvar("special_parm",p);
  822.       cmd_defined=1;
  823.     }
  824.     if(module_defined && !cmd_defined) setvar(ro_name[ro_cmd],commands[cmd_intro]);
  825.     robot_check(); cookie2session();
  826. }
  827.  
  828. /* parse special commands */
  829. void special_cmds(void)
  830. {
  831.     char *c, *p;
  832.     int i;
  833.     long int l=-1;
  834.  
  835.     c=getvar(ro_name[ro_cmd]);
  836.     if(c==NULL || *c==0) {  /* no module name nor command */
  837.       setvar(ro_name[ro_module],home_module);
  838.       setvar(ro_name[ro_cmd],commands[cmd_new]);
  839.       return;
  840.     }
  841.     for(i=0;i<CMD_NO && strcmp(commands[i],c)!=0; i++);
  842.     switch(i) {
  843.       case cmd_intro: {
  844.           set_module_prefix();
  845.           default_form_method="get";
  846.           if(ftest(mkfname(NULL,"%s/%s",module_prefix,intro_file))<0) {
  847.             force_setvar(ro_name[ro_cmd],commands[cmd_new]);
  848.             return;
  849.           }
  850.           p=getvar("wims_session");
  851.           if(p!=NULL && *p!=0) {
  852.             if(set_session_prefix()==0) check_session();
  853.             else {
  854.                 trap_check(p);
  855.                 if(strchr(p,'_')!=NULL && strchr(p,'/')==NULL) {
  856.                   get_static_session_var();
  857.                 }
  858.             }
  859.           }
  860. /* determine http protocol name. How to detect? */
  861.           p=getenv("HTTPS"); if(p!=NULL && strcmp(p,"on")==0) {
  862.             protocol="https"; set_protocol();
  863.           }
  864.           force_setvar("wims_protocol",protocol);
  865.           determine_font(lang);
  866.           determine_dirn(lang);
  867.           main_phtml_put(intro_file); debug_output();
  868.           introend: write_logs();free(var_str);
  869.           delete_pid(); exit(0);
  870.       }
  871.       case cmd_ref: {
  872.           set_module_prefix();
  873.           default_form_method="get";
  874.           p=getvar("wims_session");
  875.           if(p!=NULL && *p!=0) {
  876.             if(set_session_prefix()==0) check_session();
  877.             else trap_check(p);
  878.           }
  879.           determine_font(lang);
  880.           determine_dirn(lang);
  881.           main_phtml_put(ref_file); goto introend;
  882.       }
  883.       case cmd_getins: {
  884.           c=getvar(ro_name[ro_special_parm]);
  885.           if(c==NULL || *c==0) {
  886.             user_error_nolog=1; user_error("no_insnum");
  887.           }
  888.           if(*c=='/' || strstr(c,"..")!=NULL) goto badins;
  889.           set_session_prefix();
  890.           if(strstr(session_prefix,"robot")!=NULL) exit(0);
  891.           l=filelength("%s/%s",s2_prefix,c);
  892.           if(l<0) {
  893.             badins: user_error_nolog=1; user_error("bad_insnum");
  894.           }
  895.           {
  896.             char *fmt;
  897.             fmt=strchr(c,'.');
  898.             if(fmt==NULL) {
  899.                 user_error_nolog=1; user_error("bad_insnum");
  900.             }
  901.             else fmt++;
  902.  
  903.             nph_header(200);
  904. /* insert format problem; bricolage */
  905.             printf("Content-type: image/%s\r\n\
  906. Content-length: %ld\r\n\r\n",fmt,l);
  907.             catfile(stdout,"%s/%s",s2_prefix,c); exit(0);
  908.           }
  909.       }
  910.       case cmd_getfile: {
  911.           char fname[MAX_FNAME+1];
  912.           c=getvar(ro_name[ro_special_parm]);
  913.           if(c==NULL || *c==0) {
  914.             user_error_nolog=1; user_error("no_insnum");
  915.           }
  916.           if(*c=='/' || strstr(c,"..")!=NULL) goto badfile;
  917.  
  918.           set_session_prefix();
  919.           if(strstr(session_prefix,"robot")!=NULL) exit(0);
  920.           mkfname(fname,"%s/getfile/%s",session_prefix,c);
  921.           l=filelength("%s",fname);
  922.           if(l<0 && strchr(session_prefix,'_')!=NULL) {
  923.             char *pt;
  924.             mystrncpy(fname,session_prefix,sizeof(fname));
  925.             pt=strrchr(fname,'_'); if(pt) *pt=0;
  926.             snprintf(fname+(pt-fname),sizeof(fname)-(pt-fname),
  927.                   "/getfile/%s",c);
  928.             l=filelength("%s",fname);
  929.           }
  930.           if(l<0) {
  931.             badfile: user_error_nolog=1; user_error("bad_insnum");
  932.           }
  933.           if(l>512*1024) {
  934.             struct rlimit rlim;
  935.             rlimit_cpu*=l/(10*1024);
  936.             rlim.rlim_cur=rlim.rlim_max=rlimit_cpu;
  937.             initalarm();
  938.           }
  939.           {
  940.             char *p1;
  941.             char mime[MAX_LINELEN+1];
  942.             for(p1=c+strlen(c);p1>c && isalpha(*(p1-1)); p1--);
  943.             ovlstrcpy(mime,"application/octet-stream");
  944.             if(p1>c && *(p1-1)=='.') {
  945.                 setvar("translator_unknown",mime);
  946.                 setvar("dictionary","bases/sys/mime");
  947.                 snprintf(mime,sizeof(mime),"translator %s",p1);
  948.                 calc_exec(mime);
  949.             }
  950.             nph_header(200);
  951.             printf("Content-type: %s\r\n\
  952. Content-length: %ld\r\n\r\n",mime,l);
  953.             catfile(stdout,"%s",fname); exit(0);
  954.           }
  955.       }
  956.       case cmd_close: {
  957.           char *p, b2[32]; int w;
  958.           char nbuf[MAX_FNAME+1], vbuf[MAX_LINELEN+1];
  959.           p=getvar(ro_name[ro_session]);
  960.           if(p==NULL || strlen(p)<10 ||
  961.              strchr(p,'/')!=NULL) return;
  962.           mystrncpy(b2,p,sizeof(b2));
  963.           p=strchr(b2,'.'); if(p!=NULL) *p=0;
  964.           mkfname(nbuf,"%s/%s/var.stat",session_dir,b2);
  965.           getdef(nbuf,"wims_caller",vbuf);
  966.           if(vbuf[0]!=0) force_setvar(ro_name[ro_session],vbuf);
  967.           w=wrapexec; wrapexec=1;
  968.           call_sh("rm -Rf %s/%s* %s/%s* >/dev/null 2>&1",session_dir,b2,s2_dir,b2);
  969.           wrapexec=w; cookiegot[0]=0;
  970.           force_setvar(ro_name[ro_cmd],"new");
  971.       }
  972.       default: return;
  973.     }
  974. }
  975.  
  976. /* This is run only when manually invoking the program.
  977.  * Verifies the orderedness of various list tables.
  978.  */
  979. int verify_tables(void)
  980. {
  981.     if(verify_order(calc_routine,CALC_FN_NO,sizeof(calc_routine[0]))) return -1;
  982.     if(verify_order(exec_routine,EXEC_FN_NO,sizeof(exec_routine[0]))) return -1;
  983.     if(verify_order(main_config,MAIN_CONFIG_NO,sizeof(main_config[0]))) return -1;
  984.     if(verify_order(mathname,mathname_no,sizeof(mathname[0]))) return -1;
  985.     if(verify_order(hmname,hmname_no,sizeof(hmname[0]))) return -1;
  986.     if(verify_order(ro_name,RO_NAME_NO,sizeof(ro_name[0]))) return -1;
  987.     if(verify_order(distr_cmd,distr_cmd_no,sizeof(distr_cmd[0]))) return -1;
  988.     if(verify_order(internal_name,INTERNAL_NAME_NO,
  989.                 sizeof(internal_name[0]))) return -1;
  990.     if(verify_order(tmathfn,tmathfn_no,sizeof(tmathfn[0]))) return -1;
  991.     if(verify_order(tmathvar,tmathvar_no,sizeof(tmathvar[0]))) return -1;
  992.     if(verify_order(modindex,MODINDEX_NO,sizeof(modindex[0]))) return -1;
  993.     if(verify_order(exportvars,exportvarcnt,sizeof(exportvars[0]))) return -1;
  994.     if(evaltab_verify()) return -1;
  995.     if(textab_verify()) return -1;
  996.     return 0;
  997. }
  998.  
  999. void config_defaults(void)
  1000. {
  1001.     int i;
  1002.     for(i=0;i<MAIN_CONFIG_NO;i++) {
  1003.       if((1&main_config[i].is_integer)==1) {
  1004.           int *pi = (int*)main_config[i].address;
  1005.           printf("DF_%s=%d\n",main_config[i].name, *pi);
  1006.         }
  1007.       else {
  1008.             char **ps = (char**)main_config[i].address;
  1009.           printf("DF_%s=%s\n",main_config[i].name,*ps);
  1010.         }
  1011.     }
  1012. }
  1013.  
  1014. /* get and set useroptions */
  1015. void useropts(void)
  1016. {
  1017.     char *p;
  1018.     setvar("lang",lang);
  1019.     p=getvar("useropts");
  1020.     if(p==NULL || *p==0) p=getvar("wims_useropts");
  1021.     if(p!=NULL && *p!=0) {
  1022.       if(myisdigit(p[0])) {
  1023.         usertexsize=p[0]-'0';
  1024.         /* fourth digit is for special fonts*/
  1025.         if(p[1]!=0) { mathalign_base=p[1]-'0'; }
  1026.       }
  1027.       if(myisdigit(p[3]) && p[3]!=0){ spec_font=p[3]-'0';}
  1028.     }
  1029.     if(mathalign_base==1) {
  1030.         mathalign_sup1="<sup>"; mathalign_sup2="</sup>";
  1031.     } else mathalign_sup1=mathalign_sup2="";
  1032. }
  1033.  
  1034. /* popup module help */
  1035. void mhelp(void)
  1036. {
  1037.     char *p, buf[MAX_LINELEN+1];
  1038.     main_phtml_put(""); buf[0]=0;
  1039.     if(cmd_type!=cmd_help) {
  1040.       phtml_put_base("closemhelp.phtml",0);
  1041.     }
  1042.     else {
  1043.       phtml_put_base("mhelpheader.phtml",0);
  1044.       p=getvar("special_parm");
  1045.       if(p!=NULL && strcmp(p,"about")==0)
  1046.         phtml_put("about.phtml",0);
  1047.       else phtml_put("help.phtml",0); phtml_put_base("mhelptail.phtml",0);
  1048.       exec_tail(buf);
  1049.     }
  1050. }
  1051.  
  1052. #define READSTDIN_WINDOW 4096
  1053.  
  1054. void readstdin(int len)
  1055. {
  1056.     int ll, l1, lt, lr;
  1057.     int cpulim;
  1058.  
  1059.     cpulim=rlimit_cpu; rlimit_cpu=3;
  1060.     lr=len; l1=0;
  1061.     while(lr>0) {
  1062.       nowtime=time(0); initalarm();
  1063.       ll=lr; if(ll>READSTDIN_WINDOW) ll=READSTDIN_WINDOW;
  1064.       lt=fread(stdinbuf+l1,1,ll,stdin);
  1065.       if(lt!=ll) user_error("parm_too_long");
  1066.       lr-=ll; l1+=ll;
  1067.     }
  1068.     if(l1!=len) user_error("parm_too_long");
  1069.     stdinbuf[len]=0; rlimit_cpu=cpulim;
  1070. }
  1071.  
  1072. /* input: p=QUERY_STRING. output: parameter length. */
  1073. /* Netscape puts form content into /tmp. */
  1074. int formdata(char *p)
  1075. {
  1076.     char *pp;
  1077.     int inlen;
  1078.     char *ctype;
  1079.     inlen=0; ctype=getenv("CONTENT_TYPE");
  1080.     if(ctype==NULL || strstr(ctype,"multipart/form-data")==NULL
  1081.        || (p=strstr(ctype,"boundary="))==NULL) {
  1082.       bad: stdinbuf=""; return 0;
  1083.     }
  1084.     pp=getenv("CONTENT_LENGTH");
  1085.     if(pp==NULL) goto bad;
  1086.     inlen=atoi(pp); if(inlen<=10) goto bad;
  1087.     if(inlen>=MAX_DEPOSITLEN) user_error("parm_too_long");
  1088.     stdinbuf=xmalloc(inlen+1); readstdin(inlen);
  1089.     p+=strlen("boundary=");
  1090.     for(pp=p;myisalnum(*pp) || *pp=='-'; pp++);
  1091.     if(pp-p<sizeof(mpboundary)-2) {
  1092.       memmove(mpboundary,p,pp-p); mpboundary[pp-p]=0;
  1093.     }
  1094. /* empty data */
  1095.     if(strstr(stdinbuf,mpboundary)==NULL || strstr(stdinbuf,"name=")==NULL) {
  1096.       free(stdinbuf); goto bad;
  1097.     }
  1098.     form_access=1; post_log();
  1099.     return inlen;
  1100. }
  1101.  
  1102. /* get the content of POST */
  1103. void getpost(void)
  1104. {
  1105.     int ll;
  1106.     char *pp;
  1107.     pp=getenv("CONTENT_LENGTH");
  1108.     if(pp==NULL || (ll=atoi(pp))<=0) {
  1109.       stdinbuf=xmalloc(16); stdinbuf[0]=0;
  1110.     }
  1111.     else {
  1112.       if(ll>QUERY_STRING_LIMIT) user_error("parm_too_long");
  1113.       stdinbuf=xmalloc(ll+16); readstdin(ll);
  1114.       if(ll>0) {
  1115.           setenv("QUERY_STRING",stdinbuf,1);
  1116.           form_access=1; post_log();
  1117.       }
  1118.     }
  1119. }
  1120.  
  1121. void buffer_init(void)
  1122. {
  1123.     struct timeval tv;
  1124.  
  1125.     mcachecnt=readnest=0;
  1126.     mpboundary[0]=cookiegot[0]=cookieset[0]=cwdbuf[0]=0;
  1127.     rscore_class[0]=rscore_user[0]=multiexec_random[0]=0;
  1128.     lastftest[0]=0;
  1129.     lastdatafile[0]=0; lastdata=xmalloc(WORKFILE_LIMIT);
  1130.     outptr=outbuf;
  1131.     instex_src[0]=instex_fname[0]=module_prefix[0]=0;
  1132.     examlogf[0]=examlogd[0]=exam_sheetexo[0]=0;
  1133.     stdinbuf=NULL;
  1134.     mkfname(tmp_dir,"../tmp/forall");
  1135.     mkfname(session_dir,"../%s",SESSION_BASE);
  1136.     mkfname(s2_dir,"../%s",S2_BASE);
  1137.     if(gettimeofday(&tv,NULL)) startmtime=startmtime2=0;
  1138.     else {
  1139.       startmtime=((tv.tv_sec%10000)*1000+tv.tv_usec/1000);
  1140.       startmtime2=(tv.tv_sec%1000)*1000000+tv.tv_usec;
  1141.     }
  1142. }
  1143.  
  1144. int main(int argc, char *argv[], char *envp[])
  1145. {
  1146.     char *p, homebuf[MAX_FNAME+1], lbuf[32];
  1147.     int inlen=0;
  1148. /*    int mfd; */
  1149.     error1=user_error; error2=module_error; error3=internal_error;
  1150.     class_dir[0]=0;
  1151.     substitute=substit; buffer_init(); var_init();
  1152. /* WIMS internal locale is always C. */
  1153.     setenv("LANG","C",1); umask(022);
  1154.     setenv("LANGUAGE","us",1);
  1155.     setenv("LC_ALL","C",1);
  1156.     if(argc>1) {
  1157.       if(strcasecmp(argv[1],"table")==0) {
  1158.           if(verify_tables()) internal_error("Table disorder.");
  1159.           else printf("Table orders OK.\n");
  1160.           return 0;
  1161.       }
  1162.       if(strcasecmp(argv[1],"version")==0) {
  1163.           printf("%s",wims_version); return 0;
  1164.       }
  1165.       if(strcasecmp(argv[1],"defaults")==0) {
  1166.           config_defaults(); return 0;
  1167.       }
  1168.     }
  1169.     p=getenv("SERVER_SOFTWARE"); if(p!=NULL && strcasecmp(p,"WIMS")==0)
  1170.       httpd_type=httpd_wims;
  1171.     p=getenv("REMOTE_ADDR"); if(p!=NULL) remote_addr=p;
  1172.     p=getenv("REMOTE_HOST"); if(p!=NULL) remote_host=p;
  1173.     nowtime=time(0); now=localtime(&nowtime);
  1174.     memmove(&Now, now, sizeof(Now)); now=&Now;
  1175.     snprintf(nowstr,sizeof(nowstr),"%04d%02d%02d.%02d:%02d:%02d",
  1176.            (now->tm_year)+1900,(now->tm_mon)+1,now->tm_mday,
  1177.            now->tm_hour,now->tm_min,now->tm_sec);
  1178.     p=getenv("QUERY_STRING");
  1179.     if(p==NULL || *p==0) getpost();
  1180.     else if(strncmp(p,"form-data",9)==0) inlen=formdata(p);
  1181.  
  1182.     force_setvar("wims_now",nowstr);
  1183.     snprintf(lbuf,sizeof(lbuf),"%lu",nowtime);
  1184.     force_setvar("wims_nowseconds",lbuf);
  1185.     nowtime=time(0);
  1186.     initalarm();
  1187.  
  1188.     executed_gotos=insert_no=output_length=0; ins_alt[0]=0;
  1189.     setvar("empty","");       /* lock this variable */
  1190.     setvar("wims_version",wims_version);
  1191.     setvar("wims_version_date",wims_version_date);
  1192.     setvar("wims_main_font","utf-8");
  1193.     take_httpd_vars();
  1194.  
  1195.     main_configure();
  1196.     checklogd();
  1197. /* mfd=shm_open(SHM_NAME,O_RDONLY,-1);
  1198.  * if(mfd==-1) internal_error("Unable to find shared memory.");
  1199.  * shmptr=mmap(0,SHM_SIZE,PROT_READ,MAP_SHARED,mfd,0);
  1200.  * if(shmptr==MAP_FAILED) internal_error("Shared memory failure.");
  1201.  */
  1202.     getppid(); /* this is the first sysmask trigger, must be after checklogd() */
  1203.     predetermine_language();
  1204.     /* modify a few rlimits for 64-bit processors */
  1205.     if (sizeof(long) == 8) {
  1206.       rlimit_as*=2;    /* virtual memory size */
  1207.       rlimit_data*=2;  /* data segment size; maxima requires a lot */
  1208.       rlimit_stack*=2; /* stack size */
  1209.     }
  1210.     set_rlimits();
  1211.     init_random();
  1212.     module_configure();
  1213.     set_job_ident();
  1214.     m_file.name[0]=0;m_file.linecnt=m_file.linepointer=0;
  1215.     p=getenv("QUERY_STRING");
  1216.     if(p==NULL || strlen(p)==0) {
  1217.       setvar("lang",lang);
  1218.       snprintf(homebuf,sizeof(homebuf),"module=%s",home_module);
  1219.       p=homebuf;
  1220.     }
  1221.     if(strlen(p)>=QUERY_STRING_LIMIT) user_error("parm_too_long");
  1222.     if(mpboundary[0]==0) {
  1223.       var_str=xmalloc(strlen(p)+2);
  1224.       parse_query_string(http2env(var_str,p),0);
  1225.     }
  1226.     else {
  1227.       var_str=stdinbuf;
  1228.       parse_query_string(inlen,1);
  1229.     }
  1230.     if(ismhelp) {
  1231.       p=getvar(ro_name[ro_cmd]);
  1232.       if(p==NULL || (strcmp(p,"help")!=0 && strcmp(p,"getins")!=0)) {
  1233.           mhelp(); goto outgo;
  1234.       }
  1235.     }
  1236.     check_exam();
  1237.     useropts();
  1238.     special_cmds();
  1239.     parse_ro_names();
  1240.     manager_check();
  1241.     access_check(0);
  1242.     set_variables();
  1243.     determine_font(getvar("module_language"));
  1244.     determine_dirn(getvar("module_language"));
  1245.     if(!robot_access && session_prefix[0]!=0 && cmd_type!=cmd_help && !ismhelp)
  1246.       lastout_file=creat(mkfname(NULL,"%s/%s",s2_prefix,lastout),
  1247.                    S_IRUSR|S_IWUSR);
  1248.     p=getvar("module_category");
  1249.     if(p==NULL || strstr(p,"tool")==NULL) default_form_method="get";
  1250.     if(noout) {
  1251.       write_logs(); save_session_vars();
  1252.       goto outgo;
  1253.     }
  1254.     if(ismhelp) {
  1255.       mhelp();
  1256.     }
  1257.     else {
  1258.       main_phtml_put(html_file);
  1259.       if(lastout_file!=-1) {
  1260.           flushoutput(); close(lastout_file); putlastout();
  1261.       }
  1262.       write_logs(); save_session_vars();
  1263.     }
  1264.     outgo:
  1265.     debug_output();
  1266.     if(var_str!=stdinbuf) free(var_str);
  1267.     delete_pid();
  1268.     if(mode!=mode_popup && trusted_module()) {
  1269.       p=getvar("wims_mode");
  1270.       if(p!=NULL && strcmp(p,"popup")==0) mode=mode_popup;
  1271.     }
  1272.     if(mode==mode_popup && insert_no==0) {
  1273.       p=getvar("wims_mode");
  1274.       if(p!=NULL && strcmp(p,"popup")==0) {
  1275.           remove_tree(session_prefix);
  1276.  
  1277.           remove_tree(s2_prefix);
  1278.       }
  1279.     }
  1280.     return 0;
  1281. }
  1282.