]>
git.saurik.com Git - apple/libc.git/blob - gen/getcap.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1992, 1993
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
30 * Casey Leedom of Lawrence Livermore National Laboratory.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #if defined(LIBC_SCCS) && !defined(lint)
62 static char rcsid
[] = "$OpenBSD: getcap.c,v 1.4 1997/02/01 04:35:33 deraadt Exp $";
63 #endif /* LIBC_SCCS and not lint */
65 #include <sys/types.h>
79 #define ESC ('[' & 037) /* ASCII ESC */
80 #define MAX_RECURSION 32 /* maximum getent recursion */
81 #define SFRAG 100 /* cgetstr mallocs in SFRAG chunks */
85 #define SHADOW (char)2
87 static size_t topreclen
; /* toprec length */
88 static char *toprec
; /* Additional record specified by cgetset() */
89 static int gottoprec
; /* Flag indicating retrieval of toprecord */
91 static int cdbget
__P((DB
*, char **, char *));
92 static int getent
__P((char **, u_int
*, char **, int, char *, int, char *));
93 static int nfcmp
__P((char *, char *));
96 * Cgetset() allows the addition of a user specified buffer to be added
97 * to the database array, in effect "pushing" the buffer on top of the
98 * virtual database. 0 is returned on success, -1 on failure.
111 topreclen
= strlen(ent
);
112 if ((toprec
= malloc (topreclen
+ 1)) == NULL
) {
117 (void)strcpy(toprec
, ent
);
122 * Cgetcap searches the capability record buf for the capability cap with
123 * type `type'. A pointer to the value of cap is returned on success, NULL
124 * if the requested capability couldn't be found.
126 * Specifying a type of ':' means that nothing should follow cap (:cap:).
127 * In this case a pointer to the terminating ':' or NUL will be returned if
130 * If (cap, '@') or (cap, terminator, '@') is found before (cap, terminator)
134 cgetcap(buf
, cap
, type
)
138 register char *bp
, *cp
;
143 * Skip past the current capability field - it's either the
144 * name field if this is the first time through the loop, or
145 * the remainder of a field whose name failed to match cap.
155 * Try to match (cap, type) in buf.
157 for (cp
= cap
; *cp
== *bp
&& *bp
!= '\0'; cp
++, bp
++)
164 if (*bp
!= '\0' && *bp
!= ':')
171 return (*bp
== '@' ? NULL
: bp
);
177 * Cgetent extracts the capability record name from the NULL terminated file
178 * array db_array and returns a pointer to a malloc'd copy of it in buf.
179 * Buf must be retained through all subsequent calls to cgetcap, cgetnum,
180 * cgetflag, and cgetstr, but may then be free'd. 0 is returned on success,
181 * -1 if the requested record couldn't be found, -2 if a system error was
182 * encountered (couldn't open/read a file, etc.), and -3 if a potential
183 * reference loop is detected.
186 cgetent(buf
, db_array
, name
)
187 char **buf
, **db_array
, *name
;
191 return (getent(buf
, &dummy
, db_array
, -1, name
, 0, NULL
));
195 * Getent implements the functions of cgetent. If fd is non-negative,
196 * *db_array has already been opened and fd is the open file descriptor. We
197 * do this to save time and avoid using up file descriptors for tc=
200 * Getent returns the same success/failure codes as cgetent. On success, a
201 * pointer to a malloc'ed capability record with all tc= capabilities fully
202 * expanded and its length (not including trailing ASCII NUL) are left in
206 * + Allocate memory incrementally as needed in chunks of size BFRAG
207 * for capability buffer.
208 * + Recurse for each tc=name and interpolate result. Stop when all
209 * names interpolated, a name can't be found, or depth exceeds
213 getent(cap
, len
, db_array
, fd
, name
, depth
, nfield
)
214 char **cap
, **db_array
, *name
, *nfield
;
219 register char *r_end
, *rp
, **db_p
;
220 int myfd
, eof
, foundit
, retval
, clen
;
223 char pbuf
[_POSIX_PATH_MAX
];
226 * Return with ``loop detected'' error if we've recursed more than
227 * MAX_RECURSION times.
229 if (depth
> MAX_RECURSION
)
233 * Check if we have a top record from cgetset().
235 if (depth
== 0 && toprec
!= NULL
&& cgetmatch(toprec
, name
) == 0) {
236 if ((record
= malloc (topreclen
+ BFRAG
)) == NULL
) {
240 (void)strcpy(record
, toprec
);
243 rp
= record
+ topreclen
+ 1;
248 * Allocate first chunk of memory.
250 if ((record
= malloc(BFRAG
)) == NULL
) {
254 r_end
= record
+ BFRAG
;
257 * Loop through database array until finding the record.
260 for (db_p
= db_array
; *db_p
!= NULL
; db_p
++) {
264 * Open database if not already open.
268 (void)lseek(fd
, (off_t
)0, SEEK_SET
);
271 (void)snprintf(pbuf
, sizeof(pbuf
), "%s.db", *db_p
);
272 if ((capdbp
= dbopen(pbuf
, O_RDONLY
, 0, DB_HASH
, 0))
275 retval
= cdbget(capdbp
, &record
, name
);
277 /* no record available */
278 (void)capdbp
->close(capdbp
);
281 /* save the data; close frees it */
282 clen
= strlen(record
);
283 cbuf
= malloc(clen
+ 1);
284 memcpy(cbuf
, record
, clen
+ 1);
285 if (capdbp
->close(capdbp
) < 0) {
293 fd
= open(*db_p
, O_RDONLY
, 0);
295 /* No error on unfound file. */
302 * Find the requested capability record ...
306 register char *b_end
, *bp
;
311 * There is always room for one more character in record.
312 * R_end always points just past end of record.
313 * Rp always points just past last character in record.
314 * B_end always points just past last character in buf.
315 * Bp always points at next character in buf.
322 * Read in a line implementing (\, newline)
330 n
= read(fd
, buf
, sizeof(buf
));
349 if (rp
> record
&& *(rp
-1) == '\\') {
358 * Enforce loop invariant: if no room
359 * left in record buffer, try to get
367 newsize
= r_end
- record
+ BFRAG
;
368 record
= realloc(record
, newsize
);
369 if (record
== NULL
) {
375 r_end
= record
+ newsize
;
379 /* loop invariant let's us do this */
383 * If encountered eof check next file.
389 * Toss blank lines and comments.
391 if (*record
== '\0' || *record
== '#')
395 * See if this is the record we want ...
397 if (cgetmatch(record
, name
) == 0) {
398 if (nfield
== NULL
|| !nfcmp(nfield
, record
)) {
400 break; /* found it! */
415 * Got the capability record, but now we have to expand all tc=name
416 * references in it ...
419 register char *newicap
, *s
;
420 register int newilen
;
422 int diff
, iret
, tclen
;
423 char *icap
, *scan
, *tc
, *tcstart
, *tcend
;
427 * There is room for one more character in record.
428 * R_end points just past end of record.
429 * Rp points just past last character in record.
430 * Scan points at remainder of record that needs to be
431 * scanned for tc=name constructs.
436 if ((tc
= cgetcap(scan
, "tc", '=')) == NULL
)
440 * Find end of tc=name and stomp on the trailing `:'
441 * (if present) so we can use it to call ourselves.
456 iret
= getent(&icap
, &ilen
, db_p
, fd
, tc
, depth
+1,
458 newicap
= icap
; /* Put into a register. */
470 /* couldn't resolve tc */
479 /* not interested in name field of tc'ed record */
487 newilen
-= s
- newicap
;
490 /* make sure interpolated record is `:'-terminated */
493 *s
= ':'; /* overwrite NUL with : */
498 * Make sure there's enough room to insert the
501 diff
= newilen
- tclen
;
502 if (diff
>= r_end
- rp
) {
503 u_int pos
, tcpos
, tcposend
;
507 newsize
= r_end
- record
+ diff
+ BFRAG
;
508 tcpos
= tcstart
- record
;
509 tcposend
= tcend
- record
;
510 record
= realloc(record
, newsize
);
511 if (record
== NULL
) {
518 r_end
= record
+ newsize
;
520 tcstart
= record
+ tcpos
;
521 tcend
= record
+ tcposend
;
525 * Insert tc'ed record into our record.
527 s
= tcstart
+ newilen
;
528 bcopy(tcend
, s
, rp
- tcend
);
529 bcopy(newicap
, tcstart
, newilen
);
534 * Start scan on `:' so next cgetcap works properly
535 * (cgetcap always skips first field).
542 * Close file (if we opened it), give back any extra memory, and
543 * return capability, length and success.
547 *len
= rp
- record
- 1; /* don't count NUL */
550 realloc(record
, (size_t)(rp
- record
))) == NULL
) {
562 cdbget(capdbp
, bp
, name
)
569 key
.size
= strlen(name
);
572 /* Get the reference. */
573 switch(capdbp
->get(capdbp
, &key
, &data
, 0)) {
580 /* If not an index to another record, leave. */
581 if (((char *)data
.data
)[0] != SHADOW
)
584 key
.data
= (char *)data
.data
+ 1;
585 key
.size
= data
.size
- 1;
588 *bp
= (char *)data
.data
+ 1;
589 return (((char *)(data
.data
))[0] == TCERR
? 1 : 0);
593 * Cgetmatch will return 0 if name is one of the names of the capability
594 * record buf, -1 if not.
600 register char *np
, *bp
;
603 * Start search at beginning of record.
608 * Try to match a record name.
613 if (*bp
== '|' || *bp
== ':' || *bp
== '\0')
622 * Match failed, skip to next name in record.
624 bp
--; /* a '|' or ':' may have stopped the match */
626 if (*bp
== '\0' || *bp
== ':')
627 return (-1); /* match failed totally */
630 break; /* found next name */
639 cgetfirst(buf
, db_array
)
640 char **buf
, **db_array
;
643 return (cgetnext(buf
, db_array
));
664 * Cgetnext() gets either the first or next entry in the logical database
665 * specified by db_array. It returns 0 upon completion of the database, 1
666 * upon returning an entry with more remaining, and -1 if an error occurs.
669 cgetnext(bp
, db_array
)
675 char *cp
, *line
, *rp
, *np
, buf
[BSIZE
], nbuf
[BSIZE
];
681 if (pfp
== NULL
&& (pfp
= fopen(*dbp
, "r")) == NULL
) {
686 if (toprec
&& !gottoprec
) {
690 line
= fgetln(pfp
, &len
);
691 if (line
== NULL
&& pfp
) {
697 if (*++dbp
== NULL
) {
701 fopen(*dbp
, "r")) == NULL
) {
708 line
[len
- 1] = '\0';
713 if (isspace(*line
) ||
714 *line
== ':' || *line
== '#' || slash
) {
715 if (line
[len
- 2] == '\\')
721 if (line
[len
- 2] == '\\')
729 * Line points to a name line.
734 for (cp
= line
; *cp
!= '\0'; cp
++) {
747 } else { /* name field extends beyond the line */
748 line
= fgetln(pfp
, &len
);
749 if (line
== NULL
&& pfp
) {
756 line
[len
- 1] = '\0';
760 for(cp
= nbuf
; *cp
!= NULL
; cp
++)
761 if (*cp
== '|' || *cp
== ':')
769 * Last argument of getent here should be nbuf if we want true
770 * sequential access in the case of duplicates.
771 * With NULL, getent will return the first entry found
772 * rather than the duplicate entry record. This is a
773 * matter of semantics that should be resolved.
775 status
= getent(bp
, &dummy
, db_array
, -1, buf
, 0, NULL
);
776 if (status
== -2 || status
== -3)
785 * Cgetstr retrieves the value of the string capability cap from the
786 * capability record pointed to by buf. A pointer to a decoded, NUL
787 * terminated, malloc'd copy of the string is returned in the char *
788 * pointed to by str. The length of the string not including the trailing
789 * NUL is returned on success, -1 if the requested string capability
790 * couldn't be found, -2 if a system error was encountered (storage
791 * allocation failure).
794 cgetstr(buf
, cap
, str
)
798 register u_int m_room
;
799 register char *bp
, *mp
;
804 * Find string capability cap
806 bp
= cgetcap(buf
, cap
, '=');
811 * Conversion / storage allocation loop ... Allocate memory in
812 * chunks SFRAG in size.
814 if ((mem
= malloc(SFRAG
)) == NULL
) {
816 return (-2); /* couldn't even allocate the first fragment */
821 while (*bp
!= ':' && *bp
!= '\0') {
824 * There is always room for one more character in mem.
825 * Mp always points just past last character in mem.
826 * Bp always points at next character in buf.
830 if (*bp
== ':' || *bp
== '\0')
831 break; /* drop unfinished escape */
833 } else if (*bp
== '\\') {
835 if (*bp
== ':' || *bp
== '\0')
836 break; /* drop unfinished escape */
837 if ('0' <= *bp
&& *bp
<= '7') {
841 i
= 3; /* maximum of three octal digits */
843 n
= n
* 8 + (*bp
++ - '0');
844 } while (--i
&& '0' <= *bp
&& *bp
<= '7');
847 else switch (*bp
++) {
871 * Catches '\', '^', and
882 * Enforce loop invariant: if no room left in current
883 * buffer, try to get some more.
886 size_t size
= mp
- mem
;
888 if ((mem
= realloc(mem
, size
+ SFRAG
)) == NULL
)
894 *mp
++ = '\0'; /* loop invariant let's us do this */
899 * Give back any extra memory and return value and success.
902 if ((mem
= realloc(mem
, (size_t)(mp
- mem
))) == NULL
)
909 * Cgetustr retrieves the value of the string capability cap from the
910 * capability record pointed to by buf. The difference between cgetustr()
911 * and cgetstr() is that cgetustr does not decode escapes but rather treats
912 * all characters literally. A pointer to a NUL terminated malloc'd
913 * copy of the string is returned in the char pointed to by str. The
914 * length of the string not including the trailing NUL is returned on success,
915 * -1 if the requested string capability couldn't be found, -2 if a system
916 * error was encountered (storage allocation failure).
919 cgetustr(buf
, cap
, str
)
920 char *buf
, *cap
, **str
;
922 register u_int m_room
;
923 register char *bp
, *mp
;
928 * Find string capability cap
930 if ((bp
= cgetcap(buf
, cap
, '=')) == NULL
)
934 * Conversion / storage allocation loop ... Allocate memory in
935 * chunks SFRAG in size.
937 if ((mem
= malloc(SFRAG
)) == NULL
) {
939 return (-2); /* couldn't even allocate the first fragment */
944 while (*bp
!= ':' && *bp
!= '\0') {
947 * There is always room for one more character in mem.
948 * Mp always points just past last character in mem.
949 * Bp always points at next character in buf.
955 * Enforce loop invariant: if no room left in current
956 * buffer, try to get some more.
959 size_t size
= mp
- mem
;
961 if ((mem
= realloc(mem
, size
+ SFRAG
)) == NULL
)
967 *mp
++ = '\0'; /* loop invariant let's us do this */
972 * Give back any extra memory and return value and success.
975 if ((mem
= realloc(mem
, (size_t)(mp
- mem
))) == NULL
)
982 * Cgetnum retrieves the value of the numeric capability cap from the
983 * capability record pointed to by buf. The numeric value is returned in
984 * the long pointed to by num. 0 is returned on success, -1 if the requested
985 * numeric capability couldn't be found.
988 cgetnum(buf
, cap
, num
)
993 register int base
, digit
;
997 * Find numeric capability cap
999 bp
= cgetcap(buf
, cap
, '#');
1004 * Look at value and determine numeric base:
1005 * 0x... or 0X... hexadecimal,
1011 if (*bp
== 'x' || *bp
== 'X') {
1020 * Conversion loop ...
1024 if ('0' <= *bp
&& *bp
<= '9')
1026 else if ('a' <= *bp
&& *bp
<= 'f')
1027 digit
= 10 + *bp
- 'a';
1028 else if ('A' <= *bp
&& *bp
<= 'F')
1029 digit
= 10 + *bp
- 'A';
1036 n
= n
* base
+ digit
;
1041 * Return value and success.
1049 * Compare name field of record.
1058 for (cp
= rec
; *cp
!= ':'; cp
++)
1063 ret
= strcmp(nf
, rec
);