1 | //FIXME Not checked on threadsafety yet; after checking please remove this line
|
---|
2 | #include "globals.h"
|
---|
3 |
|
---|
4 | #ifdef CS_ANTICASC
|
---|
5 |
|
---|
6 | //static time_t ac_last_chk;
|
---|
7 | static uchar ac_ecmd5[CS_ECMSTORESIZE];
|
---|
8 |
|
---|
9 | LLIST *ac_stat_list = NULL; //struct s_acasc
|
---|
10 | LLIST *acasc_list = NULL; //struct s_acasc_shm
|
---|
11 |
|
---|
12 | int ac_init_log(void)
|
---|
13 | {
|
---|
14 | if( (!fpa) && (cfg->ac_logfile[0]))
|
---|
15 | {
|
---|
16 | if( (fpa=fopen(cfg->ac_logfile, "a+"))<=(FILE *)0 )
|
---|
17 | {
|
---|
18 | fpa=(FILE *)0;
|
---|
19 | fprintf(stderr, "can't open anti-cascading logfile: %s\n", cfg->ac_logfile);
|
---|
20 | }
|
---|
21 | else
|
---|
22 | cs_log("anti-cascading log initialized");
|
---|
23 | }
|
---|
24 |
|
---|
25 | return(fpa<=(FILE *)0);
|
---|
26 | }
|
---|
27 |
|
---|
28 | void ac_clear()
|
---|
29 | {
|
---|
30 | ll_clear_data(acasc_list);
|
---|
31 | ll_clear_data(ac_stat_list);
|
---|
32 | }
|
---|
33 |
|
---|
34 | void ac_done_stat()
|
---|
35 | {
|
---|
36 | ll_destroy_data(acasc_list);
|
---|
37 | ll_destroy_data(ac_stat_list);
|
---|
38 | }
|
---|
39 |
|
---|
40 | void ac_init_stat()
|
---|
41 | {
|
---|
42 | if (acasc_list)
|
---|
43 | ac_clear();
|
---|
44 | else {
|
---|
45 | ac_stat_list = ll_create();
|
---|
46 | acasc_list = ll_create();
|
---|
47 | }
|
---|
48 |
|
---|
49 | if( fpa )
|
---|
50 | fclose(fpa);
|
---|
51 | fpa=(FILE *)0;
|
---|
52 | if( ac_init_log() )
|
---|
53 | cs_exit(0);
|
---|
54 | }
|
---|
55 |
|
---|
56 | static struct s_client *idx_from_ac_idx(int ac_idx)
|
---|
57 | {
|
---|
58 | struct s_client *cl;
|
---|
59 | for (cl=first_client; cl ; cl=cl->next)
|
---|
60 | if( cl->ac_idx==ac_idx )
|
---|
61 | return cl;
|
---|
62 | return NULL;
|
---|
63 | }
|
---|
64 |
|
---|
65 | void ac_do_stat()
|
---|
66 | {
|
---|
67 | int i, j, idx, exceeds, maxval, prev_deny=0;
|
---|
68 | struct s_client *cl_idx;
|
---|
69 |
|
---|
70 | LL_ITER *itr1 = ll_iter_create(ac_stat_list);
|
---|
71 | LL_ITER *itr2 = ll_iter_create(acasc_list);
|
---|
72 | i = 1;
|
---|
73 | struct s_acasc *ac_stat = ll_iter_next(itr1);
|
---|
74 | struct s_acasc_shm *acasc;
|
---|
75 | while ((acasc=ll_iter_next(itr2)))
|
---|
76 | {
|
---|
77 | int ac_stat_next = 1;
|
---|
78 | if (!ac_stat) {
|
---|
79 | ac_stat = malloc(sizeof(struct s_acasc));
|
---|
80 | memset(ac_stat, 0, sizeof(struct s_acasc));
|
---|
81 | ll_iter_insert(itr1, ac_stat);
|
---|
82 | ac_stat_next = 0;
|
---|
83 | }
|
---|
84 |
|
---|
85 | idx = ac_stat->idx;
|
---|
86 | ac_stat->stat[idx] = acasc->ac_count;
|
---|
87 | acasc->ac_count=0;
|
---|
88 | cl_idx = idx_from_ac_idx(i);
|
---|
89 |
|
---|
90 | if( ac_stat->stat[idx] && cl_idx)
|
---|
91 | {
|
---|
92 | //if( cl_idx == NULL ) {
|
---|
93 | //cs_log("ERROR: can't find client with ac_idx=%d", i);
|
---|
94 | //client is no longer connected
|
---|
95 | //}
|
---|
96 |
|
---|
97 | if( cl_idx->ac_penalty==2 ) {// banned
|
---|
98 | cs_debug_mask(D_CLIENT, "user '%s' banned", cl_idx->account->usr);
|
---|
99 | acasc->ac_deny=1;
|
---|
100 | }
|
---|
101 | else
|
---|
102 | {
|
---|
103 | for( j=exceeds=maxval=0; j<cfg->ac_samples; j++ )
|
---|
104 | {
|
---|
105 | if( ac_stat->stat[j] > maxval )
|
---|
106 | maxval=ac_stat->stat[j];
|
---|
107 | exceeds+=(ac_stat->stat[j]>cl_idx->ac_limit);
|
---|
108 | }
|
---|
109 | prev_deny=acasc->ac_deny;
|
---|
110 | acasc->ac_deny = (exceeds >= cfg->ac_denysamples);
|
---|
111 |
|
---|
112 | cs_debug_mask(D_CLIENT, "%s limit=%d, max=%d, samples=%d, dsamples=%d, ac[ci=%d][si=%d]:",
|
---|
113 | cl_idx->account->usr, cl_idx->ac_limit, maxval,
|
---|
114 | cfg->ac_samples, cfg->ac_denysamples, i, idx);
|
---|
115 | cs_debug_mask(D_CLIENT, "%d %d %d %d %d %d %d %d %d %d ", ac_stat->stat[0],
|
---|
116 | ac_stat->stat[1], ac_stat->stat[2], ac_stat->stat[3],
|
---|
117 | ac_stat->stat[4], ac_stat->stat[5], ac_stat->stat[6],
|
---|
118 | ac_stat->stat[7], ac_stat->stat[8], ac_stat->stat[9]);
|
---|
119 | if( acasc->ac_deny ) {
|
---|
120 | cs_log("user '%s' exceeds limit", cl_idx->account->usr);
|
---|
121 | ac_stat->stat[idx] = 0;
|
---|
122 | } else if( prev_deny )
|
---|
123 | cs_log("user '%s' restored access", cl_idx->account->usr);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | else if( acasc->ac_deny )
|
---|
127 | {
|
---|
128 | prev_deny=1;
|
---|
129 | acasc->ac_deny=0;
|
---|
130 | if( cl_idx != NULL )
|
---|
131 | cs_log("restored access for inactive user '%s'", cl_idx->account->usr);
|
---|
132 | else
|
---|
133 | cs_log("restored access for unknown user (ac_idx=%d)", i);
|
---|
134 | }
|
---|
135 |
|
---|
136 | if( !acasc->ac_deny && !prev_deny )
|
---|
137 | ac_stat->idx = (ac_stat->idx + 1) % cfg->ac_samples;
|
---|
138 |
|
---|
139 | if (ac_stat_next)
|
---|
140 | ac_stat = ll_iter_next(itr1);
|
---|
141 | else
|
---|
142 | ac_stat = NULL;
|
---|
143 | i++;
|
---|
144 | }
|
---|
145 | ll_iter_release(itr2);
|
---|
146 | ll_iter_release(itr1);
|
---|
147 | }
|
---|
148 |
|
---|
149 | void ac_init_client(struct s_auth *account)
|
---|
150 | {
|
---|
151 | struct s_client *cl = cur_client();
|
---|
152 | cl->ac_idx = account->ac_idx;
|
---|
153 | cl->ac_limit = 0;
|
---|
154 | if( cfg->ac_enabled )
|
---|
155 | {
|
---|
156 | if( account->ac_users )
|
---|
157 | {
|
---|
158 | cl->ac_limit = (account->ac_users*100+80)*cfg->ac_stime;
|
---|
159 | cl->ac_penalty = account->ac_penalty;
|
---|
160 | cs_debug_mask(D_CLIENT, "login '%s', ac_idx=%d, users=%d, stime=%d min, dwlimit=%d per min, penalty=%d",
|
---|
161 | account->usr, account->ac_idx, account->ac_users, cfg->ac_stime,
|
---|
162 | account->ac_users*100+80, account->ac_penalty);
|
---|
163 | }
|
---|
164 | else
|
---|
165 | {
|
---|
166 | cs_debug_mask(D_CLIENT, "anti-cascading not used for login '%s'", account->usr);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | static int ac_dw_weight(ECM_REQUEST *er)
|
---|
172 | {
|
---|
173 | struct s_cpmap *cpmap;
|
---|
174 |
|
---|
175 | for( cpmap=cfg->cpmap; (cpmap) ; cpmap=cpmap->next )
|
---|
176 | if( (cpmap->caid ==0 || cpmap->caid ==er->caid) &&
|
---|
177 | (cpmap->provid==0 || cpmap->provid==er->prid) &&
|
---|
178 | (cpmap->sid ==0 || cpmap->sid ==er->srvid) &&
|
---|
179 | (cpmap->chid ==0 || cpmap->chid ==er->chid) )
|
---|
180 | return (cpmap->dwtime*100/60);
|
---|
181 |
|
---|
182 | cs_debug_mask(D_CLIENT, "WARNING: CAID %04X, PROVID %06X, SID %04X, CHID %04X not found in oscam.ac",
|
---|
183 | er->caid, er->prid, er->srvid, er->chid);
|
---|
184 | cs_debug_mask(D_CLIENT, "set DW lifetime 10 sec");
|
---|
185 | return 16; // 10*100/60
|
---|
186 | }
|
---|
187 |
|
---|
188 | struct s_acasc_shm *get_acasc(ushort ac_idx) {
|
---|
189 | int i=1;
|
---|
190 | LL_ITER *itr = ll_iter_create(acasc_list);
|
---|
191 |
|
---|
192 | struct s_acasc_shm *acasc;
|
---|
193 | while ((acasc=ll_iter_next(itr))) {
|
---|
194 | if (i == ac_idx) {
|
---|
195 | ll_iter_release(itr);
|
---|
196 | return acasc;
|
---|
197 | }
|
---|
198 | i++;
|
---|
199 | }
|
---|
200 | acasc = malloc(sizeof(struct s_acasc_shm));
|
---|
201 | memset(acasc, 0, sizeof(struct s_acasc_shm));
|
---|
202 | ll_iter_insert(itr, acasc);
|
---|
203 | ll_iter_release(itr);
|
---|
204 | return acasc;
|
---|
205 | }
|
---|
206 |
|
---|
207 | void ac_chk(ECM_REQUEST *er, int level)
|
---|
208 | {
|
---|
209 | struct s_client *cl = cur_client();
|
---|
210 | if (!cl->ac_limit || !cfg->ac_enabled ||!acasc_list) return;
|
---|
211 |
|
---|
212 | struct s_acasc_shm *acasc = get_acasc(cl->ac_idx);
|
---|
213 |
|
---|
214 | if( level==1 )
|
---|
215 | {
|
---|
216 | if( er->rc==7 ) acasc->ac_count++;
|
---|
217 | if( er->rc>3 ) return; // not found
|
---|
218 | if( memcmp(ac_ecmd5, er->ecmd5, CS_ECMSTORESIZE) != 0 )
|
---|
219 | {
|
---|
220 | acasc->ac_count+=ac_dw_weight(er);
|
---|
221 | memcpy(ac_ecmd5, er->ecmd5, CS_ECMSTORESIZE);
|
---|
222 | }
|
---|
223 | return;
|
---|
224 | }
|
---|
225 |
|
---|
226 | if( acasc->ac_deny )
|
---|
227 | if( cl->ac_penalty )
|
---|
228 | {
|
---|
229 | cs_debug_mask(D_CLIENT, "send fake dw");
|
---|
230 | er->rc=7; // fake
|
---|
231 | er->rcEx=0;
|
---|
232 | cs_sleepms(cfg->ac_fakedelay);
|
---|
233 | }
|
---|
234 | }
|
---|
235 | #endif
|
---|