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