Subversion Repositories wimsdev

Rev

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