Annotation of src/usr.bin/rdist/isexec.c, Revision 1.5
1.5 ! millert 1: /* $OpenBSD: isexec.c,v 1.4 1998/06/26 21:21:12 millert Exp $ */
1.3 deraadt 2:
1.1 dm 3: /*
4: * Copyright (c) 1983 Regents of the University of California.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35: #ifndef lint
1.4 millert 36: #if 0
1.1 dm 37: static char RCSid[] =
1.4 millert 38: "$From: isexec.c,v 6.21 1994/04/01 23:44:10 mcooper Exp $";
39: #else
40: static char RCSid[] =
1.5 ! millert 41: "$OpenBSD: isexec.c,v 1.4 1998/06/26 21:21:12 millert Exp $";
1.4 millert 42: #endif
1.1 dm 43:
44: static char sccsid[] = "@(#)client.c";
45:
46: static char copyright[] =
47: "@(#) Copyright (c) 1983 Regents of the University of California.\n\
48: All rights reserved.\n";
49: #endif /* not lint */
50:
51:
52: #include "defs.h"
53:
54: #if EXE_TYPE == EXE_AOUT
55: /*
56: * BSD style A.OUT
57: */
58: #include <a.out.h>
59:
60: static int _isexec(fd)
61: int fd;
62: {
63: struct exec ehdr;
64:
65: if ((read(fd, &ehdr, sizeof(ehdr)) == sizeof(ehdr)) &&
66: !N_BADMAG(ehdr))
67: return(TRUE);
68: else
69: return(FALSE);
70: }
71: #endif /* EXE_AOUT */
72:
73:
74: #if EXE_TYPE == EXE_ELF_AND_COFF || EXE_TYPE == EXE_ELF
75: /*
76: * Elf
77: */
1.5 ! millert 78: #include <elf_abi.h>
1.1 dm 79: #define ISELF(h) (h.e_type == ET_EXEC)
80: #endif /* EXE_ELF_AND_COFF || EXE_ELF */
81:
82: #if EXE_TYPE == EXE_ELF_AND_COFF || EXE_TYPE == EXE_COFF
83:
84: /*
85: * COFF
86: */
87: #if defined(FILEHDR_H)
88: #include FILEHDR_H
89: #endif /* FILEHDR_H */
90:
91: #if !defined(ISCOFF)
92:
93: /*
94: * Stupid AIX
95: */
96: #if defined(U802WRMAGIC) && defined(U802ROMAGIC) && defined(U802TOCMAGIC)
97: #define ISCOFF(x) (((x)==U802WRMAGIC) || ((x)==U802TOCMAGIC) || \
98: ((x)==U802TOCMAGIC))
99: #endif /* U802... */
100: /*
101: * Stupid Umax4.3
102: */
103: #if defined(NS32GMAGIC) || defined(NS32SMAGIC)
104: #define ISCOFF(x) (((x)==NS32GMAGIC) || ((x)==NS32SMAGIC))
105: #endif /* NS32 ... */
106:
107: #endif /* ISCOFF */
108:
109: #endif /* EXE_TYPE == EXE_ELF_AND_COFF || EXE_TYPE == EXE_COFF */
110:
111: #if EXE_TYPE == EXE_ELF_AND_COFF
112: /*
113: * ELF and COFF
114: */
115: typedef union {
116: struct filehdr coffhdr;
117: Elf32_Ehdr elfhdr;
118: } hdr_t;
119: #endif /* EXE_TYPE == EXE_ELF_AND_COFF */
120:
121: #if EXE_TYPE == EXE_ELF
122: /*
123: * Elf
124: */
125: typedef Elf32_Ehdr hdr_t;
126: #endif /* EXE_TYPE == EXE_ELF */
127:
128: #if EXE_TYPE == EXE_COFF
129: /*
130: * COFF
131: */
132:
133: #if defined(FILEHDR_H)
134: #include FILEHDR_H
135: #endif /* FILEHDR_H */
136:
137: typedef struct filehdr hdr_t;
138: #endif /* EXE_TYPE == EXE_COFF */
139:
140: #if EXE_TYPE == EXE_ELF_AND_COFF || EXE_TYPE == EXE_ELF || EXE_TYPE == EXE_COFF
141: /*
142: * System V style COFF and System V R4 style ELF
143: */
144: static int _isexec(fd)
145: int fd;
146: {
147: hdr_t hdr;
148:
149: if (read(fd, &hdr, sizeof(hdr)) == sizeof(hdr)) {
150: #if EXE_TYPE == EXE_ELF_AND_COFF
151: if (ISELF(hdr.elfhdr) || ISCOFF(hdr.coffhdr.f_magic))
152: return(TRUE);
153: #endif
154: #if EXE_TYPE == EXE_ELF
155: if (ISELF(hdr))
156: return(TRUE);
157: #endif
158: #if EXE_TYPE == EXE_COFF
159: if (ISCOFF(hdr.f_magic))
160: return(TRUE);
161: #endif
162: }
163:
164: return(FALSE);
165: }
166: #endif /* EXE_ELF_AND_COFF */
167:
168:
169: #if EXE_TYPE == EXE_MACHO
170: /*
171: * Mach-O format
172: */
173:
174: #if defined(NEXTSTEP) && NEXTSTEP >= 3
175: # include <mach-o/loader.h>
176: #else
177: # include <sys/loader.h>
178: #endif /* NEXTSTEP */
179:
180: #ifndef MH_CIGAM
181: #define MH_CIGAM 0xcefaedfe
182: #endif
183: #ifndef FAT_MAGIC
184: #define FAT_MAGIC 0xcafebabe
185: #endif
186: #ifndef FAT_CIGAM
187: #define FAT_CIGAM 0xbebafeca
188: #endif
189:
190: static int _isexec(fd)
191: int fd;
192: {
193: struct mach_header ehdr;
194:
195: if ((read(fd, &ehdr, sizeof(ehdr)) == sizeof(ehdr)) &&
196: (ehdr.magic == MH_MAGIC || ehdr.magic == MH_CIGAM ||
197: ehdr.magic == FAT_MAGIC || ehdr.magic == FAT_CIGAM))
198: return(TRUE);
199: else
200: return(FALSE);
201: }
202: #endif /* EXE_COFF */
203:
204:
205: #if EXE_TYPE == EXE_HPEXEC
206: /*
207: * HP 9000 executable format
208: */
209:
210: #ifdef hp9000s300
211:
212: #include <a.out.h>
213: #define header exec
214: #define ISEXEC(a) ((a.file_type)==EXEC_MAGIC || (a.file_type)==SHARE_MAGIC || \
215: (a.file_type)==DEMAND_MAGIC)
216:
217: #else /* ! hp9000s300 */
218:
219: #define ISEXEC(a) ((a)==EXEC_MAGIC || (a)==SHARE_MAGIC || (a)==DEMAND_MAGIC)
220: #include <filehdr.h>
221:
222: #endif /* hp9000s300 */
223:
224: static int _isexec(fd)
225: int fd;
226: {
227: struct header ehdr;
228:
229: if ((read(fd, &ehdr, sizeof(ehdr)) == sizeof(ehdr)) &&
230: ISEXEC(ehdr.a_magic))
231: return(TRUE);
232: else
233: return(FALSE);
234: }
235: #endif /* EXE_HPEXEC */
236:
237:
238: #if !defined(EXE_TYPE)
239: /*
240: * Fake _isexec() call for unknown executable formats.
241: */
242: static int _isexec(fd)
243: /*ARGSUSED*/
244: int fd;
245: {
246: return(FALSE);
247: }
248: #endif /* !defined(EXE_TYPE) */
249:
250: /*
251: * Determine whether 'file' is an executable or not.
252: */
253: extern int isexec(file, statp)
254: char *file;
255: struct stat *statp;
256: {
257: int fd, r;
258:
259: /*
260: * Must be a regular file that has some executable mode bit on
261: */
262: if (!S_ISREG(statp->st_mode) ||
263: !(statp->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
264: return(FALSE);
265:
266: if ((fd = open(file, O_RDONLY, 0)) < 0)
267: return(FALSE);
268: r = _isexec(fd);
269: (void) close(fd);
270:
271: return(r);
272: }
273: