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 |
|
30 | int load, pload; |
31 |
|
31 | char *p1, *p2, buf[64]; |
32 |
|
32 | char *pp; |
33 |
|
33 | double dload; |
34 | 34 | ||
35 |
|
35 | pload=0; pp=strchr(loadavg,'/'); if(pp) { |
36 |
|
36 | for(;pp>loadavg && isdigit(pp[-1]); pp--); |
37 |
|
37 | pload=atoi(pp); |
38 |
|
38 | if(pload*12>threshold2+3) { |
39 |
|
39 | pload_refuse: |
40 |
|
40 | refuse_log(pload+100); user_error("threshold"); |
41 | } |
- | |
42 | } |
41 | } |
- | 42 | } |
|
43 |
|
43 | if(ispriority) goto repcheck; /* priority connections will not be refused. */ |
44 |
|
44 | if(pload*20>threshold1+2) goto pload_refuse; |
45 |
|
45 | if(th<0 || th>2) goto repcheck; |
46 | /* Operating system load average facility */ |
46 | /* Operating system load average facility */ |
47 |
|
47 | if(robot_access && loadavg[0]==0) goto refuse; |
48 |
|
48 | if(loadavg[0]==0) goto repcheck; |
49 |
|
49 | p1=find_word_start(loadavg); p2=find_word_end(p1);*p2=0; |
50 |
|
50 | dload=atof(p1); |
51 | if(robot_access && |
- | |
52 |
|
51 | if(robot_access && (!isfinite(dload) || dload>1000 || dload<0 || dload*200>threshold1)) |
53 |
|
52 | goto refuse; |
54 |
|
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 |
|
55 | if(dload*200<threshold1) goto repcheck; |
57 |
|
56 | if(dload*50>threshold2) goto refuse; |
58 |
|
57 | p1=find_word_start(p2+1); /* go to second average: 5 min. */ |
59 |
|
58 | *find_word_end(p1)=0; |
60 |
|
59 | dload=atof(p1); |
61 |
|
60 | if(!isfinite(dload) || dload<=0 || dload>1000) goto repcheck; /* unreasonable */ |
62 |
|
61 | load=dload*100; |
63 |
|
62 | snprintf(buf,sizeof(buf),"%d",load); |
64 |
|
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 |
|
66 | if(load*3>=threshold1*2) { |
68 |
|
67 | struct rlimit rlim; |
69 |
|
68 | rlimit_cpu=(3*rlimit_cpu+1)/4; |
70 |
|
69 | if(load>=threshold1) rlimit_cpu=(3*rlimit_cpu+1)/4; |
71 |
|
70 | rlim.rlim_cur=rlim.rlim_max=rlimit_cpu; |
72 |
|
71 | setrlimit(RLIMIT_CPU,&rlim); |
73 |
|
72 | } |
74 |
|
73 | if((th==0 && load*2>threshold1) || |
75 |
|
74 | (th==1 && load>threshold1) || (th==2 && load>threshold2)) { |
76 |
|
75 | refuse: |
77 |
|
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 |
|
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 |
|
109 | if(rafalvl<=0) return; |
111 |
|
110 | p=getvar("module_scoring"); if(p==NULL || strcasecmp(p,"yes")!=0) return; |
112 |
|
111 | u=getvar("wims_user"); if(u!=NULL && strcmp(u,"supervisor")==0) return; |
113 |
|
112 | p=getvar("wims_developer"); if(p!=NULL && *p!=0) return; |
114 |
|
113 | p=getenv("REMOTE_ADDR");if(p!=NULL && strcmp(p,"127.0.0.1")==0) return; |
115 |
|
114 | p=getvar("session"); if(p!=NULL && strstr(p,"_exam")!=NULL) return; |
116 |
|
115 | sh=getvar("wims_sheet"); if(sh!=NULL && *sh>'0') coef*=3; |
117 |
|
116 | p=getvar("wims_rafale"); if(p==NULL) p=""; |
118 |
|
117 | mm=0; |
119 |
|
118 | for(p1=find_word_start(p),i=0;i<rafinfono && *p1;p1=find_word_start(p2)) { |
120 |
|
119 | p2=find_word_end(p1); if(*p2) *p2++=0; |
121 |
|
120 | rr=atol(p1); if(rr<=0 || rr>nowtime) continue; |
122 |
|
121 | t=coef*rafalvl*pow(i,1+rafalvl*0.05)-(nowtime-rr); if(t>mm) mm=t; |
123 |
|
122 | rafinfo[i++]=rr; |
124 |
|
123 | } |
125 |
|
124 | if(mm>0) { |
126 |
|
125 | if(u!=NULL && *u!=0) user_log("rafale"); |
127 |
|
126 | user_error("rafale"); |
128 |
|
127 | } |
129 |
|
128 | rafinfocnt=i; |
130 |
|
129 | snprintf(rbuf,sizeof(rbuf),"%lu",nowtime); |
131 |
|
130 | for(i=0;i<rafinfocnt;i++) { |
132 |
|
131 | snprintf(rbuf+strlen(rbuf),sizeof(rbuf)-strlen(rbuf), |
133 |
|
132 | " %lu",rafinfo[i]); |
134 |
|
133 | } |
135 |
|
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 |
|
140 | char *p; |
142 |
|
141 | double s; |
143 |
|
142 | int i; |
144 |
|
143 | p=getvar("module_score"); if(p==NULL) return; |
145 |
|
144 | s=atof(p); if(s<3) return; |
146 |
|
145 | p=getvar("wims_rafale"); if(p==NULL || *p==0) return; |
147 |
|
146 | for(i=0;i<2;i++) p=find_word_end(find_word_start(p)); |
148 |
|
147 | p=find_word_start(p); |
149 |
|
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 |
158 | #define ac_hint 0x80 /* hint command */ |
160 | #define ac_sheet 0x100 |
159 | #define ac_sheet 0x100/* use within a worksheet */ |
161 | #define ac_exam 0x200 |
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 |
|
165 | char *p, *p1, *p2, *p3, *pp1, *pp2; |
167 |
|
166 | char cbuf[MAX_LINELEN+1]; |
168 |
|
167 | long int thisaccess, lineaccess, linepol, thispol; |
169 |
|
168 | int non, refuse; |
170 | 169 | ||
171 |
|
170 | if(manageable>=2 || robot_access) return; |
172 |
|
171 | thisaccess=0; |
173 |
|
172 | p=getvar(ro_name[ro_module]); if(p==NULL || *p==0) return; |
174 |
|
173 | if(strncmp(p,"adm/doc",7)==0) thisaccess|=ac_doc; |
175 |
|
174 | else if(strncmp(p,"adm/",4)==0 || strcmp(p,home_module)==0) return; |
176 |
|
175 | if(strncmp(p,"local/",6)==0) thisaccess|=ac_local; |
177 |
|
176 | if(strncmp(p,"com/",4)==0) thisaccess|=ac_com; |
178 |
|
177 | p=getvar("wims_user"); |
179 |
|
178 | if(p!=NULL && *p!=0) { |
180 |
|
179 | if(!isclass && strcmp(p,"supervisor")!=0) access_check(1); |
181 |
|
180 | thisaccess|=ac_class; |
182 |
|
181 | } |
183 |
|
182 | if(isclass) { |
184 |
|
183 | if(class_dir[0]==0) return; |
185 |
|
184 | accessfile(cbuf,"r","%s/access.conf",class_dir); |
186 |
|
185 | } |
187 |
|
186 | else accessfile(cbuf,"r",ACCESS_CONF); |
188 |
|
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 |
|
193 | p2=find_word_end(p1); |
195 |
|
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 |
|
204 | p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1); |
206 |
|
205 | if(!myisalpha(*p1)) continue; |
207 |
|
206 | p3=strchr(p1,':'); if(p3==NULL) continue; |
208 |
|
207 | *p3++=0; p3=find_word_start(p3); strip_trailing_spaces(p3); |
209 |
|
208 | refuse=0; |
210 |
|
209 | if(*p3=='!') { p3=find_word_start(p3+1); refuse=1;} |
211 | } |
- | |
212 |
|
210 | if(*p3 && checkhostt(p3)==0) continue; |
213 |
|
211 | for(p=p1; *p; p++) { |
214 |
|
212 | if(myisalpha(*p)) *p=tolower(*p); else *p=' '; |
215 | else *p=' '; |
- | |
216 |
|
213 | } |
217 |
|
214 | lineaccess=thisaccess; linepol=0; |
218 |
|
215 | for(pp1=find_word_start(p1); *pp1; pp1=find_word_start(pp2)) { |
219 |
|
216 | pp2=find_word_end(pp1); if(*pp2) *pp2++=0; |
220 |
|
217 | if(strncmp(pp1,"non",3)==0) { |
221 |
|
218 | pp1=find_word_start(pp1+3); non=1; |
222 | } |
- | |
223 |
|
219 | } else non=0; |
224 |
|
220 | thispol=0; |
225 |
|
221 | if(strcmp(pp1,"class")==0) {thispol=ac_class; goto nxt;} |
226 |
|
222 | if(strcmp(pp1,"exo")==0) {thispol=ac_exo; goto nxt;} |
227 |
|
223 | if(strcmp(pp1,"exercise")==0) {thispol=ac_exo; goto nxt;} |
228 |
|
224 | if(strcmp(pp1,"tool")==0) {thispol=ac_tool; goto nxt;} |
229 |
|
225 | if(strcmp(pp1,"recre")==0) {thispol=ac_recre; goto nxt;} |
230 |
|
226 | if(strcmp(pp1,"recreation")==0) {thispol=ac_recre; goto nxt;} |
231 |
|
227 | if(strcmp(pp1,"doc")==0) {thispol=ac_doc; goto nxt;} |
232 |
|
228 | if(strcmp(pp1,"document")==0) {thispol=ac_doc; goto nxt;} |
233 |
|
229 | if(strcmp(pp1,"local")==0) {thispol=ac_local; goto nxt;} |
234 |
|
230 | if(strcmp(pp1,"com")==0) {thispol=ac_com; goto nxt;} |
235 |
|
231 | if(strcmp(pp1,"hint")==0) {thispol=ac_hint; goto nxt;} |
236 |
|
232 | nxt: |
237 |
|
233 | if(thispol==0) continue; |
238 |
|
234 | if(non) lineaccess^=thispol; |
239 |
|
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 |