]>
git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/getcap.c
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Casey Leedom of Lawrence Livermore National Laboratory.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid
[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/lib/libc/gen/getcap.c,v 1.23 2009/11/25 04:45:45 wollman Exp $");
39 #include "namespace.h"
40 #include <sys/types.h>
50 #include "un-namespace.h"
56 #define ESC ('[' & 037) /* ASCII ESC */
57 #define MAX_RECURSION 32 /* maximum getent recursion */
58 #define SFRAG 100 /* cgetstr mallocs in SFRAG chunks */
62 #define SHADOW (char)2
64 static size_t topreclen
; /* toprec length */
65 static char *toprec
; /* Additional record specified by cgetset() */
66 static int gottoprec
; /* Flag indicating retrieval of toprecord */
68 static int cdbget(DB
*, char **, const char *);
69 static int getent(char **, u_int
*, char **, int, const char *, int, char *);
70 static int nfcmp(char *, char *);
73 * Cgetset() allows the addition of a user specified buffer to be added
74 * to the database array, in effect "pushing" the buffer on top of the
75 * virtual database. 0 is returned on success, -1 on failure.
78 cgetset(const char *ent
)
87 topreclen
= strlen(ent
);
88 if ((toprec
= malloc (topreclen
+ 1)) == NULL
) {
93 (void)strcpy(toprec
, ent
);
98 * Cgetcap searches the capability record buf for the capability cap with
99 * type `type'. A pointer to the value of cap is returned on success, NULL
100 * if the requested capability couldn't be found.
102 * Specifying a type of ':' means that nothing should follow cap (:cap:).
103 * In this case a pointer to the terminating ':' or NUL will be returned if
106 * If (cap, '@') or (cap, terminator, '@') is found before (cap, terminator)
110 cgetcap(char *buf
, const char *cap
, int type
)
118 * Skip past the current capability field - it's either the
119 * name field if this is the first time through the loop, or
120 * the remainder of a field whose name failed to match cap.
130 * Try to match (cap, type) in buf.
132 for (cp
= cap
; *cp
== *bp
&& *bp
!= '\0'; cp
++, bp
++)
139 if (*bp
!= '\0' && *bp
!= ':')
146 return (*bp
== '@' ? NULL
: bp
);
152 * Cgetent extracts the capability record name from the NULL terminated file
153 * array db_array and returns a pointer to a malloc'd copy of it in buf.
154 * Buf must be retained through all subsequent calls to cgetcap, cgetnum,
155 * cgetflag, and cgetstr, but may then be free'd. 0 is returned on success,
156 * -1 if the requested record couldn't be found, -2 if a system error was
157 * encountered (couldn't open/read a file, etc.), and -3 if a potential
158 * reference loop is detected.
161 cgetent(char **buf
, char **db_array
, const char *name
)
165 return (getent(buf
, &dummy
, db_array
, -1, name
, 0, NULL
));
169 * Getent implements the functions of cgetent. If fd is non-negative,
170 * *db_array has already been opened and fd is the open file descriptor. We
171 * do this to save time and avoid using up file descriptors for tc=
174 * Getent returns the same success/failure codes as cgetent. On success, a
175 * pointer to a malloc'ed capability record with all tc= capabilities fully
176 * expanded and its length (not including trailing ASCII NUL) are left in
180 * + Allocate memory incrementally as needed in chunks of size BFRAG
181 * for capability buffer.
182 * + Recurse for each tc=name and interpolate result. Stop when all
183 * names interpolated, a name can't be found, or depth exceeds
187 getent(char **cap
, u_int
*len
, char **db_array
, int fd
, const char *name
,
188 int depth
, char *nfield
)
191 char *r_end
, *rp
, **db_p
;
192 int myfd
, eof
, foundit
, retval
;
195 char pbuf
[_POSIX_PATH_MAX
];
198 * Return with ``loop detected'' error if we've recursed more than
199 * MAX_RECURSION times.
201 if (depth
> MAX_RECURSION
)
205 * Check if we have a top record from cgetset().
207 if (depth
== 0 && toprec
!= NULL
&& cgetmatch(toprec
, name
) == 0) {
208 if ((record
= malloc (topreclen
+ BFRAG
)) == NULL
) {
212 (void)strcpy(record
, toprec
);
215 rp
= record
+ topreclen
+ 1;
220 * Allocate first chunk of memory.
222 if ((record
= malloc(BFRAG
)) == NULL
) {
226 r_end
= record
+ BFRAG
;
229 * Loop through database array until finding the record.
232 for (db_p
= db_array
; *db_p
!= NULL
; db_p
++) {
236 * Open database if not already open.
240 (void)lseek(fd
, (off_t
)0, SEEK_SET
);
243 (void)snprintf(pbuf
, sizeof(pbuf
), "%s.db", *db_p
);
244 if ((capdbp
= dbopen(pbuf
, O_RDONLY
, 0, DB_HASH
, 0))
247 retval
= cdbget(capdbp
, &record
, name
);
249 /* no record available */
250 (void)capdbp
->close(capdbp
);
253 /* save the data; close frees it */
254 cbuf
= strdup(record
);
255 if (capdbp
->close(capdbp
) < 0) {
267 fd
= _open(*db_p
, O_RDONLY
, 0);
274 * Find the requested capability record ...
283 * There is always room for one more character in record.
284 * R_end always points just past end of record.
285 * Rp always points just past last character in record.
286 * B_end always points just past last character in buf.
287 * Bp always points at next character in buf.
294 * Read in a line implementing (\, newline)
302 n
= _read(fd
, buf
, sizeof(buf
));
321 if (rp
> record
&& *(rp
-1) == '\\') {
330 * Enforce loop invariant: if no room
331 * left in record buffer, try to get
339 newsize
= r_end
- record
+ BFRAG
;
340 record
= reallocf(record
, newsize
);
341 if (record
== NULL
) {
347 r_end
= record
+ newsize
;
351 /* loop invariant let's us do this */
355 * If encountered eof check next file.
361 * Toss blank lines and comments.
363 if (*record
== '\0' || *record
== '#')
367 * See if this is the record we want ...
369 if (cgetmatch(record
, name
) == 0) {
370 if (nfield
== NULL
|| !nfcmp(nfield
, record
)) {
372 break; /* found it! */
387 * Got the capability record, but now we have to expand all tc=name
388 * references in it ...
394 int diff
, iret
, tclen
;
395 char *icap
, *scan
, *tc
, *tcstart
, *tcend
;
399 * There is room for one more character in record.
400 * R_end points just past end of record.
401 * Rp points just past last character in record.
402 * Scan points at remainder of record that needs to be
403 * scanned for tc=name constructs.
408 if ((tc
= cgetcap(scan
, "tc", '=')) == NULL
)
412 * Find end of tc=name and stomp on the trailing `:'
413 * (if present) so we can use it to call ourselves.
428 iret
= getent(&icap
, &ilen
, db_p
, fd
, tc
, depth
+1,
430 newicap
= icap
; /* Put into a register. */
442 /* couldn't resolve tc */
451 /* not interested in name field of tc'ed record */
459 newilen
-= s
- newicap
;
462 /* make sure interpolated record is `:'-terminated */
465 *s
= ':'; /* overwrite NUL with : */
470 * Make sure there's enough room to insert the
473 diff
= newilen
- tclen
;
474 if (diff
>= r_end
- rp
) {
475 u_int pos
, tcpos
, tcposend
;
479 newsize
= r_end
- record
+ diff
+ BFRAG
;
480 tcpos
= tcstart
- record
;
481 tcposend
= tcend
- record
;
482 record
= reallocf(record
, newsize
);
483 if (record
== NULL
) {
490 r_end
= record
+ newsize
;
492 tcstart
= record
+ tcpos
;
493 tcend
= record
+ tcposend
;
497 * Insert tc'ed record into our record.
499 s
= tcstart
+ newilen
;
500 bcopy(tcend
, s
, rp
- tcend
);
501 bcopy(newicap
, tcstart
, newilen
);
506 * Start scan on `:' so next cgetcap works properly
507 * (cgetcap always skips first field).
514 * Close file (if we opened it), give back any extra memory, and
515 * return capability, length and success.
519 *len
= rp
- record
- 1; /* don't count NUL */
522 reallocf(record
, (size_t)(rp
- record
))) == NULL
) {
534 cdbget(DB
*capdbp
, char **bp
, const char *name
)
539 namebuf
= strdup(name
);
543 key
.size
= strlen(namebuf
);
546 /* Get the reference. */
547 switch(capdbp
->get(capdbp
, &key
, &data
, 0)) {
556 /* If not an index to another record, leave. */
557 if (((char *)data
.data
)[0] != SHADOW
)
560 key
.data
= (char *)data
.data
+ 1;
561 key
.size
= data
.size
- 1;
564 *bp
= (char *)data
.data
+ 1;
566 return (((char *)(data
.data
))[0] == TCERR
? 1 : 0);
570 * Cgetmatch will return 0 if name is one of the names of the capability
571 * record buf, -1 if not.
574 cgetmatch(const char *buf
, const char *name
)
578 if (name
== NULL
|| *name
== '\0')
582 * Start search at beginning of record.
587 * Try to match a record name.
592 if (*bp
== '|' || *bp
== ':' || *bp
== '\0')
601 * Match failed, skip to next name in record.
603 bp
--; /* a '|' or ':' may have stopped the match */
605 if (*bp
== '\0' || *bp
== ':')
606 return (-1); /* match failed totally */
609 break; /* found next name */
618 cgetfirst(char **buf
, char **db_array
)
621 return (cgetnext(buf
, db_array
));
642 * Cgetnext() gets either the first or next entry in the logical database
643 * specified by db_array. It returns 0 upon completion of the database, 1
644 * upon returning an entry with more remaining, and -1 if an error occurs.
647 cgetnext(char **bp
, char **db_array
)
650 int done
, hadreaderr
, savederrno
, status
;
651 char *cp
, *line
, *rp
, *np
, buf
[BSIZE
], nbuf
[BSIZE
];
657 if (pfp
== NULL
&& (pfp
= fopen(*dbp
, "r")) == NULL
) {
662 if (toprec
&& !gottoprec
) {
666 line
= fgetln(pfp
, &len
);
667 if (line
== NULL
&& pfp
) {
668 hadreaderr
= ferror(pfp
);
678 if (*++dbp
== NULL
) {
682 fopen(*dbp
, "r")) == NULL
) {
689 line
[len
- 1] = '\0';
694 if (isspace((unsigned char)*line
) ||
695 *line
== ':' || *line
== '#' || slash
) {
696 if (line
[len
- 2] == '\\')
702 if (line
[len
- 2] == '\\')
710 * Line points to a name line.
715 for (cp
= line
; *cp
!= '\0'; cp
++) {
728 } else { /* name field extends beyond the line */
729 line
= fgetln(pfp
, &len
);
730 if (line
== NULL
&& pfp
) {
731 /* Name extends beyond the EOF! */
732 hadreaderr
= ferror(pfp
);
746 line
[len
- 1] = '\0';
750 for(cp
= nbuf
; *cp
!= '\0'; cp
++)
751 if (*cp
== '|' || *cp
== ':')
759 * Last argument of getent here should be nbuf if we want true
760 * sequential access in the case of duplicates.
761 * With NULL, getent will return the first entry found
762 * rather than the duplicate entry record. This is a
763 * matter of semantics that should be resolved.
765 status
= getent(bp
, &dummy
, db_array
, -1, buf
, 0, NULL
);
766 if (status
== -2 || status
== -3)
775 * Cgetstr retrieves the value of the string capability cap from the
776 * capability record pointed to by buf. A pointer to a decoded, NUL
777 * terminated, malloc'd copy of the string is returned in the char *
778 * pointed to by str. The length of the string not including the trailing
779 * NUL is returned on success, -1 if the requested string capability
780 * couldn't be found, -2 if a system error was encountered (storage
781 * allocation failure).
784 cgetstr(char *buf
, const char *cap
, char **str
)
792 * Find string capability cap
794 bp
= cgetcap(buf
, cap
, '=');
799 * Conversion / storage allocation loop ... Allocate memory in
800 * chunks SFRAG in size.
802 if ((mem
= malloc(SFRAG
)) == NULL
) {
804 return (-2); /* couldn't even allocate the first fragment */
809 while (*bp
!= ':' && *bp
!= '\0') {
812 * There is always room for one more character in mem.
813 * Mp always points just past last character in mem.
814 * Bp always points at next character in buf.
818 if (*bp
== ':' || *bp
== '\0')
819 break; /* drop unfinished escape */
825 } else if (*bp
== '\\') {
827 if (*bp
== ':' || *bp
== '\0')
828 break; /* drop unfinished escape */
829 if ('0' <= *bp
&& *bp
<= '7') {
833 i
= 3; /* maximum of three octal digits */
835 n
= n
* 8 + (*bp
++ - '0');
836 } while (--i
&& '0' <= *bp
&& *bp
<= '7');
839 else switch (*bp
++) {
863 * Catches '\', '^', and
874 * Enforce loop invariant: if no room left in current
875 * buffer, try to get some more.
878 size_t size
= mp
- mem
;
880 if ((mem
= reallocf(mem
, size
+ SFRAG
)) == NULL
)
886 *mp
++ = '\0'; /* loop invariant let's us do this */
891 * Give back any extra memory and return value and success.
894 if ((mem
= reallocf(mem
, (size_t)(mp
- mem
))) == NULL
)
901 * Cgetustr retrieves the value of the string capability cap from the
902 * capability record pointed to by buf. The difference between cgetustr()
903 * and cgetstr() is that cgetustr does not decode escapes but rather treats
904 * all characters literally. A pointer to a NUL terminated malloc'd
905 * copy of the string is returned in the char pointed to by str. The
906 * length of the string not including the trailing NUL is returned on success,
907 * -1 if the requested string capability couldn't be found, -2 if a system
908 * error was encountered (storage allocation failure).
911 cgetustr(char *buf
, const char *cap
, char **str
)
919 * Find string capability cap
921 if ((bp
= cgetcap(buf
, cap
, '=')) == NULL
)
925 * Conversion / storage allocation loop ... Allocate memory in
926 * chunks SFRAG in size.
928 if ((mem
= malloc(SFRAG
)) == NULL
) {
930 return (-2); /* couldn't even allocate the first fragment */
935 while (*bp
!= ':' && *bp
!= '\0') {
938 * There is always room for one more character in mem.
939 * Mp always points just past last character in mem.
940 * Bp always points at next character in buf.
946 * Enforce loop invariant: if no room left in current
947 * buffer, try to get some more.
950 size_t size
= mp
- mem
;
952 if ((mem
= reallocf(mem
, size
+ SFRAG
)) == NULL
)
958 *mp
++ = '\0'; /* loop invariant let's us do this */
963 * Give back any extra memory and return value and success.
966 if ((mem
= reallocf(mem
, (size_t)(mp
- mem
))) == NULL
)
973 * Cgetnum retrieves the value of the numeric capability cap from the
974 * capability record pointed to by buf. The numeric value is returned in
975 * the long pointed to by num. 0 is returned on success, -1 if the requested
976 * numeric capability couldn't be found.
979 cgetnum(char *buf
, const char *cap
, long *num
)
986 * Find numeric capability cap
988 bp
= cgetcap(buf
, cap
, '#');
993 * Look at value and determine numeric base:
994 * 0x... or 0X... hexadecimal,
1000 if (*bp
== 'x' || *bp
== 'X') {
1009 * Conversion loop ...
1013 if ('0' <= *bp
&& *bp
<= '9')
1015 else if ('a' <= *bp
&& *bp
<= 'f')
1016 digit
= 10 + *bp
- 'a';
1017 else if ('A' <= *bp
&& *bp
<= 'F')
1018 digit
= 10 + *bp
- 'A';
1025 n
= n
* base
+ digit
;
1030 * Return value and success.
1038 * Compare name field of record.
1041 nfcmp(char *nf
, char *rec
)
1046 for (cp
= rec
; *cp
!= ':'; cp
++)
1051 ret
= strcmp(nf
, rec
);