1 |
|
|
/* $OpenBSD: util.c,v 1.13 2015/01/16 06:40:09 deraadt Exp $ |
2 |
|
|
* |
3 |
|
|
* Copyright (c) 1995 Wolfram Schneider <wosch@FreeBSD.org>. Berlin. |
4 |
|
|
* Copyright (c) 1989, 1993 |
5 |
|
|
* The Regents of the University of California. All rights reserved. |
6 |
|
|
* |
7 |
|
|
* This code is derived from software contributed to Berkeley by |
8 |
|
|
* James A. Woods. |
9 |
|
|
* |
10 |
|
|
* Redistribution and use in source and binary forms, with or without |
11 |
|
|
* modification, are permitted provided that the following conditions |
12 |
|
|
* are met: |
13 |
|
|
* 1. Redistributions of source code must retain the above copyright |
14 |
|
|
* notice, this list of conditions and the following disclaimer. |
15 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
16 |
|
|
* notice, this list of conditions and the following disclaimer in the |
17 |
|
|
* documentation and/or other materials provided with the distribution. |
18 |
|
|
* 3. Neither the name of the University nor the names of its contributors |
19 |
|
|
* may be used to endorse or promote products derived from this software |
20 |
|
|
* without specific prior written permission. |
21 |
|
|
* |
22 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
23 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
24 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
26 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
29 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
31 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
32 |
|
|
* SUCH DAMAGE. |
33 |
|
|
* |
34 |
|
|
* $Id: util.c,v 1.13 2015/01/16 06:40:09 deraadt Exp $ |
35 |
|
|
*/ |
36 |
|
|
|
37 |
|
|
|
38 |
|
|
#include <stdlib.h> |
39 |
|
|
#include <string.h> |
40 |
|
|
#include <err.h> |
41 |
|
|
#include <stdio.h> |
42 |
|
|
#include <limits.h> |
43 |
|
|
|
44 |
|
|
#include "locate.h" |
45 |
|
|
|
46 |
|
|
char **colon(char **, char*, char*); |
47 |
|
|
char *patprep(char *); |
48 |
|
|
void print_matches(u_int); |
49 |
|
|
u_char *tolower_word(u_char *); |
50 |
|
|
int getwm(caddr_t); |
51 |
|
|
int getwf(FILE *); |
52 |
|
|
int check_bigram_char(int); |
53 |
|
|
|
54 |
|
|
/* |
55 |
|
|
* Validate bigram chars. If the test failed the database is corrupt |
56 |
|
|
* or the database is obviously not a locate database. |
57 |
|
|
*/ |
58 |
|
|
int |
59 |
|
|
check_bigram_char(ch) |
60 |
|
|
int ch; |
61 |
|
|
{ |
62 |
|
|
/* legal bigram: 0, ASCII_MIN ... ASCII_MAX */ |
63 |
|
|
if (ch == 0 || |
64 |
|
|
(ch >= ASCII_MIN && ch <= ASCII_MAX)) |
65 |
|
|
return(ch); |
66 |
|
|
|
67 |
|
|
(void)fprintf(stderr, "locate database header corrupt, bigram "); |
68 |
|
|
(void)fprintf(stderr, "char outside 0, %d-%d: %d\n", |
69 |
|
|
ASCII_MIN, ASCII_MAX, ch); |
70 |
|
|
exit(1); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
/* split a colon separated string into a char vector |
74 |
|
|
* |
75 |
|
|
* "bla:foo" -> {"bla", "foo"} |
76 |
|
|
* "bla:" -> {"bla", dot} |
77 |
|
|
* "bla" -> {"bla"} |
78 |
|
|
* "" -> do nothing |
79 |
|
|
* |
80 |
|
|
*/ |
81 |
|
|
char ** |
82 |
|
|
colon(dbv, path, dot) |
83 |
|
|
char **dbv; |
84 |
|
|
char *path; |
85 |
|
|
char *dot; /* default for single ':' */ |
86 |
|
|
{ |
87 |
|
|
int vlen, slen; |
88 |
|
|
char *c, *ch, *p; |
89 |
|
|
char **pv; |
90 |
|
|
|
91 |
|
|
if (dbv == NULL) { |
92 |
|
|
if ((dbv = malloc(sizeof(*dbv))) == NULL) |
93 |
|
|
err(1, "malloc"); |
94 |
|
|
*dbv = NULL; |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
/* empty string */ |
98 |
|
|
if (*path == '\0') { |
99 |
|
|
(void)fprintf(stderr, "empty database name, ignored\n"); |
100 |
|
|
return(dbv); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
/* length of string vector */ |
104 |
|
|
for (vlen = 0, pv = dbv; *pv != NULL; pv++, vlen++) |
105 |
|
|
; |
106 |
|
|
|
107 |
|
|
for (ch = c = path; ; ch++) { |
108 |
|
|
if (*ch == ':' || |
109 |
|
|
(!*ch && !(*(ch - 1) == ':' && ch == 1+ path))) { |
110 |
|
|
char **newdbv; |
111 |
|
|
|
112 |
|
|
/* single colon -> dot */ |
113 |
|
|
if (ch == c) |
114 |
|
|
p = dot; |
115 |
|
|
else { |
116 |
|
|
/* a string */ |
117 |
|
|
slen = ch - c; |
118 |
|
|
if ((p = calloc(sizeof(char), slen + 1)) |
119 |
|
|
== NULL) |
120 |
|
|
err(1, "malloc"); |
121 |
|
|
bcopy(c, p, slen); |
122 |
|
|
*(p + slen) = '\0'; |
123 |
|
|
} |
124 |
|
|
/* increase dbv with element p */ |
125 |
|
|
if ((newdbv = reallocarray(dbv, vlen + 2, |
126 |
|
|
sizeof(*newdbv))) == NULL) |
127 |
|
|
err(1, "realloc"); |
128 |
|
|
dbv = newdbv; |
129 |
|
|
*(dbv + vlen) = p; |
130 |
|
|
*(dbv + ++vlen) = NULL; |
131 |
|
|
c = ch + 1; |
132 |
|
|
} |
133 |
|
|
if (*ch == '\0') |
134 |
|
|
break; |
135 |
|
|
} |
136 |
|
|
return (dbv); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
void |
140 |
|
|
print_matches(counter) |
141 |
|
|
u_int counter; |
142 |
|
|
{ |
143 |
|
|
(void)printf("%d\n", counter); |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
|
147 |
|
|
/* |
148 |
|
|
* extract last glob-free subpattern in name for fast pre-match; prepend |
149 |
|
|
* '\0' for backwards match; return end of new pattern |
150 |
|
|
*/ |
151 |
|
|
static char globfree[100]; |
152 |
|
|
|
153 |
|
|
char * |
154 |
|
|
patprep(name) |
155 |
|
|
char *name; |
156 |
|
|
{ |
157 |
|
|
char *endmark, *p, *subp; |
158 |
|
|
|
159 |
|
|
subp = globfree; |
160 |
|
|
*subp++ = '\0'; /* set first element to '\0' */ |
161 |
|
|
p = name + strlen(name) - 1; |
162 |
|
|
|
163 |
|
|
/* skip trailing metacharacters */ |
164 |
|
|
for (; p >= name; p--) |
165 |
|
|
if (strchr(LOCATE_REG, *p) == NULL) |
166 |
|
|
break; |
167 |
|
|
|
168 |
|
|
/* |
169 |
|
|
* check if maybe we are in a character class |
170 |
|
|
* |
171 |
|
|
* 'foo.[ch]' |
172 |
|
|
* |----< p |
173 |
|
|
*/ |
174 |
|
|
if (p >= name && |
175 |
|
|
(strchr(p, '[') != NULL || strchr(p, ']') != NULL)) { |
176 |
|
|
for (p = name; *p != '\0'; p++) |
177 |
|
|
if (*p == ']' || *p == '[') |
178 |
|
|
break; |
179 |
|
|
p--; |
180 |
|
|
|
181 |
|
|
/* |
182 |
|
|
* cannot find a non-meta character, give up |
183 |
|
|
* '*\*[a-z]' |
184 |
|
|
* |-------< p |
185 |
|
|
*/ |
186 |
|
|
if (p >= name && strchr(LOCATE_REG, *p) != NULL) |
187 |
|
|
p = name - 1; |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
if (p < name) |
191 |
|
|
/* only meta chars: "???", force '/' search */ |
192 |
|
|
*subp++ = '/'; |
193 |
|
|
|
194 |
|
|
else { |
195 |
|
|
for (endmark = p; p >= name; p--) |
196 |
|
|
if (strchr(LOCATE_REG, *p) != NULL) |
197 |
|
|
break; |
198 |
|
|
for (++p; |
199 |
|
|
(p <= endmark) && subp < (globfree + sizeof(globfree));) |
200 |
|
|
*subp++ = *p++; |
201 |
|
|
} |
202 |
|
|
*subp = '\0'; |
203 |
|
|
return(--subp); |
204 |
|
|
} |
205 |
|
|
|
206 |
|
|
/* tolower word */ |
207 |
|
|
u_char * |
208 |
|
|
tolower_word(word) |
209 |
|
|
u_char *word; |
210 |
|
|
{ |
211 |
|
|
u_char *p; |
212 |
|
|
|
213 |
|
|
for (p = word; *p != '\0'; p++) |
214 |
|
|
*p = TOLOWER(*p); |
215 |
|
|
|
216 |
|
|
return(word); |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
|
220 |
|
|
/* |
221 |
|
|
* Read integer from mmap pointer. |
222 |
|
|
* Essential a simple ``return *(int *)p'' but avoid sigbus |
223 |
|
|
* for integer alignment (SunOS 4.x, 5.x). |
224 |
|
|
* |
225 |
|
|
* Convert network byte order to host byte order if necessary. |
226 |
|
|
* So we can read on FreeBSD/i386 (little endian) a locate database |
227 |
|
|
* which was built on SunOS/sparc (big endian). |
228 |
|
|
*/ |
229 |
|
|
|
230 |
|
|
int |
231 |
|
|
getwm(p) |
232 |
|
|
caddr_t p; |
233 |
|
|
{ |
234 |
|
|
union { |
235 |
|
|
char buf[INTSIZE]; |
236 |
|
|
int i; |
237 |
|
|
} u; |
238 |
|
|
int i; |
239 |
|
|
|
240 |
|
|
for (i = 0; i < INTSIZE; i++) |
241 |
|
|
u.buf[i] = *p++; |
242 |
|
|
|
243 |
|
|
i = u.i; |
244 |
|
|
|
245 |
|
|
if (i > PATH_MAX || i < -(PATH_MAX)) { |
246 |
|
|
i = ntohl(i); |
247 |
|
|
if (i > PATH_MAX || i < -(PATH_MAX)) { |
248 |
|
|
(void)fprintf(stderr, |
249 |
|
|
"integer out of +-PATH_MAX (%d): %d\n", |
250 |
|
|
PATH_MAX, i); |
251 |
|
|
exit(1); |
252 |
|
|
} |
253 |
|
|
} |
254 |
|
|
return(i); |
255 |
|
|
} |
256 |
|
|
|
257 |
|
|
/* |
258 |
|
|
* Read integer from stream. |
259 |
|
|
* |
260 |
|
|
* Convert network byte order to host byte order if necessary. |
261 |
|
|
* So we can read on FreeBSD/i386 (little endian) a locate database |
262 |
|
|
* which was built on SunOS/sparc (big endian). |
263 |
|
|
*/ |
264 |
|
|
|
265 |
|
|
int |
266 |
|
|
getwf(fp) |
267 |
|
|
FILE *fp; |
268 |
|
|
{ |
269 |
|
|
int word; |
270 |
|
|
|
271 |
|
|
word = getw(fp); |
272 |
|
|
|
273 |
|
|
if (word > PATH_MAX || word < -(PATH_MAX)) { |
274 |
|
|
word = ntohl(word); |
275 |
|
|
if (word > PATH_MAX || word < -(PATH_MAX)) { |
276 |
|
|
(void)fprintf(stderr, |
277 |
|
|
"integer out of +-PATH_MAX (%d): %d\n", |
278 |
|
|
PATH_MAX, word); |
279 |
|
|
exit(1); |
280 |
|
|
} |
281 |
|
|
} |
282 |
|
|
return(word); |
283 |
|
|
} |