Subversion Repositories wimsdev

Rev

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