1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 1996 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 /* 31 * University Copyright- Copyright (c) 1982, 1986, 1988 32 * The Regents of the University of California 33 * All Rights Reserved 34 * 35 * University Acknowledgment- Portions of this document are derived from 36 * software developed by the University of California, Berkeley, and its 37 * contributors. 38 */ 39 40 #include <sys/param.h> 41 #include <sys/types.h> 42 #include <sys/stat.h> 43 #include <sys/file.h> 44 #include <fcntl.h> 45 #include <grp.h> 46 #include <pwd.h> 47 #include <stdio.h> 48 #include <ctype.h> 49 #include <errno.h> 50 #include <locale.h> 51 52 #define DEF_GROUP "staff" /* default group */ 53 #define DEF_OWNER "root" /* default owner */ 54 #define DEF_MODE 0755 /* default mode */ 55 56 char *group = DEF_GROUP; 57 char *owner = DEF_OWNER; 58 int mode = DEF_MODE; 59 int sflag = 0; 60 struct passwd *pp; 61 struct group *gp; 62 extern int errno; 63 int copy(); 64 void usage(); 65 66 int 67 main(int argc, char **argv) 68 { 69 extern char *optarg; 70 extern int optind; 71 struct stat stb; 72 char *dirname; 73 int ch; 74 int i; 75 int rc; 76 int dflag = 0; 77 int gflag = 0; 78 int oflag = 0; 79 int mflag = 0; 80 81 (void) setlocale(LC_ALL, ""); 82 83 #if !defined(TEXT_DOMAIN) 84 #define TEXT_DOMAIN "SYS_TEST" 85 #endif 86 (void) textdomain(TEXT_DOMAIN); 87 88 while ((ch = getopt(argc, argv, "dcg:o:m:s")) != EOF) 89 switch((char)ch) { 90 case 'c': 91 break; /* always do "copy" */ 92 case 'd': 93 dflag++; 94 break; 95 case 'g': 96 gflag++; 97 group = optarg; 98 break; 99 case 'm': 100 mflag++; 101 mode = atoo(optarg); 102 break; 103 case 'o': 104 oflag++; 105 owner = optarg; 106 break; 107 case 's': 108 sflag++; 109 break; 110 case '?': 111 default: 112 usage(); 113 } 114 argc -= optind; 115 argv += optind; 116 117 /* get group and owner id's */ 118 if (!(gp = getgrnam(group))) { 119 fprintf(stderr, gettext("install: unknown group %s.\n"), group); 120 exit(1); 121 } 122 if (!(pp = getpwnam(owner))) { 123 fprintf(stderr, gettext("install: unknown user %s.\n"), owner); 124 exit(1); 125 } 126 127 if (dflag) { /* install a directory */ 128 int exists = 0; 129 130 if (argc != 1) 131 usage(); 132 dirname = argv[0]; 133 if (mkdirp(dirname, 0777) < 0) { 134 exists = errno == EEXIST; 135 if (!exists) { 136 fprintf(stderr, gettext("install: mkdir: %s: %s\n"), dirname, strerror(errno)); 137 exit(1); 138 } 139 } 140 if (stat(dirname, &stb) < 0) { 141 fprintf(stderr, gettext("install: stat: %s: %s\n"), dirname, strerror(errno)); 142 exit(1); 143 } 144 if ((stb.st_mode&S_IFMT) != S_IFDIR) { 145 fprintf(stderr, gettext("install: %s is not a directory\n"), dirname); 146 } 147 /* make sure directory setgid bit is inherited */ 148 mode = (mode & ~S_ISGID) | (stb.st_mode & S_ISGID); 149 if (mflag && chmod(dirname, mode)) { 150 fprintf(stderr, gettext("install: chmod: %s: %s\n"), dirname, strerror(errno)); 151 if (!exists) 152 (void) unlink(dirname); 153 exit(1) ; 154 } 155 if (oflag && chown(dirname, pp->pw_uid, -1) && errno != EPERM) { 156 fprintf(stderr, gettext("install: chown: %s: %s\n"), dirname, strerror(errno)); 157 if (!exists) 158 (void) unlink(dirname); 159 exit(1) ; 160 } 161 if (gflag && chown(dirname, -1, gp->gr_gid) && errno != EPERM) { 162 fprintf(stderr, gettext("install: chgrp: %s: %s\n"), dirname, strerror(errno)); 163 if (!exists) 164 (void) unlink(dirname); 165 exit(1) ; 166 } 167 exit(0); 168 } 169 170 if (argc < 2) 171 usage(); 172 173 if (argc > 2) { /* last arg must be a directory */ 174 if (stat(argv[argc-1], &stb) < 0) 175 usage(); 176 if ((stb.st_mode&S_IFMT) != S_IFDIR) 177 usage(); 178 } 179 rc = 0; 180 for (i = 0; i < argc-1; i++) 181 rc |= install(argv[i], argv[argc-1]); 182 return (rc); 183 } 184 185 int 186 install(from, to) 187 char *from, *to; 188 { 189 int to_fd; 190 int devnull; 191 int status = 0; 192 char *path; 193 struct stat from_sb, to_sb; 194 static char pbuf[MAXPATHLEN]; 195 char buf[MAXPATHLEN + 10]; 196 197 /* check source */ 198 if (stat(from, &from_sb)) { 199 fprintf(stderr, gettext("install: %s: %s\n"), from, strerror(errno)); 200 return (1); 201 } 202 /* special case for removing files */ 203 devnull = !strcmp(from, "/dev/null"); 204 if (!devnull && !((from_sb.st_mode&S_IFMT) == S_IFREG)) { 205 fprintf(stderr, gettext("install: %s isn't a regular file.\n"), from); 206 return (1); 207 } 208 209 /* build target path, find out if target is same as source */ 210 if (!stat(path = to, &to_sb)) { 211 if ((to_sb.st_mode&S_IFMT) == S_IFDIR) { 212 char *C, *strrchr(); 213 214 (void) sprintf(path = pbuf, "%s/%s", to, (C = strrchr(from, '/')) ? ++C : from); 215 if (stat(path, &to_sb)) 216 goto nocompare; 217 } 218 if ((to_sb.st_mode&S_IFMT) != S_IFREG) { 219 fprintf(stderr, gettext("install: %s isn't a regular file.\n"), path); 220 return (1); 221 } 222 if (to_sb.st_dev == from_sb.st_dev && to_sb.st_ino == from_sb.st_ino) { 223 fprintf(stderr, gettext("install: %s and %s are the same file.\n"), from, path); 224 return (1); 225 } 226 /* unlink now... avoid ETXTBSY errors later */ 227 (void) unlink(path); 228 } 229 230 nocompare: 231 /* open target, set mode, owner, group */ 232 if ((to_fd = open(path, O_CREAT|O_WRONLY|O_TRUNC, 0)) < 0) { 233 fprintf(stderr, gettext("install: %s: %s\n"), path, strerror(errno)); 234 return (1); 235 } 236 if (fchmod(to_fd, mode)) { 237 fprintf(stderr, gettext("install: chmod: %s: %s\n"), path, strerror(errno)); 238 status = 1; 239 close(to_fd); 240 goto inst_done; 241 } 242 if (!devnull) { 243 status = copy(from, to_fd, path); /* copy */ 244 close(to_fd); 245 } 246 if (sflag) { 247 sprintf(buf, "strip %s", path); 248 system(buf); 249 } 250 if (chown(path, pp->pw_uid, gp->gr_gid) && errno != EPERM) { 251 fprintf(stderr, gettext("install: chown: %s: %s\n"), path, strerror(errno)); 252 status = 1; 253 } 254 255 inst_done: 256 if (status) 257 (void) unlink(path); 258 return (status); 259 } 260 261 /* 262 * copy -- 263 * copy from one file to another 264 */ 265 int 266 copy(from_name, to_fd, to_name) 267 int to_fd; 268 char *from_name, *to_name; 269 { 270 int n, from_fd; 271 int status = 0; 272 char buf[MAXBSIZE]; 273 274 if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) { 275 fprintf(stderr, gettext("install: open: %s: %s\n"), from_name, strerror(errno)); 276 return (1); 277 } 278 while ((n = read(from_fd, buf, sizeof(buf))) > 0) 279 if (write(to_fd, buf, n) != n) { 280 fprintf(stderr, gettext("install: write: %s: %s\n"), to_name, strerror(errno)); 281 status = 1; 282 goto copy_done; 283 } 284 if (n == -1) { 285 fprintf(stderr, gettext("install: read: %s: %s\n"), from_name, strerror(errno)); 286 status = 1; 287 goto copy_done; 288 } 289 290 copy_done: 291 (void) close(from_fd); 292 return (status); 293 } 294 295 /* 296 * atoo -- 297 * octal string to int 298 */ 299 int 300 atoo(str) 301 char *str; 302 { 303 int val; 304 305 for (val = 0; isdigit(*str); ++str) 306 val = val * 8 + *str - '0'; 307 return(val); 308 } 309 310 311 /* 312 * usage -- 313 * print a usage message and die 314 */ 315 void 316 usage() 317 { 318 fputs(gettext("usage: install [-cs] [-g group] [-m mode] [-o owner] file ... destination\n"), stderr); 319 fputs(gettext(" install -d [-g group] [-m mode] [-o owner] dir\n"), stderr); 320 exit(1); 321 } 322 323 /* 324 * mkdirp -- 325 * make a directory and parents if needed 326 */ 327 int 328 mkdirp(dir, mode) 329 char *dir; 330 int mode; 331 { 332 int err; 333 char *slash; 334 char *strrchr(); 335 extern int errno; 336 337 if (mkdir(dir, mode) == 0) 338 return (0); 339 if (errno != ENOENT) 340 return (-1); 341 slash = strrchr(dir, '/'); 342 if (slash == NULL) 343 return (-1); 344 *slash = '\0'; 345 err = mkdirp(dir, 0777); 346 *slash = '/'; 347 if (err) 348 return (err); 349 return mkdir(dir, mode); 350 } 351