main.c revision 196432
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: head/usr.sbin/pkg_install/add/main.c 196432 2009-08-22 23:44:37Z kensmith $");
23
24#include <sys/param.h>
25#include <sys/utsname.h>
26#include <err.h>
27#include <getopt.h>
28
29#include "lib.h"
30#include "add.h"
31
32char	*Prefix		= NULL;
33Boolean	PrefixRecursive	= FALSE;
34char	*Chroot		= NULL;
35Boolean	NoInstall	= FALSE;
36Boolean	NoRecord	= FALSE;
37Boolean Remote		= FALSE;
38Boolean KeepPackage	= FALSE;
39Boolean FailOnAlreadyInstalled	= TRUE;
40Boolean IgnoreDeps	= FALSE;
41
42char	*Mode		= NULL;
43char	*Owner		= NULL;
44char	*Group		= NULL;
45char	*PkgName	= NULL;
46char	*PkgAddCmd	= NULL;
47char	*Directory	= NULL;
48char	FirstPen[FILENAME_MAX];
49add_mode_t AddMode	= NORMAL;
50
51char	**pkgs;
52
53struct {
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	{ 800000, 800499, "/packages-8.0-release" },
86	{ 300000, 399000, "/packages-3-stable" },
87	{ 400000, 499000, "/packages-4-stable" },
88	{ 502100, 502128, "/packages-5-current" },
89	{ 503100, 599000, "/packages-5-stable" },
90	{ 600100, 699000, "/packages-6-stable" },
91	{ 700100, 799000, "/packages-7-stable" },
92	{ 800000, 899000, "/packages-8-stable" },
93	{ 900000, 999000, "/packages-9-current" },
94	{ 0, 9999999, "/packages-current" },
95	{ 0, 0, NULL }
96};
97
98static char *getpackagesite(void);
99int getosreldate(void);
100
101static void usage(void);
102
103static char opts[] = "hviIRfFnrp:P:SMt:C:K";
104static struct option longopts[] = {
105	{ "chroot",	required_argument,	NULL,		'C' },
106	{ "dry-run",	no_argument,		NULL,		'n' },
107	{ "force",	no_argument,		NULL,		'f' },
108	{ "help",	no_argument,		NULL,		'h' },
109	{ "keep",	no_argument,		NULL,		'K' },
110	{ "master",	no_argument,		NULL,		'M' },
111	{ "no-deps",	no_argument,		NULL,		'i' },
112	{ "no-record",	no_argument,		NULL,		'R' },
113	{ "no-script",	no_argument,		NULL,		'I' },
114	{ "prefix",	required_argument,	NULL,		'p' },
115	{ "remote",	no_argument,		NULL,		'r' },
116	{ "template",	required_argument,	NULL,		't' },
117	{ "slave",	no_argument,		NULL,		'S' },
118	{ "verbose",	no_argument,		NULL,		'v' },
119	{ NULL,		0,			NULL,		0 }
120};
121
122int
123main(int argc, char **argv)
124{
125    int ch, error;
126    char **start;
127    char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr;
128    static char temppackageroot[MAXPATHLEN];
129    static char pkgaddpath[MAXPATHLEN];
130
131    if (*argv[0] != '/' && strchr(argv[0], '/') != NULL)
132	PkgAddCmd = realpath(argv[0], pkgaddpath);
133    else
134	PkgAddCmd = argv[0];
135
136    start = argv;
137    while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
138	switch(ch) {
139	case 'v':
140	    Verbose++;
141	    break;
142
143	case 'p':
144	    Prefix = optarg;
145	    PrefixRecursive = FALSE;
146	    break;
147
148	case 'P':
149	    Prefix = optarg;
150	    PrefixRecursive = TRUE;
151	    break;
152
153	case 'I':
154	    NoInstall = TRUE;
155	    break;
156
157	case 'R':
158	    NoRecord = TRUE;
159	    break;
160
161	case 'f':
162	    Force = TRUE;
163	    break;
164
165	case 'F':
166	    FailOnAlreadyInstalled = FALSE;
167	    break;
168
169	case 'K':
170	    KeepPackage = TRUE;
171	    break;
172
173	case 'n':
174	    Fake = TRUE;
175	    break;
176
177	case 'r':
178	    Remote = TRUE;
179	    break;
180
181	case 't':
182	    if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen))
183		errx(1, "-t Argument too long.");
184	    break;
185
186	case 'S':
187	    AddMode = SLAVE;
188	    break;
189
190	case 'M':
191	    AddMode = MASTER;
192	    break;
193
194	case 'C':
195	    Chroot = optarg;
196	    break;
197
198	case 'i':
199	    IgnoreDeps = TRUE;
200	    break;
201
202	case 'h':
203	default:
204	    usage();
205	    break;
206	}
207    }
208    argc -= optind;
209    argv += optind;
210
211    if (AddMode != SLAVE) {
212	pkgs = (char **)malloc((argc+1) * sizeof(char *));
213	for (ch = 0; ch <= argc; pkgs[ch++] = NULL) ;
214
215	/* Get all the remaining package names, if any */
216	for (ch = 0; *argv; ch++, argv++) {
217	    char temp[MAXPATHLEN];
218    	    if (Remote) {
219		if ((packagesite = getpackagesite()) == NULL)
220		    errx(1, "package name too long");
221		if (strlcpy(temppackageroot, packagesite,
222		    sizeof(temppackageroot)) >= sizeof(temppackageroot))
223		    errx(1, "package name too long");
224		if (strlcat(temppackageroot, *argv, sizeof(temppackageroot))
225		    >= sizeof(temppackageroot))
226		    errx(1, "package name too long");
227		remotepkg = temppackageroot;
228		if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' &&
229			(ptr[2] == 'b' || ptr[2] == 'g') && ptr[3] == 'z' &&
230			!ptr[4]))
231		    if (strlcat(remotepkg, ".tbz",
232			sizeof(temppackageroot)) >= sizeof(temppackageroot))
233			errx(1, "package name too long");
234    	    }
235	    if (!strcmp(*argv, "-"))	/* stdin? */
236		pkgs[ch] = (char *)"-";
237	    else if (isURL(*argv)) {  	/* preserve URLs */
238		if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp))
239		    errx(1, "package name too long");
240		pkgs[ch] = strdup(temp);
241	    }
242	    else if ((Remote) && isURL(remotepkg)) {
243	    	if (strlcpy(temp, remotepkg, sizeof(temp)) >= sizeof(temp))
244		    errx(1, "package name too long");
245		pkgs[ch] = strdup(temp);
246	    } else {			/* expand all pathnames to fullnames */
247		if (fexists(*argv)) /* refers to a file directly */
248		    pkgs[ch] = strdup(realpath(*argv, temp));
249		else {		/* look for the file in the expected places */
250		    if (!(cp = fileFindByPath(NULL, *argv))) {
251			/* let pkg_do() fail later, so that error is reported */
252			if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp))
253			    errx(1, "package name too long");
254			pkgs[ch] = strdup(temp);
255		    } else {
256			if (strlcpy(temp, cp, sizeof(temp)) >= sizeof(temp))
257			    errx(1, "package name too long");
258			pkgs[ch] = strdup(temp);
259		    }
260		}
261	    }
262	    if (packagesite != NULL)
263		packagesite[0] = '\0';
264	}
265    }
266    /* If no packages, yelp */
267    if (!ch) {
268	warnx("missing package name(s)");
269	usage();
270    }
271    else if (ch > 1 && AddMode == MASTER) {
272	warnx("only one package name may be specified with master mode");
273	usage();
274    }
275    /* Perform chroot if requested */
276    if (Chroot != NULL) {
277	if (chroot(Chroot))
278	    errx(1, "chroot to %s failed", Chroot);
279    }
280    /* Make sure the sub-execs we invoke get found */
281    setenv("PATH",
282	   "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin",
283	   1);
284
285    /* Set a reasonable umask */
286    umask(022);
287
288    if ((error = pkg_perform(pkgs)) != 0) {
289	if (Verbose)
290	    warnx("%d package addition(s) failed", error);
291	return error;
292    }
293    else
294	return 0;
295}
296
297static char *
298getpackagesite(void)
299{
300    int reldate, i;
301    static char sitepath[MAXPATHLEN];
302    struct utsname u;
303
304    if (getenv("PACKAGESITE")) {
305	if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath))
306	    >= sizeof(sitepath))
307	    return NULL;
308	return sitepath;
309    }
310
311    if (getenv("PACKAGEROOT")) {
312	if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))
313	    >= sizeof(sitepath))
314	    return NULL;
315    } else {
316	if (strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath))
317	    >= sizeof(sitepath))
318	    return NULL;
319    }
320
321    if (strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath))
322	>= sizeof(sitepath))
323	return NULL;
324
325    uname(&u);
326    if (strlcat(sitepath, u.machine, sizeof(sitepath)) >= sizeof(sitepath))
327	return NULL;
328
329    reldate = getosreldate();
330    for(i = 0; releases[i].directory != NULL; i++) {
331	if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) {
332	    if (strlcat(sitepath, releases[i].directory, sizeof(sitepath))
333		>= sizeof(sitepath))
334		return NULL;
335	    break;
336	}
337    }
338
339    if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath))
340	return NULL;
341
342    return sitepath;
343
344}
345
346static void
347usage()
348{
349    fprintf(stderr, "%s\n%s\n",
350	"usage: pkg_add [-viInfFrRMSK] [-t template] [-p prefix] [-P prefix] [-C chrootdir]",
351	"               pkg-name [pkg-name ...]");
352    exit(1);
353}
354