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