10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*1384Smike_s * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate 300Sstevel@tonic-gate #include <stdio.h> 310Sstevel@tonic-gate #include <stdlib.h> 320Sstevel@tonic-gate #include <strings.h> 330Sstevel@tonic-gate #include <sys/param.h> 340Sstevel@tonic-gate #include <fcntl.h> 350Sstevel@tonic-gate #include <sys/errno.h> 360Sstevel@tonic-gate #include <sys/types.h> 370Sstevel@tonic-gate #include <sys/uio.h> 380Sstevel@tonic-gate #include <unistd.h> 390Sstevel@tonic-gate #include <sys/stat.h> 400Sstevel@tonic-gate #include <errno.h> 410Sstevel@tonic-gate #include <libgen.h> 42*1384Smike_s #include "stdusers.h" 430Sstevel@tonic-gate 440Sstevel@tonic-gate 450Sstevel@tonic-gate #define FILE_BUFF 40960 460Sstevel@tonic-gate 470Sstevel@tonic-gate int supress = 0; 480Sstevel@tonic-gate 490Sstevel@tonic-gate 500Sstevel@tonic-gate void 510Sstevel@tonic-gate usage(void) 520Sstevel@tonic-gate { 530Sstevel@tonic-gate (void) fprintf(stderr, 540Sstevel@tonic-gate "usage: install [-sd][-m mode][-g group][-u owner] " 550Sstevel@tonic-gate "-f dir file ...\n"); 560Sstevel@tonic-gate } 570Sstevel@tonic-gate 580Sstevel@tonic-gate void 590Sstevel@tonic-gate file_copy(char *src_file, char *dest_file) 600Sstevel@tonic-gate { 610Sstevel@tonic-gate int src_fd; 620Sstevel@tonic-gate int dest_fd; 630Sstevel@tonic-gate int count; 640Sstevel@tonic-gate static char file_buff[FILE_BUFF]; 650Sstevel@tonic-gate 660Sstevel@tonic-gate if ((src_fd = open(src_file, O_RDONLY)) == -1) { 670Sstevel@tonic-gate perror(src_file); 680Sstevel@tonic-gate exit(1); 690Sstevel@tonic-gate } 700Sstevel@tonic-gate 710Sstevel@tonic-gate if ((dest_fd = open(dest_file, O_CREAT|O_WRONLY|O_TRUNC, 0755)) == -1) { 720Sstevel@tonic-gate perror(dest_file); 730Sstevel@tonic-gate exit(1); 740Sstevel@tonic-gate } 750Sstevel@tonic-gate 760Sstevel@tonic-gate while ((count = read(src_fd, file_buff, FILE_BUFF)) > 0) { 770Sstevel@tonic-gate write(dest_fd, file_buff, count); 780Sstevel@tonic-gate } 790Sstevel@tonic-gate 800Sstevel@tonic-gate if (count == -1) { 810Sstevel@tonic-gate perror("file_copy(read)"); 820Sstevel@tonic-gate exit(1); 830Sstevel@tonic-gate } 840Sstevel@tonic-gate 850Sstevel@tonic-gate if (!supress) 860Sstevel@tonic-gate (void) printf("%s installed as %s\n", src_file, dest_file); 870Sstevel@tonic-gate 880Sstevel@tonic-gate close(src_fd); 890Sstevel@tonic-gate close(dest_fd); 900Sstevel@tonic-gate } 910Sstevel@tonic-gate 920Sstevel@tonic-gate 930Sstevel@tonic-gate void 940Sstevel@tonic-gate chown_file(const char *file, const char *group, const char *owner) 950Sstevel@tonic-gate { 96*1384Smike_s gid_t grp = -1; 97*1384Smike_s uid_t own = -1; 980Sstevel@tonic-gate 990Sstevel@tonic-gate if (group) { 100*1384Smike_s grp = stdfind(group, groupnames); 101*1384Smike_s if (grp < 0) 1020Sstevel@tonic-gate (void) fprintf(stderr, "unknown group(%s)\n", group); 1030Sstevel@tonic-gate } 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate if (owner) { 106*1384Smike_s own = stdfind(owner, usernames); 107*1384Smike_s if (own < 0) { 1080Sstevel@tonic-gate (void) fprintf(stderr, "unknown owner(%s)\n", owner); 1090Sstevel@tonic-gate exit(1); 1100Sstevel@tonic-gate } 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate if (chown(file, own, grp) == -1) { 1150Sstevel@tonic-gate perror("chown"); 1160Sstevel@tonic-gate exit(1); 1170Sstevel@tonic-gate } 1180Sstevel@tonic-gate } 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate char * 1210Sstevel@tonic-gate find_basename(const char *str) 1220Sstevel@tonic-gate { 1230Sstevel@tonic-gate int i; 1240Sstevel@tonic-gate int len; 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate len = strlen(str); 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate for (i = len-1; i >= 0; i--) 1290Sstevel@tonic-gate if (str[i] == '/') 1300Sstevel@tonic-gate return ((char *)(str + i + 1)); 1310Sstevel@tonic-gate return ((char *)str); 1320Sstevel@tonic-gate } 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate int 1360Sstevel@tonic-gate main(int argc, char **argv) 1370Sstevel@tonic-gate { 1380Sstevel@tonic-gate int c; 1390Sstevel@tonic-gate int errflg = 0; 1400Sstevel@tonic-gate int dirflg = 0; 1410Sstevel@tonic-gate char *group = NULL; 1420Sstevel@tonic-gate char *owner = NULL; 1430Sstevel@tonic-gate char *dirb = NULL; 1440Sstevel@tonic-gate char *ins_file = NULL; 1450Sstevel@tonic-gate int mode = -1; 1460Sstevel@tonic-gate char dest_file[MAXPATHLEN]; 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate while ((c = getopt(argc, argv, "f:sm:du:g:")) != EOF) { 1500Sstevel@tonic-gate switch (c) { 1510Sstevel@tonic-gate case 'f': 1520Sstevel@tonic-gate dirb = optarg; 1530Sstevel@tonic-gate break; 1540Sstevel@tonic-gate case 'g': 1550Sstevel@tonic-gate group = optarg; 1560Sstevel@tonic-gate break; 1570Sstevel@tonic-gate case 'u': 1580Sstevel@tonic-gate owner = optarg; 1590Sstevel@tonic-gate break; 1600Sstevel@tonic-gate case 'd': 1610Sstevel@tonic-gate dirflg = 1; 1620Sstevel@tonic-gate break; 1630Sstevel@tonic-gate case 'm': 1640Sstevel@tonic-gate mode = strtol(optarg, NULL, 8); 1650Sstevel@tonic-gate break; 1660Sstevel@tonic-gate case 's': 1670Sstevel@tonic-gate supress = 1; 1680Sstevel@tonic-gate break; 1690Sstevel@tonic-gate case '?': 1700Sstevel@tonic-gate errflg++; 1710Sstevel@tonic-gate break; 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate if (errflg) { 1760Sstevel@tonic-gate usage(); 1770Sstevel@tonic-gate return (1); 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate if (argc == optind) { 1810Sstevel@tonic-gate usage(); 1820Sstevel@tonic-gate return (1); 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate if (!dirflg && (dirb == NULL)) { 1860Sstevel@tonic-gate (void) fprintf(stderr, 1870Sstevel@tonic-gate "install: no destination directory specified.\n"); 1880Sstevel@tonic-gate return (1); 1890Sstevel@tonic-gate } 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate for (c = optind; c < argc; c++) { 1930Sstevel@tonic-gate ins_file = argv[c]; 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate if (dirflg) { 1960Sstevel@tonic-gate struct stat buf; 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate if (stat(ins_file, &buf) == 0) { 1990Sstevel@tonic-gate if ((buf.st_mode & S_IFMT) == S_IFDIR) 2000Sstevel@tonic-gate continue; 2010Sstevel@tonic-gate } else { 2020Sstevel@tonic-gate if (errno != ENOENT) { 2030Sstevel@tonic-gate perror("install: stat"); 2040Sstevel@tonic-gate return (1); 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate (void) strcpy(dest_file, ins_file); 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate if (mkdirp(dest_file, 0755) == -1) { 2110Sstevel@tonic-gate if (!supress) { 2120Sstevel@tonic-gate (void) printf( 2130Sstevel@tonic-gate "install: mkdirp of %s failed\n", 2140Sstevel@tonic-gate dest_file); 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate } else if (!supress) { 2170Sstevel@tonic-gate (void) printf("directory %s created\n", 2180Sstevel@tonic-gate dest_file); 2190Sstevel@tonic-gate } 2200Sstevel@tonic-gate } else { 2210Sstevel@tonic-gate (void) strcat(strcat(strcpy(dest_file, dirb), "/"), 2220Sstevel@tonic-gate find_basename(ins_file)); 2230Sstevel@tonic-gate file_copy(ins_file, dest_file); 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate if (group || owner) 2270Sstevel@tonic-gate chown_file(dest_file, group, owner); 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate if (mode != -1) { 2300Sstevel@tonic-gate umask(0); 2310Sstevel@tonic-gate if (chmod(dest_file, mode) == -1) { 2320Sstevel@tonic-gate perror("chmod"); 2330Sstevel@tonic-gate return (1); 2340Sstevel@tonic-gate } 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate } 2370Sstevel@tonic-gate return (0); 2380Sstevel@tonic-gate } 239