Subversion Repositories wimsdev

Rev

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

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