Subversion Repositories wimsdev

Rev

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