Blame


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