1 /* 2 * Copyright (c) 1987, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static char copyright[] = 36 "@(#) Copyright (c) 1987, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93"; 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/wait.h> 46 #include <sys/mman.h> 47 #include <sys/stat.h> 48 49 #include <ctype.h> 50 #include <errno.h> 51 #include <fcntl.h> 52 #include <grp.h> 53 #include <paths.h> 54 #include <pwd.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <unistd.h> 59 60 #include "pathnames.h" 61 62 struct passwd *pp; 63 struct group *gp; 64 int docopy, dostrip; 65 int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; 66 char *group, *owner, pathbuf[MAXPATHLEN]; 67 68 #define DIRECTORY 0x01 /* Tell install it's a directory. */ 69 #define SETFLAGS 0x02 /* Tell install to set flags. */ 70 71 void copy __P((int, char *, int, char *, off_t)); 72 void err __P((const char *, ...)); 73 void install __P((char *, char *, u_long, u_int)); 74 u_long string_to_flags __P((char **, u_long *, u_long *)); 75 void strip __P((char *)); 76 void usage __P((void)); 77 78 int 79 main(argc, argv) 80 int argc; 81 char *argv[]; 82 { 83 struct stat from_sb, to_sb; 84 mode_t *set; 85 u_long fset; 86 u_int iflags; 87 int ch, no_target; 88 char *flags, *to_name; 89 90 iflags = 0; 91 while ((ch = getopt(argc, argv, "cf:g:m:o:s")) != EOF) 92 switch((char)ch) { 93 case 'c': 94 docopy = 1; 95 break; 96 case 'f': 97 flags = optarg; 98 if (string_to_flags(&flags, &fset, NULL)) 99 err("%s: invalid flag", flags); 100 iflags |= SETFLAGS; 101 break; 102 case 'g': 103 group = optarg; 104 break; 105 case 'm': 106 if (!(set = setmode(optarg))) 107 err("%s: invalid file mode", optarg); 108 mode = getmode(set, 0); 109 break; 110 case 'o': 111 owner = optarg; 112 break; 113 case 's': 114 dostrip = 1; 115 break; 116 case '?': 117 default: 118 usage(); 119 } 120 argc -= optind; 121 argv += optind; 122 if (argc < 2) 123 usage(); 124 125 /* get group and owner id's */ 126 if (group && !(gp = getgrnam(group))) 127 err("unknown group %s", group); 128 if (owner && !(pp = getpwnam(owner))) 129 err("unknown user %s", owner); 130 131 no_target = stat(to_name = argv[argc - 1], &to_sb); 132 if (!no_target && (to_sb.st_mode & S_IFMT) == S_IFDIR) { 133 for (; *argv != to_name; ++argv) 134 install(*argv, to_name, fset, iflags | DIRECTORY); 135 exit(0); 136 } 137 138 /* can't do file1 file2 directory/file */ 139 if (argc != 2) 140 usage(); 141 142 if (!no_target) { 143 if (stat(*argv, &from_sb)) 144 err("%s: %s", *argv, strerror(errno)); 145 if (!S_ISREG(to_sb.st_mode)) 146 err("%s: %s", to_name, strerror(EFTYPE)); 147 if (to_sb.st_dev == from_sb.st_dev && 148 to_sb.st_ino == from_sb.st_ino) 149 err("%s and %s are the same file", *argv, to_name); 150 /* 151 * Unlink now... avoid ETXTBSY errors later. Try and turn 152 * off the append/immutable bits -- if we fail, go ahead, 153 * it might work. 154 */ 155 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND) 156 if (to_sb.st_flags & NOCHANGEBITS) 157 (void)chflags(to_name, 158 to_sb.st_flags & ~(NOCHANGEBITS)); 159 (void)unlink(to_name); 160 } 161 install(*argv, to_name, fset, iflags); 162 exit(0); 163 } 164 165 /* 166 * install -- 167 * build a path name and install the file 168 */ 169 void 170 install(from_name, to_name, fset, flags) 171 char *from_name, *to_name; 172 u_long fset; 173 u_int flags; 174 { 175 struct stat from_sb, to_sb; 176 int devnull, from_fd, to_fd, serrno; 177 char *p; 178 179 /* If try to install NULL file to a directory, fails. */ 180 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) { 181 if (stat(from_name, &from_sb)) 182 err("%s: %s", from_name, strerror(errno)); 183 if (!S_ISREG(from_sb.st_mode)) 184 err("%s: %s", from_name, strerror(EFTYPE)); 185 /* Build the target path. */ 186 if (flags & DIRECTORY) { 187 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s", 188 to_name, 189 (p = rindex(from_name, '/')) ? ++p : from_name); 190 to_name = pathbuf; 191 } 192 devnull = 0; 193 } else { 194 from_sb.st_flags = 0; /* XXX */ 195 devnull = 1; 196 } 197 198 /* 199 * Unlink now... avoid ETXTBSY errors later. Try and turn 200 * off the append/immutable bits -- if we fail, go ahead, 201 * it might work. 202 */ 203 if (stat(to_name, &to_sb) == 0 && 204 to_sb.st_flags & (NOCHANGEBITS)) 205 (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS)); 206 (void)unlink(to_name); 207 208 /* Create target. */ 209 if ((to_fd = open(to_name, 210 O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0) 211 err("%s: %s", to_name, strerror(errno)); 212 if (!devnull) { 213 if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) { 214 (void)unlink(to_name); 215 err("%s: %s", from_name, strerror(errno)); 216 } 217 copy(from_fd, from_name, to_fd, to_name, from_sb.st_size); 218 (void)close(from_fd); 219 } 220 if (dostrip) 221 strip(to_name); 222 /* 223 * Set owner, group, mode for target; do the chown first, 224 * chown may lose the setuid bits. 225 */ 226 if ((group || owner) && 227 fchown(to_fd, owner ? pp->pw_uid : -1, group ? gp->gr_gid : -1)) { 228 serrno = errno; 229 (void)unlink(to_name); 230 err("%s: chown/chgrp: %s", to_name, strerror(serrno)); 231 } 232 if (fchmod(to_fd, mode)) { 233 serrno = errno; 234 (void)unlink(to_name); 235 err("%s: chmod: %s", to_name, strerror(serrno)); 236 } 237 238 /* 239 * If provided a set of flags, set them, otherwise, preserve the 240 * flags, except for the dump flag. 241 */ 242 if (fchflags(to_fd, 243 flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) { 244 serrno = errno; 245 (void)unlink(to_name); 246 err("%s: chflags: %s", to_name, strerror(serrno)); 247 } 248 249 (void)close(to_fd); 250 if (!docopy && !devnull && unlink(from_name)) 251 err("%s: %s", from_name, strerror(errno)); 252 } 253 254 /* 255 * copy -- 256 * copy from one file to another 257 */ 258 void 259 copy(from_fd, from_name, to_fd, to_name, size) 260 register int from_fd, to_fd; 261 char *from_name, *to_name; 262 off_t size; 263 { 264 register int nr, nw; 265 int serrno; 266 char *p, buf[MAXBSIZE]; 267 268 /* 269 * Mmap and write if less than 8M (the limit is so we don't totally 270 * trash memory on big files. This is really a minor hack, but it 271 * wins some CPU back. 272 */ 273 if (size <= 8 * 1048576) { 274 if ((p = mmap(NULL, (size_t)size, PROT_READ, 275 0, from_fd, (off_t)0)) == (char *)-1) 276 err("%s: %s", from_name, strerror(errno)); 277 if (write(to_fd, p, size) != size) 278 err("%s: %s", to_name, strerror(errno)); 279 } else { 280 while ((nr = read(from_fd, buf, sizeof(buf))) > 0) 281 if ((nw = write(to_fd, buf, nr)) != nr) { 282 serrno = errno; 283 (void)unlink(to_name); 284 err("%s: %s", 285 to_name, strerror(nw > 0 ? EIO : serrno)); 286 } 287 if (nr != 0) { 288 serrno = errno; 289 (void)unlink(to_name); 290 err("%s: %s", from_name, strerror(serrno)); 291 } 292 } 293 } 294 295 /* 296 * strip -- 297 * use strip(1) to strip the target file 298 */ 299 void 300 strip(to_name) 301 char *to_name; 302 { 303 int serrno, status; 304 305 switch (vfork()) { 306 case -1: 307 serrno = errno; 308 (void)unlink(to_name); 309 err("forks: %s", strerror(errno)); 310 case 0: 311 execl(_PATH_STRIP, "strip", to_name, NULL); 312 err("%s: %s", _PATH_STRIP, strerror(errno)); 313 default: 314 if (wait(&status) == -1 || status) 315 (void)unlink(to_name); 316 } 317 } 318 319 /* 320 * usage -- 321 * print a usage message and die 322 */ 323 void 324 usage() 325 { 326 (void)fprintf(stderr, 327 "usage: install [-cs] [-f flags] [-g group] [-m mode] [-o owner] file1 file2;\n\tor file1 ... fileN directory\n"); 328 exit(1); 329 } 330 331 #if __STDC__ 332 #include <stdarg.h> 333 #else 334 #include <varargs.h> 335 #endif 336 337 void 338 #if __STDC__ 339 err(const char *fmt, ...) 340 #else 341 err(fmt, va_alist) 342 char *fmt; 343 va_dcl 344 #endif 345 { 346 va_list ap; 347 #if __STDC__ 348 va_start(ap, fmt); 349 #else 350 va_start(ap); 351 #endif 352 (void)fprintf(stderr, "install: "); 353 (void)vfprintf(stderr, fmt, ap); 354 va_end(ap); 355 (void)fprintf(stderr, "\n"); 356 exit(1); 357 /* NOTREACHED */ 358 } 359