Subversion Repositories wimsdev

Rev

Rev 1679 | Rev 2348 | 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.         /* maximal length of any module's variable definition file. */
  24. int VAR_DEF_LENGTH_LIMIT=50000;
  25.         /* maximal nesting level of substitutions. */
  26. int SUBST_LIMIT=16;
  27.         /* We fix a limit for the number of goto's (or for jumps) executed,
  28.          * in order to avoid infinite loop or DOS attack. */
  29. int GOTO_LIMIT=500000;
  30.         /* Limit for instex, insplot, insPLOT. */
  31. int INS_LIMIT=500;
  32.         /* Limit for frames in an animation. */
  33. int ANIM_LIMIT=400;
  34.         /* module log file length */
  35. int MODULE_LOG_LIMIT=102400;
  36.         /* general log file length */
  37. int GEN_LOG_LIMIT=1024000;
  38.         /* number of old log files */
  39. int OLD_LOG_FILES=2;
  40.  
  41.         /* Resource limits */
  42. int threshold1=150;     /* First load shreshold */
  43. int threshold2=300;     /* Second load shreshold */
  44. int ispriority=0;       /* whether the connection is from priority sites */
  45.  
  46.         /* sessions not accessed more than these seconds will be erased. */
  47. int idle_time=5400;
  48.         /* non-class sessions not accessed more than these seconds will be erased. */
  49. int idle_time2=2400;
  50.         /* first-time sessions not accessed more than these seconds will be erased. */
  51. int idle_time3=900;
  52.  
  53. int rafalvl=10;         /* anti-rapidfire severity */
  54.  
  55.         /* Explanation: two real numbers a,b such that
  56.          * |a+b|>|a-b|*compare_precision
  57.          * will be considered equal in !ifvalue comparisons. */
  58. int DEFAULT_COMPARE_PRECISION=10000;
  59.         /* Precision of printing (hence of all evaluations). */
  60. int DEFAULT_PRINT_PRECISION=8;
  61.         /* Defaults for instex, insplot, etc. */
  62. char *DEFAULT_INS_FORMAT="gif";
  63. char *DEFAULT_ANIM_FORMAT="gif";
  64. char *DEFAULT_INS_DENSITY="100x100";
  65. char *DEFAULT_INSPLOT_FONT="small";
  66.  
  67.         /* gnuplot intermediate graphics format */
  68. char *gnuplot_format="gif";
  69.  
  70.   /* electronic address of site manager. */
  71. char *site_manager="wims@$httpd_HTTP_HOST";
  72.   /* Main configure file, this name cannot be configured! */
  73. char *config_file="../log/wims.conf";
  74. char PATH[512];  /* directory settings */
  75. #ifdef MACOSX
  76.  char *commonpath="/usr/local/bin:/usr/bin:/usr/games:/bin:/sw/bin:/usr/bin/X11:/usr/X11R6/bin";
  77. #else
  78.  char *commonpath="/usr/local/bin:/usr/bin:/usr/games:/bin:/usr/bin/X11:/usr/X11R6/bin:/opt/teTeX/bin";
  79. #endif
  80. char *gap_command="gap.sh -T";
  81. char *maxima_command="maxima";
  82. char *mupad_command="/usr/local/lib/mupad/share/bin/mupad";
  83. char *pari_command="gp -f -q -p 150000";
  84.  
  85.   /* default languages */
  86. char site_langbuf[]="en fr cn es it nl";
  87. char *site_languages=site_langbuf;
  88.   /* Statistics shows up? */
  89. char *show_stat="no";
  90.   /* Author names show up in search result? */
  91. char *show_author="no";
  92.   /* Show user IP adress to supervisor */
  93. char *show_ip="yes";
  94.   /* busy hours definition */
  95. char *busyhours="";
  96.   /* Presentation theme */
  97. char *theme="default";
  98.   /* directory of session files */     
  99. char session_dir[MAX_FNAME+1], s2_dir[MAX_FNAME+1];
  100.   /* standardised header model for modules' output. Will be stored
  101.    * into the variable $wims_html_header. */
  102. char *html_header="html/header.phtml";
  103.   /* standardised about table for modules */
  104. char *module_about_file="html/module_about";
  105.   /* title definition page */
  106. char *title_page="title.phtml";
  107.   /* stardardised header menu bar, to be included in module pages
  108.    * via command !headmenu */
  109. char *header_menu="headmenu.phtml";
  110. char *header_menu_user="headmenu_user.phtml";
  111. char *header_menu_supervisor="headmenu_supervisor.phtml";
  112.   /* standardised referer to wims home, to be included in
  113.    * module pages via command !homeref */
  114. char *home_referer="homeref.phtml";
  115. char *home_referer_user="homeref_user.phtml";
  116. char *home_referer_supervisor="homeref_supervisor.phtml";
  117.   /* background color in standard reference tables */
  118. char *ref_bgcolor="#d0c080";
  119.   /* background link color in standard reference tables */
  120. char *ref_menucolor="blue";
  121.   /* module log disabled by default */
  122. int modlog=0;
  123.   /* name of the variable definition file for every module.
  124.    * It is this file which defines the presence of the module.
  125.    * Better do not make it configurable. */
  126. char var_def_file[256];
  127.   /* name of variable init file. Processed only when cmd=new or renew. */
  128. char *var_init_file="var.init";
  129.   /* name of variable calculation file. Parsed at each call. */
  130. char *main_var_proc_file="var.proc";
  131.   /* name of the main (programmable) html file. */
  132. char *html_file="main.phtml";
  133.   /* Introduction page name */
  134. char *intro_file="intro.phtml";
  135.   /* Reference page */
  136. char *ref_file="wims_ref.phtml";
  137.   /* script to process different inserts.
  138.    * The two consecutive points in the name prohibits ordinary
  139.    * user execution of the script. */
  140. char *insdraw_processor="insdraw..processor";
  141. char *insplot_processor="insplot..processor";
  142. char *tex2gif="bin/tex..gif";
  143. char *priority="";              /* IPs of priority sites */
  144. char *texgif_fontdir="w/texgif";
  145. char *texgif_texheader="mathfonts/header";
  146. char *bgcolor="#E8E8E8";        /* page background color */
  147. char *bgimg="";                 /* page background image file */
  148. char *pagecss="-theme-";                /* style sheet file */
  149. char *theme_icon="default";             /* icon */
  150. char *usecookie="no";           /* whether to send cookies to anon requests */
  151. char *mail_opt="";              /* !mailto options */
  152. char nodeip[256]="";            /* ip of cluster node if relevant */
  153. char temp_log[MAX_FNAME+1];     /* temp log file name */
  154.         /* 0: low; 1: high; 2: MathML */
  155. int mathalign_base=0;           /* Not use <sup> for middle alignment */
  156. int backup_hour=-1;             /* Hour for daily backup; -1 means no backup. */
  157. int site_accounting=0;          /* whether accounting should be activated */
  158. int examlog_limit=7;            /* number of logged exam sessions for each student */
  159.  
  160.   /* Automatic module update */
  161. char *mu_auto="none";
  162. char *mu_cat="all";
  163. char *mu_delay="20";
  164. char *mu_site="http://wims.unice.fr/download/modules";
  165. char *mu_zone="all";
  166.  
  167.   /* keywords destinated to robots */
  168. char *site_keywords="interactive mathematics, interactive math, server side interactivity";
  169. char *site_description="interactive exercises, online calculators and plotters, mathematical recreation and games";
  170.  
  171.   /* class and document authorization */
  172. int class_quota=100;
  173. int superclass_quota=100;
  174. int doc_quota=32;
  175. char *class_regpass="email,email,deny";
  176. char *doc_regpass="";
  177.  
  178.   /* Where to find programs; if your httpd sets /bin:/usr/bin in PATH,
  179.    * this should be OK. */
  180. char *sed_prog="sed";
  181. char *tr_prog="tr";
  182. char *awk_prog="awk";
  183.  
  184.   /* myname */
  185. char *cgi_name="wims.cgi";
  186. char *aliased_cgi="no";
  187. char *aliased_getfile="yes";
  188.  
  189.   /* Limits for classes and participants */
  190. int class_limit=200; /* limit of number of classes */
  191. int user_limit=5000; /* limit of total number of registered users */
  192. int class_user_limit=300; /* limit of users in one class */
  193. int forum_limit=100;  /* Limit of message boards */
  194.  
  195. typedef struct {
  196.     char *name;
  197.     int is_integer;
  198.     void  *address;
  199. } CONFIG_DATA;
  200.  
  201. CONFIG_DATA main_config[]={
  202.       {"PATH",                  0, &commonpath},
  203.       {"aliased_cgi",           0, &aliased_cgi},
  204.       {"aliased_getfile",       0, &aliased_getfile},
  205.       {"backup_hour",           1, &backup_hour},
  206.       {"busyhours",             0, &busyhours},
  207.       {"cgi_name",              0, &cgi_name},
  208.       {"class_limit",           1, &class_limit},
  209.       {"class_quota",           1, &class_quota},
  210.       {"class_regpass",         0, &class_regpass},
  211.       {"class_user_limit",      1, &class_user_limit},
  212.       {"css",                   0, &pagecss},
  213.       {"default_anim_format",   0, &DEFAULT_ANIM_FORMAT},
  214.       {"default_ins_format",    0, &DEFAULT_INS_FORMAT},
  215.       {"default_insplot_font",  0, &DEFAULT_INSPLOT_FONT},
  216.       {"default_texposition",   1, &mathalign_base},
  217.       {"devel_modules",         0, &devel_modules},
  218.       {"doc_quota",             1, &doc_quota},
  219.       {"doc_regpass",           0, &doc_regpass},
  220.       {"examlog_limit",         1, &examlog_limit},
  221.       {"forum_limit",           1, &forum_limit},
  222.       {"gap_command",           2, &gap_command},
  223.       {"general_log_limit",     1, &GEN_LOG_LIMIT},
  224.       {"gnuplot_format",        0, &gnuplot_format},
  225.       {"home_module",           0, &home_module},
  226.       {"idle_time",             1, &idle_time},
  227.       {"idle_time2",            1, &idle_time2},
  228.       {"idle_time3",            1, &idle_time3},
  229.       {"insdraw_processor",     0, &insdraw_processor},
  230.       {"mail_opt",              0, &mail_opt},
  231.       {"manager_https",         1, &manager_https},
  232.       {"manager_site",          0, &manager_site},
  233.       {"maxima_command",        2, &maxima_command},
  234.       {"module_log",            1, &modlog},
  235.       {"module_log_limit",      1, &MODULE_LOG_LIMIT},
  236.       {"mu_auto",               0, &mu_auto},
  237.       {"mu_cat",                0, &mu_cat},
  238.       {"mu_delay",              0, &mu_delay},
  239.       {"mu_site",               0, &mu_site},
  240.       {"mu_zone",               0, &mu_zone},
  241.       {"mupad_command",         2, &mupad_command},
  242.       {"old_log_files",         1, &OLD_LOG_FILES},
  243.       {"page_bgcolor",          0, &bgcolor},
  244.       {"page_bgimg",            0, &bgimg},
  245.       {"pari_command",          2, &pari_command},
  246.       {"priority",              0, &priority},
  247.       {"rafale_level",          1, &rafalvl},
  248.       {"ref_bgcolor",           0, &ref_bgcolor},
  249.       {"ref_menucolor",         0, &ref_menucolor},
  250.       {"rlimit_cpu",            1, &rlimit_cpu},
  251.       {"show_author",           0, &show_author},
  252.       {"show_ip",               0, &show_ip},      
  253.       {"show_stat",             0, &show_stat},
  254.       {"site_accounting",       1, &site_accounting},
  255.       {"site_description",      0, &site_description},
  256.       {"site_keywords",         0, &site_keywords},
  257.       {"site_languages",        0, &site_languages},
  258.       {"site_manager",          0, &site_manager},
  259.       {"superclass_quota",      1, &superclass_quota},
  260.       {"texbasesize",           1, &texbasesize},
  261.       {"theme",                 0, &theme},
  262.       {"theme_icon",                    0, &theme_icon},
  263.       {"threshold1",            1, &threshold1},
  264.       {"threshold2",            1, &threshold2},
  265.       {"tmp_debug",             0, &tmp_debug},
  266.       {"trusted_modules",       0, &trusted_modules},
  267.       {"usecookie",             0, &usecookie},
  268.       {"user_limit",            1, &user_limit},
  269.       {"var_def_length_limit",  1, &VAR_DEF_LENGTH_LIMIT},
  270.       {"workfile_limit",        1, &WORKFILE_LIMIT}
  271. };
  272. #define MAIN_CONFIG_NO (sizeof(main_config)/sizeof(main_config[0]))
  273.  
  274.         /* processes the list of languages supported on the site */
  275. void language_list(void)
  276. {
  277.     int i,k;
  278.     char *lbuf[MAX_LANGUAGES];
  279.     calc_tolower(site_languages); items2words(site_languages);
  280.     setvar("wims_site_languages",site_languages);
  281.     i=cutwords(site_languages,lbuf,MAX_LANGUAGES);
  282.     available_lang_no=i;
  283.     for(i=k=0;i<available_lang_no;i++) {
  284.         if(strlen(lbuf[i])==2) memmove(available_lang[k++],lbuf[i],3);
  285.     }
  286.     if(k>0) memmove(lang,available_lang[0],3);
  287. }
  288.  
  289. char hostname[256];
  290. int html_call=0;
  291.  
  292.         /* determine the http reference name of the server */
  293. void determine_ref_name(void)
  294. {
  295.     char *s1, *s2, *p, *pl;
  296.     char buf[MAX_LINELEN+1];
  297.  
  298.     hostname[0]=0;
  299.     s1=getvar("httpd_HTTP_HOST");s2=getvar("httpd_SCRIPT_NAME");
  300.     gethostname(hostname,sizeof(hostname));
  301.     if(hostname[0]==0 && s1 && *s1!=0) mystrncpy(hostname,s1,sizeof(hostname));
  302.     if(s1==NULL || *s1==0) s1=hostname;
  303.     setvar("wims_myhostname",hostname);
  304.     ref_base[0]=0;
  305.     if(s2!=NULL) mystrncpy(buf,s2,sizeof(buf));
  306.     else buf[0]=0;
  307.     if(s1!=NULL && *buf=='/') {
  308.         p=strrchr(buf,'/'); if(p==NULL) p=buf; else *p++=0;
  309.         if(strcmp(p,cgi_name)!=0 && strlen(p)>4 &&
  310.            p[2]=='_' && islower(p[0]) && islower(p[1])) {
  311.             pre_language[0]=p[0];pre_language[1]=p[1];pre_language[2]=0;
  312.             p[2]=0; setenv("HTTP_ACCEPT_LANGUAGE",p,1); pl=p; p+=3;
  313.             s2=strrchr(p,'.'); if(s2!=NULL) {
  314.                 *s2=0; if(strcasecmp(s2+1,"cgi")!=0) html_call=1;
  315.             }
  316.             s2=p; while((s2=strpbrk(s2,"@~"))!=NULL) *s2='/';
  317.             if(strncmp(p,"doc/",4)==0) {
  318.                 char *pd, *pb;
  319.                 pd=p+4;
  320.                 if(*pd) {
  321.                     pb=strchr(pd,'/');
  322.                     if(pb) *pb++=0; else pb="main";
  323.                     setvar("doc",pd); setvar("block",pb); setvar("job","read");
  324.                 }
  325.                 setvar(ro_name[ro_module],mkfname(NULL,"adm/doc.%s",pl));
  326.             }
  327.             else setvar(ro_name[ro_module],p);
  328.             module_defined=1;
  329.         }
  330.         p=strchr(buf,'_');
  331.         if(p!=NULL && p>buf+3 && myislower(*(p-1)) && myislower(*(p-2))
  332.            && *(p-3)=='/' && myisalpha(*(p+1))) {
  333.             char *pp;
  334.             for(pp=p+1; myisalnum(*pp); pp++); if(*pp=='/') *(p-3)=0;
  335.         }
  336.         snprintf(ref_name,sizeof(ref_name)-2,"http://%s%s/%s",s1,buf,cgi_name);
  337.         mystrncpy(ref_base,ref_name,sizeof(ref_base)-2);
  338.         p=strrchr(ref_base,'/');
  339.         if(p!=NULL) *(p+1)=0;
  340.     }
  341.     else mystrncpy(ref_name,cgi_name,sizeof(ref_name)-1);
  342.     setvar("wims_ref_name",ref_name);
  343. }
  344.  
  345.         /* Read and interprete wims configuration file. */
  346. void main_configure(void)
  347. {
  348.     int i;
  349.     char *conf_buf,*e,*p,*p2,*p3;
  350.     char buf[MAX_LINELEN+1];
  351.    
  352.     conf_buf=readfile(config_file,NULL,WORKFILE_LIMIT);
  353.     if(conf_buf==NULL) goto fileend;
  354.     e=conf_buf-1; while(e) {
  355.         p=e+1; e=strchr(p,'\n'); if(e) *e=0;
  356.         p=find_word_start(p);
  357.         if(*p==0 || *p==comment_prefix_char) continue; /* empty or comment line */
  358.         p2=strchr(p,'=');
  359.         if(p2==NULL) continue; /* syntax error */
  360.         *p2=0; *find_word_end(p)=0; p2=find_word_start(p2+1);
  361.         p3=p2+strlen(p2); while(myislspace(p3[-1])) p3--;
  362.         while(p3[-1]=='\\') p3--; *p3=0;
  363.         i=search_list(main_config,MAIN_CONFIG_NO,sizeof(main_config[0]),p);
  364.         if(i<0) continue; /* name non-defined */
  365.         if(main_config[i].is_integer==1) {
  366.             int *ip;
  367.             ip=main_config[i].address;
  368.             *ip=atoi(p2); if(*ip<0) *ip=0;
  369.         }
  370.         else {
  371.             char **cp;
  372.             cp=main_config[i].address;
  373.             *cp=p2;
  374.         }
  375.     }
  376.     fileend:
  377.     setenv("session_base_dir",session_dir,1);
  378.     determine_ref_name();
  379.     language_list();
  380.     for(i=0;i<MAIN_CONFIG_NO;i++) {
  381.         char **pp;
  382.         if(main_config[i].is_integer==2) {
  383.             pp=main_config[i].address;
  384.             setenv(main_config[i].name,*pp,1);
  385.         }
  386.     }
  387.         /* check priority */
  388.     if(priority[0] && checkhost(priority)>0) ispriority=1;
  389.     getcwd(cwdbuf,sizeof(cwdbuf)); setvar("httpd_PWD",cwdbuf);
  390.     setenv("wims_server_base",cwdbuf,1);
  391.     p=strrchr(cwdbuf,'/');
  392.     if(p!=NULL && strcmp(p,"/public_html")==0) {
  393.         *p=0; snprintf(PATH,sizeof(PATH),"%s/other/bin:%s",cwdbuf,commonpath);
  394.         force_setvar("wims_home",cwdbuf);
  395.         *p='/';
  396.     }
  397.     else {
  398.         snprintf(PATH,sizeof(PATH),"%s/../other/bin:%s",cwdbuf,commonpath);
  399.         force_setvar("wims_home",cwdbuf);
  400.     }
  401.     setenv("PATH",PATH,1);
  402.     p=getvar("httpd_SERVER_SOFTWARE");
  403.     if(p && (p2=strstr(p,"Apache"))!=NULL && strcmp(p2,"Apache/1.3")>=0
  404.        && strstr(aliased_getfile,"yes")!=NULL)
  405.       good_httpd=1;
  406.     accessfile(buf,"r","../tmp/log/myip");
  407.     *find_word_end(buf)=0;
  408.     if(buf[0]) {
  409.         snprintf(nodeip,sizeof(nodeip),"%s",buf);
  410.         snprintf(temp_log,sizeof(temp_log),"%s.%s",TEMP_LOG_FILE,nodeip);
  411.         force_setvar("wims_priv_nodeip",nodeip);
  412.     }
  413.     else {
  414.         nodeip[0]=0;
  415.         snprintf(temp_log,sizeof(temp_log),"%s",TEMP_LOG_FILE);
  416.     }
  417. }
  418.  
  419. struct {
  420.     int rname;
  421.     int *rval;
  422. } resource_table[]={
  423. #ifdef RLIMIT_CPU
  424.       {RLIMIT_CPU,      &rlimit_cpu},
  425. #endif
  426. #ifdef RLIMIT_FSIZE
  427.       {RLIMIT_FSIZE,    &rlimit_fsize},
  428. #endif
  429. #ifdef RLIMIT_AS
  430.       {RLIMIT_AS,       &rlimit_as},
  431. #endif
  432. #ifdef RLIMIT_DATA
  433.       {RLIMIT_DATA,     &rlimit_data},
  434. #endif
  435. #ifdef RLIMIT_STACK
  436.       {RLIMIT_STACK,    &rlimit_stack},
  437. #endif
  438. #ifdef RLIMIT_CORE
  439.       {RLIMIT_CORE,     &rlimit_core},
  440. #endif
  441. #ifdef RLIMIT_RSS
  442.       {RLIMIT_RSS,      &rlimit_rss},
  443. #endif
  444. #ifdef RLIMIT_NPROC
  445.       {RLIMIT_NPROC,    &rlimit_nproc},
  446. #endif
  447. #ifdef RLIMIT_NOFILE
  448.       {RLIMIT_NOFILE,   &rlimit_nofile},
  449. #endif
  450. #ifdef RLIMIT_MEMLOCK
  451.       {RLIMIT_MEMLOCK,  &rlimit_memlock}
  452. #endif
  453. };
  454. #define RESOURCE_NO (sizeof(resource_table)/sizeof(resource_table[0]))
  455.  
  456.         /* set system resource limits */
  457. void set_rlimits(void)
  458. {
  459.     int i;
  460.     struct rlimit rlim;
  461.    
  462.     for(i=0;i<RESOURCE_NO;i++) {
  463.         rlim.rlim_cur=rlim.rlim_max=*(resource_table[i].rval);
  464.         setrlimit(resource_table[i].rname,&rlim);
  465.         if(resource_table[i].rname==RLIMIT_CPU) {
  466.             setvar("wims_cpu_limit",int2str(rlim.rlim_max));
  467.             initalarm();
  468.         }
  469.     }
  470. }
  471.  
  472. struct {
  473.     char *name;
  474.     int is_integer;
  475.     void *default_value;
  476. } module_default[]={
  477.       {"anim_format",           0, &DEFAULT_ANIM_FORMAT},
  478.       {"gnuplot_format",        0, &gnuplot_format},
  479.       {"ins_anim_limit",        1, &ANIM_LIMIT},
  480.       {"ins_density",           0, &DEFAULT_INS_DENSITY},
  481.       {"ins_format",            0, &DEFAULT_INS_FORMAT},
  482.       {"insplot_font",          0, &DEFAULT_INSPLOT_FONT},
  483.       {"wims_busyhours",        0, &busyhours},
  484.       {"wims_class_limit",      1, &class_limit},
  485.       {"wims_class_quota",      1, &class_quota},
  486.       {"wims_class_regpass",    0, &class_regpass},
  487.       {"wims_class_user_limit", 1, &class_user_limit},
  488.       {"wims_compare_precision",1, &DEFAULT_COMPARE_PRECISION},
  489.       {"wims_devel_modules",    0, &devel_modules},
  490.       {"wims_doc_quota",        1, &doc_quota},
  491.       {"wims_doc_regpass",      0, &doc_regpass},
  492.       {"wims_examlog_limit",    1, &examlog_limit},
  493.       {"wims_forum_limit",      1, &forum_limit},
  494.       {"wims_bgcolor",          0, &bgcolor},
  495.       {"wims_bgimg",            0, &bgimg},
  496.       {"wims_css",              0, &pagecss},
  497.       {"wims_print_precision",  1, &DEFAULT_PRINT_PRECISION},
  498.       {"wims_ref_bgcolor",      0, &ref_bgcolor},
  499.       {"wims_ref_menucolor",    0, &ref_menucolor},
  500.       {"wims_show_author",      0, &show_author},
  501.       {"wims_show_ip",          0, &show_ip},
  502.       {"wims_show_stat",        0, &show_stat},
  503.       {"wims_site_description", 0, &site_description},
  504.       {"wims_site_keywords",    0, &site_keywords},
  505.       {"wims_site_manager",     0, &site_manager},
  506.       {"wims_texalign",         1, &mathalign_base},
  507.       {"wims_texbasesize",      1, &texbasesize},
  508.       {"wims_theme",            0, &theme},
  509.       {"wims_theme_icon",       0, &theme_icon},
  510.       {"wims_tmp_debug",        0, &tmp_debug},
  511.       {"wims_usecookie",        0, &usecookie},
  512.       {"wims_user_limit",       1, &user_limit},
  513.       {"wims_superclass_quota", 1, &superclass_quota}
  514. };
  515. #define MODULE_DEFAULT_NO (sizeof(module_default)/sizeof(module_default[0]))
  516.  
  517.         /* Set defaults for module, and set corresponding variables. */
  518. void module_configure(void)
  519. {
  520.     int i;
  521.     confset=1;
  522.     for(i=0;i<MODULE_DEFAULT_NO;i++) {
  523.         if(module_default[i].is_integer) {
  524.             int j, *jp;
  525.             jp=module_default[i].default_value;
  526.             j=*jp; if(j<0) j=0;
  527.             setvar(module_default[i].name,int2str(j));
  528.         }
  529.         else {
  530.             char **cp;
  531.             cp=module_default[i].default_value;
  532.             setvar(module_default[i].name,*cp);
  533.         }
  534.     }
  535.     setenv("texgif_fontdir",texgif_fontdir,1);
  536.     setenv("texgif_texheader",texgif_texheader,1);
  537.     setvar("wims_texsize","0");
  538.     confset=0;
  539. }
  540.  
  541. char *modindex[]={
  542.       "address",
  543.       "author",
  544.       "category",
  545.       "copyright",
  546.       "data",
  547.       "description",
  548.       "domain",
  549.       "help",
  550.       "keywords",
  551.       "language",
  552.       "level",
  553.       "mode",
  554.       "require",
  555.       "scoring",
  556.       "title",
  557.       "translator",
  558.       "translator_address",
  559.       "vardef",
  560.       "version",
  561.       "wims_version",
  562. };
  563. #define MODINDEX_NO (sizeof(modindex)/sizeof(modindex[0]))
  564. char *module_special_file[]={
  565.     "intro","help"
  566. };
  567. #define MODSPEC_NO (sizeof(module_special_file)/sizeof(module_special_file[0]))
  568.  
  569. #ifdef WEBMATH
  570.  
  571.         /* read and treat module's INDEX file */
  572. void module_index(void)
  573. {
  574.     char buf[MAX_NAMELEN+1], ind_buf[MAX_LINELEN+1];
  575.     char *e, *p, *p2, *ft;
  576.     double v1, v2;
  577.     int i,l;
  578.     long indf_len;
  579.    
  580.     memmove(var_def_file,default_var_def,strlen(default_var_def)+1);
  581.     ft=readfile(mkfname(NULL,"%s/INDEX",module_prefix),
  582.                 ind_buf,sizeof(ind_buf));
  583.     if(ft==NULL) ft=readfile(mkfname(NULL,"%s/index",module_prefix),
  584.                              ind_buf,sizeof(ind_buf));
  585.     if(ft==NULL) return;
  586.     e=ind_buf-1; indf_len=strlen(ind_buf);
  587.     for(l=0; e<ind_buf+indf_len && e!=NULL ;l++) {
  588.         p=e+1; e=strchr(p,'\n'); if(e!=NULL) *e=0;
  589.         strip_trailing_spaces(p); p=find_word_start(p);
  590.         if(*p==0 || *p==comment_prefix_char) continue; /* empty or comment line */
  591.         p2=strchr(p,'=');
  592.         if(p2==NULL) continue; /* syntax error */
  593.         *p2=0; *find_word_end(p)=0; p2=find_word_start(p2+1);
  594.         i=search_list(modindex,MODINDEX_NO,sizeof(modindex[0]),p);
  595.         if(i<0) continue; /* name not in list */
  596.         snprintf(buf,sizeof(buf),"module_%s",p);
  597.         setvar(buf,p2);
  598.     }
  599.     if(mode!=mode_default) {
  600.         char *s;
  601.         s=getvar("module_mode");
  602.         if(s==NULL) {
  603.             nomode: mode=mode_default; force_setvar("wims_mode","");
  604.         }
  605.         else switch(mode) {
  606.             case mode_popup: {
  607.                 if(strstr(s,"popup")==NULL) goto nomode;
  608.                 else break;
  609.             }
  610.             case mode_raw: {
  611.                 if(strstr(s,"raw")==NULL) goto nomode;
  612.                 else break;
  613.             }
  614.             default: break;
  615.         }
  616.     }
  617.         /* test for existence of some special files in module's directory */
  618.     for(i=0;i<MODSPEC_NO;i++) {
  619.         if(ftest(mkfname(NULL,"%s/%s.phtml",module_prefix,module_special_file[i]))
  620.            ==is_file) {
  621.             snprintf(buf,sizeof(buf),"module_has_%s",module_special_file[i]);
  622.             setvar(buf,"yes");
  623.         }
  624.     }
  625.     p=getvar("module_language"); if(p==NULL || *p==0) setvar("module_language","en");
  626.     setvar("module_has_about","yes"); /* now we have default about.phtml */
  627.         /* Set var_def */
  628.     p=getvar("module_vardef");
  629.     if(p!=NULL && *p!=0) mystrncpy(var_def_file,p,sizeof(var_def_file));
  630.     p=getvar("module_wims_version"); if(p!=NULL && *p!=0) {
  631.         v1=atof(p); v2=atof(wims_version);
  632.         if(finite(v1) && finite(v2) &&
  633.            (v1>v2 || (v1==v2 && strcmp(find_word_start(p),wims_version)>0))) {
  634.             setvar("module_wims_version",p);
  635.             setvar("wims_version",wims_version);
  636.             module_error("antidated_version");
  637.         }
  638.     }
  639.     if(isdevelmodule) {
  640.         p=getvar("module_scoring");
  641.         if(p==NULL || strcasecmp(p,"yes")!=0) isdevelmodule=0;
  642.     }
  643. }
  644.  
  645. #endif
  646.  
  647.         /* Set up a unique job identity as a 4-bytes integer */
  648. unsigned long create_job_ident(void)
  649. {
  650.     unsigned long it;
  651.     int i;
  652.         /* Is this enough to guarantee uniqueness? */
  653.    
  654.     it=(nowtime<<16)+(getpid()&0xffff);
  655.         /* The encryption is very simple. */
  656.     it=it^0x5a3c9671;
  657.     for(i=0;i<MAX_SESRANDOM;i++) sesrandomtab[i]=random();
  658.     /* ensure that the result will be a 4-bytes integer gives random problem on 32 bits
  659.     return it & 0xffff;*/
  660.     return it;
  661. }
  662.  
  663.         /* Setup a job identifier */
  664. void set_job_ident(void)
  665. {
  666.     unsigned long l,r;
  667.    
  668.     l=create_job_ident();
  669.     /* the last 4 bits always make 0xA. A bug of glibc random()? */
  670.     r=random()>>4;
  671.     snprintf(job_identifier,sizeof(job_identifier),"%lX%08lX",r,l);
  672.     var_noexport=1;
  673.     setvar("job_identifier",job_identifier);
  674.     var_noexport=0;
  675. }
  676.  
  677.         /* define the variable $wims_html_header */
  678. void define_html_header(void)
  679. {
  680.     char *expir, *sp, *cp, *mp;
  681.     char buf[MAX_LINELEN+1],ebuf[128];
  682.     char *nocache="<meta http-equiv=\"Pragma\" content=\"no-cache\">\n\
  683. <meta http-equiv=\"Cache-Control\" content=\"no-cache\">\n";
  684.     int noc;
  685.     time_t t;
  686.  
  687.     noc=0;
  688.     cp=getvar("wims_expire"); if(cp!=NULL) goto css;
  689.     if(!robot_access && cmd_type==cmd_intro && isclassmodule) {
  690.         sp=getvar("special_parm"); if(sp==NULL) sp="";
  691.         if(strcmp(sp,".nocache.")==0) {
  692.             force_setvar("special_parm",""); noc=1;
  693.         }
  694.         if(!noc) {
  695.             mp=getvar(ro_name[ro_module]);
  696.             if(mp!=NULL && strncmp(mp,"devel/",strlen("devel/"))==0) noc=1;
  697.         }
  698.     }
  699.     if(mode==mode_default) {
  700.         if(!robot_access && (cmd_type!=cmd_intro || noc)) {
  701.             if(html_call) {
  702.                 /* expiration in 1 day for html call. */
  703.                 t=nowtime+(long) 24*3600; expir=ctime(&t);
  704.                 nocache="";
  705.             }
  706.             else expir="1 Jan 1990";
  707.         }
  708.         else {
  709.             /* expiration in 10 days for robot access or intro page. */
  710.             t=nowtime+(long) 10*24*3600; expir=ctime(&t); nocache="";
  711.         }
  712.         snprintf(ebuf,sizeof(ebuf),"%s",expir); strip_trailing_spaces(ebuf);
  713.         snprintf(buf,sizeof(buf),
  714.                  "<meta http-equiv=\"expires\" content=\"%s\">\n%s",ebuf,nocache);
  715.         setvar("wims_expire",buf);
  716.     }
  717.     css: setvar("wims_CSS","");
  718.     cp=getvar("wims_css");
  719.     if(!robot_access && cp!=NULL && *cp!=0 && strstr(cp,"---")==NULL) {
  720.         char *nbuf;
  721.         cp=find_word_start(cp);
  722.         if(strchr(cp,'/')==NULL) {
  723.             char *pc, *th, *ti; int st=0;
  724.             pc=getvar("wims_class");
  725.             if(pc!=NULL && *pc!=0 && strcmp(cp,"class")==0) {
  726.                  nbuf=mkfname(NULL,"%s/%s/css",class_base,pc);
  727.                 th=getvar("class_theme");ti=getvar("class_theme_icon") ;
  728.                 if(th==NULL || *th==0) th=getvar("wims_theme"); st=1;
  729.                 if(ti==NULL || *ti==0) ti=getvar("wims_theme_icon");
  730.             }
  731.             else {
  732.                 nbuf=mkfname(NULL,"html/css/%s/%s.css",lang,cp);
  733.                 th=getvar("wims_theme");
  734.                 ti=getvar("wims_theme_icon");
  735.             }
  736.             if(strcmp(cp,"-theme-")==0 && strchr(th,'.')==NULL) {
  737.                 if(th==NULL || *th==0) th="default";
  738.                 nbuf=mkfname(NULL,"html/themes/%s/css.css",th);
  739.             }
  740.             if(readfile(nbuf,tmplbuf,sizeof(tmplbuf))) {
  741.               if (st) {snprintf(buf,sizeof(buf),"<style type=\"text/css\"><!--\n\
  742. %s\n\
  743. --></style>",tmplbuf); }
  744.                   else {
  745.                   snprintf(buf,sizeof(buf),"<link href=\"%s\" rel=\"stylesheet\" type=\"text/css\">\n\
  746.                   <link href=\"gifs/themes/%s/icon.css\" rel=\"stylesheet\" type=\"text/css\">",nbuf,ti);
  747.                 }
  748.                 setvar("wims_CSS",buf);
  749.             }
  750.             else {
  751.                
  752.                
  753.             }
  754.         }
  755.     }
  756.     if(readfile(html_header,buf,sizeof(buf))) setvar("wims_html_header",buf);
  757. }
  758.  
  759. void setsesdir(char *d)
  760. {
  761.     char buf[MAX_FNAME+1];
  762.     if(strstr(d,parent_dir_string)!=NULL) {
  763.         force_setvar("wims_session",robot_session);
  764.         mystrncpy(buf,robot_session,sizeof(buf));
  765.         mkfname(session_dir,"../tmp");
  766.         mkfname(s2_dir,"../tmp");
  767.     }
  768.     else {
  769.         force_setvar("wims_session",d);
  770.         snprintf(buf,sizeof(buf),"%s/%s",SESSION_BASE,d);
  771.         mkfname(session_dir,"../%s",SESSION_BASE);
  772.         /* mkfname(s2_dir,"../%s",S2_BASE); */
  773.     }
  774.     force_setvar("wims_sesdir",buf);
  775. }
  776.  
  777.