Subversion Repositories wimsdev

Rev

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

Rev 12010 Rev 15407
Line 25... Line 25...
25
 * Threshold 1: anonymous new session refused.
25
 * Threshold 1: anonymous new session refused.
26
 * Threshold 2: New session and anonymous request refused.
26
 * Threshold 2: New session and anonymous request refused.
27
 */
27
 */
28
void check_load(int th)
28
void check_load(int th)
29
{
29
{
30
    int load, pload;
30
  int load, pload;
31
    char *p1, *p2, buf[64];
31
  char *p1, *p2, buf[64];
32
    char *pp;
32
  char *pp;
33
    double dload;
33
  double dload;
34
 
34
 
35
    pload=0; pp=strchr(loadavg,'/'); if(pp) {
35
  pload=0; pp=strchr(loadavg,'/'); if(pp) {
36
     for(;pp>loadavg && isdigit(pp[-1]); pp--);
36
    for(;pp>loadavg && isdigit(pp[-1]); pp--);
37
     pload=atoi(pp);
37
    pload=atoi(pp);
38
     if(pload*12>threshold2+3) {
38
    if(pload*12>threshold2+3) {
39
         pload_refuse:
39
      pload_refuse:
40
         refuse_log(pload+100); user_error("threshold");
40
      refuse_log(pload+100); user_error("threshold");
41
     }
-
 
42
    }
41
    }
-
 
42
  }
43
    if(ispriority) goto repcheck; /* priority connections will not be refused. */
43
  if(ispriority) goto repcheck; /* priority connections will not be refused. */
44
    if(pload*20>threshold1+2) goto pload_refuse;
44
  if(pload*20>threshold1+2) goto pload_refuse;
45
    if(th<0 || th>2) goto repcheck;
45
  if(th<0 || th>2) goto repcheck;
46
/* Operating system load average facility */
46
  /* Operating system load average facility */
47
    if(robot_access && loadavg[0]==0) goto refuse;
47
  if(robot_access && loadavg[0]==0) goto refuse;
48
    if(loadavg[0]==0) goto repcheck;
48
  if(loadavg[0]==0) goto repcheck;
49
    p1=find_word_start(loadavg); p2=find_word_end(p1);*p2=0;
49
  p1=find_word_start(loadavg); p2=find_word_end(p1);*p2=0;
50
    dload=atof(p1);
50
  dload=atof(p1);
51
    if(robot_access &&
-
 
52
       (!isfinite(dload) || dload>1000 || dload<0 || dload*200>threshold1))
51
  if(robot_access && (!isfinite(dload) || dload>1000 || dload<0 || dload*200>threshold1))
53
      goto refuse;
52
    goto refuse;
54
    if(!isfinite(dload) || dload<=0 || dload>1000) goto repcheck; /* unreasonable */
53
  if(!isfinite(dload) || dload<=0 || dload>1000) goto repcheck; /* unreasonable */
55
/* very small 1 min load average */
54
  /* very small 1 min load average */
56
    if(dload*200<threshold1) goto repcheck;
55
  if(dload*200<threshold1) goto repcheck;
57
    if(dload*50>threshold2) goto refuse;
56
  if(dload*50>threshold2) goto refuse;
58
    p1=find_word_start(p2+1); /* go to second average: 5 min. */
57
  p1=find_word_start(p2+1); /* go to second average: 5 min. */
59
    *find_word_end(p1)=0;
58
  *find_word_end(p1)=0;
60
    dload=atof(p1);
59
  dload=atof(p1);
61
    if(!isfinite(dload) || dload<=0 || dload>1000) goto repcheck; /* unreasonable */
60
  if(!isfinite(dload) || dload<=0 || dload>1000) goto repcheck; /* unreasonable */
62
    load=dload*100;
61
  load=dload*100;
63
    snprintf(buf,sizeof(buf),"%d",load);
62
  snprintf(buf,sizeof(buf),"%d",load);
64
    setvar("wims_server_load",buf);
63
  setvar("wims_server_load",buf);
65
/* cut cpu allowance to 3/4 or half if load is high.
64
  /* cut cpu allowance to 3/4 or half if load is high.
66
 * But alarm time is not changed */
65
   * But alarm time is not changed */
67
    if(load*3>=threshold1*2) {
66
  if(load*3>=threshold1*2) {
68
     struct rlimit rlim;
67
    struct rlimit rlim;
69
     rlimit_cpu=(3*rlimit_cpu+1)/4;
68
    rlimit_cpu=(3*rlimit_cpu+1)/4;
70
     if(load>=threshold1) rlimit_cpu=(3*rlimit_cpu+1)/4;
69
    if(load>=threshold1) rlimit_cpu=(3*rlimit_cpu+1)/4;
71
     rlim.rlim_cur=rlim.rlim_max=rlimit_cpu;
70
    rlim.rlim_cur=rlim.rlim_max=rlimit_cpu;
72
     setrlimit(RLIMIT_CPU,&rlim);
71
    setrlimit(RLIMIT_CPU,&rlim);
73
    }
72
  }
74
    if((th==0 && load*2>threshold1) ||
73
  if((th==0 && load*2>threshold1) ||
75
       (th==1 && load>threshold1) || (th==2 && load>threshold2)) {
74
     (th==1 && load>threshold1) || (th==2 && load>threshold2)) {
76
     refuse:
75
    refuse:
77
     if(new_session && *session_prefix!=0) {
76
  if(new_session && *session_prefix!=0) {
78
         remove_tree(session_prefix); remove_tree(s2_prefix);
-
 
79
     }
-
 
80
     refuse_log(th); user_error("threshold");
-
 
81
    }
-
 
82
    repcheck:
-
 
83
    if(robot_access) return;
-
 
84
    if(new_session && *session_prefix!=0 && *remote_addr
-
 
85
       && hostcquota && strcmp(remote_addr,"127.0.0.1")!=0
-
 
86
       && !ispriority) {
-
 
87
/* overload: */
-
 
88
     remove_tree(session_prefix); remove_tree(s2_prefix);
77
      remove_tree(session_prefix); remove_tree(s2_prefix);
89
     user_error("overload");
-
 
90
    }
78
    }
-
 
79
    refuse_log(th); user_error("threshold");
-
 
80
  }
-
 
81
  repcheck:
-
 
82
  if(robot_access) return;
-
 
83
  if(new_session && *session_prefix!=0 && *remote_addr
-
 
84
     && hostcquota && strcmp(remote_addr,"127.0.0.1")!=0
-
 
85
     && !ispriority) {
-
 
86
    /* overload: */
-
 
87
    remove_tree(session_prefix); remove_tree(s2_prefix);
-
 
88
    user_error("overload");
-
 
89
  }
91
}
90
}
92
 
91
 
93
/* User authentification routine, obsolete */
92
/* User authentification routine, obsolete */
94
void auth(void)
93
void auth(void)
95
{
94
{
96
    check_load(1); return;
95
  check_load(1); return;
97
}
96
}
98
 
97
 
99
#define rafinfono 10
98
#define rafinfono 10
-
 
99
 
-
 
100
/* check rapidfire information */
-
 
101
void checkrafale(void)
-
 
102
{
-
 
103
  char *p, *p1, *p2, *sh, *u;
-
 
104
  char rbuf[MAX_LINELEN+1];
-
 
105
  time_t rr, rafinfo[rafinfono];
-
 
106
  int i, t, mm, rafinfocnt;
-
 
107
  double coef=0.23;
100
 
108
 
101
/* check rapidfire information */
-
 
102
void checkrafale(void)
-
 
103
{
-
 
104
    char *p, *p1, *p2, *sh, *u;
-
 
105
    char rbuf[MAX_LINELEN+1];
-
 
106
    time_t rr, rafinfo[rafinfono];
-
 
107
    int i, t, mm, rafinfocnt;
-
 
108
    double coef=0.23;
-
 
109
 
-
 
110
    if(rafalvl<=0) return;
109
  if(rafalvl<=0) return;
111
    p=getvar("module_scoring"); if(p==NULL || strcasecmp(p,"yes")!=0) return;
110
  p=getvar("module_scoring"); if(p==NULL || strcasecmp(p,"yes")!=0) return;
112
    u=getvar("wims_user"); if(u!=NULL && strcmp(u,"supervisor")==0) return;
111
  u=getvar("wims_user"); if(u!=NULL && strcmp(u,"supervisor")==0) return;
113
    p=getvar("wims_developer"); if(p!=NULL && *p!=0) return;
112
  p=getvar("wims_developer"); if(p!=NULL && *p!=0) return;
114
    p=getenv("REMOTE_ADDR");if(p!=NULL && strcmp(p,"127.0.0.1")==0) return;
113
  p=getenv("REMOTE_ADDR");if(p!=NULL && strcmp(p,"127.0.0.1")==0) return;
115
    p=getvar("session"); if(p!=NULL && strstr(p,"_exam")!=NULL) return;
114
  p=getvar("session"); if(p!=NULL && strstr(p,"_exam")!=NULL) return;
116
    sh=getvar("wims_sheet"); if(sh!=NULL && *sh>'0') coef*=3;
115
  sh=getvar("wims_sheet"); if(sh!=NULL && *sh>'0') coef*=3;
117
    p=getvar("wims_rafale"); if(p==NULL) p="";
116
  p=getvar("wims_rafale"); if(p==NULL) p="";
118
    mm=0;
117
  mm=0;
119
    for(p1=find_word_start(p),i=0;i<rafinfono && *p1;p1=find_word_start(p2)) {
118
  for(p1=find_word_start(p),i=0;i<rafinfono && *p1;p1=find_word_start(p2)) {
120
     p2=find_word_end(p1); if(*p2) *p2++=0;
119
   p2=find_word_end(p1); if(*p2) *p2++=0;
121
     rr=atol(p1); if(rr<=0 || rr>nowtime) continue;
120
   rr=atol(p1); if(rr<=0 || rr>nowtime) continue;
122
     t=coef*rafalvl*pow(i,1+rafalvl*0.05)-(nowtime-rr); if(t>mm) mm=t;
121
   t=coef*rafalvl*pow(i,1+rafalvl*0.05)-(nowtime-rr); if(t>mm) mm=t;
123
     rafinfo[i++]=rr;
122
   rafinfo[i++]=rr;
124
    }
123
  }
125
    if(mm>0) {
124
  if(mm>0) {
126
     if(u!=NULL && *u!=0) user_log("rafale");
125
    if(u!=NULL && *u!=0) user_log("rafale");
127
     user_error("rafale");
126
    user_error("rafale");
128
    }
127
  }
129
    rafinfocnt=i;
128
  rafinfocnt=i;
130
    snprintf(rbuf,sizeof(rbuf),"%lu",nowtime);
129
  snprintf(rbuf,sizeof(rbuf),"%lu",nowtime);
131
    for(i=0;i<rafinfocnt;i++) {
130
  for(i=0;i<rafinfocnt;i++) {
132
     snprintf(rbuf+strlen(rbuf),sizeof(rbuf)-strlen(rbuf),
131
    snprintf(rbuf+strlen(rbuf),sizeof(rbuf)-strlen(rbuf),
133
           " %lu",rafinfo[i]);
132
         " %lu",rafinfo[i]);
134
    }
133
  }
135
    force_setvar("wims_rafale",rbuf);
134
  force_setvar("wims_rafale",rbuf);
136
}
135
}
137
 
136
 
138
/* when score is got: erase 2 rafale information. */
137
/* when score is got: erase 2 rafale information. */
139
void lessrafale(void)
138
void lessrafale(void)
140
{
139
{
141
    char *p;
140
  char *p;
142
    double s;
141
  double s;
143
    int i;
142
  int i;
144
    p=getvar("module_score"); if(p==NULL) return;
143
  p=getvar("module_score"); if(p==NULL) return;
145
    s=atof(p); if(s<3) return;
144
  s=atof(p); if(s<3) return;
146
    p=getvar("wims_rafale"); if(p==NULL || *p==0) return;
145
  p=getvar("wims_rafale"); if(p==NULL || *p==0) return;
147
    for(i=0;i<2;i++) p=find_word_end(find_word_start(p));
146
  for(i=0;i<2;i++) p=find_word_end(find_word_start(p));
148
    p=find_word_start(p);
147
  p=find_word_start(p);
149
    force_setvar("wims_rafale",p);
148
  force_setvar("wims_rafale",p);
150
}
149
}
151
 
150
 
152
#define ac_class 0x1 /* class access */
151
#define ac_class 0x1  /* class access */
153
#define ac_exo   0x2 /* access to exercises */
152
#define ac_exo   0x2  /* access to exercises */
154
#define ac_tool  0x4 /* access to tools */
153
#define ac_tool  0x4  /* access to tools */
155
#define ac_recre 0x8 /* access to recreations */
154
#define ac_recre 0x8  /* access to recreations */
156
#define ac_doc  0x10 /* access to documents */
155
#define ac_doc  0x10  /* access to documents */
157
#define ac_local 0x20 /* access to local modules */
156
#define ac_local 0x20 /* access to local modules */
158
#define ac_com   0x40 /* access to commercial modules */
157
#define ac_com   0x40 /* access to commercial modules */
159
#define ac_hint  0x80   /* hint command */
158
#define ac_hint  0x80 /* hint command */
160
#define ac_sheet 0x100  /* use within a worksheet */
159
#define ac_sheet 0x100/* use within a worksheet */
161
#define ac_exam  0x200  /* work during an exam */
160
#define ac_exam  0x200/* work during an exam */
162
 
161
 
163
/* Check site's access policy. */
162
/* Check site's access policy. */
164
void access_check(int isclass)
163
void access_check(int isclass)
165
{
164
{
166
    char *p, *p1, *p2, *p3, *pp1, *pp2;
165
  char *p, *p1, *p2, *p3, *pp1, *pp2;
167
    char cbuf[MAX_LINELEN+1];
166
  char cbuf[MAX_LINELEN+1];
168
    long int thisaccess, lineaccess, linepol, thispol;
167
  long int thisaccess, lineaccess, linepol, thispol;
169
    int non, refuse;
168
  int non, refuse;
170
 
169
 
171
    if(manageable>=2 || robot_access) return;
170
  if(manageable>=2 || robot_access) return;
172
    thisaccess=0;
171
  thisaccess=0;
173
    p=getvar(ro_name[ro_module]); if(p==NULL || *p==0) return;
172
  p=getvar(ro_name[ro_module]); if(p==NULL || *p==0) return;
174
    if(strncmp(p,"adm/doc",7)==0) thisaccess|=ac_doc;
173
  if(strncmp(p,"adm/doc",7)==0) thisaccess|=ac_doc;
175
    else if(strncmp(p,"adm/",4)==0 || strcmp(p,home_module)==0) return;
174
  else if(strncmp(p,"adm/",4)==0 || strcmp(p,home_module)==0) return;
176
    if(strncmp(p,"local/",6)==0) thisaccess|=ac_local;
175
  if(strncmp(p,"local/",6)==0) thisaccess|=ac_local;
177
    if(strncmp(p,"com/",4)==0) thisaccess|=ac_com;
176
  if(strncmp(p,"com/",4)==0) thisaccess|=ac_com;
178
    p=getvar("wims_user");
177
  p=getvar("wims_user");
179
    if(p!=NULL && *p!=0) {
178
  if(p!=NULL && *p!=0) {
180
     if(!isclass && strcmp(p,"supervisor")!=0) access_check(1);
179
    if(!isclass && strcmp(p,"supervisor")!=0) access_check(1);
181
     thisaccess|=ac_class;
180
    thisaccess|=ac_class;
182
    }
181
  }
183
    if(isclass) {
182
  if(isclass) {
184
     if(class_dir[0]==0) return;
183
    if(class_dir[0]==0) return;
185
     accessfile(cbuf,"r","%s/access.conf",class_dir);
184
    accessfile(cbuf,"r","%s/access.conf",class_dir);
186
    }
185
  }
187
    else accessfile(cbuf,"r",ACCESS_CONF);
186
  else accessfile(cbuf,"r",ACCESS_CONF);
188
    if(cbuf[0]==0) return;
187
  if(cbuf[0]==0) return;
189
/* access limited */
188
    /* access limited */
190
    if(cmd_type==cmd_hint) thisaccess|=ac_hint;
189
    if(cmd_type==cmd_hint) thisaccess|=ac_hint;
191
    p1=getvar("wims_accessright"); if(p1!=NULL && *p1!=0) {
190
    p1=getvar("wims_accessright"); if(p1!=NULL && *p1!=0) {
192
     p=getvar(ro_name[ro_module]);
191
      p=getvar(ro_name[ro_module]);
193
     for(p1=find_word_start(p1);*p1; p1=find_word_start(p2)) {
192
      for(p1=find_word_start(p1);*p1; p1=find_word_start(p2)) {
194
         p2=find_word_end(p1);
193
        p2=find_word_end(p1);
195
         if(strncmp(p,p1,p2-p1)==0) return;
194
        if(strncmp(p,p1,p2-p1)==0) return;
196
     }
195
      }
197
    }
196
    }
198
    p=getvar("module_category"); if(p) {
197
    p=getvar("module_category"); if(p) {
199
     if(strstr(p,"exercise")!=NULL) thisaccess|=ac_exo;
198
      if(strstr(p,"exercise")!=NULL) thisaccess|=ac_exo;
200
     if(strstr(p,"tool")!=NULL) thisaccess|=ac_tool;
199
      if(strstr(p,"tool")!=NULL) thisaccess|=ac_tool;
201
     if(strstr(p,"recre")!=NULL) thisaccess|=ac_recre;
200
      if(strstr(p,"recre")!=NULL) thisaccess|=ac_recre;
202
     if(strstr(p,"doc")!=NULL) thisaccess|=ac_doc;
201
      if(strstr(p,"doc")!=NULL) thisaccess|=ac_doc;
203
    }
202
    }
204
    for(p1=find_word_start(cbuf);*p1;p1=find_word_start(p2)) {
203
    for(p1=find_word_start(cbuf);*p1;p1=find_word_start(p2)) {
205
     p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1);
204
    p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1);
206
     if(!myisalpha(*p1)) continue;
205
    if(!myisalpha(*p1)) continue;
207
     p3=strchr(p1,':'); if(p3==NULL) continue;
206
    p3=strchr(p1,':'); if(p3==NULL) continue;
208
     *p3++=0; p3=find_word_start(p3); strip_trailing_spaces(p3);
207
    *p3++=0; p3=find_word_start(p3); strip_trailing_spaces(p3);
209
     refuse=0; if(*p3=='!') {
208
    refuse=0;
210
         p3=find_word_start(p3+1); refuse=1;
209
    if(*p3=='!') { p3=find_word_start(p3+1); refuse=1;}
211
     }
-
 
212
     if(*p3 && checkhostt(p3)==0) continue;
210
    if(*p3 && checkhostt(p3)==0) continue;
213
     for(p=p1; *p; p++) {
211
    for(p=p1; *p; p++) {
214
         if(myisalpha(*p)) *p=tolower(*p);
212
      if(myisalpha(*p)) *p=tolower(*p); else *p=' ';
215
         else *p=' ';
-
 
216
     }
213
    }
217
     lineaccess=thisaccess; linepol=0;
214
    lineaccess=thisaccess; linepol=0;
218
     for(pp1=find_word_start(p1); *pp1; pp1=find_word_start(pp2)) {
215
    for(pp1=find_word_start(p1); *pp1; pp1=find_word_start(pp2)) {
219
         pp2=find_word_end(pp1); if(*pp2) *pp2++=0;
216
      pp2=find_word_end(pp1); if(*pp2) *pp2++=0;
220
         if(strncmp(pp1,"non",3)==0) {
217
      if(strncmp(pp1,"non",3)==0) {
221
          pp1=find_word_start(pp1+3); non=1;
218
        pp1=find_word_start(pp1+3); non=1;
222
         }
-
 
223
         else non=0;
219
      }  else non=0;
224
         thispol=0;
220
      thispol=0;
225
         if(strcmp(pp1,"class")==0) {thispol=ac_class; goto nxt;}
221
      if(strcmp(pp1,"class")==0) {thispol=ac_class; goto nxt;}
226
         if(strcmp(pp1,"exo")==0) {thispol=ac_exo; goto nxt;}
222
      if(strcmp(pp1,"exo")==0) {thispol=ac_exo; goto nxt;}
227
         if(strcmp(pp1,"exercise")==0) {thispol=ac_exo; goto nxt;}
223
      if(strcmp(pp1,"exercise")==0) {thispol=ac_exo; goto nxt;}
228
         if(strcmp(pp1,"tool")==0) {thispol=ac_tool; goto nxt;}
224
      if(strcmp(pp1,"tool")==0) {thispol=ac_tool; goto nxt;}
229
         if(strcmp(pp1,"recre")==0) {thispol=ac_recre; goto nxt;}
225
      if(strcmp(pp1,"recre")==0) {thispol=ac_recre; goto nxt;}
230
         if(strcmp(pp1,"recreation")==0) {thispol=ac_recre; goto nxt;}
226
      if(strcmp(pp1,"recreation")==0) {thispol=ac_recre; goto nxt;}
231
         if(strcmp(pp1,"doc")==0) {thispol=ac_doc; goto nxt;}
227
      if(strcmp(pp1,"doc")==0) {thispol=ac_doc; goto nxt;}
232
         if(strcmp(pp1,"document")==0) {thispol=ac_doc; goto nxt;}
228
      if(strcmp(pp1,"document")==0) {thispol=ac_doc; goto nxt;}
233
         if(strcmp(pp1,"local")==0) {thispol=ac_local; goto nxt;}
229
      if(strcmp(pp1,"local")==0) {thispol=ac_local; goto nxt;}
234
         if(strcmp(pp1,"com")==0) {thispol=ac_com; goto nxt;}
230
      if(strcmp(pp1,"com")==0) {thispol=ac_com; goto nxt;}
235
         if(strcmp(pp1,"hint")==0) {thispol=ac_hint; goto nxt;}
231
      if(strcmp(pp1,"hint")==0) {thispol=ac_hint; goto nxt;}
236
         nxt:
232
      nxt:
237
         if(thispol==0) continue;
233
      if(thispol==0) continue;
238
         if(non) lineaccess^=thispol;
234
      if(non) lineaccess^=thispol;
239
         linepol|=thispol;
235
      linepol|=thispol;
240
     }
-
 
241
     if(linepol==0 || (linepol&lineaccess)!=linepol) continue;
-
 
242
     if(refuse) user_error("no_access");
-
 
243
     else return;
-
 
244
    }
236
    }
-
 
237
    if(linepol==0 || (linepol&lineaccess)!=linepol) continue;
-
 
238
    if(refuse) user_error("no_access"); else return;
-
 
239
  }
245
/* end of limited access */
240
  /* end of limited access */
246
}
241
}
247
 
242