xref: /titanic_54/usr/src/tools/install.bin/install.bin.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
32*7c478bd9Sstevel@tonic-gate #include <strings.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
34*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/uio.h>
38*7c478bd9Sstevel@tonic-gate #include <unistd.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
40*7c478bd9Sstevel@tonic-gate #include <errno.h>
41*7c478bd9Sstevel@tonic-gate #include <libgen.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #define	FILE_BUFF	40960
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate int supress = 0;
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate void
50*7c478bd9Sstevel@tonic-gate usage(void)
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
53*7c478bd9Sstevel@tonic-gate 	    "usage: install [-sd][-m mode][-g group][-u owner] "
54*7c478bd9Sstevel@tonic-gate 	    "-f dir file ...\n");
55*7c478bd9Sstevel@tonic-gate }
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate void
58*7c478bd9Sstevel@tonic-gate file_copy(char *src_file, char *dest_file)
59*7c478bd9Sstevel@tonic-gate {
60*7c478bd9Sstevel@tonic-gate 	int	src_fd;
61*7c478bd9Sstevel@tonic-gate 	int	dest_fd;
62*7c478bd9Sstevel@tonic-gate 	int	count;
63*7c478bd9Sstevel@tonic-gate 	static char file_buff[FILE_BUFF];
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	if ((src_fd = open(src_file, O_RDONLY))  == -1) {
66*7c478bd9Sstevel@tonic-gate 		perror(src_file);
67*7c478bd9Sstevel@tonic-gate 		exit(1);
68*7c478bd9Sstevel@tonic-gate 	}
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	if ((dest_fd = open(dest_file, O_CREAT|O_WRONLY|O_TRUNC, 0755)) == -1) {
71*7c478bd9Sstevel@tonic-gate 		perror(dest_file);
72*7c478bd9Sstevel@tonic-gate 		exit(1);
73*7c478bd9Sstevel@tonic-gate 	}
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	while ((count = read(src_fd, file_buff, FILE_BUFF)) > 0) {
76*7c478bd9Sstevel@tonic-gate 		write(dest_fd, file_buff, count);
77*7c478bd9Sstevel@tonic-gate 	}
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	if (count == -1) {
80*7c478bd9Sstevel@tonic-gate 		perror("file_copy(read)");
81*7c478bd9Sstevel@tonic-gate 		exit(1);
82*7c478bd9Sstevel@tonic-gate 	}
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	if (!supress)
85*7c478bd9Sstevel@tonic-gate 		(void) printf("%s installed as %s\n", src_file, dest_file);
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	close(src_fd);
88*7c478bd9Sstevel@tonic-gate 	close(dest_fd);
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate void
93*7c478bd9Sstevel@tonic-gate chown_file(const char *file, const char *group, const char *owner)
94*7c478bd9Sstevel@tonic-gate {
95*7c478bd9Sstevel@tonic-gate 	gid_t	grp;
96*7c478bd9Sstevel@tonic-gate 	gid_t	own;
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	if (group) {
99*7c478bd9Sstevel@tonic-gate 		if (strcmp(group, "bin") == 0)
100*7c478bd9Sstevel@tonic-gate 			grp = 2;
101*7c478bd9Sstevel@tonic-gate 		else if (strcmp(group, "sys") == 0)
102*7c478bd9Sstevel@tonic-gate 			grp = 3;
103*7c478bd9Sstevel@tonic-gate 		else if (strcmp(group, "adm") == 0)
104*7c478bd9Sstevel@tonic-gate 			grp = 4;
105*7c478bd9Sstevel@tonic-gate 		else if (strcmp(group, "uucp") == 0)
106*7c478bd9Sstevel@tonic-gate 			grp = 5;
107*7c478bd9Sstevel@tonic-gate 		else if (strcmp(group, "mail") == 0)
108*7c478bd9Sstevel@tonic-gate 			grp = 6;
109*7c478bd9Sstevel@tonic-gate 		else if (strcmp(group, "tty") == 0)
110*7c478bd9Sstevel@tonic-gate 			grp = 7;
111*7c478bd9Sstevel@tonic-gate 		else if (strcmp(group, "lp") == 0)
112*7c478bd9Sstevel@tonic-gate 			grp = 8;
113*7c478bd9Sstevel@tonic-gate 		else if (strcmp(group, "other") == 0)
114*7c478bd9Sstevel@tonic-gate 			grp = 1;
115*7c478bd9Sstevel@tonic-gate 		else if (strcmp(group, "nuucp") == 0)
116*7c478bd9Sstevel@tonic-gate 			grp = 9;
117*7c478bd9Sstevel@tonic-gate 		else if (strcmp(group, "staff") == 0)
118*7c478bd9Sstevel@tonic-gate 			grp = 10;
119*7c478bd9Sstevel@tonic-gate 		else if (strcmp(group, "daemon") == 0)
120*7c478bd9Sstevel@tonic-gate 			grp = 12;
121*7c478bd9Sstevel@tonic-gate 		else if (strcmp(group, "root") == 0)
122*7c478bd9Sstevel@tonic-gate 			grp = 0;
123*7c478bd9Sstevel@tonic-gate 		else if (strcmp(group, "sysadmin") == 0)
124*7c478bd9Sstevel@tonic-gate 			grp = 14;
125*7c478bd9Sstevel@tonic-gate 		else if (strcmp(group, "smmsp") == 0)
126*7c478bd9Sstevel@tonic-gate 			grp = 25;
127*7c478bd9Sstevel@tonic-gate 		else if (strcmp(group, "nobody") == 0)
128*7c478bd9Sstevel@tonic-gate 			grp = 60001;
129*7c478bd9Sstevel@tonic-gate 		else if (strcmp(group, "noaccess") == 0)
130*7c478bd9Sstevel@tonic-gate 			grp = 60002;
131*7c478bd9Sstevel@tonic-gate 		else if (strcmp(group, "svvs_grp") == 0)
132*7c478bd9Sstevel@tonic-gate 			grp = 101;
133*7c478bd9Sstevel@tonic-gate 		else
134*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "unknown group(%s)\n", group);
135*7c478bd9Sstevel@tonic-gate 	} else {
136*7c478bd9Sstevel@tonic-gate 		grp = -1;
137*7c478bd9Sstevel@tonic-gate 	}
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	if (owner) {
140*7c478bd9Sstevel@tonic-gate 		if (strcmp(owner, "bin") == 0)
141*7c478bd9Sstevel@tonic-gate 			own = 2;
142*7c478bd9Sstevel@tonic-gate 		else if (strcmp(owner, "daemon") == 0)
143*7c478bd9Sstevel@tonic-gate 			own = 1;
144*7c478bd9Sstevel@tonic-gate 		else if (strcmp(owner, "sys") == 0)
145*7c478bd9Sstevel@tonic-gate 			own = 3;
146*7c478bd9Sstevel@tonic-gate 		else if (strcmp(owner, "adm") == 0)
147*7c478bd9Sstevel@tonic-gate 			own = 4;
148*7c478bd9Sstevel@tonic-gate 		else if (strcmp(owner, "lp") == 0)
149*7c478bd9Sstevel@tonic-gate 			own = 71;
150*7c478bd9Sstevel@tonic-gate 		else if (strcmp(owner, "root") == 0)
151*7c478bd9Sstevel@tonic-gate 			own = 0;
152*7c478bd9Sstevel@tonic-gate 		else if (strcmp(owner, "uucp") == 0)
153*7c478bd9Sstevel@tonic-gate 			own = 5;
154*7c478bd9Sstevel@tonic-gate 		else if (strcmp(owner, "nuucp") == 0)
155*7c478bd9Sstevel@tonic-gate 			own = 9;
156*7c478bd9Sstevel@tonic-gate 		else if (strcmp(owner, "smmsp") == 0)
157*7c478bd9Sstevel@tonic-gate 			own = 25;
158*7c478bd9Sstevel@tonic-gate 		else if (strcmp(owner, "listen") == 0)
159*7c478bd9Sstevel@tonic-gate 			own = 37;
160*7c478bd9Sstevel@tonic-gate 		else if (strcmp(owner, "nobody") == 0)
161*7c478bd9Sstevel@tonic-gate 			own = 60001;
162*7c478bd9Sstevel@tonic-gate 		else if (strcmp(owner, "noaccess") == 0)
163*7c478bd9Sstevel@tonic-gate 			own = 60002;
164*7c478bd9Sstevel@tonic-gate 		/*
165*7c478bd9Sstevel@tonic-gate 		 * This is a temporary HACK that should be removed!!!
166*7c478bd9Sstevel@tonic-gate 		 */
167*7c478bd9Sstevel@tonic-gate 		else if (strcmp(owner, "sysadm") == 0)
168*7c478bd9Sstevel@tonic-gate 			own = 0;
169*7c478bd9Sstevel@tonic-gate 		else {
170*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "unknown owner(%s)\n", owner);
171*7c478bd9Sstevel@tonic-gate 			exit(1);
172*7c478bd9Sstevel@tonic-gate 		}
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	} else {
175*7c478bd9Sstevel@tonic-gate 		own = -1;
176*7c478bd9Sstevel@tonic-gate 	}
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	if (chown(file, own, grp) == -1) {
179*7c478bd9Sstevel@tonic-gate 		perror("chown");
180*7c478bd9Sstevel@tonic-gate 		exit(1);
181*7c478bd9Sstevel@tonic-gate 	}
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate char *
185*7c478bd9Sstevel@tonic-gate find_basename(const char *str)
186*7c478bd9Sstevel@tonic-gate {
187*7c478bd9Sstevel@tonic-gate 	int	i;
188*7c478bd9Sstevel@tonic-gate 	int	len;
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	len = strlen(str);
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	for (i = len-1; i >= 0; i--)
193*7c478bd9Sstevel@tonic-gate 		if (str[i] == '/')
194*7c478bd9Sstevel@tonic-gate 			return ((char *)(str + i + 1));
195*7c478bd9Sstevel@tonic-gate 	return ((char *)str);
196*7c478bd9Sstevel@tonic-gate }
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate int
200*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	int	c;
203*7c478bd9Sstevel@tonic-gate 	int	errflg = 0;
204*7c478bd9Sstevel@tonic-gate 	int	dirflg = 0;
205*7c478bd9Sstevel@tonic-gate 	char	*group = NULL;
206*7c478bd9Sstevel@tonic-gate 	char	*owner = NULL;
207*7c478bd9Sstevel@tonic-gate 	char	*dirb = NULL;
208*7c478bd9Sstevel@tonic-gate 	char	*ins_file = NULL;
209*7c478bd9Sstevel@tonic-gate 	int	mode = -1;
210*7c478bd9Sstevel@tonic-gate 	char	dest_file[MAXPATHLEN];
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "f:sm:du:g:")) != EOF) {
214*7c478bd9Sstevel@tonic-gate 		switch (c) {
215*7c478bd9Sstevel@tonic-gate 		case 'f':
216*7c478bd9Sstevel@tonic-gate 			dirb = optarg;
217*7c478bd9Sstevel@tonic-gate 			break;
218*7c478bd9Sstevel@tonic-gate 		case 'g':
219*7c478bd9Sstevel@tonic-gate 			group = optarg;
220*7c478bd9Sstevel@tonic-gate 			break;
221*7c478bd9Sstevel@tonic-gate 		case 'u':
222*7c478bd9Sstevel@tonic-gate 			owner = optarg;
223*7c478bd9Sstevel@tonic-gate 			break;
224*7c478bd9Sstevel@tonic-gate 		case 'd':
225*7c478bd9Sstevel@tonic-gate 			dirflg = 1;
226*7c478bd9Sstevel@tonic-gate 			break;
227*7c478bd9Sstevel@tonic-gate 		case 'm':
228*7c478bd9Sstevel@tonic-gate 			mode = strtol(optarg, NULL, 8);
229*7c478bd9Sstevel@tonic-gate 			break;
230*7c478bd9Sstevel@tonic-gate 		case 's':
231*7c478bd9Sstevel@tonic-gate 			supress = 1;
232*7c478bd9Sstevel@tonic-gate 			break;
233*7c478bd9Sstevel@tonic-gate 		case '?':
234*7c478bd9Sstevel@tonic-gate 			errflg++;
235*7c478bd9Sstevel@tonic-gate 			break;
236*7c478bd9Sstevel@tonic-gate 		}
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	if (errflg) {
240*7c478bd9Sstevel@tonic-gate 		usage();
241*7c478bd9Sstevel@tonic-gate 		return (1);
242*7c478bd9Sstevel@tonic-gate 	}
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 	if (argc == optind) {
245*7c478bd9Sstevel@tonic-gate 		usage();
246*7c478bd9Sstevel@tonic-gate 		return (1);
247*7c478bd9Sstevel@tonic-gate 	}
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	if (!dirflg && (dirb == NULL)) {
250*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
251*7c478bd9Sstevel@tonic-gate 		    "install: no destination directory specified.\n");
252*7c478bd9Sstevel@tonic-gate 		return (1);
253*7c478bd9Sstevel@tonic-gate 	}
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 	for (c = optind; c < argc; c++) {
257*7c478bd9Sstevel@tonic-gate 		ins_file = argv[c];
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 		if (dirflg) {
260*7c478bd9Sstevel@tonic-gate 			struct stat buf;
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 			if (stat(ins_file, &buf) == 0) {
263*7c478bd9Sstevel@tonic-gate 				if ((buf.st_mode & S_IFMT) == S_IFDIR)
264*7c478bd9Sstevel@tonic-gate 					continue;
265*7c478bd9Sstevel@tonic-gate 			} else {
266*7c478bd9Sstevel@tonic-gate 				if (errno != ENOENT) {
267*7c478bd9Sstevel@tonic-gate 					perror("install: stat");
268*7c478bd9Sstevel@tonic-gate 					return (1);
269*7c478bd9Sstevel@tonic-gate 				}
270*7c478bd9Sstevel@tonic-gate 			}
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 			(void) strcpy(dest_file, ins_file);
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 			if (mkdirp(dest_file, 0755) == -1) {
275*7c478bd9Sstevel@tonic-gate 				if (!supress) {
276*7c478bd9Sstevel@tonic-gate 					(void) printf(
277*7c478bd9Sstevel@tonic-gate 					    "install: mkdirp of %s failed\n",
278*7c478bd9Sstevel@tonic-gate 					    dest_file);
279*7c478bd9Sstevel@tonic-gate 				}
280*7c478bd9Sstevel@tonic-gate 			} else if (!supress) {
281*7c478bd9Sstevel@tonic-gate 				(void) printf("directory %s created\n",
282*7c478bd9Sstevel@tonic-gate 				    dest_file);
283*7c478bd9Sstevel@tonic-gate 			}
284*7c478bd9Sstevel@tonic-gate 		} else {
285*7c478bd9Sstevel@tonic-gate 			(void) strcat(strcat(strcpy(dest_file, dirb), "/"),
286*7c478bd9Sstevel@tonic-gate 			    find_basename(ins_file));
287*7c478bd9Sstevel@tonic-gate 			file_copy(ins_file, dest_file);
288*7c478bd9Sstevel@tonic-gate 		}
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 		if (group || owner)
291*7c478bd9Sstevel@tonic-gate 			chown_file(dest_file, group, owner);
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 		if (mode != -1) {
294*7c478bd9Sstevel@tonic-gate 			umask(0);
295*7c478bd9Sstevel@tonic-gate 			if (chmod(dest_file, mode) == -1) {
296*7c478bd9Sstevel@tonic-gate 				perror("chmod");
297*7c478bd9Sstevel@tonic-gate 				return (1);
298*7c478bd9Sstevel@tonic-gate 			}
299*7c478bd9Sstevel@tonic-gate 		}
300*7c478bd9Sstevel@tonic-gate 	}
301*7c478bd9Sstevel@tonic-gate 	return (0);
302*7c478bd9Sstevel@tonic-gate }
303