Subversion Repositories wimsdev

Rev

Rev 8171 | Rev 8191 | 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. #ifdef MACOSX
  78.  char *commonpath="/usr/local/bin:/usr/bin:/usr/games:/bin:/sw/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/texbin";
  79. #else
  80.  char *commonpath="/usr/local/bin:/usr/bin:/usr/games:/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/texbin";
  81. #endif
  82. char *gap_command="gap.sh -T -m 64M";
  83. char *maxima_command="maxima";
  84. char *pari_command="gp -f -q -p 150000";
  85. char *scilab_command="scilab -nwni";
  86. char *QRencode_command="qrencode -o";
  87.  
  88.   /* default languages */
  89. char site_langbuf[]="en fr cn es it nl";
  90. char *site_languages=site_langbuf;
  91.   /* Statistics shows up? */
  92. char *show_stat="no";
  93.   /* Author names show up in search result? */
  94. char *show_author="no";
  95.   /* Show user IP adress to supervisor */
  96. char *show_ip="yes";
  97.   /* busy hours definition */
  98. char *busyhours="";
  99.   /* Presentation theme */
  100. char *theme="standard";
  101.   /* directory of session files */
  102. char session_dir[MAX_FNAME+1], s2_dir[MAX_FNAME+1];
  103.   /* standardised header model for modules' output. Will be stored
  104.    * into the variable $wims_html_header. If the theme contains
  105.    * a file 'head.phtml', html_header will be defined by the address
  106.    * of this file  in subroutine define_html_header */
  107. char *html_header="html/header.phtml";
  108.   /* standardised about table for modules */
  109. char *module_about_file="html/module_about";
  110.   /* title definition page */
  111. char *title_page="title.phtml";
  112.   /* stardardised header menu bar, to be included in module pages
  113.    * via command !headmenu */
  114. char *header_menu="headmenu.phtml";
  115. char *header_menu_user="headmenu_user.phtml";
  116. char *header_menu_supervisor="headmenu_supervisor.phtml";
  117.   /* standardised referer to wims home, to be included in
  118.    * module pages via command !homeref */
  119. char *home_referer="homeref.phtml";
  120. char *home_referer_user="homeref_user.phtml";
  121. char *home_referer_supervisor="homeref_supervisor.phtml";
  122.   /* background color in standard reference tables */
  123. char *ref_bgcolor="#777777";
  124.   /* link color */
  125. char *link_color="#004077";
  126.   /* vlink color */
  127. char *vlink_color="#004077";
  128.   /* link color in standard reference tables */
  129. char *ref_menucolor="white";
  130.   /* background help button color */
  131. char *ref_button_help_bgcolor="green";
  132.   /* background help button color */
  133. char *ref_button_help_color="white";
  134.   /* button background color */
  135. char *ref_button_bgcolor="#666666";
  136.   /* button color */
  137. char *ref_button_color="white";
  138. /* module log disabled by default */
  139. int modlog=0;
  140. /* name of the variable definition file for every module.
  141.  * It is this file which defines the presence of the module.
  142. * Better do not make it configurable.
  143. */
  144. char var_def_file[256];
  145. /* name of variable init file. Processed only when cmd=new or renew. */
  146. char *var_init_file="var.init";
  147. /* name of variable calculation file. Parsed at each call. */
  148. char *main_var_proc_file="var.proc";
  149. /* name of the main (programmable) html file. */
  150. char *html_file="main.phtml";
  151. /* Introduction page name */
  152. char *intro_file="intro.phtml";
  153. /* Reference page */
  154. char *ref_file="wims_ref.phtml";
  155. /* script to process different inserts.
  156.  * The two consecutive points in the name prohibits ordinary
  157.  * user execution of the script.
  158.  */
  159. char *insdraw_processor="insdraw..processor";
  160. char *insplot_processor="insplot..processor";
  161. char *tex2gif="bin/tex..gif";
  162. char *priority=""; /* IPs of priority sites */
  163. char *texgif_fontdir="w/texgif";
  164. char *texgif_texheader="mathfonts/header";
  165. char *bgcolor="#E8E8E8"; /* page background color */
  166. char *bgimg="";  /* page background image file */
  167. char *pagecss="-theme-";  /* style sheet file */
  168. char *theme_icon="default";  /* icon */
  169. char *usecookie="no";  /* whether to send cookies to anon requests */
  170. char *mail_opt="";  /* !mailto options */
  171. char nodeip[256]="";  /* ip of cluster node if relevant */
  172. char temp_log[MAX_FNAME+1]; /* temp log file name */
  173.  /* 0: low; 1: high; 2: MathML */
  174. int mathalign_base=0;  /* Not use <sup> for middle alignment */
  175. char *disable_zoom="no";  /* yes/no:  default zooming allowed (if enabled via "useropts")  */
  176. int spec_font=0; /* choose of some fonts*/
  177.  /* special font can be configured in log/wims.conf with variable special_font*/
  178. char *special_font="letter-spacing:0.3em;line-height:2.7em;word-spacing:0.8em;";
  179. int backup_hour=-1; /* Hour for daily backup; -1 means no backup. */
  180. int site_accounting=0; /* whether accounting should be activated */
  181. int examlog_limit=7; /* number of logged exam sessions for each student */
  182.  
  183.   /* Automatic module update */
  184. char *mu_auto="none";
  185. char *mu_cat="all";
  186. char *mu_delay="3";
  187. char *mu_site="http://wims.unice.fr/download/modules";
  188. char *mu_zone="all";
  189. char *mu_download_site="unice,wimsedu";
  190. char *mu_publish_site="unice";
  191.  
  192.   /* keywords destinated to robots */
  193. char *site_keywords="interactive mathematics, interactive math, server side interactivity";
  194. char *site_description="interactive exercises, online calculators and plotters, mathematical recreation and games";
  195.  
  196.   /* class and document authorization */
  197. int class_quota=100;
  198. int superclass_quota=100;
  199. int doc_quota=32;
  200. char *class_regpass="email,email,deny";
  201. char *doc_regpass="";
  202.  
  203.   /* Where to find programs; if your httpd sets /bin:/usr/bin in PATH,
  204.    * this should be OK. */
  205. char *sed_prog="sed";
  206. char *tr_prog="tr";
  207. char *awk_prog="awk";
  208.  
  209.   /* myname */
  210. char *cgi_name="wims.cgi";
  211. char *aliased_cgi="no";
  212. char *aliased_getfile="yes";
  213.  
  214.   /* Limits for classes and participants */
  215. int class_limit=200; /* limit of number of classes */
  216. int user_limit=5000; /* limit of total number of registered users */
  217. int class_user_limit=300; /* limit of users in one class */
  218. int forum_limit=100;  /* Limit of message boards */
  219.  
  220. CONFIG_DATA main_config[]={
  221.       {"PATH",                  0, &commonpath},
  222.       {"QRencode_command",      2, &QRencode_command},
  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. int 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++){};
  363.             if(*pp=='/') *(p-3)=0;
  364.         }
  365.         snprintf(ref_name,sizeof(ref_name)-2,"http://%s%s/%s",s1,buf,cgi_name);
  366.         mystrncpy(ref_base,ref_name,sizeof(ref_base)-2);
  367.         p=strrchr(ref_base,'/');
  368.         if(p!=NULL) *(p+1)=0;
  369.     }
  370.     else mystrncpy(ref_name,cgi_name,sizeof(ref_name)-1);
  371.     setvar("wims_ref_name",ref_name);
  372. }
  373.  
  374.         /* Read and interprete wims configuration file. */
  375. void main_configure(void)
  376. {
  377.     int i;
  378.     char *conf_buf,*e,*p,*p2,*p3;
  379.     char buf[MAX_LINELEN+1];
  380.  
  381.     conf_buf=readfile(config_file,NULL,WORKFILE_LIMIT);
  382.     if(conf_buf==NULL) goto fileend;
  383.     e=conf_buf-1; while(e) {
  384.         p=e+1; e=strchr(p,'\n'); if(e) *e=0;
  385.         p=find_word_start(p);
  386.         if(*p==0 || *p==comment_prefix_char) continue; /* empty or comment line */
  387.         p2=strchr(p,'=');
  388.         if(p2==NULL) continue; /* syntax error */
  389.         *p2=0; *find_word_end(p)=0; p2=find_word_start(p2+1);
  390.         p3=p2+strlen(p2); while(myislspace(p3[-1])) p3--;
  391.         while(p3[-1]=='\\') p3--; *p3=0;
  392.         i=search_list(main_config,MAIN_CONFIG_NO,sizeof(main_config[0]),p);
  393.         if(i<0) continue; /* name non-defined */
  394.         if(main_config[i].is_integer==1) {
  395.             int *ip;
  396.             ip=main_config[i].address;
  397.             *ip=atoi(p2); if(*ip<0) *ip=0;
  398.         }
  399.         else {
  400.             char **cp;
  401.             cp=main_config[i].address;
  402.             *cp=p2;
  403.         }
  404.     }
  405.     fileend:
  406.     setenv("session_base_dir",session_dir,1);
  407.     determine_ref_name();
  408.     language_list();
  409.     for(i=0;i<MAIN_CONFIG_NO;i++) {
  410.         char **pp;
  411.         if(main_config[i].is_integer==2) {
  412.             pp=main_config[i].address;
  413.             setenv(main_config[i].name,*pp,1);
  414.         }
  415.     }
  416.         /* check priority */
  417.     if(priority[0] && checkhost(priority)>0) ispriority=1;
  418.     (void)getcwd(cwdbuf,sizeof(cwdbuf)); setvar("httpd_PWD",cwdbuf);
  419.     setenv("wims_server_base",cwdbuf,1);
  420.     p=strrchr(cwdbuf,'/');
  421.     if(p!=NULL && strcmp(p,"/public_html")==0) {
  422.         *p=0; snprintf(PATH,sizeof(PATH),"%s/other/bin:%s",cwdbuf,commonpath);
  423.         force_setvar("wims_home",cwdbuf);
  424.         *p='/';
  425.     }
  426.     else {
  427.         snprintf(PATH,sizeof(PATH),"%s/../other/bin:%s",cwdbuf,commonpath);
  428.         force_setvar("wims_home",cwdbuf);
  429.     }
  430.     setenv("PATH",PATH,1);
  431.     p=getvar("httpd_SERVER_SOFTWARE");
  432.     if(p && (p2=strstr(p,"Apache"))!=NULL && strcmp(p2,"Apache/1.3")>=0
  433.        && strstr(aliased_getfile,"yes")!=NULL)
  434.       good_httpd=1;
  435.     accessfile(buf,"r","../tmp/log/myip");
  436.     *find_word_end(buf)=0;
  437.     if(buf[0]) {
  438.      snprintf(nodeip,sizeof(nodeip),"%s",buf);
  439.      snprintf(temp_log,sizeof(temp_log),"%s.%s",TEMP_LOG_FILE,nodeip);
  440.      force_setvar("wims_priv_nodeip",nodeip);
  441.     }
  442.     else {
  443.      nodeip[0]=0;
  444.      snprintf(temp_log,sizeof(temp_log),"%s",TEMP_LOG_FILE);
  445.     }
  446. }
  447.  
  448. struct {
  449.     int rname;
  450.     int *rval;
  451. } resource_table[]={
  452. #ifdef RLIMIT_CPU
  453.       {RLIMIT_CPU, &rlimit_cpu},
  454. #endif
  455. #ifdef RLIMIT_FSIZE
  456.       {RLIMIT_FSIZE,  &rlimit_fsize},
  457. #endif
  458. #ifdef RLIMIT_AS
  459.       {RLIMIT_AS, &rlimit_as},
  460. #endif
  461. #ifdef RLIMIT_DATA
  462.       {RLIMIT_DATA, &rlimit_data},
  463. #endif
  464. #ifdef RLIMIT_STACK
  465.       {RLIMIT_STACK, &rlimit_stack},
  466. #endif
  467. #ifdef RLIMIT_CORE
  468.       {RLIMIT_CORE, &rlimit_core},
  469. #endif
  470. #ifdef RLIMIT_RSS
  471.       {RLIMIT_RSS, &rlimit_rss},
  472. #endif
  473. #ifdef RLIMIT_NPROC
  474.       {RLIMIT_NPROC, &rlimit_nproc},
  475. #endif
  476. #ifdef RLIMIT_NOFILE
  477.       {RLIMIT_NOFILE, &rlimit_nofile},
  478. #endif
  479. #ifdef RLIMIT_MEMLOCK
  480.       {RLIMIT_MEMLOCK, &rlimit_memlock}
  481. #endif
  482. };
  483. #define RESOURCE_NO (sizeof(resource_table)/sizeof(resource_table[0]))
  484.  
  485. /* set system resource limits */
  486. void set_rlimits(void)
  487. {
  488.     int i;
  489.     struct rlimit rlim;
  490.  
  491.     for(i=0;i<RESOURCE_NO;i++) {
  492.      rlim.rlim_cur=rlim.rlim_max=*(resource_table[i].rval);
  493.      setrlimit(resource_table[i].rname,&rlim);
  494.      if(resource_table[i].rname==RLIMIT_CPU) {
  495.          setvar("wims_cpu_limit",int2str(rlim.rlim_max));
  496.          initalarm();
  497.      }
  498.     }
  499. }
  500.  
  501. struct {
  502.     char *name;
  503.     int is_integer;
  504.     void *default_value;
  505. } module_default[]={
  506.       {"anim_format",  0, &DEFAULT_ANIM_FORMAT},
  507.       {"gnuplot_format",        0, &gnuplot_format},
  508.       {"ins_anim_limit",        1, &ANIM_LIMIT},
  509.       {"ins_density",           0, &DEFAULT_INS_DENSITY},
  510.       {"ins_format",            0, &DEFAULT_INS_FORMAT},
  511.       {"insplot_font",          0, &DEFAULT_INSPLOT_FONT},
  512.       {"wims_busyhours",        0, &busyhours},
  513.       {"wims_class_limit",      1, &class_limit},
  514.       {"wims_class_quota",      1, &class_quota},
  515.       {"wims_class_regpass",    0, &class_regpass},
  516.       {"wims_class_user_limit", 1, &class_user_limit},
  517.       {"wims_compare_precision",1, &DEFAULT_COMPARE_PRECISION},
  518.       {"wims_devel_modules",    0, &devel_modules},
  519.       {"wims_doc_quota",        1, &doc_quota},
  520.       {"wims_doc_regpass",      0, &doc_regpass},
  521.       {"wims_examlog_limit",    1, &examlog_limit},
  522.       {"wims_forum_limit",      1, &forum_limit},
  523.       {"wims_bgcolor",          0, &bgcolor},
  524.       {"wims_bgimg",            0, &bgimg},
  525.       {"wims_css",              0, &pagecss},
  526.       {"wims_link_color",       0, &link_color},
  527.       {"wims_print_precision",  1, &DEFAULT_PRINT_PRECISION},
  528.       {"wims_ref_bgcolor",      0, &ref_bgcolor},
  529.       {"wims_ref_button_bgcolor",               0, &ref_button_bgcolor},
  530.       {"wims_ref_button_color",         0, &ref_button_color},
  531.       {"wims_ref_button_help_bgcolor",          0, &ref_button_help_bgcolor},
  532.       {"wims_ref_button_help_color",            0, &ref_button_help_color},
  533.       {"wims_ref_menucolor",            0, &ref_menucolor},
  534.       {"wims_ref_menucolor",    0, &ref_menucolor},
  535.       {"wims_show_author",      0, &show_author},
  536.       {"wims_show_ip",          0, &show_ip},
  537.       {"wims_show_stat",        0, &show_stat},
  538.       {"wims_site_description", 0, &site_description},
  539.       {"wims_site_keywords",    0, &site_keywords},
  540.       {"wims_site_manager",     0, &site_manager},
  541.       {"wims_texalign",         1, &mathalign_base},
  542.       {"wims_texbasesize",      1, &texbasesize},
  543.       {"wims_theme",            0, &theme},
  544.       {"wims_theme_icon",       0, &theme_icon},
  545.       {"wims_tmp_debug",        0, &tmp_debug},
  546.       {"wims_usecookie",        0, &usecookie},
  547.       {"wims_user_limit",       1, &user_limit},
  548.       {"wims_vlink_color",      0, &vlink_color},
  549.       {"wims_superclass_quota", 1, &superclass_quota}
  550. };
  551. #define MODULE_DEFAULT_NO (sizeof(module_default)/sizeof(module_default[0]))
  552.  
  553.         /* Set defaults for module, and set corresponding variables. */
  554. void module_configure(void)
  555. {
  556.     int i;
  557.     confset=1;
  558.     for(i=0;i<MODULE_DEFAULT_NO;i++) {
  559.         if(module_default[i].is_integer) {
  560.             int j, *jp;
  561.             jp=module_default[i].default_value;
  562.             j=*jp; if(j<0) j=0;
  563.             setvar(module_default[i].name,int2str(j));
  564.         }
  565.         else {
  566.             char **cp;
  567.             cp=module_default[i].default_value;
  568.             setvar(module_default[i].name,*cp);
  569.         }
  570.     }
  571.     setenv("texgif_fontdir",texgif_fontdir,1);
  572.     setenv("texgif_texheader",texgif_texheader,1);
  573.     setvar("wims_texsize","0");
  574.     setvar("disable_zoom","0");
  575.     confset=0;
  576. }
  577.  
  578. char *modindex[]={
  579.       "address",
  580.       "author",
  581.       "category",
  582.       "copyright",
  583.       "data",
  584.       "description",
  585.       "description_ca",
  586.       "description_cn",
  587.       "description_de",
  588.       "description_en",
  589.       "description_es",
  590.       "description_fr",
  591.       "description_it",
  592.       "description_nl",
  593.       "description_si",
  594.       "description_tw",
  595.       "domain",
  596.       "help",
  597.       "keywords",
  598.       "keywords_ca",
  599.       "keywords_cn",
  600.       "keywords_de",
  601.       "keywords_en",
  602.       "keywords_es",
  603.       "keywords_fr",
  604.       "keywords_it",
  605.       "keywords_nl",
  606.       "keywords_si",
  607.       "keywords_tw",
  608.       "language",
  609.       "level",
  610.       "maintainer",
  611.       "maintainer_address",
  612.       "mode",
  613.       "require",
  614.       "scoring",
  615.       "title",
  616.       "title_ca",
  617.       "title_cn",
  618.       "title_de",
  619.       "title_en",
  620.       "title_es",
  621.       "title_fr",
  622.       "title_it",
  623.       "title_nl",
  624.       "title_si",
  625.       "title_tw",
  626.       "translator",
  627.       "translator_address",
  628.       "translator_address_ca",
  629.       "translator_address_cn",
  630.       "translator_address_de",
  631.       "translator_address_en",
  632.       "translator_address_es",
  633.       "translator_address_fr",
  634.       "translator_address_it",
  635.       "translator_address_nl",
  636.       "translator_address_si",
  637.       "translator_address_tw",
  638.       "translator_ca",
  639.       "translator_cn",
  640.       "translator_de",
  641.       "translator_en",
  642.       "translator_es",
  643.       "translator_fr",
  644.       "translator_it",
  645.       "translator_nl",
  646.       "translator_si",
  647.       "translator_tw",
  648.       "vardef",
  649.       "version",
  650.       "wims_version",
  651. };
  652. int MODINDEX_NO=(sizeof(modindex)/sizeof(modindex[0]));
  653.  
  654. char *module_special_file[]={
  655.     "intro","help"
  656. };
  657. #define MODSPEC_NO (sizeof(module_special_file)/sizeof(module_special_file[0]))
  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(isfinite(v1) && isfinite(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. /* Set up a unique job identity as a 4-bytes integer */
  734. unsigned long create_job_ident(void)
  735. {
  736. /* unsigned long it; // 64-bit compatibility: take away _long_ */
  737.     unsigned long it;
  738.     int i;
  739. /* Is this enough to guarantee uniqueness? */
  740.  
  741.     it=(nowtime<<16)+(getpid()&0xffff);
  742. /* The encryption is very simple. */
  743.     it=it^0x5a3c9671;
  744.     for(i=0;i<MAX_SESRANDOM;i++) sesrandomtab[i]=random();
  745.     /* ensure that the result will be a 4-bytes integer gives random problem on 32 bits
  746.     return it;*/
  747.     return it & 0xffffffff;
  748. }
  749.  
  750. /* Setup a job identifier */
  751. void set_job_ident(void)
  752. {
  753.     unsigned long l,r;
  754.  
  755.     l=create_job_ident();
  756.     /* the last 4 bits always make 0xA. A bug of glibc random()? */
  757.     r=random()>>4;
  758.     snprintf(job_identifier,sizeof(job_identifier),"%lX%08lX",r,l);
  759.     var_noexport=1;
  760.     setvar("job_identifier",job_identifier);
  761.     var_noexport=0;
  762. }
  763.  
  764. /* define the variable $wims_html_header */
  765. void define_html_header(void)
  766. {
  767.   char *expir, *sp, *cp, *ladirection, *mp;
  768.     char buf[MAX_LINELEN+1],ebuf[128];
  769.     char *nocache="<meta http-equiv=\"Pragma\" content=\"no-cache\" />\n\
  770. <meta http-equiv=\"Cache-Control\" content=\"no-cache\" />\n";
  771.     int noc;
  772.     time_t t;
  773.  
  774.     noc=0;
  775.     cp=getvar("wims_expire"); if(cp!=NULL) goto css;
  776.     if(!robot_access && cmd_type==cmd_intro && isclassmodule) {
  777.      sp=getvar("special_parm"); if(sp==NULL) sp="";
  778.      if(strcmp(sp,".nocache.")==0) {
  779.          force_setvar("special_parm",""); noc=1;
  780.      }
  781.      if(!noc) {
  782.          mp=getvar(ro_name[ro_module]);
  783.          if(mp!=NULL && strncmp(mp,"devel/",strlen("devel/"))==0) noc=1;
  784.      }
  785.     }
  786.     if(mode==mode_default) {
  787.      if(!robot_access && (cmd_type!=cmd_intro || noc)) {
  788.          if(html_call) {
  789. /* expiration in 1 day for html call. */
  790.           t=nowtime+(long) 24*3600; expir=ctime(&t);
  791.           nocache="";
  792.          }
  793.          else expir="1 Jan 1990";
  794.      }
  795.      else {
  796. /* expiration in 10 days for robot access or intro page. */
  797.          t=nowtime+(long) 10*24*3600; expir=ctime(&t); nocache="";
  798.      }
  799.      snprintf(ebuf,sizeof(ebuf),"%s",expir); strip_trailing_spaces(ebuf);
  800.      snprintf(buf,sizeof(buf),
  801.            "<meta http-equiv=\"expires\" content=\"%s\" />\n%s",ebuf,nocache);
  802.      setvar("wims_expire",buf);
  803.     }
  804.     css: setvar("wims_CSS","");
  805.     cp=getvar("wims_css");
  806.     ladirection=getvar("wims_main_dirn");
  807.     if (strcmp(ladirection,"ltr")==0) ladirection="";
  808.     if(!robot_access && cp!=NULL && *cp!=0 && strstr(cp,"---")==NULL) {
  809.      char *nbuf;
  810.      cp=find_word_start(cp);
  811.      if(strchr(cp,'/')==NULL) {
  812.          char *pc, *th, *ti; int st=0;
  813.          pc=getvar("wims_class");
  814.          if(pc!=NULL && *pc!=0 && strcmp(cp,"class")==0) {
  815.            nbuf=mkfname(NULL,"%s/%s/css",class_base,pc);
  816.            th=getvar("class_theme");ti=getvar("class_theme_icon") ;
  817.            if(th==NULL || *th==0) th=getvar("wims_theme"); st=1;
  818.            if(ti==NULL || *ti==0) ti=getvar("wims_theme_icon");
  819.          }
  820.          else {/* Il faut peut-être changer là aussi pour direction */
  821.           nbuf=mkfname(NULL,"html/css/%s/%s.css",lang,cp);
  822.           th=getvar("wims_theme");
  823.           ti=getvar("wims_theme_icon");
  824.          }
  825.          if(strcmp(cp,"-theme-")==0 && strchr(th,'.')==NULL) {
  826.             if(th==NULL || *th==0) th="default";
  827.             nbuf=mkfname(NULL,"html/themes/%s/css%s.css",th,ladirection);
  828.          }
  829.          if(readfile(nbuf,tmplbuf,sizeof(tmplbuf))) {
  830.          }
  831.          else {
  832.           th=getvar("wims_theme");
  833.           nbuf=mkfname(NULL,"html/themes/%s/css%s.css",th,ladirection);
  834.           }
  835.           if(readfile(nbuf,tmplbuf,sizeof(tmplbuf))) {
  836.            if (st) {snprintf(buf,sizeof(buf),"<style type=\"text/css\"><!--\n\
  837. %s\n\
  838. --></style>",tmplbuf); }
  839.             else {
  840.             snprintf(buf,sizeof(buf),"<link href=\"%s\" rel=\"stylesheet\" type=\"text/css\" />\n\
  841. <link href=\"gifs/themes/%s/icon-min.css\" rel=\"stylesheet\" type=\"text/css\" />",nbuf,ti);
  842.            }
  843.           setvar("wims_CSS",buf);
  844.          }
  845.          else {
  846.  
  847.          }
  848.          if (th) {
  849.             nbuf=mkfname(NULL,"html/themes/%s/htmlheader.phtml",th) ;
  850.             if (readfile(nbuf,tmplbuf,sizeof(tmplbuf))) html_header=nbuf ;
  851.          }
  852.        }
  853.     }
  854.     if(readfile(html_header,buf,sizeof(buf))) setvar("wims_html_header",buf);
  855. }
  856.  
  857. void setsesdir(char *d)
  858. {
  859.     char buf[MAX_FNAME+1];
  860.     if(strstr(d,parent_dir_string)!=NULL) {
  861.      force_setvar("wims_session",robot_session);
  862.      mystrncpy(buf,robot_session,sizeof(buf));
  863.      mkfname(session_dir,"../tmp");
  864.      mkfname(s2_dir,"../tmp");
  865.     }
  866.     else {
  867.      force_setvar("wims_session",d);
  868.      snprintf(buf,sizeof(buf),"%s/%s",SESSION_BASE,d);
  869.      mkfname(session_dir,"../%s",SESSION_BASE);
  870. /* mkfname(s2_dir,"../%s",S2_BASE); */
  871.     }
  872.     force_setvar("wims_sesdir",buf);
  873. }
  874.  
  875.