10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*2830Sdjl * Common Development and Distribution License (the "License").
6*2830Sdjl * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*2830Sdjl * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23*2830Sdjl * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <ctype.h>
290Sstevel@tonic-gate #include <netdb.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <nss_dbdefs.h>
330Sstevel@tonic-gate
34*2830Sdjl int str2protoent(const char *, int, void *,
350Sstevel@tonic-gate char *, int);
360Sstevel@tonic-gate
370Sstevel@tonic-gate static int proto_stayopen;
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate * Unsynchronized, but it affects only
400Sstevel@tonic-gate * efficiency, not correctness
410Sstevel@tonic-gate */
420Sstevel@tonic-gate
430Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
440Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
450Sstevel@tonic-gate
46*2830Sdjl void
_nss_initf_proto(nss_db_params_t * p)470Sstevel@tonic-gate _nss_initf_proto(nss_db_params_t *p)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate p->name = NSS_DBNAM_PROTOCOLS;
500Sstevel@tonic-gate p->default_config = NSS_DEFCONF_PROTOCOLS;
510Sstevel@tonic-gate }
520Sstevel@tonic-gate
530Sstevel@tonic-gate struct protoent *
getprotobyname_r(const char * name,struct protoent * result,char * buffer,int buflen)540Sstevel@tonic-gate getprotobyname_r(const char *name, struct protoent *result,
550Sstevel@tonic-gate char *buffer, int buflen)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate nss_XbyY_args_t arg;
580Sstevel@tonic-gate nss_status_t res;
590Sstevel@tonic-gate
60*2830Sdjl if (name == (const char *)NULL) {
61*2830Sdjl errno = ERANGE;
62*2830Sdjl return (NULL);
63*2830Sdjl }
640Sstevel@tonic-gate NSS_XbyY_INIT(&arg, result, buffer, buflen, str2protoent);
650Sstevel@tonic-gate arg.key.name = name;
660Sstevel@tonic-gate arg.stayopen = proto_stayopen;
670Sstevel@tonic-gate res = nss_search(&db_root, _nss_initf_proto,
680Sstevel@tonic-gate NSS_DBOP_PROTOCOLS_BYNAME, &arg);
690Sstevel@tonic-gate arg.status = res;
700Sstevel@tonic-gate (void) NSS_XbyY_FINI(&arg);
710Sstevel@tonic-gate return ((struct protoent *)arg.returnval);
720Sstevel@tonic-gate }
730Sstevel@tonic-gate
740Sstevel@tonic-gate struct protoent *
getprotobynumber_r(int proto,struct protoent * result,char * buffer,int buflen)750Sstevel@tonic-gate getprotobynumber_r(int proto, struct protoent *result, char *buffer, int buflen)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate nss_XbyY_args_t arg;
780Sstevel@tonic-gate nss_status_t res;
790Sstevel@tonic-gate
800Sstevel@tonic-gate NSS_XbyY_INIT(&arg, result, buffer, buflen, str2protoent);
810Sstevel@tonic-gate arg.key.number = proto;
820Sstevel@tonic-gate arg.stayopen = proto_stayopen;
830Sstevel@tonic-gate res = nss_search(&db_root, _nss_initf_proto,
840Sstevel@tonic-gate NSS_DBOP_PROTOCOLS_BYNUMBER, &arg);
850Sstevel@tonic-gate arg.status = res;
860Sstevel@tonic-gate (void) NSS_XbyY_FINI(&arg);
870Sstevel@tonic-gate return ((struct protoent *)arg.returnval);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate
900Sstevel@tonic-gate int
setprotoent(int stay)910Sstevel@tonic-gate setprotoent(int stay)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate proto_stayopen = stay;
940Sstevel@tonic-gate nss_setent(&db_root, _nss_initf_proto, &context);
950Sstevel@tonic-gate return (0);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
980Sstevel@tonic-gate int
endprotoent()990Sstevel@tonic-gate endprotoent()
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate proto_stayopen = 0;
1020Sstevel@tonic-gate nss_endent(&db_root, _nss_initf_proto, &context);
1030Sstevel@tonic-gate nss_delete(&db_root);
1040Sstevel@tonic-gate return (0);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate struct protoent *
getprotoent_r(struct protoent * result,char * buffer,int buflen)1080Sstevel@tonic-gate getprotoent_r(struct protoent *result, char *buffer, int buflen)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate nss_XbyY_args_t arg;
1110Sstevel@tonic-gate nss_status_t res;
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate NSS_XbyY_INIT(&arg, result, buffer, buflen, str2protoent);
1140Sstevel@tonic-gate /* No stayopen flag; of course you stay open for iteration */
1150Sstevel@tonic-gate res = nss_getent(&db_root, _nss_initf_proto, &context, &arg);
1160Sstevel@tonic-gate arg.status = res;
1170Sstevel@tonic-gate (void) NSS_XbyY_FINI(&arg);
1180Sstevel@tonic-gate return ((struct protoent *)arg.returnval);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ...
1230Sstevel@tonic-gate * The structure pointer passed in is a structure in the caller's space
1240Sstevel@tonic-gate * wherein the field pointers would be set to areas in the buffer if
1250Sstevel@tonic-gate * need be. instring and buffer should be separate areas. Let's not
1260Sstevel@tonic-gate * fight over crumbs.
1270Sstevel@tonic-gate */
128*2830Sdjl int
str2protoent(const char * instr,int lenstr,void * ent,char * buffer,int buflen)1290Sstevel@tonic-gate str2protoent(const char *instr, int lenstr,
1300Sstevel@tonic-gate void *ent /* it is really (struct protoent *) */,
1310Sstevel@tonic-gate char *buffer, int buflen)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate struct protoent *proto = (struct protoent *)ent;
1340Sstevel@tonic-gate const char *p, *numstart, *namestart, *limit;
1350Sstevel@tonic-gate int numlen, namelen = 0;
1360Sstevel@tonic-gate char numbuf[16];
1370Sstevel@tonic-gate char *numend;
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate if ((instr >= buffer && (buffer + buflen) > instr) ||
1400Sstevel@tonic-gate (buffer >= instr && (instr + lenstr) > buffer)) {
1410Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate p = instr;
1450Sstevel@tonic-gate limit = p + lenstr;
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate while (p < limit && isspace(*p)) {
1480Sstevel@tonic-gate p++;
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate namestart = p;
1510Sstevel@tonic-gate while (p < limit && !isspace(*p)) {
1520Sstevel@tonic-gate p++; /* Skip over the canonical name */
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate namelen = (int)(p - namestart);
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate if (buflen <= namelen) { /* not enough buffer */
1570Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate (void) memcpy(buffer, namestart, namelen);
1600Sstevel@tonic-gate buffer[namelen] = '\0';
1610Sstevel@tonic-gate proto->p_name = buffer;
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate while (p < limit && isspace(*p)) {
1640Sstevel@tonic-gate p++;
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate if (p >= limit) {
1670Sstevel@tonic-gate /* Syntax error -- no proto number */
1680Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate numstart = p;
1710Sstevel@tonic-gate do {
1720Sstevel@tonic-gate p++; /* Find the end of the proto number */
1730Sstevel@tonic-gate } while (p < limit && !isspace(*p));
1740Sstevel@tonic-gate numlen = (int)(p - numstart);
1750Sstevel@tonic-gate if (numlen >= (int)sizeof (numbuf)) {
1760Sstevel@tonic-gate /* Syntax error -- supposed number is too long */
1770Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate (void) memcpy(numbuf, numstart, (size_t)numlen);
1800Sstevel@tonic-gate numbuf[numlen] = '\0';
1810Sstevel@tonic-gate proto->p_proto = (int)strtol(numbuf, &numend, 10);
1820Sstevel@tonic-gate if (*numend != '\0') {
1830Sstevel@tonic-gate /* Syntax error -- protocol number isn't a number */
1840Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate while (p < limit && isspace(*p)) {
1880Sstevel@tonic-gate p++;
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate * Although nss_files_XY_all calls us with # stripped,
1920Sstevel@tonic-gate * we should be able to deal with it here in order to
1930Sstevel@tonic-gate * be more useful.
1940Sstevel@tonic-gate */
1950Sstevel@tonic-gate if (p >= limit || *p == '#') { /* no aliases, no problem */
1960Sstevel@tonic-gate char **ptr;
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate ptr = (char **)ROUND_UP(buffer + namelen + 1,
1990Sstevel@tonic-gate sizeof (char *));
2000Sstevel@tonic-gate if ((char *)ptr >= buffer + buflen) {
2010Sstevel@tonic-gate /* hope they don't try to peek in */
2020Sstevel@tonic-gate proto->p_aliases = 0;
2030Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE);
2040Sstevel@tonic-gate } else {
2050Sstevel@tonic-gate *ptr = 0;
2060Sstevel@tonic-gate proto->p_aliases = ptr;
2070Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate proto->p_aliases = _nss_netdb_aliases(p, lenstr - (int)(p - instr),
2110Sstevel@tonic-gate buffer + namelen + 1, buflen - namelen - 1);
2120Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS);
2130Sstevel@tonic-gate }
214