Subversion Repositories wimsdev

Rev

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