Subversion Repositories wimsdev

Rev

Rev 15297 | Rev 15509 | 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. int spec_font=0; /* choose of some fonts*/
  173.  /* special font can be configured in log/wims.conf with variable special_font*/
  174. char *special_font="letter-spacing:0.1em;line-height:2.7em;word-spacing:0.8em;font-family:OpenDyslexic3,OpenDyslexic,sans-serif !important;";
  175. int backup_hour=-1; /* Hour for daily backup; -1 means no backup. */
  176. int site_accounting=0; /* whether accounting should be activated */
  177. int examlog_limit=7; /* number of logged exam sessions for each student */
  178. char *site_hostname="";
  179.  
  180. /* Automatic module update */
  181. char *mu_auto="none";
  182. char *mu_cat="all";
  183. char *mu_delay="3";
  184. char *mu_site="https://wims.univ-cotedazur.fr/download/modules";
  185. char *mu_zone="all";
  186. char *mu_download_site="unice,wimsedu";
  187. char *mu_publish_site="unice";
  188.  
  189. /* keywords destined to robots */
  190. char *site_keywords="interactive mathematics, interactive math, server side interactivity";
  191. char *site_description="interactive exercises, online calculators and plotters, mathematical recreation and games";
  192.  
  193. /* class and document authorization */
  194. int class_quota=150;
  195. int superclass_quota=150;
  196. int doc_quota=32;
  197. char *class_regpass="email,email,deny";
  198. char *doc_regpass="";
  199.  
  200. /* Where to find programs; if your httpd sets /bin:/usr/bin in PATH,
  201.  * this should be OK.
  202.  */
  203. char *sed_prog="sed";
  204. char *tr_prog="tr";
  205. char *awk_prog="awk";
  206.  
  207.   /* myname */
  208. char *cgi_name="wims.cgi";
  209. char *aliased_cgi="no";
  210. char *aliased_getfile="yes";
  211.  
  212.   /* Limits for classes and participants */
  213. int class_limit=200; /* limit of number of classes */
  214. int user_limit=5000; /* limit of total number of registered users */
  215. int class_user_limit=300; /* limit of users in one class */
  216. int forum_limit=100;  /* Limit of message boards */
  217.  
  218. CONFIG_DATA main_config[]={
  219.   {"PATH",0, &commonpath},
  220.   {"QRencode_command",2, &QRencode_command},
  221.   {"actcolor",0, &actcolor},
  222.   {"aliased_cgi",0, &aliased_cgi},
  223.   {"aliased_getfile",0, &aliased_getfile},
  224.   {"backup_hour",1, &backup_hour},
  225.   {"busyhours",0, &busyhours},
  226.   {"cgi_name",0, &cgi_name},
  227.   {"class_limit",1, &class_limit},
  228.   {"class_quota",1, &class_quota},
  229.   {"class_regpass",0, &class_regpass},
  230.   {"class_user_limit",1, &class_user_limit},
  231.   {"css",0, &pagecss},
  232.   {"default_anim_format",0, &DEFAULT_ANIM_FORMAT},
  233.   {"default_ins_format",0, &DEFAULT_INS_FORMAT},
  234.   {"default_insplot_font",0, &DEFAULT_INSPLOT_FONT},
  235.   {"default_texposition",1, &mathalign_base},
  236.   {"devel_modules",0, &devel_modules},
  237.   {"doc_quota",1, &doc_quota},
  238.   {"doc_regpass",0, &doc_regpass},
  239.   {"examlog_limit",1, &examlog_limit},
  240.   {"forum_limit",1, &forum_limit},
  241.   {"gap_command",2, &gap_command},
  242.   {"general_log_limit",1, &GEN_LOG_LIMIT},
  243.   {"gnuplot_format",0, &gnuplot_format},
  244.   {"hlink_color",0, &hlink_color},
  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.   {"octave_command",2, &octave_command},
  265.   {"old_log_files",1, &OLD_LOG_FILES},
  266.   {"page_bgcolor",0, &bgcolor},
  267.   {"page_bgimg",0, &bgimg},
  268.   {"pari_command",2, &pari_command},
  269.   {"priority",0, &priority},
  270.   {"rafale_level",1, &rafalvl},
  271.   {"ref_bgcolor",0, &ref_bgcolor},
  272.   {"ref_button_bgcolor",0, &ref_button_bgcolor},
  273.   {"ref_button_color",0, &ref_button_color},
  274.   {"ref_button_help_bgcolor",0, &ref_button_help_bgcolor},
  275.   {"ref_button_help_color",0, &ref_button_help_color},
  276.   {"ref_menucolor",0, &ref_menucolor},
  277.   {"rlimit_cpu",1, &rlimit_cpu},
  278.   {"scilab_command",2, &scilab_command},
  279.   {"show_author",0, &show_author},
  280.   {"show_ip",0, &show_ip},
  281.   {"show_stat",0, &show_stat},
  282.   {"site_accounting",1, &site_accounting},
  283.   {"site_description",0, &site_description},
  284.   {"site_hostname",0, &site_hostname},
  285.   {"site_keywords",0, &site_keywords},
  286.   {"site_languages",0, &site_languages},
  287.   {"site_manager",0, &site_manager},
  288.   {"superclass_quota",1, &superclass_quota},
  289.   {"texbasesize",1, &texbasesize},
  290.   {"theme",0, &theme},
  291.   {"theme_icon",0, &theme_icon},
  292.   {"threshold1",1, &threshold1},
  293.   {"threshold2",1, &threshold2},
  294.   {"tmp_debug",0, &tmp_debug},
  295.   {"tmp_debug_var",0, &tmp_debug_var},
  296.   {"trusted_modules",0, &trusted_modules},
  297.   {"usecookie",0, &usecookie},
  298.   {"user_limit",1, &user_limit},
  299.   {"var_def_length_limit",1, &VAR_DEF_LENGTH_LIMIT},
  300.   {"vlink_color",0, &vlink_color},
  301.   {"workfile_limit",1, &WORKFILE_LIMIT}
  302. };
  303. int MAIN_CONFIG_NO=(sizeof(main_config)/sizeof(main_config[0]));
  304.  
  305. /* processes the list of languages supported on the site */
  306. void language_list(void)
  307. {
  308.   int i,k;
  309.   char *lbuf[MAX_LANGUAGES];
  310.   calc_tolower(site_languages); items2words(site_languages);
  311.   setvar("wims_site_languages",site_languages);
  312.   i=cutwords(site_languages,lbuf,MAX_LANGUAGES);
  313.   available_lang_no=i;
  314.   for(i=k=0;i<available_lang_no;i++) {
  315.     if(strlen(lbuf[i])==2) memmove(available_lang[k++],lbuf[i],3);
  316.   }
  317.   if(k>0) memmove(lang,available_lang[0],3);
  318. }
  319.  
  320. char hostname[256];
  321. int html_call=0;
  322.  
  323. /* determine the http reference name of the server */
  324. void determine_ref_name(void)
  325. {
  326.   char *s1, *s2, *p, *pl;
  327.   char buf[MAX_LINELEN+1];
  328.   int len;
  329.  
  330.   hostname[0]=0;
  331.   s1=getvar("httpd_HTTP_HOST");s2=getvar("httpd_SCRIPT_NAME");
  332.   gethostname(hostname,sizeof(hostname));
  333.   if(hostname[0]==0 && s1 && *s1!=0) mystrncpy(hostname,s1,sizeof(hostname));
  334.   if(s1==NULL || *s1==0) s1=hostname;
  335.   setvar("wims_myhostname",hostname);
  336.   ref_base[0]=0;
  337.   if(s2!=NULL) mystrncpy(buf,s2,sizeof(buf));
  338.   else buf[0]=0;
  339.   if(s1!=NULL && *buf=='/') {
  340.     p=strrchr(buf,'/'); if(p==NULL) p=buf; else *p++=0;
  341.     /* address of type /wims/fr_H2~algebra~oeffraction.fr.html */
  342.     if(strcmp(p,cgi_name)!=0 && strlen(p)>4 &&
  343.        p[2]=='_' && islower(p[0]) && islower(p[1])) {
  344.       pre_language[0]=p[0];pre_language[1]=p[1];pre_language[2]=0;
  345.       p[2]=0; setenv("HTTP_ACCEPT_LANGUAGE",p,1); pl=p; p+=3;
  346.       s2=strrchr(p,'.');
  347.       if(s2!=NULL) {
  348.         *s2=0;
  349.         if(strcasecmp(s2+1,"cgi")!=0) html_call=1;
  350.       }
  351.       s2=p; while((s2=strpbrk(s2,"@~"))!=NULL) *s2='/';
  352.       if(strncmp(p,"doc/",4)==0) {
  353.         char *pd, *pb;
  354.         pd=p+4;
  355.         if(*pd) {
  356.           pb=strchr(pd,'/');
  357.           if(pb) *pb++=0; else pb="main";
  358.           setvar("doc",pd); setvar("block",pb); setvar("job","read");
  359.         }
  360.         setvar(ro_name[ro_module],mkfname(NULL,"adm/doc.%s",pl));
  361.       }
  362.       else setvar(ro_name[ro_module],p);
  363.       module_defined=1;
  364.     }
  365.     p=strchr(buf,'_');
  366.     if(p!=NULL && p>buf+3 && myislower(*(p-1)) && myislower(*(p-2))
  367.          && *(p-3)=='/' && myisalpha(*(p+1))) {
  368.       char *pp;
  369.       for(pp=p+1; myisalnum(*pp); pp++){};
  370.       if(*pp=='/') *(p-3)=0;
  371.     }
  372.     p=getenv("HTTPS");
  373.     if(p!=NULL && strcmp(p,"on")==0)
  374.       snprintf(ref_name,sizeof(ref_name)-2,"https://%s%s/%s",s1,buf,cgi_name);
  375.     else
  376.       snprintf(ref_name,sizeof(ref_name)-2,"http://%s%s/%s",s1,buf,cgi_name);
  377.     mystrncpy(ref_base,ref_name,sizeof(ref_base)-2);
  378.     p=strrchr(ref_base,'/');
  379.     if(p!=NULL) *(p+1)=0;
  380.   }
  381.   else mystrncpy(ref_name,cgi_name,sizeof(ref_name)-1);
  382.  
  383.     // XSS PROTECTION (remove unwanted chars to avoid a possible XSS hack)
  384.     // Search for a first occurence of forbidden chars
  385.   len = strcspn(ref_name, "<>'\"\\");
  386.     // Give only the ref_name before forbidden chars
  387.   ref_name[len]=0;
  388.  
  389.   setvar("wims_ref_name",ref_name);
  390. }
  391.  
  392.       /* Read and interpret wims configuration file. */
  393. void main_configure(void)
  394. {
  395.   int i;
  396.   char *conf_buf,*e,*p,*p2,*p3;
  397.   char buf[MAX_LINELEN+1];
  398.  
  399.   conf_buf=readfile(config_file,NULL,WORKFILE_LIMIT);
  400.   if(conf_buf==NULL) goto fileend;
  401.   e=conf_buf-1; while(e) {
  402.     p=e+1; e=strchr(p,'\n'); if(e) *e=0;
  403.     p=find_word_start(p);
  404.     if(*p==0 || *p==comment_prefix_char) continue; /* empty or comment line */
  405.     p2=strchr(p,'=');
  406.     if(p2==NULL) continue; /* syntax error */
  407.     *p2=0; *find_word_end(p)=0; p2=find_word_start(p2+1);
  408.     p3=p2+strlen(p2); while(myislspace(p3[-1])) p3--;
  409.     while(p3[-1]=='\\') p3--;
  410.     *p3=0;
  411.     i=search_list(main_config,MAIN_CONFIG_NO,sizeof(main_config[0]),p);
  412.     if(i<0) continue; /* name non-defined */
  413.     if(main_config[i].is_integer==1) {
  414.       int *ip;
  415.       ip=main_config[i].address;
  416.       *ip=atoi(p2); if(*ip<0) *ip=0;
  417.     }
  418.     else {
  419.       char **cp;
  420.       cp=main_config[i].address;
  421.       *cp=p2;
  422.     }
  423.   }
  424.   fileend:
  425.   setenv("session_base_dir",session_dir,1);
  426.   determine_ref_name();
  427.   language_list();
  428.   for(i=0;i<MAIN_CONFIG_NO;i++) {
  429.     char **pp;
  430.     if(main_config[i].is_integer==2) {
  431.       pp=main_config[i].address;
  432.       setenv(main_config[i].name,*pp,1);
  433.     }
  434.   }
  435.         /* check priority */
  436.   if(priority[0] && checkhost(priority)>0) ispriority=1;
  437.   (void)getcwd(cwdbuf,sizeof(cwdbuf)); setvar("httpd_PWD",cwdbuf);
  438.   setenv("wims_server_base",cwdbuf,1);
  439.   p=strrchr(cwdbuf,'/');
  440.   if(p!=NULL && strcmp(p,"/public_html")==0) {
  441.     *p=0; snprintf(PATH,sizeof(PATH),"%s/other/bin:%s",cwdbuf,commonpath);
  442.     force_setvar("wims_home",cwdbuf);
  443.     *p='/';
  444.   }
  445.   else {
  446.     snprintf(PATH,sizeof(PATH),"%s/../other/bin:%s",cwdbuf,commonpath);
  447.     force_setvar("wims_home",cwdbuf);
  448.   }
  449.   setenv("PATH",PATH,1);
  450.   p=getvar("httpd_SERVER_SOFTWARE");
  451.   if(p && (strstr(p,"Apache"))!=NULL && strstr(aliased_getfile,"yes")!=NULL)
  452.     good_httpd=1;
  453.   accessfile(buf,"r","../tmp/log/myip");
  454.   *find_word_end(buf)=0;
  455.   if(buf[0]) {
  456.     snprintf(nodeip,sizeof(nodeip),"%s",buf);
  457.     snprintf(temp_log,sizeof(temp_log),"%s.%s",TEMP_LOG_FILE,nodeip);
  458.     force_setvar("wims_priv_nodeip",nodeip);
  459.   }
  460.   else {
  461.     nodeip[0]=0;
  462.     snprintf(temp_log,sizeof(temp_log),"%s",TEMP_LOG_FILE);
  463.   }
  464. }
  465.  
  466. struct {
  467.   int rname;
  468.   int *rval;
  469. } resource_table[]={
  470. #ifdef RLIMIT_CPU
  471.   {RLIMIT_CPU, &rlimit_cpu},
  472. #endif
  473. #ifdef RLIMIT_FSIZE
  474.   {RLIMIT_FSIZE, &rlimit_fsize},
  475. #endif
  476. #ifdef RLIMIT_AS
  477.   {RLIMIT_AS, &rlimit_as},
  478. #endif
  479. #ifdef RLIMIT_DATA
  480.   {RLIMIT_DATA, &rlimit_data},
  481. #endif
  482. #ifdef RLIMIT_STACK
  483.   {RLIMIT_STACK, &rlimit_stack},
  484. #endif
  485. #ifdef RLIMIT_CORE
  486.   {RLIMIT_CORE, &rlimit_core},
  487. #endif
  488. #ifdef RLIMIT_RSS
  489.   {RLIMIT_RSS, &rlimit_rss},
  490. #endif
  491. #ifdef RLIMIT_NPROC
  492.   {RLIMIT_NPROC, &rlimit_nproc},
  493. #endif
  494. #ifdef RLIMIT_NOFILE
  495.   {RLIMIT_NOFILE, &rlimit_nofile},
  496. #endif
  497. #ifdef RLIMIT_MEMLOCK
  498.   {RLIMIT_MEMLOCK, &rlimit_memlock}
  499. #endif
  500. };
  501. #define RESOURCE_NO (sizeof(resource_table)/sizeof(resource_table[0]))
  502.  
  503. /* set system resource limits */
  504. void set_rlimits(void)
  505. {
  506.   int i;
  507.   struct rlimit rlim;
  508.  
  509.   for(i=0;i<RESOURCE_NO;i++) {
  510.     rlim.rlim_cur=rlim.rlim_max=*(resource_table[i].rval);
  511.     setrlimit(resource_table[i].rname,&rlim);
  512.     if(resource_table[i].rname==RLIMIT_CPU) {
  513.       setvar("wims_cpu_limit",int2str(rlim.rlim_max));
  514.       initalarm();
  515.     }
  516.   }
  517. }
  518.  
  519. struct {
  520.   char *name;
  521.   int is_integer;
  522.   void *default_value;
  523. } module_default[]={
  524.   {"anim_format",0, &DEFAULT_ANIM_FORMAT},
  525.   {"gnuplot_format",0, &gnuplot_format},
  526.   {"ins_anim_limit",1, &ANIM_LIMIT},
  527.   {"ins_density",0, &DEFAULT_INS_DENSITY},
  528.   {"ins_format",0, &DEFAULT_INS_FORMAT},
  529.   {"insplot_font",0, &DEFAULT_INSPLOT_FONT},
  530.   {"wims_busyhours",0, &busyhours},
  531.   {"wims_class_limit",1, &class_limit},
  532.   {"wims_class_quota",1, &class_quota},
  533.   {"wims_class_regpass",0, &class_regpass},
  534.   {"wims_class_user_limit",1, &class_user_limit},
  535.   {"wims_compare_precision",1, &DEFAULT_COMPARE_PRECISION},
  536.   {"wims_devel_modules",0, &devel_modules},
  537.   {"wims_doc_quota",1, &doc_quota},
  538.   {"wims_doc_regpass",0, &doc_regpass},
  539.   {"wims_examlog_limit",1, &examlog_limit},
  540.   {"wims_forum_limit",1, &forum_limit},
  541.   {"wims_bgcolor",0, &bgcolor},
  542.   {"wims_bgimg",0, &bgimg},
  543.   {"wims_css",0, &pagecss},
  544.   {"wims_link_color",0, &link_color},
  545.   {"wims_print_precision", 1, &DEFAULT_PRINT_PRECISION},
  546.   {"wims_ref_bgcolor",0, &ref_bgcolor},
  547.   {"wims_ref_button_bgcolor",0, &ref_button_bgcolor},
  548.   {"wims_ref_button_color",0, &ref_button_color},
  549.   {"wims_ref_button_help_bgcolor",0, &ref_button_help_bgcolor},
  550.   {"wims_ref_button_help_color",0, &ref_button_help_color},
  551.   {"wims_ref_menucolor",0, &ref_menucolor},
  552.   {"wims_show_author",0, &show_author},
  553.   {"wims_show_ip",0, &show_ip},
  554.   {"wims_show_stat",0, &show_stat},
  555.   {"wims_site_description",0, &site_description},
  556.   {"wims_site_keywords",0, &site_keywords},
  557.   {"wims_site_manager",0, &site_manager},
  558.   {"wims_texalign",1, &mathalign_base},
  559.   {"wims_texbasesize",1, &texbasesize},
  560.   {"wims_theme",0, &theme},
  561.   {"wims_theme_icon",0, &theme_icon},
  562.   {"wims_tmp_debug",0, &tmp_debug},
  563.   {"wims_usecookie",0, &usecookie},
  564.   {"wims_user_limit",1, &user_limit},
  565.   {"wims_vlink_color",0, &vlink_color},
  566.   {"wims_hlink_color",0, &hlink_color},
  567.   {"wims_superclass_quota", 1, &superclass_quota}
  568. };
  569. #define MODULE_DEFAULT_NO (sizeof(module_default)/sizeof(module_default[0]))
  570.  
  571. /* Set defaults for module, and set corresponding variables. */
  572. void module_configure(void)
  573. {
  574.   int i;
  575.   confset=1;
  576.   for(i=0;i<MODULE_DEFAULT_NO;i++) {
  577.     if(module_default[i].is_integer) {
  578.       int j, *jp;
  579.       jp=module_default[i].default_value;
  580.       j=*jp; if(j<0) j=0;
  581.       setvar(module_default[i].name,int2str(j));
  582.     }
  583.     else {
  584.       char **cp;
  585.       cp=module_default[i].default_value;
  586.       setvar(module_default[i].name,*cp);
  587.     }
  588.   }
  589.   setenv("texgif_fontdir",texgif_fontdir,1);
  590.   setenv("texgif_texheader",texgif_texheader,1);
  591.   setvar("wims_texsize","0");
  592.   setvar("disable_zoom","0");
  593.   setvar("disable_mathml","0");
  594.   setvar("force_mathml","0");
  595.   /* if 'force_mathml=yes' , a check should be done on the value of 'disable_mathml'.
  596.      so it's not fool-proof.
  597.   */
  598.   confset=0;
  599. }
  600.  
  601. char *modindex[]={
  602.   "address",
  603.   "author",
  604.   "category",
  605.   "copyright",
  606.   "data",
  607.   "description",
  608.   "description_ca",
  609.   "description_cn",
  610.   "description_de",
  611.   "description_en",
  612.   "description_es",
  613.   "description_fr",
  614.   "description_it",
  615.   "description_nl",
  616.   "description_si",
  617.   "description_tw",
  618.   "domain",
  619.   "help",
  620.   "keywords",
  621.   "keywords_ca",
  622.   "keywords_cn",
  623.   "keywords_de",
  624.   "keywords_en",
  625.   "keywords_es",
  626.   "keywords_fr",
  627.   "keywords_it",
  628.   "keywords_nl",
  629.   "keywords_si",
  630.   "keywords_tw",
  631.   "language",
  632.   "level",
  633.   "maintainer",
  634.   "maintainer_address",
  635.   "mode",
  636.   "require",
  637.   "scoring",
  638.   "title",
  639.   "title_ca",
  640.   "title_cn",
  641.   "title_de",
  642.   "title_en",
  643.   "title_es",
  644.   "title_fr",
  645.   "title_it",
  646.   "title_nl",
  647.   "title_si",
  648.   "title_tw",
  649.   "translator",
  650.   "translator_address",
  651.   "translator_address_ca",
  652.   "translator_address_cn",
  653.   "translator_address_de",
  654.   "translator_address_en",
  655.   "translator_address_es",
  656.   "translator_address_fr",
  657.   "translator_address_it",
  658.   "translator_address_nl",
  659.   "translator_address_si",
  660.   "translator_address_tw",
  661.   "translator_ca",
  662.   "translator_cn",
  663.   "translator_de",
  664.   "translator_en",
  665.   "translator_es",
  666.   "translator_fr",
  667.   "translator_it",
  668.   "translator_nl",
  669.   "translator_si",
  670.   "translator_tw",
  671.   "vardef",
  672.   "version",
  673.   "wims_version",
  674. };
  675. int MODINDEX_NO=(sizeof(modindex)/sizeof(modindex[0]));
  676.  
  677. char *module_special_file[]={
  678.   "intro","help"
  679. };
  680. #define MODSPEC_NO (sizeof(module_special_file)/sizeof(module_special_file[0]))
  681.  
  682. /* read and treat module's INDEX file */
  683. void module_index(void)
  684. {
  685.   char buf[MAX_NAMELEN+1], ind_buf[MAX_LINELEN+1];
  686.   char *e, *p, *p2, *ft;
  687.   double v1, v2;
  688.   int i,l;
  689.   long indf_len;
  690.  
  691.   memmove(var_def_file,default_var_def,strlen(default_var_def)+1);
  692.   ft=readfile(mkfname(NULL,"%s/INDEX",module_prefix),
  693.           ind_buf,sizeof(ind_buf));
  694.   if(ft==NULL) ft=readfile(mkfname(NULL,"%s/index",module_prefix),
  695.                      ind_buf,sizeof(ind_buf));
  696.   if(ft==NULL) return;
  697.   e=ind_buf-1; indf_len=strlen(ind_buf);
  698.   for(l=0; e<ind_buf+indf_len && e!=NULL ;l++) {
  699.     p=e+1; e=strchr(p,'\n'); if(e!=NULL) *e=0;
  700.     strip_trailing_spaces(p); p=find_word_start(p);
  701.     if(*p==0 || *p==comment_prefix_char) continue; /* empty or comment line */
  702.     p2=strchr(p,'=');
  703.     if(p2==NULL) continue; /* syntax error */
  704.     *p2=0; *find_word_end(p)=0; p2=find_word_start(p2+1);
  705.     i=search_list(modindex,MODINDEX_NO,sizeof(modindex[0]),p);
  706.     if(i<0) continue; /* name not in list */
  707.     snprintf(buf,sizeof(buf),"module_%s",p);
  708.     setvar(buf,p2);
  709.   }
  710.   if(mode!=mode_default) {
  711.     char *s;
  712.     s=getvar("module_mode");
  713.     if(s==NULL) {
  714.       nomode: mode=mode_default; force_setvar("wims_mode","");
  715.     }
  716.     else switch(mode) {
  717.       case mode_popup: {
  718.         if(strstr(s,"popup")==NULL) goto nomode;
  719.         else break;
  720.       }
  721.       case mode_raw: {
  722.         if(strstr(s,"raw")==NULL) goto nomode;
  723.         else break;
  724.       }
  725.       default: break;
  726.     }
  727.   }
  728. /* test for existence of some special files in module's directory */
  729.   for(i=0;i<MODSPEC_NO;i++) {
  730.     if(ftest(mkfname(NULL,"%s/%s.phtml",module_prefix,module_special_file[i]))
  731.       ==is_file) {
  732.       snprintf(buf,sizeof(buf),"module_has_%s",module_special_file[i]);
  733.       setvar(buf,"yes");
  734.     }
  735.   }
  736.   p=getvar("module_language"); if(p==NULL || *p==0) setvar("module_language","en");
  737.   setvar("module_has_about","yes"); /* now we have default about.phtml */
  738. /* Set var_def */
  739.   p=getvar("module_vardef");
  740.   if(p!=NULL && *p!=0) mystrncpy(var_def_file,p,sizeof(var_def_file));
  741.   p=getvar("module_wims_version"); if(p!=NULL && *p!=0) {
  742.     v1=atof(p); v2=atof(wims_version);
  743.     if(isfinite(v1) && isfinite(v2) &&
  744.         (v1>v2 || (v1==v2 && strcmp(find_word_start(p),wims_version)>0))) {
  745.       setvar("module_wims_version",p);
  746.       setvar("wims_version",wims_version);
  747.       module_error("antidated_version");
  748.     }
  749.   }
  750.   if(isdevelmodule) {
  751.     p=getvar("module_scoring");
  752.     if(p==NULL || strcasecmp(p,"yes")!=0) isdevelmodule=0;
  753.   }
  754. }
  755.  
  756. /* Set up a unique job identity as a 4-bytes integer */
  757. unsigned long create_job_ident(void)
  758. {
  759. /* unsigned long it; // 64-bit compatibility: take away _long_ */
  760.   unsigned long it;
  761.   int i;
  762. /* Is this enough to guarantee uniqueness? */
  763.  
  764.   it=(nowtime<<16)+(getpid()&0xffff);
  765. /* The encryption is very simple. */
  766.   it=it^0x5a3c9671;
  767.   for(i=0;i<MAX_SESRANDOM;i++) sesrandomtab[i]=random();
  768.     /* ensure that the result will be a 4-bytes integer gives random problem on 32 bits
  769.     return it;*/
  770.   return it & 0xffffffff;
  771. }
  772.  
  773. /* Setup a job identifier */
  774. void set_job_ident(void)
  775. {
  776.   unsigned long l,r;
  777.  
  778.   l=create_job_ident();
  779.   /* the last 4 bits always make 0xA. A bug of glibc random()? */
  780.   r=random()>>4;
  781.   snprintf(job_identifier,sizeof(job_identifier),"%lX%08lX",r,l);
  782.   var_noexport=1;
  783.   setvar("job_identifier",job_identifier);
  784.   var_noexport=0;
  785. }
  786.  
  787. /* define the variable $wims_html_header */
  788. void define_html_header(void)
  789. {
  790.   char *sp, *cp, *ladirection, *mp, *s2;
  791.   char buf[MAX_LINELEN+1];
  792.   int buf_length = 0;
  793.   int noc;
  794.   noc=0;
  795.   cp=getvar("wims_expire"); if(cp!=NULL) goto css;
  796.   if(!robot_access && cmd_type==cmd_intro && isclassmodule) {
  797.     sp=getvar("special_parm"); if(sp==NULL) sp="";
  798.     if(strcmp(sp,".nocache.")==0) {
  799.       force_setvar("special_parm",""); noc=1;
  800.     }
  801.     if(!noc) {
  802.       mp=getvar(ro_name[ro_module]);
  803.       if(mp!=NULL && strncmp(mp,"devel/",strlen("devel/"))==0) noc=1;
  804.     }
  805.   }
  806.   if(noc==1){
  807.     _output_("Cache-Control: no-cache, no-store, must-revalidate\r\n");
  808.   }
  809.   css:
  810.   setvar("wims_CSS","");
  811.   /* wims_css peut avoir pour valeur "---" ou "-theme-" au niveau du site
  812.     + la valeur "class" si une classe décide d'utiliser son propre css à la place du theme
  813.   */
  814.   cp=getvar("wims_css");
  815.   ladirection=getvar("wims_main_dirn");
  816.   if (strcmp(ladirection,"ltr")==0) ladirection="";
  817.  
  818.   if(!robot_access && cp!=NULL && *cp!=0 && strstr(cp,"---")==NULL) {
  819.     char *nbuf;
  820.     cp=find_word_start(cp);
  821.     if(strchr(cp,'/')==NULL) {
  822.       char *pc, *th, *ti; int class_style=0;
  823.       pc=getvar("wims_class");
  824.  
  825.       /* When a class choose its own css instead of theme css */
  826.       if(pc!=NULL && *pc!=0 && strcmp(cp,"class")==0) {
  827.         nbuf=mkfname(NULL,"%s/%s/css",class_base,pc);
  828.         th=getvar("class_theme");ti=getvar("class_theme_icon") ;
  829.         if(th==NULL || *th==0) th=getvar("wims_theme");
  830.         if(ti==NULL || *ti==0) ti=getvar("wims_theme_icon");
  831.  
  832.         // Check content of "nbuf" file. (if file is too heavy (>45Ko), it will raise a user_error)
  833.         if( readfile(nbuf,tmplbuf,sizeof(tmplbuf)) ){
  834.           // file exist
  835.           //snprintf(buf,sizeof(buf),"<style>%s</style>",tmplbuf);
  836.           // The content will be added by scripts/adm/css.phtml
  837.           class_style = 1;
  838.         }
  839.       }
  840.  
  841.       if (!class_style) {
  842.         th=getvar("wims_theme");
  843.         /* when wims_css is everything but '---' */
  844.         if(strcmp(cp,"---")!=0 && strchr(th,'.')==NULL) {
  845.           if(th==NULL || *th==0) th="standard";
  846.           nbuf=mkfname(NULL,"html/themes/%s/css.css",th);
  847.  
  848.           // Check if theme css file exists (use access from unistd.h)
  849.           if( access( nbuf, F_OK ) != -1 ) {
  850.             buf_length = snprintf(buf,sizeof(buf),"<link href=\"%s\" rel=\"stylesheet\" />",nbuf);
  851.           }
  852.         }
  853.       }
  854.       /* icon theme */
  855.       ti=getvar("wims_theme_icon");
  856.       if(ti==NULL || *ti==0) ti="default";
  857.       snprintf(buf+buf_length,sizeof(buf)-buf_length,"<link href=\"gifs/themes/%s/icon-min.css\" rel=\"stylesheet\" />",ti);
  858.  
  859.       setvar("wims_CSS",buf);
  860.  
  861.       /* case of theme */
  862.       if (th) {
  863.         s2=getvar("module_title");
  864.         if(s2!=NULL && *s2!=0) {
  865.           mystrncpy(nbuf,s2,sizeof(buf)); calc_detag(nbuf); setvar("module_title2",nbuf);
  866.         };
  867.         nbuf=mkfname(NULL,"html/themes/%s/htmlheader.phtml",th) ;
  868.         if (readfile(nbuf,tmplbuf,sizeof(tmplbuf))) html_header=nbuf ;
  869.       }
  870.     }
  871.   }
  872.   if(readfile(html_header,buf,sizeof(buf)))
  873.   setvar("wims_html_header",buf);
  874. }
  875.  
  876. /* Set Session Dir */
  877. void setsesdir(char *d)
  878. {
  879.   char buf[MAX_FNAME+1];
  880.   if(strstr(d,parent_dir_string)!=NULL) {
  881.     force_setvar("wims_session",robot_session);
  882.     mystrncpy(buf,robot_session,sizeof(buf));
  883.     mkfname(session_dir,"../tmp");
  884.     mkfname(s2_dir,"../tmp");
  885.   }
  886.   else {
  887.     force_setvar("wims_session",d);
  888.     snprintf(buf,sizeof(buf),"%s/%s",SESSION_BASE,d);
  889.     mkfname(session_dir,"../%s",SESSION_BASE);
  890. /* mkfname(s2_dir,"../%s",S2_BASE); */
  891.   }
  892.   force_setvar("wims_sesdir",buf);
  893. }
  894.