[3181] | 1 | //FIXME Not checked on threadsafety yet; after checking please remove this line
|
---|
[8] | 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 |
|
---|
[4994] | 9 | int32_t ac_init_log(void)
|
---|
[8] | 10 | {
|
---|
[4588] | 11 | if( (!fpa) && (cfg.ac_logfile[0]))
|
---|
[8] | 12 | {
|
---|
[4588] | 13 | if( (fpa=fopen(cfg.ac_logfile, "a+"))<=(FILE *)0 )
|
---|
[8] | 14 | {
|
---|
| 15 | fpa=(FILE *)0;
|
---|
[4588] | 16 | fprintf(stderr, "can't open anti-cascading logfile: %s\n", cfg.ac_logfile);
|
---|
[8] | 17 | }
|
---|
| 18 | else
|
---|
| 19 | cs_log("anti-cascading log initialized");
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | return(fpa<=(FILE *)0);
|
---|
| 23 | }
|
---|
| 24 |
|
---|
[3542] | 25 | void ac_clear()
|
---|
| 26 | {
|
---|
[4977] | 27 | struct s_client *client;
|
---|
| 28 | struct s_auth *account;
|
---|
| 29 |
|
---|
| 30 | for (client=first_client;client;client=client->next)
|
---|
| 31 | {
|
---|
| 32 | if (client->typ != 'c') continue;
|
---|
| 33 | memset(&client->acasc, 0, sizeof(client->acasc));
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | for (account=cfg.account;account;account=account->next)
|
---|
| 37 | memset(&account->ac_stat, 0, sizeof(account->ac_stat));
|
---|
[3542] | 38 | }
|
---|
| 39 |
|
---|
| 40 | void ac_done_stat()
|
---|
| 41 | {
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[3562] | 44 | void ac_init_stat()
|
---|
| 45 | {
|
---|
[4977] | 46 | ac_clear();
|
---|
[3562] | 47 |
|
---|
| 48 | if( fpa )
|
---|
| 49 | fclose(fpa);
|
---|
| 50 | fpa=(FILE *)0;
|
---|
| 51 | if( ac_init_log() )
|
---|
| 52 | cs_exit(0);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[8] | 55 | void ac_do_stat()
|
---|
| 56 | {
|
---|
[4994] | 57 | int32_t j, idx, exceeds, maxval, prev_deny=0;
|
---|
[8] | 58 |
|
---|
[4977] | 59 | struct s_client *client;
|
---|
| 60 | for (client=first_client;client;client=client->next)
|
---|
[8] | 61 | {
|
---|
[4977] | 62 | if (client->typ != 'c') continue;
|
---|
| 63 |
|
---|
| 64 | struct s_acasc *ac_stat = &client->account->ac_stat;
|
---|
| 65 | struct s_acasc_shm *acasc = &client->acasc;
|
---|
[3542] | 66 |
|
---|
| 67 | idx = ac_stat->idx;
|
---|
| 68 | ac_stat->stat[idx] = acasc->ac_count;
|
---|
| 69 | acasc->ac_count=0;
|
---|
[8] | 70 |
|
---|
[4977] | 71 | if( ac_stat->stat[idx])
|
---|
[8] | 72 | {
|
---|
[4977] | 73 | if( client->account->ac_penalty==2 ) {// banned
|
---|
| 74 | cs_debug_mask(D_CLIENT, "user '%s' banned", client->account->usr);
|
---|
[3542] | 75 | acasc->ac_deny=1;
|
---|
[8] | 76 | }
|
---|
| 77 | else
|
---|
| 78 | {
|
---|
[4977] | 79 | for(j=exceeds=maxval=0; j<cfg.ac_samples; j++)
|
---|
[8] | 80 | {
|
---|
[4977] | 81 | if (ac_stat->stat[j] > maxval)
|
---|
[3542] | 82 | maxval=ac_stat->stat[j];
|
---|
[4977] | 83 | exceeds+=(ac_stat->stat[j] > client->ac_limit);
|
---|
[8] | 84 | }
|
---|
[3542] | 85 | prev_deny=acasc->ac_deny;
|
---|
[4588] | 86 | acasc->ac_deny = (exceeds >= cfg.ac_denysamples);
|
---|
[8] | 87 |
|
---|
[4977] | 88 | cs_debug_mask(D_CLIENT, "%s limit=%d, max=%d, samples=%d, dsamples=%d, [idx=%d]:",
|
---|
| 89 | client->account->usr, client->ac_limit, maxval,
|
---|
| 90 | cfg.ac_samples, cfg.ac_denysamples, idx);
|
---|
[4141] | 91 | cs_debug_mask(D_CLIENT, "%d %d %d %d %d %d %d %d %d %d ", ac_stat->stat[0],
|
---|
[3542] | 92 | ac_stat->stat[1], ac_stat->stat[2], ac_stat->stat[3],
|
---|
| 93 | ac_stat->stat[4], ac_stat->stat[5], ac_stat->stat[6],
|
---|
| 94 | ac_stat->stat[7], ac_stat->stat[8], ac_stat->stat[9]);
|
---|
| 95 | if( acasc->ac_deny ) {
|
---|
[4977] | 96 | cs_log("user '%s' exceeds limit", client->account->usr);
|
---|
[3542] | 97 | ac_stat->stat[idx] = 0;
|
---|
[8] | 98 | } else if( prev_deny )
|
---|
[4977] | 99 | cs_log("user '%s' restored access", client->account->usr);
|
---|
[8] | 100 | }
|
---|
| 101 | }
|
---|
[4977] | 102 | else if (acasc->ac_deny)
|
---|
[8] | 103 | {
|
---|
| 104 | prev_deny=1;
|
---|
[3542] | 105 | acasc->ac_deny=0;
|
---|
[4977] | 106 | cs_log("restored access for inactive user '%s'", client->account->usr);
|
---|
[8] | 107 | }
|
---|
| 108 |
|
---|
[4977] | 109 | if (!acasc->ac_deny && !prev_deny)
|
---|
[4588] | 110 | ac_stat->idx = (ac_stat->idx + 1) % cfg.ac_samples;
|
---|
[8] | 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[5361] | 114 | /* Starts the anticascader thread. */
|
---|
| 115 | void start_anticascader(){
|
---|
| 116 | struct s_client * cl = create_client(first_client->ip);
|
---|
| 117 | if (cl == NULL) return;
|
---|
| 118 | cl->thread = pthread_self();
|
---|
| 119 | pthread_setspecific(getclient, cl);
|
---|
| 120 | cl->typ = 'a';
|
---|
| 121 |
|
---|
| 122 | ac_init_stat();
|
---|
| 123 | while(1)
|
---|
| 124 | {
|
---|
| 125 | int32_t i;
|
---|
| 126 | for( i=0; i<cfg.ac_stime*60; i++ )
|
---|
| 127 | cs_sleepms(1000); //FIXME this is a cpu-killer!
|
---|
| 128 | ac_do_stat();
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[4977] | 132 | void ac_init_client(struct s_client *client, struct s_auth *account)
|
---|
[8] | 133 | {
|
---|
[4977] | 134 | client->ac_limit = 0;
|
---|
[4588] | 135 | if( cfg.ac_enabled )
|
---|
[8] | 136 | {
|
---|
| 137 | if( account->ac_users )
|
---|
| 138 | {
|
---|
[4977] | 139 | client->ac_limit = (account->ac_users*100+80)*cfg.ac_stime;
|
---|
| 140 | cs_debug_mask(D_CLIENT, "login '%s', users=%d, stime=%d min, dwlimit=%d per min, penalty=%d",
|
---|
| 141 | account->usr, account->ac_users, cfg.ac_stime,
|
---|
[8] | 142 | account->ac_users*100+80, account->ac_penalty);
|
---|
| 143 | }
|
---|
| 144 | else
|
---|
[3354] | 145 | {
|
---|
[4141] | 146 | cs_debug_mask(D_CLIENT, "anti-cascading not used for login '%s'", account->usr);
|
---|
[3354] | 147 | }
|
---|
[8] | 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[4994] | 151 | static int32_t ac_dw_weight(ECM_REQUEST *er)
|
---|
[8] | 152 | {
|
---|
| 153 | struct s_cpmap *cpmap;
|
---|
| 154 |
|
---|
[4588] | 155 | for( cpmap=cfg.cpmap; (cpmap) ; cpmap=cpmap->next )
|
---|
[8] | 156 | if( (cpmap->caid ==0 || cpmap->caid ==er->caid) &&
|
---|
| 157 | (cpmap->provid==0 || cpmap->provid==er->prid) &&
|
---|
| 158 | (cpmap->sid ==0 || cpmap->sid ==er->srvid) &&
|
---|
| 159 | (cpmap->chid ==0 || cpmap->chid ==er->chid) )
|
---|
| 160 | return (cpmap->dwtime*100/60);
|
---|
| 161 |
|
---|
[4141] | 162 | cs_debug_mask(D_CLIENT, "WARNING: CAID %04X, PROVID %06X, SID %04X, CHID %04X not found in oscam.ac",
|
---|
[8] | 163 | er->caid, er->prid, er->srvid, er->chid);
|
---|
[4141] | 164 | cs_debug_mask(D_CLIENT, "set DW lifetime 10 sec");
|
---|
[8] | 165 | return 16; // 10*100/60
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[4994] | 168 | void ac_chk(struct s_client *cl, ECM_REQUEST *er, int32_t level)
|
---|
[8] | 169 | {
|
---|
[4987] | 170 | if (!cl->ac_limit || !cfg.ac_enabled) return;
|
---|
[8] | 171 |
|
---|
[4987] | 172 | struct s_acasc_shm *acasc = &cl->acasc;
|
---|
[3542] | 173 |
|
---|
[4987] | 174 | if( level == 1 ) {
|
---|
| 175 | if( er->rc == E_FAKE )
|
---|
| 176 | acasc->ac_count++;
|
---|
[8] | 177 |
|
---|
[4987] | 178 | if( er->rc >= E_NOTFOUND )
|
---|
| 179 | return; // not found
|
---|
| 180 |
|
---|
| 181 | if( memcmp(ac_ecmd5, er->ecmd5, CS_ECMSTORESIZE) != 0 ) {
|
---|
| 182 | acasc->ac_count += ac_dw_weight(er);
|
---|
| 183 | memcpy(ac_ecmd5, er->ecmd5, CS_ECMSTORESIZE);
|
---|
| 184 | }
|
---|
| 185 | return;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | if( acasc->ac_deny ) {
|
---|
| 189 | if( cl->account->ac_penalty ) {
|
---|
| 190 | if (cl->account->ac_penalty == 3) {
|
---|
| 191 | cs_debug_mask(D_CLIENT, "fake delay %dms", cfg.ac_fakedelay);
|
---|
| 192 | } else {
|
---|
| 193 | cs_debug_mask(D_CLIENT, "send fake dw");
|
---|
| 194 | er->rc = E_FAKE; // fake
|
---|
| 195 | er->rcEx = 0;
|
---|
| 196 | }
|
---|
| 197 | cs_sleepms(cfg.ac_fakedelay);
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
[8] | 200 | }
|
---|
| 201 | #endif
|
---|