Subversion Repositories wimsdev

Rev

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