]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - usr.sbin/pkg_install/add/main.c
MFC r229304:
[FreeBSD/releng/9.0.git] / usr.sbin / pkg_install / add / main.c
1 /*
2  *
3  * FreeBSD install - a package for the installation and maintainance
4  * of non-core utilities.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * Jordan K. Hubbard
16  * 18 July 1993
17  *
18  * This is the add module.
19  */
20
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23
24 #include <sys/param.h>
25 #include <sys/sysctl.h>
26 #include <err.h>
27 #include <getopt.h>
28
29 #include "lib.h"
30 #include "add.h"
31
32 char    *Prefix         = NULL;
33 Boolean PrefixRecursive = FALSE;
34 char    *Chroot         = NULL;
35 Boolean NoInstall       = FALSE;
36 Boolean NoRecord        = FALSE;
37 Boolean Remote          = FALSE;
38 Boolean KeepPackage     = FALSE;
39 Boolean FailOnAlreadyInstalled  = TRUE;
40 Boolean IgnoreDeps      = FALSE;
41
42 char    *Mode           = NULL;
43 char    *Owner          = NULL;
44 char    *Group          = NULL;
45 char    *PkgName        = NULL;
46 char    *PkgAddCmd      = NULL;
47 char    *Directory      = NULL;
48 char    FirstPen[FILENAME_MAX];
49 add_mode_t AddMode      = NORMAL;
50
51 char    **pkgs;
52
53 struct {
54         int lowver;     /* Lowest version number to match */
55         int hiver;      /* Highest version number to match */
56         const char *directory;  /* Directory it lives in */
57 } releases[] = {
58         { 410000, 410000, "/packages-4.1-release" },
59         { 420000, 420000, "/packages-4.2-release" },
60         { 430000, 430000, "/packages-4.3-release" },
61         { 440000, 440000, "/packages-4.4-release" },
62         { 450000, 450000, "/packages-4.5-release" },
63         { 460000, 460001, "/packages-4.6-release" },
64         { 460002, 460099, "/packages-4.6.2-release" },
65         { 470000, 470099, "/packages-4.7-release" },
66         { 480000, 480099, "/packages-4.8-release" },
67         { 490000, 490099, "/packages-4.9-release" },
68         { 491000, 491099, "/packages-4.10-release" },
69         { 492000, 492099, "/packages-4.11-release" },
70         { 500000, 500099, "/packages-5.0-release" },
71         { 501000, 501099, "/packages-5.1-release" },
72         { 502000, 502009, "/packages-5.2-release" },
73         { 502010, 502099, "/packages-5.2.1-release" },
74         { 503000, 503099, "/packages-5.3-release" },
75         { 504000, 504099, "/packages-5.4-release" },
76         { 505000, 505099, "/packages-5.5-release" },
77         { 600000, 600099, "/packages-6.0-release" },
78         { 601000, 601099, "/packages-6.1-release" },
79         { 602000, 602099, "/packages-6.2-release" },
80         { 603000, 603099, "/packages-6.3-release" },
81         { 604000, 604099, "/packages-6.4-release" },
82         { 700000, 700099, "/packages-7.0-release" },
83         { 701000, 701099, "/packages-7.1-release" },
84         { 702000, 702099, "/packages-7.2-release" },
85         { 703000, 703099, "/packages-7.3-release" },
86         { 704000, 704099, "/packages-7.4-release" },
87         { 800000, 800499, "/packages-8.0-release" },
88         { 801000, 801499, "/packages-8.1-release" },
89         { 802000, 802499, "/packages-8.2-release" },
90         { 900000, 900499, "/packages-9.0-release" },
91         { 300000, 399000, "/packages-3-stable" },
92         { 400000, 499000, "/packages-4-stable" },
93         { 502100, 502128, "/packages-5-current" },
94         { 503100, 599000, "/packages-5-stable" },
95         { 600100, 699000, "/packages-6-stable" },
96         { 700100, 799000, "/packages-7-stable" },
97         { 800500, 899000, "/packages-8-stable" },
98         { 900000, 999000, "/packages-9-current" },
99         { 0, 9999999, "/packages-current" },
100         { 0, 0, NULL }
101 };
102
103 static char *getpackagesite(void);
104 int getosreldate(void);
105
106 static void usage(void);
107
108 static char opts[] = "hviIRfFnrp:P:SMt:C:K";
109 static struct option longopts[] = {
110         { "chroot",     required_argument,      NULL,           'C' },
111         { "dry-run",    no_argument,            NULL,           'n' },
112         { "force",      no_argument,            NULL,           'f' },
113         { "help",       no_argument,            NULL,           'h' },
114         { "keep",       no_argument,            NULL,           'K' },
115         { "master",     no_argument,            NULL,           'M' },
116         { "no-deps",    no_argument,            NULL,           'i' },
117         { "no-record",  no_argument,            NULL,           'R' },
118         { "no-script",  no_argument,            NULL,           'I' },
119         { "prefix",     required_argument,      NULL,           'p' },
120         { "remote",     no_argument,            NULL,           'r' },
121         { "template",   required_argument,      NULL,           't' },
122         { "slave",      no_argument,            NULL,           'S' },
123         { "verbose",    no_argument,            NULL,           'v' },
124         { NULL,         0,                      NULL,           0 }
125 };
126
127 int
128 main(int argc, char **argv)
129 {
130     int ch, error;
131     char **start;
132     char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr;
133     static char temppackageroot[MAXPATHLEN];
134     static char pkgaddpath[MAXPATHLEN];
135
136     if (*argv[0] != '/' && strchr(argv[0], '/') != NULL)
137         PkgAddCmd = realpath(argv[0], pkgaddpath);
138     else
139         PkgAddCmd = argv[0];
140
141     start = argv;
142     while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
143         switch(ch) {
144         case 'v':
145             Verbose++;
146             break;
147
148         case 'p':
149             Prefix = optarg;
150             PrefixRecursive = FALSE;
151             break;
152
153         case 'P':
154             Prefix = optarg;
155             PrefixRecursive = TRUE;
156             break;
157
158         case 'I':
159             NoInstall = TRUE;
160             break;
161
162         case 'R':
163             NoRecord = TRUE;
164             break;
165
166         case 'f':
167             Force = TRUE;
168             break;
169
170         case 'F':
171             FailOnAlreadyInstalled = FALSE;
172             break;
173
174         case 'K':
175             KeepPackage = TRUE;
176             break;
177
178         case 'n':
179             Fake = TRUE;
180             break;
181
182         case 'r':
183             Remote = TRUE;
184             break;
185
186         case 't':
187             if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen))
188                 errx(1, "-t Argument too long.");
189             break;
190
191         case 'S':
192             AddMode = SLAVE;
193             break;
194
195         case 'M':
196             AddMode = MASTER;
197             break;
198
199         case 'C':
200             Chroot = optarg;
201             break;
202
203         case 'i':
204             IgnoreDeps = TRUE;
205             break;
206
207         case 'h':
208         default:
209             usage();
210             break;
211         }
212     }
213     argc -= optind;
214     argv += optind;
215
216     if (AddMode != SLAVE) {
217         pkgs = (char **)malloc((argc+1) * sizeof(char *));
218         for (ch = 0; ch <= argc; pkgs[ch++] = NULL) ;
219
220         /* Get all the remaining package names, if any */
221         for (ch = 0; *argv; ch++, argv++) {
222             char temp[MAXPATHLEN];
223             if (Remote) {
224                 if ((packagesite = getpackagesite()) == NULL)
225                     errx(1, "package name too long");
226                 if (strlcpy(temppackageroot, packagesite,
227                     sizeof(temppackageroot)) >= sizeof(temppackageroot))
228                     errx(1, "package name too long");
229                 if (strlcat(temppackageroot, *argv, sizeof(temppackageroot))
230                     >= sizeof(temppackageroot))
231                     errx(1, "package name too long");
232                 remotepkg = temppackageroot;
233                 if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' &&
234                         (ptr[2] == 'b' || ptr[2] == 'g' || ptr[2] == 'x') &&
235                         ptr[3] == 'z' && !ptr[4]))
236                     if (strlcat(remotepkg, ".tbz",
237                         sizeof(temppackageroot)) >= sizeof(temppackageroot))
238                         errx(1, "package name too long");
239             }
240             if (!strcmp(*argv, "-"))    /* stdin? */
241                 pkgs[ch] = (char *)"-";
242             else if (isURL(*argv)) {    /* preserve URLs */
243                 if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp))
244                     errx(1, "package name too long");
245                 pkgs[ch] = strdup(temp);
246             }
247             else if ((Remote) && isURL(remotepkg)) {
248                 if (strlcpy(temp, remotepkg, sizeof(temp)) >= sizeof(temp))
249                     errx(1, "package name too long");
250                 pkgs[ch] = strdup(temp);
251             } else {                    /* expand all pathnames to fullnames */
252                 if (fexists(*argv)) /* refers to a file directly */
253                     pkgs[ch] = strdup(realpath(*argv, temp));
254                 else {          /* look for the file in the expected places */
255                     if (!(cp = fileFindByPath(NULL, *argv))) {
256                         /* let pkg_do() fail later, so that error is reported */
257                         if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp))
258                             errx(1, "package name too long");
259                         pkgs[ch] = strdup(temp);
260                     } else {
261                         if (strlcpy(temp, cp, sizeof(temp)) >= sizeof(temp))
262                             errx(1, "package name too long");
263                         pkgs[ch] = strdup(temp);
264                     }
265                 }
266             }
267             if (packagesite != NULL)
268                 packagesite[0] = '\0';
269         }
270     }
271     /* If no packages, yelp */
272     if (!ch) {
273         warnx("missing package name(s)");
274         usage();
275     }
276     else if (ch > 1 && AddMode == MASTER) {
277         warnx("only one package name may be specified with master mode");
278         usage();
279     }
280     /* Perform chroot if requested */
281     if (Chroot != NULL) {
282         if (chroot(Chroot))
283             errx(1, "chroot to %s failed", Chroot);
284     }
285     /* Make sure the sub-execs we invoke get found */
286     setenv("PATH", 
287            "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin",
288            1);
289
290     /* Set a reasonable umask */
291     umask(022);
292
293     if ((error = pkg_perform(pkgs)) != 0) {
294         if (Verbose)
295             warnx("%d package addition(s) failed", error);
296         return error;
297     }
298     else
299         return 0;
300 }
301
302 static char *
303 getpackagesite(void)
304 {
305     int reldate, i;
306     static char sitepath[MAXPATHLEN];
307     int archmib[] = { CTL_HW, HW_MACHINE_ARCH };
308     char arch[64];
309     size_t archlen = sizeof(arch);
310
311     if (getenv("PACKAGESITE")) {
312         if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath))
313             >= sizeof(sitepath))
314             return NULL;
315         return sitepath;
316     }
317
318     if (getenv("PACKAGEROOT")) {
319         if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))
320             >= sizeof(sitepath))
321             return NULL;
322     } else {
323         if (strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath))
324             >= sizeof(sitepath))
325             return NULL;
326     }
327
328     if (strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath))
329         >= sizeof(sitepath))
330         return NULL;
331
332     if (sysctl(archmib, 2, arch, &archlen, NULL, 0) == -1)
333         return NULL;
334     arch[archlen-1] = 0;
335     if (strlcat(sitepath, arch, sizeof(sitepath)) >= sizeof(sitepath))
336         return NULL;
337
338     reldate = getosreldate();
339     for(i = 0; releases[i].directory != NULL; i++) {
340         if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) {
341             if (strlcat(sitepath, releases[i].directory, sizeof(sitepath))
342                 >= sizeof(sitepath))
343                 return NULL;
344             break;
345         }
346     }
347
348     if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath))
349         return NULL;
350
351     return sitepath;
352
353 }
354
355 static void
356 usage(void)
357 {
358     fprintf(stderr, "%s\n%s\n",
359         "usage: pkg_add [-viInfFrRMSK] [-t template] [-p prefix] [-P prefix] [-C chrootdir]",
360         "               pkg-name [pkg-name ...]");
361     exit(1);
362 }