main.c revision 124054
1110007Smarkm/*
255714Skris *
355714Skris * FreeBSD install - a package for the installation and maintainance
455714Skris * of non-core utilities.
555714Skris *
655714Skris * Redistribution and use in source and binary forms, with or without
755714Skris * modification, are permitted provided that the following conditions
8296341Sdelphij * are met:
955714Skris * 1. Redistributions of source code must retain the above copyright
1055714Skris *    notice, this list of conditions and the following disclaimer.
1155714Skris * 2. Redistributions in binary form must reproduce the above copyright
1255714Skris *    notice, this list of conditions and the following disclaimer in the
1355714Skris *    documentation and/or other materials provided with the distribution.
1455714Skris *
15296341Sdelphij * Jordan K. Hubbard
1655714Skris * 18 July 1993
1755714Skris *
1855714Skris * This is the add module.
1955714Skris */
2055714Skris
2155714Skris#include <sys/cdefs.h>
22296341Sdelphij__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/add/main.c 124054 2004-01-02 00:13:20Z bmah $");
2355714Skris
2455714Skris#include <err.h>
2555714Skris#include <sys/param.h>
2655714Skris#include <sys/utsname.h>
2755714Skris#include "lib.h"
2855714Skris#include "add.h"
2955714Skris
3055714Skrisstatic char Options[] = "hvIRfnrp:SMt:";
3155714Skris
3255714Skrischar	*Prefix		= NULL;
3355714SkrisBoolean	NoInstall	= FALSE;
3455714SkrisBoolean	NoRecord	= FALSE;
3555714SkrisBoolean Remote		= FALSE;
3655714Skris
37296341Sdelphijchar	*Mode		= NULL;
3855714Skrischar	*Owner		= NULL;
3955714Skrischar	*Group		= NULL;
40296341Sdelphijchar	*PkgName	= NULL;
4155714Skrischar	*Directory	= NULL;
4255714Skrischar	FirstPen[FILENAME_MAX];
4355714Skrisadd_mode_t AddMode	= NORMAL;
4455714Skris
4555714Skris#define MAX_PKGS	200
4655714Skrischar	pkgnames[MAX_PKGS][MAXPATHLEN];
4755714Skrischar	*pkgs[MAX_PKGS];
4855714Skris
4955714Skrisstruct {
5055714Skris	int lowver;	/* Lowest version number to match */
5155714Skris	int hiver;	/* Highest version number to match */
52296341Sdelphij	const char *directory;	/* Directory it lives in */
5355714Skris} releases[] = {
5455714Skris	{ 410000, 410000, "/packages-4.1-release" },
5555714Skris	{ 420000, 420000, "/packages-4.2-release" },
5655714Skris	{ 430000, 430000, "/packages-4.3-release" },
5755714Skris	{ 440000, 440000, "/packages-4.4-release" },
58160817Ssimon	{ 450000, 450000, "/packages-4.5-release" },
59160817Ssimon	{ 460000, 460001, "/packages-4.6-release" },
60160817Ssimon	{ 460002, 460099, "/packages-4.6.2-release" },
61296341Sdelphij	{ 470000, 470099, "/packages-4.7-release" },
62160817Ssimon	{ 480000, 480099, "/packages-4.8-release" },
63160817Ssimon	{ 490000, 490099, "/packages-4.9-release" },
64160817Ssimon	{ 500000, 500099, "/packages-5.0-release" },
65160817Ssimon	{ 501000, 501099, "/packages-5.1-release" },
66160817Ssimon	{ 502000, 502099, "/packages-5.2-release" },
67296341Sdelphij	{ 300000, 399000, "/packages-3-stable" },
68160817Ssimon	{ 400000, 499000, "/packages-4-stable" },
69160817Ssimon	{ 502100, 599000, "/packages-5-current" },
70160817Ssimon	{ 0, 9999999, "/packages-current" },
7155714Skris	{ 0, 0, NULL }
7255714Skris};
7355714Skris
74111150Snectarstatic char *getpackagesite(void);
75111150Snectarint getosreldate(void);
76296341Sdelphij
77296341Sdelphijstatic void usage __P((void));
78296341Sdelphij
79296341Sdelphijint
80296341Sdelphijmain(int argc, char **argv)
81296341Sdelphij{
8255714Skris    int ch, error;
8355714Skris    char **start;
8455714Skris    char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr;
8555714Skris    static char temppackageroot[MAXPATHLEN];
86296341Sdelphij
87296341Sdelphij    start = argv;
8855714Skris    while ((ch = getopt(argc, argv, Options)) != -1) {
89296341Sdelphij	switch(ch) {
90296341Sdelphij	case 'v':
91160817Ssimon	    Verbose = TRUE;
92296341Sdelphij	    break;
93296341Sdelphij
94296341Sdelphij	case 'p':
95296341Sdelphij	    Prefix = optarg;
96296341Sdelphij	    break;
97296341Sdelphij
98296341Sdelphij	case 'I':
99296341Sdelphij	    NoInstall = TRUE;
100296341Sdelphij	    break;
101296341Sdelphij
102296341Sdelphij	case 'R':
103296341Sdelphij	    NoRecord = TRUE;
104296341Sdelphij	    break;
105296341Sdelphij
10655714Skris	case 'f':
107296341Sdelphij	    Force = TRUE;
108296341Sdelphij	    break;
109296341Sdelphij
110160817Ssimon	case 'n':
111296341Sdelphij	    Fake = TRUE;
112296341Sdelphij	    Verbose = TRUE;
113296341Sdelphij	    break;
114296341Sdelphij
115296341Sdelphij	case 'r':
116296341Sdelphij	    Remote = TRUE;
117296341Sdelphij	    break;
118296341Sdelphij
119238405Sjkim	case 't':
120296341Sdelphij	    if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen))
121238405Sjkim		errx(1, "-t Argument too long.");
12255714Skris	    break;
123296341Sdelphij
124296341Sdelphij	case 'S':
125296341Sdelphij	    AddMode = SLAVE;
126296341Sdelphij	    break;
127296341Sdelphij
128296341Sdelphij	case 'M':
129296341Sdelphij	    AddMode = MASTER;
130296341Sdelphij	    break;
131296341Sdelphij
132296341Sdelphij	case 'h':
133296341Sdelphij	case '?':
134296341Sdelphij	default:
135296341Sdelphij	    usage();
136296341Sdelphij	    break;
137296341Sdelphij	}
138296341Sdelphij    }
139296341Sdelphij    argc -= optind;
140296341Sdelphij    argv += optind;
141296341Sdelphij
142296341Sdelphij    if (argc > MAX_PKGS) {
143296341Sdelphij	errx(1, "too many packages (max %d)", MAX_PKGS);
144296341Sdelphij    }
145296341Sdelphij
146296341Sdelphij    if (AddMode != SLAVE) {
147296341Sdelphij	for (ch = 0; ch < MAX_PKGS; pkgs[ch++] = NULL) ;
148296341Sdelphij
149296341Sdelphij	/* Get all the remaining package names, if any */
150296341Sdelphij	for (ch = 0; *argv; ch++, argv++) {
151296341Sdelphij    	    if (Remote) {
152296341Sdelphij		if ((packagesite = getpackagesite()) == NULL)
153296341Sdelphij		    errx(1, "package name too long");
154296341Sdelphij		if (strlcpy(temppackageroot, packagesite,
155296341Sdelphij		    sizeof(temppackageroot)) >= sizeof(temppackageroot))
156296341Sdelphij		    errx(1, "package name too long");
157296341Sdelphij		if (strlcat(temppackageroot, *argv, sizeof(temppackageroot))
158296341Sdelphij		    >= sizeof(temppackageroot))
159296341Sdelphij		    errx(1, "package name too long");
160296341Sdelphij		remotepkg = temppackageroot;
161296341Sdelphij		if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' &&
162296341Sdelphij			(ptr[2] == 'b' || ptr[2] == 'g') && ptr[3] == 'z' &&
163296341Sdelphij			!ptr[4]))
164296341Sdelphij		    /* XXX: need to handle .tgz also */
165296341Sdelphij		    if (strlcat(remotepkg, ".tbz", sizeof(temppackageroot))
166296341Sdelphij			>= sizeof(temppackageroot))
167296341Sdelphij			errx(1, "package name too long");
168296341Sdelphij    	    }
169296341Sdelphij	    if (!strcmp(*argv, "-"))	/* stdin? */
170296341Sdelphij		(const char *)pkgs[ch] = "-";
171296341Sdelphij	    else if (isURL(*argv)) {  	/* preserve URLs */
172296341Sdelphij		if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
173296341Sdelphij		    >= sizeof(pkgnames[ch]))
174296341Sdelphij		    errx(1, "package name too long");
175296341Sdelphij		pkgs[ch] = pkgnames[ch];
176296341Sdelphij	    }
177296341Sdelphij	    else if ((Remote) && isURL(remotepkg)) {
178296341Sdelphij	    	if (strlcpy(pkgnames[ch], remotepkg, sizeof(pkgnames[ch]))
179296341Sdelphij		    >= sizeof(pkgnames[ch]))
180296341Sdelphij		    errx(1, "package name too long");
181296341Sdelphij		pkgs[ch] = pkgnames[ch];
182296341Sdelphij	    } else {			/* expand all pathnames to fullnames */
183296341Sdelphij		if (fexists(*argv)) /* refers to a file directly */
184296341Sdelphij		    pkgs[ch] = realpath(*argv, pkgnames[ch]);
185296341Sdelphij		else {		/* look for the file in the expected places */
186296341Sdelphij		    if (!(cp = fileFindByPath(NULL, *argv))) {
187296341Sdelphij			/* let pkg_do() fail later, so that error is reported */
188296341Sdelphij			if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
189296341Sdelphij			    >= sizeof(pkgnames[ch]))
190296341Sdelphij			    errx(1, "package name too long");
191296341Sdelphij			pkgs[ch] = pkgnames[ch];
192296341Sdelphij		    } else {
193296341Sdelphij			if (strlcpy(pkgnames[ch], cp, sizeof(pkgnames[ch]))
194296341Sdelphij			    >= sizeof(pkgnames[ch]))
19555714Skris			    errx(1, "package name too long");
196296341Sdelphij			pkgs[ch] = pkgnames[ch];
197296341Sdelphij		    }
198296341Sdelphij		}
199296341Sdelphij	    }
200296341Sdelphij	    if (packagesite != NULL)
201296341Sdelphij		packagesite[0] = '\0';
202296341Sdelphij	}
203296341Sdelphij    }
204296341Sdelphij    /* If no packages, yelp */
205296341Sdelphij    else if (!ch) {
206296341Sdelphij	warnx("missing package name(s)");
207296341Sdelphij	usage();
208296341Sdelphij    }
209296341Sdelphij    else if (ch > 1 && AddMode == MASTER) {
210296341Sdelphij	warnx("only one package name may be specified with master mode");
211296341Sdelphij	usage();
212296341Sdelphij    }
21355714Skris    /* Make sure the sub-execs we invoke get found */
214296341Sdelphij    setenv("PATH",
215296341Sdelphij	   "/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin",
216296341Sdelphij	   1);
217296341Sdelphij
218296341Sdelphij    /* Set a reasonable umask */
219296341Sdelphij    umask(022);
220296341Sdelphij
221296341Sdelphij    if ((error = pkg_perform(pkgs)) != 0) {
222296341Sdelphij	if (Verbose)
223296341Sdelphij	    warnx("%d package addition(s) failed", error);
224206046Ssimon	return error;
225296341Sdelphij    }
226206046Ssimon    else
227206046Ssimon	return 0;
228296341Sdelphij}
229296341Sdelphij
230296341Sdelphijstatic char *
231110007Smarkmgetpackagesite(void)
232296341Sdelphij{
233296341Sdelphij    int reldate, i;
23455714Skris    static char sitepath[MAXPATHLEN];
235110007Smarkm    struct utsname u;
236296341Sdelphij
237160817Ssimon    if (getenv("PACKAGESITE")) {
238296341Sdelphij	if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath))
239296341Sdelphij	    >= sizeof(sitepath))
240296341Sdelphij	    return NULL;
241110007Smarkm	return sitepath;
242296341Sdelphij    }
243110007Smarkm
244296341Sdelphij    if (getenv("PACKAGEROOT")) {
245296341Sdelphij	if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))
246296341Sdelphij	    >= sizeof(sitepath))
247296341Sdelphij	    return NULL;
248160817Ssimon    } else {
249296341Sdelphij	if (strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath))
250296341Sdelphij	    >= sizeof(sitepath))
251160817Ssimon	    return NULL;
252296341Sdelphij    }
253296341Sdelphij
254296341Sdelphij    if (strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath))
255296341Sdelphij	>= sizeof(sitepath))
256296341Sdelphij	return NULL;
257296341Sdelphij
258296341Sdelphij    uname(&u);
259296341Sdelphij    if (strlcat(sitepath, u.machine, sizeof(sitepath)) >= sizeof(sitepath))
260296341Sdelphij	return NULL;
261296341Sdelphij
262110007Smarkm    reldate = getosreldate();
263296341Sdelphij    for(i = 0; releases[i].directory != NULL; i++) {
264296341Sdelphij	if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) {
265296341Sdelphij	    if (strlcat(sitepath, releases[i].directory, sizeof(sitepath))
266110007Smarkm		>= sizeof(sitepath))
267296341Sdelphij		return NULL;
268296341Sdelphij	    break;
269110007Smarkm	}
270296341Sdelphij    }
271296341Sdelphij
272160817Ssimon    if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath))
273296341Sdelphij	return NULL;
274296341Sdelphij
275160817Ssimon    return sitepath;
276296341Sdelphij
277110007Smarkm}
278296341Sdelphij
279296341Sdelphijstatic void
280296341Sdelphijusage()
281160817Ssimon{
282296341Sdelphij    fprintf(stderr, "%s\n%s\n",
283160817Ssimon		"usage: pkg_add [-vInrfRMS] [-t template] [-p prefix]",
284296341Sdelphij		"               pkg-name [pkg-name ...]");
285296341Sdelphij    exit(1);
286296341Sdelphij}
287296341Sdelphij