main.c revision 151754
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 151754 2005-10-27 18:34:51Z murray $");
23
24#include <err.h>
25#include <sys/param.h>
26#include <sys/utsname.h>
27#include "lib.h"
28#include "add.h"
29
30static char Options[] = "hvIRfnrp:SMt:C:";
31
32char	*Prefix		= NULL;
33char	*Chroot		= NULL;
34Boolean	NoInstall	= FALSE;
35Boolean	NoRecord	= FALSE;
36Boolean Remote		= FALSE;
37
38char	*Mode		= NULL;
39char	*Owner		= NULL;
40char	*Group		= NULL;
41char	*PkgName	= NULL;
42char	*PkgAddCmd	= NULL;
43char	*Directory	= NULL;
44char	FirstPen[FILENAME_MAX];
45add_mode_t AddMode	= NORMAL;
46
47#define MAX_PKGS	200
48char	pkgnames[MAX_PKGS][MAXPATHLEN];
49char	*pkgs[MAX_PKGS];
50
51struct {
52	int lowver;	/* Lowest version number to match */
53	int hiver;	/* Highest version number to match */
54	const char *directory;	/* Directory it lives in */
55} releases[] = {
56	{ 410000, 410000, "/packages-4.1-release" },
57	{ 420000, 420000, "/packages-4.2-release" },
58	{ 430000, 430000, "/packages-4.3-release" },
59	{ 440000, 440000, "/packages-4.4-release" },
60	{ 450000, 450000, "/packages-4.5-release" },
61	{ 460000, 460001, "/packages-4.6-release" },
62	{ 460002, 460099, "/packages-4.6.2-release" },
63	{ 470000, 470099, "/packages-4.7-release" },
64	{ 480000, 480099, "/packages-4.8-release" },
65	{ 490000, 490099, "/packages-4.9-release" },
66	{ 491000, 491099, "/packages-4.10-release" },
67	{ 492000, 492099, "/packages-4.11-release" },
68	{ 500000, 500099, "/packages-5.0-release" },
69	{ 501000, 501099, "/packages-5.1-release" },
70	{ 502000, 502009, "/packages-5.2-release" },
71	{ 502010, 502099, "/packages-5.2.1-release" },
72	{ 503000, 503099, "/packages-5.3-release" },
73	{ 504000, 504099, "/packages-5.4-release" },
74	{ 600000, 600099, "/packages-6.0-release" },
75	{ 300000, 399000, "/packages-3-stable" },
76	{ 400000, 499000, "/packages-4-stable" },
77	{ 502100, 502128, "/packages-5-current" },
78	{ 503100, 599000, "/packages-5-stable" },
79	{ 600100, 699000, "/packages-6-stable" },
80	{ 700000, 799000, "/packages-7-current" },
81	{ 0, 9999999, "/packages-current" },
82	{ 0, 0, NULL }
83};
84
85static char *getpackagesite(void);
86int getosreldate(void);
87
88static void usage __P((void));
89
90int
91main(int argc, char **argv)
92{
93    int ch, error;
94    char **start;
95    char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr;
96    static char temppackageroot[MAXPATHLEN];
97    static char pkgaddpath[MAXPATHLEN];
98
99    if (*argv[0] != '/' && strchr(argv[0], '/') != NULL)
100	PkgAddCmd = realpath(argv[0], pkgaddpath);
101    else
102	PkgAddCmd = argv[0];
103
104    start = argv;
105    while ((ch = getopt(argc, argv, Options)) != -1) {
106	switch(ch) {
107	case 'v':
108	    Verbose = TRUE;
109	    break;
110
111	case 'p':
112	    Prefix = optarg;
113	    break;
114
115	case 'I':
116	    NoInstall = TRUE;
117	    break;
118
119	case 'R':
120	    NoRecord = TRUE;
121	    break;
122
123	case 'f':
124	    Force = TRUE;
125	    break;
126
127	case 'n':
128	    Fake = TRUE;
129	    Verbose = TRUE;
130	    break;
131
132	case 'r':
133	    Remote = TRUE;
134	    break;
135
136	case 't':
137	    if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen))
138		errx(1, "-t Argument too long.");
139	    break;
140
141	case 'S':
142	    AddMode = SLAVE;
143	    break;
144
145	case 'M':
146	    AddMode = MASTER;
147	    break;
148
149	case 'C':
150	    Chroot = optarg;
151	    break;
152
153	case 'h':
154	case '?':
155	default:
156	    usage();
157	    break;
158	}
159    }
160    argc -= optind;
161    argv += optind;
162
163    if (argc > MAX_PKGS) {
164	errx(1, "too many packages (max %d)", MAX_PKGS);
165    }
166
167    if (AddMode != SLAVE) {
168	for (ch = 0; ch < MAX_PKGS; pkgs[ch++] = NULL) ;
169
170	/* Get all the remaining package names, if any */
171	for (ch = 0; *argv; ch++, argv++) {
172    	    if (Remote) {
173		if ((packagesite = getpackagesite()) == NULL)
174		    errx(1, "package name too long");
175		if (strlcpy(temppackageroot, packagesite,
176		    sizeof(temppackageroot)) >= sizeof(temppackageroot))
177		    errx(1, "package name too long");
178		if (strlcat(temppackageroot, *argv, sizeof(temppackageroot))
179		    >= sizeof(temppackageroot))
180		    errx(1, "package name too long");
181		remotepkg = temppackageroot;
182		if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' &&
183			(ptr[2] == 'b' || ptr[2] == 'g') && ptr[3] == 'z' &&
184			!ptr[4]))
185		    if (strlcat(remotepkg,
186#if defined(__FreeBSD_version) && __FreeBSD_version >= 500039
187			".tbz",
188#else
189			".tgz",
190#endif
191			sizeof(temppackageroot)) >= sizeof(temppackageroot))
192			errx(1, "package name too long");
193    	    }
194	    if (!strcmp(*argv, "-"))	/* stdin? */
195		pkgs[ch] = (char *)"-";
196	    else if (isURL(*argv)) {  	/* preserve URLs */
197		if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
198		    >= sizeof(pkgnames[ch]))
199		    errx(1, "package name too long");
200		pkgs[ch] = pkgnames[ch];
201	    }
202	    else if ((Remote) && isURL(remotepkg)) {
203	    	if (strlcpy(pkgnames[ch], remotepkg, sizeof(pkgnames[ch]))
204		    >= sizeof(pkgnames[ch]))
205		    errx(1, "package name too long");
206		pkgs[ch] = pkgnames[ch];
207	    } else {			/* expand all pathnames to fullnames */
208		if (fexists(*argv)) /* refers to a file directly */
209		    pkgs[ch] = realpath(*argv, pkgnames[ch]);
210		else {		/* look for the file in the expected places */
211		    if (!(cp = fileFindByPath(NULL, *argv))) {
212			/* let pkg_do() fail later, so that error is reported */
213			if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
214			    >= sizeof(pkgnames[ch]))
215			    errx(1, "package name too long");
216			pkgs[ch] = pkgnames[ch];
217		    } else {
218			if (strlcpy(pkgnames[ch], cp, sizeof(pkgnames[ch]))
219			    >= sizeof(pkgnames[ch]))
220			    errx(1, "package name too long");
221			pkgs[ch] = pkgnames[ch];
222		    }
223		}
224	    }
225	    if (packagesite != NULL)
226		packagesite[0] = '\0';
227	}
228    }
229    /* If no packages, yelp */
230    else if (!ch) {
231	warnx("missing package name(s)");
232	usage();
233    }
234    else if (ch > 1 && AddMode == MASTER) {
235	warnx("only one package name may be specified with master mode");
236	usage();
237    }
238    /* Perform chroot if requested */
239    if (Chroot != NULL) {
240	if (chroot(Chroot))
241	    errx(1, "chroot to %s failed", Chroot);
242    }
243    /* Make sure the sub-execs we invoke get found */
244    setenv("PATH",
245	   "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin",
246	   1);
247
248    /* Set a reasonable umask */
249    umask(022);
250
251    if ((error = pkg_perform(pkgs)) != 0) {
252	if (Verbose)
253	    warnx("%d package addition(s) failed", error);
254	return error;
255    }
256    else
257	return 0;
258}
259
260static char *
261getpackagesite(void)
262{
263    int reldate, i;
264    static char sitepath[MAXPATHLEN];
265    struct utsname u;
266
267    if (getenv("PACKAGESITE")) {
268	if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath))
269	    >= sizeof(sitepath))
270	    return NULL;
271	return sitepath;
272    }
273
274    if (getenv("PACKAGEROOT")) {
275	if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))
276	    >= sizeof(sitepath))
277	    return NULL;
278    } else {
279	if (strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath))
280	    >= sizeof(sitepath))
281	    return NULL;
282    }
283
284    if (strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath))
285	>= sizeof(sitepath))
286	return NULL;
287
288    uname(&u);
289    if (strlcat(sitepath, u.machine, sizeof(sitepath)) >= sizeof(sitepath))
290	return NULL;
291
292    reldate = getosreldate();
293    for(i = 0; releases[i].directory != NULL; i++) {
294	if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) {
295	    if (strlcat(sitepath, releases[i].directory, sizeof(sitepath))
296		>= sizeof(sitepath))
297		return NULL;
298	    break;
299	}
300    }
301
302    if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath))
303	return NULL;
304
305    return sitepath;
306
307}
308
309static void
310usage()
311{
312    fprintf(stderr, "%s\n%s\n",
313	"usage: pkg_add [-vInrfRMS] [-t template] [-p prefix] [-C chrootdir]",
314	"               pkg-name [pkg-name ...]");
315    exit(1);
316}
317