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