Blame


1 13b2bc37 2022-10-23 stsp /*
2 13b2bc37 2022-10-23 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 13b2bc37 2022-10-23 stsp *
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.
7 13b2bc37 2022-10-23 stsp *
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.
15 13b2bc37 2022-10-23 stsp */
16 13b2bc37 2022-10-23 stsp
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>
20 13b2bc37 2022-10-23 stsp
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>
30 13b2bc37 2022-10-23 stsp
31 13b2bc37 2022-10-23 stsp #include "got_error.h"
32 13b2bc37 2022-10-23 stsp #include "got_serve.h"
33 13b2bc37 2022-10-23 stsp
34 13b2bc37 2022-10-23 stsp #include "gotd.h"
35 13b2bc37 2022-10-23 stsp
36 13b2bc37 2022-10-23 stsp static int chattygot;
37 13b2bc37 2022-10-23 stsp
38 13b2bc37 2022-10-23 stsp __dead static void
39 13b2bc37 2022-10-23 stsp usage()
40 13b2bc37 2022-10-23 stsp {
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);
43 13b2bc37 2022-10-23 stsp exit(1);
44 13b2bc37 2022-10-23 stsp }
45 13b2bc37 2022-10-23 stsp
46 13b2bc37 2022-10-23 stsp static const struct got_error *
47 13b2bc37 2022-10-23 stsp apply_unveil(const char *unix_socket_path)
48 13b2bc37 2022-10-23 stsp {
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");
52 13b2bc37 2022-10-23 stsp #endif
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);
55 13b2bc37 2022-10-23 stsp
56 13b2bc37 2022-10-23 stsp if (unveil(NULL, NULL) != 0)
57 13b2bc37 2022-10-23 stsp return got_error_from_errno("unveil");
58 13b2bc37 2022-10-23 stsp
59 13b2bc37 2022-10-23 stsp return NULL;
60 13b2bc37 2022-10-23 stsp }
61 13b2bc37 2022-10-23 stsp
62 13b2bc37 2022-10-23 stsp int
63 13b2bc37 2022-10-23 stsp main(int argc, char *argv[])
64 13b2bc37 2022-10-23 stsp {
65 13b2bc37 2022-10-23 stsp const struct got_error *error;
66 13b2bc37 2022-10-23 stsp char unix_socket_path[PATH_MAX];
67 13b2bc37 2022-10-23 stsp char *unix_socket_path_env = getenv("GOTD_UNIX_SOCKET");
68 13b2bc37 2022-10-23 stsp int gotd_sock = -1;
69 13b2bc37 2022-10-23 stsp struct sockaddr_un sun;
70 13b2bc37 2022-10-23 stsp
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");
74 13b2bc37 2022-10-23 stsp #endif
75 13b2bc37 2022-10-23 stsp if (argc != 3 ||
76 13b2bc37 2022-10-23 stsp strcmp(argv[1], "-c") != 0 ||
77 13b2bc37 2022-10-23 stsp (strncmp(argv[2], GOT_SERVE_CMD_SEND,
78 13b2bc37 2022-10-23 stsp strlen(GOT_SERVE_CMD_SEND)) != 0 &&
79 13b2bc37 2022-10-23 stsp (strncmp(argv[2], GOT_SERVE_CMD_FETCH,
80 13b2bc37 2022-10-23 stsp strlen(GOT_SERVE_CMD_FETCH)) != 0)))
81 13b2bc37 2022-10-23 stsp usage();
82 13b2bc37 2022-10-23 stsp
83 13b2bc37 2022-10-23 stsp if (unix_socket_path_env) {
84 13b2bc37 2022-10-23 stsp if (strlcpy(unix_socket_path, unix_socket_path_env,
85 13b2bc37 2022-10-23 stsp sizeof(unix_socket_path)) >= sizeof(unix_socket_path))
86 13b2bc37 2022-10-23 stsp errx(1, "gotd socket path too long");
87 13b2bc37 2022-10-23 stsp } else {
88 13b2bc37 2022-10-23 stsp strlcpy(unix_socket_path, GOTD_UNIX_SOCKET,
89 13b2bc37 2022-10-23 stsp sizeof(unix_socket_path));
90 13b2bc37 2022-10-23 stsp }
91 13b2bc37 2022-10-23 stsp
92 13b2bc37 2022-10-23 stsp error = apply_unveil(unix_socket_path);
93 13b2bc37 2022-10-23 stsp if (error)
94 13b2bc37 2022-10-23 stsp goto done;
95 13b2bc37 2022-10-23 stsp
96 13b2bc37 2022-10-23 stsp #ifndef PROFILE
97 13b2bc37 2022-10-23 stsp if (pledge("stdio recvfd unix", NULL) == -1)
98 13b2bc37 2022-10-23 stsp err(1, "pledge");
99 13b2bc37 2022-10-23 stsp #endif
100 13b2bc37 2022-10-23 stsp if ((gotd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
101 13b2bc37 2022-10-23 stsp err(1, "socket");
102 13b2bc37 2022-10-23 stsp
103 13b2bc37 2022-10-23 stsp memset(&sun, 0, sizeof(sun));
104 13b2bc37 2022-10-23 stsp sun.sun_family = AF_UNIX;
105 13b2bc37 2022-10-23 stsp if (strlcpy(sun.sun_path, unix_socket_path,
106 13b2bc37 2022-10-23 stsp sizeof(sun.sun_path)) >= sizeof(sun.sun_path))
107 13b2bc37 2022-10-23 stsp errx(1, "gotd socket path too long");
108 13b2bc37 2022-10-23 stsp if (connect(gotd_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
109 13b2bc37 2022-10-23 stsp err(1, "connect: %s", unix_socket_path);
110 13b2bc37 2022-10-23 stsp
111 13b2bc37 2022-10-23 stsp #ifndef PROFILE
112 13b2bc37 2022-10-23 stsp if (pledge("stdio recvfd", NULL) == -1)
113 13b2bc37 2022-10-23 stsp err(1, "pledge");
114 13b2bc37 2022-10-23 stsp #endif
115 13b2bc37 2022-10-23 stsp error = got_serve(STDIN_FILENO, STDOUT_FILENO, argv[2], gotd_sock,
116 13b2bc37 2022-10-23 stsp chattygot);
117 13b2bc37 2022-10-23 stsp done:
118 13b2bc37 2022-10-23 stsp if (gotd_sock != -1)
119 13b2bc37 2022-10-23 stsp close(gotd_sock);
120 13b2bc37 2022-10-23 stsp if (error) {
121 13b2bc37 2022-10-23 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
122 13b2bc37 2022-10-23 stsp return 1;
123 13b2bc37 2022-10-23 stsp }
124 13b2bc37 2022-10-23 stsp
125 13b2bc37 2022-10-23 stsp return 0;
126 13b2bc37 2022-10-23 stsp }