Subversion Repositories wimsdev

Rev

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