1 | #define MODULE_LOG_PREFIX "emm"
|
---|
2 |
|
---|
3 | #include "globals.h"
|
---|
4 | #include "cscrypt/md5.h"
|
---|
5 | #include "module-dvbapi.h"
|
---|
6 | #include "module-led.h"
|
---|
7 | #include "oscam-client.h"
|
---|
8 | #include "oscam-config.h"
|
---|
9 | #include "oscam-emm.h"
|
---|
10 | #include "oscam-string.h"
|
---|
11 | #include "oscam-time.h"
|
---|
12 | #include "oscam-work.h"
|
---|
13 | #include "reader-common.h"
|
---|
14 | #include "oscam-chk.h"
|
---|
15 | #include "oscam-emm-cache.h"
|
---|
16 |
|
---|
17 | const char *entitlement_type[] = { "", "package", "PPV-Event", "chid", "tier", "class", "PBM", "admin" };
|
---|
18 |
|
---|
19 | static struct timeb last_emm_clean;
|
---|
20 | static int8_t cs_emmlen_is_blocked(struct s_reader *rdr, int16_t len)
|
---|
21 | {
|
---|
22 | struct s_emmlen_range *blocklen;
|
---|
23 | if(!rdr->blockemmbylen)
|
---|
24 | { return 0; }
|
---|
25 | LL_ITER it = ll_iter_create(rdr->blockemmbylen);
|
---|
26 | while((blocklen = ll_iter_next(&it)))
|
---|
27 | {
|
---|
28 | if(blocklen->min <= len && (len <= blocklen->max || blocklen->max == 0))
|
---|
29 | { return 1; }
|
---|
30 | }
|
---|
31 | return 0;
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Function to filter emm by cardsystem.
|
---|
36 | * Every cardsystem can export a function "get_emm_filter"
|
---|
37 | *
|
---|
38 | * the emm is checked against it and returns 1 for a valid emm or 0 if not
|
---|
39 | */
|
---|
40 | static int8_t do_simple_emm_filter(struct s_reader *rdr, const struct s_cardsystem *csystem, EMM_PACKET *ep, int8_t cl_dvbapi)
|
---|
41 | {
|
---|
42 | if(is_network_reader(rdr)) { return 1; } // don't evaluate on network readers, server with local reader will check it
|
---|
43 | if(rdr->typ == R_EMU) { return 1; } // don't evalutate on emu reader
|
---|
44 |
|
---|
45 | //copied and enhanced from module-dvbapi.c
|
---|
46 | //dvbapi_start_emm_filter()
|
---|
47 | int32_t i, k, match;
|
---|
48 | uint8_t flt, mask;
|
---|
49 | struct s_csystem_emm_filter *dmx_filter = NULL;
|
---|
50 | unsigned int j, filter_count = 0;
|
---|
51 |
|
---|
52 | // Call cardsystems emm filter
|
---|
53 | csystem->get_emm_filter(rdr, &dmx_filter, &filter_count);
|
---|
54 |
|
---|
55 | // Only check matching emmtypes:
|
---|
56 | uint8_t org_emmtype;
|
---|
57 | if(ep->type == UNKNOWN)
|
---|
58 | { org_emmtype = EMM_UNKNOWN; }
|
---|
59 | else
|
---|
60 | { org_emmtype = 1 << (ep->type - 1); }
|
---|
61 |
|
---|
62 | // Now check all filter values
|
---|
63 |
|
---|
64 |
|
---|
65 | for(j = 0; j < filter_count; j++)
|
---|
66 | {
|
---|
67 | if(dmx_filter[j].enabled == 0)
|
---|
68 | { continue; }
|
---|
69 |
|
---|
70 | uint8_t emmtype = dmx_filter[j].type;
|
---|
71 | if(emmtype != org_emmtype)
|
---|
72 | { continue; }
|
---|
73 |
|
---|
74 | match = 1;
|
---|
75 | for(i = 0, k = 0; i < 16 && k < ep->emmlen && match; i++, k++)
|
---|
76 | {
|
---|
77 | mask = dmx_filter[j].mask[i];
|
---|
78 | if(k == 1 && cl_dvbapi) // fixup for emms send by dvbapi
|
---|
79 | { k += 2; } // skip emm len bytes
|
---|
80 | if(!mask)
|
---|
81 | { continue; }
|
---|
82 | //cs_log("**** filter %d [%d] = %02X, filter mask[%d] = %02X, flt&mask = %02X , ep->emm[%d] = %02X, ep->emm[%d] & mask = %02X ****", j, i,
|
---|
83 | // dmx_filter[j].filter[i], i, dmx_filter[j].mask[i], flt&mask, k, ep->emm[k], k, ep->emm[k] & mask);
|
---|
84 | flt = (dmx_filter[j].filter[i] & mask);
|
---|
85 | match = (flt == (ep->emm[k] & mask));
|
---|
86 | if(!match)
|
---|
87 | { break; }
|
---|
88 | }
|
---|
89 | if(match)
|
---|
90 | {
|
---|
91 | NULLFREE(dmx_filter);
|
---|
92 | return 1; // valid emm
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | NULLFREE(dmx_filter);
|
---|
97 |
|
---|
98 | return 0; // emm filter does not match, illegal emm, return
|
---|
99 | }
|
---|
100 |
|
---|
101 | static void reader_log_emm(struct s_reader *reader, EMM_PACKET *ep, int32_t count, int32_t rc, struct timeb *tps)
|
---|
102 | {
|
---|
103 | char *rtxt[] =
|
---|
104 | {
|
---|
105 | "error",
|
---|
106 | is_network_reader(reader) ? "sent" : "written",
|
---|
107 | "skipped",
|
---|
108 | "blocked"
|
---|
109 | };
|
---|
110 | char *typedesc[] = { "unknown", "unique", "shared", "global" };
|
---|
111 | struct s_client *cl = reader->client;
|
---|
112 | struct timeb tpe;
|
---|
113 |
|
---|
114 | if(reader->logemm & (1 << rc))
|
---|
115 | {
|
---|
116 | cs_ftime(&tpe);
|
---|
117 | if(!tps)
|
---|
118 | { tps = &tpe; }
|
---|
119 |
|
---|
120 | rdr_log(reader, "%s emmtype=%s, len=%d (hex: 0x%.2X), cnt=%d: %s (%"PRId64" ms)",
|
---|
121 | username(ep->client), typedesc[ep->type], SCT_LEN(ep->emm)-3, SCT_LEN(ep->emm)-3, count, rtxt[rc], comp_timeb(&tpe, tps));
|
---|
122 | }
|
---|
123 |
|
---|
124 | if(rc)
|
---|
125 | {
|
---|
126 | cl->lastemm = time(NULL);
|
---|
127 | led_status_emm_ok();
|
---|
128 | }
|
---|
129 |
|
---|
130 | #if defined(WEBIF) || defined(LCDSUPPORT)
|
---|
131 | // counting results
|
---|
132 | switch(rc)
|
---|
133 | {
|
---|
134 | case 0:
|
---|
135 | reader->emmerror[ep->type]++;
|
---|
136 | reader->webif_emmerror[ep->type]++;
|
---|
137 | break;
|
---|
138 |
|
---|
139 | case 1:
|
---|
140 | reader->emmwritten[ep->type]++;
|
---|
141 | reader->webif_emmwritten[ep->type]++;
|
---|
142 | break;
|
---|
143 |
|
---|
144 | case 2:
|
---|
145 | reader->emmskipped[ep->type]++;
|
---|
146 | reader->webif_emmskipped[ep->type]++;
|
---|
147 | break;
|
---|
148 |
|
---|
149 | case 3:
|
---|
150 | reader->emmblocked[ep->type]++;
|
---|
151 | reader->webif_emmblocked[ep->type]++;
|
---|
152 | break;
|
---|
153 | }
|
---|
154 | #endif
|
---|
155 | }
|
---|
156 |
|
---|
157 | int32_t emm_reader_match(struct s_reader *reader, uint16_t caid, uint32_t provid)
|
---|
158 | {
|
---|
159 | int32_t i;
|
---|
160 |
|
---|
161 | // if physical reader a card needs to be inserted
|
---|
162 | if(!is_network_reader(reader) && reader->card_status != CARD_INSERTED)
|
---|
163 | { return 0; }
|
---|
164 |
|
---|
165 | if(reader->audisabled)
|
---|
166 | { return 0; }
|
---|
167 |
|
---|
168 | if(reader->cwpkcaid_length && reader->nuid_length)
|
---|
169 | {
|
---|
170 | uint8_t check[1];
|
---|
171 | check[0] = caid & 0xFF;
|
---|
172 | if(check[0] == reader->cwpkcaid[1])
|
---|
173 | {
|
---|
174 | return 1;
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | uint16_t emmcaid;
|
---|
179 | if(reader->caid == 0x186D)
|
---|
180 | {
|
---|
181 | emmcaid = reader->caid - 0x03;
|
---|
182 | }
|
---|
183 | else if (reader->caid == 0x1856)
|
---|
184 | {
|
---|
185 | emmcaid = reader->caid + 0x28;
|
---|
186 | }
|
---|
187 | else
|
---|
188 | {
|
---|
189 | emmcaid = reader->caid;
|
---|
190 | }
|
---|
191 |
|
---|
192 | if(emmcaid != caid)
|
---|
193 | {
|
---|
194 | int caid_found = 0;
|
---|
195 | if (!reader->csystem)
|
---|
196 | return 0;
|
---|
197 | for(i = 0; reader->csystem->caids[i]; i++)
|
---|
198 | {
|
---|
199 | uint16_t cs_caid = reader->csystem->caids[i];
|
---|
200 | if (emmcaid && cs_caid == caid)
|
---|
201 | {
|
---|
202 | caid_found = 1;
|
---|
203 | break;
|
---|
204 | }
|
---|
205 |
|
---|
206 | if ((emmcaid == 0) && chk_ctab_ex(caid, &reader->ctab))
|
---|
207 | {
|
---|
208 | caid_found = 1;
|
---|
209 | break;
|
---|
210 | }
|
---|
211 |
|
---|
212 | }
|
---|
213 | if(!caid_found)
|
---|
214 | {
|
---|
215 | rdr_log_dbg(reader, D_EMM, "reader_caid %04X != emmpid caid %04X -> SKIP!", emmcaid, caid);
|
---|
216 | return 0;
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | //if(!hexserialset(reader)) // There are cards without serial, they should get emm of type global and shared!
|
---|
221 | //{
|
---|
222 | // rdr_log_dbg(reader, D_EMM, "no hexserial is set");
|
---|
223 | // return 0;
|
---|
224 | //}
|
---|
225 |
|
---|
226 | if(!provid)
|
---|
227 | {
|
---|
228 | rdr_log_dbg(reader, D_EMM, "reader %04X match since emmpid has no provid -> SEND!", caid);
|
---|
229 | return 1;
|
---|
230 | }
|
---|
231 |
|
---|
232 | uint32_t prid = reader->auprovid;
|
---|
233 |
|
---|
234 | if(caid_is_viaccess(caid) && (prid != 0) && ((prid &0xFFFFF0) != prid)) // viaccess fixup last digit of provid is a dont care!
|
---|
235 | {
|
---|
236 | prid &= 0xFFFFF0;
|
---|
237 | rdr_log_dbg(reader, D_EMM, "reader auprovid = %06X fixup to %06X (ignoring last digit)", reader->auprovid, prid);
|
---|
238 | }
|
---|
239 |
|
---|
240 | #ifdef WITH_EMU
|
---|
241 | if(reader->typ == R_EMU)
|
---|
242 | {
|
---|
243 | FILTER *emu_provids = get_emu_prids_for_caid(reader, caid);
|
---|
244 | if(emu_provids != NULL)
|
---|
245 | {
|
---|
246 | for(i = 0; i < emu_provids->nprids; i++)
|
---|
247 | {
|
---|
248 | if(provid == emu_provids->prids[i])
|
---|
249 | {
|
---|
250 | return 1;
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|
254 | return 0;
|
---|
255 | }
|
---|
256 | #endif
|
---|
257 |
|
---|
258 | if(prid == provid)
|
---|
259 | {
|
---|
260 | rdr_log_dbg(reader, D_EMM, "reader auprovid = %06X matching with emm provid = %06X -> SEND!", prid, provid);
|
---|
261 | return 1;
|
---|
262 | }
|
---|
263 |
|
---|
264 | for(i = 0; i < reader->nprov; i++)
|
---|
265 | {
|
---|
266 | prid = b2i(4, reader->prid[i]);
|
---|
267 |
|
---|
268 | if(caid_is_viaccess(caid) && (prid != 0) && ((prid &0xFFFFF0) != prid)) // viaccess fixup last digit of provid is a dont care!
|
---|
269 | {
|
---|
270 | rdr_log_dbg(reader, D_EMM, "reader provid = %06X fixup to %06X (ignoring last digit)", prid, (prid &0xFFFFF0));
|
---|
271 | prid &= 0xFFFFF0;
|
---|
272 | }
|
---|
273 |
|
---|
274 | if(prid == provid)
|
---|
275 | {
|
---|
276 | rdr_log_dbg(reader, D_EMM, "reader provid %06X matching with emm provid %06X -> SEND!", prid, provid);
|
---|
277 | return 1;
|
---|
278 | }
|
---|
279 |
|
---|
280 | if((reader->typ == R_CAMD35 || reader->typ == R_CS378X) && (prid & 0xFFFF) == (provid & 0xFFFF))
|
---|
281 | {
|
---|
282 | rdr_log_dbg(reader, D_EMM, "CS378: Match after fixing reader provid %06X to ??%04X and emm provid %06X to ??%04X -> SEND!", prid, prid&0xFFFF, provid, provid&0xFFFF);
|
---|
283 | return 1;
|
---|
284 | }
|
---|
285 |
|
---|
286 | rdr_log_dbg(reader, D_EMM, "reader provid %06X no match with emm provid %06X -> SKIP!", prid, provid);
|
---|
287 | }
|
---|
288 | return 0;
|
---|
289 | }
|
---|
290 |
|
---|
291 | static char *get_emmlog_filename(char *dest, size_t destlen, const char *basefilename, const char *type, const char *ext)
|
---|
292 | {
|
---|
293 | char filename[64 + 16];
|
---|
294 | snprintf(filename, sizeof(filename), "%s_%s_emm.%s", basefilename, type, ext);
|
---|
295 | if(!cfg.emmlogdir)
|
---|
296 | {
|
---|
297 | get_config_filename(dest, destlen, filename);
|
---|
298 | }
|
---|
299 | else
|
---|
300 | {
|
---|
301 | const char *slash = "/";
|
---|
302 | if(cfg.emmlogdir[cs_strlen(cfg.emmlogdir) - 1] == '/') { slash = ""; }
|
---|
303 | snprintf(dest, destlen, "%s%s%s", cfg.emmlogdir, slash, filename);
|
---|
304 | }
|
---|
305 | return dest;
|
---|
306 | }
|
---|
307 |
|
---|
308 | static void saveemm(struct s_reader *aureader, EMM_PACKET *ep, const char *proceded)
|
---|
309 | {
|
---|
310 | FILE *fp_log;
|
---|
311 | char tmp[17];
|
---|
312 | char buf[80];
|
---|
313 | char token_log[256];
|
---|
314 | char *tmp2;
|
---|
315 | time_t rawtime;
|
---|
316 | uint32_t emmtype;
|
---|
317 | struct tm timeinfo;
|
---|
318 | if(ep->type == UNKNOWN)
|
---|
319 | { emmtype = EMM_UNKNOWN; }
|
---|
320 | else
|
---|
321 | { emmtype = 1 << (ep->type - 1); }
|
---|
322 | // should this nano be saved?
|
---|
323 | if(((1 << (ep->emm[0] % 0x80)) & aureader->s_nano) || (aureader->saveemm & emmtype))
|
---|
324 | {
|
---|
325 | time(&rawtime);
|
---|
326 | localtime_r(&rawtime, &timeinfo); // to access LOCAL date/time info
|
---|
327 | int32_t emm_length = SCT_LEN(ep->emm);
|
---|
328 | strftime(buf, sizeof(buf), "%Y/%m/%d %H:%M:%S", &timeinfo);
|
---|
329 |
|
---|
330 | switch(ep->type)
|
---|
331 | {
|
---|
332 | case GLOBAL:
|
---|
333 | fp_log = fopen(get_emmlog_filename(token_log, sizeof(token_log), aureader->label, "global", "log"), "a");
|
---|
334 | break;
|
---|
335 |
|
---|
336 | case SHARED:
|
---|
337 | fp_log = fopen(get_emmlog_filename(token_log, sizeof(token_log), aureader->label, "shared", "log"), "a");
|
---|
338 | break;
|
---|
339 |
|
---|
340 | case UNIQUE:
|
---|
341 | fp_log = fopen(get_emmlog_filename(token_log, sizeof(token_log), aureader->label, "unique", "log"), "a");
|
---|
342 | break;
|
---|
343 |
|
---|
344 | case UNKNOWN:
|
---|
345 | default:
|
---|
346 | fp_log = fopen(get_emmlog_filename(token_log, sizeof(token_log), aureader->label, "unknown", "log"), "a");
|
---|
347 | }
|
---|
348 |
|
---|
349 | if(!fp_log)
|
---|
350 | {
|
---|
351 | rdr_log(aureader, "ERROR: Cannot open file '%s' (errno=%d: %s)\n", token_log, errno, strerror(errno));
|
---|
352 | }
|
---|
353 | else
|
---|
354 | {
|
---|
355 | if(cs_malloc(&tmp2, emm_length * 2 + 1))
|
---|
356 | {
|
---|
357 | fprintf(fp_log, "%s %s ", buf, cs_hexdump(0, ep->hexserial, 8, tmp, sizeof(tmp)));
|
---|
358 | fprintf(fp_log, "%s %s\n", cs_hexdump(0, ep->emm, emm_length, tmp2, emm_length * 2 + 1), proceded);
|
---|
359 | NULLFREE(tmp2);
|
---|
360 | rdr_log(aureader, "Successfully added EMM to %s", token_log);
|
---|
361 | }
|
---|
362 | fclose(fp_log);
|
---|
363 | }
|
---|
364 | }
|
---|
365 | }
|
---|
366 |
|
---|
367 | void do_emm(struct s_client *client, EMM_PACKET *ep)
|
---|
368 | {
|
---|
369 | int32_t writeemm = 1; // 0= dont write emm, 1=write emm, default = write
|
---|
370 | char *typtext[] = {"unknown", "unique", "shared", "global"};
|
---|
371 | char tmp[17];
|
---|
372 | int32_t emmnok = 0;
|
---|
373 | bool lastseendone = false;
|
---|
374 |
|
---|
375 | struct s_reader *aureader = NULL;
|
---|
376 | uint16_t sct_len;
|
---|
377 |
|
---|
378 | if(ep->emmlen < 3)
|
---|
379 | {
|
---|
380 | cs_log("EMM size %d invalid, ignored! client %s", ep->emmlen, username(client));
|
---|
381 | return;
|
---|
382 | }
|
---|
383 |
|
---|
384 | if(ep->emmlen > MAX_EMM_SIZE)
|
---|
385 | {
|
---|
386 | cs_log("EMM size %d > Max EMM size %d, ignored! client %s", ep->emmlen, MAX_EMM_SIZE, username(client));
|
---|
387 | return;
|
---|
388 | }
|
---|
389 |
|
---|
390 | sct_len = SCT_LEN(ep->emm);
|
---|
391 | if(sct_len > ep->emmlen)
|
---|
392 | {
|
---|
393 | cs_log("Real EMM size %d > EMM size %d, ignored! client %s", sct_len, ep->emmlen, username(client));
|
---|
394 | return;
|
---|
395 | }
|
---|
396 | ep->emmlen = sct_len;
|
---|
397 |
|
---|
398 | cs_log_dump_dbg(D_EMM, ep->emm, ep->emmlen, "emm:");
|
---|
399 |
|
---|
400 | int8_t assemble = 0;
|
---|
401 | bool cl_dvbapi = is_dvbapi_usr(client->account->usr);
|
---|
402 | if(client->account->emm_reassembly > 1 || (client->account->emm_reassembly && cl_dvbapi))
|
---|
403 | { assemble = 1; }
|
---|
404 |
|
---|
405 | LL_ITER itr = ll_iter_create(client->aureader_list);
|
---|
406 | while((aureader = ll_iter_next(&itr)))
|
---|
407 | {
|
---|
408 | if(!aureader->enable)
|
---|
409 | { continue; }
|
---|
410 |
|
---|
411 | uint16_t caid = b2i(2, ep->caid);
|
---|
412 | uint32_t provid = b2i(4, ep->provid);
|
---|
413 |
|
---|
414 | if(caid_is_viaccess(caid)) // viaccess fixup last digit is a dont care!
|
---|
415 | {
|
---|
416 | provid &= 0xFFFFF0;
|
---|
417 | }
|
---|
418 |
|
---|
419 | if(aureader->audisabled)
|
---|
420 | {
|
---|
421 | rdr_log_dbg(aureader, D_EMM, "AU is disabled");
|
---|
422 | /* we have to write the log for blocked EMM here because
|
---|
423 | this EMM never reach the reader module where the rest
|
---|
424 | of EMM log is done. */
|
---|
425 | if(aureader->logemm & 0x10)
|
---|
426 | {
|
---|
427 | rdr_log(aureader, "%s emmtype=%s, len=%d (hex: 0x%02X), idx=0, cnt=1: audisabled (0 ms)",
|
---|
428 | client->account->usr,
|
---|
429 | typtext[ep->type],
|
---|
430 | SCT_LEN(ep->emm) - 3,
|
---|
431 | SCT_LEN(ep->emm) - 3);
|
---|
432 | }
|
---|
433 | continue;
|
---|
434 | }
|
---|
435 |
|
---|
436 | if(!(aureader->grp & client->grp))
|
---|
437 | {
|
---|
438 | rdr_log_dbg(aureader, D_EMM, "skip emm, group mismatch");
|
---|
439 | continue;
|
---|
440 | }
|
---|
441 |
|
---|
442 | // TODO: provider possibly not set yet, this is done in get_emm_type()
|
---|
443 | if(!emm_reader_match(aureader, caid, provid))
|
---|
444 | { continue; }
|
---|
445 |
|
---|
446 | const struct s_cardsystem *csystem = NULL;
|
---|
447 |
|
---|
448 | if(is_network_reader(aureader)) // network reader (R_CAMD35 R_NEWCAMD R_CS378X R_CCCAM)
|
---|
449 | {
|
---|
450 | if(!aureader->ph.c_send_emm) // no emm support
|
---|
451 | { continue; }
|
---|
452 |
|
---|
453 | csystem = get_cardsystem_by_caid(caid);
|
---|
454 | if(!csystem)
|
---|
455 | {
|
---|
456 | rdr_log_dbg(aureader, D_EMM, "unable to find cardsystem for caid %04X", caid);
|
---|
457 | continue;
|
---|
458 | }
|
---|
459 | }
|
---|
460 | else // local reader
|
---|
461 | {
|
---|
462 | if(aureader->csystem_active)
|
---|
463 | { csystem = aureader->csystem; }
|
---|
464 | }
|
---|
465 |
|
---|
466 | if(csystem && csystem->get_emm_type)
|
---|
467 | {
|
---|
468 | if(!csystem->get_emm_type(ep, aureader))
|
---|
469 | {
|
---|
470 | rdr_log_dbg(aureader, D_EMM, "emm skipped, get_emm_type() returns error");
|
---|
471 | emmnok++;
|
---|
472 | continue;
|
---|
473 | }
|
---|
474 | }
|
---|
475 |
|
---|
476 | if(!ep->skip_filter_check && csystem && csystem->get_emm_filter)
|
---|
477 | {
|
---|
478 | if(!do_simple_emm_filter(aureader, csystem, ep, 1)) // do check with dvbapi fixup enabled
|
---|
479 | {
|
---|
480 | if(!do_simple_emm_filter(aureader, csystem, ep, 0)) // do check with dvbapi fixup disabled
|
---|
481 | {
|
---|
482 | rdr_log_dbg(aureader, D_EMM, "emm skipped, do_simple_emm_filter() returns invalid");
|
---|
483 | emmnok++;
|
---|
484 | continue;
|
---|
485 | }
|
---|
486 | }
|
---|
487 | }
|
---|
488 |
|
---|
489 | if(csystem && csystem->do_emm_reassembly)
|
---|
490 | {
|
---|
491 | if(assemble)
|
---|
492 | {
|
---|
493 | if(!csystem->do_emm_reassembly(aureader, client, ep))
|
---|
494 | { continue; } // skip this reader
|
---|
495 | }
|
---|
496 | else
|
---|
497 | {
|
---|
498 | rdr_log_dbg(aureader, D_EMM, "processing raw emm");
|
---|
499 | }
|
---|
500 | }
|
---|
501 |
|
---|
502 | rdr_log_dbg_sensitive(aureader, D_EMM, "emmtype %s. Reader serial {%s}.", typtext[ep->type],
|
---|
503 | cs_hexdump(0, aureader->hexserial, 8, tmp, sizeof(tmp)));
|
---|
504 | rdr_log_dbg_sensitive(aureader, D_EMM, "emm UA/SA: {%s}.",
|
---|
505 | cs_hexdump(0, ep->hexserial, 8, tmp, sizeof(tmp)));
|
---|
506 |
|
---|
507 | client->last = time(NULL);
|
---|
508 |
|
---|
509 | int32_t is_blocked = 0;
|
---|
510 |
|
---|
511 | if (aureader->fix_07 == 1 && ep->type == UNIQUE)
|
---|
512 | {
|
---|
513 | if((caid == 0x098D || caid == 0x098C || caid == 0x09C4) && ep->emm[1] == 0x70 && (ep->emm[8] * 0x100 + ep->emm[9] != 0x200))
|
---|
514 | {
|
---|
515 | rdr_log(aureader,"emmtype 0x%04X marked as unknown for caid 0x%04X", (ep->emm[8] * 0x100 + ep->emm[9]),caid);
|
---|
516 | ep->type = UNKNOWN;
|
---|
517 | }
|
---|
518 |
|
---|
519 | if((caid == 0x098D || caid == 0x098C || caid == 0x09C4) && ep->emm[1] == 0 && (ep->emm[4] * 0x100 + ep->emm[5] != 0x200))
|
---|
520 | {
|
---|
521 | rdr_log(aureader,"emmtype 0x%04X marked as unknown for caid 0x%04X", (ep->emm[4] * 0x100 + ep->emm[5]),caid);
|
---|
522 | ep->type = UNKNOWN;
|
---|
523 | }
|
---|
524 |
|
---|
525 | if(caid == 0x09AF && ep->emm[1] == 0x70 && ep->emm[11] != 2)
|
---|
526 | {
|
---|
527 | rdr_log(aureader,"emmtype 0x%02X marked as unknown for caid 0x%04X", ep->emm[11],caid);
|
---|
528 | ep->type = UNKNOWN;
|
---|
529 | }
|
---|
530 |
|
---|
531 | if(caid == 0x09AF && ep->emm[1] == 0 && ep->emm[7] != 2)
|
---|
532 | {
|
---|
533 | rdr_log(aureader,"emmtype 0x%02X marked as unknown for caid 0x%04X", ep->emm[7],caid);
|
---|
534 | ep->type = UNKNOWN;
|
---|
535 | }
|
---|
536 | }
|
---|
537 |
|
---|
538 | #ifdef READER_CRYPTOWORKS
|
---|
539 | if ((ep->type == GLOBAL) && ((caid == 0x0D96) || (caid == 0x0D98)) && ((aureader->blockemm & EMM_GLOBAL) != EMM_GLOBAL) && ((aureader->blockemm & EMM_SHARED) != EMM_SHARED) && (aureader->needsglobalfirst == 1))
|
---|
540 | {
|
---|
541 | // save global EMM
|
---|
542 | cs_log_dbg(D_EMM,"save global EMM for caid 0x%04X",caid);
|
---|
543 | ep->client = client;
|
---|
544 | memcpy(aureader->last_g_emm, ep, sizeof(EMM_PACKET));
|
---|
545 | aureader->last_g_emm_valid = true;
|
---|
546 |
|
---|
547 | #ifdef WEBIF
|
---|
548 | aureader->emmblocked[ep->type]++;
|
---|
549 | aureader->webif_emmblocked[ep->type]++;
|
---|
550 | is_blocked = aureader->emmblocked[ep->type];
|
---|
551 | #endif
|
---|
552 |
|
---|
553 | if(aureader->logemm & 0x08)
|
---|
554 | {
|
---|
555 | rdr_log(aureader, "%s emmtype=%s, len=%d (hex: 0x%02X), idx=0, cnt=%d: blocked & saved (0 ms)",
|
---|
556 | client->account->usr,
|
---|
557 | typtext[ep->type],
|
---|
558 | SCT_LEN(ep->emm)-3,
|
---|
559 | SCT_LEN(ep->emm)-3,
|
---|
560 | is_blocked);
|
---|
561 | }
|
---|
562 | saveemm(aureader, ep, "blocked & saved");
|
---|
563 | continue;
|
---|
564 | }
|
---|
565 | #endif
|
---|
566 |
|
---|
567 | switch(ep->type)
|
---|
568 | {
|
---|
569 | case UNKNOWN:
|
---|
570 | is_blocked = (aureader->blockemm & EMM_UNKNOWN) == EMM_UNKNOWN;
|
---|
571 | break;
|
---|
572 |
|
---|
573 | case UNIQUE :
|
---|
574 | is_blocked = (aureader->blockemm & EMM_UNIQUE) == EMM_UNIQUE;
|
---|
575 | break;
|
---|
576 |
|
---|
577 | case SHARED :
|
---|
578 | is_blocked = (aureader->blockemm & EMM_SHARED) == EMM_SHARED;
|
---|
579 | break;
|
---|
580 |
|
---|
581 | case GLOBAL :
|
---|
582 | is_blocked = (aureader->blockemm & EMM_GLOBAL) == EMM_GLOBAL;
|
---|
583 | break;
|
---|
584 | }
|
---|
585 |
|
---|
586 | // if not already blocked we check for block by len
|
---|
587 | if(!is_blocked) { is_blocked = cs_emmlen_is_blocked(aureader, SCT_LEN(ep->emm)-3) ; }
|
---|
588 |
|
---|
589 | if(is_blocked != 0)
|
---|
590 | {
|
---|
591 | #ifdef WEBIF
|
---|
592 | aureader->emmblocked[ep->type]++;
|
---|
593 | aureader->webif_emmblocked[ep->type]++;
|
---|
594 | is_blocked = aureader->emmblocked[ep->type];
|
---|
595 | #endif
|
---|
596 | /* we have to write the log for blocked EMM here because
|
---|
597 | this EMM never reach the reader module where the rest
|
---|
598 | of EMM log is done. */
|
---|
599 | if(aureader->logemm & 0x08)
|
---|
600 | {
|
---|
601 | rdr_log(aureader, "%s emmtype=%s, len=%d (hex: 0x%02X), idx=0, cnt=%d: blocked (0 ms)",
|
---|
602 | client->account->usr,
|
---|
603 | typtext[ep->type],
|
---|
604 | SCT_LEN(ep->emm)-3,
|
---|
605 | SCT_LEN(ep->emm)-3,
|
---|
606 | is_blocked);
|
---|
607 | }
|
---|
608 | saveemm(aureader, ep, "blocked");
|
---|
609 | continue;
|
---|
610 | }
|
---|
611 |
|
---|
612 | client->lastemm = time((time_t *)0);
|
---|
613 |
|
---|
614 | client->emmok++;
|
---|
615 | if(client->account)
|
---|
616 | { client->account->emmok++; }
|
---|
617 | first_client->emmok++;
|
---|
618 |
|
---|
619 | ep->client = client;
|
---|
620 |
|
---|
621 |
|
---|
622 | if(aureader->cachemm && !(caid_is_irdeto(caid) || caid_is_videoguard(caid))) // Check emmcache early:
|
---|
623 | {
|
---|
624 | uint8_t md5tmp[MD5_DIGEST_LENGTH];
|
---|
625 |
|
---|
626 | MD5(ep->emm, SCT_LEN(ep->emm), md5tmp);
|
---|
627 |
|
---|
628 | struct s_emmcache *emmcache = find_emm_cache(md5tmp); // check emm cache
|
---|
629 | if(emmcache && !lastseendone)
|
---|
630 | {
|
---|
631 | cs_ftime(&emmcache->lastseen);
|
---|
632 | lastseendone = true; // in case several aureaders, only do lastseen once!
|
---|
633 | }
|
---|
634 |
|
---|
635 | struct s_emmstat *emmstat = get_emm_stat(aureader, md5tmp, ep->type);
|
---|
636 | if(emmstat)
|
---|
637 | {
|
---|
638 | rdr_log_dbg(aureader, D_EMM, "emm count %d rewrite %d", emmstat->count, aureader->rewritemm);
|
---|
639 |
|
---|
640 | if(emmstat->count >= aureader->rewritemm)
|
---|
641 | {
|
---|
642 | reader_log_emm(aureader, ep, emmstat->count, 2, NULL);
|
---|
643 | writeemm = 0; // don't write emm!
|
---|
644 | saveemm(aureader, ep, "emmcache");
|
---|
645 | continue; // found emm match needs no further handling, proceed with next reader!
|
---|
646 | }
|
---|
647 | }
|
---|
648 | }
|
---|
649 |
|
---|
650 | if(writeemm) // only write on no cache hit or cache hit that needs further rewrite
|
---|
651 | {
|
---|
652 | EMM_PACKET *emm_pack;
|
---|
653 | if(cs_malloc(&emm_pack, sizeof(EMM_PACKET)))
|
---|
654 | {
|
---|
655 | #ifdef READER_CRYPTOWORKS
|
---|
656 | if ((ep->type == SHARED) && ((caid == 0x0D96) || (caid == 0x0D98)) && (aureader->last_g_emm_valid == true) && (aureader->needsglobalfirst == 1))
|
---|
657 | {
|
---|
658 | EMM_PACKET *emm_pack_global;
|
---|
659 | if(cs_malloc(&emm_pack_global, sizeof(EMM_PACKET)))
|
---|
660 | {
|
---|
661 | rdr_log_dbg(aureader, D_EMM, "Last stored global EMM for caid 0x%04X is being sent to Reader first", caid);
|
---|
662 | memcpy(emm_pack_global, aureader->last_g_emm, sizeof(EMM_PACKET));
|
---|
663 | add_job(aureader->client, ACTION_READER_EMM, emm_pack_global, sizeof(EMM_PACKET));
|
---|
664 | saveemm(aureader, aureader->last_g_emm, "written stored global");
|
---|
665 | cs_log_dump_dbg(D_EMM,emm_pack_global->emm, emm_pack_global->emmlen, "Last stored global EMM to be written before shared EMM:");
|
---|
666 | }
|
---|
667 | }
|
---|
668 | #endif
|
---|
669 | rdr_log_dbg(aureader, D_EMM, "emm is being sent to reader");
|
---|
670 | memcpy(emm_pack, ep, sizeof(EMM_PACKET));
|
---|
671 | add_job(aureader->client, ACTION_READER_EMM, emm_pack, sizeof(EMM_PACKET));
|
---|
672 | saveemm(aureader, ep, "written");
|
---|
673 | }
|
---|
674 | }
|
---|
675 |
|
---|
676 | } // done with this reader, process next reader!
|
---|
677 |
|
---|
678 | if(emmnok > 0 && emmnok == ll_count(client->aureader_list))
|
---|
679 | {
|
---|
680 | client->emmnok++;
|
---|
681 | if(client->account)
|
---|
682 | { client->account->emmnok++; }
|
---|
683 | first_client->emmnok++;
|
---|
684 | }
|
---|
685 | }
|
---|
686 |
|
---|
687 |
|
---|
688 | int32_t reader_do_emm(struct s_reader *reader, EMM_PACKET *ep)
|
---|
689 | {
|
---|
690 | int32_t rc, ecs = 0,count = 0;
|
---|
691 | uint8_t md5tmp[MD5_DIGEST_LENGTH];
|
---|
692 | struct timeb tps;
|
---|
693 |
|
---|
694 | cs_ftime(&tps);
|
---|
695 | uint16_t caid = b2i(2, ep->caid);
|
---|
696 | if(reader->cachemm && !(caid_is_irdeto(caid) || caid_is_videoguard(caid)))
|
---|
697 | {
|
---|
698 | MD5(ep->emm, SCT_LEN(ep->emm), md5tmp);
|
---|
699 | int64_t gone = comp_timeb(&tps, &last_emm_clean);
|
---|
700 | if(gone > (int64_t)1000 * 60 * 60 * 24 * 30 || gone < 0) // dont run every time, only on first emm oscam is started and then every 30 days
|
---|
701 | {
|
---|
702 | last_emm_clean = tps;
|
---|
703 | count = clean_stale_emm_cache_and_stat(md5tmp, (int64_t)1000 * 60 * 60 *24 * 30); // clean global all emms from all readers after 30 days emm is last seen!
|
---|
704 | cs_log_dbg(D_EMM, "Cleaned %d emm stale stats and cache entries", count);
|
---|
705 | }
|
---|
706 |
|
---|
707 | struct s_emmcache *emmcache = find_emm_cache(md5tmp); // check emm cache
|
---|
708 | if(!emmcache)
|
---|
709 | {
|
---|
710 | emm_edit_cache(md5tmp, ep, true);
|
---|
711 | }
|
---|
712 |
|
---|
713 | struct s_emmstat *emmstat = get_emm_stat(reader, md5tmp, ep->type);
|
---|
714 | if(emmstat)
|
---|
715 | {
|
---|
716 | if(reader->cachemm && emmstat->count >= reader->rewritemm)
|
---|
717 | {
|
---|
718 | ecs = 2; // skip emm
|
---|
719 | }
|
---|
720 | else
|
---|
721 | {
|
---|
722 | ecs = 1; // rewrite emm
|
---|
723 | if(!emmstat->count)
|
---|
724 | {
|
---|
725 | cs_ftime(&emmstat->firstwritten);
|
---|
726 | emmstat->lastwritten = emmstat->firstwritten;
|
---|
727 | }
|
---|
728 | else
|
---|
729 | {
|
---|
730 | cs_ftime(&emmstat->lastwritten);
|
---|
731 | }
|
---|
732 | count = ++emmstat->count;
|
---|
733 | }
|
---|
734 | }
|
---|
735 | else
|
---|
736 | {
|
---|
737 | cs_log("abort: oscam seems out of resources!");
|
---|
738 | return 0;
|
---|
739 | }
|
---|
740 | }
|
---|
741 |
|
---|
742 | // Ecs=0 not found in cache
|
---|
743 | // Ecs=1 found in cache, rewrite emm
|
---|
744 | // Ecs=2 skip
|
---|
745 | if((rc = ecs) < 2)
|
---|
746 | {
|
---|
747 | if(is_network_reader(reader))
|
---|
748 | {
|
---|
749 | rdr_log_dbg(reader, D_READER, "network emm reader");
|
---|
750 | if(reader->ph.c_send_emm)
|
---|
751 | {
|
---|
752 | rc = reader->ph.c_send_emm(ep);
|
---|
753 | }
|
---|
754 | else
|
---|
755 | {
|
---|
756 | rdr_log_dbg(reader, D_READER, "send_emm() support missing");
|
---|
757 | rc = 0;
|
---|
758 | }
|
---|
759 | }
|
---|
760 | else
|
---|
761 | {
|
---|
762 | rdr_log_dbg(reader, D_READER, "local emm reader");
|
---|
763 | rc = cardreader_do_emm(reader, ep);
|
---|
764 | }
|
---|
765 | }
|
---|
766 |
|
---|
767 | reader_log_emm(reader, ep, count, rc, &tps);
|
---|
768 |
|
---|
769 | return rc;
|
---|
770 | }
|
---|
771 |
|
---|
772 | void do_emm_from_file(struct s_reader *reader)
|
---|
773 | {
|
---|
774 | if(!reader->emmfile)
|
---|
775 | { return; }
|
---|
776 |
|
---|
777 | char token[256];
|
---|
778 | FILE *fp;
|
---|
779 |
|
---|
780 | if(reader->emmfile[0] == '/')
|
---|
781 | { snprintf(token, sizeof(token), "%s", reader->emmfile); } // pathname included
|
---|
782 | else
|
---|
783 | { get_config_filename(token, sizeof(token), reader->emmfile); } // only file specified, look in confdir for this file
|
---|
784 |
|
---|
785 | if(!(fp = fopen(token, "rb")))
|
---|
786 | {
|
---|
787 | rdr_log(reader, "ERROR: Cannot open EMM file '%s' (errno=%d %s)\n", token, errno, strerror(errno));
|
---|
788 | return;
|
---|
789 | }
|
---|
790 |
|
---|
791 | EMM_PACKET *eptmp;
|
---|
792 | if(!cs_malloc(&eptmp, sizeof(EMM_PACKET)))
|
---|
793 | {
|
---|
794 | fclose(fp);
|
---|
795 | return;
|
---|
796 | }
|
---|
797 |
|
---|
798 | size_t ret = fread(eptmp, sizeof(EMM_PACKET), 1, fp);
|
---|
799 | if(ret < 1 && ferror(fp))
|
---|
800 | {
|
---|
801 | rdr_log(reader, "ERROR: Can't read EMM from file '%s' (errno=%d %s)", token, errno, strerror(errno));
|
---|
802 | NULLFREE(eptmp);
|
---|
803 | fclose(fp);
|
---|
804 | return;
|
---|
805 | }
|
---|
806 | fclose(fp);
|
---|
807 |
|
---|
808 | if (eptmp) {
|
---|
809 | eptmp->caid[0] = (reader->caid >> 8) & 0xFF;
|
---|
810 | eptmp->caid[1] = reader->caid & 0xFF;
|
---|
811 | if(reader->nprov > 0)
|
---|
812 | { memcpy(eptmp->provid, reader->prid[0], sizeof(eptmp->provid)); }
|
---|
813 | eptmp->emmlen = SCT_LEN(eptmp->emm);
|
---|
814 | }
|
---|
815 | const struct s_cardsystem *csystem = get_cardsystem_by_caid(reader->caid);
|
---|
816 | if(csystem && csystem->get_emm_type && !csystem->get_emm_type(eptmp, reader))
|
---|
817 | {
|
---|
818 | rdr_log_dbg(reader, D_EMM, "emm skipped, get_emm_type() returns error");
|
---|
819 | NULLFREE(eptmp);
|
---|
820 | return;
|
---|
821 | }
|
---|
822 |
|
---|
823 | // save old b_nano value
|
---|
824 | // clear lsb and lsb+1, so no blocking, and no saving for this nano
|
---|
825 | uint16_t save_s_nano = reader->s_nano;
|
---|
826 | uint16_t save_b_nano = reader->b_nano;
|
---|
827 | uint32_t save_saveemm = reader->saveemm;
|
---|
828 |
|
---|
829 | reader->s_nano = reader->b_nano = 0;
|
---|
830 | reader->saveemm = 0;
|
---|
831 |
|
---|
832 | int32_t rc = 0;
|
---|
833 | rc = cardreader_do_emm(reader, eptmp);
|
---|
834 | if(rc == OK)
|
---|
835 | { rdr_log(reader, "EMM from file %s was successfully written.", token); }
|
---|
836 | else
|
---|
837 | { rdr_log(reader, "ERROR: EMM read from file %s NOT processed correctly! (rc=%d)", token, rc); }
|
---|
838 |
|
---|
839 | // restore old block/save settings
|
---|
840 | reader->s_nano = save_s_nano;
|
---|
841 | reader->b_nano = save_b_nano;
|
---|
842 | reader->saveemm = save_saveemm;
|
---|
843 |
|
---|
844 | NULLFREE(eptmp);
|
---|
845 | }
|
---|
846 |
|
---|
847 | void emm_sort_nanos(uint8_t *dest, const uint8_t *src, int32_t len)
|
---|
848 | {
|
---|
849 | int32_t w = 0, c = -1, j = 0;
|
---|
850 | while(1)
|
---|
851 | {
|
---|
852 | int32_t n = 256;
|
---|
853 | for(j = 0; j < len;)
|
---|
854 | {
|
---|
855 | int32_t l = src[j + 1] + 2;
|
---|
856 | if(src[j] == c)
|
---|
857 | {
|
---|
858 | if(w + l > len)
|
---|
859 | {
|
---|
860 | cs_log_dbg(D_EMM, "sortnanos: sanity check failed. Exceeding memory area. Probably corrupted nanos!");
|
---|
861 | memset(dest, 0, len); // zero out everything
|
---|
862 | return;
|
---|
863 | }
|
---|
864 | memcpy(&dest[w], &src[j], l);
|
---|
865 | w += l;
|
---|
866 | }
|
---|
867 | else if(src[j] > c && src[j] < n)
|
---|
868 | {
|
---|
869 | n = src[j];
|
---|
870 | }
|
---|
871 | j += l;
|
---|
872 | }
|
---|
873 | if(n >= 256)
|
---|
874 | { break; }
|
---|
875 | c = n;
|
---|
876 | }
|
---|
877 | }
|
---|