forked from pantavisor/pantavisor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctrl.c
1502 lines (1304 loc) · 38.4 KB
/
ctrl.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 (c) 2017-2023 Pantacor Ltd.
*
* 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <stdint.h>
#include <errno.h>
#include <picohttpparser.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <linux/limits.h>
#include <jsmn/jsmnutil.h>
#include "ctrl.h"
#include "utils/math.h"
#include "utils/str.h"
#include "json.h"
#include "pvlogger.h"
#include "state.h"
#include "init.h"
#include "objects.h"
#include "storage.h"
#include "metadata.h"
#include "version.h"
#include "platforms.h"
#include "updater.h"
#include "drivers.h"
#include "paths.h"
#include "utils/math.h"
#include "utils/fs.h"
#include "utils/socket.h"
#define MODULE_NAME "ctrl"
#define pv_log(level, msg, ...) vlog(MODULE_NAME, level, msg, ##__VA_ARGS__)
#include "log.h"
#define ENDPOINT_CONTAINERS "/containers"
#define ENDPOINT_GROUPS "/groups"
#define ENDPOINT_SIGNAL "/signal"
#define ENDPOINT_COMMANDS "/commands"
#define ENDPOINT_OBJECTS "/objects"
#define ENDPOINT_STEPS "/steps"
#define ENDPOINT_PROGRESS "/progress"
#define ENDPOINT_COMMITMSG "/commitmsg"
#define ENDPOINT_USER_META "/user-meta"
#define ENDPOINT_DEVICE_META "/device-meta"
#define ENDPOINT_BUILDINFO "/buildinfo"
#define ENDPOINT_CONFIG "/config"
#define ENDPOINT_CONFIG2 "/config2"
#define ENDPOINT_DRIVERS "/drivers"
#define HTTP_RES_OK "HTTP/1.1 200 OK\r\n\r\n"
#define HTTP_RES_CONT "HTTP/1.1 100 Continue\r\n\r\n"
#define HTTP_RESPONSE \
"HTTP/1.1 %s \r\nContent-Length: %zd\r\nContent-Type: application/json; charset=utf-8\r\n\r\n{\"Error\":\"%s\"}"
#define UNSUPPORTED_LOG_COMMAND_FMT \
"ERROR: unsupported legacy 'log command' command; use new REST API instead\n"
static const size_t HTTP_REQ_BUFFER_SIZE = 4096;
static const unsigned int HTTP_REQ_NUM_HEADERS = 8;
static const unsigned int HTTP_ERROR_RESPONSE_MSG_SIZE = 256;
typedef enum {
HTTP_STATUS_BAD_REQ,
HTTP_STATUS_FORBIDDEN,
HTTP_STATUS_NOT_FOUND,
HTTP_STATUS_CONFLICT,
HTTP_STATUS_UNPROCESSABLE_ENTITY,
HTTP_STATUS_ERROR,
HTTP_STATUS_INSUFF_STORAGE,
} pv_http_status_code_t;
static const char *pv_ctrl_string_http_status_code(pv_http_status_code_t code)
{
static const char *strings[] = { "400 Bad Request",
"403 Forbidden",
"404 Not Found",
"409 Conflict",
"422 Unprocessable Entity",
"500 Internal Server Error",
"507 Insufficient Storage" };
return strings[code];
}
static int pv_ctrl_socket_open()
{
int fd;
struct sockaddr_un addr;
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
pv_log(ERROR, "ctrl socket open error: %s", strerror(errno));
goto out;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
pv_paths_pv_file(addr.sun_path, sizeof(addr.sun_path) - 1,
PVCTRL_FNAME);
// sometimes, the socket file still exists after reboot
unlink(addr.sun_path);
if (bind(fd, (const struct sockaddr *)&addr, sizeof(addr)) < 0) {
pv_log(ERROR, "ctrl socket with fd %d open error: %s", fd,
strerror(errno));
close(fd);
fd = -1;
goto out;
}
// queue 15 commands
if (listen(fd, 15)) {
pv_log(ERROR, "ctrl socket with fd %d listen error: %s", fd,
strerror(errno));
close(fd);
fd = -1;
}
out:
return fd;
}
void pv_ctrl_socket_close(int ctrl_fd)
{
char path[PATH_MAX];
if (ctrl_fd >= 0) {
pv_paths_pv_file(path, PATH_MAX, PVCTRL_FNAME);
pv_log(DEBUG, "closing %s with fd %d", path, ctrl_fd);
close(ctrl_fd);
unlink(path);
}
}
static void pv_ctrl_parse_signal(char *buf, char **signal, char **payload)
{
int tokc;
jsmntok_t *tokv;
jsmnutil_parse_json(buf, &tokv, &tokc);
*signal = pv_json_get_value(buf, "type", tokv, tokc);
if (!signal) {
pv_log(WARN, "unable to get type value from signal");
}
*payload = pv_json_get_value(buf, "payload", tokv, tokc);
if (!payload) {
pv_log(WARN, "unable to get payload value from signal");
}
if (tokv)
free(tokv);
}
static int pv_ctrl_wait_recv(int req_fd, char *buf, size_t length)
{
int res;
fd_set fdset;
struct timeval tv;
FD_ZERO(&fdset);
FD_SET(req_fd, &fdset);
// for now, we wait for 1s max
tv.tv_sec = 1;
tv.tv_usec = 0;
res = select(req_fd + 1, &fdset, 0, 0, &tv);
if (res == 0) {
pv_log(WARN, "request timed out");
goto out;
} else if (res < 0) {
pv_log(WARN, "could not select socket with fd %d: %s", req_fd,
strerror(errno));
goto out;
}
res = recv(req_fd, buf, length, MSG_DONTWAIT);
out:
return res;
}
static void pv_ctrl_consume_req(int req_fd, size_t content_length)
{
char buf[HTTP_REQ_BUFFER_SIZE];
size_t total_rec = 0;
ssize_t rec;
while ((rec = read(req_fd, buf, content_length - total_rec)) > 0) {
if (rec < 0)
return;
total_rec += rec;
}
}
static int pv_ctrl_process_signal(int req_fd, size_t content_length,
char **signal, char **payload)
{
int res;
char req[HTTP_REQ_BUFFER_SIZE];
memset(req, 0, sizeof(req));
pv_log(DEBUG, "reading and parsing signal...");
if (content_length >= HTTP_REQ_BUFFER_SIZE) {
pv_ctrl_consume_req(req_fd, content_length);
pv_log(WARN, "signal request too long");
goto err;
}
// read request
res = pv_ctrl_wait_recv(req_fd, req, content_length);
if (res == 0) {
pv_log(WARN, "nothing to read from signal request");
goto err;
} else if (res < 0) {
pv_log(WARN,
"signal request could not be read from ctrl socket with fd %d: %s",
req_fd, strerror(errno));
goto err;
}
req[content_length] = 0;
pv_ctrl_parse_signal(req, signal, payload);
if (!signal || !payload)
goto err;
return 0;
err:
return -1;
}
static struct pv_cmd *pv_ctrl_parse_command(char *buf)
{
int tokc;
jsmntok_t *tokv = NULL;
char *op_string = NULL;
struct pv_cmd *cmd = NULL;
cmd = calloc(1, sizeof(struct pv_cmd));
if (!cmd) {
pv_log(ERROR, "cmd could not be allocated");
goto out;
}
jsmnutil_parse_json(buf, &tokv, &tokc);
op_string = pv_json_get_value(buf, "op", tokv, tokc);
if (!op_string) {
pv_log(WARN, "unable to get op value from command");
goto err;
}
cmd->op = pv_ctrl_int_cmd_operation(op_string, strlen(op_string));
if (!cmd->op) {
pv_log(WARN, "op from command unknown");
goto err;
}
cmd->payload = pv_json_get_value(buf, "payload", tokv, tokc);
if (!cmd->payload) {
pv_log(WARN, "unable to get payload value from command");
goto err;
}
goto out;
err:
pv_ctrl_free_cmd(cmd);
cmd = NULL;
out:
if (tokv)
free(tokv);
if (op_string)
free(op_string);
return cmd;
}
static int pv_ctrl_process_cmd(int req_fd, size_t content_length,
struct pv_cmd **cmd)
{
int res;
char req[HTTP_REQ_BUFFER_SIZE];
memset(req, 0, sizeof(req));
pv_log(DEBUG, "reading and parsing command...");
if (content_length >= HTTP_REQ_BUFFER_SIZE) {
pv_ctrl_consume_req(req_fd, content_length);
pv_log(WARN, "cmd request too long");
goto err;
}
// read request
res = pv_ctrl_wait_recv(req_fd, req, content_length);
if (res == 0) {
pv_log(WARN, "nothing to read from cmd request");
goto err;
} else if (res < 0) {
pv_log(WARN,
"cmd request could not be read from ctrl socket with fd %d: %s",
req_fd, strerror(errno));
goto err;
}
req[content_length] = 0;
*cmd = pv_ctrl_parse_command(req);
if (!*cmd)
goto err;
return 0;
err:
return -1;
}
static int pv_ctrl_send_all(int req_fd, const char *message, size_t size)
{
size_t total_sent = 0;
ssize_t size_sent;
while (total_sent < size) {
size_sent = send(req_fd, message + total_sent,
size - total_sent, MSG_NOSIGNAL);
if (size_sent < 0)
return size_sent;
total_sent += size_sent;
}
return total_sent;
}
static void pv_ctrl_write_cont_response(int req_fd)
{
int res;
res = pv_ctrl_send_all(req_fd, HTTP_RES_CONT, strlen(HTTP_RES_CONT));
if (res < 0) {
pv_log(WARN,
"HTTP CONTINUE response could not be sent to ctrl socket with fd %d: %s",
req_fd, strerror(errno));
} else if (res != strlen(HTTP_RES_CONT)) {
pv_log(WARN, "HTTP CONTINUE response was not sent");
}
}
static void pv_ctrl_write_ok_response(int req_fd)
{
int res;
res = pv_ctrl_send_all(req_fd, HTTP_RES_OK, strlen(HTTP_RES_OK));
if (res < 0) {
pv_log(WARN,
"HTTP OK response could not be written to ctrl socket with fd %d: %s",
req_fd, strerror(errno));
} else if (res != strlen(HTTP_RES_OK)) {
pv_log(WARN, "HTTP OK response was not sent");
}
}
static void pv_ctrl_write_error_response(int req_fd, pv_http_status_code_t code,
const char *message)
{
ssize_t res;
size_t content_len, response_len;
char *response = NULL;
content_len = 15 + // {\"Error\":\"%s\"}\0
strlen(message);
response_len = 93 + // HTTP/1.1...
strlen(pv_ctrl_string_http_status_code(code)) +
get_digit_count(content_len) + strlen(message);
if (response_len > HTTP_REQ_BUFFER_SIZE) {
pv_log(ERROR, "HTTP response too long");
goto out;
}
response = calloc(response_len + 1, sizeof(char));
if (!response) {
pv_log(ERROR, "HTTP response cannot be allocated");
goto out;
}
SNPRINTF_WTRUNC(response, response_len, HTTP_RESPONSE,
pv_ctrl_string_http_status_code(code), content_len,
message);
res = pv_ctrl_send_all(req_fd, response, response_len);
if (res < 0) {
pv_log(WARN,
"HTTP response could not be written to ctrl socket with fd %d: %s",
req_fd, strerror(errno));
} else if ((size_t)res != response_len) {
pv_log(WARN, "HTTP error response was not sent");
}
out:
if (response)
free(response);
}
static int pv_ctrl_process_put_file(int req_fd, size_t content_length,
bool expect_continue, char *file_path)
{
int obj_fd = -1, read_length, write_length, ret = -1;
char req[HTTP_REQ_BUFFER_SIZE];
size_t free_space = (size_t)pv_storage_get_free();
if (content_length > free_space) {
pv_ctrl_consume_req(req_fd, content_length);
pv_log(WARN,
"%zu B needed but only %zu B available. Cannot create file",
content_length, free_space);
pv_ctrl_write_error_response(req_fd, HTTP_STATUS_INSUFF_STORAGE,
"Not enough disk space available");
return ret;
}
pv_log(DEBUG,
"reading file with size %zu from endpoint and putting it in %s",
content_length, file_path);
if (expect_continue)
pv_ctrl_write_cont_response(req_fd);
obj_fd = open(file_path, O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (obj_fd < 0) {
pv_ctrl_consume_req(req_fd, content_length);
pv_log(ERROR, "'%s' could not be created: %s",
file_path, strerror(errno));
pv_ctrl_write_error_response(req_fd, HTTP_STATUS_ERROR,
"Cannot create file");
goto clean;
}
memset(req, 0, sizeof(req));
// read and save
while (content_length > 0) {
if (content_length > HTTP_REQ_BUFFER_SIZE)
read_length = HTTP_REQ_BUFFER_SIZE;
else
read_length = content_length;
write_length = pv_ctrl_wait_recv(req_fd, req, read_length);
if (write_length <= 0) {
pv_log(WARN,
"HTTP PUT content could not be read from ctrl socket with fd %d: %s",
req_fd, strerror(errno));
pv_ctrl_write_error_response(req_fd, HTTP_STATUS_ERROR,
"Cannot read from socket");
goto clean;
}
if (write(obj_fd, req, write_length) <= 0) {
pv_log(WARN,
"HTTP PUT content could not be written from ctrl socket with fd %d: %s",
req_fd, strerror(errno));
pv_ctrl_write_error_response(req_fd, HTTP_STATUS_ERROR,
"Cannot write into file");
goto clean;
}
content_length -= write_length;
}
ret = 0;
goto out;
clean:
pv_log(DEBUG, "removing '%s'...", file_path);
pv_fs_path_remove(file_path, false);
out:
if (obj_fd >= 0) {
fsync(obj_fd);
close(obj_fd);
}
pv_fs_path_sync(file_path);
return ret;
}
static int pv_ctrl_read_parse_request_header(
int req_fd, char *buf, unsigned int buf_index, const char **method,
size_t *method_len, const char **path, size_t *path_len,
struct phr_header *headers, size_t *num_headers)
{
int minor_version, res = -1;
// read from socket until end of HTTP header
do {
res = pv_ctrl_wait_recv(req_fd, &buf[buf_index], 1);
if (res < 0) {
pv_log(ERROR,
"HTTP request header could not read from fd %d: %s",
req_fd, strerror(errno));
goto out;
}
if ((buf_index > 3) && (buf[buf_index - 3] == '\r') &&
(buf[buf_index - 2] == '\n') &&
(buf[buf_index - 1] == '\r') && (buf[buf_index] == '\n')) {
break;
}
buf_index++;
if (buf_index >= HTTP_REQ_BUFFER_SIZE) {
pv_log(WARN, "HTTP request received longer than %d",
HTTP_REQ_BUFFER_SIZE);
goto out;
}
} while (res == 1);
// parse HTTP header
res = phr_parse_request(buf, buf_index + 1, method, method_len, path,
path_len, &minor_version, headers, num_headers,
0);
if (res < 0) {
pv_log(WARN, "HTTP request received has bad format");
}
out:
return res;
}
static size_t pv_ctrl_get_value_header_int(struct phr_header *headers,
size_t num_headers, const char *name)
{
size_t ret = 0;
char *value = NULL;
for (size_t header_index = 0; header_index < num_headers;
header_index++) {
if (pv_str_matches_case(headers[header_index].name,
headers[header_index].name_len, name,
strlen(name))) {
value = calloc(headers[header_index].value_len,
sizeof(char));
strncpy(value, headers[header_index].value,
headers[header_index].value_len);
ret = strtoul(value, NULL, 10);
free(value);
}
}
return ret;
}
static bool pv_ctrl_check_header_value(struct phr_header *headers,
size_t num_headers, const char *header,
const char *value)
{
bool ret = false;
for (size_t header_index = 0; header_index < num_headers;
header_index++) {
if (pv_str_matches_case(headers[header_index].name,
headers[header_index].name_len, header,
strlen(header)) &&
pv_str_matches_case(headers[header_index].value,
headers[header_index].value_len, value,
strlen(value)))
ret = true;
}
return ret;
}
static void pv_ctrl_process_get_file(int req_fd, char *file_path)
{
int obj_fd = -1;
ssize_t sent, file_size = pv_fs_path_get_size(file_path);
off_t offset = 0;
pv_log(DEBUG, "reading file from %s and sending it to endpoint",
file_path);
obj_fd = open(file_path, O_RDONLY);
if (obj_fd < 0) {
pv_log(ERROR, "%s could not be opened for read", file_path);
goto error;
}
pv_ctrl_write_ok_response(req_fd);
// read and send
for (size_t to_send = file_size; to_send > 0;) {
sent = sendfile(req_fd, obj_fd, &offset, to_send);
if (sent < 0)
pv_log(WARN,
"HTTP GET file could not be written to ctrl socket with fd %d: %s",
req_fd, strerror(errno));
if (sent <= 0)
goto out;
to_send -= sent;
}
goto out;
error:
pv_ctrl_write_error_response(req_fd, HTTP_STATUS_NOT_FOUND,
"Resource does not exist");
out:
if (obj_fd >= 0)
close(obj_fd);
}
static char *pv_ctrl_get_file_name(const char *path, size_t buf_index,
size_t path_len)
{
int len;
char *file_name;
if (buf_index > path_len)
return NULL;
len = path_len - buf_index;
file_name = calloc(len + 1, sizeof(char));
if (!file_name)
return NULL;
strncpy(file_name, &path[buf_index], len);
return file_name;
}
static void pv_ctrl_process_get_string(int req_fd, char *buf)
{
int res, buf_len;
pv_log(DEBUG,
"converting data to string and sending it to endpoint...");
buf_len = strlen(buf);
pv_ctrl_write_ok_response(req_fd);
res = pv_ctrl_send_all(req_fd, buf, buf_len);
if (res < 0) {
pv_log(WARN,
"HTTP GET content could not be written to ctrl socket with fd %d: %s",
req_fd, strerror(errno));
} else if (res != buf_len) {
pv_log(WARN, "HTTP GET response was not sent");
}
if (buf)
free(buf);
}
static char *pv_ctrl_get_body(int req_fd, size_t content_length)
{
int res;
char *req = NULL;
if (content_length >= HTTP_REQ_BUFFER_SIZE) {
pv_ctrl_consume_req(req_fd, content_length);
pv_log(WARN, "body too long");
goto err;
}
req = calloc(content_length + 1, sizeof(char));
// read request
res = pv_ctrl_wait_recv(req_fd, req, content_length);
if (res == 0) {
pv_log(WARN, "nothing to read from HTTP GET body");
goto err;
} else if (res < 0) {
pv_log(WARN,
"HTTP GET body could not be read to ctrl socket with fd %d: %s",
req_fd, strerror(errno));
goto err;
}
req[content_length] = 0;
return req;
err:
if (req)
free(req);
return NULL;
}
static int pv_ctrl_check_command(int req_fd, struct pv_cmd **cmd)
{
if (!cmd || !(*cmd))
return -1;
struct pantavisor *pv = pv_get_instance();
if (!pv->remote_mode && ((*cmd)->op == CMD_UPDATE_METADATA)) {
pv_ctrl_write_error_response(
req_fd, HTTP_STATUS_CONFLICT,
"Cannot do this operation while on local mode");
goto error;
}
if (pv->update && pv->update->status != UPDATE_APPLIED &&
(((*cmd)->op == CMD_REBOOT_DEVICE) ||
((*cmd)->op == CMD_POWEROFF_DEVICE) ||
((*cmd)->op == CMD_LOCAL_RUN) || ((*cmd)->op == CMD_LOCAL_APPLY) ||
((*cmd)->op == CMD_MAKE_FACTORY))) {
pv_ctrl_write_error_response(
req_fd, HTTP_STATUS_CONFLICT,
"Cannot do this operation while update is ongoing");
goto error;
}
if (!pv->unclaimed && ((*cmd)->op == CMD_MAKE_FACTORY)) {
pv_ctrl_write_error_response(
req_fd, HTTP_STATUS_CONFLICT,
"Cannot do this operation if device is already claimed");
goto error;
}
if (!pv_config_get_bool(PV_CONTROL_REMOTE) &&
((*cmd)->op == CMD_GO_REMOTE)) {
pv_ctrl_write_error_response(
req_fd, HTTP_STATUS_CONFLICT,
"Cannot do this operation when remote mode is disabled by config");
goto error;
}
if (pv->remote_mode && ((*cmd)->op == CMD_GO_REMOTE)) {
pv_ctrl_write_error_response(req_fd, HTTP_STATUS_CONFLICT,
"Already in remote mode");
goto error;
}
return 0;
error:
pv_ctrl_free_cmd(*cmd);
*cmd = NULL;
return -1;
}
static char *pv_ctrl_get_sender_pname(int req_fd)
{
pid_t sender_pid;
sender_pid = pv_socket_get_sender_pid(req_fd);
if (sender_pid < 0) {
pv_log(WARN, "could not get pid from sender: %s",
strerror(errno));
return NULL;
}
return pv_cgroup_get_process_name(sender_pid);
}
static struct pv_platform *pv_ctrl_get_sender_plat(const char *pname)
{
struct pantavisor *pv = pv_get_instance();
struct pv_platform *plat;
plat = pv_state_fetch_platform(pv->state, pname);
if (!plat)
pv_log(WARN, "could not find platform %s in current state",
pname);
return plat;
}
static bool pv_ctrl_check_sender_privileged(const char *pname)
{
// if this is from root context we are privileged
if (!strcmp(pname, "_pv_"))
return true;
struct pv_platform *plat = pv_ctrl_get_sender_plat(pname);
return plat ? pv_platform_has_role(plat, PLAT_ROLE_MGMT) : false;
}
static struct pv_cmd *
pv_ctrl_process_endpoint_and_reply(int req_fd, const char *method,
size_t method_len, const char *path,
size_t path_len, size_t content_length,
bool expect_continue, char *pname)
{
bool mgmt;
struct pv_cmd *cmd = NULL;
struct pantavisor *pv = pv_get_instance();
char *file_name = NULL;
char file_path_parent[PATH_MAX] = { 0 }, file_path[PATH_MAX] = { 0 },
file_path_tmp[PATH_MAX] = { 0 };
char *signal = NULL, *payload = NULL;
char *metakey = NULL, *metavalue = NULL;
char *driverkey = NULL, *drivervalue = NULL;
char *drivername = NULL;
char *driverop = NULL;
char msg[HTTP_ERROR_RESPONSE_MSG_SIZE];
struct pv_platform *p = pv_ctrl_get_sender_plat(pname);
struct stat st;
mgmt = pv_ctrl_check_sender_privileged(pname);
if (pv_str_matches(ENDPOINT_CONTAINERS, strlen(ENDPOINT_CONTAINERS),
path, path_len)) {
if (!strncmp("GET", method, method_len)) {
if (!mgmt)
goto err_pr;
pv_ctrl_process_get_string(
req_fd,
pv_state_get_containers_json(pv->state));
} else
goto err_me;
} else if (pv_str_matches(ENDPOINT_GROUPS, strlen(ENDPOINT_GROUPS),
path, path_len)) {
if (!strncmp("GET", method, method_len)) {
if (!mgmt)
goto err_pr;
pv_ctrl_process_get_string(
req_fd, pv_state_get_groups_json(pv->state));
} else
goto err_me;
} else if (pv_str_matches(ENDPOINT_SIGNAL, strlen(ENDPOINT_SIGNAL),
path, path_len)) {
if (!strncmp("POST", method, method_len)) {
if (pv_ctrl_process_signal(req_fd, content_length,
&signal, &payload)) {
pv_ctrl_write_error_response(
req_fd, HTTP_STATUS_BAD_REQ,
"Signal has bad format");
goto out;
}
if (pv_state_interpret_signal(pv->state, pname, signal,
payload)) {
pv_ctrl_write_error_response(
req_fd, HTTP_STATUS_ERROR,
"Signal not expected from this platform");
goto out;
}
pv_ctrl_write_ok_response(req_fd);
} else
goto err_me;
} else if (pv_str_startswith(ENDPOINT_COMMANDS,
strlen(ENDPOINT_COMMANDS), path)) {
if (!strncmp("POST", method, method_len)) {
if (!mgmt)
goto err_pr;
if (pv_ctrl_process_cmd(req_fd, content_length, &cmd) <
0) {
pv_ctrl_write_error_response(
req_fd, HTTP_STATUS_BAD_REQ,
"Command has bad format");
goto out;
}
if (pv_ctrl_check_command(req_fd, &cmd) < 0)
goto out;
pv_ctrl_write_ok_response(req_fd);
} else
goto err_me;
} else if (pv_str_matches(ENDPOINT_OBJECTS, strlen(ENDPOINT_OBJECTS),
path, path_len)) {
if (!strncmp("GET", method, method_len)) {
if (!mgmt)
goto err_pr;
pv_ctrl_process_get_string(
req_fd, pv_objects_get_list_string());
} else
goto err_me;
} else if (pv_str_startswith(ENDPOINT_OBJECTS, strlen(ENDPOINT_OBJECTS),
path)) {
file_name = pv_ctrl_get_file_name(
path, sizeof(ENDPOINT_OBJECTS), path_len);
pv_paths_storage_object(file_path, PATH_MAX, file_name);
pv_paths_tmp(file_path_tmp, PATH_MAX, file_path);
// sha must have 64 characters
if (!file_name || (strlen(file_name) != 64)) {
pv_log(WARN, "HTTP request has bad object name %s",
file_name);
pv_ctrl_write_error_response(
req_fd, HTTP_STATUS_BAD_REQ,
"Request has bad object name");
goto out;
}
if (!strncmp("PUT", method, method_len)) {
if (!mgmt) {
pv_ctrl_consume_req(req_fd, content_length);
goto err_pr;
}
if (pv_ctrl_process_put_file(req_fd, content_length,
expect_continue,
file_path_tmp) < 0)
goto out;
if (pv_fs_path_exist(file_path) &&
!pv_storage_validate_file_checksum(file_path,
file_name)) {
pv_log(WARN,
"object %s already exists and is valid; discarding new object upload",
file_path_tmp);
pv_fs_path_remove(file_path_tmp, false);
} else if (pv_storage_validate_file_checksum(
file_path_tmp, file_name) < 0) {
pv_log(WARN, "object %s has bad checksum",
file_path_tmp);
pv_ctrl_write_error_response(
req_fd,
HTTP_STATUS_UNPROCESSABLE_ENTITY,
"Object has bad checksum");
goto out;
} else {
pv_log(DEBUG, "renaming %s to %s",
file_path_tmp, file_path);
if (pv_fs_path_rename(file_path_tmp,
file_path) < 0) {
pv_log(ERROR, "could not rename: %s",
strerror(errno));
pv_ctrl_write_error_response(
req_fd, HTTP_STATUS_ERROR,
"Cannot rename object");
goto out;
}
}
pv_storage_gc_defer_run_threshold();
pv_ctrl_write_ok_response(req_fd);
} else if (!strncmp("GET", method, method_len)) {
if (!mgmt)
goto err_pr;
pv_ctrl_process_get_file(req_fd, file_path);
} else
goto err_me;
} else if (pv_str_matches(ENDPOINT_STEPS, strlen(ENDPOINT_STEPS), path,
path_len)) {
if (!strncmp("GET", method, method_len)) {
if (!mgmt)
goto err_pr;
pv_ctrl_process_get_string(
req_fd, pv_storage_get_revisions_string());
} else
goto err_me;
} else if (pv_str_startswith(ENDPOINT_STEPS, strlen(ENDPOINT_STEPS),
path) &&
pv_str_endswith(ENDPOINT_PROGRESS, strlen(ENDPOINT_PROGRESS),
path, path_len)) {
file_name = pv_ctrl_get_file_name(
path, sizeof(ENDPOINT_STEPS),
path_len - strlen(ENDPOINT_PROGRESS));
pv_paths_storage_trail_pv_file(file_path, PATH_MAX, file_name,
PROGRESS_FNAME);
if (!file_name) {
pv_log(WARN, "HTTP request has bad step name %s",
file_name);
pv_ctrl_write_error_response(
req_fd, HTTP_STATUS_BAD_REQ,
"Request has bad step name");