xref: /titanic_52/usr/src/tools/install.bin/install.bin.c (revision 0adc16190e36914964740716575460dda750de39)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*0adc1619Smike_s  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <strings.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate #include <sys/errno.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/uio.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <libgen.h>
42*0adc1619Smike_s #include "stdusers.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define	FILE_BUFF	40960
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate int supress = 0;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate void
517c478bd9Sstevel@tonic-gate usage(void)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
547c478bd9Sstevel@tonic-gate 	    "usage: install [-sd][-m mode][-g group][-u owner] "
557c478bd9Sstevel@tonic-gate 	    "-f dir file ...\n");
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate void
597c478bd9Sstevel@tonic-gate file_copy(char *src_file, char *dest_file)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	int	src_fd;
627c478bd9Sstevel@tonic-gate 	int	dest_fd;
637c478bd9Sstevel@tonic-gate 	int	count;
647c478bd9Sstevel@tonic-gate 	static char file_buff[FILE_BUFF];
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	if ((src_fd = open(src_file, O_RDONLY))  == -1) {
677c478bd9Sstevel@tonic-gate 		perror(src_file);
687c478bd9Sstevel@tonic-gate 		exit(1);
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	if ((dest_fd = open(dest_file, O_CREAT|O_WRONLY|O_TRUNC, 0755)) == -1) {
727c478bd9Sstevel@tonic-gate 		perror(dest_file);
737c478bd9Sstevel@tonic-gate 		exit(1);
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	while ((count = read(src_fd, file_buff, FILE_BUFF)) > 0) {
777c478bd9Sstevel@tonic-gate 		write(dest_fd, file_buff, count);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if (count == -1) {
817c478bd9Sstevel@tonic-gate 		perror("file_copy(read)");
827c478bd9Sstevel@tonic-gate 		exit(1);
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if (!supress)
867c478bd9Sstevel@tonic-gate 		(void) printf("%s installed as %s\n", src_file, dest_file);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	close(src_fd);
897c478bd9Sstevel@tonic-gate 	close(dest_fd);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate void
947c478bd9Sstevel@tonic-gate chown_file(const char *file, const char *group, const char *owner)
957c478bd9Sstevel@tonic-gate {
96*0adc1619Smike_s 	gid_t	grp = -1;
97*0adc1619Smike_s 	uid_t	own = -1;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (group) {
100*0adc1619Smike_s 		grp = stdfind(group, groupnames);
101*0adc1619Smike_s 		if (grp < 0)
1027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "unknown group(%s)\n", group);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (owner) {
106*0adc1619Smike_s 		own = stdfind(owner, usernames);
107*0adc1619Smike_s 		if (own < 0) {
1087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "unknown owner(%s)\n", owner);
1097c478bd9Sstevel@tonic-gate 			exit(1);
1107c478bd9Sstevel@tonic-gate 		}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if (chown(file, own, grp) == -1) {
1157c478bd9Sstevel@tonic-gate 		perror("chown");
1167c478bd9Sstevel@tonic-gate 		exit(1);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate char *
1217c478bd9Sstevel@tonic-gate find_basename(const char *str)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	int	i;
1247c478bd9Sstevel@tonic-gate 	int	len;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	len = strlen(str);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	for (i = len-1; i >= 0; i--)
1297c478bd9Sstevel@tonic-gate 		if (str[i] == '/')
1307c478bd9Sstevel@tonic-gate 			return ((char *)(str + i + 1));
1317c478bd9Sstevel@tonic-gate 	return ((char *)str);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate int
1367c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	int	c;
1397c478bd9Sstevel@tonic-gate 	int	errflg = 0;
1407c478bd9Sstevel@tonic-gate 	int	dirflg = 0;
1417c478bd9Sstevel@tonic-gate 	char	*group = NULL;
1427c478bd9Sstevel@tonic-gate 	char	*owner = NULL;
1437c478bd9Sstevel@tonic-gate 	char	*dirb = NULL;
1447c478bd9Sstevel@tonic-gate 	char	*ins_file = NULL;
1457c478bd9Sstevel@tonic-gate 	int	mode = -1;
1467c478bd9Sstevel@tonic-gate 	char	dest_file[MAXPATHLEN];
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "f:sm:du:g:")) != EOF) {
1507c478bd9Sstevel@tonic-gate 		switch (c) {
1517c478bd9Sstevel@tonic-gate 		case 'f':
1527c478bd9Sstevel@tonic-gate 			dirb = optarg;
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 		case 'g':
1557c478bd9Sstevel@tonic-gate 			group = optarg;
1567c478bd9Sstevel@tonic-gate 			break;
1577c478bd9Sstevel@tonic-gate 		case 'u':
1587c478bd9Sstevel@tonic-gate 			owner = optarg;
1597c478bd9Sstevel@tonic-gate 			break;
1607c478bd9Sstevel@tonic-gate 		case 'd':
1617c478bd9Sstevel@tonic-gate 			dirflg = 1;
1627c478bd9Sstevel@tonic-gate 			break;
1637c478bd9Sstevel@tonic-gate 		case 'm':
1647c478bd9Sstevel@tonic-gate 			mode = strtol(optarg, NULL, 8);
1657c478bd9Sstevel@tonic-gate 			break;
1667c478bd9Sstevel@tonic-gate 		case 's':
1677c478bd9Sstevel@tonic-gate 			supress = 1;
1687c478bd9Sstevel@tonic-gate 			break;
1697c478bd9Sstevel@tonic-gate 		case '?':
1707c478bd9Sstevel@tonic-gate 			errflg++;
1717c478bd9Sstevel@tonic-gate 			break;
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	if (errflg) {
1767c478bd9Sstevel@tonic-gate 		usage();
1777c478bd9Sstevel@tonic-gate 		return (1);
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (argc == optind) {
1817c478bd9Sstevel@tonic-gate 		usage();
1827c478bd9Sstevel@tonic-gate 		return (1);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (!dirflg && (dirb == NULL)) {
1867c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1877c478bd9Sstevel@tonic-gate 		    "install: no destination directory specified.\n");
1887c478bd9Sstevel@tonic-gate 		return (1);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	for (c = optind; c < argc; c++) {
1937c478bd9Sstevel@tonic-gate 		ins_file = argv[c];
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 		if (dirflg) {
1967c478bd9Sstevel@tonic-gate 			struct stat buf;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 			if (stat(ins_file, &buf) == 0) {
1997c478bd9Sstevel@tonic-gate 				if ((buf.st_mode & S_IFMT) == S_IFDIR)
2007c478bd9Sstevel@tonic-gate 					continue;
2017c478bd9Sstevel@tonic-gate 			} else {
2027c478bd9Sstevel@tonic-gate 				if (errno != ENOENT) {
2037c478bd9Sstevel@tonic-gate 					perror("install: stat");
2047c478bd9Sstevel@tonic-gate 					return (1);
2057c478bd9Sstevel@tonic-gate 				}
2067c478bd9Sstevel@tonic-gate 			}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 			(void) strcpy(dest_file, ins_file);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 			if (mkdirp(dest_file, 0755) == -1) {
2117c478bd9Sstevel@tonic-gate 				if (!supress) {
2127c478bd9Sstevel@tonic-gate 					(void) printf(
2137c478bd9Sstevel@tonic-gate 					    "install: mkdirp of %s failed\n",
2147c478bd9Sstevel@tonic-gate 					    dest_file);
2157c478bd9Sstevel@tonic-gate 				}
2167c478bd9Sstevel@tonic-gate 			} else if (!supress) {
2177c478bd9Sstevel@tonic-gate 				(void) printf("directory %s created\n",
2187c478bd9Sstevel@tonic-gate 				    dest_file);
2197c478bd9Sstevel@tonic-gate 			}
2207c478bd9Sstevel@tonic-gate 		} else {
2217c478bd9Sstevel@tonic-gate 			(void) strcat(strcat(strcpy(dest_file, dirb), "/"),
2227c478bd9Sstevel@tonic-gate 			    find_basename(ins_file));
2237c478bd9Sstevel@tonic-gate 			file_copy(ins_file, dest_file);
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		if (group || owner)
2277c478bd9Sstevel@tonic-gate 			chown_file(dest_file, group, owner);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		if (mode != -1) {
2307c478bd9Sstevel@tonic-gate 			umask(0);
2317c478bd9Sstevel@tonic-gate 			if (chmod(dest_file, mode) == -1) {
2327c478bd9Sstevel@tonic-gate 				perror("chmod");
2337c478bd9Sstevel@tonic-gate 				return (1);
2347c478bd9Sstevel@tonic-gate 			}
2357c478bd9Sstevel@tonic-gate 		}
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 	return (0);
2387c478bd9Sstevel@tonic-gate }
239