Subversion Repositories wimsdev

Rev

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