Subversion Repositories wimsdev

Rev

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

  1. /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18.       /* This file contains various configuration routines for wims. */
  19.  
  20.         /* Limitations are essentially for security reasons:
  21.          * to curb attacks by resource exhaustion of the server. */
  22.  
  23.         /* maximal length of any module's variable definition file. */
  24. int VAR_DEF_LENGTH_LIMIT=50000;
  25.         /* maximal nesting level of substitutions. */
  26. int SUBST_LIMIT=16;
  27.         /* We fix a limit for the number of goto's (or for jumps) executed,
  28.          * in order to avoid infinite loop or DOS attack. */
  29. int GOTO_LIMIT=500000;
  30.         /* Limit for instex, insplot, insPLOT. */
  31. int INS_LIMIT=500;
  32.         /* Limit for frames in an animation. */
  33. int ANIM_LIMIT=400;
  34.         /* module log file length */
  35. int MODULE_LOG_LIMIT=102400;
  36.         /* general log file length */
  37. int GEN_LOG_LIMIT=1024000;
  38.         /* number of old log files */
  39. int OLD_LOG_FILES=2;
  40.  
  41.         /* Resource limits */
  42. int threshold1=150;     /* First load shreshold */
  43. int threshold2=300;     /* Second load shreshold */
  44. int ispriority=0;       /* whether the connection is from priority sites */
  45.  
  46.         /* sessions not accessed more than these seconds will be erased. */
  47. int idle_time=5400;
  48.         /* non-class sessions not accessed more than these seconds will be erased. */
  49. int idle_time2=2400;
  50.         /* first-time sessions not accessed more than these seconds will be erased. */
  51. int idle_time3=900;
  52.  
  53. int rafalvl=10;         /* anti-rapidfire severity */
  54.  
  55.         /* Explanation: two real numbers a,b such that
  56.          * |a+b|>|a-b|*compare_precision
  57.          * will be considered equal in !ifvalue comparisons. */
  58. int DEFAULT_COMPARE_PRECISION=10000;
  59.         /* Precision of printing (hence of all evaluations). */
  60. int DEFAULT_PRINT_PRECISION=8;
  61.         /* Defaults for instex, insplot, etc. */
  62. char *DEFAULT_INS_FORMAT="gif";
  63. char *DEFAULT_ANIM_FORMAT="gif";
  64. char *DEFAULT_INS_DENSITY="100x100";
  65. char *DEFAULT_INSPLOT_FONT="small";
  66.  
  67.         /* gnuplot intermediate graphics format */
  68. char *gnuplot_format="gif";
  69.  
  70.   /* electronic address of site manager. */
  71. char *site_manager="wims@$httpd_HTTP_HOST";
  72.   /* Main configure file, this name cannot be configured! */
  73. char *config_file="../log/wims.conf";
  74. char PATH[512];  /* directory settings */
  75. #ifdef MACOSX
  76.  char *commonpath="/usr/local/bin:/usr/bin:/usr/games:/bin:/sw/bin:/usr/bin/X11:/usr/X11R6/bin";
  77. #else
  78.  char *commonpath="/usr/local/bin:/usr/bin:/usr/games:/bin:/usr/bin/X11:/usr/X11R6/bin:/opt/teTeX/bin";
  79. #endif
  80. char *gap_command="gap.sh -T -m 64M";
  81. char *maxima_command="maxima";
  82. char *pari_command="gp -f -q -p 150000";
  83. char *scilab_command="scilab -nwni";
  84. char *QRencode_command="qrencode -o";
  85.  
  86.   /* default languages */
  87. char site_langbuf[]="en fr cn es it nl";
  88. char *site_languages=site_langbuf;
  89.   /* Statistics shows up? */
  90. char *show_stat="no";
  91.   /* Author names show up in search result? */
  92. char *show_author="no";
  93.   /* Show user IP adress to supervisor */
  94. char *show_ip="yes";
  95.   /* busy hours definition */
  96. char *busyhours="";
  97.   /* Presentation theme */
  98. char *theme="standard";
  99.   /* directory of session files */     
  100. char session_dir[MAX_FNAME+1], s2_dir[MAX_FNAME+1];
  101.   /* standardised header model for modules' output. Will be stored
  102.    * into the variable $wims_html_header. If the theme contains
  103.    * a file 'head.phtml', html_header will be defined by the address
  104.    * of this file  in subroutine define_html_header */
  105. char *html_header="html/header.phtml";
  106.   /* standardised about table for modules */
  107. char *module_about_file="html/module_about";
  108.   /* title definition page */
  109. char *title_page="title.phtml";
  110.   /* stardardised header menu bar, to be included in module pages
  111.    * via command !headmenu */
  112. char *header_menu="headmenu.phtml";
  113. char *header_menu_user="headmenu_user.phtml";
  114. char *header_menu_supervisor="headmenu_supervisor.phtml";
  115.   /* standardised referer to wims home, to be included in
  116.    * module pages via command !homeref */
  117. char *home_referer="homeref.phtml";
  118. char *home_referer_user="homeref_user.phtml";
  119. char *home_referer_supervisor="homeref_supervisor.phtml";
  120.   /* background color in standard reference tables */
  121. char *ref_bgcolor="#777777";
  122.   /* link color */
  123. char *link_color="#004077";
  124.   /* vlink color */
  125. char *vlink_color="#004077";
  126.   /* link color in standard reference tables */
  127. char *ref_menucolor="white";
  128.   /* background help button color */
  129. char *ref_button_help_bgcolor="green";
  130.   /* background help button color */
  131. char *ref_button_help_color="white";
  132.   /* button background color */
  133. char *ref_button_bgcolor="#666666";
  134.   /* button color */
  135. char *ref_button_color="white";
  136.   /* module log disabled by default */
  137. int modlog=0;
  138.   /* name of the variable definition file for every module.
  139.    * It is this file which defines the presence of the module.
  140.    * Better do not make it configurable. */
  141. char var_def_file[256];
  142.   /* name of variable init file. Processed only when cmd=new or renew. */
  143. char *var_init_file="var.init";
  144.   /* name of variable calculation file. Parsed at each call. */
  145. char *main_var_proc_file="var.proc";
  146.   /* name of the main (programmable) html file. */
  147. char *html_file="main.phtml";
  148.   /* Introduction page name */
  149. char *intro_file="intro.phtml";
  150.   /* Reference page */
  151. char *ref_file="wims_ref.phtml";
  152.   /* script to process different inserts.
  153.    * The two consecutive points in the name prohibits ordinary
  154.    * user execution of the script. */
  155. char *insdraw_processor="insdraw..processor";
  156. char *insplot_processor="insplot..processor";
  157. char *tex2gif="bin/tex..gif";
  158. char *priority="";              /* IPs of priority sites */
  159. char *texgif_fontdir="w/texgif";
  160. char *texgif_texheader="mathfonts/header";
  161. char *bgcolor="#E8E8E8";        /* page background color */
  162. char *bgimg="";                 /* page background image file */
  163. char *pagecss="-theme-";                /* style sheet file */
  164. char *theme_icon="default";             /* icon */
  165. char *usecookie="no";           /* whether to send cookies to anon requests */
  166. char *mail_opt="";              /* !mailto options */
  167. char nodeip[256]="";            /* ip of cluster node if relevant */
  168. char temp_log[MAX_FNAME+1];     /* temp log file name */
  169.         /* 0: low; 1: high; 2: MathML */
  170. int mathalign_base=0;           /* Not use <sup> for middle alignment */
  171. char *disable_zoom="no";        /* yes/no:  default zooming allowed (if enabled via "useropts")  */
  172. int spec_font=0; /* choose of some fonts*/
  173.  /* special font can be configured in log/wims.conf with variable special_font*/
  174. char *special_font="letter-spacing:0.3em;line-height:2.7em;word-spacing:0.8em;";
  175. int backup_hour=-1;             /* Hour for daily backup; -1 means no backup. */
  176. int site_accounting=0;          /* whether accounting should be activated */
  177. int examlog_limit=7;            /* number of logged exam sessions for each student */
  178.  
  179.   /* Automatic module update */
  180. char *mu_auto="none";
  181. char *mu_cat="all";
  182. char *mu_delay="3";
  183. char *mu_site="http://wims.unice.fr/download/modules";
  184. char *mu_zone="all";
  185. char *mu_download_site="unice,wimsedu";
  186. char *mu_publish_site="unice";
  187.  
  188.   /* keywords destinated to robots */
  189. char *site_keywords="interactive mathematics, interactive math, server side interactivity";
  190. char *site_description="interactive exercises, online calculators and plotters, mathematical recreation and games";
  191.  
  192.   /* class and document authorization */
  193. int class_quota=100;
  194. int superclass_quota=100;
  195. int doc_quota=32;
  196. char *class_regpass="email,email,deny";
  197. char *doc_regpass="";
  198.  
  199.   /* Where to find programs; if your httpd sets /bin:/usr/bin in PATH,
  200.    * this should be OK. */
  201. char *sed_prog="sed";
  202. char *tr_prog="tr";
  203. char *awk_prog="awk";
  204.  
  205.   /* myname */
  206. char *cgi_name="wims.cgi";
  207. char *aliased_cgi="no";
  208. char *aliased_getfile="yes";
  209.  
  210.   /* Limits for classes and participants */
  211. int class_limit=200; /* limit of number of classes */
  212. int user_limit=5000; /* limit of total number of registered users */
  213. int class_user_limit=300; /* limit of users in one class */
  214. int forum_limit=100;  /* Limit of message boards */
  215.  
  216. typedef struct {
  217.     char *name;
  218.     int is_integer;
  219.     void  *address;
  220. } CONFIG_DATA;
  221.  
  222. CONFIG_DATA main_config[]={
  223.       {"PATH",                  0, &commonpath},
  224.       {"QRencode_command",      2, &QRencode_command},
  225.       {"aliased_cgi",           0, &aliased_cgi},
  226.       {"aliased_getfile",       0, &aliased_getfile},
  227.       {"backup_hour",           1, &backup_hour},
  228.       {"busyhours",             0, &busyhours},
  229.       {"cgi_name",              0, &cgi_name},
  230.       {"class_limit",           1, &class_limit},
  231.       {"class_quota",           1, &class_quota},
  232.       {"class_regpass",         0, &class_regpass},
  233.       {"class_user_limit",      1, &class_user_limit},
  234.       {"css",                   0, &pagecss},
  235.       {"default_anim_format",   0, &DEFAULT_ANIM_FORMAT},
  236.       {"default_ins_format",    0, &DEFAULT_INS_FORMAT},
  237.       {"default_insplot_font",  0, &DEFAULT_INSPLOT_FONT},
  238.       {"default_texposition",   1, &mathalign_base},
  239.       {"devel_modules",         0, &devel_modules},
  240.       {"doc_quota",             1, &doc_quota},
  241.       {"doc_regpass",           0, &doc_regpass},
  242.       {"examlog_limit",         1, &examlog_limit},
  243.       {"forum_limit",           1, &forum_limit},
  244.       {"gap_command",           2, &gap_command},
  245.       {"general_log_limit",     1, &GEN_LOG_LIMIT},
  246.       {"gnuplot_format",        0, &gnuplot_format},
  247.       {"home_module",           0, &home_module},
  248.       {"idle_time",             1, &idle_time},
  249.       {"idle_time2",            1, &idle_time2},
  250.       {"idle_time3",            1, &idle_time3},
  251.       {"insdraw_processor",     0, &insdraw_processor},
  252.       {"link_color",            0, &link_color},
  253.       {"mail_opt",              0, &mail_opt},
  254.       {"manager_https",         1, &manager_https},
  255.       {"manager_site",          0, &manager_site},
  256.       {"maxima_command",        2, &maxima_command},
  257.       {"module_log",            1, &modlog},
  258.       {"module_log_limit",      1, &MODULE_LOG_LIMIT},
  259.       {"mu_auto",               0, &mu_auto},
  260.       {"mu_cat",                0, &mu_cat},
  261.       {"mu_delay",              0, &mu_delay},
  262.       {"mu_download_site",              0, &mu_download_site},
  263.       {"mu_publish_site",               0, &mu_publish_site},
  264.       {"mu_site",               0, &mu_site},
  265.       {"mu_zone",               0, &mu_zone},
  266.       {"old_log_files",         1, &OLD_LOG_FILES},
  267.       {"page_bgcolor",          0, &bgcolor},
  268.       {"page_bgimg",            0, &bgimg},
  269.       {"pari_command",          2, &pari_command},
  270.       {"priority",              0, &priority},
  271.       {"rafale_level",          1, &rafalvl},
  272.       {"ref_bgcolor",           0, &ref_bgcolor},
  273.       {"ref_button_bgcolor",            0, &ref_button_bgcolor},
  274.       {"ref_button_color",              0, &ref_button_color},
  275.       {"ref_button_help_bgcolor",               0, &ref_button_help_bgcolor},
  276.       {"ref_button_help_color",         0, &ref_button_help_color},
  277.       {"ref_menucolor",         0, &ref_menucolor},
  278.       {"rlimit_cpu",            1, &rlimit_cpu},
  279.       {"scilab_command",        2, &scilab_command},
  280.       {"show_author",           0, &show_author},
  281.       {"show_ip",               0, &show_ip},
  282.       {"show_stat",             0, &show_stat},
  283.       {"site_accounting",       1, &site_accounting},
  284.       {"site_description",      0, &site_description},
  285.       {"site_keywords",         0, &site_keywords},
  286.       {"site_languages",        0, &site_languages},
  287.       {"site_manager",          0, &site_manager},
  288.       {"superclass_quota",      1, &superclass_quota},
  289.       {"texbasesize",           1, &texbasesize},
  290.       {"theme",                 0, &theme},
  291.       {"theme_icon",                    0, &theme_icon},
  292.       {"threshold1",            1, &threshold1},
  293.       {"threshold2",            1, &threshold2},
  294.       {"tmp_debug",             0, &tmp_debug},
  295.       {"trusted_modules",       0, &trusted_modules},
  296.       {"usecookie",             0, &usecookie},
  297.       {"user_limit",            1, &user_limit},
  298.       {"var_def_length_limit",  1, &VAR_DEF_LENGTH_LIMIT},
  299.       {"vlink_color",           0, &vlink_color},
  300.       {"workfile_limit",        1, &WORKFILE_LIMIT}
  301. };
  302. #define MAIN_CONFIG_NO (sizeof(main_config)/sizeof(main_config[0]))
  303.  
  304.         /* processes the list of languages supported on the site */
  305. void language_list(void)
  306. {
  307.     int i,k;
  308.     char *lbuf[MAX_LANGUAGES];
  309.     calc_tolower(site_languages); items2words(site_languages);
  310.     setvar("wims_site_languages",site_languages);
  311.     i=cutwords(site_languages,lbuf,MAX_LANGUAGES);
  312.     available_lang_no=i;
  313.     for(i=k=0;i<available_lang_no;i++) {
  314.         if(strlen(lbuf[i])==2) memmove(available_lang[k++],lbuf[i],3);
  315.     }
  316.     if(k>0) memmove(lang,available_lang[0],3);
  317. }
  318.  
  319. char hostname[256];
  320. int html_call=0;
  321.  
  322.         /* determine the http reference name of the server */
  323. void determine_ref_name(void)
  324. {
  325.     char *s1, *s2, *p, *pl;
  326.     char buf[MAX_LINELEN+1];
  327.  
  328.     hostname[0]=0;
  329.     s1=getvar("httpd_HTTP_HOST");s2=getvar("httpd_SCRIPT_NAME");
  330.     gethostname(hostname,sizeof(hostname));
  331.     if(hostname[0]==0 && s1 && *s1!=0) mystrncpy(hostname,s1,sizeof(hostname));
  332.     if(s1==NULL || *s1==0) s1=hostname;
  333.     setvar("wims_myhostname",hostname);
  334.     ref_base[0]=0;
  335.     if(s2!=NULL) mystrncpy(buf,s2,sizeof(buf));
  336.     else buf[0]=0;
  337.     if(s1!=NULL && *buf=='/') {
  338.         p=strrchr(buf,'/'); if(p==NULL) p=buf; else *p++=0;
  339.         if(strcmp(p,cgi_name)!=0 && strlen(p)>4 &&
  340.            p[2]=='_' && islower(p[0]) && islower(p[1])) {
  341.             pre_language[0]=p[0];pre_language[1]=p[1];pre_language[2]=0;
  342.             p[2]=0; setenv("HTTP_ACCEPT_LANGUAGE",p,1); pl=p; p+=3;
  343.             s2=strrchr(p,'.'); if(s2!=NULL) {
  344.                 *s2=0; if(strcasecmp(s2+1,"cgi")!=0) html_call=1;
  345.             }
  346.             s2=p; while((s2=strpbrk(s2,"@~"))!=NULL) *s2='/';
  347.             if(strncmp(p,"doc/",4)==0) {
  348.                 char *pd, *pb;
  349.                 pd=p+4;
  350.                 if(*pd) {
  351.                     pb=strchr(pd,'/');
  352.                     if(pb) *pb++=0; else pb="main";
  353.                     setvar("doc",pd); setvar("block",pb); setvar("job","read");
  354.                 }
  355.                 setvar(ro_name[ro_module],mkfname(NULL,"adm/doc.%s",pl));
  356.             }
  357.             else setvar(ro_name[ro_module],p);
  358.             module_defined=1;
  359.         }
  360.         p=strchr(buf,'_');
  361.         if(p!=NULL && p>buf+3 && myislower(*(p-1)) && myislower(*(p-2))
  362.            && *(p-3)=='/' && myisalpha(*(p+1))) {
  363.             char *pp;
  364.             for(pp=p+1; myisalnum(*pp); pp++){};
  365.             if(*pp=='/') *(p-3)=0;
  366.         }
  367.         snprintf(ref_name,sizeof(ref_name)-2,"http://%s%s/%s",s1,buf,cgi_name);
  368.         mystrncpy(ref_base,ref_name,sizeof(ref_base)-2);
  369.         p=strrchr(ref_base,'/');
  370.         if(p!=NULL) *(p+1)=0;
  371.     }
  372.     else mystrncpy(ref_name,cgi_name,sizeof(ref_name)-1);
  373.     setvar("wims_ref_name",ref_name);
  374. }
  375.  
  376.         /* Read and interprete wims configuration file. */
  377. void main_configure(void)
  378. {
  379.     int i;
  380.     char *conf_buf,*e,*p,*p2,*p3;
  381.     char buf[MAX_LINELEN+1];
  382.  
  383.     conf_buf=readfile(config_file,NULL,WORKFILE_LIMIT);
  384.     if(conf_buf==NULL) goto fileend;
  385.     e=conf_buf-1; while(e) {
  386.         p=e+1; e=strchr(p,'\n'); if(e) *e=0;
  387.         p=find_word_start(p);
  388.         if(*p==0 || *p==comment_prefix_char) continue; /* empty or comment line */
  389.         p2=strchr(p,'=');
  390.         if(p2==NULL) continue; /* syntax error */
  391.         *p2=0; *find_word_end(p)=0; p2=find_word_start(p2+1);
  392.         p3=p2+strlen(p2); while(myislspace(p3[-1])) p3--;
  393.         while(p3[-1]=='\\') p3--; *p3=0;
  394.         i=search_list(main_config,MAIN_CONFIG_NO,sizeof(main_config[0]),p);
  395.         if(i<0) continue; /* name non-defined */
  396.         if(main_config[i].is_integer==1) {
  397.             int *ip;
  398.             ip=main_config[i].address;
  399.             *ip=atoi(p2); if(*ip<0) *ip=0;
  400.         }
  401.         else {
  402.             char **cp;
  403.             cp=main_config[i].address;
  404.             *cp=p2;
  405.         }
  406.     }
  407.     fileend:
  408.     setenv("session_base_dir",session_dir,1);
  409.     determine_ref_name();
  410.     language_list();
  411.     for(i=0;i<MAIN_CONFIG_NO;i++) {
  412.         char **pp;
  413.         if(main_config[i].is_integer==2) {
  414.             pp=main_config[i].address;
  415.             setenv(main_config[i].name,*pp,1);
  416.         }
  417.     }
  418.         /* check priority */
  419.     if(priority[0] && checkhost(priority)>0) ispriority=1;
  420.     (void)getcwd(cwdbuf,sizeof(cwdbuf)); setvar("httpd_PWD",cwdbuf);
  421.     setenv("wims_server_base",cwdbuf,1);
  422.     p=strrchr(cwdbuf,'/');
  423.     if(p!=NULL && strcmp(p,"/public_html")==0) {
  424.         *p=0; snprintf(PATH,sizeof(PATH),"%s/other/bin:%s",cwdbuf,commonpath);
  425.         force_setvar("wims_home",cwdbuf);
  426.         *p='/';
  427.     }
  428.     else {
  429.         snprintf(PATH,sizeof(PATH),"%s/../other/bin:%s",cwdbuf,commonpath);
  430.         force_setvar("wims_home",cwdbuf);
  431.     }
  432.     setenv("PATH",PATH,1);
  433.     p=getvar("httpd_SERVER_SOFTWARE");
  434.     if(p && (p2=strstr(p,"Apache"))!=NULL && strcmp(p2,"Apache/1.3")>=0
  435.        && strstr(aliased_getfile,"yes")!=NULL)
  436.       good_httpd=1;
  437.     accessfile(buf,"r","../tmp/log/myip");
  438.     *find_word_end(buf)=0;
  439.     if(buf[0]) {
  440.         snprintf(nodeip,sizeof(nodeip),"%s",buf);
  441.         snprintf(temp_log,sizeof(temp_log),"%s.%s",TEMP_LOG_FILE,nodeip);
  442.         force_setvar("wims_priv_nodeip",nodeip);
  443.     }
  444.     else {
  445.         nodeip[0]=0;
  446.         snprintf(temp_log,sizeof(temp_log),"%s",TEMP_LOG_FILE);
  447.     }
  448. }
  449.  
  450. struct {
  451.     int rname;
  452.     int *rval;
  453. } resource_table[]={
  454. #ifdef RLIMIT_CPU
  455.       {RLIMIT_CPU,      &rlimit_cpu},
  456. #endif
  457. #ifdef RLIMIT_FSIZE
  458.       {RLIMIT_FSIZE,    &rlimit_fsize},
  459. #endif
  460. #ifdef RLIMIT_AS
  461.       {RLIMIT_AS,       &rlimit_as},
  462. #endif
  463. #ifdef RLIMIT_DATA
  464.       {RLIMIT_DATA,     &rlimit_data},
  465. #endif
  466. #ifdef RLIMIT_STACK
  467.       {RLIMIT_STACK,    &rlimit_stack},
  468. #endif
  469. #ifdef RLIMIT_CORE
  470.       {RLIMIT_CORE,     &rlimit_core},
  471. #endif
  472. #ifdef RLIMIT_RSS
  473.       {RLIMIT_RSS,      &rlimit_rss},
  474. #endif
  475. #ifdef RLIMIT_NPROC
  476.       {RLIMIT_NPROC,    &rlimit_nproc},
  477. #endif
  478. #ifdef RLIMIT_NOFILE
  479.       {RLIMIT_NOFILE,   &rlimit_nofile},
  480. #endif
  481. #ifdef RLIMIT_MEMLOCK
  482.       {RLIMIT_MEMLOCK,  &rlimit_memlock}
  483. #endif
  484. };
  485. #define RESOURCE_NO (sizeof(resource_table)/sizeof(resource_table[0]))
  486.  
  487.         /* set system resource limits */
  488. void set_rlimits(void)
  489. {
  490.     int i;
  491.     struct rlimit rlim;
  492.  
  493.     for(i=0;i<RESOURCE_NO;i++) {
  494.         rlim.rlim_cur=rlim.rlim_max=*(resource_table[i].rval);
  495.         setrlimit(resource_table[i].rname,&rlim);
  496.         if(resource_table[i].rname==RLIMIT_CPU) {
  497.             setvar("wims_cpu_limit",int2str(rlim.rlim_max));
  498.             initalarm();
  499.         }
  500.     }
  501. }
  502.  
  503. struct {
  504.     char *name;
  505.     int is_integer;
  506.     void *default_value;
  507. } module_default[]={
  508.       {"anim_format",           0, &DEFAULT_ANIM_FORMAT},
  509.       {"gnuplot_format",        0, &gnuplot_format},
  510.       {"ins_anim_limit",        1, &ANIM_LIMIT},
  511.       {"ins_density",           0, &DEFAULT_INS_DENSITY},
  512.       {"ins_format",            0, &DEFAULT_INS_FORMAT},
  513.       {"insplot_font",          0, &DEFAULT_INSPLOT_FONT},
  514.       {"wims_busyhours",        0, &busyhours},
  515.       {"wims_class_limit",      1, &class_limit},
  516.       {"wims_class_quota",      1, &class_quota},
  517.       {"wims_class_regpass",    0, &class_regpass},
  518.       {"wims_class_user_limit", 1, &class_user_limit},
  519.       {"wims_compare_precision",1, &DEFAULT_COMPARE_PRECISION},
  520.       {"wims_devel_modules",    0, &devel_modules},
  521.       {"wims_doc_quota",        1, &doc_quota},
  522.       {"wims_doc_regpass",      0, &doc_regpass},
  523.       {"wims_examlog_limit",    1, &examlog_limit},
  524.       {"wims_forum_limit",      1, &forum_limit},
  525.       {"wims_bgcolor",          0, &bgcolor},
  526.       {"wims_bgimg",            0, &bgimg},
  527.       {"wims_css",              0, &pagecss},
  528.       {"wims_link_color",       0, &link_color},
  529.       {"wims_print_precision",  1, &DEFAULT_PRINT_PRECISION},
  530.       {"wims_ref_bgcolor",      0, &ref_bgcolor},
  531.       {"wims_ref_button_bgcolor",               0, &ref_button_bgcolor},
  532.       {"wims_ref_button_color",         0, &ref_button_color},
  533.       {"wims_ref_button_help_bgcolor",          0, &ref_button_help_bgcolor},
  534.       {"wims_ref_button_help_color",            0, &ref_button_help_color},
  535.       {"wims_ref_menucolor",            0, &ref_menucolor},
  536.       {"wims_ref_menucolor",    0, &ref_menucolor},
  537.       {"wims_show_author",      0, &show_author},
  538.       {"wims_show_ip",          0, &show_ip},
  539.       {"wims_show_stat",        0, &show_stat},
  540.       {"wims_site_description", 0, &site_description},
  541.       {"wims_site_keywords",    0, &site_keywords},
  542.       {"wims_site_manager",     0, &site_manager},
  543.       {"wims_texalign",         1, &mathalign_base},
  544.       {"wims_texbasesize",      1, &texbasesize},
  545.       {"wims_theme",            0, &theme},
  546.       {"wims_theme_icon",       0, &theme_icon},
  547.       {"wims_tmp_debug",        0, &tmp_debug},
  548.       {"wims_usecookie",        0, &usecookie},
  549.       {"wims_user_limit",       1, &user_limit},
  550.       {"wims_vlink_color",      0, &vlink_color},
  551.       {"wims_superclass_quota", 1, &superclass_quota}
  552. };
  553. #define MODULE_DEFAULT_NO (sizeof(module_default)/sizeof(module_default[0]))
  554.  
  555.         /* Set defaults for module, and set corresponding variables. */
  556. void module_configure(void)
  557. {
  558.     int i;
  559.     confset=1;
  560.     for(i=0;i<MODULE_DEFAULT_NO;i++) {
  561.         if(module_default[i].is_integer) {
  562.             int j, *jp;
  563.             jp=module_default[i].default_value;
  564.             j=*jp; if(j<0) j=0;
  565.             setvar(module_default[i].name,int2str(j));
  566.         }
  567.         else {
  568.             char **cp;
  569.             cp=module_default[i].default_value;
  570.             setvar(module_default[i].name,*cp);
  571.         }
  572.     }
  573.     setenv("texgif_fontdir",texgif_fontdir,1);
  574.     setenv("texgif_texheader",texgif_texheader,1);
  575.     setvar("wims_texsize","0");
  576.     setvar("disable_zoom","0");
  577.     confset=0;
  578. }
  579.  
  580. char *modindex[]={
  581.       "address",
  582.       "author",
  583.       "category",
  584.       "copyright",
  585.       "data",
  586.       "description",
  587.       "description_ca",
  588.       "description_cn",
  589.       "description_de",
  590.       "description_en",
  591.       "description_es",
  592.       "description_fr",
  593.       "description_it",
  594.       "description_nl",
  595.       "description_si",
  596.       "description_tw",
  597.       "domain",
  598.       "help",
  599.       "keywords",
  600.       "keywords_ca",
  601.       "keywords_cn",
  602.       "keywords_de",
  603.       "keywords_en",
  604.       "keywords_es",
  605.       "keywords_fr",
  606.       "keywords_it",
  607.       "keywords_nl",
  608.       "keywords_si",
  609.       "keywords_tw",
  610.       "language",
  611.       "level",
  612.       "maintainer",
  613.       "maintainer_address",
  614.       "mode",
  615.       "require",
  616.       "scoring",
  617.       "title",
  618.       "title_ca",
  619.       "title_cn",
  620.       "title_de",
  621.       "title_en",
  622.       "title_es",
  623.       "title_fr",
  624.       "title_it",
  625.       "title_nl",
  626.       "title_si",
  627.       "title_tw",
  628.       "translator",
  629.       "translator_address",
  630.       "translator_address_ca",
  631.       "translator_address_cn",
  632.       "translator_address_de",
  633.       "translator_address_en",
  634.       "translator_address_es",
  635.       "translator_address_fr",
  636.       "translator_address_it",
  637.       "translator_address_nl",
  638.       "translator_address_si",
  639.       "translator_address_tw",
  640.       "translator_ca",
  641.       "translator_cn",
  642.       "translator_de",
  643.       "translator_en",
  644.       "translator_es",
  645.       "translator_fr",
  646.       "translator_it",
  647.       "translator_nl",
  648.       "translator_si",
  649.       "translator_tw",
  650.       "vardef",
  651.       "version",
  652.       "wims_version",
  653. };
  654. #define MODINDEX_NO (sizeof(modindex)/sizeof(modindex[0]))
  655. char *module_special_file[]={
  656.     "intro","help"
  657. };
  658. #define MODSPEC_NO (sizeof(module_special_file)/sizeof(module_special_file[0]))
  659.  
  660. #ifdef WEBMATH
  661.  
  662.         /* read and treat module's INDEX file */
  663. void module_index(void)
  664. {
  665.     char buf[MAX_NAMELEN+1], ind_buf[MAX_LINELEN+1];
  666.     char *e, *p, *p2, *ft;
  667.     double v1, v2;
  668.     int i,l;
  669.     long indf_len;
  670.  
  671.     memmove(var_def_file,default_var_def,strlen(default_var_def)+1);
  672.     ft=readfile(mkfname(NULL,"%s/INDEX",module_prefix),
  673.                 ind_buf,sizeof(ind_buf));
  674.     if(ft==NULL) ft=readfile(mkfname(NULL,"%s/index",module_prefix),
  675.                              ind_buf,sizeof(ind_buf));
  676.     if(ft==NULL) return;
  677.     e=ind_buf-1; indf_len=strlen(ind_buf);
  678.     for(l=0; e<ind_buf+indf_len && e!=NULL ;l++) {
  679.         p=e+1; e=strchr(p,'\n'); if(e!=NULL) *e=0;
  680.         strip_trailing_spaces(p); p=find_word_start(p);
  681.         if(*p==0 || *p==comment_prefix_char) continue; /* empty or comment line */
  682.         p2=strchr(p,'=');
  683.         if(p2==NULL) continue; /* syntax error */
  684.         *p2=0; *find_word_end(p)=0; p2=find_word_start(p2+1);
  685.         i=search_list(modindex,MODINDEX_NO,sizeof(modindex[0]),p);
  686.         if(i<0) continue; /* name not in list */
  687.         snprintf(buf,sizeof(buf),"module_%s",p);
  688.         setvar(buf,p2);
  689.     }
  690.     if(mode!=mode_default) {
  691.         char *s;
  692.         s=getvar("module_mode");
  693.         if(s==NULL) {
  694.             nomode: mode=mode_default; force_setvar("wims_mode","");
  695.         }
  696.         else switch(mode) {
  697.             case mode_popup: {
  698.                 if(strstr(s,"popup")==NULL) goto nomode;
  699.                 else break;
  700.             }
  701.             case mode_raw: {
  702.                 if(strstr(s,"raw")==NULL) goto nomode;
  703.                 else break;
  704.             }
  705.             default: break;
  706.         }
  707.     }
  708.         /* test for existence of some special files in module's directory */
  709.     for(i=0;i<MODSPEC_NO;i++) {
  710.         if(ftest(mkfname(NULL,"%s/%s.phtml",module_prefix,module_special_file[i]))
  711.            ==is_file) {
  712.             snprintf(buf,sizeof(buf),"module_has_%s",module_special_file[i]);
  713.             setvar(buf,"yes");
  714.         }
  715.     }
  716.     p=getvar("module_language"); if(p==NULL || *p==0) setvar("module_language","en");
  717.     setvar("module_has_about","yes"); /* now we have default about.phtml */
  718.         /* Set var_def */
  719.     p=getvar("module_vardef");
  720.     if(p!=NULL && *p!=0) mystrncpy(var_def_file,p,sizeof(var_def_file));
  721.     p=getvar("module_wims_version"); if(p!=NULL && *p!=0) {
  722.         v1=atof(p); v2=atof(wims_version);
  723.         if(finite(v1) && finite(v2) &&
  724.            (v1>v2 || (v1==v2 && strcmp(find_word_start(p),wims_version)>0))) {
  725.             setvar("module_wims_version",p);
  726.             setvar("wims_version",wims_version);
  727.             module_error("antidated_version");
  728.         }
  729.     }
  730.     if(isdevelmodule) {
  731.         p=getvar("module_scoring");
  732.         if(p==NULL || strcasecmp(p,"yes")!=0) isdevelmodule=0;
  733.     }
  734. }
  735.  
  736. #endif
  737.  
  738.         /* Set up a unique job identity as a 4-bytes integer */
  739. unsigned long create_job_ident(void)
  740. {
  741. /* unsigned long it; // 64-bit compatibility: take away _long_ */
  742.     unsigned long it;
  743.     int i;
  744.         /* Is this enough to guarantee uniqueness? */
  745.  
  746.     it=(nowtime<<16)+(getpid()&0xffff);
  747.         /* The encryption is very simple. */
  748.     it=it^0x5a3c9671;
  749.     for(i=0;i<MAX_SESRANDOM;i++) sesrandomtab[i]=random();
  750.     /* ensure that the result will be a 4-bytes integer gives random problem on 32 bits
  751.     return it;*/
  752.     return it & 0xffffffff;
  753. }
  754.  
  755.         /* Setup a job identifier */
  756. void set_job_ident(void)
  757. {
  758.     unsigned long l,r;
  759.  
  760.     l=create_job_ident();
  761.     /* the last 4 bits always make 0xA. A bug of glibc random()? */
  762.     r=random()>>4;
  763.     snprintf(job_identifier,sizeof(job_identifier),"%lX%08lX",r,l);
  764.     var_noexport=1;
  765.     setvar("job_identifier",job_identifier);
  766.     var_noexport=0;
  767. }
  768.  
  769.         /* define the variable $wims_html_header */
  770. void define_html_header(void)
  771. {
  772.   char *expir, *sp, *cp, *ladirection, *mp;
  773.     char buf[MAX_LINELEN+1],ebuf[128];
  774.     char *nocache="<meta http-equiv=\"Pragma\" content=\"no-cache\" />\n\
  775. <meta http-equiv=\"Cache-Control\" content=\"no-cache\" />\n";
  776.     int noc;
  777.     time_t t;
  778.  
  779.     noc=0;
  780.     cp=getvar("wims_expire"); if(cp!=NULL) goto css;
  781.     if(!robot_access && cmd_type==cmd_intro && isclassmodule) {
  782.         sp=getvar("special_parm"); if(sp==NULL) sp="";
  783.         if(strcmp(sp,".nocache.")==0) {
  784.             force_setvar("special_parm",""); noc=1;
  785.         }
  786.         if(!noc) {
  787.             mp=getvar(ro_name[ro_module]);
  788.             if(mp!=NULL && strncmp(mp,"devel/",strlen("devel/"))==0) noc=1;
  789.         }
  790.     }
  791.     if(mode==mode_default) {
  792.         if(!robot_access && (cmd_type!=cmd_intro || noc)) {
  793.             if(html_call) {
  794.                 /* expiration in 1 day for html call. */
  795.                 t=nowtime+(long) 24*3600; expir=ctime(&t);
  796.                 nocache="";
  797.             }
  798.             else expir="1 Jan 1990";
  799.         }
  800.         else {
  801.             /* expiration in 10 days for robot access or intro page. */
  802.             t=nowtime+(long) 10*24*3600; expir=ctime(&t); nocache="";
  803.         }
  804.         snprintf(ebuf,sizeof(ebuf),"%s",expir); strip_trailing_spaces(ebuf);
  805.         snprintf(buf,sizeof(buf),
  806.                  "<meta http-equiv=\"expires\" content=\"%s\" />\n%s",ebuf,nocache);
  807.         setvar("wims_expire",buf);
  808.     }
  809.     css: setvar("wims_CSS","");
  810.     cp=getvar("wims_css");
  811.     ladirection=getvar("wims_main_dirn");
  812.     if (strcmp(ladirection,"ltr")==0) ladirection="";
  813.     if(!robot_access && cp!=NULL && *cp!=0 && strstr(cp,"---")==NULL) {
  814.         char *nbuf;
  815.         cp=find_word_start(cp);
  816.         if(strchr(cp,'/')==NULL) {
  817.             char *pc, *th, *ti; int st=0;
  818.             pc=getvar("wims_class");
  819.             if(pc!=NULL && *pc!=0 && strcmp(cp,"class")==0) {
  820.                  nbuf=mkfname(NULL,"%s/%s/css",class_base,pc);
  821.                  th=getvar("class_theme");ti=getvar("class_theme_icon") ;
  822.                  if(th==NULL || *th==0) th=getvar("wims_theme"); st=1;
  823.                  if(ti==NULL || *ti==0) ti=getvar("wims_theme_icon");
  824.             }
  825.             else {/* Il faut peut-être changer là aussi pour direction */
  826.                 nbuf=mkfname(NULL,"html/css/%s/%s.css",lang,cp);
  827.                 th=getvar("wims_theme");
  828.                 ti=getvar("wims_theme_icon");
  829.             }
  830.             if(strcmp(cp,"-theme-")==0 && strchr(th,'.')==NULL) {
  831.                   if(th==NULL || *th==0) th="default";
  832.                   nbuf=mkfname(NULL,"html/themes/%s/css%s.css",th,ladirection);
  833.             }
  834.             if(readfile(nbuf,tmplbuf,sizeof(tmplbuf))) {
  835.             }
  836.             else {
  837.              th=getvar("wims_theme");
  838.              nbuf=mkfname(NULL,"html/themes/%s/css%s.css",th,ladirection);
  839.                 }
  840.                 if(readfile(nbuf,tmplbuf,sizeof(tmplbuf))) {
  841.               if (st) {snprintf(buf,sizeof(buf),"<style type=\"text/css\"><!--\n\
  842. %s\n\
  843. --></style>",tmplbuf); }
  844.                   else {
  845.                   snprintf(buf,sizeof(buf),"<link href=\"%s\" rel=\"stylesheet\" type=\"text/css\" />\n\
  846. <link href=\"gifs/themes/%s/icon-min.css\" rel=\"stylesheet\" type=\"text/css\" />",nbuf,ti);
  847.                  }
  848.                 setvar("wims_CSS",buf);
  849.             }
  850.             else {
  851.        
  852.             }
  853.             if (th) {
  854.                nbuf=mkfname(NULL,"html/themes/%s/htmlheader.phtml",th) ;
  855.                if (readfile(nbuf,tmplbuf,sizeof(tmplbuf))) html_header=nbuf ;
  856.             }
  857.           }
  858.     }
  859.     if(readfile(html_header,buf,sizeof(buf))) setvar("wims_html_header",buf);
  860. }
  861.  
  862. void setsesdir(char *d)
  863. {
  864.     char buf[MAX_FNAME+1];
  865.     if(strstr(d,parent_dir_string)!=NULL) {
  866.         force_setvar("wims_session",robot_session);
  867.         mystrncpy(buf,robot_session,sizeof(buf));
  868.         mkfname(session_dir,"../tmp");
  869.         mkfname(s2_dir,"../tmp");
  870.     }
  871.     else {
  872.         force_setvar("wims_session",d);
  873.         snprintf(buf,sizeof(buf),"%s/%s",SESSION_BASE,d);
  874.         mkfname(session_dir,"../%s",SESSION_BASE);
  875.         /* mkfname(s2_dir,"../%s",S2_BASE); */
  876.     }
  877.     force_setvar("wims_sesdir",buf);
  878. }
  879.  
  880.