Blame


1 b09c1279 2023-03-28 stsp /*
2 b09c1279 2023-03-28 stsp * Copyright (c) 2023 Stefan Sperling <stsp@openbsd.org>
3 b09c1279 2023-03-28 stsp *
4 b09c1279 2023-03-28 stsp * Permission to use, copy, modify, and distribute this software for any
5 b09c1279 2023-03-28 stsp * purpose with or without fee is hereby granted, provided that the above
6 b09c1279 2023-03-28 stsp * copyright notice and this permission notice appear in all copies.
7 b09c1279 2023-03-28 stsp *
8 b09c1279 2023-03-28 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 b09c1279 2023-03-28 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 b09c1279 2023-03-28 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 b09c1279 2023-03-28 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 b09c1279 2023-03-28 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 b09c1279 2023-03-28 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 b09c1279 2023-03-28 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 b09c1279 2023-03-28 stsp */
16 b09c1279 2023-03-28 stsp
17 b09c1279 2023-03-28 stsp /*
18 b09c1279 2023-03-28 stsp * Resolve path namespace conflicts for git-upload-pack and git-receive-pack.
19 b09c1279 2023-03-28 stsp */
20 b09c1279 2023-03-28 stsp
21 b09c1279 2023-03-28 stsp #include <sys/queue.h>
22 b09c1279 2023-03-28 stsp #include <sys/types.h>
23 b09c1279 2023-03-28 stsp #include <sys/uio.h>
24 b09c1279 2023-03-28 stsp
25 b09c1279 2023-03-28 stsp #include <err.h>
26 b09c1279 2023-03-28 stsp #include <errno.h>
27 b09c1279 2023-03-28 stsp #include <event.h>
28 b09c1279 2023-03-28 stsp #include <imsg.h>
29 b09c1279 2023-03-28 stsp #include <limits.h>
30 b09c1279 2023-03-28 stsp #include <stdio.h>
31 b09c1279 2023-03-28 stsp #include <stdlib.h>
32 b09c1279 2023-03-28 stsp #include <string.h>
33 b09c1279 2023-03-28 stsp #include <sha1.h>
34 b09c1279 2023-03-28 stsp #include <sha2.h>
35 b09c1279 2023-03-28 stsp #include <syslog.h>
36 b09c1279 2023-03-28 stsp #include <util.h>
37 b09c1279 2023-03-28 stsp #include <unistd.h>
38 b09c1279 2023-03-28 stsp
39 b09c1279 2023-03-28 stsp #include "got_error.h"
40 1b1a386d 2024-07-09 op #include "got_object.h"
41 b09c1279 2023-03-28 stsp #include "got_path.h"
42 b09c1279 2023-03-28 stsp
43 1eb38992 2023-04-14 stsp #include "got_lib_dial.h"
44 1eb38992 2023-04-14 stsp
45 b09c1279 2023-03-28 stsp #include "gotd.h"
46 b09c1279 2023-03-28 stsp #include "log.h"
47 b09c1279 2023-03-28 stsp
48 b09c1279 2023-03-28 stsp #ifndef GITWRAPPER_GIT_LIBEXEC_DIR
49 b09c1279 2023-03-28 stsp #define GITWRAPPER_GIT_LIBEXEC_DIR "/usr/local/libexec/git"
50 b09c1279 2023-03-28 stsp #endif
51 b09c1279 2023-03-28 stsp
52 b09c1279 2023-03-28 stsp #ifndef GITWRAPPER_MY_SERVER_PROG
53 b09c1279 2023-03-28 stsp #define GITWRAPPER_MY_SERVER_PROG "gotsh"
54 b09c1279 2023-03-28 stsp #endif
55 b09c1279 2023-03-28 stsp
56 b09c1279 2023-03-28 stsp
57 b09c1279 2023-03-28 stsp __dead static void
58 b09c1279 2023-03-28 stsp usage(void)
59 b09c1279 2023-03-28 stsp {
60 b09c1279 2023-03-28 stsp fprintf(stderr, "usage: %s -c '%s|%s repository-path'\n",
61 1eb38992 2023-04-14 stsp getprogname(), GOT_DIAL_CMD_SEND, GOT_DIAL_CMD_FETCH);
62 b09c1279 2023-03-28 stsp exit(1);
63 b09c1279 2023-03-28 stsp }
64 b09c1279 2023-03-28 stsp
65 b09c1279 2023-03-28 stsp /*
66 b09c1279 2023-03-28 stsp * Unveil the specific programs we want to start and hide everything else.
67 b09c1279 2023-03-28 stsp * This is important to limit the impact of our "exec" pledge.
68 b09c1279 2023-03-28 stsp */
69 b09c1279 2023-03-28 stsp static const struct got_error *
70 b09c1279 2023-03-28 stsp apply_unveil(const char *myserver)
71 b09c1279 2023-03-28 stsp {
72 b09c1279 2023-03-28 stsp const char *fetchcmd = GITWRAPPER_GIT_LIBEXEC_DIR "/" \
73 1eb38992 2023-04-14 stsp GOT_DIAL_CMD_FETCH;
74 b09c1279 2023-03-28 stsp const char *sendcmd = GITWRAPPER_GIT_LIBEXEC_DIR "/" \
75 1eb38992 2023-04-14 stsp GOT_DIAL_CMD_SEND;
76 b09c1279 2023-03-28 stsp
77 b09c1279 2023-03-28 stsp #ifdef PROFILE
78 b09c1279 2023-03-28 stsp if (unveil("gmon.out", "rwc") != 0)
79 b09c1279 2023-03-28 stsp return got_error_from_errno2("unveil", "gmon.out");
80 b09c1279 2023-03-28 stsp #endif
81 5157cdbb 2023-05-25 stsp if (unveil(fetchcmd, "x") != 0 && errno != ENOENT)
82 b09c1279 2023-03-28 stsp return got_error_from_errno2("unveil", fetchcmd);
83 b09c1279 2023-03-28 stsp
84 5157cdbb 2023-05-25 stsp if (unveil(sendcmd, "x") != 0 && errno != ENOENT)
85 b09c1279 2023-03-28 stsp return got_error_from_errno2("unveil", sendcmd);
86 b09c1279 2023-03-28 stsp
87 5157cdbb 2023-05-25 stsp if (myserver && unveil(myserver, "x") != 0 && errno != ENOENT)
88 b09c1279 2023-03-28 stsp return got_error_from_errno2("unveil", myserver);
89 b09c1279 2023-03-28 stsp
90 b09c1279 2023-03-28 stsp if (unveil(NULL, NULL) != 0)
91 b09c1279 2023-03-28 stsp return got_error_from_errno("unveil");
92 b09c1279 2023-03-28 stsp
93 b09c1279 2023-03-28 stsp return NULL;
94 b09c1279 2023-03-28 stsp }
95 b09c1279 2023-03-28 stsp
96 b09c1279 2023-03-28 stsp int
97 b09c1279 2023-03-28 stsp main(int argc, char *argv[])
98 b09c1279 2023-03-28 stsp {
99 b09c1279 2023-03-28 stsp const struct got_error *error;
100 b09c1279 2023-03-28 stsp const char *confpath = NULL;
101 b09c1279 2023-03-28 stsp char *command = NULL, *repo_name = NULL; /* for matching gotd.conf */
102 b09c1279 2023-03-28 stsp char *myserver = NULL;
103 b09c1279 2023-03-28 stsp const char *repo_path = NULL; /* as passed on the command line */
104 b09c1279 2023-03-28 stsp const char *relpath;
105 b09c1279 2023-03-28 stsp char *gitcommand = NULL;
106 b09c1279 2023-03-28 stsp struct gotd gotd;
107 b09c1279 2023-03-28 stsp struct gotd_repo *repo = NULL;
108 b09c1279 2023-03-28 stsp
109 b09c1279 2023-03-28 stsp log_init(1, LOG_USER); /* Log to stderr. */
110 b09c1279 2023-03-28 stsp
111 b09c1279 2023-03-28 stsp #ifndef PROFILE
112 c5d17ec8 2023-03-28 op if (pledge("stdio rpath exec unveil", NULL) == -1)
113 b09c1279 2023-03-28 stsp err(1, "pledge");
114 b09c1279 2023-03-28 stsp #endif
115 b09c1279 2023-03-28 stsp
116 b09c1279 2023-03-28 stsp /*
117 b09c1279 2023-03-28 stsp * Look up our own server program in PATH so we can unveil(2) it.
118 b09c1279 2023-03-28 stsp * This call only errors out upon memory allocation failure.
119 b09c1279 2023-03-28 stsp * If the program cannot be found then myserver will be set to NULL.
120 b09c1279 2023-03-28 stsp */
121 b09c1279 2023-03-28 stsp error = got_path_find_prog(&myserver, GITWRAPPER_MY_SERVER_PROG);
122 b09c1279 2023-03-28 stsp if (error)
123 b09c1279 2023-03-28 stsp goto done;
124 b09c1279 2023-03-28 stsp
125 b09c1279 2023-03-28 stsp /*
126 b09c1279 2023-03-28 stsp * Run parse_config() before unveil(2) because parse_config()
127 b09c1279 2023-03-28 stsp * checks whether repository paths exist on disk.
128 b09c1279 2023-03-28 stsp * Parsing errors and warnings will be logged to stderr.
129 b09c1279 2023-03-28 stsp * Upon failure we will run Git's native tooling so do not
130 b09c1279 2023-03-28 stsp * bother checking for errors here.
131 b09c1279 2023-03-28 stsp */
132 b09c1279 2023-03-28 stsp confpath = getenv("GOTD_CONF_PATH");
133 b09c1279 2023-03-28 stsp if (confpath == NULL)
134 b09c1279 2023-03-28 stsp confpath = GOTD_CONF_PATH;
135 4b3827cd 2023-07-08 stsp parse_config(confpath, PROC_GITWRAPPER, &gotd);
136 b09c1279 2023-03-28 stsp
137 b09c1279 2023-03-28 stsp error = apply_unveil(myserver);
138 b09c1279 2023-03-28 stsp if (error)
139 b09c1279 2023-03-28 stsp goto done;
140 b09c1279 2023-03-28 stsp
141 b09c1279 2023-03-28 stsp #ifndef PROFILE
142 c5d17ec8 2023-03-28 op if (pledge("stdio exec", NULL) == -1)
143 b09c1279 2023-03-28 stsp err(1, "pledge");
144 b09c1279 2023-03-28 stsp #endif
145 b09c1279 2023-03-28 stsp
146 1eb38992 2023-04-14 stsp if (strcmp(getprogname(), GOT_DIAL_CMD_SEND) == 0 ||
147 1eb38992 2023-04-14 stsp strcmp(getprogname(), GOT_DIAL_CMD_FETCH) == 0) {
148 b09c1279 2023-03-28 stsp if (argc != 2)
149 b09c1279 2023-03-28 stsp usage();
150 b09c1279 2023-03-28 stsp command = strdup(getprogname());
151 b09c1279 2023-03-28 stsp if (command == NULL) {
152 b09c1279 2023-03-28 stsp error = got_error_from_errno("strdup");
153 b09c1279 2023-03-28 stsp goto done;
154 b09c1279 2023-03-28 stsp }
155 b09c1279 2023-03-28 stsp repo_path = argv[1];
156 b09c1279 2023-03-28 stsp relpath = argv[1];
157 b09c1279 2023-03-28 stsp while (relpath[0] == '/')
158 b09c1279 2023-03-28 stsp relpath++;
159 b09c1279 2023-03-28 stsp repo_name = strdup(relpath);
160 b09c1279 2023-03-28 stsp if (repo_name == NULL) {
161 b09c1279 2023-03-28 stsp error = got_error_from_errno("strdup");
162 b09c1279 2023-03-28 stsp goto done;
163 b09c1279 2023-03-28 stsp }
164 b09c1279 2023-03-28 stsp } else {
165 b09c1279 2023-03-28 stsp if (argc != 3 || strcmp(argv[1], "-c") != 0)
166 b09c1279 2023-03-28 stsp usage();
167 b09c1279 2023-03-28 stsp repo_path = argv[2];
168 1eb38992 2023-04-14 stsp error = got_dial_parse_command(&command, &repo_name,
169 b09c1279 2023-03-28 stsp repo_path);
170 b09c1279 2023-03-28 stsp if (error && error->code == GOT_ERR_BAD_PACKET)
171 b09c1279 2023-03-28 stsp usage();
172 b09c1279 2023-03-28 stsp if (error)
173 b09c1279 2023-03-28 stsp goto done;
174 b09c1279 2023-03-28 stsp }
175 b09c1279 2023-03-28 stsp
176 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(repo_name, &gotd.repos);
177 b09c1279 2023-03-28 stsp
178 b09c1279 2023-03-28 stsp /*
179 6c5befc7 2023-03-28 stsp * Invoke our custom Git server if the repository was found
180 6c5befc7 2023-03-28 stsp * in gotd.conf. Otherwise invoke native git(1) tooling.
181 b09c1279 2023-03-28 stsp */
182 c5d17ec8 2023-03-28 op if (repo) {
183 c5d17ec8 2023-03-28 op if (myserver == NULL) {
184 c5d17ec8 2023-03-28 op error = got_error_fmt(GOT_ERR_NO_PROG,
185 c5d17ec8 2023-03-28 op "cannot run '%s'",
186 c5d17ec8 2023-03-28 op GITWRAPPER_MY_SERVER_PROG);
187 c5d17ec8 2023-03-28 op goto done;
188 b09c1279 2023-03-28 stsp }
189 c5d17ec8 2023-03-28 op execl(myserver, command, repo_name, (char *)NULL);
190 c5d17ec8 2023-03-28 op error = got_error_from_errno2("execl", myserver);
191 c5d17ec8 2023-03-28 op } else {
192 c5d17ec8 2023-03-28 op if (asprintf(&gitcommand, "%s/%s",
193 c5d17ec8 2023-03-28 op GITWRAPPER_GIT_LIBEXEC_DIR, command) == -1) {
194 c5d17ec8 2023-03-28 op error = got_error_from_errno("asprintf");
195 c5d17ec8 2023-03-28 op goto done;
196 c5d17ec8 2023-03-28 op }
197 c5d17ec8 2023-03-28 op execl(gitcommand, gitcommand, repo_path, (char *)NULL);
198 c5d17ec8 2023-03-28 op error = got_error_from_errno2("execl", gitcommand);
199 b09c1279 2023-03-28 stsp }
200 b09c1279 2023-03-28 stsp
201 b09c1279 2023-03-28 stsp done:
202 b09c1279 2023-03-28 stsp free(command);
203 b09c1279 2023-03-28 stsp free(repo_name);
204 b09c1279 2023-03-28 stsp free(myserver);
205 b09c1279 2023-03-28 stsp free(gitcommand);
206 b09c1279 2023-03-28 stsp if (error) {
207 b09c1279 2023-03-28 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
208 b09c1279 2023-03-28 stsp return 1;
209 b09c1279 2023-03-28 stsp }
210 b09c1279 2023-03-28 stsp
211 b09c1279 2023-03-28 stsp return 0;
212 b09c1279 2023-03-28 stsp }