- fix Building without Nagra not possible at Nagra_Merlin https://trac.streamboard...
[oscam.git] / module-camd33.c
blob48b0b4ee2c3be856ed905964b68aad6cfdaf2398
1 #define MODULE_LOG_PREFIX "camd33"
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"
12 #define REQ_SIZE 4
14 static int32_t camd33_send(uint8_t *buf, int32_t ml)
16 int32_t l;
18 if(!cur_client()->pfd)
20 return (-1);
23 l = boundary(4, ml);
24 memset(buf + ml, 0, l - ml);
25 cs_log_dump_dbg(D_CLIENT, buf, l, "send %d bytes to client", l);
27 if(cur_client()->crypted)
29 aes_encrypt_idx(cur_client()->aes_keys, buf, l);
32 return (send(cur_client()->pfd, buf, l, 0));
35 static int32_t camd33_recv(struct s_client *client, uint8_t *buf, int32_t l)
37 int32_t n;
39 if(!client->pfd)
41 return (-1);
44 if((n = cs_recv(client->pfd, buf, l, 0)) > 0)
46 client->last = time((time_t *)0);
47 if(client->crypted)
49 aes_encrypt_idx(cur_client()->aes_keys, buf, n);
52 cs_log_dump_dbg(D_CLIENT, buf, n, "received %d bytes from client", n);
54 return (n);
57 static void camd33_request_emm(void)
59 uint8_t mbuf[20];
60 struct s_reader *aureader = NULL, *rdr = NULL;
62 // TODO: just take the first reader in list
63 LL_ITER itr = ll_iter_create(cur_client()->aureader_list);
64 while((rdr = ll_iter_next(&itr)))
66 aureader = rdr;
67 break;
70 if(!aureader)
72 return;
75 if(aureader->hexserial[0])
77 cs_log("%s emm-request sent (reader=%s, caid=%04X, auprovid=%06X)",
78 username(cur_client()), aureader->label, aureader->caid,
79 aureader->auprovid ? aureader->auprovid : b2i(4, aureader->prid[0]));
81 mbuf[0] = 0;
82 mbuf[1] = aureader->caid >> 8;
83 mbuf[2] = aureader->caid & 0xff;
85 memcpy(mbuf + 3, aureader->hexserial, 4);
86 memcpy(mbuf + 7, &aureader->prid[0][1], 3);
87 memcpy(mbuf + 10, &aureader->prid[2][1], 3);
88 camd33_send(mbuf, 13);
92 static void camd33_auth_client(uint8_t *camdbug)
94 int32_t i, rc;
95 uint8_t *usr = NULL, *pwd = NULL;
96 struct s_auth *account;
97 uint8_t mbuf[1024];
98 struct s_client *cl = cur_client();
100 cl->crypted = cfg.c33_crypted;
101 if(cl->crypted)
103 cl->crypted = !check_ip(cfg.c33_plain, cl->ip);
106 if(cl->crypted)
108 if (!aes_set_key_alloc(&cl->aes_keys, (char *)cfg.c33_key))
110 cs_disconnect_client(cl);
111 return;
115 mbuf[0] = 0;
116 camd33_send(mbuf, 1); // send login-request
118 for(rc = 0, camdbug[0] = 0, mbuf[0] = 1; (rc < 2) && (mbuf[0]); rc++)
120 i = process_input(mbuf, sizeof(mbuf), 1);
121 if((i > 0) && (!mbuf[0]))
123 usr = mbuf + 1;
124 pwd = usr + cs_strlen((char *)usr) + 2;
126 else
128 memcpy(camdbug + 1, mbuf, camdbug[0] = i);
132 for(rc = -1, account = cfg.account; (usr) && (account) && (rc < 0); account = account->next)
134 if(streq((char *)usr, account->usr) && streq((char *)pwd, account->pwd))
136 rc = cs_auth_client(cl, account, NULL);
140 if(!rc)
142 camd33_request_emm();
144 else
146 if(rc < 0)
148 cs_auth_client(cl, 0, usr ? "invalid account" : "no user given");
150 cs_disconnect_client(cl);
154 static void camd33_send_dcw(struct s_client *UNUSED(client), ECM_REQUEST *er)
156 uint8_t mbuf[128];
158 mbuf[0] = 2;
159 memcpy(mbuf + 1, &er->msgid, 4); // get pin
160 memcpy(mbuf + 5, er->cw, 16);
161 camd33_send(mbuf, 21);
163 if(!cfg.c33_passive)
165 camd33_request_emm();
169 static void camd33_process_ecm(uint8_t *buf, int32_t l)
171 ECM_REQUEST *er;
173 if(l < 7)
175 return;
178 if(!(er = get_ecmtask()))
180 return;
183 memcpy(&er->msgid, buf + 3, 4); // save pin
184 er->ecmlen = l - 7;
186 if(er->ecmlen < 0 || er->ecmlen > MAX_ECM_SIZE)
188 NULLFREE(er);
189 return;
192 er->caid = b2i(2, buf + 1);
193 memcpy(er->ecm , buf + 7, er->ecmlen);
194 get_cw(cur_client(), er);
197 static void camd33_process_emm(uint8_t *buf, int32_t l)
199 EMM_PACKET epg;
201 if(l < 7)
203 return;
206 memset(&epg, 0, sizeof(epg));
207 epg.emmlen = l - 7;
209 if(epg.emmlen < 3 || epg.emmlen > MAX_EMM_SIZE)
211 return;
214 memcpy(epg.caid, buf + 1, 2);
215 memcpy(epg.hexserial, buf + 3, 4);
216 memcpy(epg.emm, buf + 7, epg.emmlen);
217 do_emm(cur_client(), &epg);
220 static void *camd33_server(struct s_client *UNUSED(client), uint8_t *mbuf, int32_t n)
222 switch(mbuf[0])
224 case 2:
225 camd33_process_ecm(mbuf, n);
226 break;
228 case 3:
229 camd33_process_emm(mbuf, n);
230 break;
232 default:
233 cs_log_dbg(D_CLIENT, "unknown command!");
236 return NULL;
239 static void camd33_server_init(struct s_client *UNUSED(client))
241 uint8_t camdbug[256];
243 camd33_auth_client(camdbug);
246 void module_camd33(struct s_module *ph)
248 cfg.c33_crypted = array_has_nonzero_byte(cfg.c33_key, sizeof(cfg.c33_key));
249 ph->ptab.nports = 1;
250 ph->ptab.ports[0].s_port = cfg.c33_port;
251 ph->desc = "camd33";
252 ph->type = MOD_CONN_TCP;
253 ph->large_ecm_support = 1;
254 ph->listenertype = LIS_CAMD33TCP;
255 IP_ASSIGN(ph->s_ip, cfg.c33_srvip);
256 ph->s_handler = camd33_server;
257 ph->s_init = camd33_server_init;
258 ph->recv = camd33_recv;
259 ph->send_dcw = camd33_send_dcw;
260 ph->num = R_CAMD33;
262 #endif