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