Subversion Repositories wimsdev

Rev

Rev 12835 | Rev 13381 | 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. /* routines to process variables */
  18.  
  19. #include "wims.h"
  20. char *computed_var_start; /* pointer to read-in var def file */
  21. int session_var_ready=0;
  22. char last_host[32]="";
  23. char *robotcheck="";
  24.  
  25. char *header_var_name[]={
  26.     "REMOTE_ADDR", "HTTP_REFERER", "QUERY_STRING", "HTTP_USER_AGENT",
  27.      "HTTP_COOKIE"
  28. };
  29. #define HEADER_VAR_NO (sizeof(header_var_name)/sizeof(header_var_name[0]))
  30.  
  31. struct special_name {char *name; int value;} special_name[]={
  32.   {"MAX_EXAMS",MAX_EXAMS},
  33.   {"MAX_EXOS",MAX_EXOS},
  34.   {"MAX_OEFCHOICES",MAX_OEFCHOICES},
  35.   {"MAX_OEFREPLIES",MAX_OEFREPLIES},
  36.   {"MAX_SHEETS",MAX_SHEETS},
  37.   {"MAX_TECHVARVAL",MAX_TECHVARVAL},
  38.   {"MAX_VOTES",MAX_VOTES},
  39. };
  40. int special_name_no=(sizeof(special_name)/sizeof(special_name[0]));
  41. char *var_allow[]={
  42.     "deny" , "init" , "config" ,
  43.       "reply", "any", "help"
  44. };
  45. enum {
  46.     var_allow_deny, var_allow_init, var_allow_config,
  47.       var_allow_reply, var_allow_any, var_allow_help
  48. } VAR_ALLOWS;
  49. #define VAR_ALLOW_NO (sizeof(var_allow)/sizeof(var_allow[0]))
  50.  
  51. struct internal_name internaldef_name[]={
  52.   {"exotrymax",var_allow_init}
  53. };
  54. int INTERNALDEF_NAME_NO=(sizeof(internaldef_name)/sizeof(internaldef_name[0]));
  55.  
  56. /* install a temporary directory for the session */
  57. void mktmpdir(char *p)
  58. {
  59.   char *base;
  60.   if(p==NULL || *p==0 || strstr(p,"robot")!=NULL) return;
  61.   if(strstr(tmp_dir,"sessions")!=NULL) return;
  62.   if(ftest("../chroot/tmp/sessions/.chroot")==is_file) base="chroot/tmp";
  63.   else base="tmp";
  64.   mkfname(tmp_dir,"../%s/sessions/%s",base,p);
  65.   remove_tree(tmp_dir); mkdirs(tmp_dir);
  66.   chmod(tmp_dir,S_IRUSR|S_IWUSR|S_IXUSR
  67.        |S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
  68.   setenv("tmp_dir",tmp_dir,1); setenv("TMPDIR",tmp_dir,1);
  69. }
  70.  
  71. /* Open session variable file */
  72. FILE *fopen_session_var_file(char read_or_write[])
  73. {
  74.   char *nbuf; FILE *f;
  75.  
  76.   nbuf=mkfname(NULL,"%s/var",session_prefix);
  77.   if(read_or_write[0]=='r') {
  78.     if(open_working_file(&m_file,nbuf)!=0) return NULL;
  79.     mkfname(m_file.name,"session_var");
  80.     return stdin;
  81.   }
  82.   f=fopen(nbuf,read_or_write);
  83.   if(f==NULL) {
  84. /* expired_session */
  85.     internal_error("fopen_session_var_file(): unable to create session variable file.");
  86.   }
  87.   mkfname(m_file.name,"session_var");
  88.   m_file.l=0;
  89.   return f;
  90. }
  91.  
  92. /* Open a module file, read only, with name checking.
  93.  * returns NULL if error.
  94.  */
  95. int read_module_file(char *fname)
  96. {
  97.   char nbuf[MAX_FNAME+1];
  98.  
  99.   if(get_cached_file(fname)>=0) return 0;
  100.   if(find_module_file(fname,nbuf,0)) return -1;
  101.   if(open_working_file(&m_file,nbuf)!=0) return -1;
  102.   untrust|=(untrust>>8);
  103.   mystrncpy(m_file.name,fname,sizeof(m_file.name));
  104.   if(strncmp(module_prefix,module_dir,strlen(module_dir))!=0) m_file.nocache|=8;
  105.   return 0;
  106. }
  107. /* list of variables get from the query parms */
  108. int varget[]={ro_module,ro_lang,ro_useropts,ro_worksheet};
  109. #define varget_no (sizeof(varget)/sizeof(varget[0]))
  110.  
  111. /* set up ref strings according to protocol */
  112. void set_protocol(void)
  113. {
  114.   if(strcmp(protocol,"https")!=0) return;
  115.   if(strncmp(ref_name,"http:",5)==0) {
  116.     string_modify(ref_name,ref_name+4,ref_name+4,"s");
  117.     string_modify(ref_base,ref_base+4,ref_base+4,"s");
  118.     force_setvar("wims_ref_name",ref_name);
  119.   }
  120. }
  121.  
  122. /* verify class participant connection data */
  123. void classlock(void)
  124. {
  125.   int lvl;
  126.   char *p;
  127.  
  128.   p=getvar("wims_classlock"); if(p==NULL) lvl=0; else {
  129.     p=find_word_start(p);
  130.     lvl=*p-'0'; if(lvl<0 || lvl>7) lvl=0;
  131.   }
  132.   if(lvl==7) { /* closed */
  133.     p=getvar("wims_user");
  134.     if(p==NULL || strcmp(p,"supervisor")!=0) user_error("class_closed");
  135.   }
  136.   if(lvl==2 || lvl==4 || lvl==6) { /* https */
  137.     p=getenv("HTTPS");
  138.     if(p==NULL || strcasecmp(p,"on")!=0) user_error("need_https");
  139.   }
  140.   if(lvl==3 || lvl>=5) {
  141.     if(strcmp(last_host,remote_addr)!=0) user_error("bad_host");
  142.   }
  143.   if((lvl==1 || lvl>=4) && cookiegot[0]==0) { /* cookie */
  144.     setcookie=1;
  145.     setvar("cookie_module",getvar(ro_name[ro_module]));
  146.     setvar("cookie_cmd",getvar(ro_name[ro_cmd]));
  147.     force_setvar(ro_name[ro_module],home_module);
  148.     force_setvar(ro_name[ro_cmd],commands[cmd_new]);
  149.     setvar("wims_askcookie","yes");
  150.     return;
  151.   }
  152.   else setcookie=0;
  153. }
  154.  
  155. /* get static session variables */
  156. void get_static_session_var(void)
  157. {
  158.   char *p, *pe, *p2, *p3;
  159.   char sbuf[MAX_FNAME+1], tbuf[MAX_LINELEN+1];
  160.   mystrncpy(sbuf,session_prefix,sizeof(sbuf));
  161.   for(p=sbuf+strlen(sbuf);p>sbuf && *p!='_' && *p!='/'; p--);
  162.   if(p>sbuf && *p=='_') *p=0;
  163.   accessfile(tbuf,"r","%s/var.stat",sbuf);
  164.   p=strrchr(sbuf,'/'); if(p!=NULL) p++; else p=sbuf;
  165.   mktmpdir(p);
  166.   for(p=find_word_start(tbuf);*p;p=find_word_start(pe)) {
  167.     pe=strchr(p,'\n'); if(pe!=NULL) *pe++=0; else pe=p+strlen(p);
  168.     p2=strchr(p,'='); if(p2==NULL) continue;
  169.     *p2++=0; force_setvar(p,p2);
  170.   }
  171.   p=getvar("wims_class"); if(p==NULL || *p==0) return;
  172.   mkfname(class_dir,"%s/%s",class_base,p);
  173.   classlock();
  174.   p3=getvar("wims_class_refcolor");
  175.   if(p3!=NULL && *p3!=0) force_setvar("wims_ref_bgcolor",p3);
  176.   p3=getvar("wims_class_ref_menucolor");
  177.   if(p3!=NULL && *p3!=0) force_setvar("wims_ref_menucolor",p3);
  178.   p3=getvar("wims_class_ref_button_color");
  179.   if(p3!=NULL && *p3!=0) force_setvar("wims_ref_button_color",p3);
  180.   p3=getvar("wims_class_ref_button_bgcolor");
  181.   if(p3!=NULL && *p3!=0) force_setvar("wims_ref_button_bgcolor",p3);
  182.   p3=getvar("wims_class_ref_button_help_bgcolor");
  183.   if(p3!=NULL && *p3!=0) force_setvar("wims_ref_button_help_bgcolor",p3);
  184.   p3=getvar("wims_class_ref_button_help_color");
  185.   if(p3!=NULL && *p3!=0) force_setvar("wims_ref_button_help_color",p3);
  186.   p2=getvar(ro_name[ro_module]);
  187.   if(p2==NULL || strncmp(p2,"classes/",strlen("classes/"))!=0) return;
  188.   mkfname(sbuf,"classes/%s",lang);
  189.   if(strcmp(sbuf,p2)!=0) force_setvar(ro_name[ro_module],sbuf);
  190. }
  191.  
  192. /* set one static session variable */
  193. void set_static_session_var(char *name, char *val)
  194. {
  195.   char *p;
  196.   char sbuf[MAX_FNAME+1], tbuf[MAX_LINELEN+1];
  197.   mystrncpy(sbuf,session_prefix,sizeof(sbuf));
  198.   for(p=sbuf+strlen(sbuf);p>sbuf && *p!='_' && *p!='/'; p--)
  199.   ;
  200.   if(p>sbuf && *p=='_') *p=0;
  201.   snprintf(sbuf+strlen(sbuf),sizeof(sbuf)-strlen(sbuf),"/var.stat");
  202.   snprintf(tbuf,sizeof(tbuf),"%s=%s",name,val);
  203.   setdef(sbuf,tbuf); setvar(name,val);
  204. }
  205.  
  206. /* The session is probably robot. */
  207. void robot_doubt(void)
  208. {
  209.   char *h, *p;
  210.  
  211.   p=getvar("special_parm"); h=getvar("module");
  212.   if(p==NULL || h==NULL) {
  213.     bad: user_error("robot_doubt"); return;
  214.   }
  215.   p=find_word_start(p); strip_trailing_spaces(p);
  216.   if(strcmp(p,"wims")!=0 || strcmp(h,home_module)!=0) goto bad;
  217.   set_static_session_var("wims_robotcheck","manual");
  218. }
  219.  
  220. /* User has changed module within an operation.
  221.  * Probably due to robot access.
  222.  */
  223. void bad_module(void)
  224. {
  225.   char *p;
  226.   p=getvar("wims_user"); if(p==NULL) p="";
  227.   if(*p==0 && strcmp(robotcheck,"manual")!=0) set_static_session_var("wims_robotcheck","robot");
  228.   else setvar("wims_human_access","yes");
  229.   user_error("module_change");
  230. }
  231.  
  232. /* returns 1 if session directory exists */
  233. int session_exists(char *s)
  234. {
  235.   if(ftest(mkfname(NULL,"../%s/%s/var",SESSION_BASE,s))==is_file) return 1;
  236.   else return 0;
  237. }
  238.  
  239. /* Check the validity of session number .
  240.  * returns 0 if OK, else -1.
  241.  */
  242. int check_session(void)
  243. {
  244.   char tbuf[MAX_LINELEN+1], vbuf[MAX_LINELEN+1];
  245.   char *p, *pp, *pr;
  246.   int i,m,n,pl,rapid,badmod;
  247.   struct stat st;
  248.  
  249.   rapid=badmod=0; pr="";
  250.   if(fopen_session_var_file("r")==NULL) return -1;
  251.   if(ftest(s2_prefix)!=is_dir) mkdirs(s2_prefix);
  252.   session_var_ready=1; memmove(&svar_file,&m_file,sizeof(WORKING_FILE));
  253. /* REMOTE_ADDR */
  254.   wgetline(vbuf,MAX_LINELEN,&m_file);
  255.   mystrncpy(last_host,vbuf+strlen("REMOTE_ADDR="),sizeof(last_host));
  256.   m_file.linepointer++; /* now it points to query_string */
  257.   pp=getenv("QUERY_STRING");
  258.   if(pp!=NULL && *pp!=0 && strlen(pp)<=MAX_LINELEN) {
  259. /* we compare the query string with the last one. */
  260.     char *p1, *p2;
  261.     wgetline(tbuf,MAX_LINELEN,&m_file);
  262.     p1=tbuf+strlen("QUERY_STRING=");
  263.     if(strncmp(tbuf,"QUERY_STRING=",strlen("QUERY_STRING="))==0 &&
  264.         strcmp(pp,p1)==0 && strstr(session_prefix,"_test")==NULL) {
  265. /* query string does not change */
  266.       if(ftest(mkfname(NULL,"%s/%s",s2_prefix,lastout))==is_file &&
  267.             ftest_size > 0) {
  268.         uselast:
  269.         putlastout(); delete_pid(); exit(0);
  270.       }
  271.       else {
  272.         if(cmd_type==cmd_new || cmd_type==cmd_renew ||
  273.              cmd_type==cmd_reply || cmd_type==cmd_next) {
  274.           cmd_type=cmd_resume;
  275.           force_setvar(ro_name[ro_cmd],"resume");
  276.           forceresume=1;
  277.         }
  278.       }
  279.     }
  280. /* stop rapidfire requests */
  281.     if((cmd_type==cmd_new || cmd_type==cmd_renew) &&
  282.         strncmp(p1,"session=",strlen("session="))==0 &&
  283.         strncmp(pp,"session=",strlen("session="))==0) {
  284.       p1=strchr(p1,'&'); if(p1==NULL) p1="";
  285.       p2=strchr(pp,'&'); if(p2==NULL) p2=""; pr=p2;
  286.       if(strcmp(p1,p2)==0) rapid=1;
  287.     }
  288.   }
  289.   m_file.linepointer=3;
  290.   wgetline(vbuf,MAX_LINELEN,&m_file); /* stored user_agent */
  291. /*    p=getenv("HTTP_USER_AGENT"); if(p==NULL) p="";
  292.     if(strcmp(vbuf+strlen("HTTP_USER_AGENT="),p)!=0) bad_ident(); */
  293.   m_file.linepointer=HEADER_VAR_NO;
  294.   pl=strlen(var_prefix);i=-1;
  295.   while(wgetline(tbuf,MAX_LINELEN,&m_file)!=EOF) {
  296.     if(tbuf[0]==0) break; /* blank line */
  297.     if(strncmp(tbuf,var_prefix,pl)!=0) break;
  298.     i++;if(i>=RO_NAME_NO) break;
  299.     for(n=0;n<varget_no && varget[n]!=i;n++)
  300.     ;
  301.       if(n>=varget_no) continue;
  302.     m=pl+strlen(ro_name[i]);
  303.     if(tbuf[m]!='=' || tbuf[m+1]==0) continue;
  304.     if(i==ro_module && cmd_type!=cmd_new && cmd_type!=cmd_intro && cmd_type!=cmd_help) {
  305.       char *pp;
  306.       pp=getvar(ro_name[i]);
  307.       if(pp!=NULL && *pp!=0 && strcmp(pp,tbuf+m+1)!=0) badmod=1;
  308.     }
  309.     if(i==ro_lang && !user_lang)
  310.       force_setvar(ro_name[i],tbuf+m+1);
  311.     else setvar(ro_name[i],tbuf+m+1);
  312.   }
  313. /* recover internal variables */
  314.   do
  315.   {
  316.     char *v;
  317.     if(tbuf[0]==0) break;
  318.     v=strchr(tbuf,'=');
  319.     if(v==NULL) break; else *(v++)=0;
  320.     if(strncmp(tbuf,var_prefix,strlen(var_prefix))!=0) setenv(tbuf,v,1);
  321.     else if(tbuf[strlen(var_prefix)]) force_setvar(tbuf+strlen(var_prefix),v);
  322.   }
  323.   while(wgetline(tbuf,MAX_LINELEN,&m_file)!=EOF);
  324.   get_static_session_var();
  325.   robotcheck=getvar("wims_robotcheck"); if(robotcheck==NULL) robotcheck="";
  326. /* form access means manual access. Mark this. */
  327.   if(form_access && strcmp(robotcheck,"manual")!=0) {
  328.     robotcheck="manual";
  329.     set_static_session_var("wims_robotcheck","manual");
  330.   }
  331.   else if(strcmp(robotcheck,"robot")==0) robot_doubt();
  332.   if(badmod) bad_module();
  333.   if(cookiegot[0]!=0) {
  334.     p=getvar("wims_sescookie");
  335.     if(p!=NULL && *p!=0 && strcmp(cookiegot,p)!=0) bad_ident();
  336.   }
  337.   p=getvar("wims_sreferer");
  338.   if(p!=NULL && *p!=0) {
  339.     setenv("HTTP_REFERER",p,1); setvar("httpd_HTTP_REFERER",p);
  340.   }
  341.   if(rapid) {
  342.     int rapidfiredelay;
  343.     char *pw, fnbuf[MAX_FNAME+1];
  344. /* Delay: 10 seconds within worksheets, 1 second otherwise. */
  345.     pw=getvar("wims_developer");
  346.     if(pw!=NULL && *pw!=0) goto delcheckend;
  347.     pw=getvar("wims_user");
  348.     if(pw==NULL || *pw==0 || strcmp(pw,"supervisor")==0) rapidfiredelay=1;
  349.     else {
  350.       pw=strstr(pr,"&+worksheet=");
  351.       if(pw!=NULL && myisdigit(*(pw+strlen("&+worksheet=")))) rapidfiredelay=10;
  352.         else rapidfiredelay=2;
  353.     }
  354.     if(ftest(mkfname(fnbuf,"%s/%s",s2_prefix,lastout))==is_file
  355.         && ftest_size > 0
  356.         && stat(fnbuf,&st) == 0
  357.         && st.st_mtime > nowtime-rapidfiredelay && st.st_mtime <= nowtime)
  358.       goto uselast;
  359.   }
  360. /* set protocol string */
  361.   delcheckend: pp=getvar("wims_protocol");
  362.   if(pp!=NULL && strcmp(pp,"https")==0) {
  363.     protocol="https"; set_protocol();
  364.   }
  365.   useropts(); return 0;
  366. }
  367. /* check whether a session is trapped. */
  368. void trap_check(char *s)
  369. {
  370.   char buf[64];
  371.   char *p;
  372.   time_t t1;
  373.  
  374.   setvar("wims_session_expired",s);
  375.   accessfile(tmplbuf,"r","../tmp/log/trap.check");
  376.   if(tmplbuf[0]==0) return;
  377.   p=getenv("REMOTE_ADDR");if(p==NULL) return;
  378.   snprintf(buf,sizeof(buf),":%s,%s,",s,p);
  379.   p=strstr(tmplbuf,buf); if(p==NULL) return;
  380.   p+=strlen(buf);*find_word_end(p)=0;
  381.   t1=atoi(p);
  382.   if(t1>nowtime) user_error("trapped");
  383. }
  384.  
  385. char cars[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  386.  
  387. void set_cookie(void)
  388. {
  389.   #define keylen 20
  390.   char sesbuf[16], keybuf[keylen+8];
  391.   char *p;
  392.  
  393.   p=getvar(ro_name[ro_session]); if(p==NULL) return;
  394.   mystrncpy(sesbuf,p,sizeof(sesbuf));
  395.   if(strchr(sesbuf,'_')==NULL) { /* main session */
  396.     int i;
  397.     for(i=0;i<keylen;i++) keybuf[i]=cars[random()%36];
  398.     keybuf[keylen]=0; cookiegot[0]=0;
  399.     snprintf(cookieset,sizeof(cookieset),"%s-%s",sesbuf,keybuf);
  400.     set_static_session_var("wims_sescookie",cookieset);
  401.     setcookie=1;
  402.   }
  403.   else { /* subsession */
  404.     p=getvar("wims_sescookie"); if(p && *p)
  405.     mystrncpy(cookieset,p,sizeof(cookieset));
  406.   }
  407. }
  408.  
  409. /* create a session number */
  410. void create_session(void)
  411. {
  412.   long t; char session_str[64],*s;
  413.   char *p, ses_dir_buf[MAX_FNAME+1], sesrandbuf[MAX_LINELEN+1];
  414.   int i;
  415.  
  416. /* no session is created for robots */
  417.   if(robot_access) return;
  418.   sesrandbuf[0]=0;
  419. /* If session is given in request_string: use it. */
  420.   s=getvar(ro_name[ro_session]); if(s==NULL) goto creat;
  421.   mystrncpy(session_str,s,sizeof(session_str));
  422.   s=strchr(session_str,'.'); if(s!=NULL) *s=0;
  423.   s=session_str;
  424.   if(*s!=0) {
  425.     int i;
  426.     mkfname(ses_dir_buf,"%s/%s",session_dir,s);
  427.     i=ftest(ses_dir_buf);
  428.     if(i<0) {
  429.       trap_check(s);
  430. /* subsession */
  431.       if(strlen(s)>10 && strchr(s,'_')!=NULL) {
  432.         char *tt;
  433.         tt=strrchr(ses_dir_buf,'_'); if(tt!=NULL) *tt=0;
  434. /* parent session gone. */
  435.         if(ftest(ses_dir_buf)<0) goto creat;
  436.         goto creat2;
  437.       }
  438.       else goto creat;
  439.     }
  440.     if(i!=is_dir) {
  441.       trap_check(s);
  442.       remove_tree(ses_dir_buf); goto creat;
  443.     }
  444.     return;
  445.   }
  446.   creat:
  447.   t=create_job_ident();
  448.   for(i=0;i<MAX_SESRANDOM;i++)
  449.     snprintf(sesrandbuf+strlen(sesrandbuf),
  450.             sizeof(sesrandbuf)-strlen(sesrandbuf),
  451.             "%d,",sesrandomtab[i]);
  452.   sesrandbuf[strlen(sesrandbuf)-1]='\n';
  453.   snprintf(session_str,sizeof(session_str),"%c%c%08lX",
  454.           cars[random()%36],cars[random()%36],t);
  455.   creat2:
  456.   force_setvar(ro_name[ro_session],session_str);
  457.   setsesdir(session_str);
  458. /* check whether the environment is created. */
  459.   s=getvar(ro_name[ro_session]);
  460.   if(s==NULL || strcmp(s,session_str))
  461.     internal_error("cannot_create_session_number");
  462.   snprintf(ses_dir_buf,sizeof(ses_dir_buf)-100,"%s/%s",
  463.           session_dir,session_str);
  464.   if(mkdir(ses_dir_buf,S_IRWXU)==-1)
  465.     internal_error("cannot_create_session_directory");
  466.   mkfname(s2_prefix,"%s/%s",s2_dir,session_str);
  467.   if(mkdir(s2_prefix,S_IRWXU)==-1) mkdirs(s2_prefix);
  468.   mystrncpy(session_prefix,ses_dir_buf,sizeof(session_prefix)); create_pid();
  469.   if(strchr(session_str,'_')==NULL) {
  470.     if((s=getenv("HTTP_REFERER"))!=NULL && *s!=0 && strlen(s)<MAX_FNAME-20
  471.         && strstr(s,"wims")==NULL) {
  472.       char *tt;
  473.       tt=getenv("SERVER_NAME");
  474.       if(tt==NULL || *tt==0 || strstr(s,tt)==NULL)
  475.         set_static_session_var("wims_sreferer",s);
  476.     }
  477.     if(sesrandbuf[0]) set_static_session_var("wims_sesrandom",sesrandbuf);
  478.   }
  479. /* determine http protocol name. How to detect? */
  480.   p=getenv("HTTPS"); if(p!=NULL && strcmp(p,"on")==0) {
  481.       protocol="https"; set_protocol();
  482.   }
  483.   force_setvar("wims_protocol",protocol);
  484.   new_session=1; session_serial=0;
  485.   setvar("wims_new_session","yes");
  486.   if(strchr(session_str,'_')!=NULL) get_static_session_var();
  487.   set_cookie();
  488. }
  489.  
  490. /* Register time of the request. */
  491. void set_req_time(void)
  492. {
  493.   char tstr[64];
  494.  
  495.   snprintf(tstr,sizeof(tstr),"%04d-%02d-%02d.%02d:%02d:%02d=%lu",
  496.          (now->tm_year)+1900, (now->tm_mon)+1, now->tm_mday,
  497.          now->tm_hour, now->tm_min, now->tm_sec, nowtime);
  498.   force_setvar("wims_req_time",tstr);
  499.   if(cmd_type == cmd_new || cmd_type == cmd_renew)
  500.     force_setvar("wims_module_start_time",tstr);
  501.   if(new_session) force_setvar("wims_session_start_time",tstr);
  502. }
  503. /* choose a seed and put in in wims_seed*/
  504. void seed_time(void)
  505. {
  506.   int r; char buf[64];
  507.   struct timeval t;
  508.   gettimeofday(&t,NULL);
  509.   r=t.tv_usec+t.tv_sec*1000;
  510.   if(r<=0) r=1-r;
  511.   snprintf(buf,sizeof(buf),"%d",r);
  512.   force_setvar("wims_seed",buf);
  513. }
  514.  
  515. /* set up module_prefix. */
  516. void set_module_prefix(void)
  517. {
  518.   char tbuf[MAX_FNAME+1], mmbuf[MAX_FNAME+1], *p, *pp, *ps;
  519.   int t,ft;
  520.   struct stat st;
  521.  
  522.   isclassmodule=0;
  523.   p=getvar(ro_name[ro_module]);
  524.   if(p==NULL || *p==0) user_error("no_module_name");
  525. /* security measure: we should not allow users to go back to
  526.  * parent directories.
  527.  */
  528.   if(strstr(p,parent_dir_string)!=NULL) user_error("wrong_module");
  529.   if(strncmp(p,"classes/",strlen("classes/"))==0) isclassmodule=1;
  530.   if(strncmp(p,"devel/",strlen("devel/"))==0) isdevelmodule=1;
  531.   mkfname(module_prefix,"%s/%s",module_dir,p);
  532. /* Now no symbolic link should appear in the module path. */
  533.   mkfname(tbuf,"modules/%s",p);
  534.   for(t=0,ps=pp=strchr(tbuf+strlen("modules/"),'/'); pp;
  535.   *pp='/', ps=pp, pp=strchr(pp+1,'/'), t++) {
  536.   *pp=0; if(lstat(tbuf,&st)) user_error("wrong_module");
  537.   if(t>0 && S_ISLNK(st.st_mode)) {
  538.     if(strcmp(ps,"/local")!=0 ||
  539.           strncmp(tbuf,"modules/home",strlen("modules/home"))==0)
  540.       user_error("wrong_module");
  541.     }
  542.   }
  543. /* Check validity of the module. */
  544.   mkfname(tbuf,"%s/%s",module_prefix,html_file);
  545.   ft=stat(tbuf,&st);
  546.   if(ft!=0 && p[strlen(p)-3]!='.') {
  547.     int i,j;
  548.     char *l;
  549.     l=getvar(ro_name[ro_lang]);
  550.     j=available_lang_no;
  551.     for(i=-1;i<j && ft!=0;i++) {
  552.       if(i<0) mkfname(mmbuf,"%s.%s",p,l);
  553.       else mkfname(mmbuf,"%s.%s",p,available_lang[i]);
  554.       mkfname(module_prefix,"%s/%s",module_dir,mmbuf);
  555.       mkfname(tbuf,"%s/%s",module_prefix,html_file);
  556.       ft=stat(tbuf,&st);
  557.     }
  558.     if(ft==0) force_setvar(ro_name[ro_module],mmbuf);
  559.   }
  560.   if(ft!=0 && !isclassmodule) user_error("wrong_module");
  561.   setenv("module_dir",module_prefix,1); setvar("module_dir",module_prefix);
  562.   module_index();
  563. }
  564.  
  565. /* set up session_prefix. */
  566. int set_session_prefix(void)
  567. {
  568.   char *p, s[32];
  569.  
  570.   if(robot_access) {
  571.     mystrncpy(session_prefix,robot_session,sizeof(session_prefix));
  572.     mystrncpy(s2_prefix,robot_session,sizeof(session_prefix));
  573.     return 0;
  574.   }
  575.   p=getvar(ro_name[ro_session]);
  576.   if(p==NULL || *p==0) user_error("no_session");
  577. /* same reason as for modules */
  578.   if (strchr(p,'/')!=NULL || strstr(p,parent_dir_string)!=NULL
  579.      || *find_word_end(p)!=0) user_error("wrong_session");
  580.   mystrncpy(s,p,sizeof(s));
  581.   p=strchr(s,'.'); if(p!=NULL) *p=0;
  582.   mkfname(session_prefix,"%s/%s",session_dir,s);
  583.   p=strstr(session_prefix,"_mhelp"); if(p!=NULL) *p=0;
  584.   if(ftest(session_prefix)!=is_dir) return -1;
  585.   mkfname(s2_prefix,"%s/%s",s2_dir,s);
  586.   setenv("session_dir",session_prefix,1);
  587.   setenv("s2_dir",s2_prefix,1);
  588.   if(ftest(mkfname(NULL,"%s/.trap",s2_prefix))==is_file)
  589.       user_error("trapped");
  590.   return 0;
  591. }
  592.  
  593. /* check reserved name values in query_string */
  594. void parse_ro_names(void)
  595. {
  596.   int i;
  597.   char *cmd, *p;
  598.   char sesbuf[64];
  599.   create:
  600.   cmd=getvar(ro_name[ro_cmd]);
  601.   if(cmd==NULL || *cmd==0) user_error("no_command");
  602.   for(i=0;i<CMD_NO;i++) if(strcmp(cmd,commands[i])==0) break;
  603.   if(i>=CMD_NO) user_error("bad_command");
  604.   cmd_type=i;
  605.   if(cmd_type == cmd_new) {
  606.     create_session();
  607.     if(set_session_prefix()==0) {
  608.       check_session();
  609.       set_module_prefix();
  610.     }
  611.     else goto redo;
  612.   }
  613.   if (set_session_prefix()==-1 || (cmd_type != cmd_new && check_session())) {
  614.     redo:
  615.     force_setvar(ro_name[ro_cmd],commands[cmd_new]);
  616.     if(strcmp(ro_name[ro_module],home_module)!=0) user_var_no=0;
  617.      goto create;
  618.   }
  619.   if(!new_session) create_pid();
  620.   session_serial++;
  621.   if(robot_access) session_serial=1;
  622.   snprintf(sesbuf,sizeof(sesbuf),"%d",session_serial);
  623.   force_setvar("wims_session_serial",sesbuf);
  624.   p=getvar(ro_name[ro_session]);
  625.   if(p==NULL || *p==0) internal_error("parse_ro_names(): bad session.\n");
  626.   mystrncpy(sesbuf,p,sizeof(sesbuf));
  627.   p=strchr(sesbuf+5,'.');
  628.   if(p!=NULL) *p=0;
  629.   mktmpdir(sesbuf);
  630.   if(!robot_access) {
  631.     setsesdir(sesbuf);
  632.     p=strchr(sesbuf,'_');
  633.     if(p!=NULL) force_setvar("wims_subsession",p);
  634.   }
  635.   snprintf(sesbuf+strlen(sesbuf),sizeof(sesbuf)-strlen(sesbuf),
  636.           ".%d",session_serial);
  637.   force_setvar(ro_name[ro_session],sesbuf);
  638.   if(cmd_type != cmd_new) set_module_prefix();
  639.   set_req_time();
  640.   if(robot_access) check_load(0);
  641.   else {
  642.     if(new_session) auth();
  643.     else {
  644.       p=getvar("wims_user"); if(p==NULL || *p==0) check_load(2);
  645.     }
  646.   }
  647.   if(cmd_type==cmd_help && open_working_file(&m_file,module_about_file)==0)
  648.     var_proc(NULL,0);
  649. }
  650.  
  651.  
  652. /* returns positive or 0 if var_def found, otherwise returns -1. */
  653. int var_def_check(char *name)
  654. {
  655.   char *p, nbuf[MAX_NAMELEN+1];
  656.   int i,tt;
  657.  
  658.   tt=-1;
  659.   for(p=name+strlen(name);p>name && myisdigit(*(p-1));p--)
  660.   ;
  661.   if(*p && *p!='0' && p>name) tt=atoi(p);
  662.   else p=name+strlen(name);
  663.   if(p>name+MAX_NAMELEN) p=name+MAX_NAMELEN;
  664.   memmove(nbuf,name,p-name); nbuf[p-name]=0;
  665.   i=search_list(var_def,defined_var_total,sizeof(var_def[0]),nbuf);
  666.   if(i<0) return -1;
  667.   while(i>0 && tt<var_def[i].beg && strcmp(nbuf,var_def[i-1].name)==0) i--;
  668.   while(i<defined_var_total-1 && tt>var_def[i].end &&
  669.            strcmp(nbuf,var_def[i+1].name)==0) i++;
  670.   if(tt<var_def[i].beg || tt>var_def[i].end) return -1;
  671.   return i;
  672. }
  673.  
  674. int var_def_name(char *n, int v)
  675. {
  676.   char *q, *r;
  677.   int j;
  678.   if(strlen(n)>=MAX_NAMELEN) module_error("defn_too_long");
  679.   var_def[v].name=n;
  680.   if((strncmp(n,wims_prefix,wpflen)==0 &&
  681.         (strncmp(n,"wims_priv_",strlen("wims_priv_"))==0 ||
  682.         search_list(internal_name,INTERNAL_NAME_NO,
  683.                sizeof(internal_name[0]),n+wpflen)>=0)) ||
  684.           search_list(ro_name,RO_NAME_NO,sizeof(ro_name[0]),n)>=0) {
  685.     setvar("wims_reserved_name",n);
  686.     module_error("name_is_reserved");
  687.   }
  688.   for(q=n;myisalnum(*q) || *q=='_'; q++);
  689.   if(q==n) {
  690.     illegal: setvar("wims_bad_name",n);
  691.     module_error("illegal_name");
  692.   }
  693.  
  694.   if(*q=='[') {
  695.     *q++=0; r=find_matching(q,']');
  696.     if(r==NULL) goto illegal;
  697.     *r=0; int i;
  698.     for(i=0;i<special_name_no && strcmp(q, special_name[i].name)!=0; i++);
  699.     if(i<special_name_no) j=special_name[i].value; else j=atoi(q);
  700.     if(j<1) j=1;
  701.     if(j>MAX_VAR_NUM) j=MAX_VAR_NUM;
  702.     var_def[v].beg=1; var_def[v].end=j;
  703.     return j;
  704.   }
  705.   if(*q) goto illegal;
  706.   for(r=q; r>n && myisdigit(*(r-1)); r--);
  707.   if(*r && *r!='0' && r>n) {
  708.     var_def[v].beg=var_def[v].end=atoi(r); *r=0;
  709.   }
  710.   else var_def[v].beg=var_def[v].end=-1;
  711.   return 1;
  712. }
  713.  
  714. int var_def_allow(char *p, int v)
  715. {
  716.   int i;
  717.   for(i=0;i<VAR_ALLOW_NO && strcasecmp(p,var_allow[i])!=0; i++);
  718.   if(i>=VAR_ALLOW_NO) module_error("bad_allow");
  719.   else var_def[v].allow=i;
  720.   return i;
  721. }
  722.  
  723. int varsort(const void *p1, const void *p2)
  724. {
  725.   int i; const struct VAR_DEF *pp1, *pp2;
  726.   pp1=p1; pp2=p2; i=strcmp(pp1->name,pp2->name);
  727.   if(i) return i; else return pp1->end - pp2->end;
  728. }
  729.  
  730. /* parse module's variable definitions */
  731. void get_var_defs(void)
  732. {
  733.   int i, j, k, v, add;
  734.   char *p, *p1, *wlist[MAX_VAR_NUM];
  735.  
  736.   defined_var_total=0;
  737.   if(read_module_file(var_def_file)!=0) return;
  738.   var_def_buf=m_file.textbuf;
  739.   for(m_file.l=v=add=0;v<MAX_VAR_NUM && m_file.l<m_file.linecnt;m_file.l++) {
  740.     if(m_file.lines[m_file.l].isstart!=1) continue;
  741.     p=find_word_start(m_file.lines[m_file.l].address);
  742.     if(*p==0 || *p==comment_prefix_char) continue;  /* empty or comment lines */
  743.     items2words(p);
  744.     if((p1=strchr(p,':'))!=NULL) {  /* new format */
  745.       *p1=' '; i=cutwords(p,wlist,MAX_VAR_NUM); if(i<=1) continue;
  746.       k=var_def_allow(wlist[0],v);
  747.       for(j=1;j<i && v<MAX_VAR_NUM;j++) {
  748.         add+=var_def_name(wlist[j],v); var_def[v].allow=k;
  749.         v++;
  750.       }
  751.     }
  752.     else {
  753.       i=cutwords(p,wlist,3); if(i<2) module_error("too_few_columns");
  754.       add+=var_def_name(wlist[0],v);
  755.       var_def_allow(wlist[1],v);
  756.       var_def[v].defined_in_parm=0;
  757.       v++;
  758.     }
  759.   }
  760.   for(j=0;j<INTERNALDEF_NAME_NO;j++) {
  761.     add+=var_def_name(internaldef_name[j].name,v);
  762.     var_def[v].allow=internaldef_name[j].stat;
  763.     v++;
  764.   }
  765.  
  766.   if(v>=MAX_VAR_NUM) module_error("too_many_variables");
  767.   defined_var_total=v;
  768.   qsort(var_def,v,sizeof(var_def[0]),varsort);
  769.   p=getvar(ro_name[ro_module]);
  770.   if(p==NULL || strncmp(p,"devel/",6)!=0) return;
  771.   for(v=1;v<defined_var_total;v++) {
  772.     if(strcmp(var_def[v].name,var_def[v-1].name)==0 &&
  773.         var_def[v].beg<=var_def[v-1].end) {
  774.       for(j=0;j<INTERNALDEF_NAME_NO;j++) {
  775.         if(strcmp(internaldef_name[j].name,var_def[v-1].name)==0){
  776.           setvar("wims_reserved_name",internaldef_name[j].name);
  777.           module_error("name_is_reserved");
  778.         }
  779.       }
  780.       setvar("wims_bad_name",var_def[v].name);
  781.       module_error("multiple_declaration");
  782.     }
  783.   }
  784. }
  785.  
  786. /* returns 1 if hacked, else 0. */
  787. int try_hack(char *var)
  788. {
  789.   int i, al;
  790.   char vbuf[16];
  791.   i=var_def_check(var);
  792.   if(i<0) return 0;
  793.   al=var_def[i].allow;
  794.   if(al != var_allow_any) switch(cmd_type) {
  795.     case cmd_new:
  796.     case cmd_renew: {
  797.       if (al != var_allow_init && al != var_allow_config) return 0;
  798.       else break;
  799.     }
  800.     case cmd_config: {
  801.       if(al != var_allow_config) return 0;
  802.       else break;
  803.     }
  804.     case cmd_reply: {
  805.       if(al != var_allow_reply) return 0;
  806.       else break;
  807.     }
  808.     case cmd_help: {
  809.       if(al != var_allow_help) return 0;
  810.       else break;
  811.     }
  812.     default: return 0;
  813.   }
  814.   snprintf(vbuf,sizeof(vbuf),"%d",(int) irand(21));
  815.   var_hacking=1; setvar(var,vbuf); var_hacking=0;
  816.   return 1;
  817. }
  818.  
  819. /* set environ variables from last session save
  820.  * The session var file starts with variables which should not
  821.  * be restored. Variables which are restored follow a blank line. */
  822. void set_vars_from_session(void)
  823. {
  824.   char lbuf[MAX_LINELEN+1];
  825.   int i;
  826.   char *p;
  827.  
  828.   if(session_var_ready) memmove(&m_file,&svar_file,sizeof(WORKING_FILE));
  829.   else fopen_session_var_file("r");
  830.   /* look for the first blank line. */
  831.   for(i=0;i<m_file.linecnt && (m_file.lines[i].isstart==0 || m_file.lines[i].llen>0);i++);
  832.   for(i++;i<m_file.linecnt && m_file.lines[i].llen==0;i++);
  833.   if(i>=m_file.linecnt) return;
  834.   m_file.linepointer=i;
  835.   if(isdevelmodule && strstr(session_prefix,"_test")==NULL) isdevelmodule=0;
  836.   while(wgetline(lbuf,MAX_LINELEN, &m_file)!=EOF) {
  837.     p=strchr(lbuf,'=');
  838.     if(p==NULL || p<=lbuf || isspace(lbuf[0]) ) {
  839.   /* this time it is corrupted var file */
  840.       call_ssh("cat %s/var >%s/corrupt.var.bak",session_prefix,log_dir);
  841.       internal_error("get_vars_from_session(): corrupt session variable file.");
  842.     }
  843.     *p=0;p++;
  844.     /* Here we suppose that nobody can tamper session variable
  845.      * file, and do not check variable names against module's
  846.      * definition file. Policy under reserve. */
  847.     /* We do not allow override though.
  848.      * Especially because reply variables should
  849.      * be preserved. */
  850.     if(strncmp(lbuf,var_prefix,strlen(var_prefix))!=0)
  851.       setenv(lbuf,p,0);
  852.     else
  853.       if(lbuf[strlen(var_prefix)]!=0 && getvar(lbuf+strlen(var_prefix))==NULL) {
  854.         if(!isdevelmodule || !try_hack(lbuf+strlen(var_prefix))) {
  855.           setvar(lbuf+strlen(var_prefix),p);
  856.         }
  857.       }
  858.   }
  859.   close_working_file(&m_file,0);
  860. }
  861.  
  862. void start_tracefile(void)
  863. {
  864.   int i;
  865.   putc('\n',trace_file);
  866.   for(i=1; i<=2*trace_indent; i++) putc(' ',trace_file);
  867.   fprintf(trace_file,"%s:",m_file.filepath);
  868.   fflush(trace_file);
  869.   trace_indent++;
  870. }
  871.  
  872. void stop_tracefile(void)
  873. {
  874.   trace_indent--;
  875. }
  876.  
  877. /* Initialize environment variables according to module's
  878.  * variable init or calculation file.
  879.  * init is only used when cmd=new or renew.
  880.  * Requires get_var_defs be run first. */
  881. void var_proc(char *fname,int cache)
  882. {
  883.   int  t;
  884.   char *p, tbuf[MAX_LINELEN+1];
  885.  
  886.   if(fname!=NULL && read_module_file(fname)) return;
  887.   if(untrust&6) get_var_privileges();
  888.   if (trace_file) start_tracefile();
  889.   while(m_file.linepointer<m_file.linecnt) {
  890.     if (trace_file) {fprintf(trace_file," %d",m_file.linepointer+1);
  891.       fflush(trace_file);
  892.     }
  893.     t=m_file.lines[m_file.linepointer].isstart;
  894.     if((t&~2)!=1 || m_file.lines[m_file.linepointer].llen==0) {
  895.       m_file.linepointer++; continue;
  896.     }
  897.     wgetline(tbuf,MAX_LINELEN,&m_file); substnest=0;
  898.     p=find_word_start(tbuf); if(*p==0) continue;
  899.     if((t&2)!=0) exec_main(p+1);
  900.     else exec_set(p);
  901.   }
  902.   if (trace_file) stop_tracefile();
  903.   close_working_file(&m_file,cache);
  904. }
  905.  
  906. /* Deposit the content of wims_deposit into a file */
  907. void var_deposit(char *p)
  908. {
  909.   char fn[MAX_FNAME+1];
  910.   int l,fd;
  911.   if(!trusted_module()) return;
  912.   if(deplen>0) l=deplen; else {
  913.     while(isspace(*p)) p++;
  914.     l=strlen(p);
  915.   }
  916.   if(l<=0) return;
  917.   if(l>MAX_DEPOSITLEN) l=MAX_DEPOSITLEN; /* silent truncation, should not occur */
  918.   mkfname(fn,"%s/user-deposit",session_prefix);
  919.   fd=creat(fn,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); if(fd==-1) return;
  920.   write(fd,p,l); close(fd);
  921.   snprintf(fn,sizeof(fn),"%u",l); setvar("wims_deposit_len",fn);
  922. }
  923.  
  924. /* Check and set variables passed in query_string */
  925. void set_vars_from_parm(void)
  926. {
  927.   int i,j,al;
  928.   char *s, vbuf[MAX_LINELEN+1];
  929.   if(forceresume) return;
  930.   for(i=0; i<user_var_no; i++) {
  931.     j=var_def_check(user_variable[i].name);
  932.     if(j<0) continue;
  933.     /* check permissions */
  934.     al=var_def[j].allow;
  935.     if(al != var_allow_any) switch(cmd_type) {
  936.       case cmd_new:
  937.       case cmd_renew:
  938.         if (al != var_allow_init && al != var_allow_config) {
  939. violat:        /* setvar(error_data_string,user_variable[i].name);
  940.               user_error("allow_violation"); */
  941.           goto loopend;
  942.         }
  943.       break;
  944.  
  945.       case cmd_config:
  946.       if(al != var_allow_config) goto violat;
  947.       break;
  948.  
  949.       case cmd_reply:
  950.         if(al != var_allow_reply) goto violat;
  951.         break;
  952.  
  953.       case cmd_help:
  954.         if(al != var_allow_help) goto violat;
  955.         break;
  956.  
  957.       default: goto violat;
  958.     }
  959.     var_def[j].defined_in_parm=1;
  960.     if(strcmp(user_variable[i].name,"wims_deposit")==0) {
  961.       var_deposit(user_variable[i].value); continue;
  962.     }
  963.     mystrncpy(vbuf,user_variable[i].value,sizeof(vbuf));
  964.     if(strchr(vbuf,'$')!=NULL) {
  965.       char *p;
  966.       while((p=strchr(vbuf,'$'))!=NULL)
  967.         string_modify(vbuf,p,p+1,"&#36;");
  968.     }
  969.     s=getvar(user_variable[i].name);
  970.     if(s==NULL || *s==0) setvar(user_variable[i].name, vbuf);
  971.     else {  /* concatenate */
  972.       int k;
  973.       k=strlen(s)+strlen(vbuf);
  974.       if(k>=MAX_LINELEN-2) user_error("string_too_long");
  975.       snprintf(tmplbuf,sizeof(tmplbuf),"%s, %s",s,vbuf);
  976.       setvar(user_variable[i].name, tmplbuf);
  977.     }
  978.     loopend: ;
  979.   }
  980. }
  981.  
  982. /* parms to be eliminated from module_init_parm */
  983. /* char *init_elim[]={
  984.     "module","cmd","session","lang","worksheet","wims_access","useropts"
  985. };
  986. #define init_elim_no (sizeof(init_elim)/sizeof(init_elim[0]))
  987. */
  988.  
  989. void elim_parm(char *str, char *parm)
  990. {
  991.   char *p1, *p2;
  992.   for(p1=strstr(str,parm);p1!=NULL;p1=strstr(p1+1,parm)) {
  993.     if( (p1>str && *(p1-1)!='&') || *(p1+strlen(parm))!='=')
  994.       continue;
  995.     p2=strchr(p1,'&');
  996.     if(p2==NULL) {
  997.       if(p1>str) *(p1-1)=0; else *p1=0;
  998.       return;
  999.     }
  1000.     ovlstrcpy(p1,p2+1); p1--;
  1001.   }
  1002. }
  1003.  
  1004. /* eliminate technical definitions form parameter string
  1005.  * that is parameters from the list RO_NAMES
  1006.  */
  1007. void prep_init_parm(char rqv[])
  1008. {
  1009.   int i;
  1010.   char *p;
  1011.  
  1012.   for(p=strstr(rqv,"&+"); p!=NULL; p=strstr(++p,"&+"))
  1013.     ovlstrcpy(p+1,p+2);
  1014.   for(i=0;i<RO_NAME_NO;i++) elim_parm(rqv,ro_name[i]);
  1015.   if(strlen(rqv)>=MAX_LINELEN) rqv[0]=0;
  1016.   while(rqv[0]=='&') ovlstrcpy(rqv,rqv+1);
  1017.   while(rqv[0]!=0 && rqv[strlen(rqv)-1]=='&') rqv[strlen(rqv)-1]=0;
  1018. }
  1019.  
  1020. /* retain initializing parameters, for use in user references */
  1021. void set_init_parm(void)
  1022. {
  1023.   char *rq, rqv[MAX_LINELEN*2+2], *u, *sh, *seedr, *exotrymax;
  1024.   char *shname;
  1025.   int public_sheet;
  1026.  
  1027.   if(isexam) return;
  1028.   force_setvar("wims_sheet",""); force_setvar("wims_exo","");
  1029.   rq=getenv("QUERY_STRING");
  1030.   if(rq==NULL || *rq==0) {
  1031.     empty:
  1032.     setvar("module_init_parm",""); return;
  1033.   }
  1034.   if(strlen(rq)>=MAX_LINELEN*2) goto empty;
  1035.   _http2env(rqv,rq); prep_init_parm(rqv);
  1036.   setvar("module_init_parm",rqv); public_sheet=0;
  1037. /* seedrepeat=1 or an integer
  1038.   must be in the url and seedrepeat
  1039.   in the var.def of the module to be used */
  1040.   seedr=getvar("seedrepeat");
  1041.   force_setvar("wims_seed_repeat",seedr);
  1042.   exotrymax=getvar("exotrymax");
  1043.   force_setvar("wims_exotrymax",exotrymax);
  1044. /* now determine the sheet number for user */
  1045.   sh=getvar(ro_name[ro_worksheet]); if(sh==NULL) return;
  1046.   if(*sh=='P') {public_sheet=1; sh++;}
  1047.   shname="sheet";
  1048.   u=getvar("wims_user"); if(u==NULL) u="";
  1049.   if(sh!=NULL && *sh!=0) {
  1050.     char buf[MAX_LINELEN+1],ubuf[32], nbuf[1024], *c, *m;
  1051.     char *p1,*p2,*p3,*p4,*p5;
  1052.     int i,j,sheet;
  1053.     sheet=atoi(sh); if(sheet<=0 || sheet>256) return;
  1054.     m=getvar(ro_name[ro_module]);
  1055.     if(m==NULL) internal_error("set_init_parm(): module name disapears.");
  1056.     if(*u==0) public_sheet=1;
  1057.     if(!public_sheet) {
  1058.       c=getvar("wims_class"); if(c==NULL) c="";
  1059.       snprintf(nbuf,sizeof(nbuf),"%s/sheets/.%s%d",
  1060.                class_dir,shname,sheet);
  1061.     }
  1062.     else {
  1063.       char bf[MAX_LINELEN+1];
  1064.       int i;
  1065.       accessfile(bf,"r","%s/.sheets",session_prefix);
  1066.       if(bf[0]==0) return;
  1067.       for(i=1, p1=bf;i<sheet;i++,p1=p2) {
  1068.         p2=strchr(p1,'\n');
  1069.         if(p2!=NULL) *p2++=0; else p2=p1+strlen(p1);
  1070.       }
  1071.       p2=strchr(p1,'\n'); if(p2) *p2=0;
  1072.       snprintf(nbuf,sizeof(nbuf),"bases/sheet/%s.def",p1);
  1073.     }
  1074.     if(readfile(nbuf,buf,sizeof(buf))==NULL) return;
  1075.     for(p1=strstr(buf,"&+");p1!=NULL;p1=strstr(++p1,"&+"))
  1076.       ovlstrcpy(p1+1,p1+2);
  1077.     if(strncmp(m,"classes/",strlen("classes/"))==0) {
  1078.       m="classes/";
  1079.       for(p1=strstr(buf,":classes/");p1;p1=strstr(p1+1,":classes/")) {
  1080.         if(p1==buf || *(p1-1)=='\n') {
  1081.           p1+=strlen(":classes/");
  1082.           p2=find_word_end(p1); if(p2>p1 && *p2=='\n') ovlstrcpy(p1,p2);
  1083.         }
  1084.       }
  1085.     }
  1086.     snprintf(nbuf,sizeof(nbuf),":%s\n%s\n",m,rqv);
  1087.     p1=strstr(buf,nbuf);
  1088.     while(p1>buf && *(p1-1)!='\n') p1=strstr(p1+1,nbuf);
  1089.     if(p1!=NULL) {
  1090.       p2=strchr(buf,':');
  1091.       while(p2>buf && *(p2-1)!='\n') p2=strchr(p2+1,':');
  1092.       for(i=1;p2!=NULL && p2<p1;i++) {
  1093.         p2=strchr(p2+1,':');
  1094.         while(p2>buf && *(p2-1)!='\n') p2=strchr(p2+1,':');
  1095.       }
  1096.       if(p2==NULL) return; /* error which should not occur */
  1097.       snprintf(ubuf,sizeof(ubuf),"%d",i);
  1098. /* look for dependency information */
  1099.       for(j=0, p3=strchr(p1+strlen(nbuf),'\n');
  1100.       j<3 && p3 && *(p3+1)!=':';
  1101.       j++, p3=strchr(p3+1,'\n'));
  1102.       if(j>=3 && p3!=NULL && *(p3+1)!=':') {
  1103.         p3++; p4=strchr(p3,'\n');
  1104.         if(p4) {
  1105.           *p4++=0;
  1106.           if(*p4!=':') { /* options */
  1107.             p5=strchr(p4,'\n'); if(p5) *p5=0;
  1108.             force_setvar("wims_exoption",p4);
  1109.           }
  1110.         }
  1111.         p3=find_word_start(p3); strip_trailing_spaces(p3);
  1112. /* non-empty dependency information */
  1113.         if(*p3 && !public_sheet) {
  1114.           exodepOK=depcheck(sh,i,p3);
  1115.           if(!exodepOK) setvar("wims_exodep","pending");
  1116.         }
  1117.       }
  1118.       if(public_sheet) {
  1119.         char bf[32];
  1120.         snprintf(bf,16,"P%s",sh);
  1121.         force_setvar("wims_sheet",bf);
  1122.       }
  1123.       else force_setvar("wims_sheet",sh);
  1124.       force_setvar("wims_exo",ubuf);
  1125.       wims_sheet=sheet; wims_exo=i;
  1126.     }
  1127.   }
  1128. }
  1129.  
  1130. /* user with class: whether exercise is registered
  1131.  * Returns 1 if got, 0 otherwise. */
  1132. int get_parmreg(void)
  1133. {
  1134.   char *p, *cl, *u, nbuf[MAX_FNAME+1];
  1135.   struct stat st;
  1136.  
  1137.   u=getvar("wims_user"); cl=getvar("wims_class");
  1138.   if(u==NULL || cl==NULL || *u==0 || *cl==0 || strcmp(u,"supervisor")==0
  1139.        || wims_sheet<=0 || wims_exo<=0) return 0;
  1140.   mkfname(nbuf,"%s/.parmreg/%s.%d.%d", class_dir,u,wims_sheet,wims_exo);
  1141.   p=getvar("wims_scorereg");
  1142.   if(p!=NULL && strcmp(p,"suspend")==0) {
  1143.     unlink(nbuf); return 0;
  1144.   }
  1145.   if(stat(nbuf,&st)) return 0;
  1146.   /* latency is 10 min. */
  1147.   if(st.st_mtime<nowtime-600 || st.st_mtime > nowtime) {
  1148.      unlink(nbuf); return 0;
  1149.   }
  1150.   if(open_working_file(&m_file,nbuf)!=0) return 0;
  1151.   mkfname(m_file.name,"parmreg/%s.%d.%d",u,wims_sheet,wims_exo);
  1152.   while(wgetline(tmplbuf,MAX_LINELEN, &m_file)!=EOF) {
  1153.     p=strchr(tmplbuf,'=');
  1154.     if(p==NULL || p<=tmplbuf || isspace(tmplbuf[0]) )
  1155.       /* this time it is corrupted var file */
  1156.       internal_error("get_parmreg(): corrupt parmreg file.");
  1157.       *p=0;p++;
  1158.       if(strncmp(tmplbuf,var_prefix,strlen(var_prefix))!=0) setenv(tmplbuf,p,1);
  1159.       else if(tmplbuf[strlen(var_prefix)]!=0) force_setvar(tmplbuf+strlen(var_prefix),p);
  1160.   }
  1161.   parm_restore=1;
  1162.   close_working_file(&m_file,0); return 1;
  1163. }
  1164.  
  1165. /* reset seed according to the values of
  1166.   wims_seed_repeat and wims_seed_score
  1167. */
  1168. /* for non registered users or suspended score,
  1169.   save some variables wims_seedcnt and seedlastcnt in sessions/xxx/var
  1170. */
  1171. void reset_seed (void) {
  1172.   char *p, *seedr, buf[64], *sh, *ex, *tseed, *seedcnt, *seedlastcnt;
  1173.   char *mp=getvar("module");
  1174.   sh=getvar("wims_sheet"); ex=getvar("wims_exo");
  1175.   seedr=getvar("wims_seed_repeat");
  1176.   /* number of seeds during the sessions only used for unregistered */
  1177.   seedcnt=getvar("wims_seedcnt");
  1178.   if(seedcnt==NULL || *seedcnt==0) {
  1179.     force_setvar("wims_seedcnt","0");
  1180.     seedcnt=getvar("wims_seedcnt");
  1181.   }
  1182.   /* number of tries with the actual seed only used for unregistered
  1183.     or score suspended */
  1184.   seedlastcnt=getvar("wims_seedlastcnt");
  1185.   if(seedlastcnt==NULL || *seedlastcnt==0) {
  1186.     force_setvar("wims_seedlastcnt","0");
  1187.     seedlastcnt=getvar("wims_seedlastcnt");
  1188.   }
  1189.   if(seedr!=NULL && atoi(seedr)>0) { /* seedrepeat>1 */
  1190.     p=getvar("wims_seed_score");
  1191.     if(evalue(p)>=10){
  1192.       seed_time(); force_setvar("wims_seed_score",NULL);
  1193.       snprintf(buf,sizeof(buf), "%d", atoi(seedcnt)+1);
  1194.       force_setvar("wims_seedcnt",buf);
  1195.       seedcnt=getvar("wims_seedcnt");
  1196.       force_setvar("wims_seedlastcnt","0");
  1197.       seedlastcnt=getvar("wims_seedlastcnt");
  1198.     }
  1199.     else { /* score < 10 */
  1200.       /* is score suspended by the student ? */
  1201.       char *screg=getvar("wims_scorereg");
  1202.       /* is score suspended by the supervisor ? */
  1203.       int scregs=0;
  1204.       if(sh!=NULL && *sh!=0)
  1205.         scregs=getscorestatus(getvar("wims_class"),atoi(sh));
  1206.       if(sh!=NULL && *sh!=0 && ex!=NULL && *ex!=0
  1207.           && (screg==NULL || strcmp(screg,"suspend")!=0)
  1208.           && (scregs==1)){
  1209.         /* the case of a supervisor looking at the sheet is not treated */
  1210.         tseed=getseedscore(getvar("wims_class"), getvar("wims_user"), atoi(sh), atoi(ex));
  1211.         if( tseed!=0 ) {
  1212.           mystrncpy(buf, tseed,sizeof(buf));
  1213.           force_setvar("wims_seed",buf);
  1214.         }
  1215.         else seed_time();
  1216.       }
  1217.       else { /* non registered or score suspended
  1218.           and seedrepeat>1 and score <10 */
  1219.         if (cmd_type==cmd_new ||cmd_type==cmd_renew) {
  1220.           snprintf(buf,sizeof(buf),"%d",atoi(seedlastcnt)+1);
  1221.           force_setvar("wims_seedlastcnt",buf);
  1222.           seedlastcnt=getvar("wims_seedlastcnt");
  1223.           if(atoi(seedlastcnt) >= atoi(seedr)){
  1224.             seed_time();
  1225.             if(strcmp(mp,home_module)!=0 && cmd_type!=cmd_intro){
  1226.               snprintf(buf,sizeof(buf),"%d",0);
  1227.               force_setvar("wims_seedlastcnt",buf);
  1228.               seedlastcnt=getvar("wims_seedlastcnt");
  1229.               snprintf(buf,sizeof(buf),"%d",atoi(seedcnt)+1);
  1230.               force_setvar("wims_seedcnt",buf);
  1231.               seedcnt=getvar("wims_seedcnt");
  1232.             }
  1233.             else {
  1234.               force_setvar("wims_seedcnt","0");
  1235.               force_setvar("wims_seedlastcnt","0");
  1236.             }
  1237.           }
  1238.         }
  1239.       }
  1240.     }
  1241.   }
  1242.   else {// no seedrepeat
  1243.     seed_time();
  1244.     if (cmd_type==cmd_reply && strcmp(mp,home_module)!=0){
  1245.       snprintf(buf,sizeof(buf),"%d",atoi(seedcnt)+1);
  1246.       force_setvar("wims_seedcnt",buf);
  1247.       seedcnt=getvar("wims_seedcnt");
  1248.       force_setvar("wims_seedlastcnt","0");
  1249.     }
  1250.     else {
  1251.       if (strcmp(mp,home_module)==0 || cmd_type==cmd_intro) {
  1252.         force_setvar("wims_seedcnt","0");
  1253.         force_setvar("wims_seedlastcnt","0");
  1254.       }
  1255.     }
  1256.   }
  1257.   p=getvar("wims_seed");
  1258.   if(p!=NULL && (cmd_type==cmd_new || cmd_type==cmd_renew || cmd_type==cmd_next)){
  1259.     mystrncpy(buf,p,sizeof(buf)); setenv("tmp_seed",buf,1);
  1260.     exec_setseed(buf);
  1261.   }
  1262. }
  1263.  
  1264. /* set environment variables */
  1265. void set_variables(void)
  1266. {
  1267.   outputing=0; readnest=0;
  1268.   get_var_defs();
  1269.   set_vars_from_parm();
  1270.   if(cmd_type != cmd_new && cmd_type != cmd_renew) {
  1271.     set_vars_from_session();
  1272.     if(cmd_type==cmd_next) reset_seed();
  1273.   }
  1274.   else {
  1275.     set_init_parm();
  1276.     reset_seed();
  1277.     if(wims_sheet>0 && get_parmreg()) {
  1278.       cmd_type=cmd_resume; force_setvar("cmd","resume");
  1279.       var_proc(main_var_proc_file,0); return;
  1280.     }
  1281.     checkrafale();
  1282.     var_proc(var_init_file,0);
  1283.   }
  1284. /* check_var_bounds(); */
  1285.   var_proc(main_var_proc_file,0);
  1286. }
  1287.  
  1288. /* Output a phtml file. */
  1289. void phtml_put(char *fname,int cache)
  1290. {
  1291.   int t;
  1292.   char tbuf[MAX_LINELEN+1];
  1293.  
  1294.   outputing=1;
  1295.      /* File not found; we give empty output, but no error message. */
  1296.   if(fname!=NULL && read_module_file(fname)!=0) return;
  1297.   if(untrust&6) get_var_privileges();
  1298.   if (trace_file) start_tracefile();
  1299.   while(m_file.linepointer<m_file.linecnt) {
  1300.     if (trace_file) {
  1301.       fprintf(trace_file," %d",m_file.linepointer+1);
  1302.       fflush(trace_file);
  1303.     }
  1304.     t=m_file.lines[m_file.linepointer].isstart;
  1305.     if((t&~18)!=1) {m_file.linepointer++; continue;}
  1306.     wgetline(tbuf,MAX_LINELEN,&m_file); substnest=0;
  1307.     if((t&2)!=0) {exec_main(tbuf+1); continue;}
  1308.     substit(tbuf); output0(tbuf); _output_("\n");
  1309.   }
  1310.   if (trace_file) stop_tracefile();
  1311.   close_working_file(&m_file,cache);
  1312. }
  1313.  
  1314. /* output a file in base html directory. Internal use only. */
  1315. void phtml_put_base(char *fname,int cache)
  1316. {
  1317.   WORKING_FILE save;
  1318.   char modsave[MAX_FNAME+1];
  1319.   memmove(&save,&m_file,sizeof(WORKING_FILE));
  1320.   mystrncpy(modsave,module_prefix,sizeof(modsave));
  1321.   ovlstrcpy(module_prefix,"html");
  1322.   phtml_put(fname,cache);
  1323.   mystrncpy(module_prefix,modsave,sizeof(module_prefix));
  1324.   memmove(&m_file,&save,sizeof(WORKING_FILE));
  1325. }
  1326.  
  1327. /* Read main.phtml, process it, and write to stdout. */
  1328. void main_phtml_put(char *mname)
  1329. {
  1330.   char *p, buf[1024], txbuf[256], dirnbuf[256], bgbuf[256], spfbuf[256], ebuf[256];
  1331.   char *bcolor, *refcolor, *bg, *tx, *dirn, *vlink, *link, *hlink, *spf ;
  1332.   define_html_header(); readnest=0;
  1333.   nph_header(200);
  1334.   p=getvar("wims_backslash_insmath");
  1335.   if(p!=NULL && strcasecmp(p,"yes")==0) backslash_insmath=1;
  1336.   p=getvar("wims_expire");
  1337.   if(p!=NULL) p=strstr(p,"no-cache");
  1338.   if(p!=NULL) _output_("Cache-Control: no-cache\r\nPragma: no-cache\r\n");
  1339. /* expiration date */
  1340.   if(mode==mode_default) {
  1341.     if(!robot_access && (cmd_type!=cmd_intro || p!=NULL)) {
  1342.       char *expir="Sat, 26 Jul 1990 05:00:00 GMT";
  1343.       snprintf(ebuf,sizeof(ebuf),"%s",expir);
  1344.     }
  1345.     else {
  1346. /* expiration in 10 days for robot access or intro page. */
  1347.       time_t t=nowtime+(long) 10*24*3600;
  1348.       { struct tm *tt = localtime(&t);
  1349.         strftime(ebuf,sizeof(ebuf),"%a, %d %b %Y %T GMT", tt);
  1350.       }
  1351.     }
  1352.     strip_trailing_spaces(ebuf);
  1353.     output("Expires:%s\r\n",ebuf);
  1354.   }
  1355.  
  1356.   output("Server: %s %s (%s)\n", SHORTSWNAME,wims_version,LONGSWNAME);
  1357.   if(!robot_access && strcasecmp(usecookie,"yes")==0 && setcookie
  1358.        && mode!=mode_popup) {
  1359.     if(cookieset[0]==0) {
  1360.       p=getvar("wims_sescookie");
  1361.       if(p!=NULL && *p!=0) mystrncpy(cookieset,p,sizeof(cookieset));
  1362.     }
  1363.      output("Set-Cookie: %s%s; path=/\r\n",cookieheader,cookieset);
  1364.   }
  1365.   p=getvar("wims_main_font");
  1366.   if(p!=NULL && *p!=0) output("Content-type: text/html; charset=%s\r\n\r\n",p);
  1367.   else _output_("Content-type: text/html\r\n\r\n");
  1368.  
  1369.   bcolor=getvar("wims_bgcolor");
  1370.   if(bcolor==NULL || *bcolor==0) bcolor=bgcolor;
  1371.  
  1372.   link=getvar("wims_link_color");
  1373.   if(link==NULL || *link==0) link="blue";
  1374.  
  1375.   vlink=getvar("wims_vlink_color");
  1376.   if(vlink==NULL || *vlink==0) vlink="blue";
  1377.  
  1378.   hlink=getvar("wims_hlink_color");
  1379.   if(hlink==NULL || *hlink==0) vlink="red";
  1380.  
  1381.   refcolor=getvar("wims_ref_bgcolor");
  1382.   if(refcolor==NULL || *refcolor==0) {
  1383.     setvar("wims_ref_bgcolor","white");
  1384.     refcolor="white";
  1385.   }
  1386.  
  1387.   bg=getvar("wims_bgimg"); bgbuf[0]=0;
  1388.   if(bg!=NULL && *bg!=0 && strchr(bg,'\"')==NULL) {
  1389.     if(strchr(bg,'/')==NULL)
  1390.       snprintf(bgbuf,sizeof(bgbuf),"background-image: url(\"gifs/bg/%s\");",bg);
  1391.      else
  1392.       snprintf(bgbuf,sizeof(bgbuf),"background-image: url(\"%s\");",bg);
  1393.   }
  1394.   setvar("wims_bodyimg",bgbuf);
  1395.   tx=getvar("wims_textcolor");
  1396.   if(tx!=NULL && *tx!=0 && strchr(tx,'\"')==NULL) {
  1397.     snprintf(txbuf,sizeof(txbuf),"color:%s;",tx);
  1398.   }
  1399.   else txbuf[0]=0;
  1400.    /* special fonts defined in special_font -should also take the value specialfont in wims.conf */
  1401.   if(spec_font==1){
  1402.     spf=getvar("wims_specialfont");
  1403.     if(spf!=NULL && *spf!=0) { snprintf(spfbuf,sizeof(spfbuf),"%s",spf);}
  1404.     else { snprintf(spfbuf,sizeof(spfbuf),"%s",special_font); }
  1405.     setvar("wims_special_font",spfbuf);
  1406.   }
  1407.   dirn=getvar("wims_main_dirn"); /* "rtl" for arabic writing ; on pourrait laisser vide pour les autres ? */
  1408.   if(dirn!=NULL && *dirn!=0 && strchr(dirn,'\"')==NULL) {
  1409.     snprintf(dirnbuf,sizeof(dirnbuf),"dir=\"%s\"",dirn);
  1410.   }
  1411.   else dirnbuf[0]=0;
  1412.   snprintf(buf,sizeof(buf), "class=\"main_body\" %s",dirnbuf);
  1413.   setvar("wims_htmlbody",buf);
  1414.   phtml_put(mname,0);
  1415. }
  1416.  
  1417. void _write_var(char *name, FILE *varf,int user,int skip)
  1418. {
  1419.   char *s, buf[MAX_NAMELEN+9], nbf[MAX_NAMELEN+9];
  1420.  
  1421.   if((user&2)!=0) {
  1422.     snprintf(nbf,sizeof(nbf),"%s%s",wims_prefix,name); name=nbf;
  1423.   }
  1424.   if((user&1)!=0) {
  1425.     snprintf(buf,sizeof(buf),"%s%s",var_prefix,name); s=_getvar(name);
  1426.   }
  1427.   else {
  1428.     mystrncpy(buf,name,sizeof(buf)); s=getenv(name);
  1429.   }
  1430.   if(s==NULL) s="";
  1431.   if(skip && *s==0) return;
  1432.   fprintf(varf,"%s=",buf);
  1433.   if(strchr(s,'\n')==NULL) fputs(s,varf);
  1434.   else
  1435.     while(*s) {
  1436.       if(*s=='\n') fputc('\\',varf);
  1437.        fputc(*s,varf); s++;
  1438.     }
  1439.   fputc('\n',varf);
  1440. }
  1441.  
  1442. void _write_vars(FILE *varf)
  1443. {
  1444.   int i,j,k;
  1445.   char nbuf[MAX_NAMELEN+1];
  1446.   for(i=0;i<defined_var_total;i++) {
  1447.     if(var_def[i].beg>=0) {
  1448.       if(var_def[i].end<=var_def[i].beg) {
  1449.         snprintf(nbuf,sizeof(nbuf),"%s%d",var_def[i].name,var_def[i].beg);
  1450.         _write_var(nbuf,varf,1,1);
  1451.       }
  1452.       else {
  1453.         char *pbuf[MAX_VAR_NUM];
  1454.         j=varsuite(var_def[i].name,var_def[i].beg,var_def[i].end,pbuf,MAX_VAR_NUM);
  1455.         for(k=0;k<j;k++) _write_var(pbuf[k],varf,1,1);
  1456.       }
  1457.     }
  1458.     else _write_var(var_def[i].name,varf,1,1);
  1459.   }
  1460. }
  1461.  
  1462. /* save exercise parm for registered users */
  1463. void save_parmreg(void)
  1464. {
  1465.   char *p, *u, *sh, *ex, nbuf[MAX_FNAME+1], dbuf[MAX_FNAME+1];
  1466.   int s;
  1467.   FILE *varf;
  1468.  
  1469.   if(cmd_type!=cmd_reply && cmd_type!=cmd_new && cmd_type!=cmd_renew)
  1470.     return;
  1471.   u=getvar("wims_user");
  1472.   if(class_dir[0]==0 || *u==0) return;
  1473.   sh=getvar("wims_sheet"); ex=getvar("wims_exo");
  1474.   if(sh==NULL || ex==NULL || *sh==0 || *ex==0) return;
  1475.   mkfname(nbuf,"%s/.parmreg/%s.%s.%s", class_dir,u,sh,ex);
  1476.   if(cmd_type==cmd_reply) {
  1477.     unlink(nbuf); return;
  1478.   }
  1479.   p=getvar("wims_scorereg");
  1480.   if(p!=NULL && strcmp(p,"suspend")==0) return;
  1481. /* these two lines must be before random factor
  1482.  * in order to set variable wims_scoring */
  1483.   s=atoi(sh); if(getscorestatus(getvar("wims_class"),s)==0) return;
  1484.   if(strcmp(u,"supervisor")==0) return;
  1485. /* 0.2 is random factor to trigger exercise parm register */
  1486. /*    return;*/
  1487.  // if((double) random()<(double) RAND_MAX*0.2){ }
  1488.   return;
  1489.   mkfname(dbuf,"%s/.parmreg",class_dir);
  1490.   mkdirs(dbuf); varf=fopen(nbuf,"w"); if(varf==NULL) return;
  1491.   _write_var("module_init_parm",varf,1,0);
  1492.   _write_var("wims_sheet",varf,1,1);
  1493.   _write_var("wims_exo",varf,1,1);
  1494.   _write_var("wims_scoring",varf,1,1);
  1495.   _write_var("wims_seed",varf,1,1);
  1496.   _write_vars(varf);
  1497.   fclose(varf);
  1498. }
  1499.  
  1500. void exolog(char *fname)
  1501. {
  1502.   FILE *varf, *varg;
  1503.   int c;
  1504.   char logftmp[MAX_FNAME+1], *sess;
  1505.  
  1506. /* add by FG to put w_module_score at the top of the file */
  1507.   if ((getenv("w_module_score"))!=NULL )
  1508.   {
  1509.     sess=getvar("wims_session");
  1510.     mkfname(logftmp,"%s/%s/.tmp%lu",s2_dir,sess,nowtime);
  1511.     rename(fname,logftmp);
  1512.     varf=fopen(fname,"a");
  1513.     if(varf!=NULL) {
  1514.       _write_var("module_score",varf,1,1);
  1515.       varg=fopen(logftmp, "r");
  1516.       if(varg!=NULL) {
  1517.         while ( (c=getc (varg)) != EOF)
  1518.           putc (c,varf);
  1519.           fclose(varg);
  1520.         }
  1521.         fclose(varf);
  1522.         remove(logftmp);
  1523.      }
  1524.   }
  1525. /* end of add */
  1526.   varf=fopen(fname,"a");
  1527.   if(varf!=NULL) {
  1528. /* The first 4 lines should not be modified */
  1529.     fprintf(varf,"\n:\n");
  1530.     _write_var("QUERY_STRING",varf,0,0);
  1531.     _write_var("module",varf,1,0);
  1532.     _write_var("cmd",varf,1,0);
  1533.     _write_var("module_init_parm",varf,1,0);
  1534.     _write_var("wims_scoring",varf,1,1);
  1535.     _write_var("module_score",varf,1,1);
  1536.     fprintf(varf,"w_wims_checktime1=%lu\n\
  1537. w_wims_checktime2=%s\n",nowtime,nowstr);
  1538.     _write_var("wims_seed",varf,1,1);
  1539.     _write_vars(varf);
  1540.     fclose(varf);
  1541.   }
  1542. }
  1543.  
  1544. /* save variables to session var file */
  1545. void save_session_vars(void)
  1546. {
  1547.   int i;
  1548.   char *mp, *sh, *ex, *ll;
  1549.   FILE *varf;
  1550. /* light pages don't need saved variables. ? */
  1551. /*    if(varchr(module_prefix,"light")!=NULL) return;*/
  1552.   if(robot_access) return;
  1553. /* no variable saving if cmd=help? */
  1554.   if(cmd_type==cmd_help) return;
  1555.   lastdatafile[0]=lastftest[0]=0;
  1556.   lessrafale();
  1557.   mp=getvar("wims_nextmodule");
  1558.   if(mp!=NULL && *mp!=0) {
  1559.     mp=getvar("module");
  1560.     if(strcmp(mp,home_module)==0)
  1561.       force_setvar("module",getvar("wims_nextmodule"));
  1562.   }
  1563.   varf=fopen_session_var_file("w");
  1564.   for(i=0;i<HEADER_VAR_NO;i++) _write_var(header_var_name[i],varf,0,0);
  1565.   for(i=0;i<RO_NAME_NO;i++) _write_var(ro_name[i],varf,1,0);
  1566.   for(i=0;i<INTERNAL_NAME_NO;i++) {
  1567.     if(internal_name[i].stat==0)
  1568.       _write_var(internal_name[i].name,varf,3,0);
  1569.   }
  1570.   _write_var("password",varf,0,1);
  1571.   save_parmreg();
  1572.   if(new_session) fprintf(varf,"wims_new_session=yes\n");
  1573.   fprintf(varf,"\n");
  1574.   _write_var("module_init_parm",varf,1,0);
  1575.   _write_var("wims_sheet",varf,1,1);
  1576.   _write_var("wims_exo",varf,1,1);
  1577.   _write_var("wims_scoring",varf,1,1);
  1578.   _write_vars(varf);
  1579.   fclose(varf);
  1580.   if(cmd_type==cmd_new || cmd_type==cmd_renew || cmd_type==cmd_reply || cmd_type==cmd_next) {
  1581.     ll=getvar("wims_class_examlog");
  1582.     if(ll!=NULL && *ll!=0) i=atoi(ll); else i=examlog_limit;
  1583.     if(strstr(session_prefix,"_exam")!=NULL && examlogf[0]!=0 &&
  1584.         simuxam==0 && i>0) {
  1585.       mkdirs(examlogd); exolog(examlogf);
  1586.     }
  1587.     else {
  1588.       ll=getvar("wims_class_exolog");
  1589.       sh=getvar("wims_sheet"); ex=getvar("wims_exo");
  1590.       if(ll!=NULL && atoi(ll)>0) {
  1591.         char buf[MAX_FNAME+1];
  1592.         if(sh!=NULL && *sh!=0 && ex!=NULL && *ex!=0){
  1593.           mkfname(buf,"%s/exolog.%s.%s",session_prefix,sh,ex);
  1594.         }
  1595.         else { mkfname(buf,"%s/exolog",session_prefix);}
  1596.         if(cmd_type==cmd_new || cmd_type==cmd_renew) unlink(buf);
  1597.         exolog(buf);
  1598.       }
  1599.     }
  1600.   }
  1601.   if(var_def_buf) free(var_def_buf);
  1602. }
  1603.