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