Subversion Repositories wimsdev

Rev

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