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 
25*93b88728SJohn Levon /*
26*93b88728SJohn Levon  * Copyright (c) 2018, Joyent, Inc.
27*93b88728SJohn Levon  */
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 
46b83ec4edSjmcp static int suppress = 0;
47b83ec4edSjmcp 
48b83ec4edSjmcp static void usage(void);
49b83ec4edSjmcp static void file_copy(char *src_file, char *dest_file);
50b83ec4edSjmcp static void chown_file(const char *file, const char *group, const char *owner);
51b83ec4edSjmcp static char *find_basename(const char *str);
52b83ec4edSjmcp static int creatdir(char *fn);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate void
usage(void)567c478bd9Sstevel@tonic-gate usage(void)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
59c0e7977aSJosef 'Jeff' Sipek 	    "usage: install [-sd][-m mode][-g group][-u owner] "
607c478bd9Sstevel@tonic-gate 	    "-f dir file ...\n");
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate void
file_copy(char * src_file,char * dest_file)647c478bd9Sstevel@tonic-gate file_copy(char *src_file, char *dest_file)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	int	src_fd;
677c478bd9Sstevel@tonic-gate 	int	dest_fd;
687c478bd9Sstevel@tonic-gate 	int	count;
697c478bd9Sstevel@tonic-gate 	static char file_buff[FILE_BUFF];
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	if ((src_fd = open(src_file, O_RDONLY))  == -1) {
72b83ec4edSjmcp 		(void) fprintf(stderr, "install:file_copy: %s failed "
73b83ec4edSjmcp 		    "(%d): %s\n", src_file, errno, strerror(errno));
747c478bd9Sstevel@tonic-gate 		exit(1);
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if ((dest_fd = open(dest_file, O_CREAT|O_WRONLY|O_TRUNC, 0755)) == -1) {
78b83ec4edSjmcp 		(void) fprintf(stderr, "install:file_copy: %s failed "
79b83ec4edSjmcp 		    "(%d): %s\n", dest_file, errno, strerror(errno));
807c478bd9Sstevel@tonic-gate 		exit(1);
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	while ((count = read(src_fd, file_buff, FILE_BUFF)) > 0) {
84b83ec4edSjmcp 		(void) write(dest_fd, file_buff, count);
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if (count == -1) {
88b83ec4edSjmcp 		(void) fprintf(stderr, "install:file_copy:read failed "
89b83ec4edSjmcp 		    "(%d): %s\n", errno, strerror(errno));
907c478bd9Sstevel@tonic-gate 		exit(1);
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
93b83ec4edSjmcp 	if (!suppress)
947c478bd9Sstevel@tonic-gate 		(void) printf("%s installed as %s\n", src_file, dest_file);
957c478bd9Sstevel@tonic-gate 
96b83ec4edSjmcp 	(void) close(src_fd);
97b83ec4edSjmcp 	(void) close(dest_fd);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate void
chown_file(const char * file,const char * group,const char * owner)1027c478bd9Sstevel@tonic-gate chown_file(const char *file, const char *group, const char *owner)
1037c478bd9Sstevel@tonic-gate {
10419ca6462Scasper 	gid_t	grp = (gid_t)-1;
10519ca6462Scasper 	uid_t	own = (uid_t)-1;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (group) {
1080adc1619Smike_s 		grp = stdfind(group, groupnames);
109*93b88728SJohn Levon 		if (grp == (gid_t)-1)
1107c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "unknown group(%s)\n", group);
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if (owner) {
1140adc1619Smike_s 		own = stdfind(owner, usernames);
115*93b88728SJohn Levon 		if (own == (uid_t)-1) {
1167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "unknown owner(%s)\n", owner);
1177c478bd9Sstevel@tonic-gate 			exit(1);
1187c478bd9Sstevel@tonic-gate 		}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (chown(file, own, grp) == -1) {
123b83ec4edSjmcp 		(void) fprintf(stderr, "install:chown_file: failed "
124b83ec4edSjmcp 		    "(%d): %s\n", errno, strerror(errno));
1257c478bd9Sstevel@tonic-gate 		exit(1);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
129b83ec4edSjmcp 
1307c478bd9Sstevel@tonic-gate char *
find_basename(const char * str)1317c478bd9Sstevel@tonic-gate find_basename(const char *str)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	int	i;
1347c478bd9Sstevel@tonic-gate 	int	len;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	len = strlen(str);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	for (i = len-1; i >= 0; i--)
1397c478bd9Sstevel@tonic-gate 		if (str[i] == '/')
1407c478bd9Sstevel@tonic-gate 			return ((char *)(str + i + 1));
1417c478bd9Sstevel@tonic-gate 	return ((char *)str);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
144b83ec4edSjmcp int
creatdir(char * fn)145b83ec4edSjmcp creatdir(char *fn) {
146b83ec4edSjmcp 
147b83ec4edSjmcp 	errno = 0;
148b83ec4edSjmcp 
149b83ec4edSjmcp 	if (mkdirp(fn, 0755) == -1) {
150b83ec4edSjmcp 		if (errno != EEXIST)
151b83ec4edSjmcp 			return (errno);
152b83ec4edSjmcp 	} else if (!suppress) {
153b83ec4edSjmcp 		(void) printf("directory %s created\n", fn);
154b83ec4edSjmcp 	}
155b83ec4edSjmcp 	return (0);
156b83ec4edSjmcp }
157b83ec4edSjmcp 
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1607c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	int	c;
1637c478bd9Sstevel@tonic-gate 	int	errflg = 0;
1647c478bd9Sstevel@tonic-gate 	int	dirflg = 0;
1657c478bd9Sstevel@tonic-gate 	char	*group = NULL;
1667c478bd9Sstevel@tonic-gate 	char	*owner = NULL;
1677c478bd9Sstevel@tonic-gate 	char	*dirb = NULL;
1687c478bd9Sstevel@tonic-gate 	char	*ins_file = NULL;
1697c478bd9Sstevel@tonic-gate 	int	mode = -1;
1707c478bd9Sstevel@tonic-gate 	char	dest_file[MAXPATHLEN];
171b83ec4edSjmcp 	int	rv = 0;
1727c478bd9Sstevel@tonic-gate 
173c0e7977aSJosef 'Jeff' Sipek 	while ((c = getopt(argc, argv, "f:sm:du:g:")) != EOF) {
1747c478bd9Sstevel@tonic-gate 		switch (c) {
1757c478bd9Sstevel@tonic-gate 		case 'f':
1767c478bd9Sstevel@tonic-gate 			dirb = optarg;
1777c478bd9Sstevel@tonic-gate 			break;
1787c478bd9Sstevel@tonic-gate 		case 'g':
1797c478bd9Sstevel@tonic-gate 			group = optarg;
1807c478bd9Sstevel@tonic-gate 			break;
1817c478bd9Sstevel@tonic-gate 		case 'u':
1827c478bd9Sstevel@tonic-gate 			owner = optarg;
1837c478bd9Sstevel@tonic-gate 			break;
1847c478bd9Sstevel@tonic-gate 		case 'd':
1857c478bd9Sstevel@tonic-gate 			dirflg = 1;
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 		case 'm':
1887c478bd9Sstevel@tonic-gate 			mode = strtol(optarg, NULL, 8);
1897c478bd9Sstevel@tonic-gate 			break;
1907c478bd9Sstevel@tonic-gate 		case 's':
191b83ec4edSjmcp 			suppress = 1;
192b83ec4edSjmcp 			break;
1937c478bd9Sstevel@tonic-gate 		case '?':
1947c478bd9Sstevel@tonic-gate 			errflg++;
1957c478bd9Sstevel@tonic-gate 			break;
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	if (errflg) {
2007c478bd9Sstevel@tonic-gate 		usage();
2017c478bd9Sstevel@tonic-gate 		return (1);
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if (argc == optind) {
2057c478bd9Sstevel@tonic-gate 		usage();
2067c478bd9Sstevel@tonic-gate 		return (1);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if (!dirflg && (dirb == NULL)) {
2107c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2117c478bd9Sstevel@tonic-gate 		    "install: no destination directory specified.\n");
2127c478bd9Sstevel@tonic-gate 		return (1);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	for (c = optind; c < argc; c++) {
2167c478bd9Sstevel@tonic-gate 		ins_file = argv[c];
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		if (dirflg) {
219b83ec4edSjmcp 			rv = creatdir(ins_file);
220b83ec4edSjmcp 			if (rv) {
221b83ec4edSjmcp 				(void) fprintf(stderr,
222b83ec4edSjmcp 				    "install: creatdir %s (%d): %s\n",
223b83ec4edSjmcp 				    ins_file, errno, strerror(errno));
224b83ec4edSjmcp 				return (rv);
2257c478bd9Sstevel@tonic-gate 			}
226b83ec4edSjmcp 			(void) strlcpy(dest_file, ins_file, MAXPATHLEN);
227b83ec4edSjmcp 
2287c478bd9Sstevel@tonic-gate 		} else {
2297c478bd9Sstevel@tonic-gate 			(void) strcat(strcat(strcpy(dest_file, dirb), "/"),
2307c478bd9Sstevel@tonic-gate 			    find_basename(ins_file));
2317c478bd9Sstevel@tonic-gate 			file_copy(ins_file, dest_file);
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 
234c0e7977aSJosef 'Jeff' Sipek 		if (group || owner)
2357c478bd9Sstevel@tonic-gate 			chown_file(dest_file, group, owner);
236c0e7977aSJosef 'Jeff' Sipek 
2377c478bd9Sstevel@tonic-gate 		if (mode != -1) {
238b83ec4edSjmcp 			(void) umask(0);
2397c478bd9Sstevel@tonic-gate 			if (chmod(dest_file, mode) == -1) {
240b83ec4edSjmcp 				(void) fprintf(stderr,
241b83ec4edSjmcp 				    "install: chmod of %s to mode %o failed "
242b83ec4edSjmcp 				    "(%d): %s\n",
243b83ec4edSjmcp 				    dest_file, mode, errno, strerror(errno));
2447c478bd9Sstevel@tonic-gate 				return (1);
2457c478bd9Sstevel@tonic-gate 			}
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 	return (0);
2497c478bd9Sstevel@tonic-gate }
250