* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
* This code is derived from software contributed to Berkeley by
* Casey Leedom of Lawrence Livermore National Laboratory.
* %sccs.include.redist.c%
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid
[] = "@(#)getcap.c 8.1 (Berkeley) %G%";
#endif /* LIBC_SCCS and not lint */
#define ESC ('[' & 037) /* ASCII ESC */
#define MAX_RECURSION 32 /* maximum getent recursion */
#define SFRAG 100 /* cgetstr mallocs in SFRAG chunks */
static size_t topreclen
; /* toprec length */
static char *toprec
; /* Additional record specified by cgetset() */
static int gottoprec
; /* Flag indicating retrieval of toprecord */
static int cdbget
__P((DB
*, char **, char *));
static int getent
__P((char **, u_int
*, char **, int, char *, int, char *));
static int nfcmp
__P((char *, char *));
* Cgetset() allows the addition of a user specified buffer to be added
* to the database array, in effect "pushing" the buffer on top of the
* virtual database. 0 is returned on success, -1 on failure.
if ((toprec
= malloc (topreclen
+ 1)) == NULL
) {
(void)strcpy(toprec
, ent
);
* Cgetcap searches the capability record buf for the capability cap with
* type `type'. A pointer to the value of cap is returned on success, NULL
* if the requested capability couldn't be found.
* Specifying a type of ':' means that nothing should follow cap (:cap:).
* In this case a pointer to the terminating ':' or NUL will be returned if
* If (cap, '@') or (cap, terminator, '@') is found before (cap, terminator)
* Skip past the current capability field - it's either the
* name field if this is the first time through the loop, or
* the remainder of a field whose name failed to match cap.
* Try to match (cap, type) in buf.
for (cp
= cap
; *cp
== *bp
&& *bp
!= '\0'; cp
++, bp
++)
if (*bp
!= '\0' && *bp
!= ':')
return (*bp
== '@' ? NULL
: bp
);
* Cgetent extracts the capability record name from the NULL terminated file
* array db_array and returns a pointer to a malloc'd copy of it in buf.
* Buf must be retained through all subsequent calls to cgetcap, cgetnum,
* cgetflag, and cgetstr, but may then be free'd. 0 is returned on success,
* -1 if the requested record couldn't be found, -2 if a system error was
* encountered (couldn't open/read a file, etc.), and -3 if a potential
* reference loop is detected.
cgetent(buf
, db_array
, name
)
char **buf
, **db_array
, *name
;
return (getent(buf
, &dummy
, db_array
, -1, name
, 0, NULL
));
* Getent implements the functions of cgetent. If fd is non-negative,
* *db_array has already been opened and fd is the open file descriptor. We
* do this to save time and avoid using up file descriptors for tc=
* Getent returns the same success/failure codes as cgetent. On success, a
* pointer to a malloc'ed capability record with all tc= capabilities fully
* expanded and its length (not including trailing ASCII NUL) are left in
* + Allocate memory incrementally as needed in chunks of size BFRAG
* + Recurse for each tc=name and interpolate result. Stop when all
* names interpolated, a name can't be found, or depth exceeds
getent(cap
, len
, db_array
, fd
, name
, depth
, nfield
)
char **cap
, **db_array
, *name
, *nfield
;
register char *r_end
, *rp
, **db_p
;
int myfd
, eof
, foundit
, retval
;
char pbuf
[_POSIX_PATH_MAX
];
* Return with ``loop detected'' error if we've recursed more than
if (depth
> MAX_RECURSION
)
* Check if we have a top record from cgetset().
if (depth
== 0 && toprec
!= NULL
&& cgetmatch(toprec
, name
) == 0) {
if ((record
= malloc (topreclen
+ BFRAG
)) == NULL
) {
(void)strcpy(record
, toprec
);
rp
= record
+ topreclen
+ 1;
* Allocate first chunk of memory.
if ((record
= malloc(BFRAG
)) == NULL
) {
* Loop through database array until finding the record.
for (db_p
= db_array
; *db_p
!= NULL
; db_p
++) {
* Open database if not already open.
(void)lseek(fd
, (off_t
)0, L_SET
);
(void)snprintf(pbuf
, sizeof(pbuf
), "%s.db", *db_p
);
if ((capdbp
= dbopen(pbuf
, O_RDONLY
, 0, DB_HASH
, 0))
retval
= cdbget(capdbp
, &record
, name
);
if (capdbp
->close(capdbp
) < 0)
memmove(*cap
, record
, *len
+ 1);
fd
= open(*db_p
, O_RDONLY
, 0);
/* No error on unfound file. */
* Find the requested capability record ...
register char *b_end
, *bp
;
* There is always room for one more character in record.
* R_end always points just past end of record.
* Rp always points just past last character in record.
* B_end always points just past last character in buf.
* Bp always points at next character in buf.
* Read in a line implementing (\, newline)
n
= read(fd
, buf
, sizeof(buf
));
if (rp
> record
&& *(rp
-1) == '\\') {
* Enforce loop invariant: if no room
* left in record buffer, try to get
newsize
= r_end
- record
+ BFRAG
;
record
= realloc(record
, newsize
);
r_end
= record
+ newsize
;
/* loop invariant let's us do this */
* If encountered eof check next file.
* Toss blank lines and comments.
if (*record
== '\0' || *record
== '#')
* See if this is the record we want ...
if (cgetmatch(record
, name
) == 0) {
if (nfield
== NULL
|| !nfcmp(nfield
, record
)) {
* Got the capability record, but now we have to expand all tc=name
register char *newicap
, *s
;
char *icap
, *scan
, *tc
, *tcstart
, *tcend
;
* There is room for one more character in record.
* R_end points just past end of record.
* Rp points just past last character in record.
* Scan points at remainder of record that needs to be
* scanned for tc=name constructs.
if ((tc
= cgetcap(scan
, "tc", '=')) == NULL
)
* Find end of tc=name and stomp on the trailing `:'
* (if present) so we can use it to call ourselves.
iret
= getent(&icap
, &ilen
, db_p
, fd
, tc
, depth
+1,
newicap
= icap
; /* Put into a register. */
/* couldn't resolve tc */
/* not interested in name field of tc'ed record */
/* make sure interpolated record is `:'-terminated */
*s
= ':'; /* overwrite NUL with : */
* Make sure there's enough room to insert the
if (diff
>= r_end
- rp
) {
u_int pos
, tcpos
, tcposend
;
newsize
= r_end
- record
+ diff
+ BFRAG
;
tcpos
= tcstart
- record
;
tcposend
= tcend
- record
;
record
= realloc(record
, newsize
);
r_end
= record
+ newsize
;
tcstart
= record
+ tcpos
;
tcend
= record
+ tcposend
;
* Insert tc'ed record into our record.
bcopy(tcend
, s
, rp
- tcend
);
bcopy(newicap
, tcstart
, newilen
);
* Start scan on `:' so next cgetcap works properly
* (cgetcap always skips first field).
* Close file (if we opened it), give back any extra memory, and
* return capability, length and success.
*len
= rp
- record
- 1; /* don't count NUL */
realloc(record
, (size_t)(rp
- record
))) == NULL
) {
switch(capdbp
->get(capdbp
, &key
, &data
, 0)) {
/* If not an index to another record, leave. */
if (((char *)data
.data
)[0] != SHADOW
)
key
.data
= (char *)data
.data
+ 1;
key
.size
= data
.size
- 1;
*bp
= (char *)data
.data
+ 1;
return (((char *)(data
.data
))[0] == TCERR
? 1 : 0);
* Cgetmatch will return 0 if name is one of the names of the capability
* Start search at beginning of record.
* Try to match a record name.
if (*bp
== '|' || *bp
== ':' || *bp
== '\0')
* Match failed, skip to next name in record.
bp
--; /* a '|' or ':' may have stopped the match */
if (*bp
== '\0' || *bp
== ':')
return (-1); /* match failed totally */
break; /* found next name */
return (cgetnext(buf
, db_array
));
* Cgetnext() gets either the first or next entry in the logical database
* specified by db_array. It returns 0 upon completion of the database, 1
* upon returning an entry with more remaining, and -1 if an error occurs.
char *cp
, *line
, *rp
, *np
, buf
[BSIZE
], nbuf
[BSIZE
];
if (pfp
== NULL
&& (pfp
= fopen(*dbp
, "r")) == NULL
) {
if (toprec
&& !gottoprec
) {
line
= fgetline(pfp
, &len
);
if (line
== NULL
&& pfp
) {
fopen(*dbp
, "r")) == NULL
) {
*line
== ':' || *line
== '#' || slash
) {
if (line
[len
- 2] == '\\')
if (line
[len
- 2] == '\\')
* Line points to a name line.
for (cp
= line
; *cp
!= '\0'; cp
++) {
} else { /* name field extends beyond the line */
line
= fgetline(pfp
, &len
);
if (line
== NULL
&& pfp
) {
for(cp
= nbuf
; *cp
!= NULL
; cp
++)
if (*cp
== '|' || *cp
== ':')
* Last argument of getent here should be nbuf if we want true
* sequential access in the case of duplicates.
* With NULL, getent will return the first entry found
* rather than the duplicate entry record. This is a
* matter of semantics that should be resolved.
status
= getent(bp
, &dummy
, db_array
, -1, buf
, 0, NULL
);
if (status
== -2 || status
== -3)
* Cgetstr retrieves the value of the string capability cap from the
* capability record pointed to by buf. A pointer to a decoded, NUL
* terminated, malloc'd copy of the string is returned in the char *
* pointed to by str. The length of the string not including the trailing
* NUL is returned on success, -1 if the requested string capability
* couldn't be found, -2 if a system error was encountered (storage
* Find string capability cap
bp
= cgetcap(buf
, cap
, '=');
* Conversion / storage allocation loop ... Allocate memory in
if ((mem
= malloc(SFRAG
)) == NULL
) {
return (-2); /* couldn't even allocate the first fragment */
while (*bp
!= ':' && *bp
!= '\0') {
* There is always room for one more character in mem.
* Mp always points just past last character in mem.
* Bp always points at next character in buf.
if (*bp
== ':' || *bp
== '\0')
break; /* drop unfinished escape */
} else if (*bp
== '\\') {
if (*bp
== ':' || *bp
== '\0')
break; /* drop unfinished escape */
if ('0' <= *bp
&& *bp
<= '7') {
i
= 3; /* maximum of three octal digits */
n
= n
* 8 + (*bp
++ - '0');
} while (--i
&& '0' <= *bp
&& *bp
<= '7');
* Enforce loop invariant: if no room left in current
* buffer, try to get some more.
if ((mem
= realloc(mem
, size
+ SFRAG
)) == NULL
)
*mp
++ = '\0'; /* loop invariant let's us do this */
* Give back any extra memory and return value and success.
if ((mem
= realloc(mem
, (size_t)(mp
- mem
))) == NULL
)
* Cgetustr retrieves the value of the string capability cap from the
* capability record pointed to by buf. The difference between cgetustr()
* and cgetstr() is that cgetustr does not decode escapes but rather treats
* all characters literally. A pointer to a NUL terminated malloc'd
* copy of the string is returned in the char pointed to by str. The
* length of the string not including the trailing NUL is returned on success,
* -1 if the requested string capability couldn't be found, -2 if a system
* error was encountered (storage allocation failure).
* Find string capability cap
if ((bp
= cgetcap(buf
, cap
, '=')) == NULL
)
* Conversion / storage allocation loop ... Allocate memory in
if ((mem
= malloc(SFRAG
)) == NULL
) {
return (-2); /* couldn't even allocate the first fragment */
while (*bp
!= ':' && *bp
!= '\0') {
* There is always room for one more character in mem.
* Mp always points just past last character in mem.
* Bp always points at next character in buf.
* Enforce loop invariant: if no room left in current
* buffer, try to get some more.
if ((mem
= realloc(mem
, size
+ SFRAG
)) == NULL
)
*mp
++ = '\0'; /* loop invariant let's us do this */
* Give back any extra memory and return value and success.
if ((mem
= realloc(mem
, (size_t)(mp
- mem
))) == NULL
)
* Cgetnum retrieves the value of the numeric capability cap from the
* capability record pointed to by buf. The numeric value is returned in
* the long pointed to by num. 0 is returned on success, -1 if the requested
* numeric capability couldn't be found.
register int base
, digit
;
* Find numeric capability cap
bp
= cgetcap(buf
, cap
, '#');
* Look at value and determine numeric base:
* 0x... or 0X... hexadecimal,
if (*bp
== 'x' || *bp
== 'X') {
if ('0' <= *bp
&& *bp
<= '9')
else if ('a' <= *bp
&& *bp
<= 'f')
else if ('A' <= *bp
&& *bp
<= 'F')
* Return value and success.
* Compare name field of record.
for (cp
= rec
; *cp
!= ':'; cp
++)