/
module-camd33.c
234 lines (209 loc) · 5.41 KB
/
module-camd33.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
#include "globals.h"
#define REQ_SIZE 4
static int32_t camd33_send(uchar *buf, int32_t ml)
{
int32_t l;
if (!cur_client()->pfd) return(-1);
l=boundary(4, ml);
memset(buf+ml, 0, l-ml);
cs_ddump_mask(D_CLIENT, buf, l, "send %d bytes to client", l);
if (cur_client()->crypted)
aes_encrypt(buf, l);
return(send(cur_client()->pfd, buf, l, 0));
}
static int32_t camd33_recv(struct s_client * client, uchar *buf, int32_t l)
{
int32_t n;
if (!client->pfd) return(-1);
if ((n=recv(client->pfd, buf, l, 0))>0)
{
client->last=time((time_t *) 0);
if (client->crypted)
aes_decrypt(buf, n);
}
cs_ddump_mask(D_CLIENT, buf, n, "received %d bytes from client", n);
return(n);
}
static void camd33_request_emm()
{
uchar mbuf[20];
struct s_reader *aureader = NULL, *rdr = NULL;
//TODO: just take the first reader in list
LL_ITER itr = ll_iter_create(cur_client()->aureader_list);
while ((rdr = ll_iter_next(&itr))) {
aureader=rdr;
break;
}
if (!aureader) return;
if (aureader->hexserial[0])
{
log_emm_request(aureader);
mbuf[0]=0;
mbuf[1]=aureader->caid>>8;
mbuf[2]=aureader->caid&0xff;
memcpy(mbuf+3, aureader->hexserial, 4);
memcpy(mbuf+7, &aureader->prid[0][1], 3);
memcpy(mbuf+10, &aureader->prid[2][1], 3);
camd33_send(mbuf, 13);
}
}
static void camd33_auth_client(uchar *camdbug)
{
int32_t i, rc;
uchar *usr=NULL, *pwd=NULL;
struct s_auth *account;
uchar mbuf[1024];
cur_client()->crypted=cfg.c33_crypted;
if (cur_client()->crypted)
cur_client()->crypted = check_ip(cfg.c33_plain, cur_client()->ip) ? 0 : 1;
if (cur_client()->crypted)
aes_set_key((char *) cfg.c33_key);
mbuf[0]=0;
camd33_send(mbuf, 1); // send login-request
for (rc=0, camdbug[0]=0, mbuf[0]=1; (rc<2) && (mbuf[0]); rc++)
{
i=process_input(mbuf, sizeof(mbuf), 1);
if ((i>0) && (!mbuf[0]))
{
usr=mbuf+1;
pwd=usr+strlen((char *)usr)+2;
}
else
memcpy(camdbug+1, mbuf, camdbug[0]=i);
}
for (rc=-1, account=cfg.account; (usr) && (account) && (rc<0); account=account->next)
if ((!strcmp((char *)usr, account->usr)) && (!strcmp((char *)pwd, account->pwd)))
rc=cs_auth_client(cur_client(), account, NULL);
if (!rc)
camd33_request_emm();
else
{
if (rc<0) cs_auth_client(cur_client(), 0, usr ? "invalid account" : "no user given");
cs_exit(0);
}
}
static int32_t get_request(uchar *buf, int32_t n, uchar *camdbug)
{
int32_t rc, w;
struct s_client *cur_cl = cur_client();
if (camdbug[0])
{
memcpy(buf, camdbug+1, rc=camdbug[0]);
camdbug[0]=0;
return(rc);
}
for (rc=w=0; !rc;)
{
switch (rc=process_input(buf, 16, (w) ? cfg.ctimeout : cfg.cmaxidle))
{
case -9:
rc=0;
case 0:
if ((w) || cfg.c33_passive)
rc=-1;
else
{
buf[0]=0;
camd33_send(buf, 1);
w++;
}
case -1:
break;
default:
if (cur_cl->account && !memcmp(buf+1, cur_cl->account->usr, strlen(cur_cl->account->usr)))
{
cs_log("%s still alive", cs_inet_ntoa(cur_cl->ip));
rc=w=0;
}
else
{
switch (buf[0])
{
case 2:
case 3: w=boundary(4, buf[9]+10); break;
default: w=n; // garbage ?
}
w=process_input(buf+16, w-16, 0);
if (w>0) rc+=w;
}
}
}
if (rc<0) rc=0;
return(rc);
}
static void camd33_send_dcw(struct s_client *client, ECM_REQUEST *er)
{
uchar mbuf[1024];
mbuf[0]=2;
memcpy(mbuf+1, client->req+(er->cpti*REQ_SIZE), 4); // get pin
memcpy(mbuf+5, er->cw, 16);
camd33_send(mbuf, 21);
if (!cfg.c33_passive)
camd33_request_emm();
}
static void camd33_process_ecm(uchar *buf, int32_t l)
{
ECM_REQUEST *er;
if (!(er=get_ecmtask()))
return;
memcpy(cur_client()->req+(er->cpti*REQ_SIZE), buf+3, 4); // save pin
er->l=l-7;
er->caid=b2i(2, buf+1);
memcpy(er->ecm , buf+7, er->l);
get_cw(cur_client(), er);
}
static void camd33_process_emm(uchar *buf, int32_t l)
{
EMM_PACKET epg;
memset(&epg, 0, sizeof(epg));
epg.l=l-7;
memcpy(epg.caid , buf+1, 2);
memcpy(epg.hexserial, buf+3, 4);
memcpy(epg.emm , buf+7, epg.l);
do_emm(cur_client(), &epg);
}
static void * camd33_server(void* cli)
{
int32_t n;
uchar mbuf[1024];
uchar camdbug[256];
struct s_client * client = (struct s_client *) cli;
client->thread=pthread_self();
pthread_setspecific(getclient, cli);
cs_malloc(&client->req,CS_MAXPENDING*REQ_SIZE, 1);
camd33_auth_client(camdbug);
while ((n=get_request(mbuf, sizeof(mbuf), camdbug))>0)
{
switch(mbuf[0])
{
case 2:
camd33_process_ecm(mbuf, n);
break;
case 3:
camd33_process_emm(mbuf, n);
break;
default:
cs_debug_mask(D_CLIENT, "unknown command !");
}
}
cs_disconnect_client(client);
return NULL;
}
void module_camd33(struct s_module *ph)
{
static PTAB ptab; //since there is always only 1 camd33 server running, this is threadsafe
ptab.ports[0].s_port = cfg.c33_port;
ph->ptab = &ptab;
ph->ptab->nports = 1;
cs_strncpy(ph->desc, "camd33", sizeof(ph->desc));
ph->type=MOD_CONN_TCP;
ph->listenertype = LIS_CAMD33TCP;
ph->logtxt=cfg.c33_crypted ? ", crypted" : ", UNCRYPTED!";
ph->multi=1;
ph->watchdog=1;
ph->s_ip=cfg.c33_srvip;
ph->s_handler=camd33_server;
ph->recv=camd33_recv;
ph->send_dcw=camd33_send_dcw;
ph->num=R_CAMD33;
}