Blob


1 /*
2 * Copyright (c) 2022 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 #include <sys/queue.h>
18 #include <sys/socket.h>
19 #include <sys/un.h>
21 #include <err.h>
22 #include <event.h>
23 #include <imsg.h>
24 #include <limits.h>
25 #include <locale.h>
26 #include <sha1.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <getopt.h>
31 #include <unistd.h>
33 #include "got_error.h"
34 #include "got_version.h"
36 #include "got_lib_gitproto.h"
38 #include "gotd.h"
40 #ifndef nitems
41 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
42 #endif
44 #define GOTCTL_CMD_INFO "info"
45 #define GOTCTL_CMD_STOP "stop"
47 struct gotctl_cmd {
48 const char *cmd_name;
49 const struct got_error *(*cmd_main)(int, char *[], int);
50 void (*cmd_usage)(void);
51 };
53 __dead static void usage(int, int);
55 __dead static void usage_info(void);
56 __dead static void usage_stop(void);
58 static const struct got_error* cmd_info(int, char *[], int);
59 static const struct got_error* cmd_stop(int, char *[], int);
61 static const struct gotctl_cmd gotctl_commands[] = {
62 { "info", cmd_info, usage_info },
63 { "stop", cmd_stop, usage_stop },
64 };
66 __dead static void
67 usage_info(void)
68 {
69 fprintf(stderr, "usage: %s info\n", getprogname());
70 exit(1);
71 }
73 static const struct got_error *
74 show_info(struct imsg *imsg)
75 {
76 struct gotd_imsg_info info;
77 size_t datalen;
79 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
80 if (datalen != sizeof(info))
81 return got_error(GOT_ERR_PRIVSEP_LEN);
82 memcpy(&info, imsg->data, sizeof(info));
84 printf("gotd PID: %d\n", info.pid);
85 printf("verbosity: %d\n", info.verbosity);
86 printf("number of repositories: %d\n", info.nrepos);
87 printf("number of connected clients: %d\n", info.nclients);
88 return NULL;
89 }
91 static const struct got_error *
92 show_repo_info(struct imsg *imsg)
93 {
94 struct gotd_imsg_info_repo info;
95 size_t datalen;
97 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
98 if (datalen != sizeof(info))
99 return got_error(GOT_ERR_PRIVSEP_LEN);
100 memcpy(&info, imsg->data, sizeof(info));
102 printf("repository \"%s\", path %s\n", info.repo_name, info.repo_path);
103 return NULL;
106 static const char *
107 get_state_name(enum gotd_client_state state)
109 static char unknown_state[64];
111 switch (state) {
112 case GOTD_STATE_EXPECT_LIST_REFS:
113 return "list-refs";
114 case GOTD_STATE_EXPECT_CAPABILITIES:
115 return "expect-capabilities";
116 case GOTD_STATE_EXPECT_WANT:
117 return "expect-want";
118 case GOTD_STATE_EXPECT_REF_UPDATE:
119 return "expect-ref-update";
120 case GOTD_STATE_EXPECT_MORE_REF_UPDATES:
121 return "expect-more-ref-updates";
122 case GOTD_STATE_EXPECT_HAVE:
123 return "expect-have";
124 case GOTD_STATE_EXPECT_PACKFILE:
125 return "expect-packfile";
126 case GOTD_STATE_EXPECT_DONE:
127 return "expect-done";
128 case GOTD_STATE_DONE:
129 return "done";
132 snprintf(unknown_state, sizeof(unknown_state),
133 "unknown state %d", state);
134 return unknown_state;
137 static const struct got_error *
138 show_client_info(struct imsg *imsg)
140 struct gotd_imsg_info_client info;
141 size_t datalen;
143 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
144 if (datalen != sizeof(info))
145 return got_error(GOT_ERR_PRIVSEP_LEN);
146 memcpy(&info, imsg->data, sizeof(info));
148 printf("client UID %d, GID %d, protocol state '%s', ",
149 info.euid, info.egid, get_state_name(info.state));
150 if (info.session_child_pid)
151 printf("session PID %ld, ", (long)info.session_child_pid);
152 if (info.repo_child_pid)
153 printf("repo PID %ld, ", (long)info.repo_child_pid);
154 if (info.is_writing)
155 printf("writing to %s\n", info.repo_name);
156 else
157 printf("reading from %s\n", info.repo_name);
159 return NULL;
162 static const struct got_error *
163 show_capability(struct imsg *imsg)
165 struct gotd_imsg_capability icapa;
166 size_t datalen;
167 char *key, *value = NULL;
169 memset(&icapa, 0, sizeof(icapa));
171 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
172 if (datalen < sizeof(icapa))
173 return got_error(GOT_ERR_PRIVSEP_LEN);
174 memcpy(&icapa, imsg->data, sizeof(icapa));
176 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
177 return got_error(GOT_ERR_PRIVSEP_LEN);
179 key = malloc(icapa.key_len + 1);
180 if (key == NULL)
181 return got_error_from_errno("malloc");
182 if (icapa.value_len > 0) {
183 value = malloc(icapa.value_len + 1);
184 if (value == NULL) {
185 free(key);
186 return got_error_from_errno("malloc");
190 memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
191 key[icapa.key_len] = '\0';
192 if (value) {
193 memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
194 icapa.value_len);
195 value[icapa.value_len] = '\0';
198 if (strcmp(key, GOT_CAPA_AGENT) == 0)
199 printf(" client user agent: %s\n", value);
200 else if (value)
201 printf(" client supports %s=%s\n", key, value);
202 else
203 printf(" client supports %s\n", key);
205 free(key);
206 free(value);
207 return NULL;
210 static const struct got_error *
211 cmd_info(int argc, char *argv[], int gotd_sock)
213 const struct got_error *err;
214 struct imsgbuf ibuf;
215 struct imsg imsg;
217 imsg_init(&ibuf, gotd_sock);
219 if (imsg_compose(&ibuf, GOTD_IMSG_INFO, 0, 0, -1, NULL, 0) == -1)
220 return got_error_from_errno("imsg_compose INFO");
222 err = gotd_imsg_flush(&ibuf);
223 while (err == NULL) {
224 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
225 if (err) {
226 if (err->code == GOT_ERR_EOF)
227 err = NULL;
228 break;
231 switch (imsg.hdr.type) {
232 case GOTD_IMSG_ERROR:
233 err = gotd_imsg_recv_error(NULL, &imsg);
234 break;
235 case GOTD_IMSG_INFO:
236 err = show_info(&imsg);
237 break;
238 case GOTD_IMSG_INFO_REPO:
239 err = show_repo_info(&imsg);
240 break;
241 case GOTD_IMSG_INFO_CLIENT:
242 err = show_client_info(&imsg);
243 break;
244 case GOTD_IMSG_CAPABILITY:
245 err = show_capability(&imsg);
246 break;
247 default:
248 err = got_error(GOT_ERR_PRIVSEP_MSG);
249 break;
252 imsg_free(&imsg);
255 imsg_clear(&ibuf);
256 return err;
259 __dead static void
260 usage_stop(void)
262 fprintf(stderr, "usage: %s stop\n", getprogname());
263 exit(1);
266 static const struct got_error *
267 cmd_stop(int argc, char *argv[], int gotd_sock)
269 const struct got_error *err;
270 struct imsgbuf ibuf;
271 struct imsg imsg;
273 imsg_init(&ibuf, gotd_sock);
275 if (imsg_compose(&ibuf, GOTD_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
276 return got_error_from_errno("imsg_compose STOP");
278 err = gotd_imsg_flush(&ibuf);
279 while (err == NULL) {
280 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
281 if (err) {
282 if (err->code == GOT_ERR_EOF)
283 err = NULL;
284 break;
287 switch (imsg.hdr.type) {
288 case GOTD_IMSG_ERROR:
289 err = gotd_imsg_recv_error(NULL, &imsg);
290 break;
291 default:
292 err = got_error(GOT_ERR_PRIVSEP_MSG);
293 break;
296 imsg_free(&imsg);
299 imsg_clear(&ibuf);
300 return err;
303 static void
304 list_commands(FILE *fp)
306 size_t i;
308 fprintf(fp, "commands:");
309 for (i = 0; i < nitems(gotctl_commands); i++) {
310 const struct gotctl_cmd *cmd = &gotctl_commands[i];
311 fprintf(fp, " %s", cmd->cmd_name);
313 fputc('\n', fp);
316 __dead static void
317 usage(int hflag, int status)
319 FILE *fp = (status == 0) ? stdout : stderr;
321 fprintf(fp, "usage: %s [-hV] [-f path] command [arg ...]\n",
322 getprogname());
323 if (hflag)
324 list_commands(fp);
325 exit(status);
328 static const struct got_error *
329 apply_unveil(const char *unix_socket_path)
331 #ifdef PROFILE
332 if (unveil("gmon.out", "rwc") != 0)
333 return got_error_from_errno2("unveil", "gmon.out");
334 #endif
335 if (unveil(unix_socket_path, "w") != 0)
336 return got_error_from_errno2("unveil", unix_socket_path);
338 if (unveil(NULL, NULL) != 0)
339 return got_error_from_errno("unveil");
341 return NULL;
344 static int
345 connect_gotd(const char *socket_path)
347 const struct got_error *error = NULL;
348 int gotd_sock = -1;
349 struct sockaddr_un sun;
351 error = apply_unveil(socket_path);
352 if (error)
353 errx(1, "%s", error->msg);
355 #ifndef PROFILE
356 if (pledge("stdio unix", NULL) == -1)
357 err(1, "pledge");
358 #endif
359 if ((gotd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
360 err(1, "socket");
362 memset(&sun, 0, sizeof(sun));
363 sun.sun_family = AF_UNIX;
364 if (strlcpy(sun.sun_path, socket_path, sizeof(sun.sun_path)) >=
365 sizeof(sun.sun_path))
366 errx(1, "gotd socket path too long");
367 if (connect(gotd_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
368 err(1, "connect: %s", socket_path);
370 #ifndef PROFILE
371 if (pledge("stdio", NULL) == -1)
372 err(1, "pledge");
373 #endif
375 return gotd_sock;
378 int
379 main(int argc, char *argv[])
381 const struct gotctl_cmd *cmd;
382 int gotd_sock = -1, i;
383 int ch;
384 int hflag = 0, Vflag = 0;
385 static const struct option longopts[] = {
386 { "version", no_argument, NULL, 'V' },
387 { NULL, 0, NULL, 0 }
388 };
389 const char *socket_path = GOTD_UNIX_SOCKET;
391 setlocale(LC_CTYPE, "");
393 #ifndef PROFILE
394 if (pledge("stdio unix unveil", NULL) == -1)
395 err(1, "pledge");
396 #endif
398 while ((ch = getopt_long(argc, argv, "+hf:V", longopts, NULL)) != -1) {
399 switch (ch) {
400 case 'h':
401 hflag = 1;
402 break;
403 case 'f':
404 socket_path = optarg;
405 break;
406 case 'V':
407 Vflag = 1;
408 break;
409 default:
410 usage(hflag, 1);
411 /* NOTREACHED */
415 argc -= optind;
416 argv += optind;
417 optind = 1;
418 optreset = 1;
420 if (Vflag) {
421 got_version_print_str();
422 return 0;
425 if (argc <= 0)
426 usage(hflag, hflag ? 0 : 1);
428 for (i = 0; i < nitems(gotctl_commands); i++) {
429 const struct got_error *error;
431 cmd = &gotctl_commands[i];
433 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])) != 0)
434 continue;
436 if (hflag)
437 cmd->cmd_usage();
439 gotd_sock = connect_gotd(socket_path);
440 if (gotd_sock == -1)
441 return 1;
442 error = cmd->cmd_main(argc, argv, gotd_sock);
443 close(gotd_sock);
444 if (error) {
445 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
446 return 1;
449 return 0;
452 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
453 list_commands(stderr);
454 return 1;