summaryrefslogtreecommitdiffhomepage
path: root/npcompatsrv/npcompatsrv.c
blob: 1ad53d32610fdaeb5ca113914e83161df45b104a (plain)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644

/*

  Copyright (c) 2014 Samuel Lidén Borell <samuel@kodafritt.se>
 
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

#define _BSD_SOURCE
#define _POSIX_C_SOURCE 1
#include <string.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#define IPCVERSION "10"


/* this program is multiprocess with fork() so it's ok to have global data */
static char request[100*1024]; /* max 100 KiB */
static char result[400*1024]; /* max 400 KiB */

static pid_t signbinary_pid;
static FILE *pipe_in, *pipe_out;

static const char *ipc_argv[] = {
    NULL /* assigned in main() */, "--internal--ipc=" IPCVERSION, (char *)NULL,
};

#define PIPE_READ_END  0
#define PIPE_WRITE_END 1

static int open_pipes() {
    int pipeIn[2];
    int pipeOut[2];
    
    if (pipe(pipeIn) == -1 || pipe(pipeOut) == -1) {
        perror("failed to create pipe");
        return 0;
    }
    
    signbinary_pid = fork();
    if (signbinary_pid == 0) {
        /* Child process */
        close(STDOUT_FILENO);
        close(STDIN_FILENO);
        close(pipeIn[PIPE_READ_END]);
        close(pipeOut[PIPE_WRITE_END]);
        dup2(pipeIn[PIPE_WRITE_END], STDOUT_FILENO);
        dup2(pipeOut[PIPE_READ_END], STDIN_FILENO);
        
        /* These have been copied now */
        /*close(pipeIn[PIPE_WRITE_END]);
        close(pipeOut[PIPE_READ_END]);*/
        
        execvp(ipc_argv[0], (char *const *)ipc_argv);
        perror("failed to execute main binary");
        exit(1);
    } else {
        /* Parent process */
        close(pipeOut[PIPE_READ_END]);
        close(pipeIn[PIPE_WRITE_END]);
        
        pipe_in = fdopen(pipeIn[PIPE_READ_END], "r");
        pipe_out = fdopen(pipeOut[PIPE_WRITE_END], "w");
        return 1;
    }
}

static void ipc_send_str(FILE *out, const char *data)
{
    if (data) {
        int length = strlen(data);
        fprintf(out, "%d;", length);
        fwrite(data, length, 1, out);
        fprintf(out, "\n");
    } else {
        fprintf(out, "0;\n");
    }
}

static int ipc_read_int(FILE *in)
{
    int value = -1;
    if (fscanf(in, " %d;", &value) != 1) {
        perror("reading from pipe");
    }
    return value;
}

static char *ipc_read_string(FILE *in, int *lenptr) {
    char *data;
    int length = ipc_read_int(in);
    if (length <= 0) return strdup("");
    if (lenptr) {
        *lenptr = length;
    }
    
    data = malloc(length +1);
    if (!data) {
        perror("malloc string from pipe");
        return strdup("");
    }
    
    data[length] = '\0';
    if (fread(data, length, 1, in) == 1) {
        return data;
    } else {
        fprintf(stderr, "failed to read string from pipe");
        free(data);
        return strdup("");
    }
}


static int unhex(char c)
{
    if (c >= '0' && c <= '9') return c - '0';
    else if (c >= 'a' && c <= 'f') return c - 'a' + 0xa;
    else if (c >= 'A' && c <= 'F') return c - 'A' + 0xa;
    else return 0;
}

static int ipc_request(const char *url, const char *host, const char *ip,
                       const char *urlencoded, char **signature, int *siglen)
{
    /* Common parameters */
    int requestcmd = -1;
    char *requesttype = NULL, *nonce = NULL, *serverTime = NULL, *policys = NULL, *subjects = NULL;
    /* Signing paramters */
    char *messageEncoding = NULL, *message = NULL, *invisibleMessage = NULL;
    /* Result data */
    int errorcode;
    
    /* Parse request content */
    while (*urlencoded && *urlencoded != '\r' && *urlencoded != '\n') {
        char **valueptr = NULL;
        size_t valuelen;
        if (!strncmp(urlencoded, "requestType=", 12)) {
            urlencoded += 12;
            valueptr = &requesttype;
        } else if (!strncmp(urlencoded, "nonce=", 6)) {
            urlencoded += 6;
            valueptr = &nonce;
        } else if (!strncmp(urlencoded, "challenge=", 10)) {
            urlencoded += 10;
            valueptr = &nonce; /* yes, "challenge" is a synonym for "nonce" in this context */
        } else if (!strncmp(urlencoded, "servertime=", 11)) {
            urlencoded += 11;
            valueptr = &serverTime;
        } else if (!strncmp(urlencoded, "policys=", 8)) {
            urlencoded += 8;
            valueptr = &policys;
        } else if (!strncmp(urlencoded, "subjects=", 9)) {
            urlencoded += 9;
            valueptr = &subjects;
        } else if (!strncmp(urlencoded, "textcharacterencoding=", 22)) {
            urlencoded += 22;
            valueptr = &messageEncoding;
        } else if (!strncmp(urlencoded, "texttobesigned=", 15)) {
            urlencoded += 15;
            valueptr = &message;
        } else if (!strncmp(urlencoded, "nonvisibledata=", 15)) {
            urlencoded += 15;
            valueptr = &invisibleMessage;
        /* Ignored parameters and return parameters */
        } else if (!strncmp(urlencoded, "onlyacceptmru=", 14)) {
            urlencoded += 14;
            valueptr = NULL;
        } else if (!strncmp(urlencoded, "signature=", 10)) {
            urlencoded += 10;
            valueptr = NULL;
        } else {
            fprintf(stderr, "unrecognized url encoded data: %s\n", urlencoded);
            break;
        }
        
        
        valuelen = strcspn(urlencoded, "&");
        if (valueptr) {
            char *value = malloc(valuelen+1);
            const char *inp = urlencoded;
            char *outp = value;
            while (*inp && *inp != '&') {
                int val;
                if (*inp == '%' && inp[1] && inp[2]) {
                    val = (unhex(inp[1]) << 4) | unhex(inp[2]);
                    inp += 3;
                } else {
                    val = *inp;
                    inp++;
                }
                if (val <= ' ' || val >= 0x7F || val == ';') {
                    val = '_';
                }
                *outp = val;
                outp++;
            }
            
            *outp = '\0';
            *valueptr = value;
        }
        
        urlencoded += valuelen;
        if (*urlencoded == '&') {
            urlencoded++;
        }
    }
    
    if (!requesttype || !url || !host || !ip) return 20;
    
    if (!strcmp(requesttype, "authenticate")) requestcmd = 2;
    else if (!strcmp(requesttype, "sign")) requestcmd = 3;
    else return 50;
    
    if (requestcmd == 3 && !message) return 30;
    
    if (!serverTime || *serverTime == '\0') {
        serverTime = "0";
    } else if (strspn(serverTime, "0123456789") != strlen(serverTime)) {
        return 60;
    }
    
    /* Send IPC request */
    if (!open_pipes()) {
        return 40;
    }
/*    pipe_out = stderr;
fprintf(stderr, "------\n\n");*/
    fprintf(pipe_out, "%d;\n", requestcmd);
    ipc_send_str(pipe_out, url);
    ipc_send_str(pipe_out, host);
    ipc_send_str(pipe_out, ip);
    
    ipc_send_str(pipe_out, nonce);
    fprintf(pipe_out, "%s;\n", serverTime);
    ipc_send_str(pipe_out, policys);
    ipc_send_str(pipe_out, subjects);
    if (requestcmd == 3) {
        /* Sign */
        ipc_send_str(pipe_out, messageEncoding);
        ipc_send_str(pipe_out, message);
        ipc_send_str(pipe_out, invisibleMessage);
    }
    fprintf(pipe_out, "end\n");
    fflush(pipe_out);
    
    /* Read response */
    errorcode = ipc_read_int(pipe_in);
    *signature = ipc_read_string(pipe_in, siglen);
    
    /* Clean up */
    fclose(pipe_out);
    fclose(pipe_in);
    waitpid(signbinary_pid, NULL, 0);
    return errorcode;
}

static ssize_t findnlnl(const char *req, ssize_t from, ssize_t len)
{
    const char *r;
    ssize_t pos = 0;
    
    if (from > 0) {
        r = req+from-1;
        pos = from-1;
        len++;
    } else {
        r = req+from;
        pos = from;
    }
    
    while (len) {
        if (*r == '\n') {
            if (len >= 3 && r[1] == '\r' && r[2] == '\n') return pos+3;
            if (len >= 2 && r[1] == '\n') return pos+2;
        }
        len--;
        pos++;
        r++;
    }
    
    return 0;
}

static void skipws(const char **s, ssize_t *len)
{
    while (*len && (**s == ' ' || **s == '\t')) {
        ++*s;
        --*len;
    }
}

static const char *extract_header(const char *req, ssize_t len, const char *headername)
{
    ssize_t namelen = strlen(headername);
    while (len > 0) {
        if (*req == '\r' || *req == '\n') break;
        if (len <= namelen) break;
        
        if (!strncasecmp(req, headername, namelen)) {
            req += namelen;
            len -= namelen;
            
            skipws(&req, &len);
            if (*req == ':') {
                req++;
                len--;
                skipws(&req, &len);
                return req; /* terminated with a line end, \r\n, \n or \r */
            }
        }
        
        /* skip to end of line */
        while (len && *req != '\r' && *req != '\n') {
            req++;
            len--;
        }
        
        if (len >= 2 && *req == '\r' && req[1] == '\n') {
            req += 2;
            len -= 2;
        } else {
            req++;
            len--;
        }
    }
    return NULL;
}

/* XXX There's an unavoidable race condition in this function.
   The IP address returned might have changed and might not be what the
   browser has connected to. */
static char *get_ip_from_host(const char *hostname)
{
    char ip[NI_MAXHOST];
    const struct addrinfo *ai;
    struct addrinfo *firstai;
    
    int ret = getaddrinfo(hostname, NULL, NULL, &firstai);
    if (ret != 0) return NULL;
    
    /* Find first INET (IPv4) address (BankID supports IPv4 addresses only) */
    ai = firstai;
    while (ai && ai->ai_family != AF_INET)
        ai = ai->ai_next;
    
    if (!ai) return NULL;
    
    if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ip, NI_MAXHOST,
                    NULL, 0, NI_NUMERICHOST) != 0) {
        freeaddrinfo(firstai);
        return NULL;
    }
    freeaddrinfo(firstai);
    
    return strdup(ip);
}

static int handle_req(int sock, char *req, ssize_t len, ssize_t contentlen)
{
    int resultlen;
    
    (void)contentlen; /* unused */
    
    if (!strncasecmp(req, "POST /FriBID_NPAPI_Request HTTP/1.", 34)) {
        int errorcode, siglen, jsonlength;
        char *signature;
        /* Parse headers. Note: these are newline terminated! */
        const char *contenttype = extract_header(req, len, "Content-Type");
        char *origin = (char*)extract_header(req, len, "Origin");
        const char *domain = NULL, *ip = NULL;
        
        if (strncasecmp(contenttype, "application/x-www-form-urlencoded", 33)) {
            goto badreq;
        }
        
        /* Determine host/ip */
        if (origin) {
            origin[strcspn(origin, "\r\n")] = '\0';
            /* This depends on the browser providing reliable data, of course */
            if (!strncmp("https://", origin, 8)) {
                domain = origin+8;
                ip = get_ip_from_host(domain);
            }
        }
        
        /* Parse content */
        signature = NULL;
        siglen = 0;
        errorcode = ipc_request(origin, domain, ip,
                                req+len, &signature, &siglen);
        
        jsonlength = sprintf(result, "%d", errorcode) + siglen + 49;
        
        resultlen = sprintf(result,
            "HTTP/1.0 200 Ok\r\n"
            "Access-Control-Allow-Headers: content-type\r\n"
            "Access-Control-Allow-Methods: POST\r\n"
            "Access-Control-Allow-Origin: *\r\n"
            "Connection: keep-alive\r\n"
            "Content-Length: %d\r\n"
            "Content-Type: text/json\r\n"
            "\r\n"
            "{ \"errorCode\": %d, \"params\": { \"signature\": \"%.*s\" } }\n",
            jsonlength, errorcode, siglen, signature);
    } else if (!strncasecmp(req, "OPTIONS ", 8)) {
        resultlen = sprintf(result,
            "HTTP/1.0 200 Ok\r\n"
            "Access-Control-Allow-Headers: content-type\r\n"
            "Access-Control-Allow-Methods: POST\r\n"
            "Access-Control-Allow-Origin: *\r\n"
            "Allow: POST\r\n"
            "Connection: keep-alive\r\n"
            "Content-Length: 0\r\n"
            "\r\n");
    } else {
      badreq:
        resultlen = sprintf(result,
            "HTTP/1.0 400 Bad Request\r\n"
            "Connection: keep-alive\r\n"
            "Content-Length: 12\r\n"
            "Content-Type: text/html\r\n"
            "\r\n"
            "Bad Request\n");
    }
    
    
    if (send(sock, result, resultlen, 0) == -1) {
        perror("failed to send data");
    }
    return 1;
}

static int extract_contentlength(const char *req, ssize_t len)
{
    const char *value = extract_header(req, len, "Content-Length");
    return value ? atoi(value) : 0;
}

static void handle_connection(int sock)
{
    ssize_t bytesrecv = 0;
    
    while (1) {
        ssize_t nbytes = recv(sock, &request[bytesrecv], 4096-bytesrecv, 0);
        ssize_t reqend;
        if (nbytes == -1) {
            perror("failed to receive data");
            return;
        }
        
        if (nbytes == 0) return;
        
        reqend = findnlnl(request, bytesrecv, nbytes);
        bytesrecv += nbytes;
        if (reqend) {
            /* Read request body (if any) */
            int contentlength = extract_contentlength(request, reqend);
            if (contentlength > 90*1024) {
                int resultlen = sprintf(result,
                    "HTTP/1.0 413 Request Entity Too Large\r\n"
                    "Connection: close\r\n"
                    "Content-Length: 23\r\n"
                    "Content-Type: text/html\r\n"
                    "\r\n"
                    "Request Body Too Large\n");
                
                if (send(sock, result, resultlen, 0) == -1) {
                    perror("failed to send data");
                }
                return;
            }
            
            while (bytesrecv-reqend < contentlength) {
                ssize_t nbytes = recv(sock, &request[bytesrecv], contentlength-bytesrecv+reqend, 0);
                if (nbytes == -1) {
                    perror("failed to receive data");
                    return;
                }
                if (nbytes == 0) return;
                bytesrecv += nbytes;
            }
            request[reqend+contentlength] = '\0';
            
            /* Process the request */
            if (!handle_req(sock, request, reqend, contentlength)) {
                return;
            }
            memmove(request, request+reqend, bytesrecv-reqend);
            bytesrecv -= reqend+contentlength;
        }
    }
}

#define NUM_COMMON_PATHS 8
static const char *common_fribid_paths[NUM_COMMON_PATHS] = {
    "/usr/lib/fribid/sign",
    "/usr/local/lib/fribid/sign",
    "/usr/lib/x86_64-linux-gnu/fribid/sign",
    "/usr/local/lib/x86_64-linux-gnu/fribid/sign",
    "/usr/lib64/fribid/sign",
    "/usr/local/lib64/fribid/sign",
    "/usr/lib/i386-linux-gnu/fribid/sign",
    "/usr/local/lib/i386-linux-gnu/fribid/sign"
};

int main(int argc, const char **argv)
{
    int srvsock, opt, i;
    struct stat typecheck;
    struct sockaddr_in addr;
    
    /* Check arguments */
    if (argc > 2 || (argc > 1 && argv[1][0] == '-')) {
        printf("Usage: %s [path_to_signing_binary]\n\n"
               "For example:\n", argv[0]);
        for (i = 0; i < NUM_COMMON_PATHS; i++) {
            char exists = access(common_fribid_paths[i], X_OK) == 0 ? 'X' : ' ';
            printf(" <%c>  %s %s\n", exists, argv[0], common_fribid_paths[i]);
        }
        printf("\n<X> = exists on your system\n");
        return 1;
    }
    
    if (argc == 2) {
        if (access(argv[1], X_OK) != 0) {
            perror(argv[1]);
            return 1;
        }
        ipc_argv[0] = argv[1];
    } else {
        /* Try to autodetect path */
        for (i = 0; i < NUM_COMMON_PATHS; i++) {
            if (access(common_fribid_paths[i], X_OK) == 0) {
                ipc_argv[0] = common_fribid_paths[i];
                break;
            }
        }
        
        if (!ipc_argv[0]) {
            fprintf(stderr, "%s: failed to autodetect path to the FriBID \"sign\" binary.\n", argv[0]);
            return 1;
        }
    }
    
    /* Check that it's not e.g. a directory */
    if (stat(ipc_argv[0], &typecheck) == -1) {
        perror("failed to stat signing binary");
        return 1;
    }
    if (!S_ISREG(typecheck.st_mode)) {
        fprintf(stderr, "%s: Signing binary must be a regular executable file\n", ipc_argv[0]);
        return 1;
    }
    
    printf("Using binary: %s\n", ipc_argv[0]);
    
    /* Start the server */
    srvsock = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
    if (srvsock == -1) {
        perror("failed to create socket");
        return 1;
    }
    
    opt = 1;
    if (setsockopt(srvsock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
        perror("failed to set socket options");
        close(srvsock);
        return 1;
    }
    
    addr.sin_family = AF_INET;
    addr.sin_port = htons(20048);
    addr.sin_addr.s_addr = htonl(0x7F000001L);
    if (bind(srvsock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
        perror("failed to bind port/address");
        close(srvsock);
        return 1;
    }
    
    if (listen(srvsock, 50) == -1) {
        perror("failed to listen on socket");
        close(srvsock);
        return 1;
    }
    
    printf("Server started on port %d\n", ntohs(addr.sin_port));
    while (1) {
        pid_t pid;
        struct sockaddr connaddr;
        socklen_t connaddrlen = sizeof(connaddr);
        int connsock = accept(srvsock, &connaddr, &connaddrlen);
        
        if (connsock == -1) {
            perror("failed to set socket options");
            close(srvsock);
            return 1;
        }
        
        pid = fork();
        if (connsock == -1) {
            perror("failed to fork");
            close(connsock);
            continue;
        }
        
        if (pid != 0) {
            waitpid(pid, NULL, WNOHANG);
            continue;
        }
        
        /* This is executed in the forked process */
        close(srvsock);
        
        handle_connection(connsock);
        close(connsock);
        exit(0);
    }
}