]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.sbin/pkg/pkg.c
MFC: r256968, r256971, r256978
[FreeBSD/stable/10.git] / usr.sbin / pkg / pkg.c
1 /*-
2  * Copyright (c) 2012-2013 Baptiste Daroussin <bapt@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/wait.h>
32
33 #include <archive.h>
34 #include <archive_entry.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <fetch.h>
39 #include <paths.h>
40 #include <stdbool.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <time.h>
45 #include <unistd.h>
46
47 #include "dns_utils.h"
48 #include "config.h"
49
50 static int
51 extract_pkg_static(int fd, char *p, int sz)
52 {
53         struct archive *a;
54         struct archive_entry *ae;
55         char *end;
56         int ret, r;
57
58         ret = -1;
59         a = archive_read_new();
60         if (a == NULL) {
61                 warn("archive_read_new");
62                 return (ret);
63         }
64         archive_read_support_filter_all(a);
65         archive_read_support_format_tar(a);
66
67         if (lseek(fd, 0, 0) == -1) {
68                 warn("lseek");
69                 goto cleanup;
70         }
71
72         if (archive_read_open_fd(a, fd, 4096) != ARCHIVE_OK) {
73                 warnx("archive_read_open_fd: %s", archive_error_string(a));
74                 goto cleanup;
75         }
76
77         ae = NULL;
78         while ((r = archive_read_next_header(a, &ae)) == ARCHIVE_OK) {
79                 end = strrchr(archive_entry_pathname(ae), '/');
80                 if (end == NULL)
81                         continue;
82
83                 if (strcmp(end, "/pkg-static") == 0) {
84                         r = archive_read_extract(a, ae,
85                             ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM |
86                             ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL |
87                             ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_XATTR);
88                         strlcpy(p, archive_entry_pathname(ae), sz);
89                         break;
90                 }
91         }
92
93         if (r == ARCHIVE_OK)
94                 ret = 0;
95         else
96                 warnx("fail to extract pkg-static");
97
98 cleanup:
99         archive_read_free(a);
100         return (ret);
101
102 }
103
104 static int
105 install_pkg_static(char *path, char *pkgpath)
106 {
107         int pstat;
108         pid_t pid;
109
110         switch ((pid = fork())) {
111         case -1:
112                 return (-1);
113         case 0:
114                 execl(path, "pkg-static", "add", pkgpath, (char *)NULL);
115                 _exit(1);
116         default:
117                 break;
118         }
119
120         while (waitpid(pid, &pstat, 0) == -1)
121                 if (errno != EINTR)
122                         return (-1);
123
124         if (WEXITSTATUS(pstat))
125                 return (WEXITSTATUS(pstat));
126         else if (WIFSIGNALED(pstat))
127                 return (128 & (WTERMSIG(pstat)));
128         return (pstat);
129 }
130
131 static int
132 bootstrap_pkg(void)
133 {
134         struct url *u;
135         FILE *remote;
136         FILE *config;
137         char *site;
138         struct dns_srvinfo *mirrors, *current;
139         /* To store _https._tcp. + hostname + \0 */
140         char zone[MAXHOSTNAMELEN + 13];
141         char url[MAXPATHLEN];
142         char conf[MAXPATHLEN];
143         char tmppkg[MAXPATHLEN];
144         const char *packagesite, *mirror_type;
145         char buf[10240];
146         char pkgstatic[MAXPATHLEN];
147         int fd, retry, ret, max_retry;
148         struct url_stat st;
149         off_t done, r;
150         time_t now;
151         time_t last;
152
153         done = 0;
154         last = 0;
155         max_retry = 3;
156         ret = -1;
157         remote = NULL;
158         config = NULL;
159         current = mirrors = NULL;
160
161         printf("Bootstrapping pkg please wait\n");
162
163         if (config_string(PACKAGESITE, &packagesite) != 0) {
164                 warnx("No PACKAGESITE defined");
165                 return (-1);
166         }
167         if (config_string(MIRROR_TYPE, &mirror_type) != 0) {
168                 warnx("No MIRROR_TYPE defined");
169                 return (-1);
170         }
171         snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz", packagesite);
172
173         snprintf(tmppkg, MAXPATHLEN, "%s/pkg.txz.XXXXXX",
174             getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
175
176         if ((fd = mkstemp(tmppkg)) == -1) {
177                 warn("mkstemp()");
178                 return (-1);
179         }
180
181         retry = max_retry;
182
183         u = fetchParseURL(url);
184         while (remote == NULL) {
185                 if (retry == max_retry) {
186                         if (strcmp(u->scheme, "file") != 0 &&
187                             strcasecmp(mirror_type, "srv") == 0) {
188                                 snprintf(zone, sizeof(zone),
189                                     "_%s._tcp.%s", u->scheme, u->host);
190                                 mirrors = dns_getsrvinfo(zone);
191                                 current = mirrors;
192                         }
193                 }
194
195                 if (mirrors != NULL) {
196                         strlcpy(u->host, current->host, sizeof(u->host));
197                         u->port = current->port;
198                 }
199
200                 remote = fetchXGet(u, &st, "");
201                 if (remote == NULL) {
202                         --retry;
203                         if (retry <= 0)
204                                 goto fetchfail;
205                         if (mirrors == NULL) {
206                                 sleep(1);
207                         } else {
208                                 current = current->next;
209                                 if (current == NULL)
210                                         current = mirrors;
211                         }
212                 }
213         }
214
215         if (remote == NULL)
216                 goto fetchfail;
217
218         while (done < st.size) {
219                 if ((r = fread(buf, 1, sizeof(buf), remote)) < 1)
220                         break;
221
222                 if (write(fd, buf, r) != r) {
223                         warn("write()");
224                         goto cleanup;
225                 }
226
227                 done += r;
228                 now = time(NULL);
229                 if (now > last || done == st.size)
230                         last = now;
231         }
232
233         if (ferror(remote))
234                 goto fetchfail;
235
236         if ((ret = extract_pkg_static(fd, pkgstatic, MAXPATHLEN)) == 0)
237                 ret = install_pkg_static(pkgstatic, tmppkg);
238
239         snprintf(conf, MAXPATHLEN, "%s/etc/pkg.conf",
240             getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
241
242         if (access(conf, R_OK) == -1) {
243                 site = strrchr(url, '/');
244                 if (site == NULL)
245                         goto cleanup;
246                 site[0] = '\0';
247                 site = strrchr(url, '/');
248                 if (site == NULL)
249                         goto cleanup;
250                 site[0] = '\0';
251
252                 config = fopen(conf, "w+");
253                 if (config == NULL)
254                         goto cleanup;
255                 fprintf(config, "packagesite: %s\n", url);
256                 fclose(config);
257         }
258
259         goto cleanup;
260
261 fetchfail:
262         warnx("Error fetching %s: %s", url, fetchLastErrString);
263         fprintf(stderr, "A pre-built version of pkg could not be found for your system.\n");
264         fprintf(stderr, "Consider changing PACKAGESITE or installing it from ports: 'ports-mgmt/pkg'.\n");
265
266 cleanup:
267         if (remote != NULL)
268                 fclose(remote);
269         close(fd);
270         unlink(tmppkg);
271
272         return (ret);
273 }
274
275 static const char confirmation_message[] =
276 "The package management tool is not yet installed on your system.\n"
277 "Do you want to fetch and install it now? [y/N]: ";
278
279 static int
280 pkg_query_yes_no(void)
281 {
282         int ret, c;
283
284         c = getchar();
285
286         if (c == 'y' || c == 'Y')
287                 ret = 1;
288         else
289                 ret = 0;
290
291         while (c != '\n' && c != EOF)
292                 c = getchar();
293
294         return (ret);
295 }
296
297 int
298 main(__unused int argc, char *argv[])
299 {
300         char pkgpath[MAXPATHLEN];
301         char pkgstatic[MAXPATHLEN];
302         bool yes = false;
303         int fd, ret;
304
305         snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg",
306             getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
307
308         if (access(pkgpath, X_OK) == -1) {
309                 /* 
310                  * To allow 'pkg -N' to be used as a reliable test for whether
311                  * a system is configured to use pkg, don't bootstrap pkg
312                  * when that argument is given as argv[1].
313                  */
314                 if (argv[1] != NULL && strcmp(argv[1], "-N") == 0)
315                         errx(EXIT_FAILURE, "pkg is not installed");
316
317                 if (argc > 2 && strcmp(argv[1], "add") == 0 &&
318                     access(argv[2], R_OK) == 0) {
319                         fd = open(argv[2], O_RDONLY);
320                         if (fd == -1)
321                                 err(EXIT_FAILURE, "Unable to open %s", argv[2]);
322
323                         if ((ret = extract_pkg_static(fd, pkgstatic, MAXPATHLEN)) == 0)
324                                 ret = install_pkg_static(pkgstatic, argv[2]);
325                         close(fd);
326                         if (ret != 0)
327                                 exit(EXIT_FAILURE);
328                         exit(EXIT_SUCCESS);
329                 }
330                 /*
331                  * Do not ask for confirmation if either of stdin or stdout is
332                  * not tty. Check the environment to see if user has answer
333                  * tucked in there already.
334                  */
335                 config_init();
336                 config_bool(ASSUME_ALWAYS_YES, &yes);
337                 if (!yes) {
338                         printf("%s", confirmation_message);
339                         if (!isatty(fileno(stdin)))
340                                 exit(EXIT_FAILURE);
341
342                         if (pkg_query_yes_no() == 0)
343                                 exit(EXIT_FAILURE);
344                 }
345                 if (bootstrap_pkg() != 0)
346                         exit(EXIT_FAILURE);
347                 config_finish();
348         }
349
350         execv(pkgpath, argv);
351
352         /* NOT REACHED */
353         return (EXIT_FAILURE);
354 }