getcap.c revision 1.55 1 1.55 he /* $NetBSD: getcap.c,v 1.55 2014/09/24 07:53:06 he Exp $ */
2 1.9 cgd
3 1.1 cgd /*-
4 1.9 cgd * Copyright (c) 1992, 1993
5 1.9 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Casey Leedom of Lawrence Livermore National Laboratory.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.38 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.39 lukem #if HAVE_NBTOOL_CONFIG_H
36 1.39 lukem #include "nbtool_config.h"
37 1.37 uwe #endif
38 1.37 uwe
39 1.13 christos #include <sys/cdefs.h>
40 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
41 1.9 cgd #if 0
42 1.9 cgd static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94";
43 1.9 cgd #else
44 1.55 he __RCSID("$NetBSD: getcap.c,v 1.55 2014/09/24 07:53:06 he Exp $");
45 1.9 cgd #endif
46 1.1 cgd #endif /* LIBC_SCCS and not lint */
47 1.1 cgd
48 1.41 christos #ifndef SMALL
49 1.14 jtc #include "namespace.h"
50 1.41 christos #endif
51 1.1 cgd #include <sys/types.h>
52 1.36 tron #include <sys/param.h>
53 1.30 lukem
54 1.30 lukem #include <assert.h>
55 1.51 christos #include <stddef.h>
56 1.1 cgd #include <ctype.h>
57 1.41 christos #ifndef SMALL
58 1.1 cgd #include <db.h>
59 1.41 christos #endif
60 1.1 cgd #include <errno.h>
61 1.1 cgd #include <fcntl.h>
62 1.1 cgd #include <limits.h>
63 1.1 cgd #include <stdio.h>
64 1.1 cgd #include <stdlib.h>
65 1.1 cgd #include <string.h>
66 1.1 cgd #include <unistd.h>
67 1.14 jtc
68 1.55 he #ifndef O_CLOEXEC /* For tools, as in NetBSD 5.2 or earlier... */
69 1.55 he #define O_CLOEXEC 0
70 1.55 he #endif
71 1.55 he
72 1.49 joerg #if defined(__weak_alias) && !defined(SMALL)
73 1.32 mycroft __weak_alias(cgetcap,_cgetcap)
74 1.32 mycroft __weak_alias(cgetclose,_cgetclose)
75 1.32 mycroft __weak_alias(cgetent,_cgetent)
76 1.32 mycroft __weak_alias(cgetfirst,_cgetfirst)
77 1.32 mycroft __weak_alias(cgetmatch,_cgetmatch)
78 1.32 mycroft __weak_alias(cgetnext,_cgetnext)
79 1.32 mycroft __weak_alias(cgetnum,_cgetnum)
80 1.32 mycroft __weak_alias(cgetset,_cgetset)
81 1.32 mycroft __weak_alias(cgetstr,_cgetstr)
82 1.32 mycroft __weak_alias(cgetustr,_cgetustr)
83 1.48 christos __weak_alias(csetexpandtc,_csetexpandtc)
84 1.14 jtc #endif
85 1.1 cgd
86 1.1 cgd #define BFRAG 1024
87 1.1 cgd #define BSIZE 1024
88 1.1 cgd #define ESC ('[' & 037) /* ASCII ESC */
89 1.1 cgd #define MAX_RECURSION 32 /* maximum getent recursion */
90 1.1 cgd #define SFRAG 100 /* cgetstr mallocs in SFRAG chunks */
91 1.1 cgd
92 1.1 cgd #define RECOK (char)0
93 1.1 cgd #define TCERR (char)1
94 1.1 cgd #define SHADOW (char)2
95 1.1 cgd
96 1.1 cgd static size_t topreclen; /* toprec length */
97 1.1 cgd static char *toprec; /* Additional record specified by cgetset() */
98 1.1 cgd static int gottoprec; /* Flag indicating retrieval of toprecord */
99 1.48 christos static int expandtc = 1; /* flag to expand tc= or not */
100 1.1 cgd
101 1.41 christos #ifndef SMALL
102 1.40 christos static int cdbget(DB *, char **, const char *);
103 1.41 christos #endif
104 1.40 christos static int getent(char **, size_t *, const char * const *, int,
105 1.40 christos const char *, int, char *);
106 1.40 christos static int nfcmp(char *, char *);
107 1.1 cgd
108 1.1 cgd /*
109 1.1 cgd * Cgetset() allows the addition of a user specified buffer to be added
110 1.1 cgd * to the database array, in effect "pushing" the buffer on top of the
111 1.1 cgd * virtual database. 0 is returned on success, -1 on failure.
112 1.1 cgd */
113 1.1 cgd int
114 1.40 christos cgetset(const char *ent)
115 1.1 cgd {
116 1.27 abs const char *source, *check;
117 1.27 abs char *dest;
118 1.27 abs
119 1.1 cgd if (ent == NULL) {
120 1.47 jnemeth if (toprec != NULL)
121 1.1 cgd free(toprec);
122 1.1 cgd toprec = NULL;
123 1.1 cgd topreclen = 0;
124 1.47 jnemeth return 0;
125 1.1 cgd }
126 1.1 cgd topreclen = strlen(ent);
127 1.47 jnemeth if ((toprec = malloc(topreclen + 1)) == NULL) {
128 1.1 cgd errno = ENOMEM;
129 1.47 jnemeth return -1;
130 1.1 cgd }
131 1.1 cgd gottoprec = 0;
132 1.27 abs
133 1.47 jnemeth source = ent;
134 1.47 jnemeth dest = toprec;
135 1.47 jnemeth while (*source != '\0') { /* Strip whitespace */
136 1.27 abs *dest++ = *source++; /* Do not check first field */
137 1.27 abs while (*source == ':') {
138 1.47 jnemeth check = source + 1;
139 1.29 abs while (*check && (isspace((unsigned char)*check) ||
140 1.29 abs (*check=='\\' && isspace((unsigned char)check[1]))))
141 1.27 abs ++check;
142 1.47 jnemeth if (*check == ':')
143 1.47 jnemeth source = check;
144 1.27 abs else
145 1.27 abs break;
146 1.27 abs
147 1.27 abs }
148 1.27 abs }
149 1.47 jnemeth *dest = 0;
150 1.27 abs
151 1.47 jnemeth return 0;
152 1.1 cgd }
153 1.1 cgd
154 1.1 cgd /*
155 1.1 cgd * Cgetcap searches the capability record buf for the capability cap with
156 1.1 cgd * type `type'. A pointer to the value of cap is returned on success, NULL
157 1.1 cgd * if the requested capability couldn't be found.
158 1.1 cgd *
159 1.1 cgd * Specifying a type of ':' means that nothing should follow cap (:cap:).
160 1.1 cgd * In this case a pointer to the terminating ':' or NUL will be returned if
161 1.1 cgd * cap is found.
162 1.1 cgd *
163 1.1 cgd * If (cap, '@') or (cap, terminator, '@') is found before (cap, terminator)
164 1.1 cgd * return NULL.
165 1.1 cgd */
166 1.1 cgd char *
167 1.52 joerg cgetcap(char *buf, const char *cap, int type)
168 1.1 cgd {
169 1.21 mycroft char *bp;
170 1.21 mycroft const char *cp;
171 1.1 cgd
172 1.30 lukem _DIAGASSERT(buf != NULL);
173 1.30 lukem _DIAGASSERT(cap != NULL);
174 1.30 lukem
175 1.1 cgd bp = buf;
176 1.1 cgd for (;;) {
177 1.1 cgd /*
178 1.1 cgd * Skip past the current capability field - it's either the
179 1.1 cgd * name field if this is the first time through the loop, or
180 1.1 cgd * the remainder of a field whose name failed to match cap.
181 1.1 cgd */
182 1.1 cgd for (;;)
183 1.1 cgd if (*bp == '\0')
184 1.47 jnemeth return NULL;
185 1.47 jnemeth else if (*bp++ == ':')
186 1.47 jnemeth break;
187 1.1 cgd
188 1.1 cgd /*
189 1.1 cgd * Try to match (cap, type) in buf.
190 1.1 cgd */
191 1.1 cgd for (cp = cap; *cp == *bp && *bp != '\0'; cp++, bp++)
192 1.1 cgd continue;
193 1.1 cgd if (*cp != '\0')
194 1.1 cgd continue;
195 1.1 cgd if (*bp == '@')
196 1.47 jnemeth return NULL;
197 1.1 cgd if (type == ':') {
198 1.1 cgd if (*bp != '\0' && *bp != ':')
199 1.1 cgd continue;
200 1.47 jnemeth return bp;
201 1.1 cgd }
202 1.1 cgd if (*bp != type)
203 1.1 cgd continue;
204 1.1 cgd bp++;
205 1.47 jnemeth return *bp == '@' ? NULL : bp;
206 1.1 cgd }
207 1.1 cgd /* NOTREACHED */
208 1.1 cgd }
209 1.1 cgd
210 1.1 cgd /*
211 1.1 cgd * Cgetent extracts the capability record name from the NULL terminated file
212 1.1 cgd * array db_array and returns a pointer to a malloc'd copy of it in buf.
213 1.1 cgd * Buf must be retained through all subsequent calls to cgetcap, cgetnum,
214 1.1 cgd * cgetflag, and cgetstr, but may then be free'd. 0 is returned on success,
215 1.1 cgd * -1 if the requested record couldn't be found, -2 if a system error was
216 1.1 cgd * encountered (couldn't open/read a file, etc.), and -3 if a potential
217 1.1 cgd * reference loop is detected.
218 1.1 cgd */
219 1.45 christos /* coverity[+alloc : arg-*0] */
220 1.1 cgd int
221 1.40 christos cgetent(char **buf, const char * const *db_array, const char *name)
222 1.1 cgd {
223 1.18 thorpej size_t dummy;
224 1.1 cgd
225 1.30 lukem _DIAGASSERT(buf != NULL);
226 1.30 lukem _DIAGASSERT(db_array != NULL);
227 1.30 lukem _DIAGASSERT(name != NULL);
228 1.30 lukem
229 1.47 jnemeth return getent(buf, &dummy, db_array, -1, name, 0, NULL);
230 1.1 cgd }
231 1.1 cgd
232 1.48 christos void
233 1.48 christos csetexpandtc(int etc)
234 1.48 christos {
235 1.48 christos expandtc = etc;
236 1.48 christos }
237 1.48 christos
238 1.1 cgd /*
239 1.1 cgd * Getent implements the functions of cgetent. If fd is non-negative,
240 1.1 cgd * *db_array has already been opened and fd is the open file descriptor. We
241 1.1 cgd * do this to save time and avoid using up file descriptors for tc=
242 1.1 cgd * recursions.
243 1.1 cgd *
244 1.1 cgd * Getent returns the same success/failure codes as cgetent. On success, a
245 1.1 cgd * pointer to a malloc'ed capability record with all tc= capabilities fully
246 1.1 cgd * expanded and its length (not including trailing ASCII NUL) are left in
247 1.1 cgd * *cap and *len.
248 1.1 cgd *
249 1.1 cgd * Basic algorithm:
250 1.1 cgd * + Allocate memory incrementally as needed in chunks of size BFRAG
251 1.1 cgd * for capability buffer.
252 1.1 cgd * + Recurse for each tc=name and interpolate result. Stop when all
253 1.1 cgd * names interpolated, a name can't be found, or depth exceeds
254 1.1 cgd * MAX_RECURSION.
255 1.1 cgd */
256 1.45 christos /* coverity[+alloc : arg-*0] */
257 1.1 cgd static int
258 1.40 christos getent(char **cap, size_t *len, const char * const *db_array, int fd,
259 1.40 christos const char *name, int depth, char *nfield)
260 1.1 cgd {
261 1.41 christos char *record, *newrecord;
262 1.47 jnemeth char *r_end, *rp; /* pacify gcc */
263 1.40 christos const char * const *db_p;
264 1.47 jnemeth int myfd, eof, foundit;
265 1.1 cgd int tc_not_resolved;
266 1.1 cgd
267 1.30 lukem _DIAGASSERT(cap != NULL);
268 1.30 lukem _DIAGASSERT(len != NULL);
269 1.30 lukem _DIAGASSERT(db_array != NULL);
270 1.30 lukem /* fd may be -1 */
271 1.30 lukem _DIAGASSERT(name != NULL);
272 1.30 lukem /* nfield may be NULL */
273 1.30 lukem
274 1.47 jnemeth myfd = 0;
275 1.47 jnemeth rp = NULL;
276 1.47 jnemeth
277 1.1 cgd /*
278 1.1 cgd * Return with ``loop detected'' error if we've recursed more than
279 1.1 cgd * MAX_RECURSION times.
280 1.1 cgd */
281 1.1 cgd if (depth > MAX_RECURSION)
282 1.47 jnemeth return -3;
283 1.1 cgd
284 1.1 cgd /*
285 1.1 cgd * Check if we have a top record from cgetset().
286 1.1 cgd */
287 1.1 cgd if (depth == 0 && toprec != NULL && cgetmatch(toprec, name) == 0) {
288 1.47 jnemeth if ((record = malloc(topreclen + BFRAG)) == NULL) {
289 1.1 cgd errno = ENOMEM;
290 1.47 jnemeth return -2;
291 1.1 cgd }
292 1.11 mrg (void)strcpy(record, toprec); /* XXX: strcpy is safe */
293 1.1 cgd db_p = db_array;
294 1.1 cgd rp = record + topreclen + 1;
295 1.1 cgd r_end = rp + BFRAG;
296 1.1 cgd goto tc_exp;
297 1.1 cgd }
298 1.1 cgd /*
299 1.1 cgd * Allocate first chunk of memory.
300 1.1 cgd */
301 1.1 cgd if ((record = malloc(BFRAG)) == NULL) {
302 1.1 cgd errno = ENOMEM;
303 1.47 jnemeth return -2;
304 1.1 cgd }
305 1.1 cgd r_end = record + BFRAG;
306 1.1 cgd foundit = 0;
307 1.1 cgd /*
308 1.1 cgd * Loop through database array until finding the record.
309 1.1 cgd */
310 1.1 cgd
311 1.1 cgd for (db_p = db_array; *db_p != NULL; db_p++) {
312 1.1 cgd eof = 0;
313 1.1 cgd
314 1.1 cgd /*
315 1.1 cgd * Open database if not already open.
316 1.1 cgd */
317 1.1 cgd
318 1.1 cgd if (fd >= 0) {
319 1.15 kleink (void)lseek(fd, (off_t)0, SEEK_SET);
320 1.1 cgd } else {
321 1.41 christos #ifndef SMALL
322 1.53 christos DB *capdbp;
323 1.53 christos char pbuf[MAXPATHLEN];
324 1.53 christos char *cbuf;
325 1.53 christos int retval;
326 1.53 christos size_t clen;
327 1.53 christos
328 1.1 cgd (void)snprintf(pbuf, sizeof(pbuf), "%s.db", *db_p);
329 1.54 christos if ((capdbp = dbopen(pbuf, O_RDONLY | O_CLOEXEC, 0,
330 1.54 christos DB_HASH, 0)) != NULL) {
331 1.1 cgd free(record);
332 1.1 cgd retval = cdbget(capdbp, &record, name);
333 1.8 cgd if (retval < 0) {
334 1.8 cgd /* no record available */
335 1.8 cgd (void)capdbp->close(capdbp);
336 1.47 jnemeth return retval;
337 1.8 cgd }
338 1.8 cgd /* save the data; close frees it */
339 1.7 cgd clen = strlen(record);
340 1.46 jnemeth if ((cbuf = malloc(clen + 1)) == NULL) {
341 1.46 jnemeth (void)capdbp->close(capdbp);
342 1.46 jnemeth errno = ENOMEM;
343 1.47 jnemeth return -2;
344 1.46 jnemeth }
345 1.23 perry memmove(cbuf, record, clen + 1);
346 1.7 cgd if (capdbp->close(capdbp) < 0) {
347 1.30 lukem int serrno = errno;
348 1.30 lukem
349 1.7 cgd free(cbuf);
350 1.30 lukem errno = serrno;
351 1.47 jnemeth return -2;
352 1.7 cgd }
353 1.7 cgd *len = clen;
354 1.7 cgd *cap = cbuf;
355 1.47 jnemeth return retval;
356 1.41 christos } else
357 1.41 christos #endif
358 1.41 christos {
359 1.54 christos fd = open(*db_p, O_RDONLY | O_CLOEXEC, 0);
360 1.1 cgd if (fd < 0) {
361 1.1 cgd /* No error on unfound file. */
362 1.10 mycroft continue;
363 1.1 cgd }
364 1.1 cgd myfd = 1;
365 1.1 cgd }
366 1.1 cgd }
367 1.1 cgd /*
368 1.1 cgd * Find the requested capability record ...
369 1.1 cgd */
370 1.1 cgd {
371 1.1 cgd char buf[BUFSIZ];
372 1.20 mycroft char *b_end, *bp, *cp;
373 1.20 mycroft int c, slash;
374 1.1 cgd
375 1.1 cgd /*
376 1.1 cgd * Loop invariants:
377 1.1 cgd * There is always room for one more character in record.
378 1.1 cgd * R_end always points just past end of record.
379 1.1 cgd * Rp always points just past last character in record.
380 1.1 cgd * B_end always points just past last character in buf.
381 1.1 cgd * Bp always points at next character in buf.
382 1.20 mycroft * Cp remembers where the last colon was.
383 1.1 cgd */
384 1.1 cgd b_end = buf;
385 1.1 cgd bp = buf;
386 1.47 jnemeth cp = NULL;
387 1.20 mycroft slash = 0;
388 1.1 cgd for (;;) {
389 1.1 cgd /*
390 1.1 cgd * Read in a line implementing (\, newline)
391 1.1 cgd * line continuation.
392 1.1 cgd */
393 1.1 cgd rp = record;
394 1.1 cgd for (;;) {
395 1.1 cgd if (bp >= b_end) {
396 1.51 christos ssize_t n;
397 1.1 cgd
398 1.1 cgd n = read(fd, buf, sizeof(buf));
399 1.1 cgd if (n <= 0) {
400 1.1 cgd if (myfd)
401 1.1 cgd (void)close(fd);
402 1.1 cgd if (n < 0) {
403 1.30 lukem int serrno = errno;
404 1.30 lukem
405 1.1 cgd free(record);
406 1.30 lukem errno = serrno;
407 1.47 jnemeth return -2;
408 1.1 cgd } else {
409 1.1 cgd fd = -1;
410 1.1 cgd eof = 1;
411 1.1 cgd break;
412 1.1 cgd }
413 1.1 cgd }
414 1.1 cgd b_end = buf+n;
415 1.1 cgd bp = buf;
416 1.1 cgd }
417 1.1 cgd
418 1.1 cgd c = *bp++;
419 1.1 cgd if (c == '\n') {
420 1.20 mycroft if (slash) {
421 1.20 mycroft slash = 0;
422 1.1 cgd rp--;
423 1.1 cgd continue;
424 1.1 cgd } else
425 1.1 cgd break;
426 1.1 cgd }
427 1.20 mycroft if (slash) {
428 1.20 mycroft slash = 0;
429 1.20 mycroft cp = 0;
430 1.20 mycroft }
431 1.20 mycroft if (c == ':') {
432 1.20 mycroft /*
433 1.20 mycroft * If the field was `empty' (i.e.
434 1.20 mycroft * contained only white space), back up
435 1.20 mycroft * to the colon (eliminating the
436 1.20 mycroft * field).
437 1.20 mycroft */
438 1.47 jnemeth if (cp != NULL)
439 1.20 mycroft rp = cp;
440 1.20 mycroft else
441 1.20 mycroft cp = rp;
442 1.20 mycroft } else if (c == '\\') {
443 1.20 mycroft slash = 1;
444 1.20 mycroft } else if (c != ' ' && c != '\t') {
445 1.20 mycroft /*
446 1.20 mycroft * Forget where the colon was, as this
447 1.20 mycroft * is not an empty field.
448 1.20 mycroft */
449 1.20 mycroft cp = 0;
450 1.20 mycroft }
451 1.1 cgd *rp++ = c;
452 1.1 cgd
453 1.1 cgd /*
454 1.1 cgd * Enforce loop invariant: if no room
455 1.1 cgd * left in record buffer, try to get
456 1.1 cgd * some more.
457 1.1 cgd */
458 1.1 cgd if (rp >= r_end) {
459 1.51 christos ptrdiff_t pos;
460 1.1 cgd size_t newsize;
461 1.1 cgd
462 1.1 cgd pos = rp - record;
463 1.1 cgd newsize = r_end - record + BFRAG;
464 1.33 itojun newrecord = realloc(record, newsize);
465 1.33 itojun if (newrecord == NULL) {
466 1.33 itojun free(record);
467 1.1 cgd if (myfd)
468 1.1 cgd (void)close(fd);
469 1.30 lukem errno = ENOMEM;
470 1.47 jnemeth return -2;
471 1.1 cgd }
472 1.33 itojun record = newrecord;
473 1.1 cgd r_end = record + newsize;
474 1.1 cgd rp = record + pos;
475 1.1 cgd }
476 1.1 cgd }
477 1.20 mycroft /* Eliminate any white space after the last colon. */
478 1.20 mycroft if (cp)
479 1.20 mycroft rp = cp + 1;
480 1.20 mycroft /* Loop invariant lets us do this. */
481 1.1 cgd *rp++ = '\0';
482 1.1 cgd
483 1.1 cgd /*
484 1.1 cgd * If encountered eof check next file.
485 1.1 cgd */
486 1.1 cgd if (eof)
487 1.1 cgd break;
488 1.1 cgd
489 1.1 cgd /*
490 1.1 cgd * Toss blank lines and comments.
491 1.1 cgd */
492 1.1 cgd if (*record == '\0' || *record == '#')
493 1.1 cgd continue;
494 1.1 cgd
495 1.1 cgd /*
496 1.1 cgd * See if this is the record we want ...
497 1.1 cgd */
498 1.47 jnemeth if (cgetmatch(record, name) == 0)
499 1.1 cgd if (nfield == NULL || !nfcmp(nfield, record)) {
500 1.1 cgd foundit = 1;
501 1.1 cgd break; /* found it! */
502 1.1 cgd }
503 1.1 cgd }
504 1.47 jnemeth }
505 1.1 cgd if (foundit)
506 1.1 cgd break;
507 1.1 cgd }
508 1.1 cgd
509 1.1 cgd if (!foundit)
510 1.47 jnemeth return -1;
511 1.1 cgd
512 1.1 cgd /*
513 1.1 cgd * Got the capability record, but now we have to expand all tc=name
514 1.1 cgd * references in it ...
515 1.1 cgd */
516 1.48 christos tc_exp:
517 1.48 christos tc_not_resolved = 0;
518 1.48 christos if (expandtc) {
519 1.16 perry char *newicap, *s;
520 1.17 perry size_t ilen, newilen;
521 1.51 christos int iret;
522 1.51 christos ptrdiff_t diff, tclen;
523 1.1 cgd char *icap, *scan, *tc, *tcstart, *tcend;
524 1.1 cgd
525 1.1 cgd /*
526 1.1 cgd * Loop invariants:
527 1.1 cgd * There is room for one more character in record.
528 1.1 cgd * R_end points just past end of record.
529 1.1 cgd * Rp points just past last character in record.
530 1.1 cgd * Scan points at remainder of record that needs to be
531 1.1 cgd * scanned for tc=name constructs.
532 1.1 cgd */
533 1.1 cgd scan = record;
534 1.1 cgd for (;;) {
535 1.1 cgd if ((tc = cgetcap(scan, "tc", '=')) == NULL)
536 1.1 cgd break;
537 1.1 cgd
538 1.1 cgd /*
539 1.1 cgd * Find end of tc=name and stomp on the trailing `:'
540 1.1 cgd * (if present) so we can use it to call ourselves.
541 1.1 cgd */
542 1.1 cgd s = tc;
543 1.1 cgd for (;;)
544 1.1 cgd if (*s == '\0')
545 1.1 cgd break;
546 1.1 cgd else
547 1.1 cgd if (*s++ == ':') {
548 1.1 cgd *(s - 1) = '\0';
549 1.1 cgd break;
550 1.1 cgd }
551 1.1 cgd tcstart = tc - 3;
552 1.1 cgd tclen = s - tcstart;
553 1.1 cgd tcend = s;
554 1.1 cgd
555 1.1 cgd iret = getent(&icap, &ilen, db_p, fd, tc, depth+1,
556 1.1 cgd NULL);
557 1.1 cgd newicap = icap; /* Put into a register. */
558 1.1 cgd newilen = ilen;
559 1.1 cgd if (iret != 0) {
560 1.1 cgd /* an error */
561 1.1 cgd if (iret < -1) {
562 1.1 cgd if (myfd)
563 1.1 cgd (void)close(fd);
564 1.1 cgd free(record);
565 1.47 jnemeth return iret;
566 1.1 cgd }
567 1.1 cgd if (iret == 1)
568 1.1 cgd tc_not_resolved = 1;
569 1.1 cgd /* couldn't resolve tc */
570 1.1 cgd if (iret == -1) {
571 1.1 cgd *(s - 1) = ':';
572 1.1 cgd scan = s - 1;
573 1.1 cgd tc_not_resolved = 1;
574 1.1 cgd continue;
575 1.1 cgd
576 1.1 cgd }
577 1.1 cgd }
578 1.1 cgd /* not interested in name field of tc'ed record */
579 1.1 cgd s = newicap;
580 1.1 cgd for (;;)
581 1.1 cgd if (*s == '\0')
582 1.1 cgd break;
583 1.47 jnemeth else if (*s++ == ':')
584 1.47 jnemeth break;
585 1.1 cgd newilen -= s - newicap;
586 1.1 cgd newicap = s;
587 1.1 cgd
588 1.1 cgd /* make sure interpolated record is `:'-terminated */
589 1.1 cgd s += newilen;
590 1.47 jnemeth if (*(s - 1) != ':') {
591 1.1 cgd *s = ':'; /* overwrite NUL with : */
592 1.1 cgd newilen++;
593 1.1 cgd }
594 1.1 cgd
595 1.1 cgd /*
596 1.1 cgd * Make sure there's enough room to insert the
597 1.1 cgd * new record.
598 1.1 cgd */
599 1.1 cgd diff = newilen - tclen;
600 1.1 cgd if (diff >= r_end - rp) {
601 1.51 christos ptrdiff_t pos, tcpos, tcposend;
602 1.1 cgd size_t newsize;
603 1.1 cgd
604 1.1 cgd pos = rp - record;
605 1.1 cgd newsize = r_end - record + diff + BFRAG;
606 1.1 cgd tcpos = tcstart - record;
607 1.1 cgd tcposend = tcend - record;
608 1.33 itojun newrecord = realloc(record, newsize);
609 1.33 itojun if (newrecord == NULL) {
610 1.33 itojun free(record);
611 1.1 cgd if (myfd)
612 1.1 cgd (void)close(fd);
613 1.1 cgd free(icap);
614 1.30 lukem errno = ENOMEM;
615 1.47 jnemeth return -2;
616 1.1 cgd }
617 1.33 itojun record = newrecord;
618 1.1 cgd r_end = record + newsize;
619 1.1 cgd rp = record + pos;
620 1.1 cgd tcstart = record + tcpos;
621 1.1 cgd tcend = record + tcposend;
622 1.1 cgd }
623 1.1 cgd
624 1.1 cgd /*
625 1.1 cgd * Insert tc'ed record into our record.
626 1.1 cgd */
627 1.1 cgd s = tcstart + newilen;
628 1.23 perry memmove(s, tcend, (size_t)(rp - tcend));
629 1.23 perry memmove(tcstart, newicap, newilen);
630 1.1 cgd rp += diff;
631 1.1 cgd free(icap);
632 1.1 cgd
633 1.1 cgd /*
634 1.1 cgd * Start scan on `:' so next cgetcap works properly
635 1.1 cgd * (cgetcap always skips first field).
636 1.1 cgd */
637 1.47 jnemeth scan = s - 1;
638 1.1 cgd }
639 1.1 cgd
640 1.1 cgd }
641 1.1 cgd /*
642 1.1 cgd * Close file (if we opened it), give back any extra memory, and
643 1.1 cgd * return capability, length and success.
644 1.1 cgd */
645 1.1 cgd if (myfd)
646 1.1 cgd (void)close(fd);
647 1.1 cgd *len = rp - record - 1; /* don't count NUL */
648 1.33 itojun if (r_end > rp) {
649 1.33 itojun if ((newrecord =
650 1.1 cgd realloc(record, (size_t)(rp - record))) == NULL) {
651 1.33 itojun free(record);
652 1.1 cgd errno = ENOMEM;
653 1.47 jnemeth return -2;
654 1.1 cgd }
655 1.33 itojun record = newrecord;
656 1.33 itojun }
657 1.1 cgd
658 1.1 cgd *cap = record;
659 1.1 cgd if (tc_not_resolved)
660 1.47 jnemeth return 1;
661 1.47 jnemeth return 0;
662 1.1 cgd }
663 1.1 cgd
664 1.41 christos #ifndef SMALL
665 1.1 cgd static int
666 1.40 christos cdbget(DB *capdbp, char **bp, const char *name)
667 1.1 cgd {
668 1.25 christos DBT key;
669 1.24 christos DBT data;
670 1.1 cgd
671 1.30 lukem _DIAGASSERT(capdbp != NULL);
672 1.30 lukem _DIAGASSERT(bp != NULL);
673 1.30 lukem _DIAGASSERT(name != NULL);
674 1.30 lukem
675 1.42 christos key.data = __UNCONST(name);
676 1.1 cgd key.size = strlen(name);
677 1.1 cgd
678 1.1 cgd for (;;) {
679 1.1 cgd /* Get the reference. */
680 1.1 cgd switch(capdbp->get(capdbp, &key, &data, 0)) {
681 1.1 cgd case -1:
682 1.47 jnemeth return -2;
683 1.1 cgd case 1:
684 1.47 jnemeth return -1;
685 1.1 cgd }
686 1.1 cgd
687 1.1 cgd /* If not an index to another record, leave. */
688 1.1 cgd if (((char *)data.data)[0] != SHADOW)
689 1.1 cgd break;
690 1.1 cgd
691 1.1 cgd key.data = (char *)data.data + 1;
692 1.1 cgd key.size = data.size - 1;
693 1.1 cgd }
694 1.1 cgd
695 1.1 cgd *bp = (char *)data.data + 1;
696 1.47 jnemeth return ((char *)(data.data))[0] == TCERR ? 1 : 0;
697 1.1 cgd }
698 1.41 christos #endif
699 1.1 cgd
700 1.1 cgd /*
701 1.1 cgd * Cgetmatch will return 0 if name is one of the names of the capability
702 1.1 cgd * record buf, -1 if not.
703 1.1 cgd */
704 1.1 cgd int
705 1.40 christos cgetmatch(const char *buf, const char *name)
706 1.1 cgd {
707 1.21 mycroft const char *np, *bp;
708 1.1 cgd
709 1.30 lukem _DIAGASSERT(buf != NULL);
710 1.30 lukem _DIAGASSERT(name != NULL);
711 1.30 lukem
712 1.1 cgd /*
713 1.1 cgd * Start search at beginning of record.
714 1.1 cgd */
715 1.1 cgd bp = buf;
716 1.1 cgd for (;;) {
717 1.1 cgd /*
718 1.1 cgd * Try to match a record name.
719 1.1 cgd */
720 1.1 cgd np = name;
721 1.1 cgd for (;;)
722 1.26 christos if (*np == '\0') {
723 1.1 cgd if (*bp == '|' || *bp == ':' || *bp == '\0')
724 1.47 jnemeth return 0;
725 1.1 cgd else
726 1.1 cgd break;
727 1.47 jnemeth } else if (*bp++ != *np++)
728 1.47 jnemeth break;
729 1.1 cgd
730 1.1 cgd /*
731 1.1 cgd * Match failed, skip to next name in record.
732 1.1 cgd */
733 1.34 mrg if (bp > buf)
734 1.34 mrg bp--; /* a '|' or ':' may have stopped the match */
735 1.34 mrg else
736 1.47 jnemeth return -1;
737 1.1 cgd for (;;)
738 1.1 cgd if (*bp == '\0' || *bp == ':')
739 1.47 jnemeth return -1; /* match failed totally */
740 1.47 jnemeth else if (*bp++ == '|')
741 1.47 jnemeth break; /* found next name */
742 1.1 cgd }
743 1.1 cgd }
744 1.1 cgd
745 1.1 cgd int
746 1.40 christos cgetfirst(char **buf, const char * const *db_array)
747 1.1 cgd {
748 1.30 lukem
749 1.30 lukem _DIAGASSERT(buf != NULL);
750 1.30 lukem _DIAGASSERT(db_array != NULL);
751 1.30 lukem
752 1.1 cgd (void)cgetclose();
753 1.47 jnemeth return cgetnext(buf, db_array);
754 1.1 cgd }
755 1.1 cgd
756 1.1 cgd static FILE *pfp;
757 1.1 cgd static int slash;
758 1.40 christos static const char * const *dbp;
759 1.1 cgd
760 1.1 cgd int
761 1.40 christos cgetclose(void)
762 1.1 cgd {
763 1.1 cgd if (pfp != NULL) {
764 1.1 cgd (void)fclose(pfp);
765 1.1 cgd pfp = NULL;
766 1.1 cgd }
767 1.1 cgd dbp = NULL;
768 1.1 cgd gottoprec = 0;
769 1.1 cgd slash = 0;
770 1.47 jnemeth return 0;
771 1.1 cgd }
772 1.1 cgd
773 1.1 cgd /*
774 1.1 cgd * Cgetnext() gets either the first or next entry in the logical database
775 1.1 cgd * specified by db_array. It returns 0 upon completion of the database, 1
776 1.1 cgd * upon returning an entry with more remaining, and -1 if an error occurs.
777 1.1 cgd */
778 1.45 christos /* coverity[+alloc : arg-*0] */
779 1.1 cgd int
780 1.40 christos cgetnext(char **bp, const char * const *db_array)
781 1.1 cgd {
782 1.43 christos size_t len = 0;
783 1.17 perry int status, done;
784 1.1 cgd char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE];
785 1.18 thorpej size_t dummy;
786 1.1 cgd
787 1.30 lukem _DIAGASSERT(bp != NULL);
788 1.30 lukem _DIAGASSERT(db_array != NULL);
789 1.30 lukem
790 1.1 cgd if (dbp == NULL)
791 1.1 cgd dbp = db_array;
792 1.1 cgd
793 1.50 christos if (pfp == NULL && (pfp = fopen(*dbp, "re")) == NULL) {
794 1.1 cgd (void)cgetclose();
795 1.47 jnemeth return -1;
796 1.1 cgd }
797 1.47 jnemeth for (;;) {
798 1.47 jnemeth if (toprec != NULL && !gottoprec) {
799 1.1 cgd gottoprec = 1;
800 1.1 cgd line = toprec;
801 1.1 cgd } else {
802 1.6 cgd line = fgetln(pfp, &len);
803 1.44 christos if (line == NULL) {
804 1.44 christos if (pfp == NULL)
805 1.44 christos return -1;
806 1.1 cgd if (ferror(pfp)) {
807 1.1 cgd (void)cgetclose();
808 1.47 jnemeth return -1;
809 1.1 cgd } else {
810 1.19 tv (void)fclose(pfp);
811 1.19 tv pfp = NULL;
812 1.1 cgd if (*++dbp == NULL) {
813 1.1 cgd (void)cgetclose();
814 1.47 jnemeth return 0;
815 1.1 cgd } else if ((pfp =
816 1.50 christos fopen(*dbp, "re")) == NULL) {
817 1.1 cgd (void)cgetclose();
818 1.47 jnemeth return -1;
819 1.1 cgd } else
820 1.1 cgd continue;
821 1.1 cgd }
822 1.5 cgd } else
823 1.5 cgd line[len - 1] = '\0';
824 1.5 cgd if (len == 1) {
825 1.1 cgd slash = 0;
826 1.1 cgd continue;
827 1.1 cgd }
828 1.26 christos if (isspace((unsigned char)*line) ||
829 1.1 cgd *line == ':' || *line == '#' || slash) {
830 1.5 cgd if (line[len - 2] == '\\')
831 1.1 cgd slash = 1;
832 1.1 cgd else
833 1.1 cgd slash = 0;
834 1.1 cgd continue;
835 1.1 cgd }
836 1.5 cgd if (line[len - 2] == '\\')
837 1.1 cgd slash = 1;
838 1.1 cgd else
839 1.1 cgd slash = 0;
840 1.1 cgd }
841 1.1 cgd
842 1.1 cgd
843 1.1 cgd /*
844 1.1 cgd * Line points to a name line.
845 1.1 cgd */
846 1.35 groo if (len > sizeof(nbuf))
847 1.35 groo return -1;
848 1.1 cgd done = 0;
849 1.1 cgd np = nbuf;
850 1.1 cgd for (;;) {
851 1.1 cgd for (cp = line; *cp != '\0'; cp++) {
852 1.1 cgd if (*cp == ':') {
853 1.1 cgd *np++ = ':';
854 1.1 cgd done = 1;
855 1.1 cgd break;
856 1.1 cgd }
857 1.1 cgd if (*cp == '\\')
858 1.1 cgd break;
859 1.1 cgd *np++ = *cp;
860 1.1 cgd }
861 1.1 cgd if (done) {
862 1.1 cgd *np = '\0';
863 1.1 cgd break;
864 1.1 cgd } else { /* name field extends beyond the line */
865 1.6 cgd line = fgetln(pfp, &len);
866 1.1 cgd if (line == NULL && pfp) {
867 1.1 cgd if (ferror(pfp)) {
868 1.1 cgd (void)cgetclose();
869 1.47 jnemeth return -1;
870 1.1 cgd }
871 1.19 tv (void)fclose(pfp);
872 1.19 tv pfp = NULL;
873 1.19 tv *np = '\0';
874 1.19 tv break;
875 1.5 cgd } else
876 1.5 cgd line[len - 1] = '\0';
877 1.1 cgd }
878 1.1 cgd }
879 1.35 groo if (len > sizeof(buf))
880 1.35 groo return -1;
881 1.1 cgd rp = buf;
882 1.47 jnemeth for (cp = nbuf; *cp != '\0'; cp++)
883 1.1 cgd if (*cp == '|' || *cp == ':')
884 1.1 cgd break;
885 1.1 cgd else
886 1.1 cgd *rp++ = *cp;
887 1.1 cgd
888 1.1 cgd *rp = '\0';
889 1.1 cgd /*
890 1.1 cgd * XXX
891 1.1 cgd * Last argument of getent here should be nbuf if we want true
892 1.1 cgd * sequential access in the case of duplicates.
893 1.1 cgd * With NULL, getent will return the first entry found
894 1.1 cgd * rather than the duplicate entry record. This is a
895 1.1 cgd * matter of semantics that should be resolved.
896 1.1 cgd */
897 1.1 cgd status = getent(bp, &dummy, db_array, -1, buf, 0, NULL);
898 1.1 cgd if (status == -2 || status == -3)
899 1.1 cgd (void)cgetclose();
900 1.1 cgd
901 1.47 jnemeth return status + 1;
902 1.1 cgd }
903 1.1 cgd /* NOTREACHED */
904 1.1 cgd }
905 1.1 cgd
906 1.1 cgd /*
907 1.1 cgd * Cgetstr retrieves the value of the string capability cap from the
908 1.1 cgd * capability record pointed to by buf. A pointer to a decoded, NUL
909 1.1 cgd * terminated, malloc'd copy of the string is returned in the char *
910 1.1 cgd * pointed to by str. The length of the string not including the trailing
911 1.1 cgd * NUL is returned on success, -1 if the requested string capability
912 1.1 cgd * couldn't be found, -2 if a system error was encountered (storage
913 1.1 cgd * allocation failure).
914 1.1 cgd */
915 1.1 cgd int
916 1.40 christos cgetstr(char *buf, const char *cap, char **str)
917 1.1 cgd {
918 1.16 perry u_int m_room;
919 1.21 mycroft const char *bp;
920 1.21 mycroft char *mp;
921 1.51 christos ptrdiff_t len;
922 1.33 itojun char *mem, *newmem;
923 1.1 cgd
924 1.30 lukem _DIAGASSERT(buf != NULL);
925 1.30 lukem _DIAGASSERT(cap != NULL);
926 1.30 lukem _DIAGASSERT(str != NULL);
927 1.30 lukem
928 1.1 cgd /*
929 1.1 cgd * Find string capability cap
930 1.1 cgd */
931 1.1 cgd bp = cgetcap(buf, cap, '=');
932 1.1 cgd if (bp == NULL)
933 1.47 jnemeth return -1;
934 1.1 cgd
935 1.1 cgd /*
936 1.1 cgd * Conversion / storage allocation loop ... Allocate memory in
937 1.1 cgd * chunks SFRAG in size.
938 1.1 cgd */
939 1.1 cgd if ((mem = malloc(SFRAG)) == NULL) {
940 1.1 cgd errno = ENOMEM;
941 1.47 jnemeth return -2; /* couldn't even allocate the first fragment */
942 1.1 cgd }
943 1.1 cgd m_room = SFRAG;
944 1.1 cgd mp = mem;
945 1.1 cgd
946 1.1 cgd while (*bp != ':' && *bp != '\0') {
947 1.1 cgd /*
948 1.1 cgd * Loop invariants:
949 1.1 cgd * There is always room for one more character in mem.
950 1.1 cgd * Mp always points just past last character in mem.
951 1.1 cgd * Bp always points at next character in buf.
952 1.1 cgd */
953 1.1 cgd if (*bp == '^') {
954 1.1 cgd bp++;
955 1.1 cgd if (*bp == ':' || *bp == '\0')
956 1.1 cgd break; /* drop unfinished escape */
957 1.1 cgd *mp++ = *bp++ & 037;
958 1.1 cgd } else if (*bp == '\\') {
959 1.1 cgd bp++;
960 1.1 cgd if (*bp == ':' || *bp == '\0')
961 1.1 cgd break; /* drop unfinished escape */
962 1.1 cgd if ('0' <= *bp && *bp <= '7') {
963 1.16 perry int n, i;
964 1.1 cgd
965 1.1 cgd n = 0;
966 1.1 cgd i = 3; /* maximum of three octal digits */
967 1.1 cgd do {
968 1.1 cgd n = n * 8 + (*bp++ - '0');
969 1.1 cgd } while (--i && '0' <= *bp && *bp <= '7');
970 1.1 cgd *mp++ = n;
971 1.1 cgd }
972 1.1 cgd else switch (*bp++) {
973 1.1 cgd case 'b': case 'B':
974 1.1 cgd *mp++ = '\b';
975 1.1 cgd break;
976 1.1 cgd case 't': case 'T':
977 1.1 cgd *mp++ = '\t';
978 1.1 cgd break;
979 1.1 cgd case 'n': case 'N':
980 1.1 cgd *mp++ = '\n';
981 1.1 cgd break;
982 1.1 cgd case 'f': case 'F':
983 1.1 cgd *mp++ = '\f';
984 1.1 cgd break;
985 1.1 cgd case 'r': case 'R':
986 1.1 cgd *mp++ = '\r';
987 1.1 cgd break;
988 1.1 cgd case 'e': case 'E':
989 1.1 cgd *mp++ = ESC;
990 1.1 cgd break;
991 1.1 cgd case 'c': case 'C':
992 1.1 cgd *mp++ = ':';
993 1.1 cgd break;
994 1.1 cgd default:
995 1.1 cgd /*
996 1.1 cgd * Catches '\', '^', and
997 1.1 cgd * everything else.
998 1.1 cgd */
999 1.1 cgd *mp++ = *(bp-1);
1000 1.1 cgd break;
1001 1.1 cgd }
1002 1.1 cgd } else
1003 1.1 cgd *mp++ = *bp++;
1004 1.1 cgd m_room--;
1005 1.1 cgd
1006 1.1 cgd /*
1007 1.1 cgd * Enforce loop invariant: if no room left in current
1008 1.1 cgd * buffer, try to get some more.
1009 1.1 cgd */
1010 1.1 cgd if (m_room == 0) {
1011 1.1 cgd size_t size = mp - mem;
1012 1.1 cgd
1013 1.33 itojun if ((newmem = realloc(mem, size + SFRAG)) == NULL) {
1014 1.33 itojun free(mem);
1015 1.47 jnemeth return -2;
1016 1.33 itojun }
1017 1.33 itojun mem = newmem;
1018 1.1 cgd m_room = SFRAG;
1019 1.1 cgd mp = mem + size;
1020 1.1 cgd }
1021 1.1 cgd }
1022 1.1 cgd *mp++ = '\0'; /* loop invariant let's us do this */
1023 1.1 cgd m_room--;
1024 1.1 cgd len = mp - mem - 1;
1025 1.1 cgd
1026 1.1 cgd /*
1027 1.1 cgd * Give back any extra memory and return value and success.
1028 1.1 cgd */
1029 1.33 itojun if (m_room != 0) {
1030 1.33 itojun if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) {
1031 1.33 itojun free(mem);
1032 1.47 jnemeth return -2;
1033 1.33 itojun }
1034 1.33 itojun mem = newmem;
1035 1.33 itojun }
1036 1.1 cgd *str = mem;
1037 1.51 christos _DIAGASSERT(__type_fit(int, len));
1038 1.51 christos return (int)len;
1039 1.1 cgd }
1040 1.1 cgd
1041 1.1 cgd /*
1042 1.1 cgd * Cgetustr retrieves the value of the string capability cap from the
1043 1.1 cgd * capability record pointed to by buf. The difference between cgetustr()
1044 1.1 cgd * and cgetstr() is that cgetustr does not decode escapes but rather treats
1045 1.1 cgd * all characters literally. A pointer to a NUL terminated malloc'd
1046 1.1 cgd * copy of the string is returned in the char pointed to by str. The
1047 1.1 cgd * length of the string not including the trailing NUL is returned on success,
1048 1.1 cgd * -1 if the requested string capability couldn't be found, -2 if a system
1049 1.1 cgd * error was encountered (storage allocation failure).
1050 1.1 cgd */
1051 1.1 cgd int
1052 1.40 christos cgetustr(char *buf, const char *cap, char **str)
1053 1.1 cgd {
1054 1.16 perry u_int m_room;
1055 1.21 mycroft const char *bp;
1056 1.21 mycroft char *mp;
1057 1.51 christos size_t len;
1058 1.33 itojun char *mem, *newmem;
1059 1.1 cgd
1060 1.30 lukem _DIAGASSERT(buf != NULL);
1061 1.30 lukem _DIAGASSERT(cap != NULL);
1062 1.30 lukem _DIAGASSERT(str != NULL);
1063 1.30 lukem
1064 1.1 cgd /*
1065 1.1 cgd * Find string capability cap
1066 1.1 cgd */
1067 1.1 cgd if ((bp = cgetcap(buf, cap, '=')) == NULL)
1068 1.47 jnemeth return -1;
1069 1.1 cgd
1070 1.1 cgd /*
1071 1.1 cgd * Conversion / storage allocation loop ... Allocate memory in
1072 1.1 cgd * chunks SFRAG in size.
1073 1.1 cgd */
1074 1.1 cgd if ((mem = malloc(SFRAG)) == NULL) {
1075 1.1 cgd errno = ENOMEM;
1076 1.47 jnemeth return -2; /* couldn't even allocate the first fragment */
1077 1.1 cgd }
1078 1.1 cgd m_room = SFRAG;
1079 1.1 cgd mp = mem;
1080 1.1 cgd
1081 1.1 cgd while (*bp != ':' && *bp != '\0') {
1082 1.1 cgd /*
1083 1.1 cgd * Loop invariants:
1084 1.1 cgd * There is always room for one more character in mem.
1085 1.1 cgd * Mp always points just past last character in mem.
1086 1.1 cgd * Bp always points at next character in buf.
1087 1.1 cgd */
1088 1.1 cgd *mp++ = *bp++;
1089 1.1 cgd m_room--;
1090 1.1 cgd
1091 1.1 cgd /*
1092 1.1 cgd * Enforce loop invariant: if no room left in current
1093 1.1 cgd * buffer, try to get some more.
1094 1.1 cgd */
1095 1.1 cgd if (m_room == 0) {
1096 1.1 cgd size_t size = mp - mem;
1097 1.1 cgd
1098 1.33 itojun if ((newmem = realloc(mem, size + SFRAG)) == NULL) {
1099 1.33 itojun free(mem);
1100 1.47 jnemeth return -2;
1101 1.33 itojun }
1102 1.33 itojun mem = newmem;
1103 1.1 cgd m_room = SFRAG;
1104 1.1 cgd mp = mem + size;
1105 1.1 cgd }
1106 1.1 cgd }
1107 1.1 cgd *mp++ = '\0'; /* loop invariant let's us do this */
1108 1.1 cgd m_room--;
1109 1.1 cgd len = mp - mem - 1;
1110 1.1 cgd
1111 1.1 cgd /*
1112 1.1 cgd * Give back any extra memory and return value and success.
1113 1.1 cgd */
1114 1.33 itojun if (m_room != 0) {
1115 1.33 itojun if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) {
1116 1.33 itojun free(mem);
1117 1.47 jnemeth return -2;
1118 1.33 itojun }
1119 1.33 itojun mem = newmem;
1120 1.33 itojun }
1121 1.1 cgd *str = mem;
1122 1.51 christos _DIAGASSERT(__type_fit(int, len));
1123 1.51 christos return (int)len;
1124 1.1 cgd }
1125 1.1 cgd
1126 1.1 cgd /*
1127 1.1 cgd * Cgetnum retrieves the value of the numeric capability cap from the
1128 1.1 cgd * capability record pointed to by buf. The numeric value is returned in
1129 1.1 cgd * the long pointed to by num. 0 is returned on success, -1 if the requested
1130 1.1 cgd * numeric capability couldn't be found.
1131 1.1 cgd */
1132 1.1 cgd int
1133 1.40 christos cgetnum(char *buf, const char *cap, long *num)
1134 1.1 cgd {
1135 1.16 perry long n;
1136 1.16 perry int base, digit;
1137 1.21 mycroft const char *bp;
1138 1.1 cgd
1139 1.30 lukem _DIAGASSERT(buf != NULL);
1140 1.30 lukem _DIAGASSERT(cap != NULL);
1141 1.30 lukem _DIAGASSERT(num != NULL);
1142 1.30 lukem
1143 1.1 cgd /*
1144 1.1 cgd * Find numeric capability cap
1145 1.1 cgd */
1146 1.1 cgd bp = cgetcap(buf, cap, '#');
1147 1.1 cgd if (bp == NULL)
1148 1.47 jnemeth return -1;
1149 1.1 cgd
1150 1.1 cgd /*
1151 1.1 cgd * Look at value and determine numeric base:
1152 1.1 cgd * 0x... or 0X... hexadecimal,
1153 1.1 cgd * else 0... octal,
1154 1.1 cgd * else decimal.
1155 1.1 cgd */
1156 1.1 cgd if (*bp == '0') {
1157 1.1 cgd bp++;
1158 1.1 cgd if (*bp == 'x' || *bp == 'X') {
1159 1.1 cgd bp++;
1160 1.1 cgd base = 16;
1161 1.1 cgd } else
1162 1.1 cgd base = 8;
1163 1.1 cgd } else
1164 1.1 cgd base = 10;
1165 1.1 cgd
1166 1.1 cgd /*
1167 1.1 cgd * Conversion loop ...
1168 1.1 cgd */
1169 1.1 cgd n = 0;
1170 1.1 cgd for (;;) {
1171 1.1 cgd if ('0' <= *bp && *bp <= '9')
1172 1.1 cgd digit = *bp - '0';
1173 1.1 cgd else if ('a' <= *bp && *bp <= 'f')
1174 1.1 cgd digit = 10 + *bp - 'a';
1175 1.1 cgd else if ('A' <= *bp && *bp <= 'F')
1176 1.1 cgd digit = 10 + *bp - 'A';
1177 1.1 cgd else
1178 1.1 cgd break;
1179 1.1 cgd
1180 1.1 cgd if (digit >= base)
1181 1.1 cgd break;
1182 1.1 cgd
1183 1.1 cgd n = n * base + digit;
1184 1.1 cgd bp++;
1185 1.1 cgd }
1186 1.1 cgd
1187 1.1 cgd /*
1188 1.1 cgd * Return value and success.
1189 1.1 cgd */
1190 1.1 cgd *num = n;
1191 1.47 jnemeth return 0;
1192 1.1 cgd }
1193 1.1 cgd
1194 1.1 cgd
1195 1.1 cgd /*
1196 1.1 cgd * Compare name field of record.
1197 1.1 cgd */
1198 1.1 cgd static int
1199 1.40 christos nfcmp(char *nf, char *rec)
1200 1.1 cgd {
1201 1.1 cgd char *cp, tmp;
1202 1.1 cgd int ret;
1203 1.30 lukem
1204 1.30 lukem _DIAGASSERT(nf != NULL);
1205 1.30 lukem _DIAGASSERT(rec != NULL);
1206 1.30 lukem
1207 1.1 cgd for (cp = rec; *cp != ':'; cp++)
1208 1.47 jnemeth continue;
1209 1.1 cgd
1210 1.1 cgd tmp = *(cp + 1);
1211 1.1 cgd *(cp + 1) = '\0';
1212 1.1 cgd ret = strcmp(nf, rec);
1213 1.1 cgd *(cp + 1) = tmp;
1214 1.1 cgd
1215 1.47 jnemeth return ret;
1216 1.1 cgd }
1217