2 13b2bc37 2022-10-23 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 13b2bc37 2022-10-23 stsp #include <sys/queue.h>
18 13b2bc37 2022-10-23 stsp #include <sys/socket.h>
19 13b2bc37 2022-10-23 stsp #include <sys/un.h>
21 13b2bc37 2022-10-23 stsp #include <err.h>
22 13b2bc37 2022-10-23 stsp #include <event.h>
23 13b2bc37 2022-10-23 stsp #include <imsg.h>
24 13b2bc37 2022-10-23 stsp #include <limits.h>
25 13b2bc37 2022-10-23 stsp #include <sha1.h>
26 13b2bc37 2022-10-23 stsp #include <stdio.h>
27 13b2bc37 2022-10-23 stsp #include <stdlib.h>
28 13b2bc37 2022-10-23 stsp #include <string.h>
29 13b2bc37 2022-10-23 stsp #include <unistd.h>
31 13b2bc37 2022-10-23 stsp #include "got_error.h"
32 13b2bc37 2022-10-23 stsp #include "got_serve.h"
34 13b2bc37 2022-10-23 stsp #include "gotd.h"
36 13b2bc37 2022-10-23 stsp static int chattygot;
38 13b2bc37 2022-10-23 stsp __dead static void
41 13b2bc37 2022-10-23 stsp fprintf(stderr, "usage: %s -c '%s|%s repository-path'\n",
42 13b2bc37 2022-10-23 stsp getprogname(), GOT_SERVE_CMD_SEND, GOT_SERVE_CMD_FETCH);
46 13b2bc37 2022-10-23 stsp static const struct got_error *
47 13b2bc37 2022-10-23 stsp apply_unveil(const char *unix_socket_path)
49 13b2bc37 2022-10-23 stsp #ifdef PROFILE
50 13b2bc37 2022-10-23 stsp if (unveil("gmon.out", "rwc") != 0)
51 13b2bc37 2022-10-23 stsp return got_error_from_errno2("unveil", "gmon.out");
53 13b2bc37 2022-10-23 stsp if (unveil(unix_socket_path, "w") != 0)
54 13b2bc37 2022-10-23 stsp return got_error_from_errno2("unveil", unix_socket_path);
56 13b2bc37 2022-10-23 stsp if (unveil(NULL, NULL) != 0)
57 13b2bc37 2022-10-23 stsp return got_error_from_errno("unveil");
59 13b2bc37 2022-10-23 stsp return NULL;
63 13b2bc37 2022-10-23 stsp main(int argc, char *argv[])
65 13b2bc37 2022-10-23 stsp const struct got_error *error;
66 8edf8796 2023-01-22 op const char *unix_socket_path;
67 13b2bc37 2022-10-23 stsp int gotd_sock = -1;
68 13b2bc37 2022-10-23 stsp struct sockaddr_un sun;
69 9aeaf23a 2023-01-22 op char *gitcmd = NULL, *command = NULL, *repo_path = NULL;
71 13b2bc37 2022-10-23 stsp #ifndef PROFILE
72 13b2bc37 2022-10-23 stsp if (pledge("stdio recvfd unix unveil", NULL) == -1)
73 13b2bc37 2022-10-23 stsp err(1, "pledge");
76 16e4df9d 2023-01-22 op unix_socket_path = getenv("GOTD_UNIX_SOCKET");
77 16e4df9d 2023-01-22 op if (unix_socket_path == NULL)
78 16e4df9d 2023-01-22 op unix_socket_path = GOTD_UNIX_SOCKET;
80 16e4df9d 2023-01-22 op error = apply_unveil(unix_socket_path);
84 bc854c7b 2022-10-23 stsp if (strcmp(argv[0], GOT_SERVE_CMD_SEND) == 0 ||
85 bc854c7b 2022-10-23 stsp strcmp(argv[0], GOT_SERVE_CMD_FETCH) == 0) {
86 bc854c7b 2022-10-23 stsp if (argc != 2)
88 bc854c7b 2022-10-23 stsp if (asprintf(&gitcmd, "%s %s", argv[0], argv[1]) == -1)
89 bc854c7b 2022-10-23 stsp err(1, "asprintf");
90 9aeaf23a 2023-01-22 op error = got_serve_parse_command(&command, &repo_path, gitcmd);
92 9aeaf23a 2023-01-22 op if (argc != 3 || strcmp(argv[1], "-c") != 0)
94 9aeaf23a 2023-01-22 op error = got_serve_parse_command(&command, &repo_path, argv[2]);
96 9aeaf23a 2023-01-22 op if (error && error->code == GOT_ERR_BAD_PACKET)
101 13b2bc37 2022-10-23 stsp if ((gotd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
102 13b2bc37 2022-10-23 stsp err(1, "socket");
104 13b2bc37 2022-10-23 stsp memset(&sun, 0, sizeof(sun));
105 13b2bc37 2022-10-23 stsp sun.sun_family = AF_UNIX;
106 13b2bc37 2022-10-23 stsp if (strlcpy(sun.sun_path, unix_socket_path,
107 13b2bc37 2022-10-23 stsp sizeof(sun.sun_path)) >= sizeof(sun.sun_path))
108 13b2bc37 2022-10-23 stsp errx(1, "gotd socket path too long");
109 13b2bc37 2022-10-23 stsp if (connect(gotd_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
110 13b2bc37 2022-10-23 stsp err(1, "connect: %s", unix_socket_path);
112 13b2bc37 2022-10-23 stsp #ifndef PROFILE
113 13b2bc37 2022-10-23 stsp if (pledge("stdio recvfd", NULL) == -1)
114 13b2bc37 2022-10-23 stsp err(1, "pledge");
116 9aeaf23a 2023-01-22 op error = got_serve(STDIN_FILENO, STDOUT_FILENO, command, repo_path,
117 9aeaf23a 2023-01-22 op gotd_sock, chattygot);
119 bc854c7b 2022-10-23 stsp free(gitcmd);
120 9aeaf23a 2023-01-22 op free(command);
121 9aeaf23a 2023-01-22 op free(repo_path);
122 13b2bc37 2022-10-23 stsp if (gotd_sock != -1)
123 13b2bc37 2022-10-23 stsp close(gotd_sock);
124 13b2bc37 2022-10-23 stsp if (error) {
125 13b2bc37 2022-10-23 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);