1 | #define MODULE_LOG_PREFIX "camd33"
|
---|
2 |
|
---|
3 | #include "globals.h"
|
---|
4 | #ifdef MODULE_CAMD33
|
---|
5 | #include "oscam-aes.h"
|
---|
6 | #include "oscam-client.h"
|
---|
7 | #include "oscam-ecm.h"
|
---|
8 | #include "oscam-emm.h"
|
---|
9 | #include "oscam-net.h"
|
---|
10 | #include "oscam-string.h"
|
---|
11 |
|
---|
12 | #define REQ_SIZE 4
|
---|
13 |
|
---|
14 | static int32_t camd33_send(uchar *buf, int32_t ml)
|
---|
15 | {
|
---|
16 | int32_t l;
|
---|
17 | if(!cur_client()->pfd) { return (-1); }
|
---|
18 | l = boundary(4, ml);
|
---|
19 | memset(buf + ml, 0, l - ml);
|
---|
20 | cs_log_dump_dbg(D_CLIENT, buf, l, "send %d bytes to client", l);
|
---|
21 | if(cur_client()->crypted)
|
---|
22 | { aes_encrypt_idx(cur_client()->aes_keys, buf, l); }
|
---|
23 | return (send(cur_client()->pfd, buf, l, 0));
|
---|
24 | }
|
---|
25 |
|
---|
26 | static int32_t camd33_recv(struct s_client *client, uchar *buf, int32_t l)
|
---|
27 | {
|
---|
28 | int32_t n;
|
---|
29 | if(!client->pfd) { return (-1); }
|
---|
30 | if((n = recv(client->pfd, buf, l, 0)) > 0)
|
---|
31 | {
|
---|
32 | client->last = time((time_t *) 0);
|
---|
33 | if(client->crypted)
|
---|
34 | { aes_encrypt_idx(cur_client()->aes_keys, buf, n); }
|
---|
35 | }
|
---|
36 | cs_log_dump_dbg(D_CLIENT, buf, n, "received %d bytes from client", n);
|
---|
37 | return (n);
|
---|
38 | }
|
---|
39 |
|
---|
40 | static void camd33_request_emm(void)
|
---|
41 | {
|
---|
42 | uchar mbuf[20];
|
---|
43 | struct s_reader *aureader = NULL, *rdr = NULL;
|
---|
44 |
|
---|
45 | //TODO: just take the first reader in list
|
---|
46 | LL_ITER itr = ll_iter_create(cur_client()->aureader_list);
|
---|
47 | while((rdr = ll_iter_next(&itr)))
|
---|
48 | {
|
---|
49 | aureader = rdr;
|
---|
50 | break;
|
---|
51 | }
|
---|
52 |
|
---|
53 | if(!aureader) { return; }
|
---|
54 |
|
---|
55 | if(aureader->hexserial[0])
|
---|
56 | {
|
---|
57 | cs_log("%s emm-request sent (reader=%s, caid=%04X, auprovid=%06X)",
|
---|
58 | username(cur_client()), aureader->label, aureader->caid,
|
---|
59 | aureader->auprovid ? aureader->auprovid : b2i(4, aureader->prid[0]));
|
---|
60 | mbuf[0] = 0;
|
---|
61 | mbuf[1] = aureader->caid >> 8;
|
---|
62 | mbuf[2] = aureader->caid & 0xff;
|
---|
63 | memcpy(mbuf + 3, aureader->hexserial, 4);
|
---|
64 | memcpy(mbuf + 7, &aureader->prid[0][1], 3);
|
---|
65 | memcpy(mbuf + 10, &aureader->prid[2][1], 3);
|
---|
66 | camd33_send(mbuf, 13);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | static void camd33_auth_client(uchar *camdbug)
|
---|
71 | {
|
---|
72 | int32_t i, rc;
|
---|
73 | uchar *usr = NULL, *pwd = NULL;
|
---|
74 | struct s_auth *account;
|
---|
75 | uchar mbuf[1024];
|
---|
76 | struct s_client *cl = cur_client();
|
---|
77 |
|
---|
78 | cl->crypted = cfg.c33_crypted;
|
---|
79 |
|
---|
80 | if(cl->crypted)
|
---|
81 | { cl->crypted = !check_ip(cfg.c33_plain, cl->ip); }
|
---|
82 |
|
---|
83 | if(cl->crypted)
|
---|
84 | {
|
---|
85 | if (!aes_set_key_alloc(&cl->aes_keys, (char *)cfg.c33_key))
|
---|
86 | {
|
---|
87 | cs_disconnect_client(cl);
|
---|
88 | return;
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | mbuf[0] = 0;
|
---|
93 | camd33_send(mbuf, 1); // send login-request
|
---|
94 |
|
---|
95 | for(rc = 0, camdbug[0] = 0, mbuf[0] = 1; (rc < 2) && (mbuf[0]); rc++)
|
---|
96 | {
|
---|
97 | i = process_input(mbuf, sizeof(mbuf), 1);
|
---|
98 | if((i > 0) && (!mbuf[0]))
|
---|
99 | {
|
---|
100 | usr = mbuf + 1;
|
---|
101 | pwd = usr + strlen((char *)usr) + 2;
|
---|
102 | }
|
---|
103 | else
|
---|
104 | { memcpy(camdbug + 1, mbuf, camdbug[0] = i); }
|
---|
105 | }
|
---|
106 | for(rc = -1, account = cfg.account; (usr) && (account) && (rc < 0); account = account->next)
|
---|
107 | if(streq((char *)usr, account->usr) && streq((char *)pwd, account->pwd))
|
---|
108 | { rc = cs_auth_client(cl, account, NULL); }
|
---|
109 | if(!rc)
|
---|
110 | { camd33_request_emm(); }
|
---|
111 | else
|
---|
112 | {
|
---|
113 | if(rc < 0) { cs_auth_client(cl, 0, usr ? "invalid account" : "no user given"); }
|
---|
114 | cs_disconnect_client(cl);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | static void camd33_send_dcw(struct s_client *UNUSED(client), ECM_REQUEST *er)
|
---|
119 | {
|
---|
120 | uchar mbuf[1024];
|
---|
121 | mbuf[0] = 2;
|
---|
122 | memcpy(mbuf + 1, &er->msgid, 4); // get pin
|
---|
123 | memcpy(mbuf + 5, er->cw, 16);
|
---|
124 | camd33_send(mbuf, 21);
|
---|
125 | if(!cfg.c33_passive)
|
---|
126 | { camd33_request_emm(); }
|
---|
127 | }
|
---|
128 |
|
---|
129 | static void camd33_process_ecm(uchar *buf, int32_t l)
|
---|
130 | {
|
---|
131 | ECM_REQUEST *er;
|
---|
132 | if(!(er = get_ecmtask()))
|
---|
133 | { return; }
|
---|
134 | memcpy(&er->msgid, buf + 3, 4); // save pin
|
---|
135 | er->ecmlen = l - 7;
|
---|
136 | er->caid = b2i(2, buf + 1);
|
---|
137 | memcpy(er->ecm , buf + 7, er->ecmlen);
|
---|
138 | get_cw(cur_client(), er);
|
---|
139 | }
|
---|
140 |
|
---|
141 | static void camd33_process_emm(uchar *buf, int32_t l)
|
---|
142 | {
|
---|
143 | EMM_PACKET epg;
|
---|
144 | memset(&epg, 0, sizeof(epg));
|
---|
145 | epg.emmlen = l - 7;
|
---|
146 | memcpy(epg.caid , buf + 1, 2);
|
---|
147 | memcpy(epg.hexserial, buf + 3, 4);
|
---|
148 | memcpy(epg.emm , buf + 7, epg.emmlen);
|
---|
149 | do_emm(cur_client(), &epg);
|
---|
150 | }
|
---|
151 |
|
---|
152 | static void *camd33_server(struct s_client *UNUSED(client), uchar *mbuf, int32_t n)
|
---|
153 | {
|
---|
154 | switch(mbuf[0])
|
---|
155 | {
|
---|
156 | case 2:
|
---|
157 | camd33_process_ecm(mbuf, n);
|
---|
158 | break;
|
---|
159 | case 3:
|
---|
160 | camd33_process_emm(mbuf, n);
|
---|
161 | break;
|
---|
162 | default:
|
---|
163 | cs_log_dbg(D_CLIENT, "unknown command !");
|
---|
164 | }
|
---|
165 |
|
---|
166 | return NULL;
|
---|
167 | }
|
---|
168 |
|
---|
169 | static void camd33_server_init(struct s_client *UNUSED(client))
|
---|
170 | {
|
---|
171 | uchar camdbug[256];
|
---|
172 |
|
---|
173 | camd33_auth_client(camdbug);
|
---|
174 | }
|
---|
175 |
|
---|
176 | void module_camd33(struct s_module *ph)
|
---|
177 | {
|
---|
178 | cfg.c33_crypted = check_filled(cfg.c33_key, sizeof(cfg.c33_key));
|
---|
179 | ph->ptab.nports = 1;
|
---|
180 | ph->ptab.ports[0].s_port = cfg.c33_port;
|
---|
181 |
|
---|
182 | ph->desc = "camd33";
|
---|
183 | ph->type = MOD_CONN_TCP;
|
---|
184 | ph->large_ecm_support = 1;
|
---|
185 | ph->listenertype = LIS_CAMD33TCP;
|
---|
186 | IP_ASSIGN(ph->s_ip, cfg.c33_srvip);
|
---|
187 | ph->s_handler = camd33_server;
|
---|
188 | ph->s_init = camd33_server_init;
|
---|
189 | ph->recv = camd33_recv;
|
---|
190 | ph->send_dcw = camd33_send_dcw;
|
---|
191 | ph->num = R_CAMD33;
|
---|
192 | }
|
---|
193 | #endif
|
---|