1 | //FIXME Not checked on threadsafety yet; after checking please remove this line
|
---|
2 | #include <sys/time.h>
|
---|
3 | #include "globals.h"
|
---|
4 | #include "module-cccam.h"
|
---|
5 |
|
---|
6 | void aes_set_key(char *key)
|
---|
7 | {
|
---|
8 | AES_set_decrypt_key((const unsigned char *)key, 128, &cur_client()->aeskey_decrypt);
|
---|
9 | AES_set_encrypt_key((const unsigned char *)key, 128, &cur_client()->aeskey);
|
---|
10 | }
|
---|
11 |
|
---|
12 | void aes_decrypt(uchar *buf, int n)
|
---|
13 | {
|
---|
14 | int i;
|
---|
15 | for(i=0; i<n; i+=16)
|
---|
16 | AES_decrypt(buf+i, buf+i, &cur_client()->aeskey_decrypt);
|
---|
17 | }
|
---|
18 |
|
---|
19 | void aes_encrypt_idx(struct s_client *cl, uchar *buf, int n)
|
---|
20 | {
|
---|
21 | int i;
|
---|
22 | for(i=0; i<n; i+=16)
|
---|
23 | AES_encrypt(buf+i, buf+i, &cl->aeskey);
|
---|
24 | }
|
---|
25 |
|
---|
26 | void add_aes_entry(struct s_reader *rdr, ushort caid, uint32 ident, int keyid, uchar *aesKey)
|
---|
27 | {
|
---|
28 | AES_ENTRY *new_entry;
|
---|
29 | AES_ENTRY *next,*current;
|
---|
30 |
|
---|
31 | // create de AES key entry for the linked list
|
---|
32 | new_entry=malloc(sizeof(AES_ENTRY));
|
---|
33 | if(!new_entry) {
|
---|
34 | cs_log("Error alocation memory for AES key entry");
|
---|
35 | return;
|
---|
36 | }
|
---|
37 |
|
---|
38 | memcpy(new_entry->plainkey, aesKey, 16);
|
---|
39 | new_entry->caid=caid;
|
---|
40 | new_entry->ident=ident;
|
---|
41 | new_entry->keyid=keyid;
|
---|
42 | if(memcmp(aesKey,"\xFF\xFF",2)) {
|
---|
43 | AES_set_decrypt_key((const unsigned char *)aesKey, 128, &(new_entry->key));
|
---|
44 | // cs_log("adding key : %s",cs_hexdump(1,aesKey,16));
|
---|
45 | }
|
---|
46 | else {
|
---|
47 | memset(&new_entry->key,0,sizeof(AES_KEY));
|
---|
48 | // cs_log("adding fake key");
|
---|
49 | }
|
---|
50 | new_entry->next=NULL;
|
---|
51 |
|
---|
52 | //if list is empty, new_entry is the new head
|
---|
53 | if(!rdr->aes_list) {
|
---|
54 | rdr->aes_list=new_entry;
|
---|
55 | return;
|
---|
56 | }
|
---|
57 |
|
---|
58 | //happend it to the list
|
---|
59 | current=rdr->aes_list;
|
---|
60 | next=current->next;
|
---|
61 | while(next) {
|
---|
62 | current=next;
|
---|
63 | next=current->next;
|
---|
64 | }
|
---|
65 |
|
---|
66 | current->next=new_entry;
|
---|
67 |
|
---|
68 | }
|
---|
69 |
|
---|
70 | void parse_aes_entry(struct s_reader *rdr,char *value) {
|
---|
71 | ushort caid;
|
---|
72 | ushort dummy;
|
---|
73 | uint32 ident;
|
---|
74 | int len;
|
---|
75 | char *tmp;
|
---|
76 | int nb_keys,key_id;
|
---|
77 | uchar aes_key[16];
|
---|
78 | char *save=NULL;
|
---|
79 |
|
---|
80 | tmp=strtok_r(value,"@",&save);
|
---|
81 | caid=a2i(tmp,2);
|
---|
82 | tmp=strtok_r(NULL,":",&save);
|
---|
83 | ident=a2i(tmp,3);
|
---|
84 |
|
---|
85 | // now we need to split the key ane add the entry to the reader.
|
---|
86 | nb_keys=0;
|
---|
87 | key_id=0;
|
---|
88 | while((tmp=strtok_r(NULL,",",&save))) {
|
---|
89 | dummy=0;
|
---|
90 | len=strlen(tmp);
|
---|
91 | if(len!=32) {
|
---|
92 | dummy=a2i(tmp,1);
|
---|
93 | // FF means the card will do the AES decrypt
|
---|
94 | // 00 means we don't have the aes.
|
---|
95 | if((dummy!=0xFF && dummy!=0x00) || len>2) {
|
---|
96 | key_id++;
|
---|
97 | cs_log("AES key length error .. not adding");
|
---|
98 | continue;
|
---|
99 | }
|
---|
100 | if(dummy==0x00) {
|
---|
101 | key_id++;
|
---|
102 | continue;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | nb_keys++;
|
---|
106 | if(dummy)
|
---|
107 | memset(aes_key,0xFF,16);
|
---|
108 | else
|
---|
109 | key_atob_l(tmp,aes_key,32);
|
---|
110 | // now add the key to the reader... TBD
|
---|
111 | add_aes_entry(rdr,caid,ident,key_id,aes_key);
|
---|
112 | key_id++;
|
---|
113 | }
|
---|
114 |
|
---|
115 | cs_log("%d AES key(s) added on reader %s for %04x:%06x", nb_keys, rdr->label, caid, ident);
|
---|
116 | }
|
---|
117 |
|
---|
118 | void parse_aes_keys(struct s_reader *rdr,char *value)
|
---|
119 | {
|
---|
120 | // value format is caid1@ident1:key0,key1;caid2@indent2:key0,key1
|
---|
121 | char *entry;
|
---|
122 | char *save=NULL;
|
---|
123 |
|
---|
124 | rdr->aes_list=NULL;
|
---|
125 | for (entry=strtok_r(value, ";",&save); entry; entry=strtok_r(NULL, ";",&save)) {
|
---|
126 | parse_aes_entry(rdr,entry);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /*
|
---|
130 | AES_ENTRY *current;
|
---|
131 | current=rdr->aes_list;
|
---|
132 | while(current) {
|
---|
133 | cs_log("**************************");
|
---|
134 | cs_log("current = %p",current);
|
---|
135 | cs_log("CAID = %04x",current->caid);
|
---|
136 | cs_log("IDENT = %06x",current->ident);
|
---|
137 | cs_log("keyID = %d",current->keyid);
|
---|
138 | cs_log("next = %p",current->next);
|
---|
139 | cs_log("**************************");
|
---|
140 | current=current->next;
|
---|
141 | }
|
---|
142 | */
|
---|
143 | }
|
---|
144 |
|
---|
145 | int aes_decrypt_from_list(AES_ENTRY *list, ushort caid, uint32 provid,int keyid, uchar *buf, int n)
|
---|
146 | {
|
---|
147 | AES_ENTRY *current;
|
---|
148 | AES_KEY dummy;
|
---|
149 | int i;
|
---|
150 | int ok=1;
|
---|
151 | int error=0;
|
---|
152 |
|
---|
153 | current=list;
|
---|
154 | while(current) {
|
---|
155 | if(current->caid==caid && current->ident==provid && current->keyid==keyid)
|
---|
156 | break;
|
---|
157 | current=current->next;
|
---|
158 | }
|
---|
159 |
|
---|
160 | if(!current) {
|
---|
161 | cs_log("AES Decrypt : key id %d not found for CAID %04X , provider %06x",keyid,caid,provid);
|
---|
162 | return error; // we don't have the key to decode this buffer.
|
---|
163 | }
|
---|
164 | else {
|
---|
165 | // hack for card that do the AES decrypt themsleves
|
---|
166 | memset(&dummy,0,sizeof(AES_KEY));
|
---|
167 | if(!memcmp(¤t->key,&dummy,sizeof(AES_KEY))) {
|
---|
168 | return ok;
|
---|
169 | }
|
---|
170 | // decode the key
|
---|
171 | for(i=0; i<n; i+=16)
|
---|
172 | AES_decrypt(buf+i, buf+i, &(current->key));
|
---|
173 | }
|
---|
174 | return ok; // all ok, key decoded.
|
---|
175 | }
|
---|
176 |
|
---|
177 | int aes_present(AES_ENTRY *list, ushort caid, uint32 provid,int keyid)
|
---|
178 | {
|
---|
179 | AES_ENTRY *current;
|
---|
180 | int ok=1;
|
---|
181 | int error=0;
|
---|
182 |
|
---|
183 | current=list;
|
---|
184 | while(current) {
|
---|
185 | if(current->caid==caid && current->ident==provid && current->keyid==keyid)
|
---|
186 | break;
|
---|
187 | current=current->next;
|
---|
188 | }
|
---|
189 |
|
---|
190 | if(!current) {
|
---|
191 | cs_log("AES Decrypt : key id %d not found for CAID %04X , provider %06x",keyid,caid,provid);
|
---|
192 | return error; // we don't have the key to decode this buffer.
|
---|
193 | }
|
---|
194 |
|
---|
195 | return ok;
|
---|
196 | }
|
---|
197 |
|
---|
198 | void aes_clear_entries(struct s_reader *rdr)
|
---|
199 | {
|
---|
200 |
|
---|
201 | AES_ENTRY *current;
|
---|
202 | AES_ENTRY *next;
|
---|
203 |
|
---|
204 | current=NULL;
|
---|
205 | next=rdr->aes_list;
|
---|
206 | while(next) {
|
---|
207 | current=next;
|
---|
208 | next=current->next;
|
---|
209 | free(current);
|
---|
210 | }
|
---|
211 | rdr->aes_list=NULL;
|
---|
212 | }
|
---|
213 |
|
---|
214 | char *remote_txt(void)
|
---|
215 | {
|
---|
216 | if (cur_client()->typ == 'c')
|
---|
217 | return("client");
|
---|
218 | else
|
---|
219 | return("remote server");
|
---|
220 | }
|
---|
221 |
|
---|
222 | char *trim(txt)
|
---|
223 | char *txt;
|
---|
224 | {
|
---|
225 | register int l;
|
---|
226 | register char *p1, *p2;
|
---|
227 |
|
---|
228 | if (*txt==' ')
|
---|
229 | {
|
---|
230 | for (p1=p2=txt;
|
---|
231 | (*p1==' ') || (*p1=='\t') || (*p1=='\n') || (*p1=='\r');
|
---|
232 | p1++);
|
---|
233 | while (*p1)
|
---|
234 | *p2++=*p1++;
|
---|
235 | *p2='\0';
|
---|
236 | }
|
---|
237 | if ((l=strlen(txt))>0)
|
---|
238 | for (p1=txt+l-1;
|
---|
239 | (*p1==' ') || (*p1=='\t') || (*p1=='\n') || (*p1=='\r');
|
---|
240 | *p1--='\0');
|
---|
241 |
|
---|
242 | return(txt);
|
---|
243 | }
|
---|
244 |
|
---|
245 | char *strtolower(char *txt)
|
---|
246 | {
|
---|
247 | char *p;
|
---|
248 | for (p=txt; *p; p++)
|
---|
249 | if (isupper((uchar)*p)) *p=tolower((uchar)*p);
|
---|
250 | return(txt);
|
---|
251 | }
|
---|
252 |
|
---|
253 | int gethexval(char c)
|
---|
254 | {
|
---|
255 | if ((c>='0') && (c<='9')) return(c-'0');
|
---|
256 | if ((c>='A') && (c<='F')) return(c-'A'+10);
|
---|
257 | if ((c>='a') && (c<='f')) return(c-'a'+10);
|
---|
258 | return(-1);
|
---|
259 | }
|
---|
260 |
|
---|
261 | int cs_atob(uchar *buf, char *asc, int n)
|
---|
262 | {
|
---|
263 | int i, rc;
|
---|
264 | for (i=0; i<n; i++)
|
---|
265 | {
|
---|
266 | if ((rc=(gethexval(asc[i<<1])<<4)|gethexval(asc[(i<<1)+1]))&0x100)
|
---|
267 | return(-1);
|
---|
268 | buf[i]=rc;
|
---|
269 | }
|
---|
270 | return(n);
|
---|
271 | }
|
---|
272 |
|
---|
273 | ulong cs_atoi(char *asc, int l, int val_on_err)
|
---|
274 | {
|
---|
275 | int i, n=0;
|
---|
276 | ulong rc=0;
|
---|
277 | for (i=((l-1)<<1), errno=0; (i>=0) && (n<4); i-=2)
|
---|
278 | {
|
---|
279 | int b;
|
---|
280 | b=(gethexval(asc[i])<<4) | gethexval(asc[i+1]);
|
---|
281 | if (b<0)
|
---|
282 | {
|
---|
283 | errno=EINVAL;
|
---|
284 | rc=(val_on_err) ? 0xFFFFFFFF : 0;
|
---|
285 | break;
|
---|
286 | }
|
---|
287 | rc|=b<<(n<<3);
|
---|
288 | n++;
|
---|
289 | }
|
---|
290 | return(rc);
|
---|
291 | }
|
---|
292 |
|
---|
293 | int byte_atob(char *asc)
|
---|
294 | {
|
---|
295 | int rc;
|
---|
296 |
|
---|
297 | if (strlen(trim(asc))!=2)
|
---|
298 | rc=(-1);
|
---|
299 | else
|
---|
300 | if ((rc=(gethexval(asc[0])<<4)|gethexval(asc[1]))&0x100)
|
---|
301 | rc=(-1);
|
---|
302 | return(rc);
|
---|
303 | }
|
---|
304 |
|
---|
305 | long word_atob(char *asc)
|
---|
306 | {
|
---|
307 | long rc;
|
---|
308 |
|
---|
309 | if (strlen(trim(asc))!=4)
|
---|
310 | rc=(-1);
|
---|
311 | else
|
---|
312 | {
|
---|
313 | rc=gethexval(asc[0])<<12 | gethexval(asc[1])<<8 |
|
---|
314 | gethexval(asc[2])<<4 | gethexval(asc[3]);
|
---|
315 | if (rc&0x10000)
|
---|
316 | rc=(-1);
|
---|
317 | }
|
---|
318 | return(rc);
|
---|
319 | }
|
---|
320 |
|
---|
321 | /*
|
---|
322 | * dynamic word_atob
|
---|
323 | * converts an 1-4 digit asc hexstring
|
---|
324 | */
|
---|
325 | long dyn_word_atob(char *asc)
|
---|
326 | {
|
---|
327 | long rc = (-1);
|
---|
328 | int i, len = strlen(trim(asc));
|
---|
329 |
|
---|
330 | if (len <= 4 && len > 0) {
|
---|
331 | for(i = 0, rc = 0; i < len; i++)
|
---|
332 | rc = rc << 4 | gethexval(asc[i]);
|
---|
333 |
|
---|
334 | if (rc & 0x10000)
|
---|
335 | rc = (-1);
|
---|
336 | }
|
---|
337 | return(rc);
|
---|
338 | }
|
---|
339 |
|
---|
340 | int key_atob_l(char *asc, uchar *bin, int l)
|
---|
341 | {
|
---|
342 | int i, n1, n2, rc;
|
---|
343 | for (i=rc=0; i<l; i+=2)
|
---|
344 | {
|
---|
345 | if ((n1=gethexval(asc[i ]))<0) rc=(-1);
|
---|
346 | if ((n2=gethexval(asc[i+1]))<0) rc=(-1);
|
---|
347 | bin[i>>1]=(n1<<4)+(n2&0xff);
|
---|
348 | }
|
---|
349 | return(rc);
|
---|
350 | }
|
---|
351 |
|
---|
352 | char *cs_hexdump(int m, const uchar *buf, int n)
|
---|
353 | {
|
---|
354 | //TODO: not threadsafe
|
---|
355 | int i;
|
---|
356 | char *dump = (char *)cur_client()->dump;
|
---|
357 |
|
---|
358 | dump[i=0]='\0';
|
---|
359 | m=(m)?3:2;
|
---|
360 | if (m*n>=(int)sizeof(cur_client()->dump)) n=(sizeof(cur_client()->dump)/m)-1;
|
---|
361 | while (i<n)
|
---|
362 | sprintf(dump+(m*i++), "%02X%s", *buf++, (m>2)?" ":"");
|
---|
363 | return(dump);
|
---|
364 | }
|
---|
365 |
|
---|
366 | static int inet_byteorder=0;
|
---|
367 | in_addr_t cs_inet_order(in_addr_t n)
|
---|
368 | {
|
---|
369 | if (!inet_byteorder)
|
---|
370 | inet_byteorder=((inet_addr("1.2.3.4")+1)==inet_addr("1.2.3.5")) ? 1 : 2;
|
---|
371 | switch (inet_byteorder)
|
---|
372 | {
|
---|
373 | case 1:
|
---|
374 | break;
|
---|
375 | case 2:
|
---|
376 | n=((n&0xff000000) >> 24 ) |
|
---|
377 | ((n&0x00ff0000) >> 8 ) |
|
---|
378 | ((n&0x0000ff00) << 8 ) |
|
---|
379 | ((n&0x000000ff) << 24 );
|
---|
380 | break;
|
---|
381 | }
|
---|
382 | return(n);
|
---|
383 | }
|
---|
384 |
|
---|
385 | char *cs_inet_ntoa(in_addr_t n)
|
---|
386 | {
|
---|
387 | struct in_addr in;
|
---|
388 | in.s_addr=cs_inet_order(n);
|
---|
389 | return((char *)inet_ntoa(in));
|
---|
390 | }
|
---|
391 |
|
---|
392 | in_addr_t cs_inet_addr(char *txt)
|
---|
393 | {
|
---|
394 | if (!inet_byteorder)
|
---|
395 | inet_byteorder=((inet_addr("1.2.3.4")+1)==inet_addr("1.2.3.5")) ? 1 : 2;
|
---|
396 | if (inet_byteorder == 1)
|
---|
397 | return(inet_addr(txt));
|
---|
398 | else
|
---|
399 | return(inet_network(txt));
|
---|
400 | }
|
---|
401 |
|
---|
402 | ulong b2i(int n, uchar *b)
|
---|
403 | {
|
---|
404 | switch(n)
|
---|
405 | {
|
---|
406 | case 2:
|
---|
407 | return ((b[0]<<8) | b[1]);
|
---|
408 | case 3:
|
---|
409 | return ((b[0]<<16) | (b[1]<<8) | b[2]);
|
---|
410 | case 4:
|
---|
411 | return (((b[0]<<24) | (b[1]<<16) | (b[2]<<8) | b[3]) & 0xffffffffL);
|
---|
412 | default:
|
---|
413 | cs_log("Error in b2i, n=%i",n);
|
---|
414 | }
|
---|
415 | return 0;
|
---|
416 | }
|
---|
417 |
|
---|
418 | ullong b2ll(int n, uchar *b)
|
---|
419 | {
|
---|
420 | int i;
|
---|
421 | ullong k=0;
|
---|
422 | for(i=0; i<n; k+=b[i++])
|
---|
423 | k<<=8;
|
---|
424 | return(k);
|
---|
425 | }
|
---|
426 |
|
---|
427 | uchar *i2b(int n, ulong i)
|
---|
428 | {
|
---|
429 | uchar *b = cur_client()->dump;
|
---|
430 | switch(n)
|
---|
431 | {
|
---|
432 | case 2:
|
---|
433 | b[0]=(i>> 8) & 0xff;
|
---|
434 | b[1]=(i ) & 0xff;
|
---|
435 | break;
|
---|
436 | case 3:
|
---|
437 | b[0]=(i>>16) & 0xff;
|
---|
438 | b[1]=(i>> 8) & 0xff;
|
---|
439 | b[2]=(i ) & 0xff;
|
---|
440 | case 4:
|
---|
441 | b[0]=(i>>24) & 0xff;
|
---|
442 | b[1]=(i>>16) & 0xff;
|
---|
443 | b[2]=(i>> 8) & 0xff;
|
---|
444 | b[3]=(i ) & 0xff;
|
---|
445 | break;
|
---|
446 | }
|
---|
447 | return(b);
|
---|
448 | }
|
---|
449 |
|
---|
450 | ulong a2i(char *asc, int bytes)
|
---|
451 | {
|
---|
452 | int i, n;
|
---|
453 | ulong rc;
|
---|
454 | for (rc=i=0, n=strlen(trim(asc))-1; i<(abs(bytes)<<1); n--, i++)
|
---|
455 | if (n>=0)
|
---|
456 | {
|
---|
457 | int rcl;
|
---|
458 | if ((rcl=gethexval(asc[n]))<0)
|
---|
459 | {
|
---|
460 | errno=EINVAL;
|
---|
461 | return(0x1F1F1F);
|
---|
462 | }
|
---|
463 | rc|=(rcl<<(i<<2));
|
---|
464 | }
|
---|
465 | else
|
---|
466 | if (bytes<0)
|
---|
467 | rc|=(0xf<<(i<<2));
|
---|
468 | errno=0;
|
---|
469 | return(rc);
|
---|
470 | }
|
---|
471 |
|
---|
472 | int boundary(int exp, int n)
|
---|
473 | {
|
---|
474 | return((((n-1)>>exp)+1)<<exp);
|
---|
475 | }
|
---|
476 |
|
---|
477 | void cs_ftime(struct timeb *tp)
|
---|
478 | {
|
---|
479 | #ifdef NO_FTIME
|
---|
480 | struct timeval tv;
|
---|
481 | gettimeofday(&tv, (struct timezone *)0);
|
---|
482 | tp->time=tv.tv_sec;
|
---|
483 | tp->millitm=tv.tv_usec/1000;
|
---|
484 | #else
|
---|
485 | ftime(tp);
|
---|
486 | #endif
|
---|
487 | }
|
---|
488 |
|
---|
489 | void cs_sleepms(unsigned int msec)
|
---|
490 | {
|
---|
491 | //does not interfere with signals like sleep and usleep do
|
---|
492 | struct timespec req_ts;
|
---|
493 | req_ts.tv_sec = msec/1000;
|
---|
494 | req_ts.tv_nsec = (msec % 1000) * 1000000L;
|
---|
495 | nanosleep (&req_ts, NULL);
|
---|
496 | }
|
---|
497 |
|
---|
498 | void cs_sleepus(unsigned int usec)
|
---|
499 | {
|
---|
500 | //does not interfere with signals like sleep and usleep do
|
---|
501 | struct timespec req_ts;
|
---|
502 | req_ts.tv_sec = usec/1000000;
|
---|
503 | req_ts.tv_nsec = (usec % 1000000) * 1000L;
|
---|
504 | nanosleep (&req_ts, NULL);
|
---|
505 | }
|
---|
506 |
|
---|
507 | int bytes_available(int fd)
|
---|
508 | {
|
---|
509 | fd_set rfds;
|
---|
510 | fd_set erfds;
|
---|
511 | int select_ret;
|
---|
512 | int in_fd;
|
---|
513 |
|
---|
514 | in_fd=fd;
|
---|
515 |
|
---|
516 | FD_ZERO(&rfds);
|
---|
517 | FD_SET(in_fd, &rfds);
|
---|
518 |
|
---|
519 | FD_ZERO(&erfds);
|
---|
520 | FD_SET(in_fd, &erfds);
|
---|
521 |
|
---|
522 | select_ret = select(in_fd+1, &rfds, NULL, &erfds, NULL);
|
---|
523 | if (select_ret==-1)
|
---|
524 | {
|
---|
525 | cs_log("ERROR reading from fd %d select_ret=%i, errno=%d",in_fd, select_ret, errno);
|
---|
526 | return 0;
|
---|
527 | }
|
---|
528 |
|
---|
529 | if (FD_ISSET(in_fd, &erfds))
|
---|
530 | {
|
---|
531 | cs_log("ERROR reading from fd %d select_ret=%i, errno=%d",in_fd, select_ret, errno);
|
---|
532 | return 0;
|
---|
533 | }
|
---|
534 | if (FD_ISSET(in_fd,&rfds))
|
---|
535 | return 1;
|
---|
536 | else
|
---|
537 | return 0;
|
---|
538 | }
|
---|
539 |
|
---|
540 |
|
---|
541 | #ifdef OS_CYGWIN32
|
---|
542 | #include <windows.h>
|
---|
543 | void cs_setpriority(int prio)
|
---|
544 | {
|
---|
545 | HANDLE WinId;
|
---|
546 | ulong wprio;
|
---|
547 | switch((prio+20)/10)
|
---|
548 | {
|
---|
549 | case 0: wprio=REALTIME_PRIORITY_CLASS; break;
|
---|
550 | case 1: wprio=HIGH_PRIORITY_CLASS; break;
|
---|
551 | case 2: wprio=NORMAL_PRIORITY_CLASS; break;
|
---|
552 | default: wprio=IDLE_PRIORITY_CLASS; break;
|
---|
553 | }
|
---|
554 | WinId=GetCurrentProcess();
|
---|
555 | SetPriorityClass(WinId, wprio);
|
---|
556 | }
|
---|
557 | #else
|
---|
558 | void cs_setpriority(int prio)
|
---|
559 | {
|
---|
560 | #ifdef PRIO_PROCESS
|
---|
561 | setpriority(PRIO_PROCESS, 0, prio); // ignore errors
|
---|
562 | #endif
|
---|
563 | }
|
---|
564 | #endif
|
---|
565 |
|
---|
566 | /* Checks an array if it is filled (a value > 0) and returns the last position (1...length) where something was found.
|
---|
567 | length specifies the maximum length to check for. */
|
---|
568 | int check_filled(uchar *value, int length){
|
---|
569 | int i, j = 0;
|
---|
570 | for (i = 0; i < length; ++i){
|
---|
571 | if(value[j] > 0) j = i + 1;
|
---|
572 | }
|
---|
573 | return j;
|
---|
574 | }
|
---|
575 |
|
---|
576 | /* This function encapsulates malloc. It automatically adds an error message to the log if it failed and calls cs_exit(quiterror) if quiterror > -1.
|
---|
577 | result will be automatically filled with the new memory position or NULL on failure. */
|
---|
578 | void *cs_malloc(void *result, size_t size, int quiterror){
|
---|
579 | void **tmp = (void *)result;
|
---|
580 | *tmp = malloc (size);
|
---|
581 | if(*tmp == NULL){
|
---|
582 | cs_log("Couldn't allocate memory (errno=%d)!", errno);
|
---|
583 | if(quiterror > -1) cs_exit(quiterror);
|
---|
584 | }
|
---|
585 | return *tmp;
|
---|
586 | }
|
---|
587 |
|
---|
588 | /* This function encapsulates realloc. It automatically adds an error message to the log if it failed and calls cs_exit(quiterror) if quiterror > -1.
|
---|
589 | result will be automatically filled with the new memory position or NULL on failure. If a failure occured, the existing memory in result will be freed. */
|
---|
590 | void *cs_realloc(void *result, size_t size, int quiterror){
|
---|
591 | void **tmp = (void *)result, **tmp2 = (void *)result;
|
---|
592 | *tmp = realloc (*tmp, size);
|
---|
593 | //printf("reallocating\n");
|
---|
594 | //fflush(stdout);
|
---|
595 | if(*tmp == NULL){
|
---|
596 | cs_log("Couldn't allocate memory (errno=%d)!", errno);
|
---|
597 | free(*tmp2);
|
---|
598 | if(quiterror > -1) cs_exit(quiterror);
|
---|
599 | }
|
---|
600 | return *tmp;
|
---|
601 | }
|
---|
602 |
|
---|
603 | #ifdef WEBIF
|
---|
604 | /* Helper function for urldecode.*/
|
---|
605 | int x2i(int i){
|
---|
606 | i=toupper(i);
|
---|
607 | i = i - '0';
|
---|
608 | if(i > 9) i = i - 'A' + '9' + 1;
|
---|
609 | return i;
|
---|
610 | }
|
---|
611 |
|
---|
612 | /* Decodes values in a http url */
|
---|
613 | void urldecode(char *s){
|
---|
614 | int c, c1, n;
|
---|
615 | char *s0,*t;
|
---|
616 | t = s0 = s;
|
---|
617 | n = strlen(s);
|
---|
618 | while(n >0){
|
---|
619 | c = *s++;
|
---|
620 | if(c == '+') c = ' ';
|
---|
621 | else if(c == '%' && n > 2){
|
---|
622 | c = *s++;
|
---|
623 | c1 = c;
|
---|
624 | c = *s++;
|
---|
625 | c = 16*x2i(c1) + x2i(c);
|
---|
626 | n -= 2;
|
---|
627 | }
|
---|
628 | *t++ = c;
|
---|
629 | n--;
|
---|
630 | }
|
---|
631 | *t = 0;
|
---|
632 | }
|
---|
633 |
|
---|
634 | /* Helper function for urlencode.*/
|
---|
635 | char to_hex(char code){
|
---|
636 | static const char hex[] = "0123456789abcdef";
|
---|
637 | return hex[(int)code & 15];
|
---|
638 | }
|
---|
639 |
|
---|
640 | /* Encode values in a http url. Note: Be sure to free() the returned string after use */
|
---|
641 | char *urlencode(char *str){
|
---|
642 | char buf[strlen(str) * 3 + 1];
|
---|
643 | char *pstr = str, *pbuf = buf;
|
---|
644 | while (*pstr) {
|
---|
645 | if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') *pbuf++ = *pstr;
|
---|
646 | else if (*pstr == ' ') *pbuf++ = '+';
|
---|
647 | else {
|
---|
648 | *pbuf++ = '%';
|
---|
649 | *pbuf++ = to_hex(*pstr >> 4);
|
---|
650 | *pbuf++ = to_hex(*pstr & 15);
|
---|
651 | }
|
---|
652 | ++pstr;
|
---|
653 | }
|
---|
654 | *pbuf = '\0';
|
---|
655 | pbuf = (char *) malloc((strlen(buf) + 1) * sizeof(char));
|
---|
656 | strcpy(pbuf, buf);
|
---|
657 | return pbuf;
|
---|
658 | }
|
---|
659 |
|
---|
660 | /* Converts a char array to a char array with hex values (needed for example for md5). The hex2ascii
|
---|
661 | array is a lookup table with the corresponding hex string on the array position of the integer representation
|
---|
662 | of the ascii value. Note that result needs to be at least (p_array_len * 2) + 1 large. */
|
---|
663 | void char_to_hex(const unsigned char* p_array, unsigned int p_array_len, unsigned char *result, char hex2ascii[256][2]) {
|
---|
664 | result[p_array_len*2] = '\0';
|
---|
665 | const unsigned char* p_end = p_array + p_array_len;
|
---|
666 | size_t pos=0;
|
---|
667 | const unsigned char* p;
|
---|
668 | for( p = p_array; p != p_end; p++, pos+=2 ) {
|
---|
669 | result[pos] = hex2ascii[*p][0];
|
---|
670 | result[pos+1] = hex2ascii[*p][1];
|
---|
671 | }
|
---|
672 | }
|
---|
673 |
|
---|
674 | /* Creates a random string with specified length. Note that dst must be one larger than size to hold the trailing \0*/
|
---|
675 | void create_rand_str(char *dst, int size){
|
---|
676 | static const char text[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
---|
677 | int i;
|
---|
678 | for (i = 0; i < size; ++i){
|
---|
679 | dst[i] = text[rand() % (sizeof(text) - 1)];
|
---|
680 | }
|
---|
681 | dst[i] = '\0';
|
---|
682 | }
|
---|
683 | #endif
|
---|
684 |
|
---|
685 | /* Converts a uint64 value (you may also cast smaller types) to a char array in bitwise representation.
|
---|
686 | Note that the result array MUST be at least size+1 bit large and that this function assumes values
|
---|
687 | to be only positive! The result of e.g. uint64 7 is
|
---|
688 | 11100000000000000000000000000000000000000000000000000000000000000 this means the array is reversed */
|
---|
689 | void uint64ToBitchar(uint64 value, int size, char *result){
|
---|
690 | int pos;
|
---|
691 | for (pos=0;pos<size;pos++) result[pos]='0';
|
---|
692 | result[pos] = '\0';
|
---|
693 |
|
---|
694 | pos=0;
|
---|
695 | while (value > 0 && pos < size){
|
---|
696 | if(value % 2 == 1) result[pos]='1';
|
---|
697 | else result[pos]='0';
|
---|
698 | value=value / (uint64)2;
|
---|
699 | pos++;
|
---|
700 | }
|
---|
701 | }
|
---|
702 |
|
---|
703 | /* Return 1 if the file exists, else 0 */
|
---|
704 | int file_exists(const char * filename){
|
---|
705 | FILE *file;
|
---|
706 | if ((file = fopen(filename, "r"))){
|
---|
707 | fclose(file);
|
---|
708 | return 1;
|
---|
709 | }
|
---|
710 | return 0;
|
---|
711 | }
|
---|
712 |
|
---|
713 | /* Clears the s_ip structure provided. The pointer will be set to NULL so everything is cleared.*/
|
---|
714 | void clear_sip(struct s_ip **sip){
|
---|
715 | struct s_ip *cip = *sip, *lip;
|
---|
716 | for (*sip = NULL; cip != NULL; cip = lip){
|
---|
717 | lip = cip->next;
|
---|
718 | free(cip);
|
---|
719 | }
|
---|
720 | }
|
---|
721 |
|
---|
722 | /* Clears the s_ftab struct provided by setting nfilts and nprids to zero. */
|
---|
723 | void clear_ftab(struct s_ftab *ftab){
|
---|
724 | int i, j;
|
---|
725 | for (i = 0; i < CS_MAXFILTERS; i++) {
|
---|
726 | ftab->filts[i].caid = 0;
|
---|
727 | for (j = 0; j < CS_MAXPROV; j++)
|
---|
728 | ftab->filts[i].prids[j] = 0;
|
---|
729 | ftab->filts[i].nprids = 0;
|
---|
730 | }
|
---|
731 | ftab->nfilts = 0;
|
---|
732 | }
|
---|
733 |
|
---|
734 | /* Clears the s_ptab struct provided by setting nfilts and nprids to zero. */
|
---|
735 | void clear_ptab(struct s_ptab *ptab){
|
---|
736 | int i;
|
---|
737 | for (i = 0; i < ptab->nports; i++) {
|
---|
738 | ptab->ports[i].ftab.nfilts = 0;
|
---|
739 | ptab->ports[i].ftab.filts[0].nprids = 0;
|
---|
740 | }
|
---|
741 | ptab->nports = 0;
|
---|
742 | }
|
---|
743 |
|
---|
744 | /* Clears given caidtab */
|
---|
745 | void clear_caidtab(struct s_caidtab *ctab){
|
---|
746 | int i;
|
---|
747 | for (i = 0; i < CS_MAXCAIDTAB; i++) {
|
---|
748 | ctab->caid[i] = 0;
|
---|
749 | ctab->mask[i] = 0;
|
---|
750 | ctab->cmap[i] = 0;
|
---|
751 | }
|
---|
752 | }
|
---|
753 |
|
---|
754 | /* Clears given tuntab */
|
---|
755 | void clear_tuntab(struct s_tuntab *ttab){
|
---|
756 | int i;
|
---|
757 | for (i = 0; i < CS_MAXTUNTAB; i++) {
|
---|
758 | ttab->bt_caidfrom[i] = 0;
|
---|
759 | ttab->bt_caidto[i] = 0;
|
---|
760 | ttab->bt_srvid[i] = 0;
|
---|
761 | }
|
---|
762 | }
|
---|
763 | /* Overwrites destfile with tmpfile. If forceBakOverWrite = 0, the bakfile will not be overwritten if it exists, else it will be.*/
|
---|
764 | int safe_overwrite_with_bak(char *destfile, char *tmpfile, char *bakfile, int forceBakOverWrite){
|
---|
765 | if (file_exists(destfile)) {
|
---|
766 | if(forceBakOverWrite != 0 && file_exists(bakfile)){
|
---|
767 | if(remove(bakfile) < 0) cs_log("Error removing backup conf file %s (errno=%d)! Will try to proceed nonetheless...", bakfile, errno);
|
---|
768 | }
|
---|
769 | if(file_exists(bakfile)){
|
---|
770 | if(remove(destfile) < 0) {
|
---|
771 | cs_log("Error removing original conf file %s (errno=%d). Will maintain original one!", destfile, errno);
|
---|
772 | if(remove(tmpfile) < 0) cs_log("Error removing temp conf file %s (errno=%d)!", tmpfile, errno);
|
---|
773 | return(1);
|
---|
774 | }
|
---|
775 | } else {
|
---|
776 | if(rename(destfile, bakfile) < 0){
|
---|
777 | cs_log("Error renaming original conf file %s to %s (errno=%d). Will maintain original one!", destfile, bakfile, errno);
|
---|
778 | if(remove(tmpfile) < 0) cs_log("Error removing temp conf file %s (errno=%d)!", tmpfile, errno);
|
---|
779 | return(1);
|
---|
780 | }
|
---|
781 | }
|
---|
782 | if(rename(tmpfile, destfile) < 0){
|
---|
783 | cs_log("Error renaming new conf file %s to %s (errno=%d). The config will be missing upon next startup as this is non-recoverable!", tmpfile, destfile, errno);
|
---|
784 | return(1);
|
---|
785 | }
|
---|
786 | } else {
|
---|
787 | if(rename(tmpfile, destfile) < 0){
|
---|
788 | cs_log("Error renaming new conf file %s to %s (errno=%d). The config will be missing upon next startup as this is non-recoverable!", tmpfile, destfile, errno);
|
---|
789 | return(1);
|
---|
790 | }
|
---|
791 | }
|
---|
792 | return(0);
|
---|
793 | }
|
---|
794 |
|
---|
795 | /* Replacement of fprintf which adds necessary whitespace to fill up the varname to a fixed width.
|
---|
796 | If varname is longer than varnameWidth, no whitespace is added*/
|
---|
797 | void fprintf_conf(FILE *f, int varnameWidth, const char *varname, const char *fmtstring, ...){
|
---|
798 | int varlen = strlen(varname);
|
---|
799 | int max = (varlen > varnameWidth) ? varlen : varnameWidth;
|
---|
800 | char varnamebuf[max + 3];
|
---|
801 | char *ptr = varnamebuf + varlen;
|
---|
802 | va_list argptr;
|
---|
803 |
|
---|
804 | strcpy(varnamebuf, varname);
|
---|
805 | while(varlen < varnameWidth){
|
---|
806 | ptr[0] = ' ';
|
---|
807 | ++ptr;
|
---|
808 | ++varlen;
|
---|
809 | }
|
---|
810 | strcpy(ptr, "= ");
|
---|
811 | if (fwrite(varnamebuf, sizeof(char), strlen(varnamebuf), f)){
|
---|
812 | if(strlen(fmtstring) > 0){
|
---|
813 | va_start(argptr, fmtstring);
|
---|
814 | vfprintf(f, fmtstring, argptr);
|
---|
815 | va_end(argptr);
|
---|
816 | }
|
---|
817 | }
|
---|
818 | }
|
---|
819 |
|
---|
820 | /* Ordinary strncpy does not terminate the string if the source is exactly as long or longer as the specified size. This can raise security issues.
|
---|
821 | This function is a replacement which makes sure that a \0 is always added. num should be the real size of char array (do not subtract -1). */
|
---|
822 | void cs_strncpy(char * destination, const char * source, size_t num){
|
---|
823 | uint32 l, size = strlen(source);
|
---|
824 | if(size > num - 1) l = num - 1;
|
---|
825 | else l = size;
|
---|
826 | memcpy(destination, source, l);
|
---|
827 | destination[l] = '\0';
|
---|
828 | }
|
---|
829 |
|
---|
830 | char *get_servicename(int srvid, int caid){
|
---|
831 | int i;
|
---|
832 | struct s_srvid *this = cfg->srvid;
|
---|
833 | static char name[83];
|
---|
834 |
|
---|
835 | for (name[0] = 0; this && (!name[0]); this = this->next)
|
---|
836 | if (this->srvid == srvid)
|
---|
837 | for (i=0; i<this->ncaid; i++)
|
---|
838 | if (this->caid[i] == caid)
|
---|
839 | cs_strncpy(name, this->name, 32);
|
---|
840 |
|
---|
841 | if (!name[0]) sprintf(name, "%04X:%04X unknown", caid, srvid);
|
---|
842 | if (!srvid) name[0] = '\0';
|
---|
843 | return(name);
|
---|
844 | }
|
---|
845 |
|
---|
846 | char *get_tiername(int tierid, int caid){
|
---|
847 | int i;
|
---|
848 | struct s_tierid *this = cfg->tierid;
|
---|
849 | static char name[83];
|
---|
850 |
|
---|
851 | for (name[0] = 0; this && (!name[0]); this = this->next)
|
---|
852 | if (this->tierid == tierid)
|
---|
853 | for (i=0; i<this->ncaid; i++)
|
---|
854 | if (this->caid[i] == caid)
|
---|
855 | cs_strncpy(name, this->name, 32);
|
---|
856 |
|
---|
857 | //if (!name[0]) sprintf(name, "%04X:%04X unknown", caid, tierid);
|
---|
858 | if (!tierid) name[0] = '\0';
|
---|
859 | return(name);
|
---|
860 | }
|
---|
861 |
|
---|
862 | char *get_provider(int caid, ulong provid){
|
---|
863 | struct s_provid *this = cfg->provid;
|
---|
864 | static char name[83];
|
---|
865 |
|
---|
866 | for (name[0] = 0; this && (!name[0]); this = this->next) {
|
---|
867 | if (this->caid == caid && this->provid == provid) {
|
---|
868 | snprintf(name, 83, "%s", this->prov);
|
---|
869 | if (this->sat[0]) {
|
---|
870 | strcat(name, " / ");
|
---|
871 | strcat(name, this->sat);
|
---|
872 | }
|
---|
873 | if (this->lang[0]) {
|
---|
874 | strcat(name, " / ");
|
---|
875 | strcat(name, this->lang);
|
---|
876 | }
|
---|
877 | }
|
---|
878 | }
|
---|
879 |
|
---|
880 | if (!name[0]) snprintf(name, 83, "%04X:%06lX unknown", caid, provid);
|
---|
881 | if (!caid) name[0] = '\0';
|
---|
882 | return(name);
|
---|
883 | }
|
---|
884 |
|
---|
885 | void make_non_blocking(int fd) {
|
---|
886 | int fl;
|
---|
887 | fl=fcntl(fd, F_GETFL);
|
---|
888 | fcntl(fd, F_SETFL, fl | O_NONBLOCK | O_NDELAY);
|
---|
889 | }
|
---|
890 |
|
---|
891 | unsigned int seed;
|
---|
892 |
|
---|
893 | uchar fast_rnd() {
|
---|
894 | unsigned int offset = 12923;
|
---|
895 | unsigned int multiplier = 4079;
|
---|
896 |
|
---|
897 | seed = seed * multiplier + offset;
|
---|
898 | return (uchar) (seed % 0xFF);
|
---|
899 | }
|
---|
900 |
|
---|
901 | void init_rnd() {
|
---|
902 | seed = (unsigned int) time((time_t*)0);
|
---|
903 | }
|
---|
904 |
|
---|
905 | int hexserialset(struct s_reader *rdr)
|
---|
906 | {
|
---|
907 | int i;
|
---|
908 |
|
---|
909 | if (!rdr) return 0;
|
---|
910 |
|
---|
911 | for (i = 0; i < 8; i++)
|
---|
912 | if (rdr->hexserial[i])
|
---|
913 | return 1;
|
---|
914 | return 0;
|
---|
915 | }
|
---|
916 |
|
---|
917 | static char *netw_ext_prot[] = { "cccam", "cccam ext", "newcamd524" };
|
---|
918 |
|
---|
919 | char *reader_get_type_desc(struct s_reader * rdr, int extended)
|
---|
920 | {
|
---|
921 | static char *typtxt[] = { "unknown", "mouse", "mouse", "sc8in1", "mp35", "mouse", "internal", "smartreader", "pcsc" };
|
---|
922 | char *desc = typtxt[0];
|
---|
923 |
|
---|
924 | if (rdr->typ & R_IS_NETWORK)
|
---|
925 | desc = rdr->ph.desc;
|
---|
926 | else
|
---|
927 | desc = typtxt[rdr->typ];
|
---|
928 |
|
---|
929 | if ((rdr->typ == R_NEWCAMD) && (rdr->ncd_proto == NCD_524))
|
---|
930 | desc = netw_ext_prot[2];
|
---|
931 |
|
---|
932 | else if (rdr->typ == R_CCCAM) {
|
---|
933 | if (rdr->client && rdr->client->cc && ((struct cc_data *)rdr->client->cc)->extended_mode)
|
---|
934 | desc = netw_ext_prot[extended];
|
---|
935 | else
|
---|
936 | desc = netw_ext_prot[0];
|
---|
937 | }
|
---|
938 |
|
---|
939 | return (desc);
|
---|
940 | }
|
---|
941 |
|
---|
942 | char *monitor_get_proto(struct s_client *cl)
|
---|
943 | {
|
---|
944 | char *ctyp;
|
---|
945 | switch(cl->typ) {
|
---|
946 | case 's' : ctyp = "server"; break;
|
---|
947 | case 'p' :
|
---|
948 | case 'r' : ctyp = reader_get_type_desc(cl->reader, 1); break;
|
---|
949 | case 'c' :
|
---|
950 | if (cl->cc && ((struct cc_data *)cl->cc)->extended_mode) {
|
---|
951 | ctyp = netw_ext_prot[1];
|
---|
952 | break;
|
---|
953 | }
|
---|
954 | default : ctyp = ph[cl->ctyp].desc;
|
---|
955 | }
|
---|
956 | return(ctyp);
|
---|
957 | }
|
---|
958 |
|
---|
959 | /*
|
---|
960 | * resolve clienttype for newcamdprotocol
|
---|
961 | */
|
---|
962 | char *get_ncd_client_name(char *client_id)
|
---|
963 | {
|
---|
964 | static const int max_id_idx = 31;
|
---|
965 | static const char const *ncd_service_ids[] = { "0000", "5644", "4C43", "4333", "7264", "6762", "6D67", "7763", "6E73", "6378", "6B61",
|
---|
966 | "6576", "4343", "5456", "414C", "0666", "0667", "9911", "434C", "4765", "5342",
|
---|
967 | "6E65", "4E58", "4453", "8888", "7363", "0669", "0665", "0769", "4543", "6D63",
|
---|
968 | "6B63" };
|
---|
969 |
|
---|
970 | static char *ncd_service_names[] = { "generic", "vdr-sc", "LCE", "camd3", "radegast", "gbox2CS", "mgcamd", //actually a const so threadsafe
|
---|
971 | "WinCSC", "NewCS", "cx", "Kaffeine", "evocamd", "CCcam", "Tecview",
|
---|
972 | "AlexCS", "rqcamd", "rq-echo-client", "ACamd", "Cardlink", "Octagon", "SBCL",
|
---|
973 | "NextYE2k", "NextYE2k", "DiabloCam/UW", "OSCam", "Scam", "rq-sssp-client/CW",
|
---|
974 | "rq-sssp-client/CS", "JlsRq", "eyetvCamd", "mpcs", "kpcs", "unknown - please report" };
|
---|
975 |
|
---|
976 | int idx = 0;
|
---|
977 | for (idx = 0; idx <= max_id_idx; idx++) {
|
---|
978 | if(!memcmp(ncd_service_ids[idx], client_id, 4))
|
---|
979 | return ncd_service_names[idx];
|
---|
980 |
|
---|
981 | }
|
---|
982 |
|
---|
983 | return ncd_service_names[max_id_idx+1];
|
---|
984 | }
|
---|