-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathoscam-chk.c
1112 lines (971 loc) · 25.4 KB
/
oscam-chk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define MODULE_LOG_PREFIX "chk"
#include "globals.h"
#include "oscam-cache.h"
#include "oscam-chk.h"
#include "oscam-ecm.h"
#include "oscam-client.h"
#include "oscam-lock.h"
#include "oscam-net.h"
#include "oscam-string.h"
#include "module-stat.h"
#include "oscam-reader.h"
#define CS_NANO_CLASS 0xE2
#define OK 1
#define ERROR 0
uint32_t get_fallbacktimeout(uint16_t caid)
{
uint32_t ftimeout = caidvaluetab_get_value(&cfg.ftimeouttab, caid, 0);
if(ftimeout == 0) { ftimeout = cfg.ftimeout; }
if(ftimeout < 100) { ftimeout = CS_CLIENT_TIMEOUT / 2; }
if(ftimeout >= cfg.ctimeout) { ftimeout = cfg.ctimeout - 100; }
return ftimeout;
}
static int32_t find_nano(uchar *ecm, int32_t l, uchar nano, int32_t s)
{
uchar *snano;
if(s >= l) { return 0; }
if(!s) { s = (ecm[4] == 0xD2) ? 12 : 9; } // tpsflag -> offset+3
snano = ecm + s;
while((*snano != nano) && (s < l))
{
if(*snano == 0xEA) { return 0; }
snano++;
s++;
}
return (s < l) ? ++s : 0;
}
static int32_t chk_class(ECM_REQUEST *er, CLASSTAB *clstab, const char *type, const char *name)
{
int32_t i, j, an, cl_n, l;
uchar ecm_class;
if(er->caid != 0x0500) { return 1; }
if(!clstab->bn && !clstab->an) { return 1; }
j = an = cl_n = l = 0;
while((j = find_nano(er->ecm, er->ecmlen, CS_NANO_CLASS, j)) > 0)
{
l = er->ecm[j];
if(l + j > er->ecmlen) { continue; } // skip, this is not a valid class identifier!
ecm_class = er->ecm[j + l];
cs_log_dbg(D_CLIENT, "ecm class=%02X", ecm_class);
for(i = 0; i < clstab->bn; i++) // search in blocked
if(ecm_class == clstab->bclass[i])
{
cs_log_dbg(D_CLIENT, "class %02X rejected by %s '%s' !%02X filter",
ecm_class, type, name, ecm_class);
return 0;
}
cl_n++;
for(i = 0; i < clstab->an; i++) // search in allowed
if(ecm_class == clstab->aclass[i])
{
an++;
break;
}
j += l;
}
if(cl_n && clstab->an)
{
if(an)
{ cs_log_dbg(D_CLIENT, "ECM classes allowed by %s '%s' filter", type, name); }
else
{
cs_log_dbg(D_CLIENT, "ECM classes don't match %s '%s' filter, rejecting", type, name);
return 0;
}
}
return 1;
}
int32_t chk_srvid_match(ECM_REQUEST *er, SIDTAB *sidtab)
{
int32_t i, rc = 0;
if(!sidtab->num_caid)
{ rc |= 1; }
else
for(i = 0; (i < sidtab->num_caid) && (!(rc & 1)); i++)
if(er->caid == sidtab->caid[i]) { rc |= 1; }
if(!er->prid || !sidtab->num_provid)
{ rc |= 2; }
else
for(i = 0; (i < sidtab->num_provid) && (!(rc & 2)); i++)
if(er->prid == sidtab->provid[i]) { rc |= 2; }
if(!sidtab->num_srvid)
{ rc |= 4; }
else
for(i = 0; (i < sidtab->num_srvid) && (!(rc & 4)); i++)
if(er->srvid == sidtab->srvid[i]) { rc |= 4; }
return (rc == 7);
}
int32_t chk_srvid(struct s_client *cl, ECM_REQUEST *er)
{
int32_t nr, rc = 0;
SIDTAB *sidtab;
if(!cl->sidtabs.ok)
{
if(!cl->sidtabs.no) { return (1); }
rc = 1;
}
for(nr = 0, sidtab = cfg.sidtab; sidtab; sidtab = sidtab->next, nr++)
if(sidtab->num_caid | sidtab->num_provid | sidtab->num_srvid)
{
if((cl->sidtabs.no & ((SIDTABBITS)1 << nr)) &&
(chk_srvid_match(er, sidtab)))
{ return (0); }
if((cl->sidtabs.ok & ((SIDTABBITS)1 << nr)) &&
(chk_srvid_match(er, sidtab)))
{ rc = 1; }
}
return (rc);
}
int32_t has_srvid(struct s_client *cl, ECM_REQUEST *er)
{
if(!cl->sidtabs.ok)
{ return 0; }
int32_t nr;
SIDTAB *sidtab;
for(nr = 0, sidtab = cfg.sidtab; sidtab; sidtab = sidtab->next, nr++)
if(sidtab->num_srvid)
{
if((cl->sidtabs.ok & ((SIDTABBITS)1 << nr)) &&
(chk_srvid_match(er, sidtab)))
{ return 1; }
}
return 0;
}
int32_t has_lb_srvid(struct s_client *cl, ECM_REQUEST *er)
{
if(!cl->lb_sidtabs.ok)
{ return 0; }
int32_t nr;
SIDTAB *sidtab;
for(nr = 0, sidtab = cfg.sidtab; sidtab; sidtab = sidtab->next, nr++)
if((cl->lb_sidtabs.ok & ((SIDTABBITS)1 << nr)) &&
(chk_srvid_match(er, sidtab)))
{ return 1; }
return 0;
}
int32_t chk_srvid_match_by_caid_prov(uint16_t caid, uint32_t provid, SIDTAB *sidtab)
{
int32_t i, rc = 0;
if(!sidtab->num_caid)
{ rc |= 1; }
else
for(i = 0; (i < sidtab->num_caid) && (!(rc & 1)); i++)
if(caid == sidtab->caid[i]) { rc |= 1; }
if(!sidtab->num_provid)
{ rc |= 2; }
else
for(i = 0; (i < sidtab->num_provid) && (!(rc & 2)); i++)
if(provid == sidtab->provid[i]) { rc |= 2; }
return (rc == 3);
}
int32_t chk_srvid_by_caid_prov(struct s_client *cl, uint16_t caid, uint32_t provid)
{
int32_t nr, rc = 0;
SIDTAB *sidtab;
if(!cl->sidtabs.ok)
{
if(!cl->sidtabs.no) { return (1); }
rc = 1;
}
for(nr = 0, sidtab = cfg.sidtab; sidtab; sidtab = sidtab->next, nr++)
if(sidtab->num_caid | sidtab->num_provid)
{
if((cl->sidtabs.no & ((SIDTABBITS)1 << nr)) && !sidtab->num_srvid &&
(chk_srvid_match_by_caid_prov(caid, provid, sidtab)))
{ return (0); }
if((cl->sidtabs.ok & ((SIDTABBITS)1 << nr)) &&
(chk_srvid_match_by_caid_prov(caid, provid, sidtab)))
{ rc = 1; }
}
return (rc);
}
int32_t chk_srvid_by_caid_prov_rdr(struct s_reader *rdr, uint16_t caid, uint32_t provid)
{
int32_t nr, rc = 0;
SIDTAB *sidtab;
if(!rdr->sidtabs.ok)
{
if(!rdr->sidtabs.no) { return (1); }
rc = 1;
}
for(nr = 0, sidtab = cfg.sidtab; sidtab; sidtab = sidtab->next, nr++)
if(sidtab->num_caid | sidtab->num_provid)
{
if((rdr->sidtabs.no & ((SIDTABBITS)1 << nr)) && !sidtab->num_srvid &&
(chk_srvid_match_by_caid_prov(caid, provid, sidtab)))
{ return (0); }
if((rdr->sidtabs.ok & ((SIDTABBITS)1 << nr)) &&
(chk_srvid_match_by_caid_prov(caid, provid, sidtab)))
{ rc = 1; }
}
return (rc);
}
int32_t chk_is_betatunnel_caid(uint16_t caid)
{
if(caid == 0x1702 || caid == 0x1722) { return 1; }
if(caid == 0x1801 || caid == 0x1833 || caid == 0x1834 || caid == 0x1835) { return 2; }
return 0;
}
uint16_t chk_on_btun(uint8_t chk_sx, struct s_client *cl, ECM_REQUEST *er)
{
if(chk_is_betatunnel_caid(er->caid))
{
int32_t i;
TUNTAB *ttab;
ttab = &cl->ttab;
if(ttab->ttdata)
{
for(i = 0; i < ttab->ttnum; i++)
{
if(er->caid == ttab->ttdata[i].bt_caidfrom)
{
if(er->srvid == ttab->ttdata[i].bt_srvid) { return ttab->ttdata[i].bt_caidto; }
if(chk_sx && ttab->ttdata[i].bt_srvid == 0xFFFF) { return ttab->ttdata[i].bt_caidto; }
if(!chk_sx && !ttab->ttdata[i].bt_srvid) { return ttab->ttdata[i].bt_caidto; }
}
}
}
if(chk_sx)
return lb_get_betatunnel_caid_to(er);
}
return 0;
}
// server filter for newcamd
int32_t chk_sfilter(ECM_REQUEST *er, PTAB *ptab)
{
#ifdef MODULE_NEWCAMD
int32_t i, j, pi, rc = 1;
uint16_t caid, scaid;
uint32_t prid, sprid;
if(!ptab) { return (1); }
struct s_client *cur_cl = cur_client();
caid = er->caid;
prid = er->prid;
pi = cur_cl->port_idx;
if(cfg.ncd_mgclient)
{ return 1; }
if(ptab->nports && ptab->ports[pi].ncd && ptab->ports[pi].ncd->ncd_ftab.nfilts)
{
for(rc = j = 0; (!rc) && (j < ptab->ports[pi].ncd->ncd_ftab.nfilts); j++)
{
scaid = ptab->ports[pi].ncd->ncd_ftab.filts[j].caid;
if(caid == 0 || (caid != 0 && caid == scaid))
{
for(i = 0; (!rc) && i < ptab->ports[pi].ncd->ncd_ftab.filts[j].nprids; i++)
{
sprid = ptab->ports[pi].ncd->ncd_ftab.filts[j].prids[i];
cs_log_dbg(D_CLIENT, "trying server filter %04X@%06X", scaid, sprid);
if(prid == sprid)
{
rc = 1;
cs_log_dbg(D_CLIENT, "%04X@%06X allowed by server filter %04X@%06X",
caid, prid, scaid, sprid);
}
}
}
}
if(!rc)
{
cs_log_dbg(D_CLIENT, "no match, %04X@%06X rejected by server filters", caid, prid);
snprintf(er->msglog, MSGLOGSIZE, "no server match %04X@%06X",
caid, (uint32_t) prid);
if(!er->rcEx) { er->rcEx = (E1_LSERVER << 4) | E2_IDENT; }
return (rc);
}
}
return (rc);
#else
(void)er;
(void)ptab;
return 1;
#endif
}
static int32_t chk_chid(ECM_REQUEST *er, FTAB *fchid, char *type, char *name)
{
int32_t rc = 1, i, j, found_caid = 0;
if(!fchid->nfilts) { return 1; }
for(i = rc = 0; (!rc) && i < fchid->nfilts; i++)
if(er->caid == fchid->filts[i].caid)
{
found_caid = 1;
for(j = 0; (!rc) && j < fchid->filts[i].nprids; j++)
{
cs_log_dbg(D_CLIENT, "trying %s '%s' CHID filter %04X:%04X",
type, name, fchid->filts[i].caid, fchid->filts[i].prids[j]);
if(er->chid == fchid->filts[i].prids[j])
{
cs_log_dbg(D_CLIENT, "%04X:%04X allowed by %s '%s' CHID filter %04X:%04X",
er->caid, er->chid, type, name, fchid->filts[i].caid,
fchid->filts[i].prids[j]);
rc = 1;
}
}
}
if(!rc)
{
if(found_caid)
cs_log_dbg(D_CLIENT, "no match, %04X:%04X rejected by %s '%s' CHID filter(s)",
er->caid, er->chid, type, name);
else
{
rc = 1;
cs_log_dbg(D_CLIENT, "%04X:%04X allowed by %s '%s' CHID filter, CAID not spezified",
er->caid, er->chid, type, name);
}
}
return (rc);
}
int32_t chk_ident_filter(uint16_t rcaid, uint32_t rprid, FTAB *ftab)
{
int32_t i, j, rc=1;
uint16_t caid=0;
uint32_t prid=0;
if(ftab->nfilts)
{
for(rc=i=0; (!rc) && (i<ftab->nfilts); i++)
{
caid = ftab->filts[i].caid;
if((caid!=0 && caid==rcaid) || caid==0)
{
for(j=0; (!rc) && (j<ftab->filts[i].nprids); j++)
{
prid = ftab->filts[i].prids[j];
if(prid==rprid)
{
rc=1;
}
}
}
}
if(!rc)
{ return 0; }
}
return(rc);
}
int32_t chk_ufilters(ECM_REQUEST *er)
{
int32_t i, j, rc;
uint16_t ucaid;
uint32_t uprid;
struct s_client *cur_cl = cur_client();
rc = 1;
if(cur_cl->ftab.nfilts)
{
FTAB *f = &cur_cl->ftab;
for(i = rc = 0; (!rc) && (i < f->nfilts); i++)
{
ucaid = f->filts[i].caid;
if(er->caid == 0 || ucaid == 0 || (er->caid != 0 && er->caid == ucaid))
{
if (er->prid == 0)
{
cs_log_dbg(D_CLIENT, "%04X@%06X allowed by user '%s' filter caid %04X prid %06X",
er->caid, er->prid, cur_cl->account->usr, ucaid, 0);
rc = 1;
break;
}
for(j = rc = 0; (!rc) && (j < f->filts[i].nprids); j++)
{
uprid = f->filts[i].prids[j];
cs_log_dbg(D_CLIENT, "trying user '%s' filter %04X@%06X",
cur_cl->account->usr, ucaid, uprid);
if(er->prid == uprid)
{
rc = 1;
cs_log_dbg(D_CLIENT, "%04X@%06X allowed by user '%s' filter %04X@%06X",
er->caid, er->prid, cur_cl->account->usr, ucaid, uprid);
}
}
}
}
if(!rc)
{
cs_log_dbg(D_CLIENT, "no match, %04X@%06X rejected by user '%s' filters",
er->caid, er->prid, cur_cl->account->usr);
snprintf(er->msglog, MSGLOGSIZE, "no card support %04X@%06X",
er->caid, (uint32_t) er->prid);
if(!er->rcEx) { er->rcEx = (E1_USER << 4) | E2_IDENT; }
return (rc);
}
}
if(!(rc = chk_class(er, &cur_cl->cltab, "user", cur_cl->account->usr)))
{
if(!er->rcEx) { er->rcEx = (E1_USER << 4) | E2_CLASS; }
}
else if(!(rc = chk_chid(er, &cur_cl->fchid, "user", cur_cl->account->usr)))
if(!er->rcEx) { er->rcEx = (E1_USER << 4) | E2_CHID; }
if(rc) { er->rcEx = 0; }
return (rc);
}
int32_t chk_rsfilter(struct s_reader *reader, ECM_REQUEST *er)
{
int32_t i, rc = 1;
uint16_t caid;
uint32_t prid;
if(reader->ncd_disable_server_filt)
{
cs_log_dbg(D_CLIENT, "%04X@%06X allowed - server filters disabled",
er->caid, er->prid);
return 1;
}
rc = prid = 0;
caid = reader->caid;
if(caid == er->caid)
{
for(i = 0; (!rc) && (i < reader->nprov); i++)
{
prid = (uint32_t)((reader->prid[i][1] << 16) |
(reader->prid[i][2] << 8) |
(reader->prid[i][3]));
cs_log_dbg(D_CLIENT, "trying server '%s' filter %04X@%06X",
reader->device, caid, prid);
if(prid == er->prid)
{
rc = 1;
cs_log_dbg(D_CLIENT, "%04X@%06X allowed by server '%s' filter %04X@%06X",
er->caid, er->prid, reader->device, caid, prid);
}
}
}
if(!rc)
{
cs_log_dbg(D_CLIENT, "no match, %04X@%06X rejected by server '%s' filters",
er->caid, er->prid, reader->device);
if(!er->rcEx) { er->rcEx = (E1_SERVER << 4) | E2_IDENT; }
return 0;
}
return (rc);
}
int32_t chk_rfilter2(uint16_t rcaid, uint32_t rprid, struct s_reader *rdr)
{
int32_t i, j, rc = 1;
uint16_t caid = 0;
uint32_t prid = 0;
if(rdr->ftab.nfilts)
{
for(rc = i = 0; (!rc) && (i < rdr->ftab.nfilts); i++)
{
caid = rdr->ftab.filts[i].caid;
if((caid != 0 && caid == rcaid) || caid == 0)
{
for(j = 0; (!rc) && (j < rdr->ftab.filts[i].nprids); j++)
{
prid = rdr->ftab.filts[i].prids[j];
cs_log_dbg(D_CLIENT, "trying reader '%s' filter %04X@%06X",
rdr->label, caid, prid);
if(prid == rprid)
{
rc = 1;
cs_log_dbg(D_CLIENT, "%04X@%06X allowed by reader '%s' filter %04X@%06X",
rcaid, rprid, rdr->label, caid, prid);
}
}
}
}
if(!rc)
{
cs_log_dbg(D_CLIENT, "no match, %04X@%06X rejected by reader '%s' filters",
rcaid, rprid, rdr->label);
return 0;
}
}
return (rc);
}
static int32_t chk_rfilter(ECM_REQUEST *er, struct s_reader *rdr)
{
return chk_rfilter2(er->caid, er->prid, rdr);
}
int32_t chk_ctab(uint16_t caid, CAIDTAB *ctab)
{
if(!caid || !ctab->ctnum)
{ return 1; }
int32_t i;
for(i = 0; i < ctab->ctnum; i++)
{
CAIDTAB_DATA *d = &ctab->ctdata[i];
if(!d->caid)
{
return 0;
}
if((caid & d->mask) == d->caid)
{ return 1; }
}
return 0;
}
int32_t chk_ctab_ex(uint16_t caid, CAIDTAB *ctab)
{
if(!caid || !ctab->ctnum)
{ return 0; }
int32_t i;
for(i = 0; i < ctab->ctnum; i++)
{
CAIDTAB_DATA *d = &ctab->ctdata[i];
if(!d->caid)
{
return 0;
}
if((caid & d->mask) == d->caid)
{ return 1; }
}
return 0;
}
uint8_t is_localreader(struct s_reader *rdr, ECM_REQUEST *er) //to be used for LB/reader selections checks only
{
if(!rdr) return 0;
if(!is_network_reader(rdr)){
return 1;
}
if(!rdr->localcards.nfilts) { return 0; }
int32_t i, k;
for(i = 0; i < rdr->localcards.nfilts; i++)
{
uint16_t tcaid = rdr->localcards.filts[i].caid;
if(tcaid && tcaid == er->caid) //caid match
{
int32_t nprids = rdr->localcards.filts[i].nprids;
if(!nprids) // No Provider ->Ok
{ return 1; }
for(k = 0; k < nprids; k++)
{
uint32_t prid = rdr->localcards.filts[i].prids[k];
if(prid == er->prid) //Provider matches
{
return 1;
}
}
}
}
return 0;
}
uint8_t chk_is_fixed_fallback(struct s_reader *rdr, ECM_REQUEST *er)
{
if(!rdr->fallback && !rdr->fallback_percaid.nfilts) { return 0; }
if(!rdr->fallback_percaid.nfilts)
if(rdr->fallback)
{ return 1; }
int32_t i, k;
for(i = 0; i < rdr->fallback_percaid.nfilts; i++)
{
uint16_t tcaid = rdr->fallback_percaid.filts[i].caid;
if(tcaid && (tcaid == er->caid || (tcaid < 0x0100 && (er->caid >> 8) == tcaid))) //caid match
{
int32_t nprids = rdr->fallback_percaid.filts[i].nprids;
if(!nprids) // No Provider ->Ok
{ return 1; }
for(k = 0; k < nprids; k++)
{
uint32_t prid = rdr->fallback_percaid.filts[i].prids[k];
if(prid == er->prid) //Provider matches
{
return 1;
}
}
}
}
return 0;
}
uint8_t chk_has_fixed_fallback(ECM_REQUEST *er)
{
struct s_ecm_answer *ea;
struct s_reader *rdr;
int32_t n_falb = 0;
for(ea = er->matching_rdr; ea; ea = ea->next)
{
rdr = ea->reader;
if(chk_is_fixed_fallback(rdr, er))
{ n_falb++; }
}
return n_falb;
}
int32_t matching_reader(ECM_REQUEST *er, struct s_reader *rdr)
{
//simple checks first:
if(!er || !rdr)
{ return (0); }
//reader active?
struct s_client *cl = rdr->client;
if(!cl || !rdr->enable)
{ return (0); }
// if physical reader a card needs to be inserted
if(!is_network_reader(rdr) && rdr->card_status != CARD_INSERTED)
{ return (0); }
//Checking connected & group valid:
struct s_client *cur_cl = er->client; //cur_client();
#ifdef CS_CACHEEX
//Cacheex=3 defines a Cacheex-only reader. never match them.
if(rdr->cacheex.mode == 3)
{ return (0); }
if(rdr->cacheex.mode == 2 && !rdr->cacheex.allow_request)
{ return (0); }
#endif
if(!(rdr->grp & cur_cl->grp))
{ return (0); }
//Checking caids:
if((!er->ocaid || !chk_ctab(er->ocaid, &rdr->ctab)) && !chk_ctab(er->caid, &rdr->ctab))
{
cs_log_dbg(D_TRACE, "caid %04X not found in caidlist reader %s", er->caid, rdr->label);
return 0;
}
if(!is_network_reader(rdr) && ((rdr->caid >> 8) != ((er->caid >> 8) & 0xFF) && (rdr->caid >> 8) != ((er->ocaid >> 8) & 0xFF)))
{
if (!rdr->csystem)
return 0;
int i, caid_found = 0;
for(i = 0; rdr->csystem->caids[i]; i++)
{
uint16_t cs_caid = rdr->csystem->caids[i];
if(!cs_caid)
{ continue; }
if(cs_caid == er->caid || cs_caid == er->ocaid)
{
caid_found = 1;
break;
}
}
if(!caid_found)
{ return 0; }
}
//Supports long ecms?
if(er->ecmlen > 255 && is_network_reader(rdr) && !rdr->ph.large_ecm_support)
{
cs_log_dbg(D_TRACE, "no large ecm support (l=%d) for reader %s", er->ecmlen, rdr->label);
return 0;
}
//Checking services:
if(!chk_srvid(rdr->client, er))
{
cs_log_dbg(D_TRACE, "service %04X not matching reader %s", er->srvid, rdr->label);
return (0);
}
//Checking ident:
if(!chk_rfilter(er, rdr))
{
cs_log_dbg(D_TRACE, "r-filter reader %s", rdr->label);
return (0);
}
//Check ECM nanos:
if(!chk_class(er, &rdr->cltab, "reader", rdr->label))
{
cs_log_dbg(D_TRACE, "class filter reader %s", rdr->label);
return (0);
}
// CDS NL: check for right seca type
if(!is_network_reader(rdr) && er->caid == 0x100 && er->prid == 0x00006a &&
!(er->ecm[8] == 0x00 && er->ecm[9] == 0x00)) // no empty ecm
{
if(er->ecm[8] == 0x00 && rdr->secatype == 2)
{
cs_log_dbg(D_TRACE, "Error: this is a nagra/mediaguard3 ECM and readertype is seca2!");
return 0; // we dont send a nagra/mediaguard3 ecm to a seca2 reader!
}
if((er->ecm[8] == 0x10) && (er->ecm[9] == 0x01) && rdr->secatype == 3)
{
cs_log_dbg(D_TRACE, "Error: this is a seca2 ECM and readertype is nagra/mediaguard3!");
return 0; // we dont send a seca2 ecm to a nagra/mediaguard3 reader!
}
}
// CDS NL: check for right seca type by ECMPID
if(!is_network_reader(rdr) && er->caid == 0x100 && er->prid == 0x00006a)
{
if(rdr->secatype == 2 && er->pid >> 8 == 7)
{
cs_log_dbg(D_TRACE, "Error: this is a nagra/mediaguard3 ECM and readertype is seca2!");
return 0; // we dont send a nagra/mediaguard3 ecm to a seca2 reader!
}
if(rdr->secatype == 3 && er->pid >> 8 == 6)
{
cs_log_dbg(D_TRACE, "Error: this is a seca2 ECM and readertype is nagra/mediaguard3!");
return 0; // we dont send a seca2 ecm to a nagra/mediaguard3 reader!
}
}
//Checking chid:
if(!chk_chid(er, &rdr->fchid, "reader", rdr->label))
{
cs_log_dbg(D_TRACE, "chid filter reader %s", rdr->label);
return (0);
}
//Schlocke reader-defined function, reader-self-check
if(rdr->ph.c_available && !rdr->ph.c_available(rdr, AVAIL_CHECK_CONNECTED, er))
{
cs_log_dbg(D_TRACE, "reader unavailable %s", rdr->label);
return 0;
}
//Checking entitlements:
if(ll_count(rdr->ll_entitlements) > 0)
{
LL_ITER itr = ll_iter_create(rdr->ll_entitlements);
S_ENTITLEMENT *item;
int8_t found = 0;
while((item = ll_iter_next(&itr)))
{
//if (item->caid == er->caid && (!er->prid || !item->provid || item->provid == er->prid)) { //provid check causing problems?
if(item->caid == er->caid || item->caid == er->ocaid) //... so check at least caid only
{
found = 1;
break;
}
}
if(!found)
{
cs_log_dbg(D_TRACE, "entitlements check failed on reader %s", rdr->label);
return 0;
}
}
//Checking ecmlength:
if(rdr->ecm_whitelist.ewnum && er->ecmlen)
{
int32_t i;
int8_t ok = 0, foundident = 0;
for (i = 0; i < rdr->ecm_whitelist.ewnum; i++)
{
ECM_WHITELIST_DATA *d = &rdr->ecm_whitelist.ewdata[i];
if ((d->caid == 0 || d->caid == er->caid) && (d->ident == 0 || d->ident == er->prid))
{
foundident = 1;
if (d->len == er->ecmlen)
{
ok = 1;
break;
}
}
}
if(foundident == 1 && ok == 0)
{
cs_log_dbg(D_TRACE, "ECM is not in ecmwhitelist of reader %s.", rdr->label);
rdr->ecmsfilteredlen += 1;
return (0);
}
}
// ECM Header Check
if(rdr->ecm_hdr_whitelist.ehdata && er->ecmlen)
{
int8_t byteok = 0;
int8_t entryok = 0;
int8_t foundcaid = 0;
int8_t foundprovid = 0;
int16_t len = 0;
int32_t i = 0;
int8_t skip = 0;
int32_t r;
for(r = 0; r < rdr->ecm_hdr_whitelist.ehnum; r++)
{
ECM_HDR_WHITELIST_DATA *tmp = &rdr->ecm_hdr_whitelist.ehdata[r];
skip = 0;
byteok = 0;
entryok = 0;
len = 0;
if(tmp->caid == 0 || tmp->caid == er->caid)
{
foundcaid = 1; //-> caid was in list
//rdr_log_dbg(rdr, D_READER, "Headerwhitelist: found matching CAID: %04X in list", tmp->caid);
if(tmp->provid == 0 || tmp->provid == er->prid)
{
foundprovid = 1; //-> provid was in list
//rdr_log_dbg(rdr, D_READER, "Headerwhitelist: found matching Provid: %06X in list", tmp->provid);
len = tmp->len;
for(i = 0; i < len / 2; i++)
{
if(tmp->header[i] == er->ecm[i])
{
byteok = 1;
//rdr_log_dbg(rdr, D_READER, "ECM Byte: %i of ECMHeaderwhitelist is correct. (%02X = %02X Headerlen: %i)", i, er->ecm[i], tmp->header[i], len/2);
}
else
{
byteok = 0;
//rdr_log_dbg(rdr, D_READER, "ECM Byte: %i of ECMHeaderwhitelist is not valid. (%02X != %02X Headerlen: %i)", i, er->ecm[i], tmp->header[i], len/2);
entryok = 0;
break;
}
if(i == len / 2 - 1 && byteok == 1)
{
entryok = 1;
}
}
}
else
{
//rdr_log_dbg(rdr, D_READER, "ECMHeaderwhitelist: Provid: %06X not found in List-Entry -> skipping check", er->prid);
skip = 1;
continue;
}
}
else
{
//rdr_log_dbg(rdr, D_READER, "ECMHeaderwhitelist: CAID: %04X not found in List-Entry -> skipping check", er->caid);
skip = 1;
continue;
}
if(entryok == 1)
{
break;
}
}
if(foundcaid == 1 && foundprovid == 1 && byteok == 1 && entryok == 1)
{
//cs_log("ECM for %04X@%06X:%04X is valid for ECMHeaderwhitelist of reader %s.", er->caid, er->prid, er->srvid, rdr->label);
}
else
{
if(skip == 0 || (foundcaid == 1 && foundprovid == 1 && entryok == 0 && skip == 1))
{
cs_log_dump_dbg(D_TRACE, er->ecm, er->ecmlen,
"following ECM %04X@%06X:%04X was filtered by ECMHeaderwhitelist of Reader %s from User %s because of not matching Header:",
er->caid, er->prid, er->srvid, rdr->label, username(er->client));
rdr->ecmsfilteredhead += 1;
return (0);
}
}
}
//Simple ring connection check:
//Check ip source+dest:
if(cfg.block_same_ip && IP_EQUAL(cur_cl->ip, rdr->client->ip) &&
get_module(cur_cl)->listenertype != LIS_DVBAPI &&
is_network_reader(rdr))
{
rdr_log_dbg(rdr, D_TRACE, "User (%s) has the same ip (%s) as the reader, blocked because block_same_ip=1!",
username(cur_cl), cs_inet_ntoa(rdr->client->ip));
return 0;
}
if(cfg.block_same_name && strcmp(username(cur_cl), rdr->label) == 0)
{
rdr_log_dbg(rdr, D_TRACE, "User (%s) has the same name as the reader, blocked because block_same_name=1!",
username(cur_cl));
return 0;
}
if(!reader_slots_available(rdr, er)&& er->ecmlen > 0) // check free slots, er->ecmlen>0 trick to skip this test for matching readers in dvbapi module
{
return 0;
}
//All checks done, reader is matching!
return (1);
}
int32_t chk_caid(uint16_t caid, CAIDTAB *ctab)
{
int32_t i;
if (!ctab->ctnum) return caid;
for(i = 0; i < ctab->ctnum; i++)
{
CAIDTAB_DATA *d = &ctab->ctdata[i];
if((caid & d->mask) == d->caid)
return d->cmap ? d->cmap : caid;
}
return -1;
}
int32_t chk_caid_rdr(struct s_reader *rdr, uint16_t caid)
{
if(is_network_reader(rdr))
{
return 1; //reader caid is not real caid
}
else if(rdr->caid == caid)
{
return 1;
}
return 0;
}
int32_t chk_bcaid(ECM_REQUEST *er, CAIDTAB *ctab)
{
int32_t caid;
caid = chk_caid(er->caid, ctab);
if(caid < 0)
{ return 0; }
er->caid = caid;
return 1;
}
/**
* Check for NULL CWs