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 *
25*2830Sdjl * files/getservent.c -- "files" backend for nsswitch "services" database
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <netdb.h>
310Sstevel@tonic-gate #include "files_common.h"
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <netinet/in.h>
34*2830Sdjl #include <inttypes.h>
350Sstevel@tonic-gate #include <strings.h>
36*2830Sdjl #include <ctype.h>
37*2830Sdjl #include <stdlib.h>
380Sstevel@tonic-gate
390Sstevel@tonic-gate static int
check_name(nss_XbyY_args_t * argp,const char * line,int linelen)40*2830Sdjl check_name(nss_XbyY_args_t *argp, const char *line, int linelen)
410Sstevel@tonic-gate {
42*2830Sdjl const char *limit, *linep, *keyp;
43*2830Sdjl int name_match = 0;
44*2830Sdjl
45*2830Sdjl linep = line;
46*2830Sdjl limit = line + linelen;
47*2830Sdjl keyp = argp->key.serv.serv.name;
48*2830Sdjl
49*2830Sdjl /* compare name */
50*2830Sdjl while (*keyp && linep < limit && !isspace(*linep) && *keyp == *linep) {
51*2830Sdjl keyp++;
52*2830Sdjl linep++;
53*2830Sdjl }
54*2830Sdjl if (*keyp == '\0' && linep < limit && isspace(*linep)) {
55*2830Sdjl if (argp->key.serv.proto == NULL)
56*2830Sdjl return (1);
57*2830Sdjl else
58*2830Sdjl name_match = 1;
59*2830Sdjl }
60*2830Sdjl
61*2830Sdjl /* skip remainder of the name, if any */
62*2830Sdjl while (linep < limit && !isspace(*linep))
63*2830Sdjl linep++;
64*2830Sdjl /* skip the delimiting spaces */
65*2830Sdjl while (linep < limit && isspace(*linep))
66*2830Sdjl linep++;
67*2830Sdjl /* skip port number */
68*2830Sdjl while (linep < limit && !isspace(*linep) && *linep != '/')
69*2830Sdjl linep++;
70*2830Sdjl if (linep == limit || *linep != '/')
71*2830Sdjl return (0);
720Sstevel@tonic-gate
73*2830Sdjl linep++;
74*2830Sdjl if ((keyp = argp->key.serv.proto) == NULL) {
75*2830Sdjl /* skip protocol */
76*2830Sdjl while (linep < limit && !isspace(*linep))
77*2830Sdjl linep++;
78*2830Sdjl } else {
79*2830Sdjl /* compare protocol */
80*2830Sdjl while (*keyp && linep < limit && !isspace(*linep) &&
81*2830Sdjl *keyp == *linep) {
82*2830Sdjl keyp++;
83*2830Sdjl linep++;
84*2830Sdjl }
85*2830Sdjl /* no protocol match */
86*2830Sdjl if (*keyp || (linep < limit && !isspace(*linep)))
87*2830Sdjl return (0);
88*2830Sdjl /* protocol and name match, return */
89*2830Sdjl if (name_match)
90*2830Sdjl return (1);
91*2830Sdjl /* protocol match but name yet to be matched, so continue */
920Sstevel@tonic-gate }
93*2830Sdjl
94*2830Sdjl /* compare with the aliases */
95*2830Sdjl while (linep < limit) {
96*2830Sdjl /* skip the delimiting spaces */
97*2830Sdjl while (linep < limit && isspace(*linep))
98*2830Sdjl linep++;
99*2830Sdjl
100*2830Sdjl /* compare with the alias name */
101*2830Sdjl keyp = argp->key.serv.serv.name;
102*2830Sdjl while (*keyp && linep < limit && !isspace(*linep) &&
103*2830Sdjl *keyp == *linep) {
104*2830Sdjl keyp++;
105*2830Sdjl linep++;
1060Sstevel@tonic-gate }
107*2830Sdjl if (*keyp == '\0' && (linep == limit || isspace(*linep)))
108*2830Sdjl return (1);
109*2830Sdjl
110*2830Sdjl /* skip remainder of the alias name, if any */
111*2830Sdjl while (linep < limit && !isspace(*linep))
112*2830Sdjl linep++;
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate return (0);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate static nss_status_t
getbyname(be,a)1180Sstevel@tonic-gate getbyname(be, a)
1190Sstevel@tonic-gate files_backend_ptr_t be;
1200Sstevel@tonic-gate void *a;
1210Sstevel@tonic-gate {
122*2830Sdjl nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate return (_nss_files_XY_all(be, argp, 1,
1250Sstevel@tonic-gate argp->key.serv.serv.name, check_name));
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate static int
check_port(nss_XbyY_args_t * argp,const char * line,int linelen)129*2830Sdjl check_port(nss_XbyY_args_t *argp, const char *line, int linelen)
1300Sstevel@tonic-gate {
131*2830Sdjl const char *limit, *linep, *keyp, *numstart;
132*2830Sdjl int numlen, s_port;
133*2830Sdjl char numbuf[12], *numend;
134*2830Sdjl
135*2830Sdjl linep = line;
136*2830Sdjl limit = line + linelen;
137*2830Sdjl
138*2830Sdjl /* skip name */
139*2830Sdjl while (linep < limit && !isspace(*linep))
140*2830Sdjl linep++;
141*2830Sdjl /* skip the delimiting spaces */
142*2830Sdjl while (linep < limit && isspace(*linep))
143*2830Sdjl linep++;
1440Sstevel@tonic-gate
145*2830Sdjl /* compare port num */
146*2830Sdjl numstart = linep;
147*2830Sdjl while (linep < limit && !isspace(*linep) && *linep != '/')
148*2830Sdjl linep++;
149*2830Sdjl if (linep == limit || *linep != '/')
150*2830Sdjl return (0);
151*2830Sdjl numlen = linep - numstart;
152*2830Sdjl if (numlen == 0 || numlen >= sizeof (numbuf))
153*2830Sdjl return (0);
154*2830Sdjl (void) memcpy(numbuf, numstart, numlen);
155*2830Sdjl numbuf[numlen] = '\0';
156*2830Sdjl s_port = htons((int)strtol(numbuf, &numend, 10));
157*2830Sdjl if (*numend != '\0')
158*2830Sdjl return (0);
159*2830Sdjl if (s_port == argp->key.serv.serv.port) {
160*2830Sdjl if ((keyp = argp->key.serv.proto) == NULL)
161*2830Sdjl return (1);
162*2830Sdjl } else
163*2830Sdjl return (0);
164*2830Sdjl
165*2830Sdjl /* compare protocol */
166*2830Sdjl linep++;
167*2830Sdjl while (*keyp && linep < limit && !isspace(*linep) && *keyp == *linep) {
168*2830Sdjl keyp++;
169*2830Sdjl linep++;
170*2830Sdjl }
171*2830Sdjl return (*keyp == '\0' && (linep == limit || isspace(*linep)));
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate static nss_status_t
getbyport(be,a)1750Sstevel@tonic-gate getbyport(be, a)
1760Sstevel@tonic-gate files_backend_ptr_t be;
1770Sstevel@tonic-gate void *a;
1780Sstevel@tonic-gate {
179*2830Sdjl nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
1800Sstevel@tonic-gate char portstr[12];
1810Sstevel@tonic-gate
182*2830Sdjl (void) snprintf(portstr, 12, "%d", ntohs(argp->key.serv.serv.port));
1830Sstevel@tonic-gate return (_nss_files_XY_all(be, argp, 1, portstr, check_port));
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate static files_backend_op_t serv_ops[] = {
1870Sstevel@tonic-gate _nss_files_destr,
1880Sstevel@tonic-gate _nss_files_endent,
1890Sstevel@tonic-gate _nss_files_setent,
1900Sstevel@tonic-gate _nss_files_getent_netdb,
1910Sstevel@tonic-gate getbyname,
1920Sstevel@tonic-gate getbyport
1930Sstevel@tonic-gate };
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate /*ARGSUSED*/
1960Sstevel@tonic-gate nss_backend_t *
_nss_files_services_constr(dummy1,dummy2,dummy3)1970Sstevel@tonic-gate _nss_files_services_constr(dummy1, dummy2, dummy3)
1980Sstevel@tonic-gate const char *dummy1, *dummy2, *dummy3;
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate return (_nss_files_constr(serv_ops,
2010Sstevel@tonic-gate sizeof (serv_ops) / sizeof (serv_ops[0]),
2020Sstevel@tonic-gate _PATH_SERVICES,
2030Sstevel@tonic-gate NSS_LINELEN_SERVICES,
2040Sstevel@tonic-gate NULL));
2050Sstevel@tonic-gate }
206