17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0dfdd7f3Smichen * Common Development and Distribution License (the "License").
6*0dfdd7f3Smichen * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21*0dfdd7f3Smichen
22*0dfdd7f3Smichen /*
23*0dfdd7f3Smichen * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24*0dfdd7f3Smichen * Use is subject to license terms.
25*0dfdd7f3Smichen */
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * nis/getnetent.c -- "nis" backend for nsswitch "networks" database
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include "nis_common.h"
317c478bd9Sstevel@tonic-gate #include <synch.h>
327c478bd9Sstevel@tonic-gate #include <netdb.h>
337c478bd9Sstevel@tonic-gate #include <sys/socket.h>
347c478bd9Sstevel@tonic-gate #include <netinet/in.h>
357c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate
38*0dfdd7f3Smichen static int nettoa(int anet, char *buf, int buflen, char **pnull);
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate static nss_status_t
getbyname(be,a)417c478bd9Sstevel@tonic-gate getbyname(be, a)
427c478bd9Sstevel@tonic-gate nis_backend_ptr_t be;
437c478bd9Sstevel@tonic-gate void *a;
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate return (_nss_nis_lookup(be, argp, 1, "networks.byname",
487c478bd9Sstevel@tonic-gate argp->key.name, 0));
497c478bd9Sstevel@tonic-gate }
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate static nss_status_t
getbyaddr(be,a)527c478bd9Sstevel@tonic-gate getbyaddr(be, a)
537c478bd9Sstevel@tonic-gate nis_backend_ptr_t be;
547c478bd9Sstevel@tonic-gate void *a;
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
577c478bd9Sstevel@tonic-gate char addrstr[16];
58*0dfdd7f3Smichen char *pnull;
59*0dfdd7f3Smichen nss_status_t rc;
607c478bd9Sstevel@tonic-gate
61*0dfdd7f3Smichen if (nettoa((int)argp->key.netaddr.net, addrstr, 16, &pnull) != 0)
627c478bd9Sstevel@tonic-gate return (NSS_UNAVAIL); /* it's really ENOMEM */
63*0dfdd7f3Smichen rc = _nss_nis_lookup(be, argp, 1, "networks.byaddr", addrstr, 0);
64*0dfdd7f3Smichen
65*0dfdd7f3Smichen /*
66*0dfdd7f3Smichen * if not found, try again with the untruncated address string
67*0dfdd7f3Smichen * that has the trailing zero(s)
68*0dfdd7f3Smichen */
69*0dfdd7f3Smichen if (rc == NSS_NOTFOUND && pnull != NULL) {
70*0dfdd7f3Smichen *pnull = '.';
71*0dfdd7f3Smichen rc = _nss_nis_lookup(be, argp, 1, "networks.byaddr",
72*0dfdd7f3Smichen addrstr, 0);
73*0dfdd7f3Smichen }
74*0dfdd7f3Smichen return (rc);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate static nis_backend_op_t net_ops[] = {
787c478bd9Sstevel@tonic-gate _nss_nis_destr,
797c478bd9Sstevel@tonic-gate _nss_nis_endent,
807c478bd9Sstevel@tonic-gate _nss_nis_setent,
817c478bd9Sstevel@tonic-gate _nss_nis_getent_netdb,
827c478bd9Sstevel@tonic-gate getbyname,
837c478bd9Sstevel@tonic-gate getbyaddr
847c478bd9Sstevel@tonic-gate };
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate /*ARGSUSED*/
877c478bd9Sstevel@tonic-gate nss_backend_t *
_nss_nis_networks_constr(dummy1,dummy2,dummy3)887c478bd9Sstevel@tonic-gate _nss_nis_networks_constr(dummy1, dummy2, dummy3)
897c478bd9Sstevel@tonic-gate const char *dummy1, *dummy2, *dummy3;
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate return (_nss_nis_constr(net_ops,
927c478bd9Sstevel@tonic-gate sizeof (net_ops) / sizeof (net_ops[0]),
937c478bd9Sstevel@tonic-gate "networks.byaddr"));
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate * Takes an unsigned integer in host order, and returns a printable
987c478bd9Sstevel@tonic-gate * string for it as a network number. To allow for the possibility of
99*0dfdd7f3Smichen * naming subnets, only trailing dot-zeros are truncated. The location
100*0dfdd7f3Smichen * where the string is truncated (or set to '\0') is returned in *pnull.
1017c478bd9Sstevel@tonic-gate */
1027c478bd9Sstevel@tonic-gate static int
nettoa(int anet,char * buf,int buflen,char ** pnull)103*0dfdd7f3Smichen nettoa(int anet, char *buf, int buflen, char **pnull)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate char *p;
1067c478bd9Sstevel@tonic-gate struct in_addr in;
1077c478bd9Sstevel@tonic-gate int addr;
1087c478bd9Sstevel@tonic-gate
109*0dfdd7f3Smichen *pnull = NULL;
110*0dfdd7f3Smichen
1117c478bd9Sstevel@tonic-gate if (buf == 0)
1127c478bd9Sstevel@tonic-gate return (1);
1137c478bd9Sstevel@tonic-gate in = inet_makeaddr(anet, INADDR_ANY);
1147c478bd9Sstevel@tonic-gate addr = in.s_addr;
1157c478bd9Sstevel@tonic-gate (void) strncpy(buf, inet_ntoa(in), buflen);
1167c478bd9Sstevel@tonic-gate if ((IN_CLASSA_HOST & htonl(addr)) == 0) {
1177c478bd9Sstevel@tonic-gate p = strchr(buf, '.');
1187c478bd9Sstevel@tonic-gate if (p == NULL)
1197c478bd9Sstevel@tonic-gate return (1);
1207c478bd9Sstevel@tonic-gate *p = 0;
121*0dfdd7f3Smichen *pnull = p;
1227c478bd9Sstevel@tonic-gate } else if ((IN_CLASSB_HOST & htonl(addr)) == 0) {
1237c478bd9Sstevel@tonic-gate p = strchr(buf, '.');
1247c478bd9Sstevel@tonic-gate if (p == NULL)
1257c478bd9Sstevel@tonic-gate return (1);
1267c478bd9Sstevel@tonic-gate p = strchr(p+1, '.');
1277c478bd9Sstevel@tonic-gate if (p == NULL)
1287c478bd9Sstevel@tonic-gate return (1);
1297c478bd9Sstevel@tonic-gate *p = 0;
130*0dfdd7f3Smichen *pnull = p;
1317c478bd9Sstevel@tonic-gate } else if ((IN_CLASSC_HOST & htonl(addr)) == 0) {
1327c478bd9Sstevel@tonic-gate p = strrchr(buf, '.');
1337c478bd9Sstevel@tonic-gate if (p == NULL)
1347c478bd9Sstevel@tonic-gate return (1);
1357c478bd9Sstevel@tonic-gate *p = 0;
136*0dfdd7f3Smichen *pnull = p;
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate return (0);
1397c478bd9Sstevel@tonic-gate }
140