Subversion Repositories wimsdev

Rev

Rev 5200 | Rev 5523 | 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=614457600;/* virtual memory size */
  171. int rlimit_data=204857600;/* data segment size; maxima requires a lot (must be lower than rlimit_as)*/
  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 IPv4 IPv6*/
  188. char *manager_site="127.0.0.1 ::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.       "special_parm3",
  220.       "special_parm4",
  221.       "useropts" ,
  222.       "wims_session",
  223.       "wims_subsession",
  224.       "wims_window",
  225.       "worksheet"
  226. };
  227. enum {
  228.     ro_cmd, ro_empty, ro_lang, ro_module, ro_session, ro_special_parm,
  229.       ro_special_parm2, ro_special_parm3, ro_special_parm4, ro_useropts, ro_wsession, ro_subsession, ro_win, ro_worksheet
  230. } RO_NAMES;
  231. #define RO_NAME_NO (sizeof(ro_name)/sizeof(ro_name[0]))
  232.  
  233. int cmd_type;
  234. char *commands[]={
  235.     "intro" , "new" , "renew" , "reply" , "config" , "hint" , "help" ,
  236.       "resume", "next", "getins", "getframe", "getfile", "close", "ref"
  237. };
  238. enum {
  239.     cmd_intro, cmd_new, cmd_renew, cmd_reply, cmd_config, cmd_hint, cmd_help,
  240.       cmd_resume, cmd_next, cmd_getins, cmd_getframe, cmd_getfile, cmd_close,
  241.       cmd_ref
  242. }COMMANDS;
  243. #define CMD_NO (sizeof(commands)/sizeof(commands[0]))
  244.  
  245.         /* stat=0: saved variables */
  246.         /* all names starting with wims_priv_ are also internal. */
  247. struct {
  248.     char *name; int stat;
  249. } internal_name[]={
  250.       {"accessright",   1},     /* right to access commercial resources */
  251.       {"caller",        1},     /* caller session */
  252.       {"check",         1},     /* for exam check use */
  253.       {"class",         1},
  254.       {"class_examlog", 1},
  255.       {"class_exolog",  1},
  256.       {"class_limit",   1},
  257.       {"class_quota",   1},
  258.       {"class_regpass", 1},
  259.       {"class_user_limit",1},
  260.       {"classdir",      1},
  261.       {"classname",     1},
  262.       {"devel_modules", 1},
  263.       {"developer",     1},
  264.       {"doc_quota",     1},
  265.       {"doc_regpass",   1},
  266.       {"email",         1},
  267.       {"exo",           0},     /* exercise number */
  268.       {"exoption",      1},     /* exercise option */
  269.       {"firstname",     1},
  270.       {"forum_limit",   1},
  271.       {"home",          1},
  272.       {"institutionname",       1},
  273.       {"isexam",        0},     /* whether the sheet is an exam sheet */
  274.       {"ismanager",     0},
  275.       {"lastname",      1},
  276.       {"mode",          0},     /* operating mode */
  277.       {"module_start_time",0},
  278.       {"now",           1},     /* date and time, yyyymmdd.hh:mm:ss */
  279.       {"nowseconds",    1},     /* date and time, seconds since EPOCH */
  280.       {"nr",            1},     /* non-readable variables in user file, words */
  281.       {"nw",            1},     /* non-writable variables in user file, words */
  282.       {"otherclass",    1},     /* Remember other logins */
  283.       {"participate",   1},     /* superclass definition */
  284.       {"prefix",        1},     /* user file prefix */
  285.       {"protocol",      0},     /* http protocol */
  286.       {"rafale",        0},     /* rapidfire request information */
  287.       {"readable",      1},     /* readable variables in user file, words */
  288.       {"realuser",      1},     /* real user for supervisor in gateway */
  289.       {"req_time",      0},     /* time of the request */
  290.       {"sclassdir",     1},
  291.       {"scorereg",      0},     /* score registration flag */
  292.       {"sequence", 0},  /*sequence number */
  293.       {"sescookie",     1},     /* session cookie */
  294.       {"sesdir",        1},
  295.       {"session_serial",0},     /* request serial in the session */
  296.       {"session_start_time",0},
  297.       {"sheet",         0},     /* sheet number */
  298.       {"sup_secure",    1},     /* secure level of supervisor */
  299.       {"superclass",    1},     /* superclass code */
  300.       {"superclass_quota",      1},
  301.       {"supertype",     1},     /* superclass type */
  302.       {"supervise",     1},     /* superclass definition */
  303.       {"supervisor",    1},     /* real name of the supervisor */
  304.       {"supervisormail",1},     /* email of supervisor */
  305.       {"trustfile",     1},     /* trusted files in special adm modules */
  306.       {"useropts",      1},     /* user options */
  307.       {"writable",      1},     /* writable variables in user file, words */
  308. };
  309. #define INTERNAL_NAME_NO (sizeof(internal_name)/sizeof(internal_name[0]))
  310.  
  311. char *httpd_vars[]={
  312.       "HTTP_ACCEPT",
  313.       "HTTP_ACCEPT_CHARSET",
  314.       "HTTP_ACCEPT_LANGUAGE",
  315.       "HTTP_COOKIE",
  316.       "HTTP_HOST",
  317.       "HTTP_USER_AGENT",
  318.       "HTTPS",
  319.       "QUERY_STRING",
  320.       "REMOTE_HOST",
  321.       "REMOTE_ADDR",
  322.       "REMOTE_PORT",
  323.       "REQUEST_METHOD",
  324.       "SCRIPT_NAME",
  325.       "SERVER_NAME",
  326.       "SERVER_SOFTWARE",
  327.       "SERVER_PROTOCOL"
  328. };
  329. #define HTTPD_VAR_NO (sizeof(httpd_vars)/sizeof(httpd_vars[0]))
  330.  
  331.         /* security: these variables will not be visible to
  332.          * child processes */
  333. char *unsetvars[]={
  334.         "DOCUMENT_ROOT","SERVER_SIGNATURE","SERVER_SOFTWARE",
  335.       "UNIQUE_ID","HTTP_KEEP_ALIVE","SSL_SESSION_ID"
  336. };
  337. #define unsetvarcnt (sizeof(unsetvars)/sizeof(unsetvars[0]))
  338.  
  339. enum {httpd_apache, httpd_wims};
  340. int httpd_type=httpd_apache;
  341.  
  342. char *remote_addr="";   /* storing for performance */
  343. char *remote_host="";
  344.  
  345. char ref_name[2048], ref_base[2048];
  346.  
  347. void put_special_page(char *pname);
  348. void useropts(void);
  349.  
  350. #include "lines.c"
  351. #include "var.c"
  352. #include "config.c"
  353. #include "cleaning.c"
  354. #include "files.c"
  355. #include "pedia.c"
  356. #include "evalue.c"
  357. #include "test.c"
  358. #include "compare.c"
  359. #include "html.c"
  360. #include "rawmath.c"
  361. #include "texmath.c"
  362. #include "insmath.c"
  363. #include "matrix.c"
  364. #include "score.c"
  365. #include "calc.c"
  366. #include "exec.c"
  367. #include "auth.c"
  368. #include "variables.c"
  369. #include "exam.c"
  370. #include "log.c"
  371.  
  372.         /* Make certain httpd variables readable by modules */
  373. void take_httpd_vars(void)
  374. {
  375.     int i;
  376.     char *p, buf[MAX_NAMELEN+1];
  377.     var_noexport=1;
  378.     for(i=0;i<HTTPD_VAR_NO;i++) {
  379.         snprintf(buf,sizeof(buf),"httpd_%s",httpd_vars[i]);
  380.         if((p=getenv(httpd_vars[i]))!=NULL) setvar(buf,p);
  381.     }
  382.     var_noexport=0;
  383.    
  384.     for(i=0;i<unsetvarcnt;i++) unsetenv(unsetvars[i]);
  385.      /* IPv4 IPv6*/
  386.     p=getenv("REMOTE_ADDR");if(p!=NULL && (strcmp(p,"127.0.0.1")==0 || strcmp(p,"::1")==0)) human_access=1;
  387.     p=getenv("HTTP_REFERER"); if(p!=NULL && *p!=0) setvar("wims_referer",p);
  388. }
  389.  
  390. void cookie2session(void)
  391. {
  392.     char cksession[64], psession[32], *ckey, *p;
  393.     char nbuf[MAX_FNAME+1];
  394.  
  395.     if(mode==mode_popup) return;
  396.     if(cookiegot[0]==0 || ro_cmd==cmd_close) {
  397.         ckset: cookiegot[0]=0; setcookie=1; return;
  398.     }
  399.     p=getvar("special_parm");
  400.     if(p!=NULL && strcmp(p,"ignorecookie")==0) return;
  401.     mystrncpy(cksession,cookiegot,sizeof(cksession));
  402.     ckey=strchr(cksession,'-');
  403.     if(ckey==NULL) goto ckset; else *ckey++=0;
  404.     p=getvar("wims_session"); if(p==NULL) p="";
  405.     if(strstr(p,"new")!=NULL) goto ckset;
  406.     mystrncpy(psession,p,sizeof(psession));
  407.     p=strchr(psession,'_'); if(p!=NULL) *p=0;
  408.     if(psession[0]!=0) {
  409.         if(strcmp(psession,cksession)==0) return;
  410.         if(session_exists(psession)) goto ckset;
  411.         if(session_exists(cksession)) goto change;
  412.     }
  413.     else {
  414.         if(!session_exists(cksession)) return;
  415.         change:
  416.         p=getenv("HTTPS");
  417.         if(p!=NULL && strcasecmp(p,"on")==0) goto ckset;
  418.         mkfname(nbuf,"%s/%s/var",session_dir,cksession);
  419.         getdef(nbuf,"w_wims_ismanager",tmplbuf);
  420.         if(tmplbuf[0]!=0 && tmplbuf[0]!='0') goto ckset;
  421.         getdef(nbuf,"w_wims_protocol",tmplbuf);
  422.         if(strcasecmp(tmplbuf,"https")==0) goto ckset;
  423.         mkfname(nbuf,"%s/%s/var.stat",session_dir,cksession);
  424.         getdef(nbuf,"wims_user",tmplbuf);
  425.         if(tmplbuf[0]!=0) goto ckset;
  426.         force_setvar(ro_name[ro_session],cksession);
  427.         setsesdir(cksession);
  428.         force_setvar("wims_subsession","");
  429.         session_serial=0;
  430.     }
  431. }
  432.  
  433. void determine_font(char *l)
  434. {
  435.     int i;
  436.    
  437.     if(l==NULL || *l==0) return;
  438.     for(i=0;i<charname_no && memcmp(charname[i].name,l,2);i++);
  439.     if(i<charname_no) setvar("wims_main_font",charname[i].font);
  440. }
  441.  
  442. void determine_dirn(char *l)
  443. {
  444.     int i;
  445.    
  446.     if(l==NULL || *l==0) return;
  447.     for(i=0;i<dirnname_no && memcmp(dirnname[i].name,l,2);i++);
  448.     if(i<dirnname_no) setvar("wims_main_dirn",dirnname[i].dirn);
  449. }
  450.  
  451. void predetermine_language(void)
  452. {
  453.     char *p;
  454.     int i,n;
  455.  
  456.     if(pre_language[0]!=0) p=pre_language;
  457.     else p=getenv("HTTP_ACCEPT_LANGUAGE");
  458.     if(p!=NULL && strlen(p)>=2) {
  459.       for(i=0;i<available_lang_no && memcmp(p,available_lang[i],2)!=0;i++);
  460.       if(i<available_lang_no) goto lend;
  461.     }
  462.     p=getenv("HTTP_USER_AGENT");
  463.     if(p!=NULL && strlen(p)>=5) {
  464.         char *q;
  465.         if((q=strchr(p,'['))!=NULL && islower(*(q+1)) && islower(*(q+2)) && *(q+3)==']') {
  466.             char bb[4];
  467.             bb[0]=*(q+1);bb[1]=*(q+2);bb[2]=0;
  468.             for(i=0;i<available_lang_no && memcmp(bb,available_lang[i],2)!=0;i++);
  469.             if(i<available_lang_no) {
  470.                 memmove(lang,bb,2); lang[2]=0;
  471.                 goto lend2;
  472.             }
  473.         }
  474.     }
  475.     p=getenv("HTTP_HOST"); if(p==NULL) goto lend2;
  476.     n=strlen(p); if(n<=3 || *(p+n-3)!='.') goto lend2;
  477.     p=p+n-2;
  478.     for(i=0;i<available_lang_no && memcmp(p,available_lang[i],2)!=0;i++);
  479.     if(i<available_lang_no) {
  480.         lend:   memmove(lang,p,2); lang[2]=0;
  481.         lend2:  determine_font(lang);determine_dirn(lang);
  482.     }
  483. }
  484.  
  485.         /* print a special page */
  486. void put_special_page(char *pname)
  487. {
  488.     determine_font(lang);
  489.     determine_dirn(lang);
  490.     phtml_put_base(mkfname(NULL,"%s.phtml.%s",pname,lang),0);
  491.     write_logs();free(var_str);
  492. }
  493.  
  494.         /* check whether the connection is a site manager. */
  495. void manager_check(void)
  496. {
  497.     char *p, *pp, buf[16];
  498.     struct stat confstat;
  499.     int i;
  500.    
  501.     manageable=0;
  502.     if(robot_access || *manager_site==0 || checkhost(manager_site)<1)
  503.       goto mend;
  504.     if(manager_https) {
  505.         p=getenv("HTTPS");
  506.         if(p==NULL || strcmp(p,"on")!=0) goto mend;
  507.     }
  508.  /* IPv4 IPv6*/
  509.     if(strcmp(remote_addr,"127.0.0.1")==0 || strcmp(remote_addr,"::1")==0) {
  510.         int port, port2;
  511.         char tester[128];
  512.         p=getenv("REMOTE_PORT"); if(p==NULL) goto mend;
  513.         port=atoi(p); if(port<1024 || port>65535) goto mend;
  514.         p=getenv("SERVER_PORT"); if(p==NULL) goto mend;
  515.         port2=atoi(p); if(port2>=10000 || port2<=0) goto mend;
  516.                 /* this is very non-portable */
  517.         manageable=1;
  518.         accessfile(tmplbuf,"r","/proc/net/tcp");
  519.         snprintf(tester,sizeof(tester)," 0100007F:%04X 0100007F:%04X ",
  520.                  port,port2);
  521.         p=strstr(tmplbuf,tester);
  522.         if(p!=NULL) {
  523.             pp=strchr(p,'\n'); if(pp!=NULL) *pp=0;
  524.             if(strlen(p)>=75) {
  525.                 p=find_word_start(p+70); *find_word_end(p)=0;
  526.                 if(atoi(p)==geteuid()) manageable=2;
  527.             }
  528.         }
  529.     }
  530.     else manageable=1;
  531.     i=stat(config_file,&confstat);
  532.     if(i==0 && manageable>0 && (confstat.st_mode&(S_IRWXO|S_IRWXG))!=0) manageable=-1;
  533.     if(manageable>0 && !trusted_module()) manageable=0;
  534.     if(manageable==1) {
  535.         accessfile(tmplbuf,"r","%s/.manager",session_prefix);
  536.         if(strstr(tmplbuf,"yes")!=NULL) manageable=2;
  537.     }
  538.     if(manageable==1) {
  539.         p=getvar(ro_name[ro_module]);
  540.         if(p!=NULL && strncmp(p,"adm/manage",strlen("adm/manage"))==0) {
  541.             struct stat pstat;
  542.             if(stat("../log/.wimspass",&pstat)==0) {
  543.                 if((S_IFMT&pstat.st_mode)!=S_IFREG ||
  544.                    ((S_IRWXO|S_IRWXG)&pstat.st_mode)!=0)
  545.                   manageable=-2;
  546.             }
  547.         }
  548.     }
  549.     mend:
  550.     mystrncpy(buf,int2str(manageable),sizeof(buf));
  551.     force_setvar("wims_ismanager",buf);
  552.     if(manageable>=2) {
  553.         struct rlimit rlim;
  554.         rlimit_cpu*=10;
  555.         rlim.rlim_cur=rlim.rlim_max=rlimit_cpu;
  556.         setrlimit(RLIMIT_CPU,&rlim);
  557.         mystrncpy(buf,int2str(rlimit_cpu),sizeof(buf));
  558.         setvar("wims_cpu_limit",buf);
  559.         initalarm();
  560.     }
  561. }
  562.  
  563.         /* check for robot access */
  564. void robot_check(void)
  565. {
  566.     char *ua, *p, *ses, *c, *mod;
  567.     int i;
  568.  
  569.     if(human_access) return;
  570.     mod=getvar(ro_name[ro_module]);
  571.     if(mod!=NULL && strcmp(mod,"adm/raw")==0) return;
  572.     ses=getvar(ro_name[ro_session]);
  573.         /* user has valid session; OK */
  574.     if(ses!=NULL && strncmp(ses,robot_session,strlen(robot_session))!=0
  575.        && strchr(ses,'/')==NULL
  576.        && ftest(mkfname(NULL,"%s/%s",s2_dir,ses))==is_dir)
  577.       return;
  578.     ua=getenv("HTTP_USER_AGENT"); if(ua==NULL) ua="";
  579.     ua=find_word_start(ua);
  580.     if(strncasecmp(ua,"Mozilla",strlen("Mozilla"))==0 &&
  581.        (p=strstr(ua,"compatible"))!=NULL)
  582.       ua=find_word_start(find_word_end(p));
  583.     if(*ua) {
  584.         for(i=0;i<good_agent_no
  585.             && strncasecmp(ua,good_agent[i],strlen(good_agent[i]));i++);
  586.         if(i<good_agent_no) return;
  587.         for(i=0;i<bad_agent_no
  588.             && strstr(ua,bad_agent[i])==NULL;i++);
  589.         if(i<bad_agent_no) user_error("trapped");
  590.     }
  591.     force_setvar(ro_name[ro_session],robot_session);
  592.     setsesdir(robot_session);
  593.     c=getvar(ro_name[ro_cmd]);
  594.     robot_access=1;
  595.     if(c!=NULL && strcmp(c,"new") && strcmp(c,"intro")) {
  596.         force_setvar(ro_name[ro_cmd],"robot_error");
  597.         nph_header(450); put_special_page("robot");
  598.         flushoutput(); flushlog(); exit(0);
  599.     }
  600. }
  601.  
  602.         /* type=0: ordinary; type=1: multipart/form-data */
  603. void parse_query_string(int len, int type)
  604. {
  605.     int i,j,l,v,cmd_defined;
  606.     int parenth=-1, ll, lb, dlen;
  607.     char *start, *p, *p1, *pt, *b1="";
  608.    
  609.     cmd_defined=0;
  610.     setvar("wims_subsession","");
  611.     ll=lb=0;
  612.     if(type) {
  613.         ll=strlen(mpboundary);
  614.         start=strstr(var_str,mpboundary);
  615.         if(start==NULL) start=var_str+strlen(var_str);
  616.         if(strstr(var_str,"\r\n\r\n")!=NULL) b1="\r\n\r\n";
  617.         else b1="\n\n";
  618.         lb=strlen(b1);
  619.     }
  620.     else start=var_str;
  621.     for(v=0, p1=start;p1<var_str+len;p1+=l) {
  622.         if(type) {
  623.             char *p2, *p3, *p4, *p5;
  624.             p2=p1+ll; p3=memstr(p2,mpboundary,var_str+len-p2); l=p3-p1;
  625.             p=memstr(p2,b1,var_str+len-p2); if(p>=p3) continue;
  626.             p+=lb; if(p3<var_str+len) {
  627.                 while(*p3!='\n' && p3>p2) p3--; *p3=0;
  628.                 p3--; if(*p3=='\r') *p3=0;
  629.             }
  630.             dlen=p3-p;
  631.             p2=memstr(p2,"name=",p3-p2); if(p2>=p3) continue;
  632.             p2+=strlen("name="); if(*p2=='"') p2++;
  633.             for(p3=p2; myisalnum(*p3) || strchr("._",*p3)!=NULL; p3++);
  634.             if(p3==p2) continue;
  635.             if(p3-p2==strlen("wims_deposit") &&
  636.                strncmp(p2,"wims_deposit",p3-p2)==0) {
  637.                 p4=memstr(p1,"filename=",p-p1); if(p4<p) {
  638.                     p4+=strlen("filename="); if(*p4=='"') {
  639.                         p4++; p5=strchr(p4,'"');
  640.                         if(p5==NULL || p5-p4>=MAX_FNAME) goto emptyquote;
  641.                     }
  642.                     else {
  643.                         emptyquote:
  644.                         for(p5=p4; p5<p && !isspace(*p5) &&
  645.                             strchr(";\"~#*?=,'",*p5)==NULL; p5++);
  646.                     }
  647.                     if(p5>p4) {
  648.                         *p5=0;
  649.                         for(p5--;
  650.                             p5>=p4 && !isspace(*p5) && strchr("/\\:",*p5)==NULL;
  651.                             p5--);
  652.                         if(p5>=p4) p4=p5+1; if(*p4==0) goto noname;
  653.                         if(strstr(p4,"..")!=NULL || *p4=='.')
  654.                           p4="noname.file";
  655.                         setvar("wims_deposit",p4);
  656.                     }
  657.                     else {
  658.                         noname: setvar("wims_deposit","noname.file");
  659.                     }
  660.                 }
  661.                 deplen=dlen;
  662.             }
  663.             *p3=0; l-=p2-p1; p1=p2;
  664.         }
  665.         else {
  666.             p1=find_word_start(p1);
  667.             l=strlen(p1)+1; p=strchr(p1,'=');
  668.             if(p==NULL) p=p1+strlen(p1);
  669.             if(*p==0 && l>1) {
  670.                 user_variable[v].name="no_name";
  671.                 user_variable[v].value=p1;
  672.                 coord_input=1;
  673.                 goto nnext;
  674.             }
  675.             *p++=0;
  676.         }
  677.                 /* empty name or empty value: ignore */
  678.         if(*p1==0 || *p==0) continue;
  679.                 /* We do not treat names containing '.' */
  680.         for(pt=strchr(p1,'.'); pt; pt=strchr(++pt,'.')) *pt='_';
  681.                 /* Restrictions on variable names */
  682.         for(pt=p1; myisalnum(*pt) || *pt=='_'; pt++);
  683.         if(*pt) continue;
  684.         if(strcmp(p1,"wims_deposit")!=0) _tolinux(p);
  685.                 /* This is a restriction:
  686.                  * Every parameter must have matching parentheses. */
  687.         if(parenth==-1 && strncmp(p1,"freepar_",strlen("freepar_"))!=0
  688.            && strcmp(p1,"wims_deposit")!=0
  689.            && check_parentheses(p,1)) parenth=v;
  690.         if(strcmp(p1,"special_parm")==0 && strcmp(p,"wims")==0)
  691.           human_access=1;
  692.         j=search_list(ro_name,RO_NAME_NO,sizeof(ro_name[0]),p1);
  693.         if(j>=0) {
  694.             if(j==ro_session) {
  695.                 p=find_word_start(p); *find_word_end(p)=0;
  696.                 if(strlen(p)>MAX_SESSIONLEN) continue;
  697.                 if(strcmp(p,robot_session)==0) p="";
  698.                 if(strcasecmp(p,"popup")==0) {
  699.                     mode=mode_popup;
  700.                     force_setvar("wims_mode","popup");
  701.                     force_setvar("session","");
  702.                     continue;
  703.                 }
  704.             }
  705.             if(j==ro_module) module_defined=1;
  706.             if(j==ro_cmd) {
  707.                 p=find_word_start(p); *find_word_end(p)=0;
  708.                 if(strlen(p)>16) continue;
  709.                 cmd_defined=1;
  710.             }
  711.             if(j==ro_lang) {
  712.                 if(strlen(p)!=2) continue;
  713.                 for(i=0;i<available_lang_no && strcmp(available_lang[i],p)!=0;i++);
  714.                 if(i<available_lang_no) {user_lang=1; ovlstrcpy(lang,p);}
  715.                 else continue;
  716.             }
  717.             /* strip leading and trailing '/'s in module name */
  718.             if(j==ro_module) {
  719.                 p=find_word_start(p); *find_word_end(p)=0;
  720.                 while(*p=='/') p++;
  721.                 while(*p!=0 && *(p+strlen(p)-1)=='/') *(p+strlen(p)-1)=0;
  722.                 if(strlen(p)>MAX_MODULELEN) continue;
  723.             }
  724.             setvar(p1,p);
  725.             if(j==ro_session && mode!=mode_popup) {
  726.                 char *pp, *pr;
  727.                 char buf[1024];
  728.                 mystrncpy(buf,p,sizeof(buf));
  729.                 if((pp=strchr(buf,'.'))!=NULL) {
  730.                     *pp++=0; session_serial=atoi(pp);
  731.                     if(pp<0) pp=0;
  732.                 }
  733.                 else session_serial=0;
  734.                 pp=strchr(buf,'_');
  735.                 if(pp!=NULL && (pr=strstr(pp,"_mhelp"))!=NULL) {
  736.                     *pr=0; ismhelp=1; lastout_file=-1;
  737.                     setvar("wims_inssub","mh");
  738.                 }
  739.                 force_setvar("wims_session",buf);
  740.                 if(pp!=NULL) force_setvar("wims_subsession",pp);
  741.             }
  742.             continue;
  743.         }
  744.         user_variable[v].name=p1;
  745.         user_variable[v].value=p;
  746. nnext:  v++; if(v>=MAX_VAR_NUM) user_error("too_many_variables");
  747.     }
  748.     user_var_no=v;
  749.     if(parenth>=0) {
  750.         char buf[16];
  751.         mystrncpy(buf,int2str(user_var_no),sizeof(buf));
  752.         setvar("user_var_no",buf);
  753.         for(i=0;i<user_var_no;i++) {
  754.             snprintf(buf,sizeof(buf),"name%d",i);
  755.             setvar(buf,user_variable[i].name);
  756.             snprintf(buf,sizeof(buf),"value%d",i);
  757.             setvar(buf,user_variable[i].value);
  758.         }
  759.         mystrncpy(buf,int2str(parenth),sizeof(buf));
  760.         setvar("bad_parentheses",buf);
  761.         user_error("unmatched_parentheses");
  762.     }
  763.     p=getenv("SCRIPT_NAME");
  764.     if(p!=NULL && (p=strstr(p,"/getfile/"))!=NULL) {
  765.         p+=strlen("/getfile/");
  766.         force_setvar(ro_name[ro_cmd],commands[cmd_getfile]);
  767.         force_setvar("special_parm",p);
  768.         cmd_defined=1;
  769.     }
  770.     if(module_defined && !cmd_defined) setvar(ro_name[ro_cmd],commands[cmd_intro]);
  771.     robot_check(); cookie2session();
  772. }
  773.  
  774.         /* parse special commands */
  775. void special_cmds(void)
  776. {
  777.     char *c, *p;
  778.     int i;
  779.     long int l=-1;
  780.    
  781.     c=getvar(ro_name[ro_cmd]);
  782.     if(c==NULL || *c==0) {  /* no module name nor command */
  783.         setvar(ro_name[ro_module],home_module);
  784.         setvar(ro_name[ro_cmd],commands[cmd_new]);
  785.         return;
  786.     }
  787.     for(i=0;i<CMD_NO && strcmp(commands[i],c)!=0; i++);
  788.     switch(i) {
  789.         case cmd_intro: {
  790.             set_module_prefix();
  791.             default_form_method="get";
  792.             if(ftest(mkfname(NULL,"%s/%s",module_prefix,intro_file))<0) {
  793.                 force_setvar(ro_name[ro_cmd],commands[cmd_new]);
  794.                 return;
  795.             }
  796.             p=getvar("wims_session");
  797.             if(p!=NULL && *p!=0) {
  798.                 if(set_session_prefix()==0) check_session();
  799.                 else {
  800.                     trap_check(p);
  801.                     if(strchr(p,'_')!=NULL && strchr(p,'/')==NULL) {
  802.                         get_static_session_var();
  803.                     }
  804.                 }
  805.             }
  806.             /* determine http protocol name. How to detect? */
  807.             p=getenv("HTTPS"); if(p!=NULL && strcmp(p,"on")==0) {
  808.                 protocol="https"; set_protocol();
  809.             }
  810.             force_setvar("wims_protocol",protocol);
  811.             determine_font(lang);
  812.             determine_dirn(lang);
  813.             main_phtml_put(intro_file); debug_output();
  814.             introend: write_logs();free(var_str);
  815.             delete_pid(); exit(0);
  816.         }
  817.         case cmd_ref: {
  818.             set_module_prefix();
  819.             default_form_method="get";
  820.             p=getvar("wims_session");
  821.             if(p!=NULL && *p!=0) {
  822.                 if(set_session_prefix()==0) check_session();
  823.                 else trap_check(p);
  824.             }
  825.             determine_font(lang);
  826.             determine_dirn(lang);
  827.             main_phtml_put(ref_file); goto introend;
  828.         }
  829.         case cmd_getins: {
  830.             c=getvar(ro_name[ro_special_parm]);
  831.             if(c==NULL || *c==0) {
  832.                 user_error_nolog=1; user_error("no_insnum");
  833.             }
  834.             if(*c=='/' || strstr(c,"..")!=NULL) goto badins;
  835.             set_session_prefix();
  836.             if(strstr(session_prefix,"robot")!=NULL) exit(0);
  837.             l=filelength("%s/%s",s2_prefix,c);
  838.             if(l<0) {
  839.                 badins: user_error_nolog=1; user_error("bad_insnum");
  840.             }
  841.             {
  842.                 char *fmt;
  843.                 fmt=strchr(c,'.');
  844.                 if(fmt==NULL) {
  845.                     user_error_nolog=1; user_error("bad_insnum");
  846.                 }
  847.                 else fmt++;
  848.                
  849.                 nph_header(200);
  850. /* insert format problem; bricolage */
  851.                 printf("Content-type: image/%s\r\n\
  852. Content-length: %ld\r\n\r\n",fmt,l);
  853.                 catfile(stdout,"%s/%s",s2_prefix,c); exit(0);
  854.             }
  855.         }
  856.         case cmd_getfile: {
  857.             char fname[MAX_FNAME+1];
  858.             c=getvar(ro_name[ro_special_parm]);
  859.             if(c==NULL || *c==0) {
  860.                 user_error_nolog=1; user_error("no_insnum");
  861.             }
  862.             if(*c=='/' || strstr(c,"..")!=NULL) goto badfile;
  863.            
  864.             set_session_prefix();
  865.             if(strstr(session_prefix,"robot")!=NULL) exit(0);
  866.             mkfname(fname,"%s/getfile/%s",session_prefix,c);
  867.             l=filelength("%s",fname);
  868.             if(l<0 && strchr(session_prefix,'_')!=NULL) {
  869.                 char *pt;
  870.                 mystrncpy(fname,session_prefix,sizeof(fname));
  871.                 pt=strrchr(fname,'_'); if(pt) *pt=0;
  872.                 snprintf(fname+(pt-fname),sizeof(fname)-(pt-fname),
  873.                         "/getfile/%s",c);
  874.                 l=filelength("%s",fname);
  875.             }
  876.             if(l<0) {
  877.                 badfile: user_error_nolog=1; user_error("bad_insnum");
  878.             }
  879.             if(l>512*1024) {
  880.                 struct rlimit rlim;
  881.                 rlimit_cpu*=l/(10*1024);
  882.                 rlim.rlim_cur=rlim.rlim_max=rlimit_cpu;
  883.                 initalarm();
  884.             }
  885.             {
  886.                 char *p1;
  887.                 char mime[MAX_LINELEN+1];
  888.                 for(p1=c+strlen(c);p1>c && isalpha(*(p1-1)); p1--);
  889.                 ovlstrcpy(mime,"application/octet-stream");
  890.                 if(p1>c && *(p1-1)=='.') {
  891.                     setvar("translator_unknown",mime);
  892.                     setvar("dictionary","bases/sys/mime");
  893.                     snprintf(mime,sizeof(mime),"translator %s",p1);
  894.                     calc_exec(mime);
  895.                 }
  896.                 nph_header(200);
  897.                 printf("Content-type: %s\r\n\
  898. Content-length: %ld\r\n\r\n",mime,l);
  899.                 catfile(stdout,"%s",fname); exit(0);
  900.             }
  901.         }
  902.         case cmd_close: {
  903.             char *p, b2[32]; int w;
  904.             char nbuf[MAX_FNAME+1], vbuf[MAX_LINELEN+1];
  905.             p=getvar(ro_name[ro_session]);
  906.             if(p==NULL || strlen(p)<10 ||
  907.                strchr(p,'/')!=NULL) return;
  908.             mystrncpy(b2,p,sizeof(b2));
  909.             p=strchr(b2,'.'); if(p!=NULL) *p=0;
  910.             mkfname(nbuf,"%s/%s/var.stat",session_dir,b2);
  911.             getdef(nbuf,"wims_caller",vbuf);
  912.             if(vbuf[0]!=0) force_setvar(ro_name[ro_session],vbuf);
  913.             w=wrapexec; wrapexec=1;
  914.             call_sh("rm -Rf %s/%s* %s/%s* >/dev/null 2>&1",session_dir,b2,s2_dir,b2);
  915.             wrapexec=w; cookiegot[0]=0;
  916.             force_setvar(ro_name[ro_cmd],"new");
  917.         }
  918.         default: return;
  919.     }
  920. }
  921.  
  922.         /* This is run only when manually invoking the program.
  923.          * Verifies the orderedness of various list tables. */
  924. int verify_tables(void)
  925. {
  926.     if(verify_order(calc_routine,CALC_FN_NO,sizeof(calc_routine[0]))) return -1;
  927.     if(verify_order(exec_routine,EXEC_FN_NO,sizeof(exec_routine[0]))) return -1;
  928.     if(verify_order(main_config,MAIN_CONFIG_NO,sizeof(main_config[0]))) return -1;
  929.     if(verify_order(mathname,mathname_no,sizeof(mathname[0]))) return -1;
  930.     if(verify_order(hmname,hmname_no,sizeof(hmname[0]))) return -1;
  931.     if(verify_order(ro_name,RO_NAME_NO,sizeof(ro_name[0]))) return -1;
  932.     if(verify_order(distr_cmd,distr_cmd_no,sizeof(distr_cmd[0]))) return -1;
  933.     if(verify_order(internal_name,INTERNAL_NAME_NO,
  934.                     sizeof(internal_name[0]))) return -1;
  935.     if(verify_order(tmathfn,tmathfn_no,sizeof(tmathfn[0]))) return -1;
  936.     if(verify_order(tmathvar,tmathvar_no,sizeof(tmathvar[0]))) return -1;
  937.     if(verify_order(modindex,MODINDEX_NO,sizeof(modindex[0]))) return -1;
  938.     if(verify_order(exportvars,exportvarcnt,sizeof(exportvars[0]))) return -1;
  939.     if(evaltab_verify()) return -1;
  940.     if(textab_verify()) return -1;
  941.     return 0;
  942. }
  943.  
  944. void config_defaults(void)
  945. {
  946.     int i;
  947.     for(i=0;i<MAIN_CONFIG_NO;i++) {
  948.         if((1&main_config[i].is_integer)==1) {
  949.             int *pi = (int*)main_config[i].address;
  950.             printf("DF_%s=%d\n",main_config[i].name, *pi);
  951.         }
  952.         else {
  953.             char **ps = (char**)main_config[i].address;
  954.             printf("DF_%s=%s\n",main_config[i].name,*ps);
  955.         }
  956.     }
  957. }
  958.  
  959.         /* get and set useroptions */
  960. void useropts(void)
  961. {
  962.     char *p;
  963.     setvar("lang",lang);
  964.     p=getvar("useropts");
  965.     if(p==NULL || *p==0) p=getvar("wims_useropts");
  966.     if(p!=NULL && *p!=0) {
  967.         if(myisdigit(p[0])) {
  968.             usertexsize=p[0]-'0';
  969.             if(p[1]!=0) {
  970.                 mathalign_base=p[1]-'0';
  971.             }
  972.         }
  973.     }
  974.     if(mathalign_base==1) {
  975.         mathalign_sup1="<sup>"; mathalign_sup2="</sup>";
  976.     }
  977.     else mathalign_sup1=mathalign_sup2="";
  978. }
  979.  
  980.         /* popup module help */
  981. void mhelp(void)
  982. {
  983.     char *p, buf[MAX_LINELEN+1];
  984.     main_phtml_put(""); buf[0]=0;
  985.     if(cmd_type!=cmd_help) {
  986.         phtml_put_base("closemhelp.phtml",0);
  987.     }
  988.     else {
  989.         phtml_put_base("mhelpheader.phtml",0);
  990.         p=getvar("special_parm");
  991.         if(p!=NULL && strcmp(p,"about")==0)
  992.           phtml_put("about.phtml",0);
  993.         else phtml_put("help.phtml",0); phtml_put_base("mhelptail.phtml",0);
  994.         exec_tail(buf);
  995.     }
  996. }
  997.  
  998. #define READSTDIN_WINDOW 4096
  999.  
  1000. void readstdin(int len)
  1001. {
  1002.     int ll, l1, lt, lr;
  1003.     int cpulim;
  1004.    
  1005.     cpulim=rlimit_cpu; rlimit_cpu=3;
  1006.     lr=len; l1=0;
  1007.     while(lr>0) {
  1008.         nowtime=time(0); initalarm();
  1009.         ll=lr; if(ll>READSTDIN_WINDOW) ll=READSTDIN_WINDOW;
  1010.         lt=fread(stdinbuf+l1,1,ll,stdin);
  1011.         if(lt!=ll) user_error("parm_too_long");
  1012.         lr-=ll; l1+=ll;
  1013.     }
  1014.     if(l1!=len) user_error("parm_too_long");
  1015.     stdinbuf[len]=0; rlimit_cpu=cpulim;
  1016. }
  1017.  
  1018.         /* input: p=QUERY_STRING. output: parameter length. */
  1019.         /* Netscape puts form content into /tmp. */
  1020. int formdata(char *p)
  1021. {
  1022.     char *pp;
  1023.     int inlen;
  1024.     char *ctype;
  1025.     inlen=0; ctype=getenv("CONTENT_TYPE");
  1026.     if(ctype==NULL || strstr(ctype,"multipart/form-data")==NULL
  1027.        || (p=strstr(ctype,"boundary="))==NULL) {
  1028.         bad: stdinbuf=""; return 0;
  1029.     }
  1030.     pp=getenv("CONTENT_LENGTH");
  1031.     if(pp==NULL) goto bad;
  1032.     inlen=atoi(pp); if(inlen<=10) goto bad;
  1033.     if(inlen>=MAX_DEPOSITLEN) user_error("parm_too_long");
  1034.     stdinbuf=xmalloc(inlen+1); readstdin(inlen);
  1035.     p+=strlen("boundary=");
  1036.     for(pp=p;myisalnum(*pp) || *pp=='-'; pp++);
  1037.     if(pp-p<sizeof(mpboundary)-2) {
  1038.         memmove(mpboundary,p,pp-p); mpboundary[pp-p]=0;
  1039.     }
  1040.                 /* empty data */
  1041.     if(strstr(stdinbuf,mpboundary)==NULL || strstr(stdinbuf,"name=")==NULL) {
  1042.         free(stdinbuf); goto bad;
  1043.     }
  1044.     form_access=1; post_log();
  1045.     return inlen;
  1046. }
  1047.  
  1048.         /* get the content of POST */
  1049. void getpost(void)
  1050. {
  1051.     int ll;
  1052.     char *pp;
  1053.     pp=getenv("CONTENT_LENGTH");
  1054.     if(pp==NULL || (ll=atoi(pp))<=0) {
  1055.         stdinbuf=xmalloc(16); stdinbuf[0]=0;
  1056.     }
  1057.     else {
  1058.         if(ll>QUERY_STRING_LIMIT) user_error("parm_too_long");
  1059.         stdinbuf=xmalloc(ll+16); readstdin(ll);
  1060.         if(ll>0) {
  1061.             setenv("QUERY_STRING",stdinbuf,1);
  1062.             form_access=1; post_log();
  1063.         }
  1064.     }
  1065. }
  1066.  
  1067. void buffer_init(void)
  1068. {
  1069.     struct timeval tv;
  1070.    
  1071.     mcachecnt=readnest=0;
  1072.     mpboundary[0]=cookiegot[0]=cookieset[0]=cwdbuf[0]=0;
  1073.     rscore_class[0]=rscore_user[0]=multiexec_random[0]=0;
  1074.     lastftest[0]=0;
  1075.     lastdatafile[0]=0; lastdata=xmalloc(WORKFILE_LIMIT);
  1076.     outptr=outbuf;
  1077.     instex_src[0]=instex_fname[0]=module_prefix[0]=0;
  1078.     examlogf[0]=examlogd[0]=exam_sheetexo[0]=0;
  1079.     stdinbuf=NULL;
  1080.     mkfname(tmp_dir,"../tmp/forall");
  1081.     mkfname(session_dir,"../%s",SESSION_BASE);
  1082.     mkfname(s2_dir,"../%s",S2_BASE);
  1083.     if(gettimeofday(&tv,NULL)) startmtime=startmtime2=0;
  1084.     else {
  1085.         startmtime=((tv.tv_sec%10000)*1000+tv.tv_usec/1000);
  1086.         startmtime2=(tv.tv_sec%1000)*1000000+tv.tv_usec;
  1087.     }
  1088. }
  1089.  
  1090. int main(int argc, char *argv[], char *envp[])
  1091. {
  1092.     char *p, homebuf[MAX_FNAME+1], lbuf[32];
  1093.     int inlen=0;
  1094. /*    int mfd;
  1095. */
  1096.     error1=user_error; error2=module_error; error3=internal_error;
  1097.     class_dir[0]=0;
  1098.     substitute=substit; buffer_init(); var_init();
  1099.         /* WIMS internal locale is always C. */
  1100.     setenv("LANG","C",1); umask(022);
  1101.     setenv("LANGUAGE","us",1);
  1102.     setenv("LC_ALL","C",1);
  1103.     if(argc>1) {
  1104.         if(strcasecmp(argv[1],"table")==0) {
  1105.             if(verify_tables()) internal_error("Table disorder.");
  1106.             else printf("Table orders OK.\n");
  1107.             return 0;
  1108.         }
  1109.         if(strcasecmp(argv[1],"version")==0) {
  1110.             printf("%s",wims_version); return 0;
  1111.         }
  1112.         if(strcasecmp(argv[1],"defaults")==0) {
  1113.             config_defaults(); return 0;
  1114.         }
  1115.     }
  1116.     p=getenv("SERVER_SOFTWARE"); if(p!=NULL && strcasecmp(p,"WIMS")==0)
  1117.       httpd_type=httpd_wims;
  1118.     p=getenv("REMOTE_ADDR"); if(p!=NULL) remote_addr=p;
  1119.     p=getenv("REMOTE_HOST"); if(p!=NULL) remote_host=p;
  1120.     nowtime=time(0); now=localtime(&nowtime);
  1121.     memmove(&Now, now, sizeof(Now)); now=&Now;
  1122.     snprintf(nowstr,sizeof(nowstr),"%04d%02d%02d.%02d:%02d:%02d",
  1123.              (now->tm_year)+1900,(now->tm_mon)+1,now->tm_mday,
  1124.              now->tm_hour,now->tm_min,now->tm_sec);
  1125.     p=getenv("QUERY_STRING");
  1126.     if(p==NULL || *p==0) getpost();
  1127.     else if(strncmp(p,"form-data",9)==0) inlen=formdata(p);
  1128.    
  1129.     force_setvar("wims_now",nowstr);
  1130.     snprintf(lbuf,sizeof(lbuf),"%lu",nowtime);
  1131.     force_setvar("wims_nowseconds",lbuf);
  1132.     nowtime=time(0);
  1133.     initalarm();
  1134.    
  1135.     executed_gotos=insert_no=output_length=0; ins_alt[0]=0;
  1136.     setvar("empty","");         /* lock this variable */
  1137.     setvar("wims_version",wims_version);
  1138.     setvar("wims_version_date",wims_version_date);
  1139.     setvar("wims_main_font","utf-8");
  1140.     take_httpd_vars();
  1141.  
  1142.     main_configure();
  1143.     checklogd();
  1144. /*    mfd=shm_open(SHM_NAME,O_RDONLY,-1);
  1145.     if(mfd==-1) internal_error("Unable to find shared memory.");
  1146.     shmptr=mmap(0,SHM_SIZE,PROT_READ,MAP_SHARED,mfd,0);
  1147.     if(shmptr==MAP_FAILED) internal_error("Shared memory failure.");
  1148. */
  1149.     getppid();  /* this is the first sysmask trigger, must be after checklogd() */
  1150.     predetermine_language();
  1151.     set_rlimits();
  1152.     init_random();
  1153.     module_configure();
  1154.     set_job_ident();
  1155.     m_file.name[0]=0;m_file.linecnt=m_file.linepointer=0;
  1156.     p=getenv("QUERY_STRING");
  1157.     if(p==NULL || strlen(p)==0) {
  1158.         setvar("lang",lang);
  1159.         snprintf(homebuf,sizeof(homebuf),"module=%s",home_module);
  1160.         p=homebuf;
  1161.     }
  1162.     if(strlen(p)>=QUERY_STRING_LIMIT) user_error("parm_too_long");
  1163.     if(mpboundary[0]==0) {
  1164.         var_str=xmalloc(strlen(p)+2);
  1165.         parse_query_string(http2env(var_str,p),0);
  1166.     }
  1167.     else {
  1168.         var_str=stdinbuf;
  1169.         parse_query_string(inlen,1);
  1170.     }
  1171.     if(ismhelp) {
  1172.         p=getvar(ro_name[ro_cmd]);
  1173.         if(p==NULL || (strcmp(p,"help")!=0 && strcmp(p,"getins")!=0)) {
  1174.             mhelp(); goto outgo;
  1175.         }
  1176.     }
  1177.     check_exam();
  1178.     useropts();
  1179.     special_cmds();
  1180.     parse_ro_names();
  1181.     manager_check();
  1182.     access_check(0);
  1183.     set_variables();
  1184.     determine_font(getvar("module_language"));
  1185.     determine_dirn(getvar("module_language"));
  1186.     if(!robot_access && session_prefix[0]!=0 && cmd_type!=cmd_help && !ismhelp)
  1187.       lastout_file=creat(mkfname(NULL,"%s/%s",s2_prefix,lastout),
  1188.                          S_IRUSR|S_IWUSR);
  1189.     p=getvar("module_category");
  1190.     if(p==NULL || strstr(p,"tool")==NULL) default_form_method="get";
  1191.     if(noout) {
  1192.         write_logs(); save_session_vars();
  1193.         goto outgo;
  1194.     }
  1195.     if(ismhelp) {
  1196.         mhelp();
  1197.     }
  1198.     else {
  1199.         main_phtml_put(html_file);
  1200.         if(lastout_file!=-1) {
  1201.             flushoutput(); close(lastout_file); putlastout();
  1202.         }
  1203.         write_logs(); save_session_vars();
  1204.     }
  1205.     outgo:
  1206.     debug_output();
  1207.     if(var_str!=stdinbuf) free(var_str);
  1208.     delete_pid();
  1209.     if(mode!=mode_popup && trusted_module()) {
  1210.         p=getvar("wims_mode");
  1211.         if(p!=NULL && strcmp(p,"popup")==0) mode=mode_popup;
  1212.     }
  1213.     if(mode==mode_popup && insert_no==0) {
  1214.         p=getvar("wims_mode");
  1215.         if(p!=NULL && strcmp(p,"popup")==0) {
  1216.             remove_tree(session_prefix);
  1217.            
  1218.             remove_tree(s2_prefix);
  1219.         }
  1220.     }
  1221.     return 0;
  1222. }
  1223.  
  1224.