Back to index

wims  3.65+svn20090927
config.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018       /* This file contains various configuration routines for wims. */
00019 
00020         /* Limitations are essentially for security reasons:
00021          * to curb attacks by resource exhaustion of the server. */
00022 
00023         /* maximal length of any module's variable definition file. */
00024 int VAR_DEF_LENGTH_LIMIT=50000;
00025         /* maximal nesting level of substitutions. */
00026 int SUBST_LIMIT=16;
00027         /* We fix a limit for the number of goto's (or for jumps) executed,
00028          * in order to avoid infinite loop or DOS attack. */
00029 int GOTO_LIMIT=500000;
00030         /* Limit for instex, insplot, insPLOT. */
00031 int INS_LIMIT=500;
00032        /* Limit for frames in an animation. */
00033 int ANIM_LIMIT=400;
00034        /* module log file length */
00035 int MODULE_LOG_LIMIT=102400;
00036        /* general log file length */
00037 int GEN_LOG_LIMIT=1024000;
00038        /* number of old log files */
00039 int OLD_LOG_FILES=2;
00040 
00041        /* Resource limits */
00042 int threshold1=150;  /* First load shreshold */
00043 int threshold2=300;  /* Second load shreshold */
00044 int ispriority=0;    /* whether the connection is from priority sites */
00045 
00046        /* sessions not accessed more than these seconds will be erased. */
00047 int idle_time=5400;
00048        /* non-class sessions not accessed more than these seconds will be erased. */
00049 int idle_time2=2400;
00050        /* first-time sessions not accessed more than these seconds will be erased. */
00051 int idle_time3=900;
00052 
00053 int rafalvl=10;      /* anti-rapidfire severity */
00054 
00055        /* Explanation: two real numbers a,b such that
00056         * |a+b|>|a-b|*compare_precision
00057         * will be considered equal in !ifvalue comparisons. */
00058 int DEFAULT_COMPARE_PRECISION=10000;
00059        /* Precision of printing (hence of all evaluations). */
00060 int DEFAULT_PRINT_PRECISION=8;
00061        /* Defaults for instex, insplot, etc. */
00062 char *DEFAULT_INS_FORMAT="gif";
00063 char *DEFAULT_ANIM_FORMAT="gif";
00064 char *DEFAULT_INS_DENSITY="100x100";
00065 char *DEFAULT_INSPLOT_FONT="small";
00066 
00067        /* gnuplot intermediate graphics format */
00068 char *gnuplot_format="gif";
00069 
00070   /* electronic address of site manager. */
00071 char *site_manager="wims@$httpd_HTTP_HOST";
00072   /* Main configure file, this name cannot be configured! */
00073 char *config_file="../log/wims.conf";
00074 char PATH[512];  /* directory settings */
00075 #ifdef MACOSX
00076  char *commonpath="/usr/local/bin:/usr/bin:/usr/games:/bin:/sw/bin:/usr/bin/X11:/usr/X11R6/bin";
00077 #else
00078  char *commonpath="/usr/local/bin:/usr/bin:/usr/games:/bin:/usr/bin/X11:/usr/X11R6/bin:/opt/teTeX/bin";
00079 #endif
00080 char *gap_command="gap.sh -T";
00081 char *maxima_command="maxima";
00082 char *mupad_command="/usr/local/lib/mupad/share/bin/mupad";
00083 char *pari_command="gp -f -q -p 150000";
00084 
00085   /* default languages */
00086 char site_langbuf[]="en fr cn es it nl";
00087 char *site_languages=site_langbuf;
00088   /* Statistics shows up? */
00089 char *show_stat="no";
00090   /* Author names show up in search result? */
00091 char *show_author="no";
00092   /* Show user IP adress to supervisor */
00093 char *show_ip="yes"; 
00094   /* busy hours definition */
00095 char *busyhours="";
00096   /* Presentation theme */
00097 char *theme="default";
00098   /* directory of session files */ 
00099 char session_dir[MAX_FNAME+1], s2_dir[MAX_FNAME+1];
00100   /* standardised header model for modules' output. Will be stored
00101    * into the variable $wims_html_header. */
00102 char *html_header="html/header.phtml";
00103   /* standardised about table for modules */
00104 char *module_about_file="html/module_about";
00105   /* title definition page */
00106 char *title_page="title.phtml";
00107   /* stardardised header menu bar, to be included in module pages
00108    * via command !headmenu */
00109 char *header_menu="headmenu.phtml";
00110 char *header_menu_user="headmenu_user.phtml";
00111 char *header_menu_supervisor="headmenu_supervisor.phtml";
00112   /* standardised referer to wims home, to be included in 
00113    * module pages via command !homeref */
00114 char *home_referer="homeref.phtml";
00115 char *home_referer_user="homeref_user.phtml";
00116 char *home_referer_supervisor="homeref_supervisor.phtml";
00117   /* background color in standard reference tables */
00118 char *ref_bgcolor="#d0c080";
00119   /* module log disabled by default */
00120 int modlog=0;
00121   /* name of the variable definition file for every module. 
00122    * It is this file which defines the presence of the module.
00123    * Better do not make it configurable. */
00124 char var_def_file[256];
00125   /* name of variable init file. Processed only when cmd=new or renew. */
00126 char *var_init_file="var.init";
00127   /* name of variable calculation file. Parsed at each call. */
00128 char *main_var_proc_file="var.proc";
00129   /* name of the main (programmable) html file. */
00130 char *html_file="main.phtml";
00131   /* Introduction page name */
00132 char *intro_file="intro.phtml";
00133   /* Reference page */
00134 char *ref_file="wims_ref.phtml";
00135   /* script to process different inserts.
00136    * The two consecutive points in the name prohibits ordinary
00137    * user execution of the script. */
00138 char *insdraw_processor="insdraw..processor";
00139 char *insplot_processor="insplot..processor";
00140 char *tex2gif="bin/tex..gif";
00141 char *priority="";          /* IPs of priority sites */
00142 char *texgif_fontdir="w/texgif";
00143 char *texgif_texheader="mathfonts/header";
00144 char *bgcolor="#E8E8E8";    /* page background color */
00145 char *bgimg="";                    /* page background image file */
00146 char *pagecss="-theme-";           /* style sheet file */
00147 char *theme_icon="default";        /* icon */
00148 char *usecookie="no";              /* whether to send cookies to anon requests */
00149 char *mail_opt="";          /* !mailto options */
00150 char nodeip[256]="";        /* ip of cluster node if relevant */
00151 char temp_log[MAX_FNAME+1]; /* temp log file name */
00152        /* 0: low; 1: high; 2: MathML */
00153 int mathalign_base=0;              /* Not use <sup> for middle alignment */
00154 int backup_hour=-1;         /* Hour for daily backup; -1 means no backup. */
00155 int site_accounting=0;             /* whether accounting should be activated */
00156 int examlog_limit=7;        /* number of logged exam sessions for each student */
00157 
00158   /* Automatic module update */
00159 char *mu_auto="none";
00160 char *mu_cat="all";
00161 char *mu_delay="20";
00162 char *mu_site="http://wims.unice.fr/download/modules";
00163 char *mu_zone="all";
00164 
00165   /* keywords destinated to robots */
00166 char *site_keywords="interactive mathematics, interactive math, server side interactivity";
00167 char *site_description="interactive exercises, online calculators and plotters, mathematical recreation and games";
00168 
00169   /* class and document authorization */
00170 int class_quota=100;
00171 int superclass_quota=100;
00172 int doc_quota=32;
00173 char *class_regpass="email,email,deny";
00174 char *doc_regpass="";
00175 
00176   /* Where to find programs; if your httpd sets /bin:/usr/bin in PATH,
00177    * this should be OK. */
00178 char *sed_prog="sed";
00179 char *tr_prog="tr";
00180 char *awk_prog="awk";
00181 
00182   /* myname */
00183 char *cgi_name="wims.cgi";
00184 char *aliased_cgi="no";
00185 char *aliased_getfile="yes";
00186 
00187   /* Limits for classes and participants */
00188 int class_limit=200; /* limit of number of classes */
00189 int user_limit=5000; /* limit of total number of registered users */
00190 int class_user_limit=300; /* limit of users in one class */
00191 int forum_limit=100;  /* Limit of message boards */
00192 
00193 typedef struct {
00194     char *name;
00195     int is_integer;
00196     void  *address;
00197 } CONFIG_DATA;
00198 
00199 CONFIG_DATA main_config[]={
00200       {"PATH",                     0, &commonpath},
00201       {"aliased_cgi",              0, &aliased_cgi},
00202       {"aliased_getfile",   0, &aliased_getfile},
00203       {"backup_hour",              1, &backup_hour},
00204       {"busyhours",         0, &busyhours},
00205       {"cgi_name",          0, &cgi_name},
00206       {"class_limit",              1, &class_limit},
00207       {"class_quota",              1, &class_quota},
00208       {"class_regpass",            0, &class_regpass},
00209       {"class_user_limit",  1, &class_user_limit},
00210       {"css",               0, &pagecss},
00211       {"default_anim_format",      0, &DEFAULT_ANIM_FORMAT},
00212       {"default_ins_format",       0, &DEFAULT_INS_FORMAT},
00213       {"default_insplot_font",     0, &DEFAULT_INSPLOT_FONT},
00214       {"default_texposition",      1, &mathalign_base},
00215       {"devel_modules",            0, &devel_modules},
00216       {"doc_quota",         1, &doc_quota},
00217       {"doc_regpass",              0, &doc_regpass},
00218       {"examlog_limit",            1, &examlog_limit},
00219       {"forum_limit",              1, &forum_limit},
00220       {"gap_command",              2, &gap_command},
00221       {"general_log_limit", 1, &GEN_LOG_LIMIT},
00222       {"gnuplot_format",    0, &gnuplot_format},
00223       {"home_module",              0, &home_module},
00224       {"idle_time",         1, &idle_time},
00225       {"idle_time2",        1, &idle_time2},
00226       {"idle_time3",        1, &idle_time3},
00227       {"insdraw_processor", 0, &insdraw_processor},
00228       {"mail_opt",          0, &mail_opt},
00229       {"manager_https",            1, &manager_https},
00230       {"manager_site",             0, &manager_site},
00231       {"maxima_command",    2, &maxima_command},
00232       {"module_log",        1, &modlog},
00233       {"module_log_limit",  1, &MODULE_LOG_LIMIT},
00234       {"mu_auto",           0, &mu_auto},
00235       {"mu_cat",            0, &mu_cat},
00236       {"mu_delay",          0, &mu_delay},
00237       {"mu_site",           0, &mu_site},
00238       {"mu_zone",           0, &mu_zone},
00239       {"mupad_command",            2, &mupad_command},
00240       {"old_log_files",            1, &OLD_LOG_FILES},
00241       {"page_bgcolor",             0, &bgcolor},
00242       {"page_bgimg",        0, &bgimg},
00243       {"pari_command",             2, &pari_command},
00244       {"priority",          0, &priority},
00245       {"rafale_level",             1, &rafalvl},
00246       {"ref_bgcolor",              0, &ref_bgcolor},
00247       {"rlimit_cpu",        1, &rlimit_cpu},
00248       {"show_author",              0, &show_author},
00249       {"show_ip",           0, &show_ip},      
00250       {"show_stat",         0, &show_stat},
00251       {"site_accounting",   1, &site_accounting},
00252       {"site_description",  0, &site_description},
00253       {"site_keywords",            0, &site_keywords},
00254       {"site_languages",    0, &site_languages},
00255       {"site_manager",             0, &site_manager},
00256       {"superclass_quota",  1, &superclass_quota},
00257       {"texbasesize",              1, &texbasesize},
00258       {"theme",                    0, &theme},
00259       {"theme_icon",               0, &theme_icon},
00260       {"threshold1",        1, &threshold1},
00261       {"threshold2",        1, &threshold2},
00262       {"tmp_debug",         0, &tmp_debug},
00263       {"trusted_modules",   0, &trusted_modules},
00264       {"usecookie",         0, &usecookie},
00265       {"user_limit",        1, &user_limit},
00266       {"var_def_length_limit",     1, &VAR_DEF_LENGTH_LIMIT},
00267       {"workfile_limit",    1, &WORKFILE_LIMIT}
00268 };
00269 #define MAIN_CONFIG_NO (sizeof(main_config)/sizeof(main_config[0]))
00270 
00271        /* processes the list of languages supported on the site */
00272 void language_list(void)
00273 {
00274     int i,k;
00275     char *lbuf[MAX_LANGUAGES];
00276     calc_tolower(site_languages); items2words(site_languages);
00277     setvar("wims_site_languages",site_languages);
00278     i=cutwords(site_languages,lbuf,MAX_LANGUAGES);
00279     available_lang_no=i;
00280     for(i=k=0;i<available_lang_no;i++) {
00281        if(strlen(lbuf[i])==2) memmove(available_lang[k++],lbuf[i],3);
00282     }
00283     if(k>0) memmove(lang,available_lang[0],3);
00284 }
00285 
00286 char hostname[256];
00287 int html_call=0;
00288 
00289        /* determine the http reference name of the server */
00290 void determine_ref_name(void)
00291 {
00292     char *s1, *s2, *p, *pl;
00293     char buf[MAX_LINELEN+1];
00294 
00295     hostname[0]=0;
00296     s1=getvar("httpd_HTTP_HOST");s2=getvar("httpd_SCRIPT_NAME");
00297     gethostname(hostname,sizeof(hostname));
00298     if(hostname[0]==0 && s1 && *s1!=0) mystrncpy(hostname,s1,sizeof(hostname));
00299     if(s1==NULL || *s1==0) s1=hostname;
00300     setvar("wims_myhostname",hostname);
00301     ref_base[0]=0;
00302     if(s2!=NULL) mystrncpy(buf,s2,sizeof(buf));
00303     else buf[0]=0; 
00304     if(s1!=NULL && *buf=='/') {
00305        p=strrchr(buf,'/'); if(p==NULL) p=buf; else *p++=0;
00306        if(strcmp(p,cgi_name)!=0 && strlen(p)>4 &&
00307           p[2]=='_' && islower(p[0]) && islower(p[1])) {
00308            pre_language[0]=p[0];pre_language[1]=p[1];pre_language[2]=0;
00309            p[2]=0; setenv("HTTP_ACCEPT_LANGUAGE",p,1); pl=p; p+=3;
00310            s2=strrchr(p,'.'); if(s2!=NULL) {
00311               *s2=0; if(strcasecmp(s2+1,"cgi")!=0) html_call=1;
00312            }
00313            s2=p; while((s2=strpbrk(s2,"@~"))!=NULL) *s2='/';
00314            if(strncmp(p,"doc/",4)==0) {
00315               char *pd, *pb;
00316               pd=p+4; 
00317               if(*pd) {
00318                   pb=strchr(pd,'/');
00319                   if(pb) *pb++=0; else pb="main";
00320                   setvar("doc",pd); setvar("block",pb); setvar("job","read");
00321               }
00322               setvar(ro_name[ro_module],mkfname(NULL,"adm/doc.%s",pl));
00323            }
00324            else setvar(ro_name[ro_module],p);
00325            module_defined=1;
00326        }
00327        p=strchr(buf,'_'); 
00328        if(p!=NULL && p>buf+3 && myislower(*(p-1)) && myislower(*(p-2))
00329           && *(p-3)=='/' && myisalpha(*(p+1))) {
00330            char *pp;
00331            for(pp=p+1; myisalnum(*pp); pp++); if(*pp=='/') *(p-3)=0;
00332        }
00333        snprintf(ref_name,sizeof(ref_name)-2,"http://%s%s/%s",s1,buf,cgi_name);
00334        mystrncpy(ref_base,ref_name,sizeof(ref_base)-2);
00335        p=strrchr(ref_base,'/');
00336        if(p!=NULL) *(p+1)=0;
00337     }
00338     else mystrncpy(ref_name,cgi_name,sizeof(ref_name)-1);
00339     setvar("wims_ref_name",ref_name);
00340 }
00341 
00342        /* Read and interprete wims configuration file. */
00343 void main_configure(void)
00344 {
00345     int i;
00346     char *conf_buf,*e,*p,*p2,*p3;
00347     char buf[MAX_LINELEN+1];
00348     
00349     conf_buf=readfile(config_file,NULL,WORKFILE_LIMIT);
00350     if(conf_buf==NULL) goto fileend;
00351     e=conf_buf-1; while(e) {
00352        p=e+1; e=strchr(p,'\n'); if(e) *e=0;
00353        p=find_word_start(p);
00354        if(*p==0 || *p==comment_prefix_char) continue; /* empty or comment line */
00355        p2=strchr(p,'=');
00356        if(p2==NULL) continue; /* syntax error */
00357        *p2=0; *find_word_end(p)=0; p2=find_word_start(p2+1);
00358        p3=p2+strlen(p2); while(myislspace(p3[-1])) p3--;
00359        while(p3[-1]=='\\') p3--; *p3=0;
00360        i=search_list(main_config,MAIN_CONFIG_NO,sizeof(main_config[0]),p);
00361        if(i<0) continue; /* name non-defined */
00362        if(main_config[i].is_integer==1) {
00363            int *ip;
00364            ip=main_config[i].address;
00365            *ip=atoi(p2); if(*ip<0) *ip=0;
00366        }
00367        else {
00368            char **cp;
00369            cp=main_config[i].address;
00370            *cp=p2;
00371        }
00372     }
00373     fileend:
00374     setenv("session_base_dir",session_dir,1);
00375     determine_ref_name();
00376     language_list();
00377     for(i=0;i<MAIN_CONFIG_NO;i++) {
00378        char **pp;
00379        if(main_config[i].is_integer==2) {
00380            pp=main_config[i].address;
00381            setenv(main_config[i].name,*pp,1);
00382        }
00383     }
00384        /* check priority */
00385     if(priority[0] && checkhost(priority)>0) ispriority=1;
00386     getcwd(cwdbuf,sizeof(cwdbuf)); setvar("httpd_PWD",cwdbuf);
00387     setenv("wims_server_base",cwdbuf,1);
00388     p=strrchr(cwdbuf,'/');
00389     if(p!=NULL && strcmp(p,"/public_html")==0) {
00390        *p=0; snprintf(PATH,sizeof(PATH),"%s/other/bin:%s",cwdbuf,commonpath);
00391        force_setvar("wims_home",cwdbuf);
00392        *p='/';
00393     }
00394     else {
00395        snprintf(PATH,sizeof(PATH),"%s/../other/bin:%s",cwdbuf,commonpath);
00396        force_setvar("wims_home",cwdbuf);
00397     }
00398     setenv("PATH",PATH,1);
00399     p=getvar("httpd_SERVER_SOFTWARE");
00400     if(p && (p2=strstr(p,"Apache"))!=NULL && strcmp(p2,"Apache/1.3")>=0
00401        && strstr(aliased_getfile,"yes")!=NULL)
00402       good_httpd=1;
00403     accessfile(buf,"r","../tmp/log/myip");
00404     *find_word_end(buf)=0;
00405     if(buf[0]) {
00406        snprintf(nodeip,sizeof(nodeip),"%s",buf);
00407        snprintf(temp_log,sizeof(temp_log),"%s.%s",TEMP_LOG_FILE,nodeip);
00408        force_setvar("wims_priv_nodeip",nodeip);
00409     }
00410     else {
00411        nodeip[0]=0;
00412        snprintf(temp_log,sizeof(temp_log),"%s",TEMP_LOG_FILE);
00413     }
00414 }
00415 
00416 struct {
00417     int rname;
00418     int *rval;
00419 } resource_table[]={
00420 #ifdef RLIMIT_CPU
00421       {RLIMIT_CPU,   &rlimit_cpu},
00422 #endif
00423 #ifdef RLIMIT_FSIZE
00424       {RLIMIT_FSIZE,        &rlimit_fsize},
00425 #endif
00426 #ifdef RLIMIT_AS
00427       {RLIMIT_AS,    &rlimit_as},
00428 #endif
00429 #ifdef RLIMIT_DATA
00430       {RLIMIT_DATA,  &rlimit_data},
00431 #endif
00432 #ifdef RLIMIT_STACK
00433       {RLIMIT_STACK, &rlimit_stack},
00434 #endif
00435 #ifdef RLIMIT_CORE
00436       {RLIMIT_CORE,  &rlimit_core},
00437 #endif
00438 #ifdef RLIMIT_RSS
00439       {RLIMIT_RSS,   &rlimit_rss},
00440 #endif
00441 #ifdef RLIMIT_NPROC
00442       {RLIMIT_NPROC, &rlimit_nproc},
00443 #endif
00444 #ifdef RLIMIT_NOFILE
00445       {RLIMIT_NOFILE,       &rlimit_nofile},
00446 #endif
00447 #ifdef RLIMIT_MEMLOCK
00448       {RLIMIT_MEMLOCK,      &rlimit_memlock}
00449 #endif
00450 };
00451 #define RESOURCE_NO (sizeof(resource_table)/sizeof(resource_table[0]))
00452 
00453        /* set system resource limits */
00454 void set_rlimits(void)
00455 {
00456     int i;
00457     struct rlimit rlim;
00458     
00459     for(i=0;i<RESOURCE_NO;i++) {
00460        rlim.rlim_cur=rlim.rlim_max=*(resource_table[i].rval);
00461        setrlimit(resource_table[i].rname,&rlim);
00462        if(resource_table[i].rname==RLIMIT_CPU) {
00463            setvar("wims_cpu_limit",int2str(rlim.rlim_max));
00464            initalarm();
00465        }
00466     }
00467 }
00468 
00469 struct {
00470     char *name;
00471     int is_integer;
00472     void *default_value;
00473 } module_default[]={
00474       {"anim_format",              0, &DEFAULT_ANIM_FORMAT},
00475       {"gnuplot_format",    0, &gnuplot_format},
00476       {"ins_anim_limit",    1, &ANIM_LIMIT},
00477       {"ins_density",              0, &DEFAULT_INS_DENSITY},
00478       {"ins_format",        0, &DEFAULT_INS_FORMAT},
00479       {"insplot_font",             0, &DEFAULT_INSPLOT_FONT},
00480       {"wims_busyhours",    0, &busyhours},
00481       {"wims_class_limit",  1, &class_limit},
00482       {"wims_class_quota",  1, &class_quota},
00483       {"wims_class_regpass",       0, &class_regpass},
00484       {"wims_class_user_limit",    1, &class_user_limit},
00485       {"wims_compare_precision",1, &DEFAULT_COMPARE_PRECISION},
00486       {"wims_devel_modules",       0, &devel_modules},
00487       {"wims_doc_quota",    1, &doc_quota},
00488       {"wims_doc_regpass",  0, &doc_regpass},
00489       {"wims_examlog_limit",       1, &examlog_limit},
00490       {"wims_forum_limit",  1, &forum_limit},
00491       {"wims_bgcolor",             0, &bgcolor},
00492       {"wims_bgimg",        0, &bgimg},
00493       {"wims_css",          0, &pagecss},
00494       {"wims_print_precision",     1, &DEFAULT_PRINT_PRECISION},
00495       {"wims_ref_bgcolor",  0, &ref_bgcolor},
00496       {"wims_show_author",  0, &show_author},
00497       {"wims_show_ip",             0, &show_ip},
00498       {"wims_show_stat",    0, &show_stat},
00499       {"wims_site_description",    0, &site_description},
00500       {"wims_site_keywords",       0, &site_keywords},
00501       {"wims_site_manager", 0, &site_manager},
00502       {"wims_texalign",            1, &mathalign_base},
00503       {"wims_texbasesize",  1, &texbasesize},
00504       {"wims_theme",        0, &theme},
00505       {"wims_theme_icon",   0, &theme_icon},
00506       {"wims_tmp_debug",    0, &tmp_debug},
00507       {"wims_usecookie",    0, &usecookie},
00508       {"wims_user_limit",   1, &user_limit},
00509       {"wims_superclass_quota", 1, &superclass_quota}
00510 };
00511 #define MODULE_DEFAULT_NO (sizeof(module_default)/sizeof(module_default[0]))
00512 
00513        /* Set defaults for module, and set corresponding variables. */
00514 void module_configure(void)
00515 {
00516     int i;
00517     confset=1;
00518     for(i=0;i<MODULE_DEFAULT_NO;i++) {
00519        if(module_default[i].is_integer) {
00520            int j, *jp;
00521            jp=module_default[i].default_value;
00522            j=*jp; if(j<0) j=0;
00523            setvar(module_default[i].name,int2str(j));
00524        }
00525        else {
00526            char **cp;
00527            cp=module_default[i].default_value;
00528            setvar(module_default[i].name,*cp);
00529        }
00530     }
00531     setenv("texgif_fontdir",texgif_fontdir,1);
00532     setenv("texgif_texheader",texgif_texheader,1);
00533     setvar("wims_texsize","0");
00534     confset=0;
00535 }
00536 
00537 char *modindex[]={
00538       "address",
00539       "author",
00540       "category",
00541       "copyright",
00542       "data",
00543       "description",
00544       "domain",
00545       "help",
00546       "keywords",
00547       "language",
00548       "level",
00549       "mode",
00550       "require",
00551       "scoring",
00552       "title",
00553       "translator",
00554       "translator_address",
00555       "vardef",
00556       "version",
00557       "wims_version",
00558 };
00559 #define MODINDEX_NO (sizeof(modindex)/sizeof(modindex[0]))
00560 char *module_special_file[]={
00561     "intro","help"
00562 };
00563 #define MODSPEC_NO (sizeof(module_special_file)/sizeof(module_special_file[0]))
00564 
00565 #ifdef WEBMATH
00566 
00567        /* read and treat module's INDEX file */
00568 void module_index(void)
00569 {
00570     char buf[MAX_NAMELEN+1], ind_buf[MAX_LINELEN+1];
00571     char *e, *p, *p2, *ft;
00572     double v1, v2;
00573     int i,l;
00574     long indf_len;
00575     
00576     memmove(var_def_file,default_var_def,strlen(default_var_def)+1);
00577     ft=readfile(mkfname(NULL,"%s/INDEX",module_prefix),
00578               ind_buf,sizeof(ind_buf));
00579     if(ft==NULL) ft=readfile(mkfname(NULL,"%s/index",module_prefix),
00580                           ind_buf,sizeof(ind_buf));
00581     if(ft==NULL) return;
00582     e=ind_buf-1; indf_len=strlen(ind_buf);
00583     for(l=0; e<ind_buf+indf_len && e!=NULL ;l++) {
00584        p=e+1; e=strchr(p,'\n'); if(e!=NULL) *e=0;
00585        strip_trailing_spaces(p); p=find_word_start(p);
00586        if(*p==0 || *p==comment_prefix_char) continue; /* empty or comment line */
00587        p2=strchr(p,'=');
00588        if(p2==NULL) continue; /* syntax error */
00589        *p2=0; *find_word_end(p)=0; p2=find_word_start(p2+1);
00590        i=search_list(modindex,MODINDEX_NO,sizeof(modindex[0]),p);
00591        if(i<0) continue; /* name not in list */
00592        snprintf(buf,sizeof(buf),"module_%s",p);
00593        setvar(buf,p2);
00594     }
00595     if(mode!=mode_default) {
00596        char *s;
00597        s=getvar("module_mode");
00598        if(s==NULL) {
00599            nomode: mode=mode_default; force_setvar("wims_mode","");
00600        }
00601        else switch(mode) {
00602            case mode_popup: {
00603               if(strstr(s,"popup")==NULL) goto nomode;
00604               else break;
00605            }
00606            case mode_raw: {
00607               if(strstr(s,"raw")==NULL) goto nomode;
00608               else break;
00609            }
00610            default: break;
00611        }
00612     }
00613        /* test for existence of some special files in module's directory */
00614     for(i=0;i<MODSPEC_NO;i++) {
00615        if(ftest(mkfname(NULL,"%s/%s.phtml",module_prefix,module_special_file[i]))
00616           ==is_file) {
00617            snprintf(buf,sizeof(buf),"module_has_%s",module_special_file[i]);
00618            setvar(buf,"yes");
00619        }
00620     }
00621     p=getvar("module_language"); if(p==NULL || *p==0) setvar("module_language","en");
00622     setvar("module_has_about","yes"); /* now we have default about.phtml */
00623        /* Set var_def */
00624     p=getvar("module_vardef");
00625     if(p!=NULL && *p!=0) mystrncpy(var_def_file,p,sizeof(var_def_file));
00626     p=getvar("module_wims_version"); if(p!=NULL && *p!=0) {
00627        v1=atof(p); v2=atof(wims_version);
00628        if(finite(v1) && finite(v2) && 
00629           (v1>v2 || (v1==v2 && strcmp(find_word_start(p),wims_version)>0))) {
00630            setvar("module_wims_version",p);
00631            setvar("wims_version",wims_version);
00632            module_error("antidated_version");
00633        }
00634     }
00635     if(isdevelmodule) {
00636        p=getvar("module_scoring");
00637        if(p==NULL || strcasecmp(p,"yes")!=0) isdevelmodule=0;
00638     }
00639 }
00640 
00641 #endif
00642 
00643        /* Set up a unique job identity as a 4-bytes integer */
00644 unsigned long create_job_ident(void)
00645 {
00646     unsigned long it;
00647     int i;
00648        /* Is this enough to guarantee uniqueness? */
00649     
00650     it=(nowtime<<16)+(getpid()&0xffff);
00651        /* The encryption is very simple. */
00652     it=it^0x5a3c9671;
00653     for(i=0;i<MAX_SESRANDOM;i++) sesrandomtab[i]=random();
00654     /* ensure that the result will be a 4-bytes integer */
00655     return it & 0xffff;
00656 }
00657 
00658        /* Setup a job identifier */
00659 void set_job_ident(void)
00660 {
00661     unsigned long l,r;
00662     
00663     l=create_job_ident();
00664     /* the last 4 bits always make 0xA. A bug of glibc random()? */
00665     r=random()>>4;
00666     snprintf(job_identifier,sizeof(job_identifier),"%lX%08lX",r,l);
00667     var_noexport=1;
00668     setvar("job_identifier",job_identifier);
00669     var_noexport=0;
00670 }
00671 
00672        /* define the variable $wims_html_header */
00673 void define_html_header(void)
00674 {
00675     char *expir, *sp, *cp, *mp;
00676     char buf[MAX_LINELEN+1],ebuf[128];
00677     char *nocache="<meta http-equiv=\"Pragma\" content=\"no-cache\">\n\
00678 <meta http-equiv=\"Cache-Control\" content=\"no-cache\">\n";
00679     int noc;
00680     time_t t;
00681 
00682     noc=0; 
00683     cp=getvar("wims_expire"); if(cp!=NULL) goto css;
00684     if(!robot_access && cmd_type==cmd_intro && isclassmodule) {
00685        sp=getvar("special_parm"); if(sp==NULL) sp="";
00686        if(strcmp(sp,".nocache.")==0) {
00687            force_setvar("special_parm",""); noc=1;
00688        }
00689        if(!noc) {
00690            mp=getvar(ro_name[ro_module]);
00691            if(mp!=NULL && strncmp(mp,"devel/",strlen("devel/"))==0) noc=1;
00692        }
00693     }
00694     if(mode==mode_default) {
00695        if(!robot_access && (cmd_type!=cmd_intro || noc)) {
00696            if(html_call) {
00697               /* expiration in 1 day for html call. */
00698               t=nowtime+(long) 24*3600; expir=ctime(&t);
00699               nocache="";
00700            }
00701            else expir="1 Jan 1990";
00702        }
00703        else {
00704            /* expiration in 10 days for robot access or intro page. */
00705            t=nowtime+(long) 10*24*3600; expir=ctime(&t); nocache="";
00706        }
00707        snprintf(ebuf,sizeof(ebuf),"%s",expir); strip_trailing_spaces(ebuf);
00708        snprintf(buf,sizeof(buf),
00709                "<meta http-equiv=\"expires\" content=\"%s\">\n%s",ebuf,nocache);
00710        setvar("wims_expire",buf);
00711     }
00712     css: setvar("wims_CSS","");
00713     cp=getvar("wims_css");
00714     if(!robot_access && cp!=NULL && *cp!=0 && strstr(cp,"---")==NULL) {
00715        char *nbuf;
00716        cp=find_word_start(cp);
00717        if(strchr(cp,'/')==NULL) {
00718            char *pc, *th, *ti; int st=0;
00719            pc=getvar("wims_class");
00720            if(pc!=NULL && *pc!=0 && strcmp(cp,"class")==0) {
00721                nbuf=mkfname(NULL,"%s/%s/css",class_base,pc);
00722               th=getvar("class_theme");ti=getvar("class_theme_icon") ;
00723               if(th==NULL || *th==0) th=getvar("wims_theme"); st=1;
00724               if(ti==NULL || *ti==0) ti=getvar("wims_theme_icon");
00725            }
00726            else {
00727               nbuf=mkfname(NULL,"html/css/%s/%s.css",lang,cp);
00728               th=getvar("wims_theme");
00729               ti=getvar("wims_theme_icon");
00730            }
00731            if(strcmp(cp,"-theme-")==0 && strchr(th,'.')==NULL) {
00732               if(th==NULL || *th==0) th="default";
00733               nbuf=mkfname(NULL,"html/themes/%s/css.css",th);
00734            }
00735            if(readfile(nbuf,tmplbuf,sizeof(tmplbuf))) {
00736              if (st) {snprintf(buf,sizeof(buf),"<style type=\"text/css\"><!--\n\
00737 %s\n\
00738 --></style>",tmplbuf); }
00739                 else {
00740                 snprintf(buf,sizeof(buf),"<link href=\"%s\" rel=\"stylesheet\" type=\"text/css\">\n\
00741                 <link href=\"gifs/themes/%s/icon.css\" rel=\"stylesheet\" type=\"text/css\">",nbuf,ti);
00742               }
00743               setvar("wims_CSS",buf);
00744            }
00745            else {
00746               
00747               
00748            }
00749        }
00750     }
00751     if(readfile(html_header,buf,sizeof(buf))) setvar("wims_html_header",buf);
00752 }
00753 
00754 void setsesdir(char *d)
00755 {
00756     char buf[MAX_FNAME+1];
00757     if(strstr(d,parent_dir_string)!=NULL) {
00758        force_setvar("wims_session",robot_session);
00759        mystrncpy(buf,robot_session,sizeof(buf));
00760        mkfname(session_dir,"../tmp");
00761        mkfname(s2_dir,"../tmp");
00762     }
00763     else {
00764        force_setvar("wims_session",d);
00765        snprintf(buf,sizeof(buf),"%s/%s",SESSION_BASE,d);
00766        mkfname(session_dir,"../%s",SESSION_BASE);
00767        /* mkfname(s2_dir,"../%s",S2_BASE); */
00768     }
00769     force_setvar("wims_sesdir",buf);
00770 }
00771