Subversion Repositories wimsdev

Rev

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