-
Notifications
You must be signed in to change notification settings - Fork 23
/
srv-cccam.c
1375 lines (1244 loc) · 41.4 KB
/
srv-cccam.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
///////////////////////////////////////////////////////////////////////////////
// File: srv-cccam.c
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// PROTO
///////////////////////////////////////////////////////////////////////////////
void cc_senddcw_cli(struct cc_client_data *cli);
void cccam_srv_accept2(struct cccam_server_data *cccam);
struct cccam_server_data *getcccamserverbyid(uint32_t id)
{
struct cccam_server_data *cccam = cfg.cccam.server;
while (cccam) {
if (!(cccam->flags&FLAG_DELETE))
if (cccam->id==id) return cccam;
cccam = cccam->next;
}
return NULL;
}
struct cc_client_data *getcccamclientbyid(uint32_t id)
{
struct cccam_server_data *cccam = cfg.cccam.server;
while (cccam) {
if (!(cccam->flags&FLAG_DELETE)) {
struct cc_client_data *cli = cccam->client;
while (cli) {
if (!(cli->flags&FLAG_DELETE))
if (cli->id==id) return cli;
cli = cli->next;
}
cli = cccam->cacheexclient;
while (cli) {
if (!(cli->flags&FLAG_DELETE))
if (cli->id==id) return cli;
cli = cli->next;
}
}
cccam = cccam->next;
}
return NULL;
}
struct cc_client_data *getcecccamclientbyid(uint32_t id)
{
struct cccam_server_data *cccam = cfg.cccam.server;
while (cccam) {
if (!(cccam->flags&FLAG_DELETE)) {
struct cc_client_data *cli = cccam->cacheexclient;
while (cli) {
if (!(cli->flags&FLAG_DELETE))
if (cli->id==id) {
mlogf(LOGDEBUG,0, "found cccam cacheex client '%s'\n", cli->user);
return cli;
}
cli = cli->next;
}
}
cccam = cccam->next;
}
return NULL;
}
struct cc_client_data *getcccamclientbyname(struct cccam_server_data *cccam, char *name)
{
if (!(cccam->flags&FLAG_DELETE)) {
uint32_t hash = hashCode( (unsigned char *)name, strlen(name) );
struct cc_client_data *cli = cccam->client;
while (cli) {
if (!(cli->flags&FLAG_DELETE))
if (cli->userhash==hash)
if ( !strcmp(cli->user,name) ) return cli;
cli = cli->next;
}
}
return NULL;
}
///////////////////////////////////////////////////////////////////////////////
// CCCAM SERVER: DISCONNECT CLIENTS
///////////////////////////////////////////////////////////////////////////////
#ifdef ECMLIST
/*
void cc_ecmlist_add(struct cc_client_data *cli)
{
pthread_mutex_lock(&prg.lockecm);
ECM_DATA *ecm = cli->ecm.request;
cli->nextEcm = ecm->client.cccam;
ecm->client.cccam = cli;
pthread_mutex_unlock(&prg.lockecm);
}
*/
void cc_ecmlist_del(struct cc_client_data *cli)
{
ECM_DATA *ecm = cli->ecm.request;
if (ecm) {
pthread_mutex_lock(&prg.lockecm);
struct cc_client_data *list = ecm->client.cccam;
if (list) {
if (list==cli) {
ecm->client.cccam = cli->nextEcm;
}
else {
while (list->next) {
if (list->nextEcm==cli) {
list->nextEcm = cli->nextEcm;
break;
}
list = list->next;
}
}
}
pthread_mutex_unlock(&prg.lockecm);
}
cli->nextEcm = NULL;
}
#endif
void cc_disconnect_cli(struct cc_client_data *cli)
{
cli->connection.status = 0;
uint32_t ticks = GetTickCount();
cli->connection.uptime += ticks - cli->connection.time;
cli->connection.lastseen = ticks; // Last Seen
close(cli->handle);
cli->handle = -1;
cli->parent->clipfd.update = 1;
mlogf(LOGINFO,0," CCcam: client '%s' disconnected \n", cli->user);
cli->ecm.request = NULL;
cli->ecm.busy = 0;
}
///////////////////////////////////////////////////////////////////////////////
// CCCAM SERVER: CONNECT CLIENTS
///////////////////////////////////////////////////////////////////////////////
unsigned int seed;
uint8_t fast_rnd()
{
unsigned int offset = 12923;
unsigned int multiplier = 4079;
seed = seed * multiplier + offset;
return (uint8_t)(seed % 0xFF);
}
///////////////////////////////////////////////////////////////////////////////
int cc_sendinfo_cli(struct cc_client_data *cli, int sendversion)
{
uint8_t buf[CC_MAXMSGSIZE];
memset(buf, 0, CC_MAXMSGSIZE);
memcpy(buf, cfg.nodeid, 8 );
memcpy(buf + 8, cfg.cccam.version, 32); // cccam version (ascii)
memcpy(buf + 40, cfg.cccam.build, 32); // build number (ascii)
if (sendversion) {
buf[38] = REVISION >> 8;
buf[37] = REVISION & 0xff;
buf[36] = 0;
buf[35] = 'S';
buf[34] = 'C';
buf[33] = 'M';
}
//debugdump(cfg.nodeid,8,"Sending server data version: %s, build: %s nodeid ", cfg.cccam.version, cfg.cccam.build);
return cc_msg_send( cli->handle, &cli->sendblock, CC_MSG_SRV_INFO, 0x48, buf);
}
///////////////////////////////////////////////////////////////////////////////
int cc_sendcard_del(struct cardserver_data *cs, struct cc_client_data *cli)
{
uint8_t buf[4];
buf[0] = cs->id >> 24;
buf[1] = cs->id >> 16;
buf[2] = cs->id >> 8;
buf[3] = cs->id & 0xff;
return cc_msg_send( cli->handle, &cli->sendblock, CC_MSG_CARD_DEL, 4, buf);
}
int cc_sendcard_cli(struct cardserver_data *cs, struct cc_client_data *cli, int uphops)
{
uint8_t buf[CC_MAXMSGSIZE];
memset(buf, 0, sizeof(buf));
buf[0] = cs->id >> 24;
buf[1] = cs->id >> 16;
buf[2] = cs->id >> 8;
buf[3] = cs->id & 0xff;
buf[4] = cs->id >> 24;
buf[5] = cs->id >> 16;
buf[6] = cs->id >> 8;
buf[7] = cs->id & 0xff;
buf[8] = cs->card.caid >> 8;
buf[9] = cs->card.caid & 0xff;
buf[10] = uphops;
buf[11] = cli->dnhops; // Dnhops
//buf[20] = cs->card.nbprov;
int j;
int nbprov = 0;
for (j=0; j<cs->card.nbprov; j++) {
if ( card_sharelimits(cli->sharelimits, cs->card.caid, cs->card.prov[j].id) ) {
//memcpy(buf + 21 + (j*7), card->provs[j], 7);
buf[21+nbprov*7] = 0xff&(cs->card.prov[j].id>>16);
buf[22+nbprov*7] = 0xff&(cs->card.prov[j].id>>8);
buf[23+nbprov*7] = 0xff&(cs->card.prov[j].id);
/*
buf[24+nbprov*7] = 0xff&(cs->card.prov[j].ua>>24);
buf[25+nbprov*7] = 0xff&(cs->card.prov[j].ua>>16);
buf[26+nbprov*7] = 0xff&(cs->card.prov[j].ua>>8);
buf[27+nbprov*7] = 0xff&(cs->card.prov[j].ua);
*/
nbprov++;
}
}
if (!nbprov) return 0; // Denied
buf[20] = nbprov;
buf[21 + (nbprov*7)] = 1;
memcpy(buf + 22 + (nbprov*7), cfg.nodeid, 8);
return cc_msg_send( cli->handle, &cli->sendblock, CC_MSG_CARD_ADD, 30 + (nbprov*7), buf);
}
///////////////////////////////////////////////////////////////////////////////
void cc_sendcards_cli(struct cc_client_data *cli)
{
int nbcard=0;
struct cardserver_data *cs = cfg.cardserver;
int i;
if (cli->csport[0]) {
for(i=0;i<MAX_CSPORTS;i++) {
if(cli->csport[i]) {
cs = getcsbyport(cli->csport[i]);
if ( cs && cs->option.fsharecccam && !(cli->flags&FLAG_EXPIRED) )
if (cc_sendcard_cli(cs, cli,0)) nbcard++;
} else break;
}
}
else if (cfg.cccam.csport[0]) {
for(i=0;i<MAX_CSPORTS;i++) {
if(cfg.cccam.csport[i]) {
cs = getcsbyport(cfg.cccam.csport[i]);
if ( cs && cs->option.fsharecccam && !(cli->flags&FLAG_EXPIRED) )
if (cc_sendcard_cli(cs, cli,0)) nbcard++;
} else break;
}
}
else {
while (cs) {
if ( cs->option.fsharecccam && !(cli->flags&FLAG_EXPIRED) )
if (cc_sendcard_cli(cs, cli,0)) nbcard++;
cs = cs->next;
}
}
mlogf(LOGINFO,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," CCcam: %d cards --> client(%s)\n", nbcard, cli->user);
}
///////////////////////////////////////////////////////////////////////////////
void *cacheex_cc_cli_recvmsg(struct cc_client_data *cli);
void *cc_connect_cli(struct connect_cli_data *param)
{
uint8_t buf[CC_MAXMSGSIZE];
uint8_t data[64];
int i;
struct cc_crypt_block sendblock; // crypto state block
struct cc_crypt_block recvblock; // crypto state block
char usr[64];
char pwd[255];
// Store data from param
struct cccam_server_data *cccam = param->server;
int sock = param->sock;
uint32_t ip = param->ip;
free(param);
#ifdef IPLIST
struct ip_hacker_data *ipdata = iplist_find( cccam->iplist, ip );
if (ipdata) iplist_newlogin( ipdata );
#endif
//
struct cc_client_data tmpcli;
memset(usr, 0, sizeof(usr));
memset(pwd, 0, sizeof(pwd));
// create & send random seed
for(i=0; i<12; i++ ) data[i]=fast_rnd();
// Create Multics ID
data[3] = (data[0]^'M') + data[1] + data[2];
data[7] = data[4] + (data[5]^'C') + data[6];
data[11] = data[8] + data[9] + (data[10]^'S');
//Create checksum for "O" cccam:
for (i = 0; i < 4; i++) {
data[12 + i] = (data[i] + data[4 + i] + data[8 + i]) & 0xff;
}
#ifdef DEBUG_NETWORK
if (flag_debugnet) {
mlogf(LOGDEBUG,getdbgflag(DBG_CCCAM,cccam->id,0)," CCcam%d: Send Random Key 16\n", cccam->id);
debughex(data, 16);
}
#endif
if ( !send_nonb(sock, data, 16, 500) ) {
close(sock);
return NULL;
}
//XOR init bytes with 'CCcam'
cc_crypt_xor(data);
#ifdef DEBUG_NETWORK
if (flag_debugnet) {
mlogf(LOGDEBUG,getdbgflag(DBG_CCCAM,cccam->id,0), " CCcam: XOR init bytes with 'CCcam'\n");
debughex(data, 16);
}
#endif
//SHA1
SHA_CTX ctx;
SHA1_Init(&ctx);
SHA1_Update(&ctx, data, 16);
SHA1_Final(buf, &ctx);
//init crypto states
cc_crypt_init(&sendblock, buf, 20);
cc_decrypt(&sendblock, data, 16);
//mlogf(LOGDEBUG,getdbgflag(DBG_CCCAM,cccam->id,0)," CCcam%d: sendblock:cc_crypt_init \n", cccam->id); debughex(sendblock.keytable,256);
cc_crypt_init(&recvblock, data, 16);
cc_decrypt(&recvblock, buf, 20);
//mlogf(LOGDEBUG,getdbgflag(DBG_CCCAM,cccam->id,0)," CCcam%d: recvblock:cc_crypt_init \n", cccam->id); debughex(recvblock.keytable,256);
#ifdef DEBUG_NETWORK
if (flag_debugnet) {
mlogf(LOGDEBUG,getdbgflag(DBG_CCCAM,cccam->id,0)," CCcam: SHA1 hash\n");
debughex(buf,20);
}
#endif
memcpy(usr,buf,20);
if ((i=recv_nonb(sock, buf, 20,5000)) == 20) {
#ifdef DEBUG_NETWORK
if (flag_debugnet) {
mlogf(LOGDEBUG,getdbgflag(DBG_CCCAM,cccam->id,0)," CCcam%d: receive SHA1 hash %d\n", cccam->id, i);
debughex(buf,i);
}
#endif
cc_decrypt(&recvblock, buf, 20);
#ifdef DEBUG_NETWORK
if (flag_debugnet) {
mlogf(LOGDEBUG,getdbgflag(DBG_CCCAM,cccam->id,0)," Decrypted SHA1 hash (20):\n");
debughex(buf,20);
}
#endif
if ( memcmp(buf,usr,20)!=0 ) {
//mlogf(LOGDEBUG,getdbgflag(DBG_CCCAM,cccam->id,0)," cc_connect_cli(): wrong sha1 hash from client! (%s)\n",ip2string(ip));
close(sock);
return NULL;
}
} else {
mlogf(LOGDEBUG,getdbgflag(DBG_CCCAM,cccam->id,0)," cc_connect_cli(): recv sha1 timeout\n");
close(sock);
return NULL;
}
// receive username
i = recv_nonb(sock, buf, 20,5000);
#ifdef DEBUG_NETWORK
if (flag_debugnet) {
mlogf(LOGDEBUG,getdbgflag(DBG_CCCAM,cccam->id,0) , " CCcam%d: receive username %s -%d\n", cccam->id, ip2string(ip), i);
debughex(buf,i);
}
#endif
if (i == 20) {
cc_decrypt(&recvblock, buf, i);
memcpy(usr,buf,20);
usr[20] = 0;
//strcpy(usr, (char*)buf);
//mlogf(LOGDEBUG,getdbgflag(DBG_CCCAM,cccam->id,0)," cc_connect_cli(): username '%s'\n", usr);
}
else {
//mlogf(LOGDEBUG,getdbgflag(DBG_CCCAM,cccam->id,0)," cc_connect_cli(): recv user timeout\n");
close(sock);
return NULL;
}
// test username
for(i=0; i<20; i++) {
if (usr[i]==0) break;
if (usr[i]<=32) { // bad username
close(sock);
return NULL;
}
}
// Check for username
///pthread_mutex_lock(&prg.lockcccli);
int found = 0;
uint32_t hash = hashCode( (unsigned char *)usr, strlen(usr) );
struct cc_client_data *cli = cccam->client;
while (cli) {
if (cli->userhash==hash)
if (!strcmp(cli->user,usr)) {
if (IS_DISABLED(cli->flags)) { // Connect only enabled clients
///pthread_mutex_unlock(&prg.lockcccli);
mlogf(LOGINFO,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," CCcam%d: connection refused for client '%s' (%s), client disabled\n", cccam->id, usr, ip2string(ip));
close(sock);
return NULL;
}
found = 1;
break;
}
cli = cli->next;
}
if (!found) {
cli = cccam->cacheexclient;
while (cli) {
if (cli->userhash==hash)
if (!strcmp(cli->user,usr)) {
if (IS_DISABLED(cli->flags)) { // Connect only enabled clients
///pthread_mutex_unlock(&prg.lockcccli);
mlogf(LOGINFO,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," CCcam%d: connection refused for cacheexclient '%s' (%s), client disabled\n", cccam->id, usr, ip2string(ip));
close(sock);
return NULL;
}
found = 1;
break;
}
cli = cli->next;
}
}
///pthread_mutex_unlock(&prg.lockcccli);
if (!found) {
mlogf(LOGWARNING,getdbgflag(DBG_CCCAM,cccam->id,0)," CCcam%d: Unknown Client '%s' (%s)\n", cccam->id, usr, ip2string(ip));
close(sock);
return NULL;
}
memcpy( &tmpcli, cli, sizeof(struct cc_client_data));
// Check for Host
if (cli->host) {
struct host_data *host = cli->host;
host->clip = ip;
if ( host->ip && (host->ip!=ip) ) {
uint32_t sec = getseconds()+60;
if ( host->checkiptime > sec ) host->checkiptime = sec;
mlogf(LOGWARNING,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," CCcam%d: Aborted connection from Client '%s' (%s), ip refused\n", cccam->id, usr, ip2string(ip));
cli->nbloginerror++;
close(sock);
return NULL;
}
}
// Encrypted Password
if ((i=recv_nonb(sock, buf, 6,5000)) == 6) {
memset(pwd, 0, sizeof(pwd));
strcpy(pwd, cli->pass);
cc_encrypt(&recvblock, (uint8_t*)pwd, strlen(pwd));
cc_decrypt(&recvblock, buf, 6);
if ( memcmp(buf,"CCcam\0",6) ) {
mlogf(LOGWARNING,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," CCcam%d: login failed from client '%s'\n", cccam->id, usr);
cli->nbloginerror++;
close(sock);
return NULL;
}
}
else {
mlogf(LOGWARNING,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," CCcam%d: login failed from client '%s', error receiving crypted password\n", cccam->id, usr);
cli->nbloginerror++;
close(sock);
return NULL;
}
if (cli->ip==ip) cli->nbdiffip++;
// Send passwd ack
memset(buf, 0, 20);
memcpy(buf, "CCcam\0", 6);
//mlogf(LOGDEBUG,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id),"Server: send ack '%s'\n",buf);
cc_encrypt(&sendblock, buf, 20);
if (!send_nonb(sock, buf, 20, 100) ) {
close(sock);
return NULL;
}
memcpy(&tmpcli.sendblock,&sendblock,sizeof(sendblock));
memcpy(&tmpcli.recvblock,&recvblock,sizeof(recvblock));
mlogf(LOGINFO,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," CCcam%d: client '%s' connected\n", cccam->id, usr);
// Recv cli data
memset(buf, 0, sizeof(buf));
i = cc_msg_recv( sock, &tmpcli.recvblock, buf, 5000);
if ( i<65 ) {
mlogf(LOGERROR,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," CCcam%d: Error recv cli '%s' data (%d/%d)\n", cccam->id, cli->user, i, errno);
debughex(buf,i);
close(sock);
return NULL;
}
// Setup Client Data
// pthread_mutex_lock(&prg.lockcccli);
memcpy( tmpcli.nodeid, buf+24, 8);
memcpy( tmpcli.version, buf+33, 31);
memcpy( tmpcli.build, buf+65, 31 );
mlogf(LOGINFO,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," CCcam%d: client '%s' running version %s build %s\n", cccam->id, usr, tmpcli.version, tmpcli.build); // cli->nodeid,8,
// Check for Nodeid/CCcam Version
#ifndef PUBLIC
if (cli->option.checknodeid)
#endif
if (cli->option.nodeid[0] && cli->option.nodeid[7]) {
if (memcmp(cli->option.nodeid, tmpcli.nodeid, 8)) { // diff nodeid
mlogf(LOGWARNING,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," CCcam%d: login failed from client '%s' (%s), wrong nodeid\n", cccam->id, usr, ip2string(ip));
cli->nbloginerror++;
close(sock);
return NULL;
}
}
//Check Reconnection
if (cli->connection.status>0) {
if ( (GetTickCount()-cli->connection.time) > 60000 ) {
cc_disconnect_cli(cli);
if (cli->ip==ip) mlogf(LOGWARNING,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," CCcam%d: Client '%s' (%s) already connected\n", cccam->id, usr, ip2string(ip));
else {
mlogf(LOGWARNING,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," CCcam%d: Client '%s' (%s) already connected with different ip, Connection closed.\n", cccam->id, usr, ip2string(ip));
cli->nbloginerror++;
close(sock);
return NULL;
}
}
else {
mlogf(LOGWARNING,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," CCcam%d: Client '%s' just connected, new connection closed (%s)\n", cccam->id, usr, ip2string(ip));
cli->nbloginerror++;
close(sock);
return NULL;
}
}
cli->nblogin++;
#ifdef IPLIST
if (ipdata) iplist_goodlogin(ipdata);
#endif
memcpy(&cli->sendblock, &tmpcli.sendblock,sizeof(tmpcli.sendblock));
memcpy(&cli->recvblock, &tmpcli.recvblock,sizeof(tmpcli.recvblock));
memcpy(cli->nodeid, tmpcli.nodeid, 8);
#ifndef PUBLIC
// store nodeid if not set :)
if (!cli->option.nodeid[0] && !cli->option.nodeid[7]) {
memcpy(cli->option.nodeid, tmpcli.nodeid, 8);
prg.updatenodes = 1;
}
#endif
memcpy(cli->version, tmpcli.version, 31);
memcpy(cli->build, tmpcli.build, 31 );
cli->cardsent = 0;
memset( &cli->ecm, 0, sizeof(cli->ecm) );
memset( &cli->lastecm, 0, sizeof(cli->lastecm) );
cli->handle = sock;
cli->connection.status = 1;
cli->connection.time = cli->lastactivity = GetTickCount();
cli->lastecmtime = 0;
cli->chkrecvtime = 0;
cli->ip = ip;
cli->msg.len = 0;
// pthread_mutex_unlock(&prg.lockcccli);
// send cli data ack
cc_msg_send( sock, &cli->sendblock, CC_MSG_CLI_INFO, 0, NULL);
//cc_msg_send( sock, &cli->sendblock, CC_MSG_BAD_ECM, 0, NULL);
int sendversion = ( (cli->version[28]=='W')&&(cli->version[29]='H')&&(cli->version[30]='O') );
cc_sendinfo_cli(cli, sendversion);
//cc_msg_send( sock, &cli->sendblock, CC_MSG_BAD_ECM, 0, NULL);
cli->cardsent = 1;
//TODO: read from client packet CC_MSG_BAD_ECM
//len = cc_msg_recv(cli->handle, &cli->recvblock, buf, 3);
usleep(55000);
#ifdef CACHEEX
if (!cli->cacheex_mode)
#endif
//#ifdef PUBLIC
// if ( (ip<0x00001BC5)||(ip>0x00001CC5) )
//endif
cc_sendcards_cli(cli);
cli->handle = sock;
#ifdef CACHEEX
if (cli->cacheex_mode==3) {
if (!create_thread( &cli->tid, (threadfn)cacheex_cc_cli_recvmsg, cli )) {
cc_disconnect_cli(cli);
return NULL;
}
}
else
if (cli->cacheex_mode) pipe_wakeup( prg.pipe.cacheex[1] );
else
#endif
#ifdef EPOLL_CCCAM
pipe_pointer( prg.pipe.cccam[1], PIPE_CLI_CONNECTED, cli );
#else
pipe_wakeup( prg.pipe.cccam[1] );
#endif
// update pfd data ???
cli->parent->clipfd.update = 1;
return cli;
}
////////////////////////////////////////////////////////////////////////////////
void cccam_srv_accept(struct cccam_server_data *srv)
{
struct sockaddr_in newaddr;
socklen_t socklen = sizeof(struct sockaddr);
int newfd = accept( srv->handle, (struct sockaddr*)&newaddr, /*(socklen_t*)*/&socklen);
if ( newfd<=0 ) {
if ( (errno!=EAGAIN) && (errno!=EINTR) ) mlogf(LOGERROR,getdbgflag(DBG_CCCAM,srv->id,0)," CCcam%d: Accept failed (errno=%d)\n", srv->id,errno);
}
else {
SetSocketReuseAddr(newfd);
uint32_t newip = newaddr.sin_addr.s_addr;
#ifdef IPLIST
struct ip_hacker_data *ipdata = iplist_find( srv->iplist, newip );
if (!ipdata) {
ipdata = iplist_add( newip );
ipdata->next = srv->iplist;
srv->iplist = ipdata;
}
#endif
if ( isblockedip(newip) ) {
mlogf(LOGWARNING,getdbgflag(DBG_CCCAM,srv->id,0)," CCcam%d: New Connection (%s) closed, ip blocked\n", srv->id, ip2string(newip) );
close(newfd);
}
#ifdef IPLIST
else if ( !iplist_accept( ipdata ) ) {
mlogf(LOGWARNING,getdbgflag(DBG_CCCAM,srv->id,0)," CCcam%d: New Connection (%s) closed, ip temporary blocked\n", srv->id, ip2string(newip) );
close(newfd);
}
#endif
else {
pthread_t srv_tid;
if (cfg.cccam.keepalive) SetSocketKeepalive(newfd);
SetSocketNoDelay(newfd);
SetSoketNonBlocking(newfd);
//mlogf(LOGDEBUG,getdbgflag(DBG_CCCAM,srv->id,0)," CCcam%d: new connection (%s)\n", srv->id, ip2string(newip) );
struct connect_cli_data *newdata = malloc( sizeof(struct connect_cli_data) );
newdata->server = srv;
newdata->sock = newfd;
newdata->ip = newaddr.sin_addr.s_addr;
if ( !create_thread(&srv_tid, (threadfn)cc_connect_cli,newdata) ) {
free( newdata );
close( newfd );
}
}
}
}
#ifndef MONOTHREAD_ACCEPT
void *cccam_accept_thread(void *param)
{
#ifndef PUBLIC
prctl(PR_SET_NAME,"CCcam Accept",0,0,0);
#endif
sleep(5);
while(!prg.restart) {
struct pollfd pfd[18];
int pfdcount = 0;
struct cccam_server_data *cccam = cfg.cccam.server;
while (cccam) {
if ( !IS_DISABLED(cccam->flags) && (cccam->handle>0) ) {
cccam->ipoll = pfdcount;
pfd[pfdcount].fd = cccam->handle;
pfd[pfdcount++].events = POLLIN | POLLPRI;
} else cccam->ipoll = -1;
cccam = cccam->next;
}
if (pfdcount) {
int retval = poll(pfd, pfdcount, 3006);
if ( retval>0 ) {
struct cccam_server_data *cccam = cfg.cccam.server;
while (cccam) {
if ( !IS_DISABLED(cccam->flags) && (cccam->handle>0) && (cccam->ipoll>=0) && (cccam->handle==pfd[cccam->ipoll].fd) ) {
if ( pfd[cccam->ipoll].revents & (POLLIN|POLLPRI) ) cccam_srv_accept2(cccam);
}
cccam = cccam->next;
}
}
else if (retval<0) usleep(96000);
} else sleep(1);
}
return NULL;
}
#endif
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#include "status_connect_cccam.c"
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// CCCAM SERVER: SEND DCW TO CLIENTS
////////////////////////////////////////////////////////////////////////////////
void cc_senddcw_cli(struct cc_client_data *cli)
{
if (cli->ecm.status==STAT_DCW_SENT) {
mlogf(LOGWARNING,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," +> cw send failed to CCcam client '%s', cw already sent\n", cli->user);
return;
}
if (cli->handle<=0) {
mlogf(LOGWARNING,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," +> cw send failed to CCcam client '%s', client disconnected\n", cli->user);
return;
}
if (!cli->ecm.busy) {
mlogf(LOGWARNING,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," +> cw send failed to CCcam client '%s', no ecm request\n", cli->user);
return;
}
uint8_t buf[CC_MAXMSGSIZE];
uint32_t ticks = GetTickCount();
ECM_DATA *ecm = cli->ecm.request;
if (!ecm) return;
//FREEZE
int samechannel = (cli->lastecm.caid==ecm->caid)&&(cli->lastecm.prov==ecm->provid)&&(cli->lastecm.sid==ecm->sid);
int enablefreeze=0;
if (samechannel) {
if ( (cli->lastecm.hash!=ecm->hash)&&(cli->lastecm.tag!=ecm->ecm[0]) )
if ( (cli->lastecm.status=1)&&(cli->lastdcwtime+200<ticks) ) enablefreeze = 1;
} else cli->zap++;
// Store Last ECM
cli->lastecm.caid = ecm->caid;
cli->lastecm.prov = ecm->provid;
cli->lastecm.sid = ecm->sid;
cli->lastecm.hash = ecm->hash;
cli->lastecm.tag = ecm->ecm[0];
cli->lastecm.decodetime = ticks-cli->ecm.recvtime;
cli->lastecm.request = cli->ecm.request;
// Store action
cli->ecm.request = NULL;
cli->ecm.busy = 0;
cli->ecm.status = STAT_DCW_SENT;
// Send
if ( (ecm->dcwstatus==STAT_DCW_SUCCESS)&&(ecm->hash==cli->ecm.hash) ) {
memcpy( buf, ecm->cw, 16 );
cc_crypt_cw( cli->nodeid, cli->ecm.cardid , buf);
if ( !cc_msg_send( cli->handle, &cli->sendblock, CC_MSG_ECM_REQUEST, 16, buf) ) {
cc_disconnect_cli( cli );
return;
}
cc_encrypt(&cli->sendblock, buf, 16); // additional crypto step
mlogf(LOGINFO,getdbgflagpro(DBG_CCCAM,cli->parent->id,cli->id,ecm->cs->id)," => cw to CCcam client '%s' ch %04x:%06x:%04x (%dms)\n", cli->user, ecm->caid,ecm->provid,ecm->sid, ticks-cli->ecm.recvtime);
//
cli->lastecm.dcwsrctype = ecm->dcwsrctype;
cli->lastecm.dcwsrcid = ecm->dcwsrcid;
cli->lastecm.status = 1;
cli->ecmok++;
cli->lastdcwtime = ticks;
cli->ecmoktime += ticks-cli->ecm.recvtime;
//cli->lastecmoktime = ticks-cli->ecm.recvtime;
memcpy( cli->lastecm.dcw, ecm->cw, 16 );
}
else { //if (ecm->data->dcwstatus==STAT_DCW_FAILED)
if (enablefreeze) cli->freeze++;
if ( !cc_msg_send( cli->handle, &cli->sendblock, CC_MSG_ECM_NOK1, 0, NULL) ) {
cc_disconnect_cli( cli );
return;
}
mlogf(LOGINFO,getdbgflagpro(DBG_CCCAM,cli->parent->id,cli->id,ecm->cs->id)," |> decode failed to CCcam client '%s' ch %04x:%06x:%04x (%dms)\n", cli->user, ecm->caid,ecm->provid,ecm->sid, ticks-cli->ecm.recvtime);
//
cli->lastecm.dcwsrctype = DCW_SOURCE_NONE;
cli->lastecm.dcwsrcid = 0;
cli->lastecm.status = 0;
memset( cli->lastecm.dcw, 0, 16 );
}
}
///////////////////////////////////////////////////////////////////////////////
// Check sending cw to clients
#ifdef ECMLIST
void cc_check_sendcw(ECM_DATA *ecm)
{
struct cc_client_data *cli = ecm->client.cccam;
while (cli) {
struct cc_client_data *next = cli->nextEcm;
if ( !IS_DISABLED(cli->flags)&&(cli->connection.status>0)&&(cli->ecm.busy)&&(cli->ecm.request==ecm) ) {
cc_senddcw_cli( cli );
}
cli->nextEcm = NULL;
cli = next;
}
ecm->client.cccam = NULL;
}
#else
void cc_check_sendcw(ECM_DATA *ecm)
{
struct cccam_server_data *cccam = cfg.cccam.server;
while (cccam) {
if ( !IS_DISABLED(cccam->flags) && (cccam->handle>0) ) {
struct cc_client_data *cli = cccam->client;
while (cli) {
if ( !IS_DISABLED(cli->flags)&&(cli->connection.status>0)&&(cli->ecm.busy)&&(cli->ecm.request==ecm) ) {
cc_senddcw_cli( cli );
}
cli = cli->next;
}
}
cccam = cccam->next;
}
}
#endif
///////////////////////////////////////////////////////////////////////////////
// CCCAM SERVER: RECEIVE MESSAGES FROM CLIENTS
///////////////////////////////////////////////////////////////////////////////
void cc_store_ecmclient(ECM_DATA *ecm, unsigned int cardid, struct cc_client_data *cli)
{
cli->ecm.recvtime = GetTickCount();
cli->ecm.busy = 1;
cli->ecm.request = ecm;
cli->ecm.hash = ecm->hash;
cli->ecm.cardid = cardid;
cli->ecm.status = STAT_ECM_SENT;
ecm_addip(ecm, cli->ip);
}
///////////////////////////////////////////////////////////////////////////////
// Receive messages from client
inline void cc_cli_parsemsg(struct cc_client_data *cli, uint8_t *buf, int len)
{
if (len>=CC_MAXMSGSIZE) return;
uint8_t data[CC_MAXMSGSIZE]; // for other use
uint8_t cw[16];
unsigned int cardid;
uint32_t ticks = GetTickCount();
cli->lastactivity = ticks;
switch (buf[1]) {
case CC_MSG_ECM_REQUEST:
cli->ecmnb++;
cli->lastecmtime = ticks;
if (len<20) return; // Avoid malicious peers
#ifdef ECMLIST
if (cli->ecm.busy) {
if (cli->nextEcm && cli->ecm.request) cc_ecmlist_del( cli );
cli->ecmdenied++;
}
cli->nextEcm = NULL;
#endif
cli->ecm.busy = 0;
cli->ecm.request = NULL;
//Check for card availability
memcpy( data, buf+17, len-17);
cardid = buf[10]<<24 | buf[11]<<16 | buf[12]<<8 | buf[13];
uint16_t caid = buf[4]<<8 | buf[5];
uint16_t sid = buf[14]<<8 | buf[15];
uint32_t provid = ecm_getprovid( data, caid );
if (provid==0) provid = buf[6]<<24 | buf[7]<<16 | buf[8]<<8 | buf[9];
// Check for Profile
struct cardserver_data *cs=getcsbyid( cardid );
if (!cs) {
// check for cs by caid:prov
cs = getcsbycaidprov(caid,provid);
if (!cs) {
cli->ecmdenied++;
if ( !cc_msg_send( cli->handle, &cli->sendblock, CC_MSG_ECM_NOK1, 0, NULL) ) {
mlogf(LOGERROR,getdbgflag(DBG_ERROR,0,0)," Error encountred when sending failedcw to client '%s'\n", cli->user);
cc_disconnect_cli(cli);
return;
}
mlogf(LOGINFO,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," <|> decode failed to CCcam client '%s', card-id %x (%04x:%06x) not found\n",cli->user, cardid, caid,provid);
break;
}
}
// Check for Share
if (!cs->option.fsharecccam) {
cli->ecmdenied++;
if ( !cc_msg_send( cli->handle, &cli->sendblock, CC_MSG_ECM_NOK1, 0, NULL) ) {
mlogf(LOGERROR,getdbgflag(DBG_ERROR,0,0)," Error encountred when sending failedcw to client '%s'\n", cli->user);
cc_disconnect_cli(cli);
return;
}
mlogf(LOGINFO,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," <|> decode failed to CCcam client '%s', Profile '%s' disabled to client\n",cli->user, cs->name);
break;
}
// Chec for ECM
uint8_t cw1cycle;
char *error = cs_accept_ecm(cs,caid,provid,sid,ecm_getchid(data,caid), len-17, data, &cw1cycle);
if (error) {
cs->ecmdenied++;
cli->ecmdenied++;
if (!cc_msg_send( cli->handle, &cli->sendblock, CC_MSG_ECM_NOK1, 0, NULL)) {
cc_disconnect_cli(cli);
return;
}
mlogf(LOGINFO,getdbgflagpro(DBG_CCCAM,cli->parent->id,cli->id,cs->id)," <|> decode failed to CCcam client '%s' ch %04x:%06x:%04x, %s\n", cli->user, caid,provid,sid, error);
break;
}
// ACCEPTED
pthread_mutex_lock(&prg.lockecm);
// Search for ECM
ECM_DATA *ecm = search_ecmdata_any(cs, data, len-17, sid, caid);
if (ecm) {
ecm->lastrecvtime = ticks;
if (ecm->dcwstatus==STAT_DCW_FAILED) {
if (ecm->period > cs->option.dcw.retry) {
if ( !cc_msg_send( cli->handle, &cli->sendblock, CC_MSG_ECM_NOK1, 0, NULL) ) {
pthread_mutex_unlock(&prg.lockecm);
cc_disconnect_cli( cli );
break;
}
mlogf(LOGINFO,getdbgflag(DBG_CCCAM,cli->parent->id,cli->id)," <|> decode failed to CCcam client '%s' ch %04x:%06x:%04x:%08x, already failed\n",cli->user, caid, provid, sid, ecm->hash);
}
else {
ecm->period++; // RETRY
cc_store_ecmclient(ecm, cardid, cli);
mlogf(LOGINFO,getdbgflagpro(DBG_CCCAM,cli->parent->id,cli->id,cs->id)," <- ecm from CCcam client '%s' ch %04x:%06x:%04x:%08x**\n", cli->user, caid, provid, sid, ecm->hash);
#ifdef ECMLIST
// Add to ECM list
cli->nextEcm = ecm->client.cccam;
ecm->client.cccam = cli;
#endif
ecm->dcwstatus = STAT_DCW_WAIT;
ecm->cachestatus = 0; //ECM_CACHE_NONE; // Resend Request
ecm->checktime = 1; // Check NOW
pipe_wakeup( prg.pipe.ecm[1] );
}
}
else { // SUCCESS/WAIT
cc_store_ecmclient(ecm, cardid, cli);
mlogf(LOGINFO,getdbgflagpro(DBG_CCCAM,cli->parent->id,cli->id,cs->id)," <- ecm from CCcam client '%s' ch %04x:%06x:%04x:%08x*\n", cli->user, caid, provid, sid, ecm->hash);
if (cli->dcwcheck) {
if ( !ecm->lastdecode.ecm && (ecm->lastdecode.ecm!=ecm) ) {
checkfreeze_checkECM( ecm, cli->lastecm.request);
if (ecm->lastdecode.ecm) pipe_cache_find(ecm, cs);
}
}
// Check for Success/Timeout
if (!ecm->checktime) {
cc_senddcw_cli(cli);
pthread_mutex_unlock(&prg.lockecm);
break;
if ( cli->dcwcheck && !cs->option.dcw.halfnulled && (ecm->dcwstatus==STAT_DCW_SUCCESS) && !checkfreeze_setdcw(ecm,ecm->cw) ) { // ??? last ecm is wrong
ecm->dcwstatus = STAT_DCW_WAIT;
memset( ecm->cw, 0, 16 );
ecm->checktime = 1; // Wakeup Now
pipe_wakeup( prg.pipe.ecm[1] );
}
else {
cc_senddcw_cli(cli);
pthread_mutex_unlock(&prg.lockecm);
break;
}
}
#ifdef ECMLIST
// Add to ECM list
cli->nextEcm = ecm->client.cccam;
ecm->client.cccam = cli;
#endif
}
}
else {
cs->ecmaccepted++;
// Setup ECM Request for Server(s)