2 * Copyright (c) 2023 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 * Resolve path namespace conflicts for git-upload-pack and git-receive-pack.
21 #include <sys/queue.h>
22 #include <sys/types.h>
39 #include "got_error.h"
42 #include "got_lib_dial.h"
47 #ifndef GITWRAPPER_GIT_LIBEXEC_DIR
48 #define GITWRAPPER_GIT_LIBEXEC_DIR "/usr/local/libexec/git"
51 #ifndef GITWRAPPER_MY_SERVER_PROG
52 #define GITWRAPPER_MY_SERVER_PROG "gotsh"
59 fprintf(stderr, "usage: %s -c '%s|%s repository-path'\n",
60 getprogname(), GOT_DIAL_CMD_SEND, GOT_DIAL_CMD_FETCH);
65 * Unveil the specific programs we want to start and hide everything else.
66 * This is important to limit the impact of our "exec" pledge.
68 static const struct got_error *
69 apply_unveil(const char *myserver)
71 const char *fetchcmd = GITWRAPPER_GIT_LIBEXEC_DIR "/" \
73 const char *sendcmd = GITWRAPPER_GIT_LIBEXEC_DIR "/" \
77 if (unveil("gmon.out", "rwc") != 0)
78 return got_error_from_errno2("unveil", "gmon.out");
80 if (unveil(fetchcmd, "x") != 0)
81 return got_error_from_errno2("unveil", fetchcmd);
83 if (unveil(sendcmd, "x") != 0)
84 return got_error_from_errno2("unveil", sendcmd);
86 if (myserver && unveil(myserver, "x") != 0)
87 return got_error_from_errno2("unveil", myserver);
89 if (unveil(NULL, NULL) != 0)
90 return got_error_from_errno("unveil");
96 main(int argc, char *argv[])
98 const struct got_error *error;
99 const char *confpath = NULL;
100 char *command = NULL, *repo_name = NULL; /* for matching gotd.conf */
101 char *myserver = NULL;
102 const char *repo_path = NULL; /* as passed on the command line */
104 char *gitcommand = NULL;
106 struct gotd_repo *repo = NULL;
108 log_init(1, LOG_USER); /* Log to stderr. */
111 if (pledge("stdio rpath exec unveil", NULL) == -1)
116 * Look up our own server program in PATH so we can unveil(2) it.
117 * This call only errors out upon memory allocation failure.
118 * If the program cannot be found then myserver will be set to NULL.
120 error = got_path_find_prog(&myserver, GITWRAPPER_MY_SERVER_PROG);
125 * Run parse_config() before unveil(2) because parse_config()
126 * checks whether repository paths exist on disk.
127 * Parsing errors and warnings will be logged to stderr.
128 * Upon failure we will run Git's native tooling so do not
129 * bother checking for errors here.
131 confpath = getenv("GOTD_CONF_PATH");
132 if (confpath == NULL)
133 confpath = GOTD_CONF_PATH;
134 parse_config(confpath, PROC_GOTD, &gotd, 0);
136 error = apply_unveil(myserver);
141 if (pledge("stdio exec", NULL) == -1)
145 if (strcmp(getprogname(), GOT_DIAL_CMD_SEND) == 0 ||
146 strcmp(getprogname(), GOT_DIAL_CMD_FETCH) == 0) {
149 command = strdup(getprogname());
150 if (command == NULL) {
151 error = got_error_from_errno("strdup");
156 while (relpath[0] == '/')
158 repo_name = strdup(relpath);
159 if (repo_name == NULL) {
160 error = got_error_from_errno("strdup");
164 if (argc != 3 || strcmp(argv[1], "-c") != 0)
167 error = got_dial_parse_command(&command, &repo_name,
169 if (error && error->code == GOT_ERR_BAD_PACKET)
175 repo = gotd_find_repo_by_name(repo_name, &gotd);
178 * Invoke our custom Git server if the repository was found
179 * in gotd.conf. Otherwise invoke native git(1) tooling.
182 if (myserver == NULL) {
183 error = got_error_fmt(GOT_ERR_NO_PROG,
185 GITWRAPPER_MY_SERVER_PROG);
188 execl(myserver, command, repo_name, (char *)NULL);
189 error = got_error_from_errno2("execl", myserver);
191 if (asprintf(&gitcommand, "%s/%s",
192 GITWRAPPER_GIT_LIBEXEC_DIR, command) == -1) {
193 error = got_error_from_errno("asprintf");
196 execl(gitcommand, gitcommand, repo_path, (char *)NULL);
197 error = got_error_from_errno2("execl", gitcommand);
206 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);