[8] | 1 | #include "globals.h"
|
---|
| 2 |
|
---|
[4994] | 3 | static int32_t radegast_send(struct s_client * client, uchar *buf)
|
---|
[8] | 4 | {
|
---|
[4994] | 5 | int32_t l=buf[1]+2;
|
---|
[3394] | 6 | return(send(client->pfd, buf, l, 0));
|
---|
[8] | 7 | }
|
---|
| 8 |
|
---|
[4994] | 9 | static int32_t radegast_recv(struct s_client *client, uchar *buf, int32_t l)
|
---|
[8] | 10 | {
|
---|
[4994] | 11 | int32_t n;
|
---|
[3375] | 12 | if (!client->pfd) return(-1);
|
---|
[3422] | 13 | if (client->typ == 'c') { // server code
|
---|
[3375] | 14 | if ((n=recv(client->pfd, buf, l, 0))>0)
|
---|
| 15 | client->last=time((time_t *) 0);
|
---|
[648] | 16 | } else { // client code
|
---|
[3375] | 17 | if ((n=recv(client->pfd, buf, l, 0))>0) {
|
---|
[4141] | 18 | cs_ddump_mask(D_CLIENT, buf, n, "radegast: received %d bytes from %s", n, remote_txt());
|
---|
[3375] | 19 | client->last = time((time_t *) 0);
|
---|
[648] | 20 |
|
---|
| 21 | if (buf[0] == 2) { // dcw received
|
---|
| 22 | if (buf[3] != 0x10) { // dcw ok
|
---|
| 23 | cs_log("radegast: no dcw");
|
---|
| 24 | n = -1;
|
---|
| 25 | }
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 | }
|
---|
[8] | 29 | return(n);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
[4994] | 32 | static int32_t radegast_recv_chk(struct s_client *client, uchar *dcw, int32_t *rc, uchar *buf, int32_t UNUSED(n))
|
---|
[648] | 33 | {
|
---|
| 34 | if ((buf[0] == 2) && (buf[1] == 0x12)) {
|
---|
| 35 | memcpy(dcw, buf+4, 16);
|
---|
[4141] | 36 | cs_debug_mask(D_CLIENT, "radegast: recv chk - %s", cs_hexdump(0, dcw, 16));
|
---|
[648] | 37 | *rc = 1;
|
---|
[3614] | 38 | return(client->reader->msg_idx);
|
---|
[648] | 39 | }
|
---|
| 40 |
|
---|
| 41 | return (-1);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[8] | 44 | static void radegast_auth_client(in_addr_t ip)
|
---|
| 45 | {
|
---|
[4994] | 46 | int32_t ok;
|
---|
[8] | 47 | struct s_auth *account;
|
---|
[1090] | 48 |
|
---|
[4670] | 49 | ok = check_ip(cfg.rad_allowed, ip);
|
---|
[1090] | 50 |
|
---|
[8] | 51 | if (!ok)
|
---|
| 52 | {
|
---|
[3403] | 53 | cs_auth_client(cur_client(), (struct s_auth *)0, NULL);
|
---|
[8] | 54 | cs_exit(0);
|
---|
| 55 | }
|
---|
[1090] | 56 |
|
---|
[4588] | 57 | for (ok=0, account=cfg.account; (cfg.rad_usr[0]) && (account) && (!ok); account=account->next)
|
---|
[1090] | 58 | {
|
---|
[4588] | 59 | ok=(!strcmp(cfg.rad_usr, account->usr));
|
---|
[3403] | 60 | if (ok && cs_auth_client(cur_client(), account, NULL))
|
---|
[1090] | 61 | cs_exit(0);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[8] | 64 | if (!ok)
|
---|
[3403] | 65 | cs_auth_client(cur_client(), (struct s_auth *)(-1), NULL);
|
---|
[8] | 66 | }
|
---|
| 67 |
|
---|
[4994] | 68 | static int32_t get_request(uchar *buf)
|
---|
[8] | 69 | {
|
---|
[4994] | 70 | int32_t n, rc=0;
|
---|
[4588] | 71 | if ((n=process_input(buf, 2, cfg.cmaxidle))==2)
|
---|
[8] | 72 | {
|
---|
| 73 | if ((n=process_input(buf+2, buf[1], 0))>=0)
|
---|
| 74 | n+=2;
|
---|
| 75 | if (n-2==buf[1])
|
---|
| 76 | rc=n;
|
---|
| 77 | else
|
---|
| 78 | cs_log("WARNING: protocol error (garbage)");
|
---|
| 79 | }
|
---|
| 80 | if (n>0)
|
---|
[3337] | 81 | {
|
---|
[4141] | 82 | cs_ddump_mask(D_CLIENT, buf, n, "received %d bytes from client", n);
|
---|
[3337] | 83 | }
|
---|
[8] | 84 | return(rc);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[3374] | 87 | static void radegast_send_dcw(struct s_client *client, ECM_REQUEST *er)
|
---|
[8] | 88 | {
|
---|
[3172] | 89 | uchar mbuf[1024];
|
---|
| 90 | mbuf[0]=0x02; // DCW
|
---|
[4475] | 91 | if (er->rc < E_NOTFOUND)
|
---|
[8] | 92 | {
|
---|
[3172] | 93 | mbuf[1]=0x12; // len (overall)
|
---|
| 94 | mbuf[2]=0x05; // ACCESS
|
---|
| 95 | mbuf[3]=0x10; // len
|
---|
| 96 | memcpy(mbuf+4, er->cw, 16);
|
---|
[8] | 97 | }
|
---|
| 98 | else
|
---|
| 99 | {
|
---|
[3172] | 100 | mbuf[1]=0x02; // len (overall)
|
---|
| 101 | mbuf[2]=0x04; // NO ACCESS
|
---|
| 102 | mbuf[3]=0x00; // len
|
---|
[8] | 103 | }
|
---|
[3394] | 104 | radegast_send(client, mbuf);
|
---|
[8] | 105 | }
|
---|
| 106 |
|
---|
[4994] | 107 | static void radegast_process_ecm(uchar *buf, int32_t l)
|
---|
[8] | 108 | {
|
---|
[4994] | 109 | int32_t i, n, sl;
|
---|
[8] | 110 | ECM_REQUEST *er;
|
---|
| 111 |
|
---|
| 112 | if (!(er=get_ecmtask()))
|
---|
| 113 | return;
|
---|
| 114 | for (i=0; i<l; i+=(sl+2))
|
---|
| 115 | {
|
---|
| 116 | sl=buf[i+1];
|
---|
| 117 | switch(buf[i])
|
---|
| 118 | {
|
---|
| 119 | case 2: // CAID (upper byte only, oldstyle)
|
---|
| 120 | er->caid=buf[i+2]<<8;
|
---|
| 121 | break;
|
---|
| 122 | case 10: // CAID
|
---|
| 123 | er->caid=b2i(2, buf+i+2);
|
---|
| 124 | break;
|
---|
| 125 | case 3: // ECM DATA
|
---|
| 126 | er->l=sl;
|
---|
| 127 | memcpy(er->ecm, buf+i+2, er->l);
|
---|
| 128 | break;
|
---|
| 129 | case 6: // PROVID (ASCII)
|
---|
| 130 | n=(sl>6) ? 3 : (sl>>1);
|
---|
[70] | 131 | er->prid=cs_atoi((char *) buf+i+2+sl-(n<<1), n, 0);
|
---|
[8] | 132 | break;
|
---|
| 133 | case 7: // KEYNR (ASCII), not needed
|
---|
| 134 | break;
|
---|
| 135 | case 8: // ECM PROCESS PID ?? don't know, not needed
|
---|
| 136 | break;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | if (l!=i)
|
---|
| 140 | cs_log("WARNING: ECM-request corrupt");
|
---|
| 141 | else
|
---|
[3403] | 142 | get_cw(cur_client(), er);
|
---|
[8] | 143 | }
|
---|
| 144 |
|
---|
| 145 | static void radegast_process_unknown(uchar *buf)
|
---|
| 146 | {
|
---|
| 147 | uchar answer[2]={0x81, 0x00};
|
---|
[3403] | 148 | radegast_send(cur_client(), answer);
|
---|
[8] | 149 | cs_log("unknown request %02X, len=%d", buf[0], buf[1]);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[3210] | 152 | static void * radegast_server(void *cli)
|
---|
[8] | 153 | {
|
---|
[4994] | 154 | int32_t n;
|
---|
[3172] | 155 | uchar mbuf[1024];
|
---|
[8] | 156 |
|
---|
[3210] | 157 | struct s_client * client = (struct s_client *) cli;
|
---|
| 158 | client->thread=pthread_self();
|
---|
[3402] | 159 | pthread_setspecific(getclient, cli);
|
---|
[3153] | 160 |
|
---|
[3403] | 161 | radegast_auth_client(cur_client()->ip);
|
---|
[3172] | 162 | while ((n=get_request(mbuf))>0)
|
---|
[8] | 163 | {
|
---|
[3172] | 164 | switch(mbuf[0])
|
---|
[8] | 165 | {
|
---|
| 166 | case 1:
|
---|
[3172] | 167 | radegast_process_ecm(mbuf+2, mbuf[1]);
|
---|
[8] | 168 | break;
|
---|
| 169 | default:
|
---|
[3172] | 170 | radegast_process_unknown(mbuf);
|
---|
[8] | 171 | }
|
---|
| 172 | }
|
---|
[3362] | 173 | cs_disconnect_client(client);
|
---|
[3210] | 174 | return NULL;
|
---|
[8] | 175 | }
|
---|
| 176 |
|
---|
[4994] | 177 | static int32_t radegast_send_ecm(struct s_client *client, ECM_REQUEST *er, uchar *UNUSED(buf))
|
---|
[648] | 178 | {
|
---|
[4994] | 179 | int32_t n;
|
---|
[648] | 180 | uchar provid_buf[8];
|
---|
[5299] | 181 | uchar header[22] = "\x02\x01\x00\x06\x08\x30\x30\x30\x30\x30\x30\x30\x30\x07\x04\x30\x30\x30\x38\x08\x01\x02";
|
---|
| 182 | uchar *ecmbuf;
|
---|
| 183 | if(!cs_malloc(&ecmbuf,er->l + 30, -1)) return -1;
|
---|
[648] | 184 |
|
---|
| 185 | ecmbuf[0] = 1;
|
---|
| 186 | ecmbuf[1] = er->l + 30 - 2;
|
---|
| 187 | memcpy(ecmbuf + 2, header, sizeof(header));
|
---|
| 188 | for(n = 0; n < 4; n++) {
|
---|
[4896] | 189 | snprintf((char*)provid_buf+(n*2), sizeof(provid_buf)-(n*2), "%02X", ((uchar *)(&er->prid))[4 - 1 - n]);
|
---|
[648] | 190 | }
|
---|
| 191 | ecmbuf[7] = provid_buf[0];
|
---|
| 192 | ecmbuf[8] = provid_buf[1];
|
---|
| 193 | ecmbuf[9] = provid_buf[2];
|
---|
| 194 | ecmbuf[10] = provid_buf[3];
|
---|
| 195 | ecmbuf[11] = provid_buf[4];
|
---|
| 196 | ecmbuf[12] = provid_buf[5];
|
---|
| 197 | ecmbuf[13] = provid_buf[6];
|
---|
| 198 | ecmbuf[14] = provid_buf[7];
|
---|
| 199 | ecmbuf[2 + sizeof(header)] = 0xa;
|
---|
| 200 | ecmbuf[3 + sizeof(header)] = 2;
|
---|
| 201 | ecmbuf[4 + sizeof(header)] = er->caid >> 8;
|
---|
| 202 | ecmbuf[5 + sizeof(header)] = er->caid & 0xff;
|
---|
| 203 | ecmbuf[6 + sizeof(header)] = 3;
|
---|
| 204 | ecmbuf[7 + sizeof(header)] = er->l;
|
---|
| 205 | memcpy(ecmbuf + 8 + sizeof(header), er->ecm, er->l);
|
---|
| 206 | ecmbuf[4] = er->caid >> 8;
|
---|
| 207 |
|
---|
[3614] | 208 | client->reader->msg_idx = er->idx;
|
---|
[3373] | 209 | n = send(client->pfd, ecmbuf, er->l + 30, 0);
|
---|
[648] | 210 |
|
---|
| 211 | cs_log("radegast: sending ecm");
|
---|
[4141] | 212 | cs_ddump_mask(D_CLIENT, ecmbuf, er->l + 30, "ecm:");
|
---|
[648] | 213 |
|
---|
| 214 | free(ecmbuf);
|
---|
| 215 |
|
---|
| 216 | return 0;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[4994] | 219 | int32_t radegast_cli_init(struct s_client *cl)
|
---|
[648] | 220 | {
|
---|
[3435] | 221 | *cl = *cl; //prevent compiler warning
|
---|
[3191] | 222 | struct sockaddr_in loc_sa;
|
---|
[5205] | 223 | int32_t handle;
|
---|
[648] | 224 |
|
---|
[3403] | 225 | cur_client()->pfd=0;
|
---|
[3614] | 226 | if (cur_client()->reader->r_port<=0)
|
---|
[648] | 227 | {
|
---|
[3614] | 228 | cs_log("radegast: invalid port %d for server %s", cur_client()->reader->r_port, cur_client()->reader->device);
|
---|
[648] | 229 | return(1);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[3403] | 232 | cur_client()->ip=0;
|
---|
[648] | 233 | memset((char *)&loc_sa,0,sizeof(loc_sa));
|
---|
| 234 | loc_sa.sin_family = AF_INET;
|
---|
| 235 | #ifdef LALL
|
---|
[4588] | 236 | if (cfg.serverip[0])
|
---|
| 237 | loc_sa.sin_addr.s_addr = inet_addr(cfg.serverip);
|
---|
[648] | 238 | else
|
---|
| 239 | #endif
|
---|
| 240 | loc_sa.sin_addr.s_addr = INADDR_ANY;
|
---|
[3614] | 241 | loc_sa.sin_port = htons(cur_client()->reader->l_port);
|
---|
[648] | 242 |
|
---|
[5205] | 243 | if ((cur_client()->udp_fd=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0)
|
---|
[648] | 244 | {
|
---|
[4934] | 245 | cs_log("radegast: Socket creation failed (errno=%d %s)", errno, strerror(errno));
|
---|
[648] | 246 | cs_exit(1);
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | #ifdef SO_PRIORITY
|
---|
[4588] | 250 | if (cfg.netprio)
|
---|
[3403] | 251 | setsockopt(cur_client()->udp_fd, SOL_SOCKET, SO_PRIORITY,
|
---|
[4994] | 252 | (void *)&cfg.netprio, sizeof(uintptr_t));
|
---|
[648] | 253 | #endif
|
---|
[3614] | 254 | if (!cur_client()->reader->tcp_ito) {
|
---|
[4994] | 255 | uint32_t keep_alive = cur_client()->reader->tcp_ito?1:0;
|
---|
[3403] | 256 | setsockopt(cur_client()->udp_fd, SOL_SOCKET, SO_KEEPALIVE,
|
---|
[4994] | 257 | (void *)&keep_alive, sizeof(uintptr_t));
|
---|
[648] | 258 | }
|
---|
| 259 |
|
---|
[3403] | 260 | memset((char *)&cur_client()->udp_sa,0,sizeof(cur_client()->udp_sa));
|
---|
| 261 | cur_client()->udp_sa.sin_family = AF_INET;
|
---|
[4994] | 262 | cur_client()->udp_sa.sin_port = htons((uint16_t)cur_client()->reader->r_port);
|
---|
[648] | 263 |
|
---|
| 264 | cs_log("radegast: proxy %s:%d (fd=%d)",
|
---|
[3614] | 265 | cur_client()->reader->device, cur_client()->reader->r_port, cur_client()->udp_fd);
|
---|
[648] | 266 |
|
---|
[1125] | 267 | handle = network_tcp_connection_open();
|
---|
[648] | 268 | if(handle < 0) return -1;
|
---|
| 269 |
|
---|
[3614] | 270 | cur_client()->reader->tcp_connected = 2;
|
---|
[4452] | 271 | cur_client()->reader->card_status = CARD_INSERTED;
|
---|
[3614] | 272 | cur_client()->reader->last_g = cur_client()->reader->last_s = time((time_t *)0);
|
---|
[648] | 273 |
|
---|
[4141] | 274 | cs_debug_mask(D_CLIENT, "radegast: last_s=%d, last_g=%d", cur_client()->reader->last_s, cur_client()->reader->last_g);
|
---|
[648] | 275 |
|
---|
[3403] | 276 | cur_client()->pfd=cur_client()->udp_fd;
|
---|
[648] | 277 |
|
---|
| 278 | return(0);
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[8] | 281 | void module_radegast(struct s_module *ph)
|
---|
| 282 | {
|
---|
[3193] | 283 | static PTAB ptab; //since there is always only 1 radegast server running, this is threadsafe
|
---|
[4588] | 284 | ptab.ports[0].s_port = cfg.rad_port;
|
---|
[8] | 285 | ph->ptab = &ptab;
|
---|
| 286 | ph->ptab->nports = 1;
|
---|
| 287 |
|
---|
[4896] | 288 | cs_strncpy(ph->desc, "radegast", sizeof(ph->desc));
|
---|
[8] | 289 | ph->type=MOD_CONN_TCP;
|
---|
| 290 | ph->multi=0;
|
---|
| 291 | ph->watchdog=1;
|
---|
[4588] | 292 | ph->s_ip=cfg.rad_srvip;
|
---|
[8] | 293 | ph->s_handler=radegast_server;
|
---|
| 294 | ph->recv=radegast_recv;
|
---|
| 295 | ph->send_dcw=radegast_send_dcw;
|
---|
[648] | 296 | ph->c_multi=0;
|
---|
| 297 | ph->c_init=radegast_cli_init;
|
---|
| 298 | ph->c_recv_chk=radegast_recv_chk;
|
---|
| 299 | ph->c_send_ecm=radegast_send_ecm;
|
---|
[3168] | 300 | ph->num=R_RADEGAST;
|
---|
[8] | 301 | }
|
---|