2 50b0790e 2020-09-11 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
4 50b0790e 2020-09-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 50b0790e 2020-09-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 50b0790e 2020-09-11 stsp * copyright notice and this permission notice appear in all copies.
8 50b0790e 2020-09-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 50b0790e 2020-09-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 50b0790e 2020-09-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 50b0790e 2020-09-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 50b0790e 2020-09-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 50b0790e 2020-09-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 50b0790e 2020-09-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 50b0790e 2020-09-11 stsp #include <sys/types.h>
18 50b0790e 2020-09-11 stsp #include <sys/queue.h>
19 50b0790e 2020-09-11 stsp #include <sys/uio.h>
20 50b0790e 2020-09-11 stsp #include <sys/socket.h>
22 50b0790e 2020-09-11 stsp #include <unistd.h>
23 50b0790e 2020-09-11 stsp #include <fcntl.h>
24 50b0790e 2020-09-11 stsp #include <errno.h>
25 50b0790e 2020-09-11 stsp #include <stdlib.h>
26 50b0790e 2020-09-11 stsp #include <stdio.h>
27 50b0790e 2020-09-11 stsp #include <stdint.h>
28 50b0790e 2020-09-11 stsp #include <imsg.h>
29 50b0790e 2020-09-11 stsp #include <sha1.h>
30 50b0790e 2020-09-11 stsp #include <limits.h>
32 50b0790e 2020-09-11 stsp #include "got_error.h"
33 50b0790e 2020-09-11 stsp #include "got_object.h"
34 50b0790e 2020-09-11 stsp #include "got_repository.h"
36 50b0790e 2020-09-11 stsp #include "got_lib_delta.h"
37 50b0790e 2020-09-11 stsp #include "got_lib_object.h"
38 50b0790e 2020-09-11 stsp #include "got_lib_privsep.h"
39 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
41 50b0790e 2020-09-11 stsp const struct got_error *
42 50b0790e 2020-09-11 stsp got_gotconfig_read(struct got_gotconfig **conf, const char *gotconfig_path)
44 50b0790e 2020-09-11 stsp const struct got_error *err = NULL, *child_err = NULL;
45 50b0790e 2020-09-11 stsp int fd = -1;
46 50b0790e 2020-09-11 stsp int imsg_fds[2] = { -1, -1 };
48 50b0790e 2020-09-11 stsp struct imsgbuf *ibuf;
50 50b0790e 2020-09-11 stsp *conf = calloc(1, sizeof(**conf));
51 50b0790e 2020-09-11 stsp if (*conf == NULL)
52 50b0790e 2020-09-11 stsp return got_error_from_errno("calloc");
54 50b0790e 2020-09-11 stsp fd = open(gotconfig_path, O_RDONLY);
55 50b0790e 2020-09-11 stsp if (fd == -1) {
56 50b0790e 2020-09-11 stsp if (errno == ENOENT)
57 50b0790e 2020-09-11 stsp return NULL;
58 50b0790e 2020-09-11 stsp return got_error_from_errno2("open", gotconfig_path);
61 50b0790e 2020-09-11 stsp ibuf = calloc(1, sizeof(*ibuf));
62 50b0790e 2020-09-11 stsp if (ibuf == NULL) {
63 50b0790e 2020-09-11 stsp err = got_error_from_errno("calloc");
67 50b0790e 2020-09-11 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
68 50b0790e 2020-09-11 stsp err = got_error_from_errno("socketpair");
72 50b0790e 2020-09-11 stsp pid = fork();
73 50b0790e 2020-09-11 stsp if (pid == -1) {
74 50b0790e 2020-09-11 stsp err = got_error_from_errno("fork");
76 50b0790e 2020-09-11 stsp } else if (pid == 0) {
77 50b0790e 2020-09-11 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GOTCONFIG,
78 50b0790e 2020-09-11 stsp gotconfig_path);
79 50b0790e 2020-09-11 stsp /* not reached */
82 50b0790e 2020-09-11 stsp if (close(imsg_fds[1]) == -1) {
83 50b0790e 2020-09-11 stsp err = got_error_from_errno("close");
86 50b0790e 2020-09-11 stsp imsg_fds[1] = -1;
87 50b0790e 2020-09-11 stsp imsg_init(ibuf, imsg_fds[0]);
89 50b0790e 2020-09-11 stsp err = got_privsep_send_gotconfig_parse_req(ibuf, fd);
94 50b0790e 2020-09-11 stsp err = got_privsep_send_gotconfig_author_req(ibuf);
98 50b0790e 2020-09-11 stsp err = got_privsep_recv_gotconfig_str(&(*conf)->author, ibuf);
102 50b0790e 2020-09-11 stsp err = got_privsep_send_gotconfig_remotes_req(ibuf);
106 50b0790e 2020-09-11 stsp err = got_privsep_recv_gotconfig_remotes(&(*conf)->remotes,
107 50b0790e 2020-09-11 stsp &(*conf)->nremotes, ibuf);
111 50b0790e 2020-09-11 stsp imsg_clear(ibuf);
112 50b0790e 2020-09-11 stsp err = got_privsep_send_stop(imsg_fds[0]);
113 50b0790e 2020-09-11 stsp child_err = got_privsep_wait_for_child(pid);
114 50b0790e 2020-09-11 stsp if (child_err && err == NULL)
115 50b0790e 2020-09-11 stsp err = child_err;
117 50b0790e 2020-09-11 stsp if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
118 50b0790e 2020-09-11 stsp err = got_error_from_errno("close");
119 50b0790e 2020-09-11 stsp if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
120 50b0790e 2020-09-11 stsp err = got_error_from_errno("close");
121 50b0790e 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
122 50b0790e 2020-09-11 stsp err = got_error_from_errno2("close", gotconfig_path);
124 50b0790e 2020-09-11 stsp got_gotconfig_free(*conf);
125 50b0790e 2020-09-11 stsp *conf = NULL;
127 50b0790e 2020-09-11 stsp free(ibuf);
128 50b0790e 2020-09-11 stsp return err;
132 50b0790e 2020-09-11 stsp got_gotconfig_free(struct got_gotconfig *conf)
136 a9705505 2020-09-18 stsp if (conf == NULL)
139 50b0790e 2020-09-11 stsp free(conf->author);
141 50b0790e 2020-09-11 stsp for (i = 0; i < conf->nremotes; i++) {
142 50b0790e 2020-09-11 stsp free(conf->remotes[i].name);
143 50b0790e 2020-09-11 stsp free(conf->remotes[i].url);
145 50b0790e 2020-09-11 stsp free(conf->remotes);
146 50b0790e 2020-09-11 stsp free(conf);
149 50b0790e 2020-09-11 stsp const char *
150 50b0790e 2020-09-11 stsp got_gotconfig_get_author(const struct got_gotconfig *conf)
152 50b0790e 2020-09-11 stsp return conf->author;
156 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(int *nremotes, const struct got_remote_repo **remotes,
157 50b0790e 2020-09-11 stsp const struct got_gotconfig *conf)
159 50b0790e 2020-09-11 stsp *nremotes = conf->nremotes;
160 50b0790e 2020-09-11 stsp *remotes = conf->remotes;