xref: /titanic_50/usr/src/tools/install.bin/install.bin.c (revision c0e7977a434048a8bc7386ea0e8befaa77a646cf)
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
519ca6462Scasper  * Common Development and Distribution License (the "License").
619ca6462Scasper  * 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 /*
22b83ec4edSjmcp  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <strings.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <fcntl.h>
317c478bd9Sstevel@tonic-gate #include <sys/errno.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/uio.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <libgen.h>
380adc1619Smike_s #include "stdusers.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	FILE_BUFF	40960
427c478bd9Sstevel@tonic-gate 
43b83ec4edSjmcp static int suppress = 0;
44b83ec4edSjmcp 
45b83ec4edSjmcp static void usage(void);
46b83ec4edSjmcp static void file_copy(char *src_file, char *dest_file);
47b83ec4edSjmcp static void chown_file(const char *file, const char *group, const char *owner);
48b83ec4edSjmcp static char *find_basename(const char *str);
49b83ec4edSjmcp static int creatdir(char *fn);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate void
usage(void)537c478bd9Sstevel@tonic-gate usage(void)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
56*c0e7977aSJosef 'Jeff' Sipek 	    "usage: install [-sd][-m mode][-g group][-u owner] "
577c478bd9Sstevel@tonic-gate 	    "-f dir file ...\n");
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate void
file_copy(char * src_file,char * dest_file)617c478bd9Sstevel@tonic-gate file_copy(char *src_file, char *dest_file)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	int	src_fd;
647c478bd9Sstevel@tonic-gate 	int	dest_fd;
657c478bd9Sstevel@tonic-gate 	int	count;
667c478bd9Sstevel@tonic-gate 	static char file_buff[FILE_BUFF];
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if ((src_fd = open(src_file, O_RDONLY))  == -1) {
69b83ec4edSjmcp 		(void) fprintf(stderr, "install:file_copy: %s failed "
70b83ec4edSjmcp 		    "(%d): %s\n", src_file, errno, strerror(errno));
717c478bd9Sstevel@tonic-gate 		exit(1);
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if ((dest_fd = open(dest_file, O_CREAT|O_WRONLY|O_TRUNC, 0755)) == -1) {
75b83ec4edSjmcp 		(void) fprintf(stderr, "install:file_copy: %s failed "
76b83ec4edSjmcp 		    "(%d): %s\n", dest_file, errno, strerror(errno));
777c478bd9Sstevel@tonic-gate 		exit(1);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	while ((count = read(src_fd, file_buff, FILE_BUFF)) > 0) {
81b83ec4edSjmcp 		(void) write(dest_fd, file_buff, count);
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (count == -1) {
85b83ec4edSjmcp 		(void) fprintf(stderr, "install:file_copy:read failed "
86b83ec4edSjmcp 		    "(%d): %s\n", errno, strerror(errno));
877c478bd9Sstevel@tonic-gate 		exit(1);
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 
90b83ec4edSjmcp 	if (!suppress)
917c478bd9Sstevel@tonic-gate 		(void) printf("%s installed as %s\n", src_file, dest_file);
927c478bd9Sstevel@tonic-gate 
93b83ec4edSjmcp 	(void) close(src_fd);
94b83ec4edSjmcp 	(void) close(dest_fd);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate void
chown_file(const char * file,const char * group,const char * owner)997c478bd9Sstevel@tonic-gate chown_file(const char *file, const char *group, const char *owner)
1007c478bd9Sstevel@tonic-gate {
10119ca6462Scasper 	gid_t	grp = (gid_t)-1;
10219ca6462Scasper 	uid_t	own = (uid_t)-1;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (group) {
1050adc1619Smike_s 		grp = stdfind(group, groupnames);
1060adc1619Smike_s 		if (grp < 0)
1077c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "unknown group(%s)\n", group);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if (owner) {
1110adc1619Smike_s 		own = stdfind(owner, usernames);
1120adc1619Smike_s 		if (own < 0) {
1137c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "unknown owner(%s)\n", owner);
1147c478bd9Sstevel@tonic-gate 			exit(1);
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (chown(file, own, grp) == -1) {
120b83ec4edSjmcp 		(void) fprintf(stderr, "install:chown_file: failed "
121b83ec4edSjmcp 		    "(%d): %s\n", errno, strerror(errno));
1227c478bd9Sstevel@tonic-gate 		exit(1);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
126b83ec4edSjmcp 
1277c478bd9Sstevel@tonic-gate char *
find_basename(const char * str)1287c478bd9Sstevel@tonic-gate find_basename(const char *str)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	int	i;
1317c478bd9Sstevel@tonic-gate 	int	len;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	len = strlen(str);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	for (i = len-1; i >= 0; i--)
1367c478bd9Sstevel@tonic-gate 		if (str[i] == '/')
1377c478bd9Sstevel@tonic-gate 			return ((char *)(str + i + 1));
1387c478bd9Sstevel@tonic-gate 	return ((char *)str);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
141b83ec4edSjmcp int
creatdir(char * fn)142b83ec4edSjmcp creatdir(char *fn) {
143b83ec4edSjmcp 
144b83ec4edSjmcp 	errno = 0;
145b83ec4edSjmcp 
146b83ec4edSjmcp 	if (mkdirp(fn, 0755) == -1) {
147b83ec4edSjmcp 		if (errno != EEXIST)
148b83ec4edSjmcp 			return (errno);
149b83ec4edSjmcp 	} else if (!suppress) {
150b83ec4edSjmcp 		(void) printf("directory %s created\n", fn);
151b83ec4edSjmcp 	}
152b83ec4edSjmcp 	return (0);
153b83ec4edSjmcp }
154b83ec4edSjmcp 
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1577c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	int	c;
1607c478bd9Sstevel@tonic-gate 	int	errflg = 0;
1617c478bd9Sstevel@tonic-gate 	int	dirflg = 0;
1627c478bd9Sstevel@tonic-gate 	char	*group = NULL;
1637c478bd9Sstevel@tonic-gate 	char	*owner = NULL;
1647c478bd9Sstevel@tonic-gate 	char	*dirb = NULL;
1657c478bd9Sstevel@tonic-gate 	char	*ins_file = NULL;
1667c478bd9Sstevel@tonic-gate 	int	mode = -1;
1677c478bd9Sstevel@tonic-gate 	char	dest_file[MAXPATHLEN];
168b83ec4edSjmcp 	int	rv = 0;
1697c478bd9Sstevel@tonic-gate 
170*c0e7977aSJosef 'Jeff' Sipek 	while ((c = getopt(argc, argv, "f:sm:du:g:")) != EOF) {
1717c478bd9Sstevel@tonic-gate 		switch (c) {
1727c478bd9Sstevel@tonic-gate 		case 'f':
1737c478bd9Sstevel@tonic-gate 			dirb = optarg;
1747c478bd9Sstevel@tonic-gate 			break;
1757c478bd9Sstevel@tonic-gate 		case 'g':
1767c478bd9Sstevel@tonic-gate 			group = optarg;
1777c478bd9Sstevel@tonic-gate 			break;
1787c478bd9Sstevel@tonic-gate 		case 'u':
1797c478bd9Sstevel@tonic-gate 			owner = optarg;
1807c478bd9Sstevel@tonic-gate 			break;
1817c478bd9Sstevel@tonic-gate 		case 'd':
1827c478bd9Sstevel@tonic-gate 			dirflg = 1;
1837c478bd9Sstevel@tonic-gate 			break;
1847c478bd9Sstevel@tonic-gate 		case 'm':
1857c478bd9Sstevel@tonic-gate 			mode = strtol(optarg, NULL, 8);
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 		case 's':
188b83ec4edSjmcp 			suppress = 1;
189b83ec4edSjmcp 			break;
1907c478bd9Sstevel@tonic-gate 		case '?':
1917c478bd9Sstevel@tonic-gate 			errflg++;
1927c478bd9Sstevel@tonic-gate 			break;
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if (errflg) {
1977c478bd9Sstevel@tonic-gate 		usage();
1987c478bd9Sstevel@tonic-gate 		return (1);
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (argc == optind) {
2027c478bd9Sstevel@tonic-gate 		usage();
2037c478bd9Sstevel@tonic-gate 		return (1);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if (!dirflg && (dirb == NULL)) {
2077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2087c478bd9Sstevel@tonic-gate 		    "install: no destination directory specified.\n");
2097c478bd9Sstevel@tonic-gate 		return (1);
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	for (c = optind; c < argc; c++) {
2137c478bd9Sstevel@tonic-gate 		ins_file = argv[c];
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		if (dirflg) {
216b83ec4edSjmcp 			rv = creatdir(ins_file);
217b83ec4edSjmcp 			if (rv) {
218b83ec4edSjmcp 				(void) fprintf(stderr,
219b83ec4edSjmcp 				    "install: creatdir %s (%d): %s\n",
220b83ec4edSjmcp 				    ins_file, errno, strerror(errno));
221b83ec4edSjmcp 				return (rv);
222b83ec4edSjmcp 			}
223b83ec4edSjmcp 			(void) strlcpy(dest_file, ins_file, MAXPATHLEN);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		} else {
2267c478bd9Sstevel@tonic-gate 			(void) strcat(strcat(strcpy(dest_file, dirb), "/"),
2277c478bd9Sstevel@tonic-gate 			    find_basename(ins_file));
2287c478bd9Sstevel@tonic-gate 			file_copy(ins_file, dest_file);
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 
231*c0e7977aSJosef 'Jeff' Sipek 		if (group || owner)
2327c478bd9Sstevel@tonic-gate 			chown_file(dest_file, group, owner);
233*c0e7977aSJosef 'Jeff' Sipek 
2347c478bd9Sstevel@tonic-gate 		if (mode != -1) {
235b83ec4edSjmcp 			(void) umask(0);
2367c478bd9Sstevel@tonic-gate 			if (chmod(dest_file, mode) == -1) {
237b83ec4edSjmcp 				(void) fprintf(stderr,
238b83ec4edSjmcp 				    "install: chmod of %s to mode %o failed "
239b83ec4edSjmcp 				    "(%d): %s\n",
240b83ec4edSjmcp 				    dest_file, mode, errno, strerror(errno));
2417c478bd9Sstevel@tonic-gate 				return (1);
2427c478bd9Sstevel@tonic-gate 			}
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 	return (0);
2467c478bd9Sstevel@tonic-gate }
247