Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
18 #include <sys/socket.h>
19 #include <sys/un.h>
21 #include <err.h>
22 #include <event.h>
23 #include <imsg.h>
24 #include <limits.h>
25 #include <sha1.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "got_error.h"
32 #include "got_serve.h"
34 #include "gotd.h"
36 static int chattygot;
38 __dead static void
39 usage()
40 {
41 fprintf(stderr, "usage: %s -c '%s|%s repository-path'\n",
42 getprogname(), GOT_SERVE_CMD_SEND, GOT_SERVE_CMD_FETCH);
43 exit(1);
44 }
46 static const struct got_error *
47 apply_unveil(const char *unix_socket_path)
48 {
49 #ifdef PROFILE
50 if (unveil("gmon.out", "rwc") != 0)
51 return got_error_from_errno2("unveil", "gmon.out");
52 #endif
53 if (unveil(unix_socket_path, "w") != 0)
54 return got_error_from_errno2("unveil", unix_socket_path);
56 if (unveil(NULL, NULL) != 0)
57 return got_error_from_errno("unveil");
59 return NULL;
60 }
62 int
63 main(int argc, char *argv[])
64 {
65 const struct got_error *error;
66 char unix_socket_path[PATH_MAX];
67 char *unix_socket_path_env = getenv("GOTD_UNIX_SOCKET");
68 int gotd_sock = -1;
69 struct sockaddr_un sun;
70 char *gitcmd = NULL;
72 #ifndef PROFILE
73 if (pledge("stdio recvfd unix unveil", NULL) == -1)
74 err(1, "pledge");
75 #endif
76 if (strcmp(argv[0], GOT_SERVE_CMD_SEND) == 0 ||
77 strcmp(argv[0], GOT_SERVE_CMD_FETCH) == 0) {
78 if (argc != 2)
79 usage();
80 if (asprintf(&gitcmd, "%s %s", argv[0], argv[1]) == -1)
81 err(1, "asprintf");
82 } else {
83 if (argc != 3 || strcmp(argv[1], "-c") != 0 ||
84 (strncmp(argv[2], GOT_SERVE_CMD_SEND,
85 strlen(GOT_SERVE_CMD_SEND)) != 0 &&
86 (strncmp(argv[2], GOT_SERVE_CMD_FETCH,
87 strlen(GOT_SERVE_CMD_FETCH)) != 0)))
88 usage();
89 gitcmd = strdup(argv[2]);
90 if (gitcmd == NULL)
91 err(1, "strdup");
92 }
94 if (unix_socket_path_env) {
95 if (strlcpy(unix_socket_path, unix_socket_path_env,
96 sizeof(unix_socket_path)) >= sizeof(unix_socket_path))
97 errx(1, "gotd socket path too long");
98 } else {
99 strlcpy(unix_socket_path, GOTD_UNIX_SOCKET,
100 sizeof(unix_socket_path));
103 error = apply_unveil(unix_socket_path);
104 if (error)
105 goto done;
107 #ifndef PROFILE
108 if (pledge("stdio recvfd unix", NULL) == -1)
109 err(1, "pledge");
110 #endif
111 if ((gotd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
112 err(1, "socket");
114 memset(&sun, 0, sizeof(sun));
115 sun.sun_family = AF_UNIX;
116 if (strlcpy(sun.sun_path, unix_socket_path,
117 sizeof(sun.sun_path)) >= sizeof(sun.sun_path))
118 errx(1, "gotd socket path too long");
119 if (connect(gotd_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
120 err(1, "connect: %s", unix_socket_path);
122 #ifndef PROFILE
123 if (pledge("stdio recvfd", NULL) == -1)
124 err(1, "pledge");
125 #endif
126 error = got_serve(STDIN_FILENO, STDOUT_FILENO, gitcmd, gotd_sock,
127 chattygot);
128 done:
129 free(gitcmd);
130 if (gotd_sock != -1)
131 close(gotd_sock);
132 if (error) {
133 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
134 return 1;
137 return 0;