Blob


1 /*
2 * Copyright (c) 2023 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 /*
18 * Resolve path namespace conflicts for git-upload-pack and git-receive-pack.
19 */
21 #include <sys/queue.h>
22 #include <sys/types.h>
23 #include <sys/uio.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <event.h>
28 #include <imsg.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sha1.h>
34 #include <sha2.h>
35 #include <syslog.h>
36 #include <util.h>
37 #include <unistd.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_path.h"
43 #include "got_lib_dial.h"
45 #include "gotd.h"
46 #include "log.h"
48 #ifndef GITWRAPPER_GIT_LIBEXEC_DIR
49 #define GITWRAPPER_GIT_LIBEXEC_DIR "/usr/local/libexec/git"
50 #endif
52 #ifndef GITWRAPPER_MY_SERVER_PROG
53 #define GITWRAPPER_MY_SERVER_PROG "gotsh"
54 #endif
57 __dead static void
58 usage(void)
59 {
60 fprintf(stderr, "usage: %s -c '%s|%s repository-path'\n",
61 getprogname(), GOT_DIAL_CMD_SEND, GOT_DIAL_CMD_FETCH);
62 exit(1);
63 }
65 /*
66 * Unveil the specific programs we want to start and hide everything else.
67 * This is important to limit the impact of our "exec" pledge.
68 */
69 static const struct got_error *
70 apply_unveil(const char *myserver)
71 {
72 const char *fetchcmd = GITWRAPPER_GIT_LIBEXEC_DIR "/" \
73 GOT_DIAL_CMD_FETCH;
74 const char *sendcmd = GITWRAPPER_GIT_LIBEXEC_DIR "/" \
75 GOT_DIAL_CMD_SEND;
77 #ifdef PROFILE
78 if (unveil("gmon.out", "rwc") != 0)
79 return got_error_from_errno2("unveil", "gmon.out");
80 #endif
81 if (unveil(fetchcmd, "x") != 0 && errno != ENOENT)
82 return got_error_from_errno2("unveil", fetchcmd);
84 if (unveil(sendcmd, "x") != 0 && errno != ENOENT)
85 return got_error_from_errno2("unveil", sendcmd);
87 if (myserver && unveil(myserver, "x") != 0 && errno != ENOENT)
88 return got_error_from_errno2("unveil", myserver);
90 if (unveil(NULL, NULL) != 0)
91 return got_error_from_errno("unveil");
93 return NULL;
94 }
96 int
97 main(int argc, char *argv[])
98 {
99 const struct got_error *error;
100 const char *confpath = NULL;
101 char *command = NULL, *repo_name = NULL; /* for matching gotd.conf */
102 char *myserver = NULL;
103 const char *repo_path = NULL; /* as passed on the command line */
104 const char *relpath;
105 char *gitcommand = NULL;
106 struct gotd gotd;
107 struct gotd_repo *repo = NULL;
109 log_init(1, LOG_USER); /* Log to stderr. */
111 #ifndef PROFILE
112 if (pledge("stdio rpath exec unveil", NULL) == -1)
113 err(1, "pledge");
114 #endif
116 /*
117 * Look up our own server program in PATH so we can unveil(2) it.
118 * This call only errors out upon memory allocation failure.
119 * If the program cannot be found then myserver will be set to NULL.
120 */
121 error = got_path_find_prog(&myserver, GITWRAPPER_MY_SERVER_PROG);
122 if (error)
123 goto done;
125 /*
126 * Run parse_config() before unveil(2) because parse_config()
127 * checks whether repository paths exist on disk.
128 * Parsing errors and warnings will be logged to stderr.
129 * Upon failure we will run Git's native tooling so do not
130 * bother checking for errors here.
131 */
132 confpath = getenv("GOTD_CONF_PATH");
133 if (confpath == NULL)
134 confpath = GOTD_CONF_PATH;
135 parse_config(confpath, PROC_GITWRAPPER, &gotd);
137 error = apply_unveil(myserver);
138 if (error)
139 goto done;
141 #ifndef PROFILE
142 if (pledge("stdio exec", NULL) == -1)
143 err(1, "pledge");
144 #endif
146 if (strcmp(getprogname(), GOT_DIAL_CMD_SEND) == 0 ||
147 strcmp(getprogname(), GOT_DIAL_CMD_FETCH) == 0) {
148 if (argc != 2)
149 usage();
150 command = strdup(getprogname());
151 if (command == NULL) {
152 error = got_error_from_errno("strdup");
153 goto done;
155 repo_path = argv[1];
156 relpath = argv[1];
157 while (relpath[0] == '/')
158 relpath++;
159 repo_name = strdup(relpath);
160 if (repo_name == NULL) {
161 error = got_error_from_errno("strdup");
162 goto done;
164 } else {
165 if (argc != 3 || strcmp(argv[1], "-c") != 0)
166 usage();
167 repo_path = argv[2];
168 error = got_dial_parse_command(&command, &repo_name,
169 repo_path);
170 if (error && error->code == GOT_ERR_BAD_PACKET)
171 usage();
172 if (error)
173 goto done;
176 repo = gotd_find_repo_by_name(repo_name, &gotd.repos);
178 /*
179 * Invoke our custom Git server if the repository was found
180 * in gotd.conf. Otherwise invoke native git(1) tooling.
181 */
182 if (repo) {
183 if (myserver == NULL) {
184 error = got_error_fmt(GOT_ERR_NO_PROG,
185 "cannot run '%s'",
186 GITWRAPPER_MY_SERVER_PROG);
187 goto done;
189 execl(myserver, command, repo_name, (char *)NULL);
190 error = got_error_from_errno2("execl", myserver);
191 } else {
192 if (asprintf(&gitcommand, "%s/%s",
193 GITWRAPPER_GIT_LIBEXEC_DIR, command) == -1) {
194 error = got_error_from_errno("asprintf");
195 goto done;
197 execl(gitcommand, gitcommand, repo_path, (char *)NULL);
198 error = got_error_from_errno2("execl", gitcommand);
201 done:
202 free(command);
203 free(repo_name);
204 free(myserver);
205 free(gitcommand);
206 if (error) {
207 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
208 return 1;
211 return 0;