1 /* Included and modified strings.c from the OpenBSD project */
2
3 /*
4 * Copyright (c) 1980, 1987, 1993
5 * The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #ifndef WIN32
33 #include <sys/types.h>
34
35 #include <ctype.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <locale.h>
42 #include <unistd.h>
43 #include <netinet/in.h>
44
45 #ifdef SOLARIS
46 #include <sys/exechdr.h>
47 #elif defined Darwin || defined HPUX
48
49 /* For some reason darwin does not have that */
50 struct exec {
51 unsigned long a_info; /* Use macros N_MAGIC, etc for access */
52 unsigned char a_machtype; /* machine type */
53 unsigned short a_magic; /* magic number */
54 unsigned a_text; /* length of text, in bytes */
55 unsigned a_data; /* length of data, in bytes */
56 unsigned a_bss; /* length of uninitialized data area for file, in bytes */
57 unsigned a_syms; /* length of symbol table data in file, in bytes */
58 unsigned a_entry; /* start address */
59 unsigned a_trsize; /* length of relocation info for text, in bytes */
60 unsigned a_drsize; /* length of relocation info for data, in bytes */
61 };
62
63 #define OMAGIC 0407 /* Object file or impure executable */
64 #define NMAGIC 0410 /* Code indicating pure executable */
65 #define ZMAGIC 0413 /* Code indicating demand-paged executable */
66 #define BMAGIC 0415 /* Used by a b.out object */
67 #define M_OLDSUN2 0
68
69 #else
70 #include <a.out.h>
71 #endif
72
73 #ifndef PAGSIZ
74 #define PAGSIZ 0x02000
75 #endif
76
77 #ifndef OLD_PAGSIZ
78 #define OLD_PAGSIZ 0x00800
79 #endif
80
81 #ifndef N_BADMAG
82
83 #ifdef AIX
84 #define N_BADMAG(x) \
85 (((x).magic)!=U802TOCMAGIC && ((x).magic)!=U803TOCMAGIC && ((x).magic)!=U803XTOCMAGIC && ((x).magic)!=U64_TOCMAGIC)
86 #else /* non AIX */
87 #define N_BADMAG(x) \
88 (((x).a_magic)!=OMAGIC && ((x).a_magic)!=NMAGIC && ((x).a_magic)!=ZMAGIC)
89 #endif
90
91 #endif /* N_BADMAG */
92
93 #ifndef N_PAGSIZ
94 #define N_PAGSIZ(x) \
95 ((x).a_machtype == M_OLDSUN2? OLD_PAGSIZ : PAGSIZ)
96 #endif
97
98 #ifndef N_TXTOFF
99
100 #ifdef AIX
101 #define N_TXTOFF(x) \
102 /* text segment */ \
103 ((x).magic==U64_TOCMAGIC ? 0 : sizeof (struct aouthdr))
104 #else /* non AIX */
105 #define N_TXTOFF(x) \
106 /* text segment */ \
107 ((x).a_machtype == M_OLDSUN2 \
108 ? ((x).a_magic==ZMAGIC ? N_PAGSIZ(x) : sizeof (struct exec)) \
109 : ((x).a_magic==ZMAGIC ? 0 : sizeof (struct exec)) )
110 #endif
111
112 #endif /* N_TXTOFF */
113
114 #include "headers/defs.h"
115 #include "headers/debug_op.h"
116 #include "headers/regex_op.h"
117
118 #include "error_messages/error_messages.h"
119
120 #define STR_MINLEN 4 /* Minimum length for a string */
121
122 #define ISSTR(ch) (isascii(ch) && (isprint(ch) || ch == '\t'))
123
124 #ifdef AIX
125 typedef struct aouthdr EXEC;
126 #else
127 typedef struct exec EXEC;
128 #endif
129
130 typedef struct _os_strings {
131 int head_len;
132 int read_len;
133 int hcnt;
134 long foff;
135 unsigned char hbfr[sizeof(EXEC)];
136 FILE *fp;
137 } os_strings;
138
139 /* Read each character from a binary file */
140 int os_getch(os_strings *oss);
141
142
143 /* List the strings of a binary and check if the regex given is there */
os_string(char * file,char * regex)144 int os_string(char *file, char *regex)
145 {
146 int ch, cnt;
147 unsigned char *C;
148 unsigned char *bfr;
149 char line[OS_SIZE_1024 + 1];
150 char *buf;
151 EXEC *head;
152 os_strings oss;
153
154 /* Return didn't match */
155 if (!file || !regex) {
156 return (0);
157 }
158
159 /* Allocate the buffer */
160 bfr = (unsigned char *) calloc(STR_MINLEN + 2, sizeof(unsigned char));
161 if (!bfr) {
162 merror(MEM_ERROR, ARGV0, errno, strerror(errno));
163 return (0);
164 }
165
166 /* Open the file */
167 oss.fp = fopen(file, "r");
168 if (!oss.fp) {
169 free(bfr);
170 return (0);
171 }
172
173 /* Clean the line */
174 memset(line, '\0', OS_SIZE_1024 + 1);
175
176 /* Start (from old strings.c) */
177 oss.foff = 0;
178 oss.head_len = 0;
179 oss.read_len = -1;
180 head = (EXEC *)oss.hbfr;
181
182 if ((oss.head_len = read(fileno(oss.fp), head, sizeof(EXEC))) == -1) {
183 oss.head_len = 0;
184 oss.read_len = -1;
185 } else if (oss.head_len == sizeof(EXEC) && !N_BADMAG(*head)) {
186 oss.foff = N_TXTOFF(*head);
187 if (fseek(stdin, oss.foff, SEEK_SET) == -1) {
188 oss.read_len = -1;
189 } else {
190 #ifdef AIX
191 oss.read_len = head->tsize + head->dsize;
192 #else
193 oss.read_len = head->a_text + head->a_data;
194 #endif
195 }
196
197 oss.head_len = 0;
198 } else {
199 oss.hcnt = 0;
200 }
201
202 /* Read the file and perform the regex comparison */
203 for (cnt = 0, C = bfr; (ch = os_getch(&oss)) != EOF;) {
204 if (ISSTR(ch)) {
205 if (!cnt) {
206 C = bfr;
207 }
208 *C++ = ch;
209 if (++cnt < STR_MINLEN) {
210 continue;
211 }
212
213 strncpy(line, (char *)bfr, STR_MINLEN + 1);
214 buf = line;
215 buf += strlen(line);
216
217 while ((ch = os_getch(&oss)) != EOF && ISSTR(ch)) {
218 if (cnt < OS_SIZE_1024) {
219 *buf = (char)ch;
220 buf++;
221 } else {
222 *buf = '\0';
223 break;
224 }
225 cnt++;
226 }
227
228 *buf = '\0';
229
230 if (OS_PRegex(line, regex)) {
231 if (oss.fp) {
232 fclose(oss.fp);
233 }
234 free(bfr);
235 return (1);
236 }
237 }
238
239 cnt = 0;
240 }
241
242 if (oss.fp) {
243 fclose(oss.fp);
244 }
245 free(bfr);
246 return (0);
247 }
248
249 /* Get next character from wherever */
os_getch(os_strings * oss)250 int os_getch(os_strings *oss)
251 {
252 ++oss->foff;
253 if (oss->head_len) {
254 if (oss->hcnt < oss->head_len) {
255 return ((int)oss->hbfr[oss->hcnt++]);
256 }
257 oss->head_len = 0;
258 }
259 if (oss->read_len == -1 || oss->read_len-- > 0) {
260 return (fgetc(oss->fp));
261 }
262 return (EOF);
263 }
264
265 #else
os_string(char * file,char * regex)266 int os_string(__attribute__((unused)) char *file, __attribute__((unused)) char *regex)
267 {
268 return (0);
269 }
270 #endif
271
272