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 1.1 1995-07-11 19:25:10 miki 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 "config-def.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 |
---|
74 | |
---|
75 | #if EXE_TYPE == EXE_ELF_AND_COFF || EXE_TYPE == EXE_COFF |
---|
76 | /* |
---|
77 | * COFF |
---|
78 | */ |
---|
79 | #if !defined(CXUX) |
---|
80 | #include <filehdr.h> |
---|
81 | #endif /* CXUX */ |
---|
82 | |
---|
83 | #if !defined(ISCOFF) |
---|
84 | |
---|
85 | /* |
---|
86 | * Stupid AIX |
---|
87 | */ |
---|
88 | #if defined(U802WRMAGIC) && defined(U802ROMAGIC) && defined(U802TOCMAGIC) |
---|
89 | #define ISCOFF(x) (((x)==U802WRMAGIC) || ((x)==U802TOCMAGIC) || \ |
---|
90 | ((x)==U802TOCMAGIC)) |
---|
91 | #endif /* U802... */ |
---|
92 | /* |
---|
93 | * Stupid Umax4.3 |
---|
94 | */ |
---|
95 | #if defined(NS32GMAGIC) || defined(NS32SMAGIC) |
---|
96 | #define ISCOFF(x) (((x)==NS32GMAGIC) || ((x)==NS32SMAGIC)) |
---|
97 | #endif /* NS32 ... */ |
---|
98 | |
---|
99 | #endif /* ISCOFF */ |
---|
100 | |
---|
101 | #endif |
---|
102 | |
---|
103 | #if EXE_TYPE == EXE_ELF_AND_COFF |
---|
104 | /* |
---|
105 | * ELF and COFF |
---|
106 | */ |
---|
107 | typedef union { |
---|
108 | struct filehdr coffhdr; |
---|
109 | Elf32_Ehdr elfhdr; |
---|
110 | } hdr_t; |
---|
111 | #endif /* EXE_TYPE == EXE_ELF_AND_COFF */ |
---|
112 | |
---|
113 | #if EXE_TYPE == EXE_ELF |
---|
114 | /* |
---|
115 | * Elf |
---|
116 | */ |
---|
117 | #include <elf.h> |
---|
118 | typedef Elf32_Ehdr hdr_t; |
---|
119 | #endif /* EXE_TYPE == EXE_ELF */ |
---|
120 | |
---|
121 | #if EXE_TYPE == EXE_COFF |
---|
122 | /* |
---|
123 | * COFF |
---|
124 | */ |
---|
125 | |
---|
126 | #if !defined(STELLIX) |
---|
127 | #include <filehdr.h> |
---|
128 | #endif /* !defined(STELLIX) */ |
---|
129 | |
---|
130 | typedef struct filehdr hdr_t; |
---|
131 | #endif /* EXE_TYPE == EXE_COFF */ |
---|
132 | |
---|
133 | #if EXE_TYPE == EXE_ELF_AND_COFF || EXE_TYPE == EXE_ELF || EXE_TYPE == EXE_COFF |
---|
134 | /* |
---|
135 | * System V style COFF and System V R4 style ELF |
---|
136 | */ |
---|
137 | static int _isexec(fd) |
---|
138 | int fd; |
---|
139 | { |
---|
140 | hdr_t hdr; |
---|
141 | |
---|
142 | if (read(fd, &hdr, sizeof(hdr)) == sizeof(hdr)) { |
---|
143 | #if EXE_TYPE == EXE_ELF_AND_COFF |
---|
144 | if (ISELF(hdr.elfhdr) || ISCOFF(hdr.coffhdr.f_magic)) |
---|
145 | return(TRUE); |
---|
146 | #endif |
---|
147 | #if EXE_TYPE == EXE_ELF |
---|
148 | if (ISELF(hdr)) |
---|
149 | return(TRUE); |
---|
150 | #endif |
---|
151 | #if EXE_TYPE == EXE_COFF |
---|
152 | if (ISCOFF(hdr.f_magic)) |
---|
153 | return(TRUE); |
---|
154 | #endif |
---|
155 | } |
---|
156 | |
---|
157 | return(FALSE); |
---|
158 | } |
---|
159 | #endif /* EXE_ELF_AND_COFF */ |
---|
160 | |
---|
161 | |
---|
162 | #if EXE_TYPE == EXE_MACHO |
---|
163 | /* |
---|
164 | * Mach-O format |
---|
165 | */ |
---|
166 | |
---|
167 | #if defined(NEXTSTEP) && NEXTSTEP >= 3 |
---|
168 | # include <mach-o/loader.h> |
---|
169 | #else |
---|
170 | # include <sys/loader.h> |
---|
171 | #endif /* NEXTSTEP */ |
---|
172 | |
---|
173 | #ifndef MH_CIGAM |
---|
174 | #define MH_CIGAM 0xcefaedfe |
---|
175 | #endif |
---|
176 | #ifndef FAT_MAGIC |
---|
177 | #define FAT_MAGIC 0xcafebabe |
---|
178 | #endif |
---|
179 | #ifndef FAT_CIGAM |
---|
180 | #define FAT_CIGAM 0xbebafeca |
---|
181 | #endif |
---|
182 | |
---|
183 | static int _isexec(fd) |
---|
184 | int fd; |
---|
185 | { |
---|
186 | struct mach_header ehdr; |
---|
187 | |
---|
188 | if ((read(fd, &ehdr, sizeof(ehdr)) == sizeof(ehdr)) && |
---|
189 | (ehdr.magic == MH_MAGIC || ehdr.magic == MH_CIGAM || |
---|
190 | ehdr.magic == FAT_MAGIC || ehdr.magic == FAT_CIGAM)) |
---|
191 | return(TRUE); |
---|
192 | else |
---|
193 | return(FALSE); |
---|
194 | } |
---|
195 | #endif /* EXE_COFF */ |
---|
196 | |
---|
197 | |
---|
198 | #if EXE_TYPE == EXE_HPEXEC |
---|
199 | /* |
---|
200 | * HP 9000 executable format |
---|
201 | */ |
---|
202 | |
---|
203 | #ifdef hp9000s300 |
---|
204 | |
---|
205 | #include <a.out.h> |
---|
206 | #define header exec |
---|
207 | #define ISEXEC(a) ((a.file_type)==EXEC_MAGIC || (a.file_type)==SHARE_MAGIC || |
---|
208 | (a.file_type)==DEMAND_MAGIC) |
---|
209 | |
---|
210 | #else /* ! hp9000s300 */ |
---|
211 | |
---|
212 | #define ISEXEC(a) ((a)==EXEC_MAGIC || (a)==SHARE_MAGIC || (a)==DEMAND_MAGIC) |
---|
213 | #include <filehdr.h> |
---|
214 | |
---|
215 | #endif /* hp9000s300 */ |
---|
216 | |
---|
217 | static int _isexec(fd) |
---|
218 | int fd; |
---|
219 | { |
---|
220 | struct header ehdr; |
---|
221 | |
---|
222 | if ((read(fd, &ehdr, sizeof(ehdr)) == sizeof(ehdr)) && |
---|
223 | ISEXEC(ehdr.a_magic)) |
---|
224 | return(TRUE); |
---|
225 | else |
---|
226 | return(FALSE); |
---|
227 | } |
---|
228 | #endif /* EXE_HPEXEC */ |
---|
229 | |
---|
230 | |
---|
231 | #if !defined(EXE_TYPE) |
---|
232 | /* |
---|
233 | * Fake _isexec() call for unknown executable formats. |
---|
234 | */ |
---|
235 | static int _isexec(fd) |
---|
236 | /*ARGSUSED*/ |
---|
237 | int fd; |
---|
238 | { |
---|
239 | return(FALSE); |
---|
240 | } |
---|
241 | #endif /* !defined(EXE_TYPE) */ |
---|
242 | |
---|
243 | #ifdef roy |
---|
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 | #else |
---|
268 | extern int isexec(fd) |
---|
269 | int fd; |
---|
270 | { |
---|
271 | return _isexec(fd); |
---|
272 | } |
---|
273 | #endif /* roy */ |
---|