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*4046Smichen * Common Development and Distribution License (the "License").
6*4046Smichen * 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*4046Smichen
22*4046Smichen /*
23*4046Smichen * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24*4046Smichen * Use is subject to license terms.
25*4046Smichen */
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * nis/getnetent.c -- "nis" backend for nsswitch "networks" database
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include "nis_common.h"
330Sstevel@tonic-gate #include <synch.h>
340Sstevel@tonic-gate #include <netdb.h>
350Sstevel@tonic-gate #include <sys/socket.h>
360Sstevel@tonic-gate #include <netinet/in.h>
370Sstevel@tonic-gate #include <arpa/inet.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate
40*4046Smichen static int nettoa(int anet, char *buf, int buflen, char **pnull);
410Sstevel@tonic-gate
420Sstevel@tonic-gate static nss_status_t
getbyname(be,a)430Sstevel@tonic-gate getbyname(be, a)
440Sstevel@tonic-gate nis_backend_ptr_t be;
450Sstevel@tonic-gate void *a;
460Sstevel@tonic-gate {
47*4046Smichen nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
480Sstevel@tonic-gate
490Sstevel@tonic-gate return (_nss_nis_lookup(be, argp, 1, "networks.byname",
50*4046Smichen argp->key.name, 0));
510Sstevel@tonic-gate }
520Sstevel@tonic-gate
530Sstevel@tonic-gate static nss_status_t
getbyaddr(be,a)540Sstevel@tonic-gate getbyaddr(be, a)
550Sstevel@tonic-gate nis_backend_ptr_t be;
560Sstevel@tonic-gate void *a;
570Sstevel@tonic-gate {
58*4046Smichen nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
590Sstevel@tonic-gate char addrstr[16];
60*4046Smichen char *pnull;
61*4046Smichen nss_status_t rc;
62*4046Smichen
63*4046Smichen if (nettoa((int)argp->key.netaddr.net, addrstr, 16, &pnull) != 0)
64*4046Smichen return (NSS_UNAVAIL); /* it's really ENOMEM */
65*4046Smichen rc = _nss_nis_lookup(be, argp, 1, "networks.byaddr", addrstr, 0);
660Sstevel@tonic-gate
67*4046Smichen /*
68*4046Smichen * if not found, try again with the untruncated address string
69*4046Smichen * that has the trailing zero(s)
70*4046Smichen */
71*4046Smichen if (rc == NSS_NOTFOUND && pnull != NULL) {
72*4046Smichen *pnull = '.';
73*4046Smichen rc = _nss_nis_lookup(be, argp, 1, "networks.byaddr",
74*4046Smichen addrstr, 0);
75*4046Smichen }
76*4046Smichen return (rc);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate
790Sstevel@tonic-gate static nis_backend_op_t net_ops[] = {
800Sstevel@tonic-gate _nss_nis_destr,
810Sstevel@tonic-gate _nss_nis_endent,
820Sstevel@tonic-gate _nss_nis_setent,
830Sstevel@tonic-gate _nss_nis_getent_netdb,
840Sstevel@tonic-gate getbyname,
850Sstevel@tonic-gate getbyaddr
860Sstevel@tonic-gate };
870Sstevel@tonic-gate
880Sstevel@tonic-gate /*ARGSUSED*/
890Sstevel@tonic-gate nss_backend_t *
_nss_nis_networks_constr(dummy1,dummy2,dummy3)900Sstevel@tonic-gate _nss_nis_networks_constr(dummy1, dummy2, dummy3)
910Sstevel@tonic-gate const char *dummy1, *dummy2, *dummy3;
920Sstevel@tonic-gate {
930Sstevel@tonic-gate return (_nss_nis_constr(net_ops,
940Sstevel@tonic-gate sizeof (net_ops) / sizeof (net_ops[0]),
950Sstevel@tonic-gate "networks.byaddr"));
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate * Takes an unsigned integer in host order, and returns a printable
1000Sstevel@tonic-gate * string for it as a network number. To allow for the possibility of
101*4046Smichen * naming subnets, only trailing dot-zeros are truncated. The location
102*4046Smichen * where the string is truncated (or set to '\0') is returned in *pnull.
1030Sstevel@tonic-gate */
1040Sstevel@tonic-gate static int
nettoa(int anet,char * buf,int buflen,char ** pnull)105*4046Smichen nettoa(int anet, char *buf, int buflen, char **pnull)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate char *p;
1080Sstevel@tonic-gate struct in_addr in;
1090Sstevel@tonic-gate int addr;
1100Sstevel@tonic-gate
111*4046Smichen *pnull = NULL;
112*4046Smichen
1130Sstevel@tonic-gate if (buf == 0)
1140Sstevel@tonic-gate return (1);
1150Sstevel@tonic-gate in = inet_makeaddr(anet, INADDR_ANY);
1160Sstevel@tonic-gate addr = in.s_addr;
1170Sstevel@tonic-gate (void) strncpy(buf, inet_ntoa(in), buflen);
1180Sstevel@tonic-gate if ((IN_CLASSA_HOST & htonl(addr)) == 0) {
1190Sstevel@tonic-gate p = strchr(buf, '.');
1200Sstevel@tonic-gate if (p == NULL)
1210Sstevel@tonic-gate return (1);
1220Sstevel@tonic-gate *p = 0;
123*4046Smichen *pnull = p;
1240Sstevel@tonic-gate } else if ((IN_CLASSB_HOST & htonl(addr)) == 0) {
1250Sstevel@tonic-gate p = strchr(buf, '.');
1260Sstevel@tonic-gate if (p == NULL)
1270Sstevel@tonic-gate return (1);
1280Sstevel@tonic-gate p = strchr(p+1, '.');
1290Sstevel@tonic-gate if (p == NULL)
1300Sstevel@tonic-gate return (1);
1310Sstevel@tonic-gate *p = 0;
132*4046Smichen *pnull = p;
1330Sstevel@tonic-gate } else if ((IN_CLASSC_HOST & htonl(addr)) == 0) {
1340Sstevel@tonic-gate p = strrchr(buf, '.');
1350Sstevel@tonic-gate if (p == NULL)
1360Sstevel@tonic-gate return (1);
1370Sstevel@tonic-gate *p = 0;
138*4046Smichen *pnull = p;
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate return (0);
1410Sstevel@tonic-gate }
142