Subversion Repositories wimsdev

Rev

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