Subversion Repositories wimsdev

Rev

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