Subversion Repositories wimsdev

Rev

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