1 | //FIXME Not checked on threadsafety yet; after checking please remove this line
|
---|
2 | #include "globals.h"
|
---|
3 |
|
---|
4 | #ifdef CS_ANTICASC
|
---|
5 |
|
---|
6 | #include "module-anticasc.h"
|
---|
7 | #include "oscam-conf.h"
|
---|
8 | #include "oscam-garbage.h"
|
---|
9 | #include "oscam-string.h"
|
---|
10 | #include "oscam-time.h"
|
---|
11 |
|
---|
12 | #define cs_ac "oscam.ac"
|
---|
13 |
|
---|
14 | FILE *ac_log = NULL;
|
---|
15 |
|
---|
16 | //static time_t ac_last_chk;
|
---|
17 | static uchar ac_ecmd5[CS_ECMSTORESIZE];
|
---|
18 |
|
---|
19 | static int32_t ac_init_log(void) {
|
---|
20 | if (ac_log)
|
---|
21 | return 1;
|
---|
22 | if (!cfg.ac_logfile) {
|
---|
23 | cs_log("ERROR: anti cascading is enabled but ac_logfile is not set.");
|
---|
24 | return 0;
|
---|
25 | }
|
---|
26 | ac_log = fopen(cfg.ac_logfile, "a+");
|
---|
27 | if (!ac_log) {
|
---|
28 | cs_log("ERROR: Can't open anti cascading logfile: %s (errno=%d %s)",
|
---|
29 | cfg.ac_logfile, errno, strerror(errno));
|
---|
30 | return 0;
|
---|
31 | }
|
---|
32 | cs_log("anti cascading log initialized");
|
---|
33 | return 1;
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | void ac_clear(void)
|
---|
38 | {
|
---|
39 | struct s_client *client;
|
---|
40 | struct s_auth *account;
|
---|
41 |
|
---|
42 | for (client=first_client;client;client=client->next)
|
---|
43 | {
|
---|
44 | if (client->typ != 'c') continue;
|
---|
45 | memset(&client->acasc, 0, sizeof(client->acasc));
|
---|
46 | }
|
---|
47 |
|
---|
48 | for (account=cfg.account;account;account=account->next)
|
---|
49 | memset(&account->ac_stat, 0, sizeof(account->ac_stat));
|
---|
50 | }
|
---|
51 |
|
---|
52 | void ac_init_stat(void)
|
---|
53 | {
|
---|
54 | if (!cfg.ac_enabled)
|
---|
55 | return;
|
---|
56 | ac_clear();
|
---|
57 | ac_init_log();
|
---|
58 | }
|
---|
59 |
|
---|
60 | void ac_do_stat(void)
|
---|
61 | {
|
---|
62 | int32_t j, idx, exceeds, maxval, prev_deny=0;
|
---|
63 |
|
---|
64 | struct s_client *client;
|
---|
65 | for (client=first_client;client;client=client->next)
|
---|
66 | {
|
---|
67 | if (client->typ != 'c') continue;
|
---|
68 |
|
---|
69 | struct s_acasc *ac_stat = &client->account->ac_stat;
|
---|
70 | struct s_acasc_shm *acasc = &client->acasc;
|
---|
71 |
|
---|
72 | idx = ac_stat->idx;
|
---|
73 | ac_stat->stat[idx] = acasc->ac_count;
|
---|
74 | acasc->ac_count=0;
|
---|
75 |
|
---|
76 | if( ac_stat->stat[idx])
|
---|
77 | {
|
---|
78 | if( client->ac_penalty==2 ) {// banned
|
---|
79 | cs_debug_mask(D_CLIENT, "acasc: user '%s' banned", client->account->usr);
|
---|
80 | acasc->ac_deny=1;
|
---|
81 | }
|
---|
82 | else
|
---|
83 | {
|
---|
84 | for(j=exceeds=maxval=0; j<cfg.ac_samples; j++)
|
---|
85 | {
|
---|
86 | if (ac_stat->stat[j] > maxval)
|
---|
87 | maxval=ac_stat->stat[j];
|
---|
88 | exceeds+=(ac_stat->stat[j] > client->ac_limit);
|
---|
89 | }
|
---|
90 | prev_deny=acasc->ac_deny;
|
---|
91 | acasc->ac_deny = (exceeds >= cfg.ac_denysamples);
|
---|
92 |
|
---|
93 | cs_debug_mask(D_CLIENT, "acasc: %s limit=%d, max=%d, samples=%d, dsamples=%d, [idx=%d]:",
|
---|
94 | client->account->usr, client->ac_limit, maxval,
|
---|
95 | cfg.ac_samples, cfg.ac_denysamples, idx);
|
---|
96 | cs_debug_mask(D_CLIENT, "acasc: %d %d %d %d %d %d %d %d %d %d ", ac_stat->stat[0],
|
---|
97 | ac_stat->stat[1], ac_stat->stat[2], ac_stat->stat[3],
|
---|
98 | ac_stat->stat[4], ac_stat->stat[5], ac_stat->stat[6],
|
---|
99 | ac_stat->stat[7], ac_stat->stat[8], ac_stat->stat[9]);
|
---|
100 | if( acasc->ac_deny ) {
|
---|
101 | cs_log("acasc: user '%s' exceeds limit", client->account->usr);
|
---|
102 | ac_stat->stat[idx] = 0;
|
---|
103 | } else if( prev_deny )
|
---|
104 | cs_log("acasc: user '%s' restored access", client->account->usr);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | else if (acasc->ac_deny)
|
---|
108 | {
|
---|
109 | prev_deny=1;
|
---|
110 | acasc->ac_deny=0;
|
---|
111 | cs_log("acasc: restored access for inactive user '%s'", client->account->usr);
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (!acasc->ac_deny && !prev_deny)
|
---|
115 | ac_stat->idx = (ac_stat->idx + 1) % cfg.ac_samples;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | void ac_init_client(struct s_client *client, struct s_auth *account)
|
---|
120 | {
|
---|
121 | client->ac_limit = 0;
|
---|
122 | client->ac_penalty = account->ac_penalty == -1 ? cfg.ac_penalty : account->ac_penalty;
|
---|
123 | client->ac_fakedelay = account->ac_fakedelay == -1 ? cfg.ac_fakedelay : account->ac_fakedelay;
|
---|
124 | if( cfg.ac_enabled )
|
---|
125 | {
|
---|
126 | int32_t numusers = account->ac_users;
|
---|
127 | if ( numusers == -1)
|
---|
128 | numusers = cfg.ac_users;
|
---|
129 |
|
---|
130 | if( numusers )
|
---|
131 | {
|
---|
132 | client->ac_limit = (numusers*100+80)*cfg.ac_stime;
|
---|
133 | cs_debug_mask(D_CLIENT, "acasc: user '%s', users=%d, stime=%d min, dwlimit=%d per min, penalty=%d",
|
---|
134 | account->usr, numusers, cfg.ac_stime,
|
---|
135 | numusers*100+80, client->ac_penalty);
|
---|
136 | }
|
---|
137 | else
|
---|
138 | {
|
---|
139 | cs_debug_mask(D_CLIENT, "acasc: anti-cascading not used for user '%s'", account->usr);
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | static int32_t ac_dw_weight(ECM_REQUEST *er)
|
---|
145 | {
|
---|
146 | struct s_cpmap *cpmap;
|
---|
147 |
|
---|
148 | for( cpmap=cfg.cpmap; (cpmap) ; cpmap=cpmap->next )
|
---|
149 | if( (cpmap->caid ==0 || cpmap->caid ==er->caid) &&
|
---|
150 | (cpmap->provid==0 || cpmap->provid==er->prid) &&
|
---|
151 | (cpmap->sid ==0 || cpmap->sid ==er->srvid) &&
|
---|
152 | (cpmap->chid ==0 || cpmap->chid ==er->chid) )
|
---|
153 | return (cpmap->dwtime*100/60);
|
---|
154 |
|
---|
155 | cs_debug_mask(D_CLIENT, "acasc: WARNING: CAID %04X, PROVID %06X, SID %04X, CHID %04X not found in oscam.ac",
|
---|
156 | er->caid, er->prid, er->srvid, er->chid);
|
---|
157 | cs_debug_mask(D_CLIENT, "acasc: set DW lifetime 10 sec");
|
---|
158 | return 16; // 10*100/60
|
---|
159 | }
|
---|
160 |
|
---|
161 | void ac_chk(struct s_client *cl, ECM_REQUEST *er, int32_t level)
|
---|
162 | {
|
---|
163 | if (!cl->ac_limit || !cfg.ac_enabled) return;
|
---|
164 |
|
---|
165 | struct s_acasc_shm *acasc = &cl->acasc;
|
---|
166 |
|
---|
167 | if( level == 1 ) {
|
---|
168 | if( er->rc == E_FAKE )
|
---|
169 | acasc->ac_count++;
|
---|
170 |
|
---|
171 | if( er->rc >= E_NOTFOUND )
|
---|
172 | return; // not found
|
---|
173 |
|
---|
174 | if( memcmp(ac_ecmd5, er->ecmd5, CS_ECMSTORESIZE) != 0 ) {
|
---|
175 | acasc->ac_count += ac_dw_weight(er);
|
---|
176 | memcpy(ac_ecmd5, er->ecmd5, CS_ECMSTORESIZE);
|
---|
177 | }
|
---|
178 | return;
|
---|
179 | }
|
---|
180 |
|
---|
181 | if( acasc->ac_deny ) {
|
---|
182 | if( cl->ac_penalty ) {
|
---|
183 | if (cl->ac_penalty == 3) {
|
---|
184 | if (cl->ac_fakedelay > 0)
|
---|
185 | cs_debug_mask(D_CLIENT, "acasc: fake delay %dms", cl->ac_fakedelay);
|
---|
186 | } else {
|
---|
187 | cs_debug_mask(D_CLIENT, "acasc: send fake dw");
|
---|
188 | er->rc = E_FAKE; // fake
|
---|
189 | er->rcEx = 0;
|
---|
190 | }
|
---|
191 | if (cl->ac_fakedelay > 0)
|
---|
192 | cs_sleepms(cl->ac_fakedelay);
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | static void ac_load_config(void)
|
---|
198 | {
|
---|
199 | FILE *fp = open_config_file(cs_ac);
|
---|
200 | if (!fp)
|
---|
201 | return;
|
---|
202 |
|
---|
203 | int32_t nr;
|
---|
204 | char *saveptr1 = NULL, *token;
|
---|
205 | if (!cs_malloc(&token, MAXLINESIZE))
|
---|
206 | return;
|
---|
207 | struct s_cpmap *cur_cpmap, *first_cpmap = NULL, *last_cpmap = NULL;
|
---|
208 |
|
---|
209 | for(nr=0; fgets(token, MAXLINESIZE, fp);)
|
---|
210 | {
|
---|
211 | int32_t i, skip;
|
---|
212 | uint16_t caid, sid, chid, dwtime;
|
---|
213 | uint32_t provid;
|
---|
214 | char *ptr, *ptr1;
|
---|
215 |
|
---|
216 | if( strlen(token)<4 ) continue;
|
---|
217 |
|
---|
218 | caid=sid=chid=dwtime=0;
|
---|
219 | provid=0;
|
---|
220 | skip=0;
|
---|
221 | ptr1=0;
|
---|
222 | for( i=0, ptr=strtok_r(token, "=", &saveptr1); (i<2)&&(ptr); ptr=strtok_r(NULL, "=", &saveptr1), i++ )
|
---|
223 | {
|
---|
224 | trim(ptr);
|
---|
225 | if( *ptr==';' || *ptr=='#' || *ptr=='-' ) {
|
---|
226 | skip=1;
|
---|
227 | break;
|
---|
228 | }
|
---|
229 | switch( i )
|
---|
230 | {
|
---|
231 | case 0:
|
---|
232 | ptr1=ptr;
|
---|
233 | break;
|
---|
234 | case 1:
|
---|
235 | dwtime = atoi(ptr);
|
---|
236 | break;
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | if( !skip )
|
---|
241 | {
|
---|
242 | for( i=0, ptr=strtok_r(ptr1, ":", &saveptr1); (i<4)&&(ptr); ptr=strtok_r(NULL, ":", &saveptr1), i++ )
|
---|
243 | {
|
---|
244 | trim(ptr);
|
---|
245 | switch( i )
|
---|
246 | {
|
---|
247 | case 0:
|
---|
248 | if( *ptr=='*' ) caid = 0;
|
---|
249 | else caid = a2i(ptr, 4);
|
---|
250 | break;
|
---|
251 | case 1:
|
---|
252 | if( *ptr=='*' ) provid = 0;
|
---|
253 | else provid = a2i(ptr, 6);
|
---|
254 | break;
|
---|
255 | case 2:
|
---|
256 | if( *ptr=='*' ) sid = 0;
|
---|
257 | else sid = a2i(ptr, 4);
|
---|
258 | break;
|
---|
259 | case 3:
|
---|
260 | if( *ptr=='*' ) chid = 0;
|
---|
261 | else chid = a2i(ptr, 4);
|
---|
262 | break;
|
---|
263 | }
|
---|
264 | }
|
---|
265 | if (!cs_malloc(&cur_cpmap, sizeof(struct s_cpmap))) {
|
---|
266 | for(cur_cpmap = first_cpmap; cur_cpmap; ){
|
---|
267 | last_cpmap = cur_cpmap;
|
---|
268 | cur_cpmap = cur_cpmap->next;
|
---|
269 | free(last_cpmap);
|
---|
270 | }
|
---|
271 | free(token);
|
---|
272 | return;
|
---|
273 | }
|
---|
274 | if(last_cpmap)
|
---|
275 | last_cpmap->next=cur_cpmap;
|
---|
276 | else
|
---|
277 | first_cpmap=cur_cpmap;
|
---|
278 | last_cpmap=cur_cpmap;
|
---|
279 |
|
---|
280 | cur_cpmap->caid = caid;
|
---|
281 | cur_cpmap->provid = provid;
|
---|
282 | cur_cpmap->sid = sid;
|
---|
283 | cur_cpmap->chid = chid;
|
---|
284 | cur_cpmap->dwtime = dwtime;
|
---|
285 | cur_cpmap->next = 0;
|
---|
286 |
|
---|
287 | cs_debug_mask(D_CLIENT, "nr=%d, caid=%04X, provid=%06X, sid=%04X, chid=%04X, dwtime=%d",
|
---|
288 | nr, caid, provid, sid, chid, dwtime);
|
---|
289 | nr++;
|
---|
290 | }
|
---|
291 | }
|
---|
292 | free(token);
|
---|
293 | fclose(fp);
|
---|
294 |
|
---|
295 | last_cpmap = cfg.cpmap;
|
---|
296 | cfg.cpmap = first_cpmap;
|
---|
297 | for(cur_cpmap = last_cpmap; cur_cpmap; cur_cpmap = cur_cpmap->next)
|
---|
298 | add_garbage(cur_cpmap);
|
---|
299 | //cs_log("%d lengths for caid guessing loaded", nr);
|
---|
300 | return;
|
---|
301 | }
|
---|
302 |
|
---|
303 | void ac_copy_vars(struct s_auth *src, struct s_auth *dst) {
|
---|
304 | dst->ac_stat = src->ac_stat;
|
---|
305 | }
|
---|
306 |
|
---|
307 | void ac_init(void) {
|
---|
308 | if (!cfg.ac_enabled) {
|
---|
309 | cs_log("anti cascading disabled");
|
---|
310 | return;
|
---|
311 | }
|
---|
312 |
|
---|
313 | ac_load_config();
|
---|
314 | ac_init_stat();
|
---|
315 | }
|
---|
316 |
|
---|
317 | #endif
|
---|