1 | #include "globals.h"
|
---|
2 |
|
---|
3 | #define REQ_SIZE 4
|
---|
4 |
|
---|
5 | static int camd33_send(uchar *buf, int ml)
|
---|
6 | {
|
---|
7 | int l;
|
---|
8 | if (!cur_client()->pfd) return(-1);
|
---|
9 | l=boundary(4, ml);
|
---|
10 | memset(buf+ml, 0, l-ml);
|
---|
11 | cs_ddump_mask(D_CLIENT, buf, l, "send %d bytes to client", l);
|
---|
12 | if (cur_client()->crypted)
|
---|
13 | aes_encrypt(buf, l);
|
---|
14 | return(send(cur_client()->pfd, buf, l, 0));
|
---|
15 | }
|
---|
16 |
|
---|
17 | static int camd33_recv(struct s_client * client, uchar *buf, int l)
|
---|
18 | {
|
---|
19 | int n;
|
---|
20 | if (!client->pfd) return(-1);
|
---|
21 | if ((n=recv(client->pfd, buf, l, 0))>0)
|
---|
22 | {
|
---|
23 | client->last=time((time_t *) 0);
|
---|
24 | if (client->crypted)
|
---|
25 | aes_decrypt(buf, n);
|
---|
26 | }
|
---|
27 | cs_ddump_mask(D_CLIENT, buf, n, "received %d bytes from client", n);
|
---|
28 | return(n);
|
---|
29 | }
|
---|
30 |
|
---|
31 | static void camd33_request_emm()
|
---|
32 | {
|
---|
33 | uchar mbuf[20];
|
---|
34 | struct s_reader *aureader = NULL, *rdr = NULL;
|
---|
35 |
|
---|
36 | //TODO: just take the first reader in list
|
---|
37 | LL_ITER *itr = ll_iter_create(cur_client()->aureader_list);
|
---|
38 | while ((rdr = ll_iter_next(itr))) {
|
---|
39 | aureader=rdr;
|
---|
40 | break;
|
---|
41 | }
|
---|
42 | ll_iter_release(itr);
|
---|
43 |
|
---|
44 | if (!aureader) return;
|
---|
45 |
|
---|
46 | if (aureader->hexserial[0])
|
---|
47 | {
|
---|
48 | log_emm_request(aureader);
|
---|
49 | mbuf[0]=0;
|
---|
50 | mbuf[1]=aureader->caid>>8;
|
---|
51 | mbuf[2]=aureader->caid&0xff;
|
---|
52 | memcpy(mbuf+3, aureader->hexserial, 4);
|
---|
53 | memcpy(mbuf+7, &aureader->prid[0][1], 3);
|
---|
54 | memcpy(mbuf+10, &aureader->prid[2][1], 3);
|
---|
55 | camd33_send(mbuf, 13);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | static void camd33_auth_client(uchar *camdbug)
|
---|
60 | {
|
---|
61 | int i, rc;
|
---|
62 | uchar *usr=NULL, *pwd=NULL;
|
---|
63 | struct s_auth *account;
|
---|
64 | uchar mbuf[1024];
|
---|
65 |
|
---|
66 | cur_client()->crypted=cfg.c33_crypted;
|
---|
67 |
|
---|
68 | if (cur_client()->crypted)
|
---|
69 | cur_client()->crypted = check_ip(cfg.c33_plain, cur_client()->ip) ? 0 : 1;
|
---|
70 |
|
---|
71 | if (cur_client()->crypted)
|
---|
72 | aes_set_key((char *) cfg.c33_key);
|
---|
73 |
|
---|
74 | mbuf[0]=0;
|
---|
75 | camd33_send(mbuf, 1); // send login-request
|
---|
76 |
|
---|
77 | for (rc=0, camdbug[0]=0, mbuf[0]=1; (rc<2) && (mbuf[0]); rc++)
|
---|
78 | {
|
---|
79 | i=process_input(mbuf, sizeof(mbuf), 1);
|
---|
80 | if ((i>0) && (!mbuf[0]))
|
---|
81 | {
|
---|
82 | usr=mbuf+1;
|
---|
83 | pwd=usr+strlen((char *)usr)+2;
|
---|
84 | }
|
---|
85 | else
|
---|
86 | memcpy(camdbug+1, mbuf, camdbug[0]=i);
|
---|
87 | }
|
---|
88 | for (rc=-1, account=cfg.account; (usr) && (account) && (rc<0); account=account->next)
|
---|
89 | if ((!strcmp((char *)usr, account->usr)) && (!strcmp((char *)pwd, account->pwd)))
|
---|
90 | rc=cs_auth_client(cur_client(), account, NULL);
|
---|
91 | if (!rc)
|
---|
92 | camd33_request_emm();
|
---|
93 | else
|
---|
94 | {
|
---|
95 | if (rc<0) cs_auth_client(cur_client(), 0, usr ? "invalid account" : "no user given");
|
---|
96 | cs_exit(0);
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | static int get_request(uchar *buf, int n, uchar *camdbug)
|
---|
101 | {
|
---|
102 | int rc, w;
|
---|
103 | struct s_client *cur_cl = cur_client();
|
---|
104 |
|
---|
105 | if (camdbug[0])
|
---|
106 | {
|
---|
107 | memcpy(buf, camdbug+1, rc=camdbug[0]);
|
---|
108 | camdbug[0]=0;
|
---|
109 | return(rc);
|
---|
110 | }
|
---|
111 | for (rc=w=0; !rc;)
|
---|
112 | {
|
---|
113 | switch (rc=process_input(buf, 16, (w) ? cfg.ctimeout : cfg.cmaxidle))
|
---|
114 | {
|
---|
115 | case -9:
|
---|
116 | rc=0;
|
---|
117 | case 0:
|
---|
118 | if ((w) || cfg.c33_passive)
|
---|
119 | rc=-1;
|
---|
120 | else
|
---|
121 | {
|
---|
122 | buf[0]=0;
|
---|
123 | camd33_send(buf, 1);
|
---|
124 | w++;
|
---|
125 | }
|
---|
126 | case -1:
|
---|
127 | break;
|
---|
128 | default:
|
---|
129 | if (cur_cl->account && !memcmp(buf+1, cur_cl->account->usr, strlen(cur_cl->account->usr)))
|
---|
130 | {
|
---|
131 | cs_log("%s still alive", cs_inet_ntoa(cur_cl->ip));
|
---|
132 | rc=w=0;
|
---|
133 | }
|
---|
134 | else
|
---|
135 | {
|
---|
136 | switch (buf[0])
|
---|
137 | {
|
---|
138 | case 2:
|
---|
139 | case 3: w=boundary(4, buf[9]+10); break;
|
---|
140 | default: w=n; // garbage ?
|
---|
141 | }
|
---|
142 | w=process_input(buf+16, w-16, 0);
|
---|
143 | if (w>0) rc+=w;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | if (rc<0) rc=0;
|
---|
148 | return(rc);
|
---|
149 | }
|
---|
150 |
|
---|
151 | static void camd33_send_dcw(struct s_client *client, ECM_REQUEST *er)
|
---|
152 | {
|
---|
153 | uchar mbuf[1024];
|
---|
154 | mbuf[0]=2;
|
---|
155 | memcpy(mbuf+1, client->req+(er->cpti*REQ_SIZE), 4); // get pin
|
---|
156 | memcpy(mbuf+5, er->cw, 16);
|
---|
157 | camd33_send(mbuf, 21);
|
---|
158 | if (!cfg.c33_passive)
|
---|
159 | camd33_request_emm();
|
---|
160 | }
|
---|
161 |
|
---|
162 | static void camd33_process_ecm(uchar *buf, int l)
|
---|
163 | {
|
---|
164 | ECM_REQUEST *er;
|
---|
165 | if (!(er=get_ecmtask()))
|
---|
166 | return;
|
---|
167 | memcpy(cur_client()->req+(er->cpti*REQ_SIZE), buf+3, 4); // save pin
|
---|
168 | er->l=l-7;
|
---|
169 | er->caid=b2i(2, buf+1);
|
---|
170 | memcpy(er->ecm , buf+7, er->l);
|
---|
171 | get_cw(cur_client(), er);
|
---|
172 | }
|
---|
173 |
|
---|
174 | static void camd33_process_emm(uchar *buf, int l)
|
---|
175 | {
|
---|
176 | EMM_PACKET epg;
|
---|
177 | memset(&epg, 0, sizeof(epg));
|
---|
178 | epg.l=l-7;
|
---|
179 | memcpy(epg.caid , buf+1, 2);
|
---|
180 | memcpy(epg.hexserial, buf+3, 4);
|
---|
181 | memcpy(epg.emm , buf+7, epg.l);
|
---|
182 | do_emm(cur_client(), &epg);
|
---|
183 | }
|
---|
184 |
|
---|
185 | static void * camd33_server(void* cli)
|
---|
186 | {
|
---|
187 | int n;
|
---|
188 | uchar mbuf[1024];
|
---|
189 | uchar camdbug[256];
|
---|
190 |
|
---|
191 | struct s_client * client = (struct s_client *) cli;
|
---|
192 | client->thread=pthread_self();
|
---|
193 | pthread_setspecific(getclient, cli);
|
---|
194 |
|
---|
195 | client->req=(uchar *)malloc(CS_MAXPENDING*REQ_SIZE);
|
---|
196 | if (!client->req)
|
---|
197 | {
|
---|
198 | cs_log("Cannot allocate memory (errno=%d %s)", errno, strerror(errno));
|
---|
199 | cs_exit(1);
|
---|
200 | }
|
---|
201 | memset(client->req, 0, CS_MAXPENDING*REQ_SIZE);
|
---|
202 |
|
---|
203 | camd33_auth_client(camdbug);
|
---|
204 |
|
---|
205 | while ((n=get_request(mbuf, sizeof(mbuf), camdbug))>0)
|
---|
206 | {
|
---|
207 | switch(mbuf[0])
|
---|
208 | {
|
---|
209 | case 2:
|
---|
210 | camd33_process_ecm(mbuf, n);
|
---|
211 | break;
|
---|
212 | case 3:
|
---|
213 | camd33_process_emm(mbuf, n);
|
---|
214 | break;
|
---|
215 | default:
|
---|
216 | cs_debug_mask(D_CLIENT, "unknown command !");
|
---|
217 | }
|
---|
218 | }
|
---|
219 | cs_disconnect_client(client);
|
---|
220 | return NULL;
|
---|
221 | }
|
---|
222 |
|
---|
223 | void module_camd33(struct s_module *ph)
|
---|
224 | {
|
---|
225 | static PTAB ptab; //since there is always only 1 camd33 server running, this is threadsafe
|
---|
226 | ptab.ports[0].s_port = cfg.c33_port;
|
---|
227 | ph->ptab = &ptab;
|
---|
228 | ph->ptab->nports = 1;
|
---|
229 |
|
---|
230 | cs_strncpy(ph->desc, "camd33", sizeof(ph->desc));
|
---|
231 | ph->type=MOD_CONN_TCP;
|
---|
232 | ph->logtxt=cfg.c33_crypted ? ", crypted" : ", UNCRYPTED!";
|
---|
233 | ph->multi=1;
|
---|
234 | ph->watchdog=1;
|
---|
235 | ph->s_ip=cfg.c33_srvip;
|
---|
236 | ph->s_handler=camd33_server;
|
---|
237 | ph->recv=camd33_recv;
|
---|
238 | ph->send_dcw=camd33_send_dcw;
|
---|
239 | ph->num=R_CAMD33;
|
---|
240 | }
|
---|