2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
18 #include <sys/socket.h>
32 #include "got_error.h"
33 #include "got_serve.h"
43 fprintf(stderr, "usage: %s -c '%s|%s repository-path'\n",
44 getprogname(), GOT_SERVE_CMD_SEND, GOT_SERVE_CMD_FETCH);
48 static const struct got_error *
49 apply_unveil(const char *unix_socket_path)
52 if (unveil("gmon.out", "rwc") != 0)
53 return got_error_from_errno2("unveil", "gmon.out");
55 if (unveil(unix_socket_path, "w") != 0)
56 return got_error_from_errno2("unveil", unix_socket_path);
58 if (unveil(NULL, NULL) != 0)
59 return got_error_from_errno("unveil");
65 main(int argc, char *argv[])
67 const struct got_error *error;
68 const char *unix_socket_path;
70 struct sockaddr_un sun;
71 char *gitcmd = NULL, *command = NULL, *repo_path = NULL;
74 if (pledge("stdio recvfd unix unveil", NULL) == -1)
78 unix_socket_path = getenv("GOTD_UNIX_SOCKET");
79 if (unix_socket_path == NULL)
80 unix_socket_path = GOTD_UNIX_SOCKET;
82 error = apply_unveil(unix_socket_path);
86 if (strcmp(argv[0], GOT_SERVE_CMD_SEND) == 0 ||
87 strcmp(argv[0], GOT_SERVE_CMD_FETCH) == 0) {
90 if (asprintf(&gitcmd, "%s %s", argv[0], argv[1]) == -1)
92 error = got_serve_parse_command(&command, &repo_path, gitcmd);
94 if (argc != 3 || strcmp(argv[1], "-c") != 0)
96 error = got_serve_parse_command(&command, &repo_path, argv[2]);
98 if (error && error->code == GOT_ERR_BAD_PACKET)
103 if ((gotd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
106 memset(&sun, 0, sizeof(sun));
107 sun.sun_family = AF_UNIX;
108 if (strlcpy(sun.sun_path, unix_socket_path,
109 sizeof(sun.sun_path)) >= sizeof(sun.sun_path))
110 errx(1, "gotd socket path too long");
111 if (connect(gotd_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
112 err(1, "connect: %s", unix_socket_path);
115 if (pledge("stdio recvfd", NULL) == -1)
118 error = got_serve(STDIN_FILENO, STDOUT_FILENO, command, repo_path,
119 gotd_sock, chattygot);
127 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);