Blame


1 3efd8e31 2022-10-23 thomas /*
2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 3efd8e31 2022-10-23 thomas *
4 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
5 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
6 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
7 3efd8e31 2022-10-23 thomas *
8 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 3efd8e31 2022-10-23 thomas */
16 3efd8e31 2022-10-23 thomas
17 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
18 3efd8e31 2022-10-23 thomas #include <sys/socket.h>
19 3efd8e31 2022-10-23 thomas #include <sys/un.h>
20 3efd8e31 2022-10-23 thomas
21 3efd8e31 2022-10-23 thomas #include <err.h>
22 3efd8e31 2022-10-23 thomas #include <event.h>
23 3efd8e31 2022-10-23 thomas #include <imsg.h>
24 3efd8e31 2022-10-23 thomas #include <limits.h>
25 3efd8e31 2022-10-23 thomas #include <stdio.h>
26 3efd8e31 2022-10-23 thomas #include <stdlib.h>
27 3efd8e31 2022-10-23 thomas #include <string.h>
28 3efd8e31 2022-10-23 thomas #include <unistd.h>
29 3efd8e31 2022-10-23 thomas
30 3efd8e31 2022-10-23 thomas #include "got_error.h"
31 3efd8e31 2022-10-23 thomas #include "got_serve.h"
32 3efd8e31 2022-10-23 thomas
33 3efd8e31 2022-10-23 thomas #include "gotd.h"
34 3efd8e31 2022-10-23 thomas
35 3efd8e31 2022-10-23 thomas static int chattygot;
36 3efd8e31 2022-10-23 thomas
37 3efd8e31 2022-10-23 thomas __dead static void
38 96d694ac 2023-02-17 thomas usage(void)
39 3efd8e31 2022-10-23 thomas {
40 3efd8e31 2022-10-23 thomas fprintf(stderr, "usage: %s -c '%s|%s repository-path'\n",
41 3efd8e31 2022-10-23 thomas getprogname(), GOT_SERVE_CMD_SEND, GOT_SERVE_CMD_FETCH);
42 3efd8e31 2022-10-23 thomas exit(1);
43 3efd8e31 2022-10-23 thomas }
44 3efd8e31 2022-10-23 thomas
45 3efd8e31 2022-10-23 thomas static const struct got_error *
46 3efd8e31 2022-10-23 thomas apply_unveil(const char *unix_socket_path)
47 3efd8e31 2022-10-23 thomas {
48 3efd8e31 2022-10-23 thomas #ifdef PROFILE
49 3efd8e31 2022-10-23 thomas if (unveil("gmon.out", "rwc") != 0)
50 3efd8e31 2022-10-23 thomas return got_error_from_errno2("unveil", "gmon.out");
51 3efd8e31 2022-10-23 thomas #endif
52 3efd8e31 2022-10-23 thomas if (unveil(unix_socket_path, "w") != 0)
53 3efd8e31 2022-10-23 thomas return got_error_from_errno2("unveil", unix_socket_path);
54 3efd8e31 2022-10-23 thomas
55 3efd8e31 2022-10-23 thomas if (unveil(NULL, NULL) != 0)
56 3efd8e31 2022-10-23 thomas return got_error_from_errno("unveil");
57 3efd8e31 2022-10-23 thomas
58 3efd8e31 2022-10-23 thomas return NULL;
59 3efd8e31 2022-10-23 thomas }
60 3efd8e31 2022-10-23 thomas
61 3efd8e31 2022-10-23 thomas int
62 3efd8e31 2022-10-23 thomas main(int argc, char *argv[])
63 3efd8e31 2022-10-23 thomas {
64 3efd8e31 2022-10-23 thomas const struct got_error *error;
65 c8fcdde8 2023-01-23 thomas const char *unix_socket_path;
66 3efd8e31 2022-10-23 thomas int gotd_sock = -1;
67 3efd8e31 2022-10-23 thomas struct sockaddr_un sun;
68 214d733a 2023-01-23 thomas char *gitcmd = NULL, *command = NULL, *repo_path = NULL;
69 3efd8e31 2022-10-23 thomas
70 3efd8e31 2022-10-23 thomas #ifndef PROFILE
71 3efd8e31 2022-10-23 thomas if (pledge("stdio recvfd unix unveil", NULL) == -1)
72 3efd8e31 2022-10-23 thomas err(1, "pledge");
73 3efd8e31 2022-10-23 thomas #endif
74 6e58d3c9 2023-01-23 thomas
75 6e58d3c9 2023-01-23 thomas unix_socket_path = getenv("GOTD_UNIX_SOCKET");
76 6e58d3c9 2023-01-23 thomas if (unix_socket_path == NULL)
77 6e58d3c9 2023-01-23 thomas unix_socket_path = GOTD_UNIX_SOCKET;
78 6e58d3c9 2023-01-23 thomas
79 6e58d3c9 2023-01-23 thomas error = apply_unveil(unix_socket_path);
80 6e58d3c9 2023-01-23 thomas if (error)
81 6e58d3c9 2023-01-23 thomas goto done;
82 6e58d3c9 2023-01-23 thomas
83 65ca77c9 2022-10-23 thomas if (strcmp(argv[0], GOT_SERVE_CMD_SEND) == 0 ||
84 65ca77c9 2022-10-23 thomas strcmp(argv[0], GOT_SERVE_CMD_FETCH) == 0) {
85 65ca77c9 2022-10-23 thomas if (argc != 2)
86 65ca77c9 2022-10-23 thomas usage();
87 65ca77c9 2022-10-23 thomas if (asprintf(&gitcmd, "%s %s", argv[0], argv[1]) == -1)
88 65ca77c9 2022-10-23 thomas err(1, "asprintf");
89 214d733a 2023-01-23 thomas error = got_serve_parse_command(&command, &repo_path, gitcmd);
90 65ca77c9 2022-10-23 thomas } else {
91 214d733a 2023-01-23 thomas if (argc != 3 || strcmp(argv[1], "-c") != 0)
92 65ca77c9 2022-10-23 thomas usage();
93 214d733a 2023-01-23 thomas error = got_serve_parse_command(&command, &repo_path, argv[2]);
94 65ca77c9 2022-10-23 thomas }
95 214d733a 2023-01-23 thomas if (error && error->code == GOT_ERR_BAD_PACKET)
96 214d733a 2023-01-23 thomas usage();
97 214d733a 2023-01-23 thomas if (error)
98 214d733a 2023-01-23 thomas goto done;
99 3efd8e31 2022-10-23 thomas
100 3efd8e31 2022-10-23 thomas if ((gotd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
101 3efd8e31 2022-10-23 thomas err(1, "socket");
102 3efd8e31 2022-10-23 thomas
103 3efd8e31 2022-10-23 thomas memset(&sun, 0, sizeof(sun));
104 3efd8e31 2022-10-23 thomas sun.sun_family = AF_UNIX;
105 3efd8e31 2022-10-23 thomas if (strlcpy(sun.sun_path, unix_socket_path,
106 3efd8e31 2022-10-23 thomas sizeof(sun.sun_path)) >= sizeof(sun.sun_path))
107 3efd8e31 2022-10-23 thomas errx(1, "gotd socket path too long");
108 3efd8e31 2022-10-23 thomas if (connect(gotd_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
109 3efd8e31 2022-10-23 thomas err(1, "connect: %s", unix_socket_path);
110 3efd8e31 2022-10-23 thomas
111 3efd8e31 2022-10-23 thomas #ifndef PROFILE
112 3efd8e31 2022-10-23 thomas if (pledge("stdio recvfd", NULL) == -1)
113 3efd8e31 2022-10-23 thomas err(1, "pledge");
114 3efd8e31 2022-10-23 thomas #endif
115 214d733a 2023-01-23 thomas error = got_serve(STDIN_FILENO, STDOUT_FILENO, command, repo_path,
116 214d733a 2023-01-23 thomas gotd_sock, chattygot);
117 3efd8e31 2022-10-23 thomas done:
118 65ca77c9 2022-10-23 thomas free(gitcmd);
119 214d733a 2023-01-23 thomas free(command);
120 214d733a 2023-01-23 thomas free(repo_path);
121 3efd8e31 2022-10-23 thomas if (gotd_sock != -1)
122 3efd8e31 2022-10-23 thomas close(gotd_sock);
123 3efd8e31 2022-10-23 thomas if (error) {
124 3efd8e31 2022-10-23 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
125 3efd8e31 2022-10-23 thomas return 1;
126 3efd8e31 2022-10-23 thomas }
127 3efd8e31 2022-10-23 thomas
128 3efd8e31 2022-10-23 thomas return 0;
129 3efd8e31 2022-10-23 thomas }