forked from paul2112/pushpool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
1360 lines (1112 loc) · 29.2 KB
/
server.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
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2011 Jeff Garzik
* Copyright 2009 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include "autotools-config.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <locale.h>
#include <unistd.h>
#include <signal.h>
#include <syslog.h>
#include <fcntl.h>
#include <string.h>
#include <zlib.h>
#include <netdb.h>
#include <stdarg.h>
#include <openssl/sha.h>
#include <argp.h>
#include "server.h"
const char *argp_program_version = PACKAGE_VERSION;
enum {
CLI_RD_TIMEOUT = 30,
CLI_MAX_MSG = (1 * 1024 * 1024),
SFL_FOREGROUND = (1 << 0), /* run in foreground */
};
static struct argp_option options[] = {
{ "config", 'c', "FILE", 0,
"Read master configuration from FILE (default: server.json)" },
{ "debug", 'D', "LEVEL", 0,
"Set debug output to LEVEL (0 = off, 2 = max)" },
{ "stderr", 'E', NULL, 0,
"Switch the log to standard error" },
{ "foreground", 'F', NULL, 0,
"Run in foreground, do not fork" },
{ "pid", 'P', "FILE", 0,
"Write daemon process id to FILE" },
{ "strict-free", 1001, NULL, 0,
"For memory-checker runs. When shutting down server, free local "
"heap, rather than simply exit(2)ing and letting OS clean up." },
{ }
};
static const char doc[] =
PROGRAM_NAME " - push-mining proxy daemon";
static error_t parse_opt (int key, char *arg, struct argp_state *state);
static const struct argp argp = { options, parse_opt, NULL, doc };
static bool server_running = true;
static bool dump_stats;
static bool reopen_logs;
static bool trigger_lp_flush;
bool use_syslog = true;
static bool strict_free = false;
int debugging = 0;
struct timeval current_time;
struct server srv = {
.config = "server.json",
.pid_fd = -1,
.req_fd = -1,
.share_fd = -1,
#if defined(HAVE_SQLITE3)
.db_eng = SDB_SQLITE,
.db_ops = &sqlite_db_ops,
#elif defined(HAVE_MYSQL)
.db_eng = SDB_MYSQL,
.db_ops = &mysql_db_ops,
#elif defined(HAVE_POSTGRESQL)
.db_eng = SDB_POSTGRESQL,
.db_ops = &postgresql_db_ops,
#else
#error("No valid database engines defined")
#endif
.db_port = -1,
.cred_expire = 75,
.work_expire = 120,
};
static error_t parse_opt (int key, char *arg, struct argp_state *state)
{
int v;
switch(key) {
case 'c':
srv.config = arg;
break;
case 'D':
v = atoi(arg);
if (v < 0 || v > 2) {
fprintf(stderr, "invalid debug level: '%s'\n", arg);
argp_usage(state);
}
debugging = v;
break;
case 'E':
use_syslog = false;
break;
case 'F':
srv.flags |= SFL_FOREGROUND;
break;
case 'P':
srv.pid_file = strdup(arg);
break;
case 1001: /* --strict-free */
strict_free = true;
break;
case ARGP_KEY_ARG:
argp_usage(state); /* too many args */
break;
case ARGP_KEY_END:
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static json_t *cjson_decode(void *buf, size_t buflen)
{
json_t *obj = NULL;
json_error_t err;
void *obj_unc = NULL;
unsigned long dest_len;
void *comp_p;
uint32_t unc_len;
unsigned char zero = 0;
if (buflen < 6)
return NULL;
/* look at first 32 bits of buffer, which contains uncompressed len */
unc_len = le32toh(*((uint32_t *)buf));
if (unc_len > CLI_MAX_MSG)
return NULL;
/* alloc buffer for uncompressed data */
obj_unc = malloc(unc_len + 1);
if (!obj_unc)
return NULL;
dest_len = unc_len;
/* decompress buffer (excluding first 32 bits) */
comp_p = buf + 4;
if (uncompress(obj_unc, &dest_len, comp_p, buflen - 4) != Z_OK)
goto out;
if (dest_len != unc_len)
goto out;
memcpy(obj_unc + unc_len, &zero, 1); /* null terminate */
/* attempt JSON decode of buffer */
obj = JSON_LOADS(obj_unc, &err);
out:
free(obj_unc);
return obj;
}
bool cjson_encode(unsigned char op, const char *obj_unc,
void **buf_out, size_t *buflen_out)
{
void *obj_comp, *raw_msg = NULL;
uint32_t *obj_clen;
struct ubbp_header *msg_hdr;
unsigned long comp_len;
size_t payload_len;
size_t unc_len = strlen(obj_unc);
*buf_out = NULL;
*buflen_out = 0;
/* create buffer for entire msg (header + contents), assuming
* a worst case where compressed data may be slightly larger than
* input data
*/
raw_msg = calloc(1, unc_len + 64);
if (!raw_msg)
goto err_out;
/* get ptr to uncompressed-length value, which follows header */
obj_clen = raw_msg + sizeof(struct ubbp_header);
/* get ptr to compressed data area, which follows hdr & uncompr. len */
obj_comp = raw_msg + sizeof(struct ubbp_header) + sizeof(uint32_t);
/* compress data */
comp_len = unc_len + 64 -
(sizeof(struct ubbp_header) + sizeof(uint32_t));
if (compress2(obj_comp, &comp_len,
(Bytef *) obj_unc, unc_len, 9) != Z_OK)
goto err_out;
/* fill in UBBP message header */
msg_hdr = raw_msg;
memcpy(msg_hdr->magic, PUSHPOOL_UBBP_MAGIC, 4);
payload_len = sizeof(uint32_t) + comp_len;
msg_hdr->op_size = htole32(UBBP_OP_SIZE(op, payload_len));
/* fill in uncompressed length */
*obj_clen = htole32(unc_len);
/* return entire message */
*buf_out = raw_msg;
*buflen_out = sizeof(struct ubbp_header) + payload_len;
return true;
err_out:
free(raw_msg);
return false;
}
bool cjson_encode_obj(unsigned char op, const json_t *obj,
void **buf_out, size_t *buflen_out)
{
char *obj_unc;
bool rc;
*buf_out = NULL;
*buflen_out = 0;
/* encode JSON object to flat string */
obj_unc = json_dumps(obj, JSON_COMPACT | JSON_SORT_KEYS);
if (!obj_unc)
return false;
/* build message, with compressed JSON payload */
rc = cjson_encode(op, obj_unc, buf_out, buflen_out);
free(obj_unc);
return rc;
}
static void cli_free(struct client *cli)
{
if (!cli)
return;
if (cli->ev_mask && (event_del(&cli->ev) < 0))
applog(LOG_ERR, "TCP cli poll del failed");
/* clean up network socket */
if (cli->fd >= 0) {
if (close(cli->fd) < 0)
syslogerr("close(2) TCP client socket");
}
if (debugging)
applog(LOG_DEBUG, "client %s ended", cli->addr_host);
tcp_read_free(&cli->rst);
free(cli->msg);
memset(cli, 0, sizeof(*cli)); /* poison */
free(cli);
}
static struct client *cli_alloc(int fd, struct sockaddr_in6 *addr,
socklen_t addrlen, bool have_http)
{
struct client *cli;
cli = calloc(1, sizeof(*cli));
if (!cli)
return NULL;
cli->fd = fd;
memcpy(&cli->addr, addr, addrlen);
tcp_read_init(&cli->rst, cli->fd, cli);
return cli;
}
bool cli_send_msg(struct client *cli, const void *msg, size_t msg_len)
{
ssize_t wrc;
/* send packet to client. fail on all cases where
* message is not transferred entirely into the
* socket buffer on this write(2) call
*/
wrc = write(cli->fd, msg, msg_len);
return (wrc == msg_len);
}
bool cli_send_hdronly(struct client *cli, unsigned char op)
{
struct ubbp_header hdr;
memcpy(&hdr.magic, PUSHPOOL_UBBP_MAGIC, 4);
hdr.op_size = htole32(UBBP_OP_SIZE(op, 0));
return cli_send_msg(cli, &hdr, sizeof(hdr));
}
bool cli_send_obj(struct client *cli, unsigned char op, const json_t *obj)
{
void *raw_msg = NULL;
size_t msg_len;
bool rc = false;
/* create compressed message packet */
if (!cjson_encode_obj(op, obj, &raw_msg, &msg_len))
goto out;
rc = cli_send_msg(cli, raw_msg, msg_len);
out:
free(raw_msg);
return rc;
}
bool cli_send_err(struct client *cli, unsigned char op,
int err_code, const char *err_msg)
{
char *s = NULL;
void *raw_msg = NULL;
size_t msg_len;
bool rc = false;
/* build JSON error string, strangely similar to JSON-RPC */
if (asprintf(&s, "{ \"error\" : { \"code\":%d, \"message\":\"%s\"}}",
err_code, err_msg) < 0) {
applog(LOG_ERR, "OOM");
return false;
}
/* create compressed message packet */
if (!cjson_encode(op, s, &raw_msg, &msg_len))
goto out;
rc = cli_send_msg(cli, raw_msg, msg_len);
out:
free(raw_msg);
free(s);
return rc;
}
static bool cli_msg(struct client *cli)
{
uint32_t op = UBBP_OP(cli->ubbp.op_size);
uint32_t size = UBBP_SIZE(cli->ubbp.op_size);
json_t *obj = NULL;
bool rc = false;
/* LOGIN must always be first msg from client */
if (!cli->logged_in && (op != BC_OP_LOGIN))
return false;
else if (cli->logged_in && (op == BC_OP_LOGIN))
return false;
/* decode JSON messages, for opcodes that require it */
switch (op) {
case BC_OP_LOGIN:
case BC_OP_CONFIG: {
uint32_t cjson_len = size;
/* LOGIN is special; it has a sha256 digest
* following the compressed JSON bytes
*/
if (op == BC_OP_LOGIN) {
if (size <= SHA256_DIGEST_LENGTH)
return false;
cjson_len -= SHA256_DIGEST_LENGTH;
}
obj = cjson_decode(cli->msg, cjson_len);
if (!json_is_object(obj))
goto out;
break;
}
default:
/* do nothing */
break;
}
/* message processing, determined by opcode */
switch (op) {
case BC_OP_NOP:
if (size > 0)
break;
rc = cli_send_hdronly(cli, BC_OP_NOP);
break;
case BC_OP_LOGIN:
rc = cli_op_login(cli, obj, size);
break;
case BC_OP_CONFIG:
rc = cli_op_config(cli, obj);
break;
case BC_OP_WORK_GET:
rc = cli_op_work_get(cli, size);
break;
case BC_OP_WORK_SUBMIT:
rc = cli_op_work_submit(cli, size);
break;
default:
/* invalid op. fall through to function return stmt */
break;
}
out:
json_decref(obj);
return rc;
}
static bool cli_read_msg(void *rst_priv, void *priv,
unsigned int buflen, bool success)
{
struct client *cli = rst_priv;
if (!success)
return false;
return cli_msg(cli);
}
static bool cli_read_hdr(void *rst_priv, void *priv,
unsigned int buflen, bool success)
{
struct client *cli = rst_priv;
uint32_t size;
if (!success)
return false;
if (memcmp(cli->ubbp.magic, PUSHPOOL_UBBP_MAGIC, 4))
return false;
cli->ubbp.op_size = le32toh(cli->ubbp.op_size);
size = UBBP_SIZE(cli->ubbp.op_size);
if (size > CLI_MAX_MSG)
return false;
if (size == 0)
return cli_msg(cli);
cli->msg = malloc(size);
if (!cli->msg)
return false;
return tcp_read(&cli->rst, cli->msg, size, cli_read_msg, NULL);
}
static void tcp_cli_event(int fd, short events, void *userdata)
{
struct client *cli = userdata;
bool ok = true;
if (events & EV_READ)
ok = tcp_read_runq(&cli->rst);
if (events & EV_TIMEOUT)
ok = false;
if (!ok)
cli_free(cli);
}
static void tcp_srv_event(int fd, short events, void *userdata)
{
struct server_socket *sock = userdata;
struct sockaddr_in6 addr;
socklen_t addrlen = sizeof(struct sockaddr_in6);
struct client *cli = NULL;
char host[64];
char port[16];
int cli_fd, on = 1;
struct timeval timeout = { CLI_RD_TIMEOUT, 0 };
/* receive TCP connection from kernel */
cli_fd = accept(sock->fd, (struct sockaddr *) &addr, &addrlen);
if (cli_fd < 0) {
syslogerr("tcp accept");
goto err_out;
}
srv.stats.tcp_accept++;
cli = cli_alloc(cli_fd, &addr, addrlen, true);
if (!cli) {
applog(LOG_ERR, "OOM");
close(cli_fd);
return;
}
/* mark non-blocking, for upcoming poll use */
if (fsetflags("tcp client", cli->fd, O_NONBLOCK) < 0)
goto err_out_fd;
/* disable delay of small output packets */
if (setsockopt(cli->fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
applog(LOG_WARNING, "TCP_NODELAY failed: %s",
strerror(errno));
/* turn on TCP keep-alive */
on = 1;
if (setsockopt(cli->fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0)
applog(LOG_WARNING, "SO_KEEPALIVE failed: %s",
strerror(errno));
event_set(&cli->ev, cli->fd, EV_READ | EV_PERSIST,
tcp_cli_event, cli);
/* pretty-print incoming cxn info */
getnameinfo((struct sockaddr *) &cli->addr, addrlen,
host, sizeof(host), port, sizeof(port),
NI_NUMERICHOST | NI_NUMERICSERV);
host[sizeof(host) - 1] = 0;
port[sizeof(port) - 1] = 0;
applog(LOG_INFO, "client host %s port %s connected%s", host, port,
false ? " via SSL" : "");
strcpy(cli->addr_host, host);
strcpy(cli->addr_port, port);
if (event_add(&cli->ev, &timeout) < 0) {
applog(LOG_ERR, "unable to ready cli fd for polling");
goto err_out_fd;
}
cli->ev_mask = EV_READ;
if (!tcp_read(&cli->rst, &cli->ubbp, sizeof(cli->ubbp),
cli_read_hdr, NULL))
goto err_out_fd;
return;
err_out_fd:
err_out:
cli_free(cli);
}
static bool valid_auth_hdr(const char *hdr, char *username_out)
{
char *t_type = NULL;
char *t_b64 = NULL;
char *t_userpass = NULL, *colon, *user, *pass;
char *pass_db = NULL;
bool rc = false;
size_t hdrlen = strlen(hdr);
size_t bin_len = 0;
void *bin = NULL;
t_type = calloc(1, hdrlen + 1);
t_b64 = calloc(1, hdrlen + 1);
t_userpass = calloc(1, hdrlen + 1);
if (!t_type || !t_b64 || !t_userpass)
goto out;
if (sscanf(hdr, "%s %s", t_type, t_b64) != 2)
goto out;
/* auth type Basic */
if (strcasecmp(t_type, "basic"))
goto out;
/* decode base64 token */
bin = g_base64_decode(t_b64, &bin_len);
if (!bin)
goto out;
if (bin_len > hdrlen) /* impossible */
goto out;
memcpy(t_userpass, bin, bin_len);
/* split user:pass */
colon = strchr(t_userpass, ':');
if (!colon)
goto out;
*colon = 0;
user = t_userpass;
pass = colon + 1;
/* password database authentication check */
pass_db = pwdb_lookup(user);
if (!pass_db || (strcmp(pass, pass_db) && *pass_db != '\0'))
goto out;
rc = true;
strncpy(username_out, user, 64);
username_out[64] = 0;
out:
free(pass_db);
free(bin);
free(t_type);
free(t_b64);
free(t_userpass);
return rc;
}
static void reqlog(const char *rem_host, const char *username,
const char *uri)
{
struct timeval tv = { };
char *f;
ssize_t wrc;
struct tm tm;
if (srv.req_fd < 0)
return;
gettimeofday(&tv, NULL);
gmtime_r(&tv.tv_sec, &tm);
if (asprintf(&f, "[%d-%02d-%02d %02d:%02d:%02d.%llu] %s %s \"%s\"\n",
tm.tm_year + 1900,
tm.tm_mon + 1,
tm.tm_mday,
tm.tm_hour,
tm.tm_min,
tm.tm_sec,
(unsigned long long) tv.tv_usec,
(rem_host && *rem_host) ? rem_host : "-",
(username && *username) ? username : "-",
(uri && *uri) ? uri : "") < 0)
return;
wrc = write(srv.req_fd, f, strlen(f));
if (wrc != strlen(f))
syslogerr(srv.req_log);
free(f);
}
void sharelog(const char *rem_host, const char *username,
const char *our_result, const char *upstream_result,
const char *reason, const char *solution)
{
struct timeval tv = { };
char *f;
ssize_t wrc;
struct tm tm;
if (srv.db_sharelog && srv.db_ops->sharelog != NULL)
srv.db_ops->sharelog(rem_host, username, our_result,
upstream_result, reason, solution);
if (srv.share_fd < 0)
return;
gettimeofday(&tv, NULL);
gmtime_r(&tv.tv_sec, &tm);
if (asprintf(&f, "[%d-%02d-%02d %02d:%02d:%02.6f] %s %s %s %s %s %s\n",
tm.tm_year + 1900,
tm.tm_mon + 1,
tm.tm_mday,
tm.tm_hour,
tm.tm_min,
tm.tm_sec +
tv.tv_usec/1000000.0,
(rem_host && *rem_host) ? rem_host : "-",
(username && *username) ? username : "-",
(our_result && *our_result) ? our_result : "-",
(upstream_result && *upstream_result) ? upstream_result : "-",
(reason && *reason) ? reason : "-",
(solution && *solution) ? solution : "-") < 0)
return;
wrc = write(srv.share_fd, f, strlen(f));
if (wrc != strlen(f))
syslogerr(srv.share_log);
free(f);
}
static void http_handle_req(struct evhttp_request *req, bool longpoll)
{
const char *clen_str, *auth;
char *body_str;
char username[65] = "";
void *body, *reply = NULL;
int clen = 0;
unsigned int reply_len = 0;
json_t *jreq;
json_error_t jerr;
bool rc;
struct evbuffer *evbuf;
auth = evhttp_find_header(req->input_headers, "Authorization");
if (!auth) {
reqlog(req->remote_host, username, req->uri);
evhttp_add_header(req->output_headers, "WWW-Authenticate", "Basic realm=\"pushpool\"");
evhttp_send_reply(req, 401, "not authorized", NULL);
return;
}
if (!valid_auth_hdr(auth, username)) {
reqlog(req->remote_host, username, req->uri);
evhttp_send_reply(req, 403, "access forbidden", NULL);
return;
}
if (!longpoll) {
clen_str = evhttp_find_header(req->input_headers, "Content-Length");
if (clen_str)
clen = atoi(clen_str);
if (clen < 1 || clen > 999999) {
reqlog(req->remote_host, username, req->uri);
evhttp_send_reply(req, HTTP_BADREQUEST, "invalid args", NULL);
return;
}
if (EVBUFFER_LENGTH(req->input_buffer) != clen)
goto err_out_bad_req;
body = EVBUFFER_DATA(req->input_buffer);
body_str = strndup(body, clen);
if (!body_str)
goto err_out_bad_req;
} else /* long polling */
body_str = strdup("{\"method\":\"getwork\",\"params\":[],\"id\":1}");
jreq = JSON_LOADS(body_str, &jerr);
free(body_str);
if (!jreq)
goto err_out_bad_req;
rc = msg_json_rpc(req, jreq, username, &reply, &reply_len);
json_decref(jreq);
if (!rc)
goto err_out_bad_req;
evbuf = evbuffer_new();
if (!evbuf) {
free(reply);
goto err_out_bad_req;
}
if (evbuffer_add(evbuf, reply, reply_len)) {
evbuffer_free(evbuf);
free(reply);
goto err_out_bad_req;
}
free(reply);
evhttp_add_header(req->output_headers,
"Content-Type", "application/json");
if (!longpoll && !srv.disable_lp)
evhttp_add_header(req->output_headers, "X-Long-Polling", "/LP");
if (!srv.disable_roll_ntime)
evhttp_add_header(req->output_headers, "X-Roll-NTime", srv.work_expire_str);
evhttp_send_reply(req, HTTP_OK, "ok", evbuf);
evbuffer_free(evbuf);
return;
err_out_bad_req:
evhttp_send_reply(req, HTTP_BADREQUEST, "invalid args", NULL);
}
static void flush_lp_waiters(void)
{
struct genlist *tmp, *iter;
elist_for_each_entry_safe(tmp, iter, &srv.lp_waiters, node) {
struct evhttp_request *req;
req = tmp->data;
http_handle_req(req, true);
elist_del(&tmp->node);
memset(tmp, 0, sizeof(*tmp));
free(tmp);
}
}
static void __http_srv_event(struct evhttp_request *req, void *arg,
bool longpoll)
{
struct server_socket *sock = arg;
const char *auth;
char username[65] = "";
/* copy X-Forwarded-For header to remote_host, if a trusted proxy provides it */
if (sock->cfg->proxy && !strcmp(req->remote_host, sock->cfg->proxy)) {
const char *hdr;
hdr = evhttp_find_header(req->input_headers, "X-Forwarded-For");
if (hdr) {
free(req->remote_host);
req->remote_host = strdup(hdr);
}
}
/* validate user authorization */
auth = evhttp_find_header(req->input_headers, "Authorization");
if (!auth) {
reqlog(req->remote_host, username, req->uri);
evhttp_add_header(req->output_headers, "WWW-Authenticate", "Basic realm=\"pushpool\"");
evhttp_send_reply(req, 401, "not authorized", NULL);
return;
}
if (!valid_auth_hdr(auth, username)) {
reqlog(req->remote_host, username, req->uri);
evhttp_send_reply(req, 403, "access forbidden", NULL);
return;
}
reqlog(req->remote_host, username, req->uri);
/* if longpoll, don't respond now, queue onto list for later */
if (longpoll) {
struct genlist *gl = calloc(1, sizeof(*gl));
if (!gl)
return;
gl->data = req;
INIT_ELIST_HEAD(&gl->node);
elist_add_tail(&gl->node, &srv.lp_waiters);
}
/* otherwise, handle immediately */
else
http_handle_req(req, false);
}
static void http_srv_event(struct evhttp_request *req, void *arg)
{
__http_srv_event(req, arg, false);
}
static void http_srv_event_lp(struct evhttp_request *req, void *arg)
{
__http_srv_event(req, arg, true);
}
static int net_write_port(const char *port_file, const char *port_str)
{
FILE *portf;
int rc;
portf = fopen(port_file, "w");
if (portf == NULL) {
rc = errno;
applog(LOG_INFO, "Cannot create port file %s: %s",
port_file, strerror(rc));
return -rc;
}
fprintf(portf, "%s\n", port_str);
fclose(portf);
return 0;
}
static void net_sock_free(struct server_socket *sock)
{
if (!sock)
return;
elist_del_init(&sock->sockets_node);
if (sock->http)
evhttp_free(sock->http);
else
event_del(&sock->ev);
if (sock->fd >= 0)
close(sock->fd);
memset(sock, 0, sizeof(*sock)); /* poison */
free(sock);
}
static void net_close(void)
{
struct server_socket *sock, *iter;
struct listen_cfg *cfg, *citer;
elist_for_each_entry_safe(sock, iter, &srv.sockets, sockets_node) {
net_sock_free(sock);
}
elist_for_each_entry_safe(cfg, citer, &srv.listeners, listeners_node) {
elist_del_init(&cfg->listeners_node);
free(cfg->host);
free(cfg->port_file);
memset(cfg, 0, sizeof(*cfg)); /* poison */
free(cfg);
}
}
static int net_open_socket(const struct listen_cfg *cfg,
int addr_fam, int sock_type, int sock_prot,
int addr_len, void *addr_ptr)
{
struct server_socket *sock;
int fd, on;
int rc;
bool have_http = (cfg->proto == LP_HTTP_JSON);
fd = socket(addr_fam, sock_type, sock_prot);
if (fd < 0) {
rc = errno;
syslogerr("tcp socket");
return -rc;
}
on = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
syslogerr("setsockopt(SO_REUSEADDR)");
rc = -errno;
goto err_out_fd;
}
if (bind(fd, addr_ptr, addr_len) < 0) {
syslogerr("tcp bind");
rc = -errno;
goto err_out_fd;
}
if (listen(fd, 100) < 0) {
syslogerr("tcp listen");
rc = -errno;
goto err_out_fd;
}
rc = fsetflags("tcp server", fd, O_NONBLOCK);
if (rc)
goto err_out_fd;
sock = calloc(1, sizeof(*sock));
if (!sock) {
rc = -ENOMEM;
goto err_out_fd;
}
INIT_ELIST_HEAD(&sock->sockets_node);
sock->fd = fd;
sock->cfg = cfg;
if (have_http) {
sock->http = evhttp_new(srv.evbase_main);
if (!sock->http)
goto err_out_sock;
if (evhttp_accept_socket(sock->http, fd) < 0) {
evhttp_free(sock->http);
goto err_out_sock;
}
evhttp_set_cb(sock->http, "/",
http_srv_event, sock);
if (!srv.disable_lp)
evhttp_set_cb(sock->http, "/LP",
http_srv_event_lp,sock);
} else {
event_set(&sock->ev, fd, EV_READ | EV_PERSIST,
tcp_srv_event, sock);
if (event_add(&sock->ev, NULL) < 0)
goto err_out_sock;
}
elist_add_tail(&sock->sockets_node, &srv.sockets);
return fd;