Annotation of src/usr.bin/rdist/isexec.c, Revision 1.1
1.1 ! dm 1: /*
! 2: * Copyright (c) 1983 Regents of the University of California.
! 3: * 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: #ifndef lint
! 34: static char RCSid[] =
! 35: "$Id: isexec.c,v 6.21 1994/04/01 23:44:10 mcooper Exp $";
! 36:
! 37: static char sccsid[] = "@(#)client.c";
! 38:
! 39: static char copyright[] =
! 40: "@(#) Copyright (c) 1983 Regents of the University of California.\n\
! 41: All rights reserved.\n";
! 42: #endif /* not lint */
! 43:
! 44:
! 45: #include "defs.h"
! 46:
! 47: #if EXE_TYPE == EXE_AOUT
! 48: /*
! 49: * BSD style A.OUT
! 50: */
! 51: #include <a.out.h>
! 52:
! 53: static int _isexec(fd)
! 54: int fd;
! 55: {
! 56: struct exec ehdr;
! 57:
! 58: if ((read(fd, &ehdr, sizeof(ehdr)) == sizeof(ehdr)) &&
! 59: !N_BADMAG(ehdr))
! 60: return(TRUE);
! 61: else
! 62: return(FALSE);
! 63: }
! 64: #endif /* EXE_AOUT */
! 65:
! 66:
! 67: #if EXE_TYPE == EXE_ELF_AND_COFF || EXE_TYPE == EXE_ELF
! 68: /*
! 69: * Elf
! 70: */
! 71: #include <elf.h>
! 72: #define ISELF(h) (h.e_type == ET_EXEC)
! 73: #endif /* EXE_ELF_AND_COFF || EXE_ELF */
! 74:
! 75: #if EXE_TYPE == EXE_ELF_AND_COFF || EXE_TYPE == EXE_COFF
! 76:
! 77: /*
! 78: * COFF
! 79: */
! 80: #if defined(FILEHDR_H)
! 81: #include FILEHDR_H
! 82: #endif /* FILEHDR_H */
! 83:
! 84: #if !defined(ISCOFF)
! 85:
! 86: /*
! 87: * Stupid AIX
! 88: */
! 89: #if defined(U802WRMAGIC) && defined(U802ROMAGIC) && defined(U802TOCMAGIC)
! 90: #define ISCOFF(x) (((x)==U802WRMAGIC) || ((x)==U802TOCMAGIC) || \
! 91: ((x)==U802TOCMAGIC))
! 92: #endif /* U802... */
! 93: /*
! 94: * Stupid Umax4.3
! 95: */
! 96: #if defined(NS32GMAGIC) || defined(NS32SMAGIC)
! 97: #define ISCOFF(x) (((x)==NS32GMAGIC) || ((x)==NS32SMAGIC))
! 98: #endif /* NS32 ... */
! 99:
! 100: #endif /* ISCOFF */
! 101:
! 102: #endif /* EXE_TYPE == EXE_ELF_AND_COFF || EXE_TYPE == EXE_COFF */
! 103:
! 104: #if EXE_TYPE == EXE_ELF_AND_COFF
! 105: /*
! 106: * ELF and COFF
! 107: */
! 108: typedef union {
! 109: struct filehdr coffhdr;
! 110: Elf32_Ehdr elfhdr;
! 111: } hdr_t;
! 112: #endif /* EXE_TYPE == EXE_ELF_AND_COFF */
! 113:
! 114: #if EXE_TYPE == EXE_ELF
! 115: /*
! 116: * Elf
! 117: */
! 118: #include <elf.h>
! 119: typedef Elf32_Ehdr hdr_t;
! 120: #endif /* EXE_TYPE == EXE_ELF */
! 121:
! 122: #if EXE_TYPE == EXE_COFF
! 123: /*
! 124: * COFF
! 125: */
! 126:
! 127: #if defined(FILEHDR_H)
! 128: #include FILEHDR_H
! 129: #endif /* FILEHDR_H */
! 130:
! 131: typedef struct filehdr hdr_t;
! 132: #endif /* EXE_TYPE == EXE_COFF */
! 133:
! 134: #if EXE_TYPE == EXE_ELF_AND_COFF || EXE_TYPE == EXE_ELF || EXE_TYPE == EXE_COFF
! 135: /*
! 136: * System V style COFF and System V R4 style ELF
! 137: */
! 138: static int _isexec(fd)
! 139: int fd;
! 140: {
! 141: hdr_t hdr;
! 142:
! 143: if (read(fd, &hdr, sizeof(hdr)) == sizeof(hdr)) {
! 144: #if EXE_TYPE == EXE_ELF_AND_COFF
! 145: if (ISELF(hdr.elfhdr) || ISCOFF(hdr.coffhdr.f_magic))
! 146: return(TRUE);
! 147: #endif
! 148: #if EXE_TYPE == EXE_ELF
! 149: if (ISELF(hdr))
! 150: return(TRUE);
! 151: #endif
! 152: #if EXE_TYPE == EXE_COFF
! 153: if (ISCOFF(hdr.f_magic))
! 154: return(TRUE);
! 155: #endif
! 156: }
! 157:
! 158: return(FALSE);
! 159: }
! 160: #endif /* EXE_ELF_AND_COFF */
! 161:
! 162:
! 163: #if EXE_TYPE == EXE_MACHO
! 164: /*
! 165: * Mach-O format
! 166: */
! 167:
! 168: #if defined(NEXTSTEP) && NEXTSTEP >= 3
! 169: # include <mach-o/loader.h>
! 170: #else
! 171: # include <sys/loader.h>
! 172: #endif /* NEXTSTEP */
! 173:
! 174: #ifndef MH_CIGAM
! 175: #define MH_CIGAM 0xcefaedfe
! 176: #endif
! 177: #ifndef FAT_MAGIC
! 178: #define FAT_MAGIC 0xcafebabe
! 179: #endif
! 180: #ifndef FAT_CIGAM
! 181: #define FAT_CIGAM 0xbebafeca
! 182: #endif
! 183:
! 184: static int _isexec(fd)
! 185: int fd;
! 186: {
! 187: struct mach_header ehdr;
! 188:
! 189: if ((read(fd, &ehdr, sizeof(ehdr)) == sizeof(ehdr)) &&
! 190: (ehdr.magic == MH_MAGIC || ehdr.magic == MH_CIGAM ||
! 191: ehdr.magic == FAT_MAGIC || ehdr.magic == FAT_CIGAM))
! 192: return(TRUE);
! 193: else
! 194: return(FALSE);
! 195: }
! 196: #endif /* EXE_COFF */
! 197:
! 198:
! 199: #if EXE_TYPE == EXE_HPEXEC
! 200: /*
! 201: * HP 9000 executable format
! 202: */
! 203:
! 204: #ifdef hp9000s300
! 205:
! 206: #include <a.out.h>
! 207: #define header exec
! 208: #define ISEXEC(a) ((a.file_type)==EXEC_MAGIC || (a.file_type)==SHARE_MAGIC || \
! 209: (a.file_type)==DEMAND_MAGIC)
! 210:
! 211: #else /* ! hp9000s300 */
! 212:
! 213: #define ISEXEC(a) ((a)==EXEC_MAGIC || (a)==SHARE_MAGIC || (a)==DEMAND_MAGIC)
! 214: #include <filehdr.h>
! 215:
! 216: #endif /* hp9000s300 */
! 217:
! 218: static int _isexec(fd)
! 219: int fd;
! 220: {
! 221: struct header ehdr;
! 222:
! 223: if ((read(fd, &ehdr, sizeof(ehdr)) == sizeof(ehdr)) &&
! 224: ISEXEC(ehdr.a_magic))
! 225: return(TRUE);
! 226: else
! 227: return(FALSE);
! 228: }
! 229: #endif /* EXE_HPEXEC */
! 230:
! 231:
! 232: #if !defined(EXE_TYPE)
! 233: /*
! 234: * Fake _isexec() call for unknown executable formats.
! 235: */
! 236: static int _isexec(fd)
! 237: /*ARGSUSED*/
! 238: int fd;
! 239: {
! 240: return(FALSE);
! 241: }
! 242: #endif /* !defined(EXE_TYPE) */
! 243:
! 244: /*
! 245: * Determine whether 'file' is an executable or not.
! 246: */
! 247: extern int isexec(file, statp)
! 248: char *file;
! 249: struct stat *statp;
! 250: {
! 251: int fd, r;
! 252:
! 253: /*
! 254: * Must be a regular file that has some executable mode bit on
! 255: */
! 256: if (!S_ISREG(statp->st_mode) ||
! 257: !(statp->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
! 258: return(FALSE);
! 259:
! 260: if ((fd = open(file, O_RDONLY, 0)) < 0)
! 261: return(FALSE);
! 262: r = _isexec(fd);
! 263: (void) close(fd);
! 264:
! 265: return(r);
! 266: }
! 267: