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
52830Sdjl * Common Development and Distribution License (the "License").
62830Sdjl * 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 */
21*6812Sraf
220Sstevel@tonic-gate /*
23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
242830Sdjl * Use is subject to license terms.
25*6812Sraf */
26*6812Sraf
27*6812Sraf /*
280Sstevel@tonic-gate * nis/getservent.c -- "nis" backend for nsswitch "services" database
290Sstevel@tonic-gate */
300Sstevel@tonic-gate
310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include "nis_common.h"
340Sstevel@tonic-gate #include <stdio.h>
352830Sdjl #include <stdlib.h>
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate #include <signal.h>
380Sstevel@tonic-gate #include <malloc.h>
390Sstevel@tonic-gate #include <netdb.h>
400Sstevel@tonic-gate #include <synch.h>
412830Sdjl #include <ctype.h>
420Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
430Sstevel@tonic-gate #include <thread.h>
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include <netinet/in.h>
460Sstevel@tonic-gate
470Sstevel@tonic-gate static int
check_name(args)480Sstevel@tonic-gate check_name(args)
490Sstevel@tonic-gate nss_XbyY_args_t *args;
500Sstevel@tonic-gate {
512830Sdjl struct servent *serv = (struct servent *)args->returnval;
520Sstevel@tonic-gate const char *name = args->key.serv.serv.name;
530Sstevel@tonic-gate const char *proto = args->key.serv.proto;
540Sstevel@tonic-gate char **aliasp;
550Sstevel@tonic-gate
560Sstevel@tonic-gate if (proto != 0 && strcmp(serv->s_proto, proto) != 0) {
570Sstevel@tonic-gate return (0);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate if (strcmp(serv->s_name, name) == 0) {
600Sstevel@tonic-gate return (1);
610Sstevel@tonic-gate }
620Sstevel@tonic-gate for (aliasp = serv->s_aliases; *aliasp != 0; aliasp++) {
630Sstevel@tonic-gate if (strcmp(*aliasp, name) == 0) {
640Sstevel@tonic-gate return (1);
650Sstevel@tonic-gate }
660Sstevel@tonic-gate }
670Sstevel@tonic-gate return (0);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate
702830Sdjl static int
check_name2(nss_XbyY_args_t * argp)712830Sdjl check_name2(nss_XbyY_args_t *argp)
722830Sdjl {
732830Sdjl const char *limit, *linep, *keyp;
742830Sdjl int name_match = 0;
752830Sdjl
762830Sdjl linep = (const char *)argp->buf.buffer;
772830Sdjl limit = linep + strlen(argp->buf.buffer);
782830Sdjl keyp = argp->key.serv.serv.name;
792830Sdjl
802830Sdjl /* compare name */
812830Sdjl while (*keyp && linep < limit && !isspace(*linep) && *keyp == *linep) {
822830Sdjl keyp++;
832830Sdjl linep++;
842830Sdjl }
852830Sdjl if (*keyp == '\0' && linep < limit && isspace(*linep)) {
862830Sdjl if (argp->key.serv.proto == NULL)
872830Sdjl return (1);
882830Sdjl else
892830Sdjl name_match = 1;
902830Sdjl }
912830Sdjl
922830Sdjl /* skip remainder of the name, if any */
932830Sdjl while (linep < limit && !isspace(*linep))
942830Sdjl linep++;
952830Sdjl /* skip the delimiting spaces */
962830Sdjl while (linep < limit && isspace(*linep))
972830Sdjl linep++;
982830Sdjl /* skip port number */
992830Sdjl while (linep < limit && !isspace(*linep) && *linep != '/')
1002830Sdjl linep++;
1012830Sdjl if (linep == limit || *linep != '/')
1022830Sdjl return (0);
1032830Sdjl
1042830Sdjl linep++;
1052830Sdjl if ((keyp = argp->key.serv.proto) == NULL) {
1062830Sdjl /* skip protocol */
1072830Sdjl while (linep < limit && !isspace(*linep))
1082830Sdjl linep++;
1092830Sdjl } else {
1102830Sdjl /* compare protocol */
1112830Sdjl while (*keyp && linep < limit && !isspace(*linep) &&
112*6812Sraf *keyp == *linep) {
1132830Sdjl keyp++;
1142830Sdjl linep++;
1152830Sdjl }
1162830Sdjl /* no protocol match */
1172830Sdjl if (*keyp || (linep < limit && !isspace(*linep)))
1182830Sdjl return (0);
1192830Sdjl /* protocol and name match, return */
1202830Sdjl if (name_match)
1212830Sdjl return (1);
1222830Sdjl /* protocol match but name yet to be matched, so continue */
1232830Sdjl }
1242830Sdjl
1252830Sdjl /* compare with the aliases */
1262830Sdjl while (linep < limit) {
1272830Sdjl /* skip the delimiting spaces */
1282830Sdjl while (linep < limit && isspace(*linep))
1292830Sdjl linep++;
1302830Sdjl
1312830Sdjl /* compare with the alias name */
1322830Sdjl keyp = argp->key.serv.serv.name;
1332830Sdjl while (*keyp && linep < limit && !isspace(*linep) &&
134*6812Sraf *keyp == *linep) {
1352830Sdjl keyp++;
1362830Sdjl linep++;
1372830Sdjl }
1382830Sdjl if (*keyp == '\0' && (linep == limit || isspace(*linep)))
1392830Sdjl return (1);
1402830Sdjl
1412830Sdjl /* skip remainder of the alias name, if any */
1422830Sdjl while (linep < limit && !isspace(*linep))
1432830Sdjl linep++;
1442830Sdjl }
1452830Sdjl return (0);
1462830Sdjl }
1472830Sdjl
1480Sstevel@tonic-gate static mutex_t no_byname_lock = DEFAULTMUTEX;
1490Sstevel@tonic-gate static int no_byname_map = 0;
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate static nss_status_t
getbyname(be,a)1520Sstevel@tonic-gate getbyname(be, a)
1530Sstevel@tonic-gate nis_backend_ptr_t be;
1540Sstevel@tonic-gate void *a;
1550Sstevel@tonic-gate {
1562830Sdjl nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
1570Sstevel@tonic-gate const char *name = argp->key.serv.serv.name;
1580Sstevel@tonic-gate const char *proto = argp->key.serv.proto;
1590Sstevel@tonic-gate int no_map;
1600Sstevel@tonic-gate sigset_t oldmask, newmask;
1610Sstevel@tonic-gate
1622830Sdjl (void) sigfillset(&newmask);
163*6812Sraf (void) thr_sigsetmask(SIG_SETMASK, &newmask, &oldmask);
164*6812Sraf (void) mutex_lock(&no_byname_lock);
1650Sstevel@tonic-gate no_map = no_byname_map;
166*6812Sraf (void) mutex_unlock(&no_byname_lock);
167*6812Sraf (void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate if (no_map == 0) {
1700Sstevel@tonic-gate int yp_status;
1710Sstevel@tonic-gate nss_status_t res;
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate if (proto == 0) {
1740Sstevel@tonic-gate res = _nss_nis_lookup(be, argp, 1,
1750Sstevel@tonic-gate "services.byservicename", name, &yp_status);
1760Sstevel@tonic-gate } else {
1772830Sdjl int len = strlen(name) + strlen(proto) + 3;
1782830Sdjl char *key = malloc(len);
1790Sstevel@tonic-gate
1802830Sdjl if (key == NULL) {
1810Sstevel@tonic-gate return (NSS_UNAVAIL);
1820Sstevel@tonic-gate }
1832830Sdjl (void) snprintf(key, len, "%s/%s", name, proto);
1840Sstevel@tonic-gate res = _nss_nis_lookup(be, argp, 1,
1850Sstevel@tonic-gate "services.byservicename", key, &yp_status);
1860Sstevel@tonic-gate free(key);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate if (yp_status == YPERR_MAP) {
1902830Sdjl (void) sigfillset(&newmask);
191*6812Sraf (void) thr_sigsetmask(SIG_SETMASK, &newmask, &oldmask);
192*6812Sraf (void) mutex_lock(&no_byname_lock);
1930Sstevel@tonic-gate no_byname_map = 1;
194*6812Sraf (void) mutex_unlock(&no_byname_lock);
195*6812Sraf (void) thr_sigsetmask(SIG_SETMASK, &oldmask,
1962830Sdjl (sigset_t *)NULL);
1970Sstevel@tonic-gate } else /* if (res == NSS_SUCCESS) <==== */ {
1980Sstevel@tonic-gate return (res);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate
2022830Sdjl /*
2032830Sdjl * use check_anme to compare service name if nss1 or nss2 and
2042830Sdjl * request is not from nscd; otherwise use check_name2
2052830Sdjl */
2062830Sdjl if (argp->buf.result != NULL)
2072830Sdjl return (_nss_nis_XY_all(be, argp, 1, name, check_name));
2082830Sdjl else
2092830Sdjl return (_nss_nis_XY_all(be, argp, 1, name, check_name2));
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate static int
check_port(args)2130Sstevel@tonic-gate check_port(args)
2140Sstevel@tonic-gate nss_XbyY_args_t *args;
2150Sstevel@tonic-gate {
2162830Sdjl struct servent *serv = (struct servent *)args->returnval;
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate /*
2190Sstevel@tonic-gate * We only resorted to _nss_nis_XY_all because proto == 0, so just...
2200Sstevel@tonic-gate */
2210Sstevel@tonic-gate return (serv->s_port == args->key.serv.serv.port);
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate
2242830Sdjl static int
check_port2(nss_XbyY_args_t * argp)2252830Sdjl check_port2(nss_XbyY_args_t *argp)
2262830Sdjl {
2272830Sdjl const char *limit, *linep, *keyp, *numstart;
2282830Sdjl int numlen, s_port;
2292830Sdjl char numbuf[12], *numend;
2302830Sdjl
2312830Sdjl linep = (const char *)argp->buf.buffer;
2322830Sdjl limit = linep + strlen(argp->buf.buffer);
2332830Sdjl
2342830Sdjl /* skip name */
2352830Sdjl while (linep < limit && !isspace(*linep))
2362830Sdjl linep++;
2372830Sdjl /* skip the delimiting spaces */
2382830Sdjl while (linep < limit && isspace(*linep))
2392830Sdjl linep++;
2402830Sdjl
2412830Sdjl /* compare port num */
2422830Sdjl numstart = linep;
2432830Sdjl while (linep < limit && !isspace(*linep) && *linep != '/')
2442830Sdjl linep++;
2452830Sdjl if (linep == limit || *linep != '/')
2462830Sdjl return (0);
2472830Sdjl numlen = linep - numstart;
2482830Sdjl if (numlen == 0 || numlen >= sizeof (numbuf))
2492830Sdjl return (0);
2502830Sdjl (void) memcpy(numbuf, numstart, numlen);
2512830Sdjl numbuf[numlen] = '\0';
2522830Sdjl s_port = htons((int)strtol(numbuf, &numend, 10));
2532830Sdjl if (*numend != '\0')
2542830Sdjl return (0);
2552830Sdjl if (s_port == argp->key.serv.serv.port) {
2562830Sdjl if ((keyp = argp->key.serv.proto) == NULL)
2572830Sdjl return (1);
2582830Sdjl } else
2592830Sdjl return (0);
2602830Sdjl
2612830Sdjl /* compare protocol */
2622830Sdjl linep++;
2632830Sdjl while (*keyp && linep < limit && !isspace(*linep) && *keyp == *linep) {
2642830Sdjl keyp++;
2652830Sdjl linep++;
2662830Sdjl }
2672830Sdjl return (*keyp == '\0' && (linep == limit || isspace(*linep)));
2682830Sdjl }
2692830Sdjl
2702830Sdjl
2710Sstevel@tonic-gate static nss_status_t
getbyport(be,a)2720Sstevel@tonic-gate getbyport(be, a)
2730Sstevel@tonic-gate nis_backend_ptr_t be;
2740Sstevel@tonic-gate void *a;
2750Sstevel@tonic-gate {
2762830Sdjl nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
2770Sstevel@tonic-gate int port = ntohs(argp->key.serv.serv.port);
2780Sstevel@tonic-gate const char *proto = argp->key.serv.proto;
2790Sstevel@tonic-gate char *key;
2800Sstevel@tonic-gate nss_status_t res;
2812830Sdjl int len;
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate if (proto == 0) {
2840Sstevel@tonic-gate char portstr[12];
2850Sstevel@tonic-gate
2862830Sdjl (void) snprintf(portstr, 12, "%d", port);
2872830Sdjl /*
2882830Sdjl * use check_port to compare service port if nss1 or
2892830Sdjl * nss2 and request is not from nscd; otherwise use
2902830Sdjl * check_port2
2912830Sdjl */
2922830Sdjl if (argp->buf.result != NULL)
2932830Sdjl return (_nss_nis_XY_all(be, argp, 1, portstr,
2942830Sdjl check_port));
2952830Sdjl else
2962830Sdjl return (_nss_nis_XY_all(be, argp, 1, portstr,
2972830Sdjl check_port2));
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate
3002830Sdjl len = strlen(proto) + 14;
3012830Sdjl if ((key = malloc(len)) == 0) {
3020Sstevel@tonic-gate return (NSS_UNAVAIL);
3030Sstevel@tonic-gate }
3042830Sdjl (void) snprintf(key, len, "%d/%s", port, proto);
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate res = _nss_nis_lookup(be, argp, 1, "services.byname", key, 0);
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate free(key);
3090Sstevel@tonic-gate return (res);
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate static nis_backend_op_t serv_ops[] = {
3130Sstevel@tonic-gate _nss_nis_destr,
3140Sstevel@tonic-gate _nss_nis_endent,
3150Sstevel@tonic-gate _nss_nis_setent,
3160Sstevel@tonic-gate _nss_nis_getent_netdb,
3170Sstevel@tonic-gate getbyname,
3180Sstevel@tonic-gate getbyport
3190Sstevel@tonic-gate };
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate /*ARGSUSED*/
3220Sstevel@tonic-gate nss_backend_t *
_nss_nis_services_constr(dummy1,dummy2,dummy3)3230Sstevel@tonic-gate _nss_nis_services_constr(dummy1, dummy2, dummy3)
3240Sstevel@tonic-gate const char *dummy1, *dummy2, *dummy3;
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate return (_nss_nis_constr(serv_ops,
3270Sstevel@tonic-gate sizeof (serv_ops) / sizeof (serv_ops[0]),
3280Sstevel@tonic-gate "services.byname"));
3290Sstevel@tonic-gate }
330