Subversion Repositories wimsdev

Rev

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