xref: /titanic_50/usr/src/cmd/svr4pkg/installf/installf.c (revision 5c51f1241dbbdf2656d0e10011981411ed0c9673)
1*5c51f124SMoriah Waterland /*
2*5c51f124SMoriah Waterland  * CDDL HEADER START
3*5c51f124SMoriah Waterland  *
4*5c51f124SMoriah Waterland  * The contents of this file are subject to the terms of the
5*5c51f124SMoriah Waterland  * Common Development and Distribution License (the "License").
6*5c51f124SMoriah Waterland  * You may not use this file except in compliance with the License.
7*5c51f124SMoriah Waterland  *
8*5c51f124SMoriah Waterland  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5c51f124SMoriah Waterland  * or http://www.opensolaris.org/os/licensing.
10*5c51f124SMoriah Waterland  * See the License for the specific language governing permissions
11*5c51f124SMoriah Waterland  * and limitations under the License.
12*5c51f124SMoriah Waterland  *
13*5c51f124SMoriah Waterland  * When distributing Covered Code, include this CDDL HEADER in each
14*5c51f124SMoriah Waterland  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5c51f124SMoriah Waterland  * If applicable, add the following below this CDDL HEADER, with the
16*5c51f124SMoriah Waterland  * fields enclosed by brackets "[]" replaced with your own identifying
17*5c51f124SMoriah Waterland  * information: Portions Copyright [yyyy] [name of copyright owner]
18*5c51f124SMoriah Waterland  *
19*5c51f124SMoriah Waterland  * CDDL HEADER END
20*5c51f124SMoriah Waterland  */
21*5c51f124SMoriah Waterland 
22*5c51f124SMoriah Waterland /*
23*5c51f124SMoriah Waterland  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*5c51f124SMoriah Waterland  * Use is subject to license terms.
25*5c51f124SMoriah Waterland  */
26*5c51f124SMoriah Waterland 
27*5c51f124SMoriah Waterland /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*5c51f124SMoriah Waterland /* All Rights Reserved */
29*5c51f124SMoriah Waterland 
30*5c51f124SMoriah Waterland 
31*5c51f124SMoriah Waterland #include <stdio.h>
32*5c51f124SMoriah Waterland #include <errno.h>
33*5c51f124SMoriah Waterland #include <string.h>
34*5c51f124SMoriah Waterland #include <limits.h>
35*5c51f124SMoriah Waterland #include <stdlib.h>
36*5c51f124SMoriah Waterland #include <unistd.h>
37*5c51f124SMoriah Waterland #include <sys/types.h>
38*5c51f124SMoriah Waterland #include <pkgstrct.h>
39*5c51f124SMoriah Waterland #include <locale.h>
40*5c51f124SMoriah Waterland #include <libintl.h>
41*5c51f124SMoriah Waterland #include <pkglib.h>
42*5c51f124SMoriah Waterland #include <install.h>
43*5c51f124SMoriah Waterland #include <libinst.h>
44*5c51f124SMoriah Waterland #include <libadm.h>
45*5c51f124SMoriah Waterland #include "installf.h"
46*5c51f124SMoriah Waterland 
47*5c51f124SMoriah Waterland #define	LSIZE	1024
48*5c51f124SMoriah Waterland #define	MALSIZ	164
49*5c51f124SMoriah Waterland 
50*5c51f124SMoriah Waterland #define	ERR_MAJOR 	"invalid major number <%s> specified for <%s>"
51*5c51f124SMoriah Waterland #define	ERR_MINOR 	"invalid minor number <%s> specified for <%s>"
52*5c51f124SMoriah Waterland #define	ERR_MODE	"invalid mode <%s> specified for <%s>"
53*5c51f124SMoriah Waterland #define	ERR_RELPATH 	"relative pathname <%s> not permitted"
54*5c51f124SMoriah Waterland #define	ERR_NULLPATH 	"NULL or garbled pathname"
55*5c51f124SMoriah Waterland #define	ERR_LINK	"invalid link specification <%s>"
56*5c51f124SMoriah Waterland #define	ERR_LINKFTYPE	"ftype <%c> does not match link specification <%s>"
57*5c51f124SMoriah Waterland #define	ERR_LINKARGS	"extra arguments in link specification <%s>"
58*5c51f124SMoriah Waterland #define	ERR_LINKREL	"relative pathname in link specification <%s>"
59*5c51f124SMoriah Waterland #define	ERR_FTYPE	"invalid ftype <%c> for <%s>"
60*5c51f124SMoriah Waterland #define	ERR_ARGC 	"invalid number of arguments for <%s>"
61*5c51f124SMoriah Waterland #define	ERR_SPECALL	"ftype <%c> requires all fields to be specified"
62*5c51f124SMoriah Waterland 
63*5c51f124SMoriah Waterland static int validate(struct cfextra *ext, int argc, char *argv[]);
64*5c51f124SMoriah Waterland static void checkPaths(char *argv[]);
65*5c51f124SMoriah Waterland 
66*5c51f124SMoriah Waterland int
installf(int argc,char * argv[])67*5c51f124SMoriah Waterland installf(int argc, char *argv[])
68*5c51f124SMoriah Waterland {
69*5c51f124SMoriah Waterland 	struct cfextra *new;
70*5c51f124SMoriah Waterland 	char	line[LSIZE];
71*5c51f124SMoriah Waterland 	char	*largv[8];
72*5c51f124SMoriah Waterland 	int	myerror;
73*5c51f124SMoriah Waterland 
74*5c51f124SMoriah Waterland 	if (strcmp(argv[0], "-") != 0) {
75*5c51f124SMoriah Waterland 		if (argc < 1)
76*5c51f124SMoriah Waterland 			usage(); /* at least pathname is required */
77*5c51f124SMoriah Waterland 		extlist = calloc(2, sizeof (struct cfextra *));
78*5c51f124SMoriah Waterland 		extlist[0] = new = calloc(1, sizeof (struct cfextra));
79*5c51f124SMoriah Waterland 		eptnum = 1;
80*5c51f124SMoriah Waterland 
81*5c51f124SMoriah Waterland 		/* There is only one filename on the command line. */
82*5c51f124SMoriah Waterland 		checkPaths(argv);
83*5c51f124SMoriah Waterland 		if (validate(new, argc, argv))
84*5c51f124SMoriah Waterland 			quit(1);
85*5c51f124SMoriah Waterland 		return (0);
86*5c51f124SMoriah Waterland 	}
87*5c51f124SMoriah Waterland 
88*5c51f124SMoriah Waterland 	/* Read stdin to obtain entries, which need to be sorted. */
89*5c51f124SMoriah Waterland 	eptnum = 0;
90*5c51f124SMoriah Waterland 	myerror = 0;
91*5c51f124SMoriah Waterland 	extlist = calloc(MALSIZ, sizeof (struct cfextra *));
92*5c51f124SMoriah Waterland 	while (fgets(line, LSIZE, stdin) != NULL) {
93*5c51f124SMoriah Waterland 		argc = 0;
94*5c51f124SMoriah Waterland 		argv = largv;
95*5c51f124SMoriah Waterland 		argv[argc++] = strtok(line, " \t\n");
96*5c51f124SMoriah Waterland 		while (argv[argc] = strtok(NULL, " \t\n"))
97*5c51f124SMoriah Waterland 			argc++;
98*5c51f124SMoriah Waterland 
99*5c51f124SMoriah Waterland 		if (argc < 1)
100*5c51f124SMoriah Waterland 			usage(); /* at least pathname is required */
101*5c51f124SMoriah Waterland 
102*5c51f124SMoriah Waterland 		new = calloc(1, sizeof (struct cfextra));
103*5c51f124SMoriah Waterland 		if (new == NULL) {
104*5c51f124SMoriah Waterland 			progerr(strerror(errno));
105*5c51f124SMoriah Waterland 			quit(99);
106*5c51f124SMoriah Waterland 		}
107*5c51f124SMoriah Waterland 
108*5c51f124SMoriah Waterland 		checkPaths(argv);
109*5c51f124SMoriah Waterland 
110*5c51f124SMoriah Waterland 		if (validate(new, argc, argv))
111*5c51f124SMoriah Waterland 			myerror++;
112*5c51f124SMoriah Waterland 
113*5c51f124SMoriah Waterland 		extlist[eptnum] = new;
114*5c51f124SMoriah Waterland 		if ((++eptnum % MALSIZ) == 0) {
115*5c51f124SMoriah Waterland 			extlist = realloc(extlist,
116*5c51f124SMoriah Waterland 			    (sizeof (struct cfextra *) * (eptnum+MALSIZ)));
117*5c51f124SMoriah Waterland 			if (!extlist) {
118*5c51f124SMoriah Waterland 				progerr(strerror(errno));
119*5c51f124SMoriah Waterland 				quit(99);
120*5c51f124SMoriah Waterland 			}
121*5c51f124SMoriah Waterland 		}
122*5c51f124SMoriah Waterland 	}
123*5c51f124SMoriah Waterland 	extlist[eptnum] = (struct cfextra *)NULL;
124*5c51f124SMoriah Waterland 	qsort((char *)extlist, (unsigned)eptnum, sizeof (struct cfextra *),
125*5c51f124SMoriah Waterland 	    cfentcmp);
126*5c51f124SMoriah Waterland 	return (myerror);
127*5c51f124SMoriah Waterland }
128*5c51f124SMoriah Waterland 
129*5c51f124SMoriah Waterland static int
validate(struct cfextra * ext,int argc,char * argv[])130*5c51f124SMoriah Waterland validate(struct cfextra *ext, int argc, char *argv[])
131*5c51f124SMoriah Waterland {
132*5c51f124SMoriah Waterland 	char	*ret, *pt;
133*5c51f124SMoriah Waterland 	int	n, allspec, is_a_link;
134*5c51f124SMoriah Waterland 	struct	cfent *ept;
135*5c51f124SMoriah Waterland 
136*5c51f124SMoriah Waterland 	ept = &(ext->cf_ent);
137*5c51f124SMoriah Waterland 
138*5c51f124SMoriah Waterland 	/* initialize cfent structure */
139*5c51f124SMoriah Waterland 	ept->pinfo = NULL;
140*5c51f124SMoriah Waterland 	(void) gpkgmapvfp(ept, (VFP_T *)NULL);	/* This just clears stuff. */
141*5c51f124SMoriah Waterland 
142*5c51f124SMoriah Waterland 	n = allspec = 0;
143*5c51f124SMoriah Waterland 	if (classname)
144*5c51f124SMoriah Waterland 		(void) strncpy(ept->pkg_class, classname, CLSSIZ);
145*5c51f124SMoriah Waterland 
146*5c51f124SMoriah Waterland 	if (argv[n] == NULL || *(argv[n]) == '\000') {
147*5c51f124SMoriah Waterland 		progerr(gettext(ERR_NULLPATH));
148*5c51f124SMoriah Waterland 		return (1);
149*5c51f124SMoriah Waterland 	}
150*5c51f124SMoriah Waterland 
151*5c51f124SMoriah Waterland 	/*
152*5c51f124SMoriah Waterland 	 * It would be a good idea to figure out how to get much of
153*5c51f124SMoriah Waterland 	 * this done using facilities in procmap.c - JST
154*5c51f124SMoriah Waterland 	 */
155*5c51f124SMoriah Waterland 	if (pt = strchr(argv[n], '=')) {
156*5c51f124SMoriah Waterland 		*pt = '\0';	/* cut off pathname at the = sign */
157*5c51f124SMoriah Waterland 		is_a_link = 1;
158*5c51f124SMoriah Waterland 	} else
159*5c51f124SMoriah Waterland 		is_a_link = 0;
160*5c51f124SMoriah Waterland 
161*5c51f124SMoriah Waterland 	if (RELATIVE(argv[n])) {
162*5c51f124SMoriah Waterland 		progerr(gettext(ERR_RELPATH),
163*5c51f124SMoriah Waterland 		    (argv[n] == NULL) ? "unknown" : argv[n]);
164*5c51f124SMoriah Waterland 		return (1);
165*5c51f124SMoriah Waterland 	}
166*5c51f124SMoriah Waterland 
167*5c51f124SMoriah Waterland 	/* get the pathnames */
168*5c51f124SMoriah Waterland 	if (eval_path(&(ext->server_path), &(ext->client_path),
169*5c51f124SMoriah Waterland 	    &(ext->map_path), argv[n++]) == 0)
170*5c51f124SMoriah Waterland 		return (1);
171*5c51f124SMoriah Waterland 
172*5c51f124SMoriah Waterland 	ept->path = ext->client_path;
173*5c51f124SMoriah Waterland 
174*5c51f124SMoriah Waterland 	/* This isn't likely to happen; but, better safe than sorry. */
175*5c51f124SMoriah Waterland 	if (RELATIVE(ept->path)) {
176*5c51f124SMoriah Waterland 		progerr(gettext(ERR_RELPATH), ept->path);
177*5c51f124SMoriah Waterland 		return (1);
178*5c51f124SMoriah Waterland 	}
179*5c51f124SMoriah Waterland 
180*5c51f124SMoriah Waterland 	if (is_a_link) {
181*5c51f124SMoriah Waterland 		/* links specifications should be handled right here */
182*5c51f124SMoriah Waterland 		ept->ftype = ((n >= argc) ? 'l' : argv[n++][0]);
183*5c51f124SMoriah Waterland 
184*5c51f124SMoriah Waterland 		/* If nothing follows the '=', it's invalid */
185*5c51f124SMoriah Waterland 		if (!pt[1]) {
186*5c51f124SMoriah Waterland 			progerr(gettext(ERR_LINK), ept->path);
187*5c51f124SMoriah Waterland 			return (1);
188*5c51f124SMoriah Waterland 		}
189*5c51f124SMoriah Waterland 
190*5c51f124SMoriah Waterland 		/* Test for an argument after the link. */
191*5c51f124SMoriah Waterland 		if (argc != n) {
192*5c51f124SMoriah Waterland 			progerr(gettext(ERR_LINKARGS), ept->path);
193*5c51f124SMoriah Waterland 			return (1);
194*5c51f124SMoriah Waterland 		}
195*5c51f124SMoriah Waterland 
196*5c51f124SMoriah Waterland 		/*
197*5c51f124SMoriah Waterland 		 * If it's a link but it's neither hard nor symbolic then
198*5c51f124SMoriah Waterland 		 * it's bad.
199*5c51f124SMoriah Waterland 		 */
200*5c51f124SMoriah Waterland 		if (!strchr("sl", ept->ftype)) {
201*5c51f124SMoriah Waterland 			progerr(gettext(ERR_LINKFTYPE), ept->ftype, ept->path);
202*5c51f124SMoriah Waterland 			return (1);
203*5c51f124SMoriah Waterland 		}
204*5c51f124SMoriah Waterland 
205*5c51f124SMoriah Waterland 		ext->server_local = pathdup(pt+1);
206*5c51f124SMoriah Waterland 		ext->client_local = ext->server_local;
207*5c51f124SMoriah Waterland 
208*5c51f124SMoriah Waterland 		ept->ainfo.local = ext->client_local;
209*5c51f124SMoriah Waterland 
210*5c51f124SMoriah Waterland 		return (0);
211*5c51f124SMoriah Waterland 	} else if (n >= argc) {
212*5c51f124SMoriah Waterland 		/* we are expecting to change object's contents */
213*5c51f124SMoriah Waterland 		return (0);
214*5c51f124SMoriah Waterland 	}
215*5c51f124SMoriah Waterland 
216*5c51f124SMoriah Waterland 	ept->ftype = argv[n++][0];
217*5c51f124SMoriah Waterland 	if (strchr("sl", ept->ftype)) {
218*5c51f124SMoriah Waterland 		progerr(gettext(ERR_LINK), ept->path);
219*5c51f124SMoriah Waterland 		return (1);
220*5c51f124SMoriah Waterland 	} else if (!strchr("?fvedxcbp", ept->ftype)) {
221*5c51f124SMoriah Waterland 		progerr(gettext(ERR_FTYPE), ept->ftype, ept->path);
222*5c51f124SMoriah Waterland 		return (1);
223*5c51f124SMoriah Waterland 	}
224*5c51f124SMoriah Waterland 
225*5c51f124SMoriah Waterland 	if (ept->ftype == 'b' || ept->ftype == 'c') {
226*5c51f124SMoriah Waterland 		if (n < argc) {
227*5c51f124SMoriah Waterland 			ept->ainfo.major = strtol(argv[n++], &ret, 0);
228*5c51f124SMoriah Waterland 			if (ret && *ret) {
229*5c51f124SMoriah Waterland 				progerr(gettext(ERR_MAJOR), argv[n-1],
230*5c51f124SMoriah Waterland 				    ept->path);
231*5c51f124SMoriah Waterland 				return (1);
232*5c51f124SMoriah Waterland 			}
233*5c51f124SMoriah Waterland 		}
234*5c51f124SMoriah Waterland 		if (n < argc) {
235*5c51f124SMoriah Waterland 			ept->ainfo.minor = strtol(argv[n++], &ret, 0);
236*5c51f124SMoriah Waterland 			if (ret && *ret) {
237*5c51f124SMoriah Waterland 				progerr(gettext(ERR_MINOR), argv[n-1],
238*5c51f124SMoriah Waterland 				    ept->path);
239*5c51f124SMoriah Waterland 				return (1);
240*5c51f124SMoriah Waterland 			}
241*5c51f124SMoriah Waterland 			allspec++;
242*5c51f124SMoriah Waterland 		}
243*5c51f124SMoriah Waterland 	}
244*5c51f124SMoriah Waterland 
245*5c51f124SMoriah Waterland 	allspec = 0;
246*5c51f124SMoriah Waterland 	if (n < argc) {
247*5c51f124SMoriah Waterland 		ept->ainfo.mode = strtol(argv[n++], &ret, 8);
248*5c51f124SMoriah Waterland 		if (ret && *ret) {
249*5c51f124SMoriah Waterland 			progerr(gettext(ERR_MODE), argv[n-1], ept->path);
250*5c51f124SMoriah Waterland 			return (1);
251*5c51f124SMoriah Waterland 		}
252*5c51f124SMoriah Waterland 	}
253*5c51f124SMoriah Waterland 	if (n < argc)
254*5c51f124SMoriah Waterland 		(void) strncpy(ept->ainfo.owner, argv[n++], ATRSIZ);
255*5c51f124SMoriah Waterland 	if (n < argc) {
256*5c51f124SMoriah Waterland 		(void) strncpy(ept->ainfo.group, argv[n++], ATRSIZ);
257*5c51f124SMoriah Waterland 		allspec++;
258*5c51f124SMoriah Waterland 	}
259*5c51f124SMoriah Waterland 	if (strchr("dxbcp", ept->ftype) && !allspec) {
260*5c51f124SMoriah Waterland 		progerr(gettext(ERR_ARGC), ept->path);
261*5c51f124SMoriah Waterland 		progerr(gettext(ERR_SPECALL), ept->ftype);
262*5c51f124SMoriah Waterland 		return (1);
263*5c51f124SMoriah Waterland 	}
264*5c51f124SMoriah Waterland 	if (n < argc) {
265*5c51f124SMoriah Waterland 		progerr(gettext(ERR_ARGC), ept->path);
266*5c51f124SMoriah Waterland 		return (1);
267*5c51f124SMoriah Waterland 	}
268*5c51f124SMoriah Waterland 	return (0);
269*5c51f124SMoriah Waterland }
270*5c51f124SMoriah Waterland 
271*5c51f124SMoriah Waterland int
cfentcmp(const void * p1,const void * p2)272*5c51f124SMoriah Waterland cfentcmp(const void *p1, const void *p2)
273*5c51f124SMoriah Waterland {
274*5c51f124SMoriah Waterland 	struct cfextra *ext1 = *((struct cfextra **)p1);
275*5c51f124SMoriah Waterland 	struct cfextra *ext2 = *((struct cfextra **)p2);
276*5c51f124SMoriah Waterland 
277*5c51f124SMoriah Waterland 	return (strcmp(ext1->cf_ent.path, ext2->cf_ent.path));
278*5c51f124SMoriah Waterland }
279*5c51f124SMoriah Waterland 
280*5c51f124SMoriah Waterland /*
281*5c51f124SMoriah Waterland  * If the path at argv[0] has the value of
282*5c51f124SMoriah Waterland  * PKG_INSTALL_ROOT prepended, remove it
283*5c51f124SMoriah Waterland  */
284*5c51f124SMoriah Waterland static void
checkPaths(char * argv[])285*5c51f124SMoriah Waterland checkPaths(char *argv[])
286*5c51f124SMoriah Waterland {
287*5c51f124SMoriah Waterland 	char *root;
288*5c51f124SMoriah Waterland 	int rootLen;
289*5c51f124SMoriah Waterland 
290*5c51f124SMoriah Waterland 	/*
291*5c51f124SMoriah Waterland 	 * Note- No local copy of argv is needed since this
292*5c51f124SMoriah Waterland 	 * function is guaranteed to replace argv with a subset of
293*5c51f124SMoriah Waterland 	 * the original argv.
294*5c51f124SMoriah Waterland 	 */
295*5c51f124SMoriah Waterland 
296*5c51f124SMoriah Waterland 	/* We only want to canonize the path if it contains multiple '/'s */
297*5c51f124SMoriah Waterland 
298*5c51f124SMoriah Waterland 	canonize_slashes(argv[0]);
299*5c51f124SMoriah Waterland 
300*5c51f124SMoriah Waterland 	if ((root = get_inst_root()) == NULL)
301*5c51f124SMoriah Waterland 		return;
302*5c51f124SMoriah Waterland 	if (strcmp(root, "/") != 0) {
303*5c51f124SMoriah Waterland 		rootLen = strlen(root);
304*5c51f124SMoriah Waterland 		if (strncmp(argv[0], root, rootLen) == 0) {
305*5c51f124SMoriah Waterland 			argv[0] += rootLen;
306*5c51f124SMoriah Waterland 		}
307*5c51f124SMoriah Waterland 	}
308*5c51f124SMoriah Waterland }
309