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 4efc8dcb 2023-08-29 thomas #include "got_compat.h"
18 4efc8dcb 2023-08-29 thomas
19 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
20 3efd8e31 2022-10-23 thomas #include <sys/socket.h>
21 3efd8e31 2022-10-23 thomas #include <sys/un.h>
22 3efd8e31 2022-10-23 thomas
23 3efd8e31 2022-10-23 thomas #include <err.h>
24 3efd8e31 2022-10-23 thomas #include <event.h>
25 3efd8e31 2022-10-23 thomas #include <imsg.h>
26 3efd8e31 2022-10-23 thomas #include <limits.h>
27 3efd8e31 2022-10-23 thomas #include <stdio.h>
28 3efd8e31 2022-10-23 thomas #include <stdlib.h>
29 3efd8e31 2022-10-23 thomas #include <string.h>
30 3efd8e31 2022-10-23 thomas #include <unistd.h>
31 3efd8e31 2022-10-23 thomas
32 3efd8e31 2022-10-23 thomas #include "got_error.h"
33 3efd8e31 2022-10-23 thomas #include "got_serve.h"
34 6d7eb4f7 2023-04-04 thomas #include "got_path.h"
35 52daa6ad 2023-08-29 thomas #include "got_compat.h"
36 3efd8e31 2022-10-23 thomas
37 5769f9a0 2023-04-22 thomas #include "got_lib_dial.h"
38 5769f9a0 2023-04-22 thomas
39 3efd8e31 2022-10-23 thomas #include "gotd.h"
40 3efd8e31 2022-10-23 thomas
41 3efd8e31 2022-10-23 thomas static int chattygot;
42 3efd8e31 2022-10-23 thomas
43 3efd8e31 2022-10-23 thomas __dead static void
44 96d694ac 2023-02-17 thomas usage(void)
45 3efd8e31 2022-10-23 thomas {
46 3efd8e31 2022-10-23 thomas fprintf(stderr, "usage: %s -c '%s|%s repository-path'\n",
47 5769f9a0 2023-04-22 thomas getprogname(), GOT_DIAL_CMD_SEND, GOT_DIAL_CMD_FETCH);
48 3efd8e31 2022-10-23 thomas exit(1);
49 3efd8e31 2022-10-23 thomas }
50 3efd8e31 2022-10-23 thomas
51 3efd8e31 2022-10-23 thomas static const struct got_error *
52 3efd8e31 2022-10-23 thomas apply_unveil(const char *unix_socket_path)
53 3efd8e31 2022-10-23 thomas {
54 3efd8e31 2022-10-23 thomas #ifdef PROFILE
55 3efd8e31 2022-10-23 thomas if (unveil("gmon.out", "rwc") != 0)
56 3efd8e31 2022-10-23 thomas return got_error_from_errno2("unveil", "gmon.out");
57 3efd8e31 2022-10-23 thomas #endif
58 3efd8e31 2022-10-23 thomas if (unveil(unix_socket_path, "w") != 0)
59 3efd8e31 2022-10-23 thomas return got_error_from_errno2("unveil", unix_socket_path);
60 3efd8e31 2022-10-23 thomas
61 3efd8e31 2022-10-23 thomas if (unveil(NULL, NULL) != 0)
62 3efd8e31 2022-10-23 thomas return got_error_from_errno("unveil");
63 3efd8e31 2022-10-23 thomas
64 3efd8e31 2022-10-23 thomas return NULL;
65 3efd8e31 2022-10-23 thomas }
66 3efd8e31 2022-10-23 thomas
67 3efd8e31 2022-10-23 thomas int
68 3efd8e31 2022-10-23 thomas main(int argc, char *argv[])
69 3efd8e31 2022-10-23 thomas {
70 3efd8e31 2022-10-23 thomas const struct got_error *error;
71 c8fcdde8 2023-01-23 thomas const char *unix_socket_path;
72 3efd8e31 2022-10-23 thomas int gotd_sock = -1;
73 3efd8e31 2022-10-23 thomas struct sockaddr_un sun;
74 214d733a 2023-01-23 thomas char *gitcmd = NULL, *command = NULL, *repo_path = NULL;
75 3efd8e31 2022-10-23 thomas
76 3efd8e31 2022-10-23 thomas #ifndef PROFILE
77 3efd8e31 2022-10-23 thomas if (pledge("stdio recvfd unix unveil", NULL) == -1)
78 3efd8e31 2022-10-23 thomas err(1, "pledge");
79 3efd8e31 2022-10-23 thomas #endif
80 6e58d3c9 2023-01-23 thomas
81 6e58d3c9 2023-01-23 thomas unix_socket_path = getenv("GOTD_UNIX_SOCKET");
82 6e58d3c9 2023-01-23 thomas if (unix_socket_path == NULL)
83 6e58d3c9 2023-01-23 thomas unix_socket_path = GOTD_UNIX_SOCKET;
84 6e58d3c9 2023-01-23 thomas
85 6e58d3c9 2023-01-23 thomas error = apply_unveil(unix_socket_path);
86 6e58d3c9 2023-01-23 thomas if (error)
87 6e58d3c9 2023-01-23 thomas goto done;
88 6e58d3c9 2023-01-23 thomas
89 5769f9a0 2023-04-22 thomas if (strcmp(argv[0], GOT_DIAL_CMD_SEND) == 0 ||
90 5769f9a0 2023-04-22 thomas strcmp(argv[0], GOT_DIAL_CMD_FETCH) == 0) {
91 65ca77c9 2022-10-23 thomas if (argc != 2)
92 65ca77c9 2022-10-23 thomas usage();
93 65ca77c9 2022-10-23 thomas if (asprintf(&gitcmd, "%s %s", argv[0], argv[1]) == -1)
94 65ca77c9 2022-10-23 thomas err(1, "asprintf");
95 5769f9a0 2023-04-22 thomas error = got_dial_parse_command(&command, &repo_path, gitcmd);
96 65ca77c9 2022-10-23 thomas } else {
97 214d733a 2023-01-23 thomas if (argc != 3 || strcmp(argv[1], "-c") != 0)
98 65ca77c9 2022-10-23 thomas usage();
99 5769f9a0 2023-04-22 thomas error = got_dial_parse_command(&command, &repo_path, argv[2]);
100 65ca77c9 2022-10-23 thomas }
101 214d733a 2023-01-23 thomas if (error && error->code == GOT_ERR_BAD_PACKET)
102 214d733a 2023-01-23 thomas usage();
103 214d733a 2023-01-23 thomas if (error)
104 214d733a 2023-01-23 thomas goto done;
105 3efd8e31 2022-10-23 thomas
106 3efd8e31 2022-10-23 thomas if ((gotd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
107 3efd8e31 2022-10-23 thomas err(1, "socket");
108 3efd8e31 2022-10-23 thomas
109 3efd8e31 2022-10-23 thomas memset(&sun, 0, sizeof(sun));
110 3efd8e31 2022-10-23 thomas sun.sun_family = AF_UNIX;
111 3efd8e31 2022-10-23 thomas if (strlcpy(sun.sun_path, unix_socket_path,
112 3efd8e31 2022-10-23 thomas sizeof(sun.sun_path)) >= sizeof(sun.sun_path))
113 3efd8e31 2022-10-23 thomas errx(1, "gotd socket path too long");
114 3efd8e31 2022-10-23 thomas if (connect(gotd_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
115 3efd8e31 2022-10-23 thomas err(1, "connect: %s", unix_socket_path);
116 3efd8e31 2022-10-23 thomas
117 3efd8e31 2022-10-23 thomas #ifndef PROFILE
118 3efd8e31 2022-10-23 thomas if (pledge("stdio recvfd", NULL) == -1)
119 3efd8e31 2022-10-23 thomas err(1, "pledge");
120 3efd8e31 2022-10-23 thomas #endif
121 214d733a 2023-01-23 thomas error = got_serve(STDIN_FILENO, STDOUT_FILENO, command, repo_path,
122 214d733a 2023-01-23 thomas gotd_sock, chattygot);
123 3efd8e31 2022-10-23 thomas done:
124 65ca77c9 2022-10-23 thomas free(gitcmd);
125 214d733a 2023-01-23 thomas free(command);
126 214d733a 2023-01-23 thomas free(repo_path);
127 3efd8e31 2022-10-23 thomas if (gotd_sock != -1)
128 3efd8e31 2022-10-23 thomas close(gotd_sock);
129 3efd8e31 2022-10-23 thomas if (error) {
130 3efd8e31 2022-10-23 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
131 3efd8e31 2022-10-23 thomas return 1;
132 3efd8e31 2022-10-23 thomas }
133 3efd8e31 2022-10-23 thomas
134 3efd8e31 2022-10-23 thomas return 0;
135 3efd8e31 2022-10-23 thomas }