Subversion Repositories wimsdev

Rev

Rev 10129 | Rev 11104 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 reyssat 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
 
8155 bpr 18
/* Web exerciser */
10 reyssat 19
 
8178 bpr 20
#include "wims.h"
21
 
8121 bpr 22
struct {
23
    char *name;
24
    char *font;
25
} charname[]={
26
    {"en","iso-8859-1"},
27
      {"fr","iso-8859-1"},
28
      {"es","iso-8859-1"},
29
      {"cn","gb2312"},
30
      {"de","iso-8859-1"},
31
      {"it","iso-8859-1"},
32
      {"nl","iso-8859-1"},
33
      {"si","iso-8859-2"},
34
      {"ar","iso-8859-6"},
35
      {"tw","big5"},
36
      {"pt","iso-8859-1"},
37
      {"ca","iso-8859-1"},
38
      {"pt","iso-8859-1"},
39
      {"ru","iso-8859-5"},
40
      {"ty","iso-8859-13"}
41
};
42
 
43
#define charname_no (sizeof(charname)/sizeof(charname[0]))
44
 
8155 bpr 45
/* left to right or right to left writing */
8121 bpr 46
struct {
47
    char *name;
48
    char *dirn;
49
} dirnname[]={
50
    {"en","ltr"},
51
      {"fr","ltr"},
52
      {"es","ltr"},
53
      {"cn","ltr"},
54
      {"de","ltr"},
55
      {"it","ltr"},
56
      {"nl","ltr"},
57
      {"si","ltr"},
58
      {"ar","rtl"},
59
      {"tw","ltr"},
60
      {"pt","ltr"},
61
      {"ca","ltr"},
62
      {"pt","ltr"},
63
      {"ru","ltr"},
64
      {"ty","ltr"}
65
 
66
};
67
 
68
#define dirnname_no (sizeof(dirnname)/sizeof(dirnname[0]))
69
 
70
 
10 reyssat 71
#define evalue strevalue
72
char *robot_session="../tmp/robot";
73
int robot_access=0,human_access=0;
74
int user_error_nolog=0;
75
char *good_agent[]={
76
      "Mozilla","Netscape","Opera","WIMS",
77
      "MSIE","Konqueror","Java"
78
};
79
#define good_agent_no (sizeof(good_agent)/sizeof(good_agent[0]))
80
 
81
char *bad_agent[]={ /* These are really bad agents: prohibited. */
7673 bpr 82
      "HTTrack","MemoWeb","Teleport","Offline","Wget","eCatch",
10 reyssat 83
      "Powermarks","EmailSiphon", "WebCopier"
84
};
85
#define bad_agent_no (sizeof(bad_agent)/sizeof(bad_agent[0]))
86
 
87
/* used for debugging */
88
int debug=0;
89
 
90
char class_dir[MAX_FNAME+1]; /* directory name of this class */
91
 
8185 bpr 92
struct user_variable user_variable[MAX_VAR_NUM];
10 reyssat 93
int user_var_no;
94
 
8185 bpr 95
struct VAR_DEF var_def[MAX_VAR_NUM];
10 reyssat 96
int defined_var_total;
97
 
7673 bpr 98
/* Destinated to module error messages */
10 reyssat 99
WORKING_FILE m_file,svar_file,mcache[MAX_MCACHE];
100
int mcachecnt;
101
 
7673 bpr 102
/* Limit for any data working files. */
10 reyssat 103
int WORKFILE_LIMIT=2048*1024;
104
 
7673 bpr 105
/* whether the user has defined language */
10 reyssat 106
int user_lang=0;
107
 
7673 bpr 108
/* for instex grouping */
10 reyssat 109
int instex_cnt=0, getwimstexsize=1;
110
char instex_src[MAX_LINELEN+1], instex_fname[MAX_LINELEN+1];
111
char *instex_processor="tex..gif";
112
 
7673 bpr 113
/* Cookie management */
10 reyssat 114
char cookiegot[256], cookieset[256], cookieheader[64]="WIMSUSER=";
115
 
7673 bpr 116
/* multipart boundary */
10 reyssat 117
char mpboundary[1024];
118
int  deplen=0; /* length of deposit */
119
 
120
int confset=0; /* set to 1 if setvar for config */
121
 
7673 bpr 122
/* Operating mode: default, popup, raw, etc. */
8185 bpr 123
 
10 reyssat 124
int mode=mode_default;
125
 
7673 bpr 126
/* Switch; notice subroutines wherether we are outputing. */
10 reyssat 127
int outputing;
128
 
129
char *home_module="home"; /* name of home module */
7673 bpr 130
extern char **environ;      /* table of environment variables */
10 reyssat 131
int module_defined=0;
132
 
133
        /* directory prefix buffers */
134
char session_prefix[MAX_FNAME+1], s2_prefix[MAX_FNAME+1], module_prefix[MAX_FNAME+1];
135
 
136
char *stdinbuf;
7673 bpr 137
int sesrandomtab[MAX_SESRANDOM]; /* session random values */
10 reyssat 138
char multiexec_random[64];
139
int executed_gotos; /* number of goto's executed. to check with GOTO_LIMIT. */
140
int insert_no; /* number of instex, insplot, insPLOT. */
141
int output_length; /* length of the output. */
7673 bpr 142
int isexam=0; /* non-zero if request is exam */
143
int simuxam=0; /* exam is simulation */
144
int is_multiexec=0; /* for execredirected */
10 reyssat 145
int multiexec_index;
146
int direct_datafile=0;
147
int exec_is_module=0;
148
  /* root directory of modules */
149
char *module_dir="modules";
150
int new_session=0; /* =1 if new session created */
7673 bpr 151
int good_httpd=0; /* Whether the http server is intelligent */
10 reyssat 152
/* int internal_sql=0; */ /* for internal sql use */
153
int direct_exec=0; /* calc routine is exected by exec if 1 */
154
int print_precision=8; /* precision when printing real numbers */
155
int session_serial; /* request serial for session control */
7673 bpr 156
int form_access=0; /* identifies form access, for robot identification */
157
int lastout_file=-1; /* file to hold last output */
158
char *instex_style=""; /* "": text "$": math "$$": displaymath */
159
int instex_usedynamic=0; /* always dynamic if 1 */
160
int wrapexec=0; /* if set to 1, change uid (nobody) to euid (wims).
161
                 * if set to -1, change euid to uid when exec(). */
162
int parm_restore=0; /* Restoring saved parameters? */
163
int exec_wait=1; /* whether to wait for fork return */
164
int execnt=0; /* count executions */
165
int readnest; /* nested read count */
166
int mfilecnt=0; /* count working files */
167
int forceresume=0; /* force user to resume old request */
168
int manageable=0; /* whether the connection may be site manager
169
                   * 0: no; 1: maybe; 2: sure */
170
int ismhelp=0;  /* 1 if session is in mhelp. */
171
int getvar_len; /* length of the last-got variable. */
172
int noout=0; /* if set to 1 then output is skipped */
173
char tmp_dir[MAX_FNAME+1]; /* temporary directory */
174
char *bin_dir="bin"; /* directory containing executable scripts and programs. */
175
char cwdbuf[MAX_FNAME+1]; /* store current working directory */
176
char var_hacking=0; /* Trying to hack a variable? */
10 reyssat 177
char *tmp_debug="no";
7673 bpr 178
char ins_alt[MAX_LINELEN+1]; /* dynamic insertion alternative text */
179
char *devel_modules="close"; /* whether to open devel modules */
180
int isclassmodule=0; /* 1 if the module is class module */
181
int isdevelmodule=0; /* development module? */
182
int setcookie=0; /* 1 if need to set cookie */
183
int killpid=0; /* pid of process to kill by alarm */
184
char *mathalign_sup1, *mathalign_sup2; /* see mathalign_base */
185
int substnest=0; /* nesting level of substit() */
10 reyssat 186
int exodepOK=1;
7673 bpr 187
long int startmtime; /* start time in milliseconds */
188
long int startmtime2; /* start time in microseconds */
189
int backslash_insmath=0; /* \(...) substitution? */
190
char examlogf[MAX_FNAME+1]; /* examlog file name */
191
char examlogd[MAX_FNAME+1]; /* examlog file name */
192
char exam_sheetexo[32]; /* sheet data of an exam */
10 reyssat 193
char loadavg[64];
7673 bpr 194
/* user file variable access control. */
10 reyssat 195
char *var_readable, *var_writable, *var_nr, *var_nw, *var_pfx;
196
int hostcquota;
7673 bpr 197
int var_noexport; /* do not export variable */
10 reyssat 198
 
7673 bpr 199
char tmplbuf[MAX_LINELEN+1]; /* for temporary uses not thru subroutines. */
10 reyssat 200
 
7673 bpr 201
struct tm *now, Now; /* time of request */
10 reyssat 202
time_t nowtime, limtime, limtimex;
203
char nowstr[32];
204
 
7673 bpr 205
/* Resource limits. Capital names are reserved by system. */
206
int rlimit_cpu=20;  /* cpu time in seconds */
10 reyssat 207
int rlimit_fsize=8388608;/* file size */
5222 guerimand 208
int rlimit_as=614457600;/* virtual memory size */
209
int rlimit_data=204857600;/* data segment size; maxima requires a lot (must be lower than rlimit_as)*/
10 reyssat 210
int rlimit_stack=2097152;/* stack size */
7673 bpr 211
int rlimit_core=0; /* core dump size */
212
int rlimit_rss=16777216; /* resident size */
213
int rlimit_nproc=1024; /* number of processes */
214
int rlimit_nofile=512; /* number of open files */
10 reyssat 215
int rlimit_memlock=2097152;/* locked-in-memory address space */
216
 
217
char *var_str; /* malloc'ed buffer to hold translated query_string */
218
 
7673 bpr 219
/* buffer to hold module's variable definition file, malloc'ed. */
10 reyssat 220
char *var_def_buf;
221
 
8185 bpr 222
/* job_identifier is even a reserved variable name */
10 reyssat 223
char job_identifier[32];
224
 
8185 bpr 225
/* site manager definition IPv4 IPv6*/
4648 bpr 226
char *manager_site="127.0.0.1 ::1";
10 reyssat 227
int   manager_https=0;
228
 
8185 bpr 229
/* sheet and exercise information */
10 reyssat 230
int wims_sheet=0,wims_exo=0;
231
 
8185 bpr 232
/* Form method: get or post */
10 reyssat 233
char *default_form_method="post";
234
 
8185 bpr 235
/* Je suis maintenant oblige de passer a l'anglais
236
 * pour la langue de defaut.
237
 */
10 reyssat 238
char lang[16]="en";
239
char available_lang[MAX_LANGUAGES][4]={"en","fr"};
240
int available_lang_no=2;
241
char pre_language[4]="";
10051 bpr 242
FILE *trace_file;
10 reyssat 243
char *protocol="http"; /* http or https */
244
 
8185 bpr 245
/* check for coordinate input. This will mean that
246
 * the request is manual, but not robot.
247
 */
10 reyssat 248
int coord_input=0;
249
 
8155 bpr 250
/* These are readonly environment variable names
251
 * special parm used for special cmds (getins, etc).
252
 */
10 reyssat 253
char *ro_name[]={
254
      "cmd" ,
255
      "empty",
256
      "lang" ,
257
      "module" ,
258
      "session" ,
259
      "special_parm",
260
      "special_parm2",
1970 guerimand 261
      "special_parm3",
1980 guerimand 262
      "special_parm4",
10 reyssat 263
      "useropts" ,
264
      "wims_session",
265
      "wims_subsession",
266
      "wims_window",
267
      "worksheet"
268
};
269
 
8185 bpr 270
int RO_NAME_NO=(sizeof(ro_name)/sizeof(ro_name[0]));
271
 
10 reyssat 272
int cmd_type;
273
char *commands[]={
274
    "intro" , "new" , "renew" , "reply" , "config" , "hint" , "help" ,
275
      "resume", "next", "getins", "getframe", "getfile", "close", "ref"
276
};
277
 
8185 bpr 278
int CMD_NO=(sizeof(commands)/sizeof(commands[0]));
279
 
8155 bpr 280
/* stat=0: saved variables
281
 * all names starting with wims_priv_ are also internal.
282
 */
8185 bpr 283
struct internal_name internal_name[]={
7673 bpr 284
      {"accessright", 1}, /* right to access commercial resources */
285
      {"caller", 1}, /* caller session */
286
      {"check", 1}, /* for exam check use */
287
      {"class", 1},
288
      {"class_examlog", 1},
289
      {"class_exolog", 1},
290
      {"class_limit", 1},
291
      {"class_quota", 1},
292
      {"class_regpass", 1},
293
      {"class_user_limit", 1},
294
      {"classdir", 1},
295
      {"classname", 1},
10 reyssat 296
      {"devel_modules", 1},
7673 bpr 297
      {"developer", 1},
298
      {"doc_quota", 1},
299
      {"doc_regpass", 1},
300
      {"email", 1},
301
      {"exo", 0}, /* exercise number */
302
      {"exoption", 1}, /* exercise option */
303
      {"firstname", 1},
304
      {"forum_limit", 1},
305
      {"home", 1},
306
      {"institutionname", 1},
307
      {"isexam", 0}, /* whether the sheet is an exam sheet */
308
      {"ismanager", 0},
309
      {"lastname", 1},
310
      {"mode", 0}, /* operating mode */
311
      {"module_start_time", 0},
312
      {"now", 1}, /* date and time, yyyymmdd.hh:mm:ss */
313
      {"nowseconds", 1}, /* date and time, seconds since EPOCH */
314
      {"nr", 1}, /* non-readable variables in user file, words */
315
      {"nw", 1}, /* non-writable variables in user file, words */
316
      {"otherclass", 1}, /* Remember other logins */
317
      {"participate", 1}, /* superclass definition */
318
      {"prefix", 1}, /* user file prefix */
319
      {"protocol", 0}, /* http protocol */
320
      {"rafale", 0}, /* rapidfire request information */
321
      {"readable", 1}, /* readable variables in user file, words */
322
      {"realuser", 1}, /* real user for supervisor in gateway */
323
      {"req_time", 0}, /* time of the request */
324
      {"sclassdir", 1},
325
      {"scorereg", 0}, /* score registration flag */
326
      {"sequence", 0}, /*sequence number */
327
      {"sescookie", 1}, /* session cookie */
328
      {"sesdir", 1},
329
      {"session_serial", 0}, /* request serial in the session */
330
      {"session_start_time", 0},
331
      {"sheet", 0}, /* sheet number */
332
      {"sup_secure", 1}, /* secure level of supervisor */
333
      {"superclass", 1}, /* superclass code */
334
      {"superclass_quota", 1},
335
      {"supertype", 1}, /* superclass type */
336
      {"supervise", 1}, /* superclass definition */
337
      {"supervisor", 1}, /* real name of the supervisor */
338
      {"supervisormail",1}, /* email of supervisor */
339
      {"trustfile", 1}, /* trusted files in special adm modules */
340
      {"useropts", 1}, /* user options */
341
      {"writable", 1}, /* writable variables in user file, words */
10 reyssat 342
};
8185 bpr 343
int INTERNAL_NAME_NO=(sizeof(internal_name)/sizeof(internal_name[0]));
10 reyssat 344
 
345
char *httpd_vars[]={
346
      "HTTP_ACCEPT",
347
      "HTTP_ACCEPT_CHARSET",
348
      "HTTP_ACCEPT_LANGUAGE",
349
      "HTTP_COOKIE",
350
      "HTTP_HOST",
351
      "HTTP_USER_AGENT",
352
      "HTTPS",
353
      "QUERY_STRING",
354
      "REMOTE_HOST",
355
      "REMOTE_ADDR",
356
      "REMOTE_PORT",
357
      "REQUEST_METHOD",
358
      "SCRIPT_NAME",
359
      "SERVER_NAME",
360
      "SERVER_SOFTWARE",
361
      "SERVER_PROTOCOL"
362
};
363
#define HTTPD_VAR_NO (sizeof(httpd_vars)/sizeof(httpd_vars[0]))
364
 
8155 bpr 365
/* security: these variables will not be visible to child processes */
10 reyssat 366
char *unsetvars[]={
7673 bpr 367
 "DOCUMENT_ROOT","SERVER_SIGNATURE","SERVER_SOFTWARE",
10 reyssat 368
      "UNIQUE_ID","HTTP_KEEP_ALIVE","SSL_SESSION_ID"
369
};
370
#define unsetvarcnt (sizeof(unsetvars)/sizeof(unsetvars[0]))
371
 
372
int httpd_type=httpd_apache;
373
 
7673 bpr 374
char *remote_addr=""; /* storing for performance */
10 reyssat 375
char *remote_host="";
376
 
377
char ref_name[2048], ref_base[2048];
378
 
379
void put_special_page(char *pname);
380
void useropts(void);
381
 
7673 bpr 382
/* Make certain httpd variables readable by modules */
10 reyssat 383
void take_httpd_vars(void)
384
{
385
    int i;
386
    char *p, buf[MAX_NAMELEN+1];
387
    var_noexport=1;
388
    for(i=0;i<HTTPD_VAR_NO;i++) {
7673 bpr 389
     snprintf(buf,sizeof(buf),"httpd_%s",httpd_vars[i]);
390
     if((p=getenv(httpd_vars[i]))!=NULL) setvar(buf,p);
10 reyssat 391
    }
392
    var_noexport=0;
7673 bpr 393
 
10 reyssat 394
    for(i=0;i<unsetvarcnt;i++) unsetenv(unsetvars[i]);
4648 bpr 395
     /* IPv4 IPv6*/
396
    p=getenv("REMOTE_ADDR");if(p!=NULL && (strcmp(p,"127.0.0.1")==0 || strcmp(p,"::1")==0)) human_access=1;
10 reyssat 397
    p=getenv("HTTP_REFERER"); if(p!=NULL && *p!=0) setvar("wims_referer",p);
398
}
399
 
400
void cookie2session(void)
401
{
402
    char cksession[64], psession[32], *ckey, *p;
403
    char nbuf[MAX_FNAME+1];
404
 
405
    if(mode==mode_popup) return;
6796 kbelabas 406
    if(cookiegot[0]==0) {
7673 bpr 407
     ckset: cookiegot[0]=0; setcookie=1; return;
10 reyssat 408
    }
409
    p=getvar("special_parm");
410
    if(p!=NULL && strcmp(p,"ignorecookie")==0) return;
411
    mystrncpy(cksession,cookiegot,sizeof(cksession));
412
    ckey=strchr(cksession,'-');
413
    if(ckey==NULL) goto ckset; else *ckey++=0;
414
    p=getvar("wims_session"); if(p==NULL) p="";
415
    if(strstr(p,"new")!=NULL) goto ckset;
416
    mystrncpy(psession,p,sizeof(psession));
417
    p=strchr(psession,'_'); if(p!=NULL) *p=0;
418
    if(psession[0]!=0) {
7673 bpr 419
      if(strcmp(psession,cksession)==0) return;
420
      if(session_exists(psession)) goto ckset;
421
      if(session_exists(cksession)) goto change;
10 reyssat 422
    }
423
    else {
7673 bpr 424
      if(!session_exists(cksession)) return;
425
      change:
426
      p=getenv("HTTPS");
427
      if(p!=NULL && strcasecmp(p,"on")==0) goto ckset;
428
      mkfname(nbuf,"%s/%s/var",session_dir,cksession);
429
      getdef(nbuf,"w_wims_ismanager",tmplbuf);
430
      if(tmplbuf[0]!=0 && tmplbuf[0]!='0') goto ckset;
431
      getdef(nbuf,"w_wims_protocol",tmplbuf);
432
      if(strcasecmp(tmplbuf,"https")==0) goto ckset;
433
      mkfname(nbuf,"%s/%s/var.stat",session_dir,cksession);
434
      getdef(nbuf,"wims_user",tmplbuf);
435
      if(tmplbuf[0]!=0) goto ckset;
436
      force_setvar(ro_name[ro_session],cksession);
437
      setsesdir(cksession);
438
      force_setvar("wims_subsession","");
439
      session_serial=0;
10 reyssat 440
    }
441
}
442
 
443
void determine_font(char *l)
444
{
445
    int i;
7673 bpr 446
 
10 reyssat 447
    if(l==NULL || *l==0) return;
448
    for(i=0;i<charname_no && memcmp(charname[i].name,l,2);i++);
449
    if(i<charname_no) setvar("wims_main_font",charname[i].font);
450
}
451
 
3278 reyssat 452
void determine_dirn(char *l)
453
{
454
    int i;
7673 bpr 455
 
3278 reyssat 456
    if(l==NULL || *l==0) return;
457
    for(i=0;i<dirnname_no && memcmp(dirnname[i].name,l,2);i++);
458
    if(i<dirnname_no) setvar("wims_main_dirn",dirnname[i].dirn);
459
}
460
 
10 reyssat 461
void predetermine_language(void)
462
{
463
    char *p;
464
    int i,n;
465
 
466
    if(pre_language[0]!=0) p=pre_language;
467
    else p=getenv("HTTP_ACCEPT_LANGUAGE");
468
    if(p!=NULL && strlen(p)>=2) {
469
      for(i=0;i<available_lang_no && memcmp(p,available_lang[i],2)!=0;i++);
470
      if(i<available_lang_no) goto lend;
471
    }
472
    p=getenv("HTTP_USER_AGENT");
473
    if(p!=NULL && strlen(p)>=5) {
7673 bpr 474
      char *q;
475
      if((q=strchr(p,'['))!=NULL && islower(*(q+1)) && islower(*(q+2)) && *(q+3)==']') {
476
          char bb[4];
477
          bb[0]=*(q+1);bb[1]=*(q+2);bb[2]=0;
478
          for(i=0;i<available_lang_no && memcmp(bb,available_lang[i],2)!=0;i++);
479
          if(i<available_lang_no) {
480
            memmove(lang,bb,2); lang[2]=0;
481
            goto lend2;
482
          }
483
      }
10 reyssat 484
    }
485
    p=getenv("HTTP_HOST"); if(p==NULL) goto lend2;
486
    n=strlen(p); if(n<=3 || *(p+n-3)!='.') goto lend2;
487
    p=p+n-2;
488
    for(i=0;i<available_lang_no && memcmp(p,available_lang[i],2)!=0;i++);
489
    if(i<available_lang_no) {
7673 bpr 490
      lend: memmove(lang,p,2); lang[2]=0;
491
      lend2: determine_font(lang);determine_dirn(lang);
10 reyssat 492
    }
493
}
494
 
7673 bpr 495
/* print a special page */
10 reyssat 496
void put_special_page(char *pname)
497
{
498
    determine_font(lang);
3278 reyssat 499
    determine_dirn(lang);
10 reyssat 500
    phtml_put_base(mkfname(NULL,"%s.phtml.%s",pname,lang),0);
501
    write_logs();free(var_str);
502
}
503
 
7673 bpr 504
/* check whether the connection is a site manager. */
10 reyssat 505
void manager_check(void)
506
{
507
    char *p, *pp, buf[16];
508
    struct stat confstat;
509
    int i;
7673 bpr 510
 
10 reyssat 511
    manageable=0;
512
    if(robot_access || *manager_site==0 || checkhost(manager_site)<1)
513
      goto mend;
514
    if(manager_https) {
7673 bpr 515
      p=getenv("HTTPS");
516
      if(p==NULL || strcmp(p,"on")!=0) goto mend;
10 reyssat 517
    }
8185 bpr 518
/* IPv4 IPv6*/
4648 bpr 519
    if(strcmp(remote_addr,"127.0.0.1")==0 || strcmp(remote_addr,"::1")==0) {
7673 bpr 520
      int port, port2;
521
      char tester[128];
522
      p=getenv("REMOTE_PORT"); if(p==NULL) goto mend;
523
      port=atoi(p); if(port<1024 || port>65535) goto mend;
524
      p=getenv("SERVER_PORT"); if(p==NULL) goto mend;
525
      port2=atoi(p); if(port2>=10000 || port2<=0) goto mend;
526
/* this is very non-portable */
527
      manageable=1;
528
      accessfile(tmplbuf,"r","/proc/net/tcp");
529
      snprintf(tester,sizeof(tester)," 0100007F:%04X 0100007F:%04X ",
530
             port,port2);
531
      p=strstr(tmplbuf,tester);
532
      if(p!=NULL) {
533
          pp=strchr(p,'\n'); if(pp!=NULL) *pp=0;
534
          if(strlen(p)>=75) {
535
            p=find_word_start(p+70); *find_word_end(p)=0;
536
            if(atoi(p)==geteuid()) manageable=2;
537
          }
538
      }
10 reyssat 539
    }
540
    else manageable=1;
541
    i=stat(config_file,&confstat);
542
    if(i==0 && manageable>0 && (confstat.st_mode&(S_IRWXO|S_IRWXG))!=0) manageable=-1;
543
    if(manageable>0 && !trusted_module()) manageable=0;
544
    if(manageable==1) {
7673 bpr 545
      accessfile(tmplbuf,"r","%s/.manager",session_prefix);
546
      if(strstr(tmplbuf,"yes")!=NULL) manageable=2;
10 reyssat 547
    }
548
    if(manageable==1) {
7673 bpr 549
      p=getvar(ro_name[ro_module]);
550
      if(p!=NULL && strncmp(p,"adm/manage",strlen("adm/manage"))==0) {
551
          struct stat pstat;
552
          if(stat("../log/.wimspass",&pstat)==0) {
553
            if((S_IFMT&pstat.st_mode)!=S_IFREG ||
554
               ((S_IRWXO|S_IRWXG)&pstat.st_mode)!=0)
555
              manageable=-2;
556
          }
557
      }
10 reyssat 558
    }
559
    mend:
560
    mystrncpy(buf,int2str(manageable),sizeof(buf));
561
    force_setvar("wims_ismanager",buf);
562
    if(manageable>=2) {
7673 bpr 563
      struct rlimit rlim;
564
      rlimit_cpu*=10;
565
      rlim.rlim_cur=rlim.rlim_max=rlimit_cpu;
566
      setrlimit(RLIMIT_CPU,&rlim);
567
      mystrncpy(buf,int2str(rlimit_cpu),sizeof(buf));
568
      setvar("wims_cpu_limit",buf);
569
      initalarm();
10 reyssat 570
    }
571
}
572
 
7673 bpr 573
/* check for robot access */
10 reyssat 574
void robot_check(void)
575
{
576
    char *ua, *p, *ses, *c, *mod;
577
    int i;
578
 
579
    if(human_access) return;
580
    mod=getvar(ro_name[ro_module]);
581
    if(mod!=NULL && strcmp(mod,"adm/raw")==0) return;
582
    ses=getvar(ro_name[ro_session]);
7673 bpr 583
/* user has valid session; OK */
10 reyssat 584
    if(ses!=NULL && strncmp(ses,robot_session,strlen(robot_session))!=0
585
       && strchr(ses,'/')==NULL
586
       && ftest(mkfname(NULL,"%s/%s",s2_dir,ses))==is_dir)
587
      return;
588
    ua=getenv("HTTP_USER_AGENT"); if(ua==NULL) ua="";
589
    ua=find_word_start(ua);
7673 bpr 590
    if(strncasecmp(ua,"Mozilla",strlen("Mozilla"))==0 &&
591
       (p=strstr(ua,"compatible"))!=NULL)
10 reyssat 592
      ua=find_word_start(find_word_end(p));
593
    if(*ua) {
7673 bpr 594
      for(i=0;i<good_agent_no
595
          && strncasecmp(ua,good_agent[i],strlen(good_agent[i]));i++);
596
      if(i<good_agent_no) return;
597
      for(i=0;i<bad_agent_no
598
          && strstr(ua,bad_agent[i])==NULL;i++);
599
      if(i<bad_agent_no) user_error("trapped");
10 reyssat 600
    }
601
    force_setvar(ro_name[ro_session],robot_session);
602
    setsesdir(robot_session);
603
    c=getvar(ro_name[ro_cmd]);
604
    robot_access=1;
605
    if(c!=NULL && strcmp(c,"new") && strcmp(c,"intro")) {
7673 bpr 606
      force_setvar(ro_name[ro_cmd],"robot_error");
607
      nph_header(450); put_special_page("robot");
608
      flushoutput(); flushlog(); exit(0);
10 reyssat 609
    }
610
}
611
 
7673 bpr 612
/* type=0: ordinary; type=1: multipart/form-data */
10 reyssat 613
void parse_query_string(int len, int type)
614
{
615
    int i,j,l,v,cmd_defined;
616
    int parenth=-1, ll, lb, dlen;
617
    char *start, *p, *p1, *pt, *b1="";
7673 bpr 618
 
10 reyssat 619
    cmd_defined=0;
620
    setvar("wims_subsession","");
621
    ll=lb=0;
622
    if(type) {
7673 bpr 623
      ll=strlen(mpboundary);
624
      start=strstr(var_str,mpboundary);
625
      if(start==NULL) start=var_str+strlen(var_str);
626
      if(strstr(var_str,"\r\n\r\n")!=NULL) b1="\r\n\r\n";
627
      else b1="\n\n";
628
      lb=strlen(b1);
10 reyssat 629
    }
630
    else start=var_str;
631
    for(v=0, p1=start;p1<var_str+len;p1+=l) {
7673 bpr 632
      if(type) {
633
          char *p2, *p3, *p4, *p5;
634
          p2=p1+ll; p3=memstr(p2,mpboundary,var_str+len-p2); l=p3-p1;
635
          p=memstr(p2,b1,var_str+len-p2); if(p>=p3) continue;
636
          p+=lb; if(p3<var_str+len) {
637
            while(*p3!='\n' && p3>p2) p3--; *p3=0;
638
            p3--; if(*p3=='\r') *p3=0;
639
          }
640
          dlen=p3-p;
641
          p2=memstr(p2,"name=",p3-p2); if(p2>=p3) continue;
642
          p2+=strlen("name="); if(*p2=='"') p2++;
643
          for(p3=p2; myisalnum(*p3) || strchr("._",*p3)!=NULL; p3++);
644
          if(p3==p2) continue;
645
          if(p3-p2==strlen("wims_deposit") &&
646
             strncmp(p2,"wims_deposit",p3-p2)==0) {
647
            p4=memstr(p1,"filename=",p-p1); if(p4<p) {
648
                p4+=strlen("filename="); if(*p4=='"') {
649
                  p4++; p5=strchr(p4,'"');
650
                  if(p5==NULL || p5-p4>=MAX_FNAME) goto emptyquote;
651
                }
652
                else {
653
                  emptyquote:
654
                  for(p5=p4; p5<p && !isspace(*p5) &&
655
                      strchr(";\"~#*?=,'",*p5)==NULL; p5++);
656
                }
657
                if(p5>p4) {
658
                  *p5=0;
659
                  for(p5--;
660
                      p5>=p4 && !isspace(*p5) && strchr("/\\:",*p5)==NULL;
661
                      p5--);
662
                  if(p5>=p4) p4=p5+1; if(*p4==0) goto noname;
663
                  if(strstr(p4,"..")!=NULL || *p4=='.')
664
                    p4="noname.file";
665
                  setvar("wims_deposit",p4);
666
                }
667
                else {
668
                  noname: setvar("wims_deposit","noname.file");
669
                }
670
            }
671
            deplen=dlen;
672
          }
673
          *p3=0; l-=p2-p1; p1=p2;
674
      }
675
      else {
676
          p1=find_word_start(p1);
677
          l=strlen(p1)+1; p=strchr(p1,'=');
678
          if(p==NULL) p=p1+strlen(p1);
679
          if(*p==0 && l>1) {
680
            user_variable[v].name="no_name";
681
            user_variable[v].value=p1;
682
            coord_input=1;
683
            goto nnext;
684
          }
685
          *p++=0;
686
      }
687
/* empty name or empty value: ignore */
10 reyssat 688
        if(*p1==0 || *p==0) continue;
7673 bpr 689
/* We do not treat names containing '.' */
690
      for(pt=strchr(p1,'.'); pt; pt=strchr(++pt,'.')) *pt='_';
691
/* Restrictions on variable names */
692
      for(pt=p1; myisalnum(*pt) || *pt=='_'; pt++);
693
      if(*pt) continue;
694
      if(strcmp(p1,"wims_deposit")!=0) _tolinux(p);
695
/* This is a restriction:
8155 bpr 696
 * Every parameter must have matching parentheses.
697
 */
7673 bpr 698
      if(parenth==-1 && strncmp(p1,"freepar_",strlen("freepar_"))!=0
699
         && strcmp(p1,"wims_deposit")!=0
700
         && check_parentheses(p,1)) parenth=v;
701
      if(strcmp(p1,"special_parm")==0 && strcmp(p,"wims")==0)
702
        human_access=1;
703
      j=search_list(ro_name,RO_NAME_NO,sizeof(ro_name[0]),p1);
704
      if(j>=0) {
705
          if(j==ro_session) {
706
            p=find_word_start(p); *find_word_end(p)=0;
707
            if(strlen(p)>MAX_SESSIONLEN) continue;
708
            if(strcmp(p,robot_session)==0) p="";
709
            if(strcasecmp(p,"popup")==0) {
710
                mode=mode_popup;
711
                force_setvar("wims_mode","popup");
712
                force_setvar("session","");
713
                continue;
714
            }
715
          }
716
          if(j==ro_module) module_defined=1;
717
          if(j==ro_cmd) {
718
            p=find_word_start(p); *find_word_end(p)=0;
719
            if(strlen(p)>16) continue;
720
            cmd_defined=1;
721
          }
722
          if(j==ro_lang) {
723
            if(strlen(p)!=2) continue;
724
            for(i=0;i<available_lang_no && strcmp(available_lang[i],p)!=0;i++);
725
            if(i<available_lang_no) {user_lang=1; ovlstrcpy(lang,p);}
726
            else continue;
727
          }
728
/* strip leading and trailing '/'s in module name */
729
          if(j==ro_module) {
730
            p=find_word_start(p); *find_word_end(p)=0;
731
            while(*p=='/') p++;
732
            while(*p!=0 && *(p+strlen(p)-1)=='/') *(p+strlen(p)-1)=0;
733
            if(strlen(p)>MAX_MODULELEN) continue;
734
          }
735
          setvar(p1,p);
736
          if(j==ro_session && mode!=mode_popup) {
737
            char *pp, *pr;
738
            char buf[1024];
739
            mystrncpy(buf,p,sizeof(buf));
740
            if((pp=strchr(buf,'.'))!=NULL) {
741
                *pp++=0; session_serial=atoi(pp);
742
                if(pp<0) pp=0;
743
            }
744
            else session_serial=0;
745
            pp=strchr(buf,'_');
746
            if(pp!=NULL && (pr=strstr(pp,"_mhelp"))!=NULL) {
747
                *pr=0; ismhelp=1; lastout_file=-1;
748
                setvar("wims_inssub","mh");
749
            }
750
            force_setvar("wims_session",buf);
751
            if(pp!=NULL) force_setvar("wims_subsession",pp);
752
          }
753
          continue;
754
      }
755
      user_variable[v].name=p1;
756
      user_variable[v].value=p;
757
nnext:v++; if(v>=MAX_VAR_NUM) user_error("too_many_variables");
10 reyssat 758
    }
759
    user_var_no=v;
760
    if(parenth>=0) {
7673 bpr 761
      char buf[16];
762
      mystrncpy(buf,int2str(user_var_no),sizeof(buf));
763
      setvar("user_var_no",buf);
764
      for(i=0;i<user_var_no;i++) {
765
          snprintf(buf,sizeof(buf),"name%d",i);
766
          setvar(buf,user_variable[i].name);
767
          snprintf(buf,sizeof(buf),"value%d",i);
768
          setvar(buf,user_variable[i].value);
769
      }
770
      mystrncpy(buf,int2str(parenth),sizeof(buf));
771
      setvar("bad_parentheses",buf);
772
      user_error("unmatched_parentheses");
10 reyssat 773
    }
7673 bpr 774
    p=getenv("SCRIPT_NAME");
10 reyssat 775
    if(p!=NULL && (p=strstr(p,"/getfile/"))!=NULL) {
7673 bpr 776
      p+=strlen("/getfile/");
777
      force_setvar(ro_name[ro_cmd],commands[cmd_getfile]);
778
      force_setvar("special_parm",p);
779
      cmd_defined=1;
10 reyssat 780
    }
781
    if(module_defined && !cmd_defined) setvar(ro_name[ro_cmd],commands[cmd_intro]);
782
    robot_check(); cookie2session();
783
}
784
 
7673 bpr 785
/* parse special commands */
10 reyssat 786
void special_cmds(void)
787
{
788
    char *c, *p;
789
    int i;
790
    long int l=-1;
7673 bpr 791
 
10 reyssat 792
    c=getvar(ro_name[ro_cmd]);
793
    if(c==NULL || *c==0) {  /* no module name nor command */
7673 bpr 794
      setvar(ro_name[ro_module],home_module);
795
      setvar(ro_name[ro_cmd],commands[cmd_new]);
796
      return;
10 reyssat 797
    }
798
    for(i=0;i<CMD_NO && strcmp(commands[i],c)!=0; i++);
799
    switch(i) {
7673 bpr 800
      case cmd_intro: {
801
          set_module_prefix();
802
          default_form_method="get";
803
          if(ftest(mkfname(NULL,"%s/%s",module_prefix,intro_file))<0) {
804
            force_setvar(ro_name[ro_cmd],commands[cmd_new]);
805
            return;
806
          }
807
          p=getvar("wims_session");
808
          if(p!=NULL && *p!=0) {
809
            if(set_session_prefix()==0) check_session();
810
            else {
811
                trap_check(p);
812
                if(strchr(p,'_')!=NULL && strchr(p,'/')==NULL) {
813
                  get_static_session_var();
814
                }
815
            }
816
          }
817
/* determine http protocol name. How to detect? */
818
          p=getenv("HTTPS"); if(p!=NULL && strcmp(p,"on")==0) {
819
            protocol="https"; set_protocol();
820
          }
821
          force_setvar("wims_protocol",protocol);
822
          determine_font(lang);
823
          determine_dirn(lang);
824
          main_phtml_put(intro_file); debug_output();
825
          introend: write_logs();free(var_str);
826
          delete_pid(); exit(0);
827
      }
828
      case cmd_ref: {
829
          set_module_prefix();
830
          default_form_method="get";
831
          p=getvar("wims_session");
832
          if(p!=NULL && *p!=0) {
833
            if(set_session_prefix()==0) check_session();
834
            else trap_check(p);
835
          }
836
          determine_font(lang);
837
          determine_dirn(lang);
838
          main_phtml_put(ref_file); goto introend;
839
      }
840
      case cmd_getins: {
841
          c=getvar(ro_name[ro_special_parm]);
842
          if(c==NULL || *c==0) {
843
            user_error_nolog=1; user_error("no_insnum");
844
          }
845
          if(*c=='/' || strstr(c,"..")!=NULL) goto badins;
846
          set_session_prefix();
847
          if(strstr(session_prefix,"robot")!=NULL) exit(0);
848
          l=filelength("%s/%s",s2_prefix,c);
849
          if(l<0) {
850
            badins: user_error_nolog=1; user_error("bad_insnum");
851
          }
852
          {
853
            char *fmt;
854
            fmt=strchr(c,'.');
855
            if(fmt==NULL) {
856
                user_error_nolog=1; user_error("bad_insnum");
857
            }
858
            else fmt++;
859
 
860
            nph_header(200);
10 reyssat 861
/* insert format problem; bricolage */
7673 bpr 862
            printf("Content-type: image/%s\r\n\
10 reyssat 863
Content-length: %ld\r\n\r\n",fmt,l);
7673 bpr 864
            catfile(stdout,"%s/%s",s2_prefix,c); exit(0);
865
          }
866
      }
867
      case cmd_getfile: {
868
          char fname[MAX_FNAME+1];
869
          c=getvar(ro_name[ro_special_parm]);
870
          if(c==NULL || *c==0) {
871
            user_error_nolog=1; user_error("no_insnum");
872
          }
873
          if(*c=='/' || strstr(c,"..")!=NULL) goto badfile;
874
 
875
          set_session_prefix();
876
          if(strstr(session_prefix,"robot")!=NULL) exit(0);
877
          mkfname(fname,"%s/getfile/%s",session_prefix,c);
878
          l=filelength("%s",fname);
879
          if(l<0 && strchr(session_prefix,'_')!=NULL) {
880
            char *pt;
881
            mystrncpy(fname,session_prefix,sizeof(fname));
882
            pt=strrchr(fname,'_'); if(pt) *pt=0;
883
            snprintf(fname+(pt-fname),sizeof(fname)-(pt-fname),
884
                  "/getfile/%s",c);
885
            l=filelength("%s",fname);
886
          }
887
          if(l<0) {
888
            badfile: user_error_nolog=1; user_error("bad_insnum");
889
          }
890
          if(l>512*1024) {
891
            struct rlimit rlim;
892
            rlimit_cpu*=l/(10*1024);
893
            rlim.rlim_cur=rlim.rlim_max=rlimit_cpu;
894
            initalarm();
895
          }
896
          {
897
            char *p1;
898
            char mime[MAX_LINELEN+1];
899
            for(p1=c+strlen(c);p1>c && isalpha(*(p1-1)); p1--);
900
            ovlstrcpy(mime,"application/octet-stream");
901
            if(p1>c && *(p1-1)=='.') {
902
                setvar("translator_unknown",mime);
903
                setvar("dictionary","bases/sys/mime");
904
                snprintf(mime,sizeof(mime),"translator %s",p1);
905
                calc_exec(mime);
906
            }
907
            nph_header(200);
908
            printf("Content-type: %s\r\n\
10 reyssat 909
Content-length: %ld\r\n\r\n",mime,l);
7673 bpr 910
            catfile(stdout,"%s",fname); exit(0);
911
          }
912
      }
913
      case cmd_close: {
914
          char *p, b2[32]; int w;
915
          char nbuf[MAX_FNAME+1], vbuf[MAX_LINELEN+1];
916
          p=getvar(ro_name[ro_session]);
917
          if(p==NULL || strlen(p)<10 ||
918
             strchr(p,'/')!=NULL) return;
919
          mystrncpy(b2,p,sizeof(b2));
920
          p=strchr(b2,'.'); if(p!=NULL) *p=0;
921
          mkfname(nbuf,"%s/%s/var.stat",session_dir,b2);
922
          getdef(nbuf,"wims_caller",vbuf);
923
          if(vbuf[0]!=0) force_setvar(ro_name[ro_session],vbuf);
924
          w=wrapexec; wrapexec=1;
925
          call_sh("rm -Rf %s/%s* %s/%s* >/dev/null 2>&1",session_dir,b2,s2_dir,b2);
926
          wrapexec=w; cookiegot[0]=0;
927
          force_setvar(ro_name[ro_cmd],"new");
928
      }
929
      default: return;
10 reyssat 930
    }
931
}
932
 
7673 bpr 933
/* This is run only when manually invoking the program.
8155 bpr 934
 * Verifies the orderedness of various list tables.
935
 */
10 reyssat 936
int verify_tables(void)
937
{
938
    if(verify_order(calc_routine,CALC_FN_NO,sizeof(calc_routine[0]))) return -1;
939
    if(verify_order(exec_routine,EXEC_FN_NO,sizeof(exec_routine[0]))) return -1;
940
    if(verify_order(main_config,MAIN_CONFIG_NO,sizeof(main_config[0]))) return -1;
941
    if(verify_order(mathname,mathname_no,sizeof(mathname[0]))) return -1;
942
    if(verify_order(hmname,hmname_no,sizeof(hmname[0]))) return -1;
943
    if(verify_order(ro_name,RO_NAME_NO,sizeof(ro_name[0]))) return -1;
944
    if(verify_order(distr_cmd,distr_cmd_no,sizeof(distr_cmd[0]))) return -1;
945
    if(verify_order(internal_name,INTERNAL_NAME_NO,
7673 bpr 946
                sizeof(internal_name[0]))) return -1;
10 reyssat 947
    if(verify_order(tmathfn,tmathfn_no,sizeof(tmathfn[0]))) return -1;
948
    if(verify_order(tmathvar,tmathvar_no,sizeof(tmathvar[0]))) return -1;
949
    if(verify_order(modindex,MODINDEX_NO,sizeof(modindex[0]))) return -1;
950
    if(verify_order(exportvars,exportvarcnt,sizeof(exportvars[0]))) return -1;
951
    if(evaltab_verify()) return -1;
952
    if(textab_verify()) return -1;
953
    return 0;
954
}
955
 
956
void config_defaults(void)
957
{
3857 kbelabas 958
    int i;
10 reyssat 959
    for(i=0;i<MAIN_CONFIG_NO;i++) {
7673 bpr 960
      if((1&main_config[i].is_integer)==1) {
961
          int *pi = (int*)main_config[i].address;
962
          printf("DF_%s=%d\n",main_config[i].name, *pi);
3857 kbelabas 963
        }
7673 bpr 964
      else {
3857 kbelabas 965
            char **ps = (char**)main_config[i].address;
7673 bpr 966
          printf("DF_%s=%s\n",main_config[i].name,*ps);
3857 kbelabas 967
        }
10 reyssat 968
    }
969
}
970
 
7673 bpr 971
/* get and set useroptions */
10 reyssat 972
void useropts(void)
973
{
974
    char *p;
975
    setvar("lang",lang);
976
    p=getvar("useropts");
977
    if(p==NULL || *p==0) p=getvar("wims_useropts");
978
    if(p!=NULL && *p!=0) {
6178 bpr 979
      if(myisdigit(p[0])) {
980
        usertexsize=p[0]-'0';
6184 bpr 981
        /* fourth digit is for special fonts*/
6178 bpr 982
        if(p[1]!=0) { mathalign_base=p[1]-'0'; }
983
      }
6184 bpr 984
      if(myisdigit(p[3]) && p[3]!=0){ spec_font=p[3]-'0';}
985
    }
986
    if(mathalign_base==1) {
6178 bpr 987
        mathalign_sup1="<sup>"; mathalign_sup2="</sup>";
6184 bpr 988
    } else mathalign_sup1=mathalign_sup2="";
10 reyssat 989
}
990
 
7673 bpr 991
/* popup module help */
10 reyssat 992
void mhelp(void)
993
{
994
    char *p, buf[MAX_LINELEN+1];
995
    main_phtml_put(""); buf[0]=0;
996
    if(cmd_type!=cmd_help) {
7673 bpr 997
      phtml_put_base("closemhelp.phtml",0);
10 reyssat 998
    }
999
    else {
7673 bpr 1000
      phtml_put_base("mhelpheader.phtml",0);
1001
      p=getvar("special_parm");
1002
      if(p!=NULL && strcmp(p,"about")==0)
1003
        phtml_put("about.phtml",0);
1004
      else phtml_put("help.phtml",0); phtml_put_base("mhelptail.phtml",0);
1005
      exec_tail(buf);
10 reyssat 1006
    }
1007
}
1008
 
1009
#define READSTDIN_WINDOW 4096
1010
 
1011
void readstdin(int len)
1012
{
1013
    int ll, l1, lt, lr;
1014
    int cpulim;
7673 bpr 1015
 
10 reyssat 1016
    cpulim=rlimit_cpu; rlimit_cpu=3;
1017
    lr=len; l1=0;
1018
    while(lr>0) {
7673 bpr 1019
      nowtime=time(0); initalarm();
1020
      ll=lr; if(ll>READSTDIN_WINDOW) ll=READSTDIN_WINDOW;
1021
      lt=fread(stdinbuf+l1,1,ll,stdin);
1022
      if(lt!=ll) user_error("parm_too_long");
1023
      lr-=ll; l1+=ll;
10 reyssat 1024
    }
1025
    if(l1!=len) user_error("parm_too_long");
1026
    stdinbuf[len]=0; rlimit_cpu=cpulim;
1027
}
1028
 
7673 bpr 1029
/* input: p=QUERY_STRING. output: parameter length. */
1030
/* Netscape puts form content into /tmp. */
10 reyssat 1031
int formdata(char *p)
1032
{
1033
    char *pp;
1034
    int inlen;
1035
    char *ctype;
1036
    inlen=0; ctype=getenv("CONTENT_TYPE");
1037
    if(ctype==NULL || strstr(ctype,"multipart/form-data")==NULL
1038
       || (p=strstr(ctype,"boundary="))==NULL) {
7673 bpr 1039
      bad: stdinbuf=""; return 0;
10 reyssat 1040
    }
1041
    pp=getenv("CONTENT_LENGTH");
1042
    if(pp==NULL) goto bad;
1043
    inlen=atoi(pp); if(inlen<=10) goto bad;
1044
    if(inlen>=MAX_DEPOSITLEN) user_error("parm_too_long");
1045
    stdinbuf=xmalloc(inlen+1); readstdin(inlen);
1046
    p+=strlen("boundary=");
1047
    for(pp=p;myisalnum(*pp) || *pp=='-'; pp++);
1048
    if(pp-p<sizeof(mpboundary)-2) {
7673 bpr 1049
      memmove(mpboundary,p,pp-p); mpboundary[pp-p]=0;
10 reyssat 1050
    }
7673 bpr 1051
/* empty data */
10 reyssat 1052
    if(strstr(stdinbuf,mpboundary)==NULL || strstr(stdinbuf,"name=")==NULL) {
7673 bpr 1053
      free(stdinbuf); goto bad;
10 reyssat 1054
    }
1055
    form_access=1; post_log();
1056
    return inlen;
1057
}
1058
 
7673 bpr 1059
/* get the content of POST */
10 reyssat 1060
void getpost(void)
1061
{
1062
    int ll;
1063
    char *pp;
1064
    pp=getenv("CONTENT_LENGTH");
1065
    if(pp==NULL || (ll=atoi(pp))<=0) {
7673 bpr 1066
      stdinbuf=xmalloc(16); stdinbuf[0]=0;
10 reyssat 1067
    }
1068
    else {
7673 bpr 1069
      if(ll>QUERY_STRING_LIMIT) user_error("parm_too_long");
1070
      stdinbuf=xmalloc(ll+16); readstdin(ll);
1071
      if(ll>0) {
1072
          setenv("QUERY_STRING",stdinbuf,1);
1073
          form_access=1; post_log();
1074
      }
10 reyssat 1075
    }
1076
}
1077
 
1078
void buffer_init(void)
1079
{
1080
    struct timeval tv;
7673 bpr 1081
 
10 reyssat 1082
    mcachecnt=readnest=0;
1083
    mpboundary[0]=cookiegot[0]=cookieset[0]=cwdbuf[0]=0;
1084
    rscore_class[0]=rscore_user[0]=multiexec_random[0]=0;
1085
    lastftest[0]=0;
1086
    lastdatafile[0]=0; lastdata=xmalloc(WORKFILE_LIMIT);
1087
    outptr=outbuf;
1088
    instex_src[0]=instex_fname[0]=module_prefix[0]=0;
1089
    examlogf[0]=examlogd[0]=exam_sheetexo[0]=0;
1090
    stdinbuf=NULL;
1091
    mkfname(tmp_dir,"../tmp/forall");
1092
    mkfname(session_dir,"../%s",SESSION_BASE);
1093
    mkfname(s2_dir,"../%s",S2_BASE);
1094
    if(gettimeofday(&tv,NULL)) startmtime=startmtime2=0;
1095
    else {
7673 bpr 1096
      startmtime=((tv.tv_sec%10000)*1000+tv.tv_usec/1000);
1097
      startmtime2=(tv.tv_sec%1000)*1000000+tv.tv_usec;
10 reyssat 1098
    }
1099
}
1100
 
8216 bpr 1101
/**
1102
 * runs the test suite, to check whether the documented commands of modtool
1103
 * behave properly.
1104
 * @param prefix path to the proc file to process
1105
 * @param p      name of the proc file to proceed
1106
 * @param vars a list of var names to print.
1107
 **/
1108
void test_suite(char *prefix, char *p, char *vars){
1109
  char *v, *nextv;
1110
  mystrncpy(module_prefix, prefix, sizeof(module_prefix));
1111
  exec_read(p);
1112
  nextv=vars;
1113
  while (*nextv!='\0'){
1114
    v=find_word_start(nextv);
1115
    nextv=find_word_end(v);
1116
    if (*nextv != '\0'){
1117
      *nextv++='\0';
1118
    }
8925 bpr 1119
    v=getvar(v);
1120
    if(v) puts(v); else putchar('\n');
8216 bpr 1121
  }
1122
}
1123
 
10 reyssat 1124
int main(int argc, char *argv[], char *envp[])
1125
{
1126
    char *p, homebuf[MAX_FNAME+1], lbuf[32];
1127
    int inlen=0;
8155 bpr 1128
/*    int mfd; */
8195 bpr 1129
 
10 reyssat 1130
    class_dir[0]=0;
1131
    substitute=substit; buffer_init(); var_init();
7673 bpr 1132
/* WIMS internal locale is always C. */
10 reyssat 1133
    setenv("LANG","C",1); umask(022);
1134
    setenv("LANGUAGE","us",1);
1135
    setenv("LC_ALL","C",1);
1136
    if(argc>1) {
7673 bpr 1137
      if(strcasecmp(argv[1],"table")==0) {
1138
          if(verify_tables()) internal_error("Table disorder.");
1139
          else printf("Table orders OK.\n");
1140
          return 0;
1141
      }
1142
      if(strcasecmp(argv[1],"version")==0) {
1143
          printf("%s",wims_version); return 0;
1144
      }
1145
      if(strcasecmp(argv[1],"defaults")==0) {
1146
          config_defaults(); return 0;
1147
      }
8216 bpr 1148
      if(strcasecmp(argv[1],"test")==0) {
8851 bpr 1149
/* launches a test suite */
1150
         if (argc < 5) {
1151
           printf("Not enough arguments to launch a test; usage:\n");
1152
           printf("wims test path_to_file name_of_proc_file 'var1 var2 ...'\n\n");
1153
           printf("the file at path_to_file/name_of_proc_file will be evaluated by wims for\nexec commands, then the values of var1, var2, ... will be printed\nin the standard output, one per line.\n");
1154
           return 1;
1155
         }
1156
         test_suite(argv[2], argv[3], argv[4]);
1157
         return 0;
1158
       }
10 reyssat 1159
    }
1160
    p=getenv("SERVER_SOFTWARE"); if(p!=NULL && strcasecmp(p,"WIMS")==0)
1161
      httpd_type=httpd_wims;
1162
    p=getenv("REMOTE_ADDR"); if(p!=NULL) remote_addr=p;
1163
    p=getenv("REMOTE_HOST"); if(p!=NULL) remote_host=p;
1164
    nowtime=time(0); now=localtime(&nowtime);
9286 bpr 1165
/* nowtime contains the seconds elapsed from EPOCH (1970)
1166
   but tm_year is set to current year-1900.
1167
 */
10 reyssat 1168
    memmove(&Now, now, sizeof(Now)); now=&Now;
1169
    snprintf(nowstr,sizeof(nowstr),"%04d%02d%02d.%02d:%02d:%02d",
7673 bpr 1170
           (now->tm_year)+1900,(now->tm_mon)+1,now->tm_mday,
1171
           now->tm_hour,now->tm_min,now->tm_sec);
10 reyssat 1172
    p=getenv("QUERY_STRING");
1173
    if(p==NULL || *p==0) getpost();
1174
    else if(strncmp(p,"form-data",9)==0) inlen=formdata(p);
7673 bpr 1175
 
10 reyssat 1176
    force_setvar("wims_now",nowstr);
1177
    snprintf(lbuf,sizeof(lbuf),"%lu",nowtime);
1178
    force_setvar("wims_nowseconds",lbuf);
1179
    nowtime=time(0);
1180
    initalarm();
7673 bpr 1181
 
10 reyssat 1182
    executed_gotos=insert_no=output_length=0; ins_alt[0]=0;
7673 bpr 1183
    setvar("empty","");       /* lock this variable */
10 reyssat 1184
    setvar("wims_version",wims_version);
1185
    setvar("wims_version_date",wims_version_date);
1186
    setvar("wims_main_font","utf-8");
1187
    take_httpd_vars();
1188
 
1189
    main_configure();
1190
    checklogd();
8155 bpr 1191
/* mfd=shm_open(SHM_NAME,O_RDONLY,-1);
1192
 * if(mfd==-1) internal_error("Unable to find shared memory.");
1193
 * shmptr=mmap(0,SHM_SIZE,PROT_READ,MAP_SHARED,mfd,0);
1194
 * if(shmptr==MAP_FAILED) internal_error("Shared memory failure.");
1195
 */
7673 bpr 1196
    getppid(); /* this is the first sysmask trigger, must be after checklogd() */
10 reyssat 1197
    predetermine_language();
6784 kbelabas 1198
    /* modify a few rlimits for 64-bit processors */
1199
    if (sizeof(long) == 8) {
1200
      rlimit_as*=2;    /* virtual memory size */
1201
      rlimit_data*=2;  /* data segment size; maxima requires a lot */
1202
      rlimit_stack*=2; /* stack size */
1203
    }
10 reyssat 1204
    set_rlimits();
1205
    init_random();
1206
    module_configure();
1207
    set_job_ident();
1208
    m_file.name[0]=0;m_file.linecnt=m_file.linepointer=0;
1209
    p=getenv("QUERY_STRING");
1210
    if(p==NULL || strlen(p)==0) {
7673 bpr 1211
      setvar("lang",lang);
1212
      snprintf(homebuf,sizeof(homebuf),"module=%s",home_module);
1213
      p=homebuf;
10 reyssat 1214
    }
1215
    if(strlen(p)>=QUERY_STRING_LIMIT) user_error("parm_too_long");
1216
    if(mpboundary[0]==0) {
7673 bpr 1217
      var_str=xmalloc(strlen(p)+2);
1218
      parse_query_string(http2env(var_str,p),0);
10 reyssat 1219
    }
1220
    else {
7673 bpr 1221
      var_str=stdinbuf;
1222
      parse_query_string(inlen,1);
10 reyssat 1223
    }
1224
    if(ismhelp) {
7673 bpr 1225
      p=getvar(ro_name[ro_cmd]);
1226
      if(p==NULL || (strcmp(p,"help")!=0 && strcmp(p,"getins")!=0)) {
1227
          mhelp(); goto outgo;
1228
      }
10 reyssat 1229
    }
1230
    check_exam();
1231
    useropts();
1232
    special_cmds();
1233
    parse_ro_names();
1234
    manager_check();
1235
    access_check(0);
10129 bpr 1236
/* this gives some bug on some servers... so desactivated
1237
  it is really for developing in wims, so no use in general */
1238
   if (1==0) {
10051 bpr 1239
    if(strstr(tmp_debug,"yes")!=NULL && checkhost(manager_site)>=1)
1240
       trace_file = fopen(mkfname(NULL,"%s/%s",tmp_dir,"trace.txt"),"a");
10129 bpr 1241
   }
10 reyssat 1242
    set_variables();
1243
    determine_font(getvar("module_language"));
3278 reyssat 1244
    determine_dirn(getvar("module_language"));
10 reyssat 1245
    if(!robot_access && session_prefix[0]!=0 && cmd_type!=cmd_help && !ismhelp)
1246
      lastout_file=creat(mkfname(NULL,"%s/%s",s2_prefix,lastout),
7673 bpr 1247
                   S_IRUSR|S_IWUSR);
10 reyssat 1248
    p=getvar("module_category");
1249
    if(p==NULL || strstr(p,"tool")==NULL) default_form_method="get";
1250
    if(noout) {
7673 bpr 1251
      write_logs(); save_session_vars();
1252
      goto outgo;
10 reyssat 1253
    }
1254
    if(ismhelp) {
7673 bpr 1255
      mhelp();
10 reyssat 1256
    }
1257
    else {
7673 bpr 1258
      main_phtml_put(html_file);
1259
      if(lastout_file!=-1) {
1260
          flushoutput(); close(lastout_file); putlastout();
1261
      }
1262
      write_logs(); save_session_vars();
10 reyssat 1263
    }
1264
    outgo:
1265
    debug_output();
10291 bpr 1266
    if (trace_file)  { fclose(trace_file); trace_file=NULL; }
10 reyssat 1267
    if(var_str!=stdinbuf) free(var_str);
1268
    delete_pid();
1269
    if(mode!=mode_popup && trusted_module()) {
7673 bpr 1270
      p=getvar("wims_mode");
1271
      if(p!=NULL && strcmp(p,"popup")==0) mode=mode_popup;
10 reyssat 1272
    }
1273
    if(mode==mode_popup && insert_no==0) {
7673 bpr 1274
      p=getvar("wims_mode");
1275
      if(p!=NULL && strcmp(p,"popup")==0) {
1276
          remove_tree(session_prefix);
1277
 
1278
          remove_tree(s2_prefix);
1279
      }
10 reyssat 1280
    }
1281
    return 0;
1282
}