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