Subversion Repositories wimsdev

Rev

Rev 12011 | Rev 12271 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 12011 Rev 12263
Line 18... Line 18...
18
/* Web exerciser */
18
/* Web exerciser */
19
 
19
 
20
#include "wims.h"
20
#include "wims.h"
21
 
21
 
22
struct {
22
struct {
23
    char *name;
23
  char *name;
24
    char *font;
24
  char *font;
25
} charname[]={
25
} charname[]={
26
      {"en","windows-1252"},
26
  {"en","windows-1252"},
27
      {"fr","windows-1252"},
27
  {"fr","windows-1252"},
28
      {"es","windows-1252"},
28
  {"es","windows-1252"},
29
      {"cn","gb2312"},
29
  {"cn","gb2312"},
30
      {"de","windows-1252"},
30
  {"de","windows-1252"},
31
      {"it","windows-1252"},
31
  {"it","windows-1252"},
32
      {"nl","windows-1252"},
32
  {"nl","windows-1252"},
33
      {"si","iso-8859-2"},
33
  {"si","iso-8859-2"},
34
      {"ar","iso-8859-6"},
34
  {"ar","iso-8859-6"},
35
      {"tw","big5"},
35
  {"tw","big5"},
36
      {"pt","windows-1252"},
36
  {"pt","windows-1252"},
37
      {"ca","windows-1252"},
37
  {"ca","windows-1252"},
38
      {"pt","windows-1252"},
38
  {"pt","windows-1252"},
39
      {"ru","iso-8859-5"},
39
  {"ru","iso-8859-5"},
40
      {"ty","iso-8859-13"}
40
  {"ty","iso-8859-13"}
41
};
41
};
42
 
42
 
43
#define charname_no (sizeof(charname)/sizeof(charname[0]))
43
#define charname_no (sizeof(charname)/sizeof(charname[0]))
44
 
44
 
45
/* left to right or right to left writing */
45
/* left to right or right to left writing */
46
struct {
46
struct {
47
    char *name;
47
  char *name;
48
    char *dirn;
48
  char *dirn;
49
} dirnname[]={
49
} dirnname[]={
50
      {"en","ltr"},
50
  {"en","ltr"},
51
      {"fr","ltr"},
51
  {"fr","ltr"},
52
      {"es","ltr"},
52
  {"es","ltr"},
53
      {"cn","ltr"},
53
  {"cn","ltr"},
54
      {"de","ltr"},
54
  {"de","ltr"},
55
      {"it","ltr"},
55
  {"it","ltr"},
56
      {"nl","ltr"},
56
  {"nl","ltr"},
57
      {"si","ltr"},
57
  {"si","ltr"},
58
      {"ar","rtl"},
58
  {"ar","rtl"},
59
      {"tw","ltr"},
59
  {"tw","ltr"},
60
      {"pt","ltr"},
60
  {"pt","ltr"},
61
      {"ca","ltr"},
61
  {"ca","ltr"},
62
      {"pt","ltr"},
62
  {"pt","ltr"},
63
      {"ru","ltr"},
63
  {"ru","ltr"},
64
      {"ty","ltr"}
64
  {"ty","ltr"}
65
 
65
 
66
};
66
};
67
 
67
 
68
#define dirnname_no (sizeof(dirnname)/sizeof(dirnname[0]))
68
#define dirnname_no (sizeof(dirnname)/sizeof(dirnname[0]))
69
 
69
 
Line 71... Line 71...
71
#define evalue strevalue
71
#define evalue strevalue
72
char *robot_session="../tmp/robot";
72
char *robot_session="../tmp/robot";
73
int robot_access=0,human_access=0;
73
int robot_access=0,human_access=0;
74
int user_error_nolog=0;
74
int user_error_nolog=0;
75
char *good_agent[]={
75
char *good_agent[]={
76
      "Mozilla","Netscape","Opera","WIMS",
76
  "Mozilla","Netscape","Opera","WIMS",
77
      "MSIE","Konqueror","Java"
77
  "MSIE","Konqueror","Java"
78
};
78
};
79
#define good_agent_no (sizeof(good_agent)/sizeof(good_agent[0]))
79
#define good_agent_no (sizeof(good_agent)/sizeof(good_agent[0]))
80
 
80
 
81
char *bad_agent[]={ /* These are really bad agents: prohibited. */
81
char *bad_agent[]={ /* These are really bad agents: prohibited. */
82
      "HTTrack","MemoWeb","Teleport","Offline","Wget","eCatch",
82
  "HTTrack","MemoWeb","Teleport","Offline","Wget","eCatch",
83
      "Powermarks","EmailSiphon", "WebCopier"
83
  "Powermarks","EmailSiphon", "WebCopier"
84
};
84
};
85
#define bad_agent_no (sizeof(bad_agent)/sizeof(bad_agent[0]))
85
#define bad_agent_no (sizeof(bad_agent)/sizeof(bad_agent[0]))
86
 
86
 
87
/* used for debugging */
87
/* used for debugging */
88
int debug=0;
88
int debug=0;
Line 251... Line 251...
251
 
251
 
252
/* These are readonly environment variable names
252
/* These are readonly environment variable names
253
 * special parm used for special cmds (getins, etc).
253
 * special parm used for special cmds (getins, etc).
254
 */
254
 */
255
char *ro_name[]={
255
char *ro_name[]={
256
      "cmd" ,
256
  "cmd" ,
257
      "empty",
257
  "empty",
258
      "lang" ,
258
  "lang" ,
259
      "module" ,
259
  "module" ,
260
      "session" ,
260
  "session" ,
261
      "special_parm",
261
  "special_parm",
262
      "special_parm2",
262
  "special_parm2",
263
      "special_parm3",
263
  "special_parm3",
264
      "special_parm4",
264
  "special_parm4",
265
      "useropts" ,
265
  "useropts" ,
266
      "wims_session",
266
  "wims_session",
267
      "wims_subsession",
267
  "wims_subsession",
268
      "wims_window",
268
  "wims_window",
269
      "worksheet"
269
  "worksheet"
270
};
270
};
271
 
271
 
272
int RO_NAME_NO=(sizeof(ro_name)/sizeof(ro_name[0]));
272
int RO_NAME_NO=(sizeof(ro_name)/sizeof(ro_name[0]));
273
 
273
 
274
int cmd_type;
274
int cmd_type;
Line 281... Line 281...
281
 
281
 
282
/* stat=0: saved variables
282
/* stat=0: saved variables
283
 * all names starting with wims_priv_ are also internal.
283
 * all names starting with wims_priv_ are also internal.
284
 */
284
 */
285
struct internal_name internal_name[]={
285
struct internal_name internal_name[]={
286
      {"accessright", 1}, /* right to access commercial resources */
286
  {"accessright", 1}, /* right to access commercial resources */
287
      {"caller", 1}, /* caller session */
287
  {"caller", 1}, /* caller session */
288
      {"check", 1}, /* for exam check use */
288
  {"check", 1}, /* for exam check use */
289
      {"class", 1},
289
  {"class", 1},
290
      {"class_examlog", 1},
290
  {"class_examlog", 1},
291
      {"class_exolog", 1},
291
  {"class_exolog", 1},
292
      {"class_limit", 1},
292
  {"class_limit", 1},
293
      {"class_quota", 1},
293
  {"class_quota", 1},
294
      {"class_regpass", 1},
294
  {"class_regpass", 1},
295
      {"class_user_limit", 1},
295
  {"class_user_limit", 1},
296
      {"classdir", 1},
296
  {"classdir", 1},
297
      {"classname", 1},
297
  {"classname", 1},
298
      {"devel_modules", 1},
298
  {"devel_modules", 1},
299
      {"developer", 1},
299
  {"developer", 1},
300
      {"doc_quota", 1},
300
  {"doc_quota", 1},
301
      {"doc_regpass", 1},
301
  {"doc_regpass", 1},
302
      {"email", 1},
302
  {"email", 1},
303
      {"exo", 0}, /* exercise number */
303
  {"exo", 0}, /* exercise number */
304
      {"exoption", 1}, /* exercise option */
304
  {"exoption", 1}, /* exercise option */
305
      {"firstname", 1},
305
  {"firstname", 1},
306
      {"forum_limit", 1},
306
  {"forum_limit", 1},
307
      {"home", 1},
307
  {"home", 1},
308
      {"institutionname", 1},
308
  {"institutionname", 1},
309
      {"isexam", 0}, /* whether the sheet is an exam sheet */
309
  {"isexam", 0}, /* whether the sheet is an exam sheet */
310
      {"ismanager", 0},
310
  {"ismanager", 0},
311
      {"lastname", 1},
311
  {"lastname", 1},
312
      {"mode", 0}, /* operating mode */
312
  {"mode", 0}, /* operating mode */
313
      {"module_start_time", 0},
313
  {"module_start_time", 0},
314
      {"now", 1}, /* date and time, yyyymmdd.hh:mm:ss */
314
  {"now", 1}, /* date and time, yyyymmdd.hh:mm:ss */
315
      {"nowseconds", 1}, /* date and time, seconds since EPOCH */
315
  {"nowseconds", 1}, /* date and time, seconds since EPOCH */
316
      {"nr", 1}, /* non-readable variables in user file, words */
316
  {"nr", 1}, /* non-readable variables in user file, words */
317
      {"nw", 1}, /* non-writable variables in user file, words */
317
  {"nw", 1}, /* non-writable variables in user file, words */
318
      {"otherclass", 1}, /* Remember other logins */
318
  {"otherclass", 1}, /* Remember other logins */
319
      {"participate", 1}, /* superclass definition */
319
  {"participate", 1}, /* superclass definition */
320
      {"prefix", 1}, /* user file prefix */
320
  {"prefix", 1}, /* user file prefix */
321
      {"protocol", 0}, /* http protocol */
321
  {"protocol", 0}, /* http protocol */
322
      {"rafale", 0}, /* rapidfire request information */
322
  {"rafale", 0}, /* rapidfire request information */
323
      {"readable", 1}, /* readable variables in user file, words */
323
  {"readable", 1}, /* readable variables in user file, words */
324
      {"realuser", 1}, /* real user for supervisor in gateway */
324
  {"realuser", 1}, /* real user for supervisor in gateway */
325
      {"req_time", 0}, /* time of the request */
325
  {"req_time", 0}, /* time of the request */
326
      {"sclassdir", 1},
326
  {"sclassdir", 1},
327
      {"scorereg", 0}, /* score registration flag */
327
  {"scorereg", 0}, /* score registration flag */
328
      {"seed", 0},
328
  {"seed", 0},
329
      {"seed_repeat",0},
329
  {"seed_repeat",0},
330
      {"seed_score", 0},
330
  {"seed_score", 0},
331
      {"sequence", 0}, /* sequence number */
331
  {"sequence", 0}, /* sequence number */
332
      {"sescookie", 1}, /* session cookie */
332
  {"sescookie", 1}, /* session cookie */
333
      {"sesdir", 1},
333
  {"sesdir", 1},
334
      {"session_serial", 0}, /* request serial in the session */
334
  {"session_serial", 0}, /* request serial in the session */
335
      {"session_start_time", 0},
335
  {"session_start_time", 0},
336
      {"sheet", 0}, /* sheet number */
336
  {"sheet", 0}, /* sheet number */
337
      {"sup_secure", 1}, /* secure level of supervisor */
337
  {"sup_secure", 1}, /* secure level of supervisor */
338
      {"superclass", 1}, /* superclass code */
338
  {"superclass", 1}, /* superclass code */
339
      {"superclass_quota", 1},
339
  {"superclass_quota", 1},
340
      {"supertype", 1}, /* superclass type */
340
  {"supertype", 1}, /* superclass type */
341
      {"supervise", 1}, /* superclass definition */
341
  {"supervise", 1}, /* superclass definition */
342
      {"supervisor", 1}, /* real name of the supervisor */
342
  {"supervisor", 1}, /* real name of the supervisor */
343
      {"supervisormail",1}, /* email of supervisor */
343
  {"supervisormail",1}, /* email of supervisor */
344
      {"trustfile", 1}, /* trusted files in special adm modules */
344
  {"trustfile", 1}, /* trusted files in special adm modules */
345
      {"useropts", 1}, /* user options */
345
  {"useropts", 1}, /* user options */
346
      {"writable", 1}, /* writable variables in user file, words */
346
  {"writable", 1}, /* writable variables in user file, words */
347
};
347
};
348
int INTERNAL_NAME_NO=(sizeof(internal_name)/sizeof(internal_name[0]));
348
int INTERNAL_NAME_NO=(sizeof(internal_name)/sizeof(internal_name[0]));
349
 
349
 
350
char *httpd_vars[]={
350
char *httpd_vars[]={
351
      "HTTP_ACCEPT",
351
  "HTTP_ACCEPT",
352
      "HTTP_ACCEPT_CHARSET",
352
  "HTTP_ACCEPT_CHARSET",
353
      "HTTP_ACCEPT_LANGUAGE",
353
  "HTTP_ACCEPT_LANGUAGE",
354
      "HTTP_COOKIE",
354
  "HTTP_COOKIE",
355
      "HTTP_HOST",
355
  "HTTP_HOST",
356
      "HTTP_USER_AGENT",
356
  "HTTP_USER_AGENT",
357
      "HTTP_X_REQUESTED_WITH",
357
  "HTTP_X_REQUESTED_WITH",
358
      "HTTPS",
358
  "HTTPS",
359
      "QUERY_STRING",
359
  "QUERY_STRING",
360
      "REMOTE_HOST",
360
  "REMOTE_HOST",
361
      "REMOTE_ADDR",
361
  "REMOTE_ADDR",
362
      "REMOTE_PORT",
362
  "REMOTE_PORT",
363
      "REQUEST_METHOD",
363
  "REQUEST_METHOD",
364
      "SCRIPT_NAME",
364
  "SCRIPT_NAME",
365
      "SERVER_NAME",
365
  "SERVER_NAME",
366
      "SERVER_SOFTWARE",
366
  "SERVER_SOFTWARE",
367
      "SERVER_PROTOCOL"
367
  "SERVER_PROTOCOL"
368
};
368
};
369
#define HTTPD_VAR_NO (sizeof(httpd_vars)/sizeof(httpd_vars[0]))
369
#define HTTPD_VAR_NO (sizeof(httpd_vars)/sizeof(httpd_vars[0]))
370
 
370
 
371
/* security: these variables will not be visible to child processes */
371
/* security: these variables will not be visible to child processes */
372
char *unsetvars[]={
372
char *unsetvars[]={
373
 "DOCUMENT_ROOT","SERVER_SIGNATURE","SERVER_SOFTWARE",
373
  "DOCUMENT_ROOT","SERVER_SIGNATURE","SERVER_SOFTWARE",
374
      "UNIQUE_ID","HTTP_KEEP_ALIVE","SSL_SESSION_ID"
374
      "UNIQUE_ID","HTTP_KEEP_ALIVE","SSL_SESSION_ID"
375
};
375
};
376
#define unsetvarcnt (sizeof(unsetvars)/sizeof(unsetvars[0]))
376
#define unsetvarcnt (sizeof(unsetvars)/sizeof(unsetvars[0]))
377
 
377
 
378
int httpd_type=httpd_apache;
378
int httpd_type=httpd_apache;
Line 386... Line 386...
386
void useropts(void);
386
void useropts(void);
387
 
387
 
388
/* Make certain httpd variables readable by modules */
388
/* Make certain httpd variables readable by modules */
389
void take_httpd_vars(void)
389
void take_httpd_vars(void)
390
{
390
{
391
    int i;
391
  int i;
392
    char *p, buf[MAX_NAMELEN+1];
392
  char *p, buf[MAX_NAMELEN+1];
393
    var_noexport=1;
393
  var_noexport=1;
394
    for(i=0;i<HTTPD_VAR_NO;i++) {
394
  for(i=0;i<HTTPD_VAR_NO;i++) {
395
     snprintf(buf,sizeof(buf),"httpd_%s",httpd_vars[i]);
395
   snprintf(buf,sizeof(buf),"httpd_%s",httpd_vars[i]);
396
     if((p=getenv(httpd_vars[i]))!=NULL) setvar(buf,p);
396
   if((p=getenv(httpd_vars[i]))!=NULL) setvar(buf,p);
397
    }
397
  }
398
    var_noexport=0;
398
  var_noexport=0;
399
 
399
 
400
    for(i=0;i<unsetvarcnt;i++) unsetenv(unsetvars[i]);
400
  for(i=0;i<unsetvarcnt;i++) unsetenv(unsetvars[i]);
401
     /* IPv4 IPv6*/
401
   /* IPv4 IPv6*/
402
    p=getenv("REMOTE_ADDR");if(p!=NULL && (strcmp(p,"127.0.0.1")==0 || strcmp(p,"::1")==0)) human_access=1;
402
  p=getenv("REMOTE_ADDR");if(p!=NULL && (strcmp(p,"127.0.0.1")==0 || strcmp(p,"::1")==0)) human_access=1;
403
    p=getenv("HTTP_REFERER"); if(p!=NULL && *p!=0) setvar("wims_referer",p);
403
  p=getenv("HTTP_REFERER"); if(p!=NULL && *p!=0) setvar("wims_referer",p);
404
}
404
}
405
 
405
 
406
/* cookie2session */
406
/* cookie2session */
407
void cookie2session(void)
407
void cookie2session(void)
408
{
408
{
409
    char cksession[64], psession[32], *ckey, *p;
409
  char cksession[64], psession[32], *ckey, *p;
410
    char nbuf[MAX_FNAME+1];
410
  char nbuf[MAX_FNAME+1];
411
 
411
 
412
    if(mode==mode_popup) return;
412
  if(mode==mode_popup) return;
413
    if(cookiegot[0]==0) {
413
  if(cookiegot[0]==0) {
414
     ckset: cookiegot[0]=0; setcookie=1; return;
414
   ckset: cookiegot[0]=0; setcookie=1; return;
415
    }
415
  }
416
    p=getvar("special_parm");
416
  p=getvar("special_parm");
417
    if(p!=NULL && strcmp(p,"ignorecookie")==0) return;
417
  if(p!=NULL && strcmp(p,"ignorecookie")==0) return;
418
    mystrncpy(cksession,cookiegot,sizeof(cksession));
418
  mystrncpy(cksession,cookiegot,sizeof(cksession));
419
    ckey=strchr(cksession,'-');
419
  ckey=strchr(cksession,'-');
420
    if(ckey==NULL) goto ckset; else *ckey++=0;
420
  if(ckey==NULL) goto ckset; else *ckey++=0;
421
    p=getvar("wims_session"); if(p==NULL) p="";
421
  p=getvar("wims_session"); if(p==NULL) p="";
422
    if(strstr(p,"new")!=NULL) goto ckset;
422
  if(strstr(p,"new")!=NULL) goto ckset;
423
    mystrncpy(psession,p,sizeof(psession));
423
  mystrncpy(psession,p,sizeof(psession));
424
    p=strchr(psession,'_'); if(p!=NULL) *p=0;
424
  p=strchr(psession,'_'); if(p!=NULL) *p=0;
425
    if(psession[0]!=0) {
425
  if(psession[0]!=0) {
426
      if(strcmp(psession,cksession)==0) return;
426
    if(strcmp(psession,cksession)==0) return;
427
      if(session_exists(psession)) goto ckset;
427
    if(session_exists(psession)) goto ckset;
428
      if(session_exists(cksession)) goto change;
428
    if(session_exists(cksession)) goto change;
429
    }
429
  }
430
    else {
430
  else {
431
      if(!session_exists(cksession)) return;
431
    if(!session_exists(cksession)) return;
432
      change:
432
    change:
433
      p=getenv("HTTPS");
433
    p=getenv("HTTPS");
434
      if(p!=NULL && strcasecmp(p,"on")==0) goto ckset;
434
    if(p!=NULL && strcasecmp(p,"on")==0) goto ckset;
435
      mkfname(nbuf,"%s/%s/var",session_dir,cksession);
435
    mkfname(nbuf,"%s/%s/var",session_dir,cksession);
436
      getdef(nbuf,"w_wims_ismanager",tmplbuf);
436
    getdef(nbuf,"w_wims_ismanager",tmplbuf);
437
      if(tmplbuf[0]!=0 && tmplbuf[0]!='0') goto ckset;
437
    if(tmplbuf[0]!=0 && tmplbuf[0]!='0') goto ckset;
438
      getdef(nbuf,"w_wims_protocol",tmplbuf);
438
    getdef(nbuf,"w_wims_protocol",tmplbuf);
439
      if(strcasecmp(tmplbuf,"https")==0) goto ckset;
439
    if(strcasecmp(tmplbuf,"https")==0) goto ckset;
440
      mkfname(nbuf,"%s/%s/var.stat",session_dir,cksession);
440
    mkfname(nbuf,"%s/%s/var.stat",session_dir,cksession);
441
      getdef(nbuf,"wims_user",tmplbuf);
441
    getdef(nbuf,"wims_user",tmplbuf);
442
      if(tmplbuf[0]!=0) goto ckset;
442
    if(tmplbuf[0]!=0) goto ckset;
443
      force_setvar(ro_name[ro_session],cksession);
443
    force_setvar(ro_name[ro_session],cksession);
444
      setsesdir(cksession);
444
    setsesdir(cksession);
445
      force_setvar("wims_subsession","");
445
    force_setvar("wims_subsession","");
446
      session_serial=0;
446
    session_serial=0;
447
    }
447
  }
448
}
448
}
449
 
449
 
450
void determine_font(char *l)
450
void determine_font(char *l)
451
{
451
{
452
    int i;
452
  int i;
453
 
453
 
454
    if(l==NULL || *l==0) return;
454
  if(l==NULL || *l==0) return;
455
    for(i=0;i<charname_no && memcmp(charname[i].name,l,2);i++);
455
  for(i=0;i<charname_no && memcmp(charname[i].name,l,2);i++);
456
    if(i<charname_no) setvar("wims_main_font",charname[i].font);
456
  if(i<charname_no) setvar("wims_main_font",charname[i].font);
457
}
457
}
458
 
458
 
459
void determine_dirn(char *l)
459
void determine_dirn(char *l)
460
{
460
{
461
    int i;
461
  int i;
462
 
462
 
463
    if(l==NULL || *l==0) return;
463
  if(l==NULL || *l==0) return;
464
    for(i=0;i<dirnname_no && memcmp(dirnname[i].name,l,2);i++);
464
  for(i=0;i<dirnname_no && memcmp(dirnname[i].name,l,2);i++);
465
    if(i<dirnname_no) setvar("wims_main_dirn",dirnname[i].dirn);
465
  if(i<dirnname_no) setvar("wims_main_dirn",dirnname[i].dirn);
466
}
466
}
467
 
467
 
468
void predetermine_language(void)
468
void predetermine_language(void)
469
{
469
{
470
    char *p;
470
  char *p;
471
    int i,n;
471
  int i,n;
472
 
472
 
473
    if(pre_language[0]!=0) p=pre_language;
473
  if(pre_language[0]!=0) p=pre_language;
474
    else p=getenv("HTTP_ACCEPT_LANGUAGE");
474
  else p=getenv("HTTP_ACCEPT_LANGUAGE");
475
    if(p!=NULL && strlen(p)>=2) {
475
  if(p!=NULL && strlen(p)>=2) {
476
      for(i=0;i<available_lang_no && memcmp(p,available_lang[i],2)!=0;i++);
-
 
477
      if(i<available_lang_no) goto lend;
-
 
478
    }
-
 
479
    p=getenv("HTTP_USER_AGENT");
-
 
480
    if(p!=NULL && strlen(p)>=5) {
-
 
481
      char *q;
-
 
482
      if((q=strchr(p,'['))!=NULL && islower(*(q+1)) && islower(*(q+2)) && *(q+3)==']') {
-
 
483
          char bb[4];
-
 
484
          bb[0]=*(q+1);bb[1]=*(q+2);bb[2]=0;
-
 
485
          for(i=0;i<available_lang_no && memcmp(bb,available_lang[i],2)!=0;i++);
-
 
486
          if(i<available_lang_no) {
-
 
487
            memmove(lang,bb,2); lang[2]=0;
-
 
488
            goto lend2;
-
 
489
          }
-
 
490
      }
-
 
491
    }
-
 
492
    p=getenv("HTTP_HOST"); if(p==NULL) goto lend2;
-
 
493
    n=strlen(p); if(n<=3 || *(p+n-3)!='.') goto lend2;
-
 
494
    p=p+n-2;
-
 
495
    for(i=0;i<available_lang_no && memcmp(p,available_lang[i],2)!=0;i++);
476
    for(i=0;i<available_lang_no && memcmp(p,available_lang[i],2)!=0;i++);
-
 
477
    if(i<available_lang_no) goto lend;
-
 
478
  }
-
 
479
  p=getenv("HTTP_USER_AGENT");
-
 
480
  if(p!=NULL && strlen(p)>=5) {
-
 
481
    char *q;
-
 
482
    if((q=strchr(p,'['))!=NULL && islower(*(q+1)) && islower(*(q+2)) && *(q+3)==']') {
-
 
483
        char bb[4];
-
 
484
        bb[0]=*(q+1);bb[1]=*(q+2);bb[2]=0;
-
 
485
        for(i=0;i<available_lang_no && memcmp(bb,available_lang[i],2)!=0;i++);
496
    if(i<available_lang_no) {
486
        if(i<available_lang_no) {
497
      lend: memmove(lang,p,2); lang[2]=0;
487
          memmove(lang,bb,2); lang[2]=0;
498
      lend2: determine_font(lang);determine_dirn(lang);
488
          goto lend2;
-
 
489
        }
499
    }
490
    }
-
 
491
  }
-
 
492
  p=getenv("HTTP_HOST"); if(p==NULL) goto lend2;
-
 
493
  n=strlen(p); if(n<=3 || *(p+n-3)!='.') goto lend2;
-
 
494
  p=p+n-2;
-
 
495
  for(i=0;i<available_lang_no && memcmp(p,available_lang[i],2)!=0;i++);
-
 
496
  if(i<available_lang_no) {
-
 
497
    lend: memmove(lang,p,2); lang[2]=0;
-
 
498
    lend2: determine_font(lang);determine_dirn(lang);
-
 
499
  }
500
}
500
}
501
 
501
 
502
/* print a special page */
502
/* print a special page */
503
void put_special_page(char *pname)
503
void put_special_page(char *pname)
504
{
504
{
505
    determine_font(lang);
505
  determine_font(lang);
506
    determine_dirn(lang);
506
  determine_dirn(lang);
507
    phtml_put_base(mkfname(NULL,"%s.phtml.%s",pname,lang),0);
507
  phtml_put_base(mkfname(NULL,"%s.phtml.%s",pname,lang),0);
508
    write_logs();free(var_str);
508
  write_logs();free(var_str);
509
}
509
}
510
 
510
 
511
/* check whether the connection is a site manager. */
511
/* check whether the connection is a site manager. */
512
void manager_check(void)
512
void manager_check(void)
513
{
513
{
514
    char *p, *pp, buf[16];
514
  char *p, *pp, buf[16];
515
    struct stat confstat;
515
  struct stat confstat;
516
    int i;
516
  int i;
517
 
517
 
518
    manageable=0;
518
  manageable=0;
519
    if(robot_access || *manager_site==0 || checkhost(manager_site)<1)
519
  if(robot_access || *manager_site==0 || checkhost(manager_site)<1)
520
      goto mend;
520
    goto mend;
521
    if(manager_https) {
521
  if(manager_https) {
522
      p=getenv("HTTPS");
522
    p=getenv("HTTPS");
523
      if(p==NULL || strcmp(p,"on")!=0) goto mend;
523
    if(p==NULL || strcmp(p,"on")!=0) goto mend;
524
    }
524
  }
525
/* IPv4 IPv6*/
525
/* IPv4 IPv6*/
526
    if(strcmp(remote_addr,"127.0.0.1")==0 || strcmp(remote_addr,"::1")==0) {
526
  if(strcmp(remote_addr,"127.0.0.1")==0 || strcmp(remote_addr,"::1")==0) {
527
      int port, port2;
527
    int port, port2;
528
      char tester[128];
528
    char tester[128];
529
      p=getenv("REMOTE_PORT"); if(p==NULL) goto mend;
529
    p=getenv("REMOTE_PORT"); if(p==NULL) goto mend;
530
      port=atoi(p); if(port<1024 || port>65535) goto mend;
530
    port=atoi(p); if(port<1024 || port>65535) goto mend;
531
      p=getenv("SERVER_PORT"); if(p==NULL) goto mend;
531
    p=getenv("SERVER_PORT"); if(p==NULL) goto mend;
532
      port2=atoi(p); if(port2>=10000 || port2<=0) goto mend;
532
    port2=atoi(p); if(port2>=10000 || port2<=0) goto mend;
533
/* this is very non-portable */
533
/* this is very non-portable */
534
      manageable=1;
534
    manageable=1;
535
      accessfile(tmplbuf,"r","/proc/net/tcp");
535
    accessfile(tmplbuf,"r","/proc/net/tcp");
536
      snprintf(tester,sizeof(tester)," 0100007F:%04X 0100007F:%04X ",
536
    snprintf(tester,sizeof(tester)," 0100007F:%04X 0100007F:%04X ",
537
             port,port2);
537
           port,port2);
538
      p=strstr(tmplbuf,tester);
538
    p=strstr(tmplbuf,tester);
539
      if(p!=NULL) {
539
    if(p!=NULL) {
540
          pp=strchr(p,'\n'); if(pp!=NULL) *pp=0;
540
      pp=strchr(p,'\n'); if(pp!=NULL) *pp=0;
541
          if(strlen(p)>=75) {
541
      if(strlen(p)>=75) {
542
            p=find_word_start(p+70); *find_word_end(p)=0;
542
        p=find_word_start(p+70); *find_word_end(p)=0;
543
            if(atoi(p)==geteuid()) manageable=2;
543
        if(atoi(p)==geteuid()) manageable=2;
544
          }
-
 
545
      }
-
 
546
    }
-
 
547
    else manageable=1;
-
 
548
    i=stat(config_file,&confstat);
-
 
549
    if(i==0 && manageable>0 && (confstat.st_mode&(S_IRWXO|S_IRWXG))!=0) manageable=-1;
-
 
550
    if(manageable>0 && !trusted_module()) manageable=0;
-
 
551
    if(manageable==1) {
-
 
552
      accessfile(tmplbuf,"r","%s/.manager",session_prefix);
-
 
553
      if(strstr(tmplbuf,"yes")!=NULL) manageable=2;
-
 
554
    }
-
 
555
    if(manageable==1) {
-
 
556
      p=getvar(ro_name[ro_module]);
-
 
557
      if(p!=NULL && strncmp(p,"adm/manage",strlen("adm/manage"))==0) {
-
 
558
          struct stat pstat;
-
 
559
          if(stat("../log/.wimspass",&pstat)==0) {
-
 
560
            if((S_IFMT&pstat.st_mode)!=S_IFREG ||
-
 
561
               ((S_IRWXO|S_IRWXG)&pstat.st_mode)!=0)
-
 
562
              manageable=-2;
-
 
563
          }
-
 
564
      }
544
      }
565
    }
545
    }
-
 
546
  }
-
 
547
  else manageable=1;
-
 
548
  i=stat(config_file,&confstat);
-
 
549
  if(i==0 && manageable>0 && (confstat.st_mode&(S_IRWXO|S_IRWXG))!=0) manageable=-1;
-
 
550
  if(manageable>0 && !trusted_module()) manageable=0;
-
 
551
  if(manageable==1) {
-
 
552
    accessfile(tmplbuf,"r","%s/.manager",session_prefix);
-
 
553
    if(strstr(tmplbuf,"yes")!=NULL) manageable=2;
-
 
554
  }
-
 
555
  if(manageable==1) {
-
 
556
    p=getvar(ro_name[ro_module]);
-
 
557
    if(p!=NULL && strncmp(p,"adm/manage",strlen("adm/manage"))==0) {
-
 
558
      struct stat pstat;
-
 
559
      if(stat("../log/.wimspass",&pstat)==0) {
-
 
560
        if((S_IFMT&pstat.st_mode)!=S_IFREG ||
-
 
561
           ((S_IRWXO|S_IRWXG)&pstat.st_mode)!=0)
-
 
562
          manageable=-2;
-
 
563
      }
-
 
564
    }
-
 
565
  }
566
    mend:
566
  mend:
567
    mystrncpy(buf,int2str(manageable),sizeof(buf));
567
  mystrncpy(buf,int2str(manageable),sizeof(buf));
568
    force_setvar("wims_ismanager",buf);
568
  force_setvar("wims_ismanager",buf);
569
    if(manageable>=2) {
569
  if(manageable>=2) {
570
      struct rlimit rlim;
570
    struct rlimit rlim;
571
      rlimit_cpu*=10;
571
    rlimit_cpu*=10;
572
      rlim.rlim_cur=rlim.rlim_max=rlimit_cpu;
572
    rlim.rlim_cur=rlim.rlim_max=rlimit_cpu;
573
      setrlimit(RLIMIT_CPU,&rlim);
573
    setrlimit(RLIMIT_CPU,&rlim);
574
      mystrncpy(buf,int2str(rlimit_cpu),sizeof(buf));
574
    mystrncpy(buf,int2str(rlimit_cpu),sizeof(buf));
575
      setvar("wims_cpu_limit",buf);
575
    setvar("wims_cpu_limit",buf);
576
      initalarm();
576
    initalarm();
577
    }
577
  }
578
}
578
}
579
 
579
 
580
/* check for robot access */
580
/* check for robot access */
581
void robot_check(void)
581
void robot_check(void)
582
{
582
{
583
    char *ua, *p, *ses, *c, *mod;
583
  char *ua, *p, *ses, *c, *mod;
584
    int i;
584
  int i;
585
 
585
 
586
    if(human_access) return;
586
  if(human_access) return;
587
    mod=getvar(ro_name[ro_module]);
587
  mod=getvar(ro_name[ro_module]);
588
    if(mod!=NULL && strcmp(mod,"adm/raw")==0) return;
588
  if(mod!=NULL && strcmp(mod,"adm/raw")==0) return;
589
    ses=getvar(ro_name[ro_session]);
589
  ses=getvar(ro_name[ro_session]);
590
/* user has valid session; OK */
590
/* user has valid session; OK */
591
    if(ses!=NULL && strncmp(ses,robot_session,strlen(robot_session))!=0
591
  if(ses!=NULL && strncmp(ses,robot_session,strlen(robot_session))!=0
592
       && strchr(ses,'/')==NULL
592
     && strchr(ses,'/')==NULL
593
       && ftest(mkfname(NULL,"%s/%s",s2_dir,ses))==is_dir)
593
     && ftest(mkfname(NULL,"%s/%s",s2_dir,ses))==is_dir)
594
      return;
594
    return;
595
    ua=getenv("HTTP_USER_AGENT"); if(ua==NULL) ua="";
595
  ua=getenv("HTTP_USER_AGENT"); if(ua==NULL) ua="";
596
    ua=find_word_start(ua);
596
  ua=find_word_start(ua);
597
    if(strncasecmp(ua,"Mozilla",strlen("Mozilla"))==0 &&
597
  if(strncasecmp(ua,"Mozilla",strlen("Mozilla"))==0 &&
598
       (p=strstr(ua,"compatible"))!=NULL)
598
     (p=strstr(ua,"compatible"))!=NULL)
599
      ua=find_word_start(find_word_end(p));
599
    ua=find_word_start(find_word_end(p));
600
    if(*ua) {
600
  if(*ua) {
601
      for(i=0;i<good_agent_no
601
    for(i=0;i<good_agent_no
602
          && strncasecmp(ua,good_agent[i],strlen(good_agent[i]));i++);
602
        && strncasecmp(ua,good_agent[i],strlen(good_agent[i]));i++);
603
      if(i<good_agent_no) return;
603
    if(i<good_agent_no) return;
604
      for(i=0;i<bad_agent_no
604
    for(i=0;i<bad_agent_no
605
          && strstr(ua,bad_agent[i])==NULL;i++);
605
        && strstr(ua,bad_agent[i])==NULL;i++);
606
      if(i<bad_agent_no) user_error("trapped");
606
    if(i<bad_agent_no) user_error("trapped");
607
    }
607
  }
608
    force_setvar(ro_name[ro_session],robot_session);
608
  force_setvar(ro_name[ro_session],robot_session);
609
    setsesdir(robot_session);
609
  setsesdir(robot_session);
610
    c=getvar(ro_name[ro_cmd]);
610
  c=getvar(ro_name[ro_cmd]);
611
    robot_access=1;
611
  robot_access=1;
612
    if(c!=NULL && strcmp(c,"new") && strcmp(c,"intro")) {
612
  if(c!=NULL && strcmp(c,"new") && strcmp(c,"intro")) {
613
      force_setvar(ro_name[ro_cmd],"robot_error");
613
    force_setvar(ro_name[ro_cmd],"robot_error");
614
      nph_header(450); put_special_page("robot");
614
    nph_header(450); put_special_page("robot");
615
      flushoutput(); flushlog(); exit(0);
615
    flushoutput(); flushlog(); exit(0);
616
    }
616
  }
617
}
617
}
618
 
618
 
619
/* type=0: ordinary; type=1: multipart/form-data */
619
/* type=0: ordinary; type=1: multipart/form-data */
620
void parse_query_string(int len, int type)
620
void parse_query_string(int len, int type)
621
{
621
{
622
    int i,j,l,v,cmd_defined;
622
  int i,j,l,v,cmd_defined;
623
    int parenth=-1, ll, lb, dlen;
623
  int parenth=-1, ll, lb, dlen;
624
    char *start, *p, *p1, *pt, *b1="";
624
  char *start, *p, *p1, *pt, *b1="";
625
 
625
 
626
    cmd_defined=0;
626
  cmd_defined=0;
627
    setvar("wims_subsession","");
627
  setvar("wims_subsession","");
628
    ll=lb=0;
628
  ll=lb=0;
-
 
629
  if(type) {
-
 
630
    ll=strlen(mpboundary);
-
 
631
    start=strstr(var_str,mpboundary);
-
 
632
    if(start==NULL) start=var_str+strlen(var_str);
-
 
633
    if(strstr(var_str,"\r\n\r\n")!=NULL) b1="\r\n\r\n";
-
 
634
    else b1="\n\n";
-
 
635
    lb=strlen(b1);
-
 
636
  }
-
 
637
  else start=var_str;
-
 
638
  for(v=0, p1=start;p1<var_str+len;p1+=l) {
629
    if(type) {
639
    if(type) {
630
      ll=strlen(mpboundary);
-
 
631
      start=strstr(var_str,mpboundary);
-
 
632
      if(start==NULL) start=var_str+strlen(var_str);
-
 
633
      if(strstr(var_str,"\r\n\r\n")!=NULL) b1="\r\n\r\n";
-
 
634
      else b1="\n\n";
-
 
635
      lb=strlen(b1);
-
 
636
    }
-
 
637
    else start=var_str;
-
 
638
    for(v=0, p1=start;p1<var_str+len;p1+=l) {
-
 
639
      if(type) {
-
 
640
          char *p2, *p3, *p4, *p5;
640
      char *p2, *p3, *p4, *p5;
641
          p2=p1+ll; p3=memstr(p2,mpboundary,var_str+len-p2); l=p3-p1;
641
      p2=p1+ll; p3=memstr(p2,mpboundary,var_str+len-p2); l=p3-p1;
642
          p=memstr(p2,b1,var_str+len-p2); if(p>=p3) continue;
642
      p=memstr(p2,b1,var_str+len-p2); if(p>=p3) continue;
-
 
643
      p+=lb;
643
          p+=lb; if(p3<var_str+len) {
644
      if(p3<var_str+len) {
644
            while(*p3!='\n' && p3>p2) p3--;
645
        while(*p3!='\n' && p3>p2) p3--;
645
            *p3=0;
646
        *p3=0;
646
            p3--; if(*p3=='\r') *p3=0;
647
        p3--; if(*p3=='\r') *p3=0;
647
          }
-
 
648
          dlen=p3-p;
-
 
649
          p2=memstr(p2,"name=",p3-p2); if(p2>=p3) continue;
-
 
650
          p2+=strlen("name="); if(*p2=='"') p2++;
-
 
651
          for(p3=p2; myisalnum(*p3) || strchr("._",*p3)!=NULL; p3++);
-
 
652
          if(p3==p2) continue;
-
 
653
          if(p3-p2==strlen("wims_deposit") &&
-
 
654
             strncmp(p2,"wims_deposit",p3-p2)==0) {
-
 
655
            p4=memstr(p1,"filename=",p-p1); if(p4<p) {
-
 
656
                p4+=strlen("filename="); if(*p4=='"') {
-
 
657
                  p4++; p5=strchr(p4,'"');
-
 
658
                  if(p5==NULL || p5-p4>=MAX_FNAME) goto emptyquote;
-
 
659
                }
-
 
660
                else {
-
 
661
                  emptyquote:
-
 
662
                  for(p5=p4; p5<p && !isspace(*p5) &&
-
 
663
                      strchr(";\"~#*?=,'",*p5)==NULL; p5++);
-
 
664
                }
-
 
665
                if(p5>p4) {
-
 
666
                  *p5=0;
-
 
667
                  for(p5--;
-
 
668
                      p5>=p4 && !isspace(*p5) && strchr("/\\:",*p5)==NULL;
-
 
669
                      p5--);
-
 
670
                  if(p5>=p4) p4=p5+1;
-
 
671
                  if(*p4==0) goto noname;
-
 
672
                  if(strstr(p4,"..")!=NULL || *p4=='.')
-
 
673
                    p4="noname.file";
-
 
674
                  setvar("wims_deposit",p4);
-
 
675
                }
-
 
676
                else {
-
 
677
                  noname: setvar("wims_deposit","noname.file");
-
 
678
                }
-
 
679
            }
-
 
680
            deplen=dlen;
-
 
681
          }
-
 
682
          *p3=0; l-=p2-p1; p1=p2;
-
 
683
      }
648
      }
684
      else {
649
      dlen=p3-p;
-
 
650
      p2=memstr(p2,"name=",p3-p2); if(p2>=p3) continue;
-
 
651
      p2+=strlen("name="); if(*p2=='"') p2++;
-
 
652
      for(p3=p2; myisalnum(*p3) || strchr("._",*p3)!=NULL; p3++);
685
          p1=find_word_start(p1);
653
      if(p3==p2) continue;
-
 
654
        if(p3-p2==strlen("wims_deposit") &&
-
 
655
           strncmp(p2,"wims_deposit",p3-p2)==0) {
-
 
656
          p4=memstr(p1,"filename=",p-p1); if(p4<p) {
-
 
657
            p4+=strlen("filename="); if(*p4=='"') {
686
          l=strlen(p1)+1; p=strchr(p1,'=');
658
              p4++; p5=strchr(p4,'"');
-
 
659
              if(p5==NULL || p5-p4>=MAX_FNAME) goto emptyquote;
-
 
660
            }
-
 
661
            else {
-
 
662
              emptyquote:
-
 
663
              for(p5=p4; p5<p && !isspace(*p5) &&
-
 
664
                  strchr(";\"~#*?=,'",*p5)==NULL; p5++);
-
 
665
            }
-
 
666
            if(p5>p4) {
-
 
667
              *p5=0;
-
 
668
              for(p5--;
-
 
669
                  p5>=p4 && !isspace(*p5) && strchr("/\\:",*p5)==NULL;
-
 
670
                  p5--);
687
          if(p==NULL) p=p1+strlen(p1);
671
              if(p5>=p4) p4=p5+1;
688
          if(*p==0 && l>1) {
672
              if(*p4==0) goto noname;
-
 
673
              if(strstr(p4,"..")!=NULL || *p4=='.')
689
            user_variable[v].name="no_name";
674
                p4="noname.file";
690
            user_variable[v].value=p1;
675
              setvar("wims_deposit",p4);
691
            coord_input=1;
676
            }
692
            goto nnext;
677
            else {
-
 
678
              noname: setvar("wims_deposit","noname.file");
-
 
679
            }
693
          }
680
          }
694
          *p++=0;
681
          deplen=dlen;
-
 
682
        }
-
 
683
        *p3=0; l-=p2-p1; p1=p2;
-
 
684
    }
-
 
685
    else {
-
 
686
      p1=find_word_start(p1);
-
 
687
      l=strlen(p1)+1; p=strchr(p1,'=');
-
 
688
      if(p==NULL) p=p1+strlen(p1);
-
 
689
      if(*p==0 && l>1) {
-
 
690
        user_variable[v].name="no_name";
-
 
691
        user_variable[v].value=p1;
-
 
692
        coord_input=1;
-
 
693
        goto nnext;
695
      }
694
      }
-
 
695
      *p++=0;
-
 
696
    }
696
/* empty name or empty value: ignore */
697
/* empty name or empty value: ignore */
697
        if(*p1==0 || *p==0) continue;
698
    if(*p1==0 || *p==0) continue;
698
/* We do not treat names containing '.' */
699
/* We do not treat names containing '.' */
699
      for(pt=strchr(p1,'.'); pt; pt=strchr(++pt,'.')) *pt='_';
700
    for(pt=strchr(p1,'.'); pt; pt=strchr(++pt,'.')) *pt='_';
700
/* Restrictions on variable names */
701
/* Restrictions on variable names */
701
      for(pt=p1; myisalnum(*pt) || *pt=='_'; pt++);
702
    for(pt=p1; myisalnum(*pt) || *pt=='_'; pt++);
702
      if(*pt) continue;
703
    if(*pt) continue;
703
      if(strcmp(p1,"wims_deposit")!=0) _tolinux(p);
704
    if(strcmp(p1,"wims_deposit")!=0) _tolinux(p);
704
/* This is a restriction:
705
/* This is a restriction:
705
 * Every parameter must have matching parentheses.
706
 * Every parameter must have matching parentheses.
706
 */
707
 */
707
      if(parenth==-1 && strncmp(p1,"freepar_",strlen("freepar_"))!=0
708
    if(parenth==-1 && strncmp(p1,"freepar_",strlen("freepar_"))!=0
708
         && strcmp(p1,"wims_deposit")!=0
709
         && strcmp(p1,"wims_deposit")!=0
709
         && check_parentheses(p,1)) parenth=v;
710
         && check_parentheses(p,1)) parenth=v;
710
      if(strcmp(p1,"special_parm")==0 && strcmp(p,"wims")==0)
711
    if(strcmp(p1,"special_parm")==0 && strcmp(p,"wims")==0)
711
        human_access=1;
712
        human_access=1;
712
      j=search_list(ro_name,RO_NAME_NO,sizeof(ro_name[0]),p1);
713
    j=search_list(ro_name,RO_NAME_NO,sizeof(ro_name[0]),p1);
713
      if(j>=0) {
714
    if(j>=0) {
714
          if(j==ro_session) {
715
      if(j==ro_session) {
715
            p=find_word_start(p); *find_word_end(p)=0;
716
        p=find_word_start(p); *find_word_end(p)=0;
716
            if(strlen(p)>MAX_SESSIONLEN) continue;
717
        if(strlen(p)>MAX_SESSIONLEN) continue;
717
            if(strcmp(p,robot_session)==0) p="";
718
        if(strcmp(p,robot_session)==0) p="";
718
            if(strcasecmp(p,"popup")==0) {
719
        if(strcasecmp(p,"popup")==0) {
719
                mode=mode_popup;
720
          mode=mode_popup;
720
                force_setvar("wims_mode","popup");
721
          force_setvar("wims_mode","popup");
721
                force_setvar("session","");
722
          force_setvar("session","");
722
                continue;
-
 
723
            }
-
 
724
          }
-
 
725
          if(j==ro_module) module_defined=1;
-
 
726
          if(j==ro_cmd) {
-
 
727
            p=find_word_start(p); *find_word_end(p)=0;
-
 
728
            if(strlen(p)>16) continue;
-
 
729
            cmd_defined=1;
-
 
730
          }
-
 
731
          if(j==ro_lang) {
-
 
732
            if(strlen(p)!=2) continue;
-
 
733
            for(i=0;i<available_lang_no && strcmp(available_lang[i],p)!=0;i++);
-
 
734
            if(i<available_lang_no) {user_lang=1; ovlstrcpy(lang,p);}
-
 
735
            else continue;
-
 
736
          }
-
 
737
/* strip leading and trailing '/'s in module name */
-
 
738
          if(j==ro_module) {
-
 
739
            p=find_word_start(p); *find_word_end(p)=0;
-
 
740
            while(*p=='/') p++;
-
 
741
            while(*p!=0 && *(p+strlen(p)-1)=='/') *(p+strlen(p)-1)=0;
-
 
742
            if(strlen(p)>MAX_MODULELEN) continue;
-
 
743
          }
-
 
744
          setvar(p1,p);
-
 
745
          if(j==ro_session && mode!=mode_popup) {
-
 
746
            char *pp, *pr;
-
 
747
            char buf[1024];
-
 
748
            mystrncpy(buf,p,sizeof(buf));
-
 
749
            if((pp=strchr(buf,'.'))!=NULL) {
-
 
750
                *pp++=0; session_serial=atoi(pp);
-
 
751
                if(pp<0) pp=0;
-
 
752
            }
-
 
753
            else session_serial=0;
-
 
754
            pp=strchr(buf,'_');
-
 
755
            if(pp!=NULL && (pr=strstr(pp,"_mhelp"))!=NULL) {
-
 
756
                *pr=0; ismhelp=1; lastout_file=-1;
-
 
757
                setvar("wims_inssub","mh");
-
 
758
            }
-
 
759
            force_setvar("wims_session",buf);
-
 
760
            if(pp!=NULL) force_setvar("wims_subsession",pp);
-
 
761
          }
-
 
762
          continue;
723
          continue;
-
 
724
        }
-
 
725
      }
-
 
726
      if(j==ro_module) module_defined=1;
-
 
727
      if(j==ro_cmd) {
-
 
728
        p=find_word_start(p); *find_word_end(p)=0;
-
 
729
        if(strlen(p)>16) continue;
-
 
730
        cmd_defined=1;
-
 
731
      }
-
 
732
      if(j==ro_lang) {
-
 
733
        if(strlen(p)!=2) continue;
-
 
734
        for(i=0;i<available_lang_no && strcmp(available_lang[i],p)!=0;i++);
-
 
735
        if(i<available_lang_no) {user_lang=1; ovlstrcpy(lang,p);}
-
 
736
        else continue;
-
 
737
      }
-
 
738
/* strip leading and trailing '/'s in module name */
-
 
739
      if(j==ro_module) {
-
 
740
        p=find_word_start(p); *find_word_end(p)=0;
-
 
741
        while(*p=='/') p++;
-
 
742
        while(*p!=0 && *(p+strlen(p)-1)=='/') *(p+strlen(p)-1)=0;
-
 
743
        if(strlen(p)>MAX_MODULELEN) continue;
-
 
744
      }
-
 
745
      setvar(p1,p);
-
 
746
      if(j==ro_session && mode!=mode_popup) {
-
 
747
        char *pp, *pr;
-
 
748
        char buf[1024];
-
 
749
        mystrncpy(buf,p,sizeof(buf));
-
 
750
        if((pp=strchr(buf,'.'))!=NULL) {
-
 
751
          *pp++=0; session_serial=atoi(pp);
-
 
752
          if(pp<0) pp=0;
-
 
753
        }
-
 
754
        else session_serial=0;
-
 
755
        pp=strchr(buf,'_');
-
 
756
        if(pp!=NULL && (pr=strstr(pp,"_mhelp"))!=NULL) {
-
 
757
          *pr=0; ismhelp=1; lastout_file=-1;
-
 
758
          setvar("wims_inssub","mh");
-
 
759
        }
-
 
760
        force_setvar("wims_session",buf);
-
 
761
        if(pp!=NULL) force_setvar("wims_subsession",pp);
763
      }
762
      }
764
      user_variable[v].name=p1;
-
 
765
      user_variable[v].value=p;
763
      continue;
766
nnext:v++; if(v>=MAX_VAR_NUM) user_error("too_many_variables");
-
 
767
    }
764
    }
-
 
765
    user_variable[v].name=p1;
-
 
766
    user_variable[v].value=p;
-
 
767
nnext:v++; if(v>=MAX_VAR_NUM) user_error("too_many_variables");
-
 
768
  }
768
    user_var_no=v;
769
  user_var_no=v;
769
    if(parenth>=0) {
770
  if(parenth>=0) {
770
      char buf[16];
771
    char buf[16];
771
      mystrncpy(buf,int2str(user_var_no),sizeof(buf));
772
    mystrncpy(buf,int2str(user_var_no),sizeof(buf));
772
      setvar("user_var_no",buf);
773
    setvar("user_var_no",buf);
773
      for(i=0;i<user_var_no;i++) {
774
    for(i=0;i<user_var_no;i++) {
774
          snprintf(buf,sizeof(buf),"name%d",i);
775
      snprintf(buf,sizeof(buf),"name%d",i);
775
          setvar(buf,user_variable[i].name);
776
      setvar(buf,user_variable[i].name);
776
          snprintf(buf,sizeof(buf),"value%d",i);
777
      snprintf(buf,sizeof(buf),"value%d",i);
777
          setvar(buf,user_variable[i].value);
778
      setvar(buf,user_variable[i].value);
778
      }
-
 
779
      mystrncpy(buf,int2str(parenth),sizeof(buf));
-
 
780
      setvar("bad_parentheses",buf);
-
 
781
      user_error("unmatched_parentheses");
-
 
782
    }
779
    }
-
 
780
    mystrncpy(buf,int2str(parenth),sizeof(buf));
-
 
781
    setvar("bad_parentheses",buf);
-
 
782
    user_error("unmatched_parentheses");
-
 
783
  }
783
    p=getenv("SCRIPT_NAME");
784
  p=getenv("SCRIPT_NAME");
784
    if(p!=NULL && (p=strstr(p,"/getfile/"))!=NULL) {
785
  if(p!=NULL && (p=strstr(p,"/getfile/"))!=NULL) {
785
      p+=strlen("/getfile/");
786
    p+=strlen("/getfile/");
786
      force_setvar(ro_name[ro_cmd],commands[cmd_getfile]);
787
    force_setvar(ro_name[ro_cmd],commands[cmd_getfile]);
787
      force_setvar("special_parm",p);
788
    force_setvar("special_parm",p);
788
      cmd_defined=1;
789
    cmd_defined=1;
789
    }
790
  }
790
    if(module_defined && !cmd_defined) setvar(ro_name[ro_cmd],commands[cmd_intro]);
791
  if(module_defined && !cmd_defined) setvar(ro_name[ro_cmd],commands[cmd_intro]);
791
    robot_check(); cookie2session();
792
  robot_check(); cookie2session();
792
}
793
}
793
 
794
 
794
/* parse special commands */
795
/* parse special commands */
795
void special_cmds(void)
796
void special_cmds(void)
796
{
797
{
797
    char *c, *p;
798
  char *c, *p;
798
    int i;
799
  int i;
799
    long int l=-1;
800
  long int l=-1;
800
 
801
 
801
    c=getvar(ro_name[ro_cmd]);
802
  c=getvar(ro_name[ro_cmd]);
802
    if(c==NULL || *c==0) {  /* no module name nor command */
803
  if(c==NULL || *c==0) {  /* no module name nor command */
803
      setvar(ro_name[ro_module],home_module);
804
    setvar(ro_name[ro_module],home_module);
804
      setvar(ro_name[ro_cmd],commands[cmd_new]);
805
    setvar(ro_name[ro_cmd],commands[cmd_new]);
-
 
806
    return;
-
 
807
  }
-
 
808
  for(i=0;i<CMD_NO && strcmp(commands[i],c)!=0; i++);
-
 
809
  switch(i) {
-
 
810
    case cmd_intro: {
-
 
811
      set_module_prefix();
-
 
812
      default_form_method="get";
-
 
813
      if(ftest(mkfname(NULL,"%s/%s",module_prefix,intro_file))<0) {
-
 
814
        force_setvar(ro_name[ro_cmd],commands[cmd_new]);
805
      return;
815
        return;
-
 
816
      }
-
 
817
      p=getvar("wims_session");
-
 
818
      if(p!=NULL && *p!=0) {
-
 
819
        if(set_session_prefix()==0) check_session();
-
 
820
        else {
-
 
821
          trap_check(p);
-
 
822
          if(strchr(p,'_')!=NULL && strchr(p,'/')==NULL) {
-
 
823
            get_static_session_var();
-
 
824
          }
-
 
825
        }
-
 
826
      }
-
 
827
/* determine http protocol name. How to detect? */
-
 
828
      p=getenv("HTTPS");
-
 
829
      if(p!=NULL && strcmp(p,"on")==0) {
-
 
830
        protocol="https"; set_protocol();
-
 
831
      }
-
 
832
      force_setvar("wims_protocol",protocol);
-
 
833
      determine_font(lang);
-
 
834
      determine_dirn(lang);
-
 
835
      main_phtml_put(intro_file); debug_output();
-
 
836
      introend: write_logs();free(var_str);
-
 
837
      delete_pid(); exit(0);
-
 
838
    }
-
 
839
    case cmd_ref: {
-
 
840
      set_module_prefix();
-
 
841
      default_form_method="get";
-
 
842
      p=getvar("wims_session");
-
 
843
      if(p!=NULL && *p!=0) {
-
 
844
        if(set_session_prefix()==0) check_session();
-
 
845
        else trap_check(p);
-
 
846
      }
-
 
847
      determine_font(lang);
-
 
848
      determine_dirn(lang);
-
 
849
      main_phtml_put(ref_file); goto introend;
806
    }
850
    }
807
    for(i=0;i<CMD_NO && strcmp(commands[i],c)!=0; i++);
-
 
808
    switch(i) {
-
 
809
      case cmd_intro: {
-
 
810
          set_module_prefix();
-
 
811
          default_form_method="get";
-
 
812
          if(ftest(mkfname(NULL,"%s/%s",module_prefix,intro_file))<0) {
-
 
813
            force_setvar(ro_name[ro_cmd],commands[cmd_new]);
-
 
814
            return;
-
 
815
          }
-
 
816
          p=getvar("wims_session");
-
 
817
          if(p!=NULL && *p!=0) {
-
 
818
            if(set_session_prefix()==0) check_session();
-
 
819
            else {
-
 
820
                trap_check(p);
-
 
821
                if(strchr(p,'_')!=NULL && strchr(p,'/')==NULL) {
-
 
822
                  get_static_session_var();
-
 
823
                }
-
 
824
            }
-
 
825
          }
-
 
826
/* determine http protocol name. How to detect? */
-
 
827
          p=getenv("HTTPS"); if(p!=NULL && strcmp(p,"on")==0) {
-
 
828
            protocol="https"; set_protocol();
-
 
829
          }
-
 
830
          force_setvar("wims_protocol",protocol);
-
 
831
          determine_font(lang);
-
 
832
          determine_dirn(lang);
-
 
833
          main_phtml_put(intro_file); debug_output();
-
 
834
          introend: write_logs();free(var_str);
-
 
835
          delete_pid(); exit(0);
-
 
836
      }
-
 
837
      case cmd_ref: {
-
 
838
          set_module_prefix();
-
 
839
          default_form_method="get";
-
 
840
          p=getvar("wims_session");
-
 
841
          if(p!=NULL && *p!=0) {
-
 
842
            if(set_session_prefix()==0) check_session();
-
 
843
            else trap_check(p);
-
 
844
          }
-
 
845
          determine_font(lang);
-
 
846
          determine_dirn(lang);
-
 
847
          main_phtml_put(ref_file); goto introend;
-
 
848
      }
-
 
849
      case cmd_getins: {
851
    case cmd_getins: {
850
          c=getvar(ro_name[ro_special_parm]);
852
      c=getvar(ro_name[ro_special_parm]);
851
          if(c==NULL || *c==0) {
853
      if(c==NULL || *c==0) {
852
            user_error_nolog=1; user_error("no_insnum");
854
        user_error_nolog=1; user_error("no_insnum");
853
          }
855
      }
854
          if(*c=='/' || strstr(c,"..")!=NULL) goto badins;
856
      if(*c=='/' || strstr(c,"..")!=NULL) goto badins;
855
          set_session_prefix();
857
      set_session_prefix();
856
          if(strstr(session_prefix,"robot")!=NULL) exit(0);
858
      if(strstr(session_prefix,"robot")!=NULL) exit(0);
857
          l=filelength("%s/%s",s2_prefix,c);
859
      l=filelength("%s/%s",s2_prefix,c);
858
          if(l<0) {
860
      if(l<0) {
859
            badins: user_error_nolog=1; user_error("bad_insnum");
861
        badins: user_error_nolog=1; user_error("bad_insnum");
860
          }
862
      }
861
          {
863
      {
862
            char *fmt;
864
        char *fmt;
863
            fmt=strchr(c,'.');
865
        fmt=strchr(c,'.');
864
            if(fmt==NULL) {
866
        if(fmt==NULL) {
865
                user_error_nolog=1; user_error("bad_insnum");
867
            user_error_nolog=1; user_error("bad_insnum");
866
            }
868
        }
867
            else fmt++;
869
        else fmt++;
868
 
870
 
869
            nph_header(200);
871
        nph_header(200);
870
/* insert format problem; bricolage */
872
/* insert format problem; bricolage */
871
            printf("Content-type: image/%s\r\n\
873
        printf("Content-type: image/%s\r\n\
872
Content-length: %ld\r\n\r\n",fmt,l);
874
Content-length: %ld\r\n\r\n",fmt,l);
873
            catfile(stdout,"%s/%s",s2_prefix,c); exit(0);
875
        catfile(stdout,"%s/%s",s2_prefix,c); exit(0);
874
          }
-
 
875
      }
876
      }
-
 
877
    }
876
      case cmd_getfile: {
878
    case cmd_getfile: {
877
          char fname[MAX_FNAME+1];
879
      char fname[MAX_FNAME+1];
878
          c=getvar(ro_name[ro_special_parm]);
880
      c=getvar(ro_name[ro_special_parm]);
879
          if(c==NULL || *c==0) {
881
      if(c==NULL || *c==0) {
880
            user_error_nolog=1; user_error("no_insnum");
882
        user_error_nolog=1; user_error("no_insnum");
881
          }
883
      }
882
          if(*c=='/' || strstr(c,"..")!=NULL) goto badfile;
884
      if(*c=='/' || strstr(c,"..")!=NULL) goto badfile;
883
 
885
 
884
          set_session_prefix();
886
      set_session_prefix();
885
          if(strstr(session_prefix,"robot")!=NULL) exit(0);
887
      if(strstr(session_prefix,"robot")!=NULL) exit(0);
886
          mkfname(fname,"%s/getfile/%s",session_prefix,c);
888
      mkfname(fname,"%s/getfile/%s",session_prefix,c);
887
          l=filelength("%s",fname);
889
      l=filelength("%s",fname);
888
          if(l<0 && strchr(session_prefix,'_')!=NULL) {
890
      if(l<0 && strchr(session_prefix,'_')!=NULL) {
889
            char *pt;
891
        char *pt;
890
            mystrncpy(fname,session_prefix,sizeof(fname));
892
        mystrncpy(fname,session_prefix,sizeof(fname));
891
            pt=strrchr(fname,'_'); if(pt) *pt=0;
893
        pt=strrchr(fname,'_'); if(pt) *pt=0;
892
            snprintf(fname+(pt-fname),sizeof(fname)-(pt-fname),
894
        snprintf(fname+(pt-fname),sizeof(fname)-(pt-fname),
893
                  "/getfile/%s",c);
895
              "/getfile/%s",c);
894
            l=filelength("%s",fname);
896
        l=filelength("%s",fname);
895
          }
897
      }
896
          if(l<0) {
898
      if(l<0) {
897
            badfile: user_error_nolog=1; user_error("bad_insnum");
899
        badfile: user_error_nolog=1; user_error("bad_insnum");
898
          }
900
      }
899
          if(l>512*1024) {
901
      if(l>512*1024) {
900
            struct rlimit rlim;
902
        struct rlimit rlim;
901
            rlimit_cpu*=l/(10*1024);
903
        rlimit_cpu*=l/(10*1024);
902
            rlim.rlim_cur=rlim.rlim_max=rlimit_cpu;
904
        rlim.rlim_cur=rlim.rlim_max=rlimit_cpu;
903
            initalarm();
905
          initalarm();
904
          }
906
      }
905
          {
907
      {
906
            char *p1;
908
        char *p1;
907
            char mime[MAX_LINELEN+1];
909
        char mime[MAX_LINELEN+1];
908
            for(p1=c+strlen(c);p1>c && isalpha(*(p1-1)); p1--);
910
        for(p1=c+strlen(c);p1>c && isalpha(*(p1-1)); p1--);
909
            ovlstrcpy(mime,"application/octet-stream");
911
        ovlstrcpy(mime,"application/octet-stream");
910
            if(p1>c && *(p1-1)=='.') {
912
        if(p1>c && *(p1-1)=='.') {
911
                setvar("translator_unknown",mime);
913
          setvar("translator_unknown",mime);
912
                setvar("dictionary","bases/sys/mime");
914
          setvar("dictionary","bases/sys/mime");
913
                snprintf(mime,sizeof(mime),"translator %s",p1);
915
          snprintf(mime,sizeof(mime),"translator %s",p1);
914
                calc_exec(mime);
916
          calc_exec(mime);
915
            }
917
        }
916
            nph_header(200);
918
        nph_header(200);
917
            printf("Content-type: %s\r\n\
919
        printf("Content-type: %s\r\n\
918
Content-length: %ld\r\n\r\n",mime,l);
920
Content-length: %ld\r\n\r\n",mime,l);
919
            catfile(stdout,"%s",fname); exit(0);
921
        catfile(stdout,"%s",fname); exit(0);
920
          }
-
 
921
      }
922
      }
-
 
923
    }
922
      case cmd_close: {
924
      case cmd_close: {
923
          char *p, b2[32]; int w;
925
        char *p, b2[32]; int w;
924
          char nbuf[MAX_FNAME+1], vbuf[MAX_LINELEN+1];
926
        char nbuf[MAX_FNAME+1], vbuf[MAX_LINELEN+1];
925
          p=getvar(ro_name[ro_session]);
927
        p=getvar(ro_name[ro_session]);
926
          if(p==NULL || strlen(p)<10 ||
928
        if(p==NULL || strlen(p)<10 ||
927
             strchr(p,'/')!=NULL) return;
929
           strchr(p,'/')!=NULL) return;
928
          mystrncpy(b2,p,sizeof(b2));
930
        mystrncpy(b2,p,sizeof(b2));
929
          p=strchr(b2,'.'); if(p!=NULL) *p=0;
931
        p=strchr(b2,'.'); if(p!=NULL) *p=0;
930
          mkfname(nbuf,"%s/%s/var.stat",session_dir,b2);
932
        mkfname(nbuf,"%s/%s/var.stat",session_dir,b2);
931
          getdef(nbuf,"wims_caller",vbuf);
933
        getdef(nbuf,"wims_caller",vbuf);
932
          if(vbuf[0]!=0) force_setvar(ro_name[ro_session],vbuf);
934
        if(vbuf[0]!=0) force_setvar(ro_name[ro_session],vbuf);
933
          w=wrapexec; wrapexec=1;
935
        w=wrapexec; wrapexec=1;
934
          call_sh("rm -Rf %s/%s* %s/%s* >/dev/null 2>&1",session_dir,b2,s2_dir,b2);
936
        call_sh("rm -Rf %s/%s* %s/%s* >/dev/null 2>&1",session_dir,b2,s2_dir,b2);
935
          wrapexec=w; cookiegot[0]=0;
937
        wrapexec=w; cookiegot[0]=0;
936
          force_setvar(ro_name[ro_cmd],"new");
938
        force_setvar(ro_name[ro_cmd],"new");
937
      }
939
      }
938
      default: return;
940
      default: return;
939
    }
941
  }
940
}
942
}
941
 
943
 
942
/* This is run only when manually invoking the program.
944
/* This is run only when manually invoking the program.
943
 * Verifies the orderedness of various list tables.
945
 * Verifies the orderedness of various list tables.
944
 */
946
 */
Line 1020... Line 1022...
1020
 
1022
 
1021
void readstdin(int len)
1023
void readstdin(int len)
1022
{
1024
{
1023
    int ll, l1, lt, lr;
1025
    int ll, l1, lt, lr;
1024
    int cpulim;
1026
    int cpulim;
1025
 
1027
 
1026
    cpulim=rlimit_cpu; rlimit_cpu=3;
1028
    cpulim=rlimit_cpu; rlimit_cpu=3;
1027
    lr=len; l1=0;
1029
    lr=len; l1=0;
1028
    while(lr>0) {
1030
    while(lr>0) {
1029
      nowtime=time(0); initalarm();
1031
      nowtime=time(0); initalarm();
1030
      ll=lr; if(ll>READSTDIN_WINDOW) ll=READSTDIN_WINDOW;
1032
      ll=lr; if(ll>READSTDIN_WINDOW) ll=READSTDIN_WINDOW;
Line 1243... Line 1245...
1243
    special_cmds();
1245
    special_cmds();
1244
    parse_ro_names();
1246
    parse_ro_names();
1245
    manager_check();
1247
    manager_check();
1246
    access_check(0);
1248
    access_check(0);
1247
/* it is really for developing in wims, so no use in general */
1249
/* it is really for developing in wims, so no use in general */
1248
   if (1==1) {
1250
  if (1==1) {
1249
    if(strstr(tmp_debug,"yes")!=NULL && checkhost(manager_site)>=1)
1251
    if(strstr(tmp_debug,"yes")!=NULL && checkhost(manager_site)>=1)
1250
       trace_file = fopen(mkfname(NULL,"%s/%s",tmp_dir,"trace.txt"),"a");
1252
       trace_file = fopen(mkfname(NULL,"%s/%s",tmp_dir,"trace.txt"),"a");
1251
   }
1253
  }
1252
    set_variables();
1254
  set_variables();
1253
    determine_font(getvar("module_language"));
1255
  determine_font(getvar("module_language"));
1254
    determine_dirn(getvar("module_language"));
1256
  determine_dirn(getvar("module_language"));
1255
    if(!robot_access && session_prefix[0]!=0 && cmd_type!=cmd_help && !ismhelp)
1257
  if(!robot_access && session_prefix[0]!=0 && cmd_type!=cmd_help && !ismhelp)
1256
      lastout_file=creat(mkfname(NULL,"%s/%s",s2_prefix,lastout),
1258
    lastout_file=creat(mkfname(NULL,"%s/%s",s2_prefix,lastout),
1257
                   S_IRUSR|S_IWUSR);
1259
                 S_IRUSR|S_IWUSR);
1258
    p=getvar("module_score");
1260
  p=getvar("module_score");
1259
    if(p!=NULL) force_setvar("wims_seed_score",p);
1261
  if(p!=NULL) force_setvar("wims_seed_score",p);
1260
    p=getvar("module_category");
1262
  p=getvar("module_category");
1261
    if(p==NULL || strstr(p,"tool")==NULL) default_form_method="get";
1263
  if(p==NULL || strstr(p,"tool")==NULL) default_form_method="get";
1262
    if(noout) {
1264
  if(noout) {
1263
      write_logs(); save_session_vars();
1265
    write_logs(); save_session_vars();
1264
      goto outgo;
1266
    goto outgo;
1265
    }
1267
  }
1266
    if(ismhelp) {
1268
  if(ismhelp) {
1267
      mhelp();
1269
    mhelp();
1268
    }
1270
  }
1269
    else {
1271
  else {
1270
      main_phtml_put(html_file);
1272
    main_phtml_put(html_file);
1271
      if(lastout_file!=-1) {
1273
    if(lastout_file!=-1) {
1272
          flushoutput(); close(lastout_file); putlastout();
1274
        flushoutput(); close(lastout_file); putlastout();
1273
      }
1275
    }
1274
      write_logs(); save_session_vars();
1276
    write_logs(); save_session_vars();
1275
    }
1277
  }
1276
    outgo:
1278
  outgo:
1277
    debug_output();
1279
  debug_output();
1278
    if (trace_file)  { fclose(trace_file); trace_file=NULL; }
1280
  if (trace_file)  { fclose(trace_file); trace_file=NULL; }
1279
    if(var_str!=stdinbuf) free(var_str);
1281
  if(var_str!=stdinbuf) free(var_str);
1280
    delete_pid();
1282
  delete_pid();
1281
    if(mode!=mode_popup && trusted_module()) {
1283
  if(mode!=mode_popup && trusted_module()) {
1282
      p=getvar("wims_mode");
1284
    p=getvar("wims_mode");
1283
      if(p!=NULL && strcmp(p,"popup")==0) mode=mode_popup;
1285
    if(p!=NULL && strcmp(p,"popup")==0) mode=mode_popup;
1284
    }
1286
  }
1285
    if(mode==mode_popup && insert_no==0) {
1287
  if(mode==mode_popup && insert_no==0) {
1286
      p=getvar("wims_mode");
1288
    p=getvar("wims_mode");
1287
      if(p!=NULL && strcmp(p,"popup")==0) {
1289
    if(p!=NULL && strcmp(p,"popup")==0) {
1288
          remove_tree(session_prefix);
1290
      remove_tree(session_prefix);
1289
 
1291
 
1290
          remove_tree(s2_prefix);
1292
      remove_tree(s2_prefix);
1291
      }
-
 
1292
    }
1293
    }
-
 
1294
  }
1293
    return 0;
1295
  return 0;
1294
}
1296
}