xref: /onnv-gate/usr/src/lib/nsswitch/files/common/netmasks.c (revision 3386:ceea3e620194)
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  */
210Sstevel@tonic-gate /*
22*3386Smichen  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
232830Sdjl  * Use is subject to license terms.
240Sstevel@tonic-gate  *
252830Sdjl  * files/netmasks.c -- "files" backend for nsswitch "netmasks" database
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * All routines necessary to deal with the file /etc/inet/netmasks.  The file
320Sstevel@tonic-gate  * contains mappings from 32 bit network internet addresses to their
330Sstevel@tonic-gate  * corresponding 32 bit mask internet addresses. The addresses are in dotted
340Sstevel@tonic-gate  * internet address form.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include "files_common.h"
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/socket.h>
410Sstevel@tonic-gate #include <net/if.h>
420Sstevel@tonic-gate #include <netinet/in.h>
430Sstevel@tonic-gate #include <arpa/inet.h>
440Sstevel@tonic-gate #include <nss_dbdefs.h>
452830Sdjl #include <ctype.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * Validate 'files' netmasks entry. The comparison objects are in IPv4
490Sstevel@tonic-gate  * internet address format.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate static int
check_addr(nss_XbyY_args_t * argp,const char * line,int linelen)522830Sdjl check_addr(nss_XbyY_args_t *argp, const char *line, int linelen)
530Sstevel@tonic-gate {
542830Sdjl 	const char	*limit, *linep, *addrstart;
552830Sdjl 	int		addrlen;
562830Sdjl 	char		addrbuf[NSS_LINELEN_NETMASKS];
572830Sdjl 	struct in_addr	lineaddr, argsaddr;
582830Sdjl 
592830Sdjl 	linep = line;
602830Sdjl 	limit = line + linelen;
612830Sdjl 
622830Sdjl 	/* skip leading spaces */
632830Sdjl 	while (linep < limit && isspace(*linep))
642830Sdjl 		linep++;
650Sstevel@tonic-gate 
662830Sdjl 	addrstart = linep;
672830Sdjl 	while (linep < limit && !isspace(*linep))
682830Sdjl 		linep++;
692830Sdjl 	if (linep == limit)
702830Sdjl 		return (0);
712830Sdjl 	addrlen = linep - addrstart;
722830Sdjl 	if (addrlen < sizeof (addrbuf)) {
732830Sdjl 		(void) memcpy(addrbuf, addrstart, addrlen);
742830Sdjl 		addrbuf[addrlen] = '\0';
752830Sdjl 		if ((lineaddr.s_addr = inet_addr(addrbuf)) ==
762830Sdjl 						(in_addr_t)0xffffffffU)
772830Sdjl 			return (0);
782830Sdjl 		if ((argsaddr.s_addr = inet_addr(argp->key.name))
792830Sdjl 				== (in_addr_t)0xffffffffU)
802830Sdjl 			return (0);
812830Sdjl 		return (lineaddr.s_addr == argsaddr.s_addr);
822830Sdjl 	}
832830Sdjl 	return (0);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate 
860Sstevel@tonic-gate static nss_status_t
getbynet(be,a)870Sstevel@tonic-gate getbynet(be, a)
880Sstevel@tonic-gate 	files_backend_ptr_t	be;
890Sstevel@tonic-gate 	void			*a;
900Sstevel@tonic-gate {
912830Sdjl 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
920Sstevel@tonic-gate 	nss_status_t		res;
930Sstevel@tonic-gate 	char			tmpbuf[NSS_LINELEN_NETMASKS];
940Sstevel@tonic-gate 
952830Sdjl 	/*
962830Sdjl 	 * use the buffer passed in if result is to be returned
972830Sdjl 	 * in /etc file format
982830Sdjl 	 */
992830Sdjl 	if (argp->buf.result != NULL) {
1002830Sdjl 		argp->buf.buffer = tmpbuf;
1012830Sdjl 		argp->buf.buflen = NSS_LINELEN_NETMASKS;
1022830Sdjl 	}
103*3386Smichen 	res = _nss_files_XY_all(be, argp, 1, argp->key.name, check_addr);
1042830Sdjl 	if (argp->buf.result != NULL) {
1052830Sdjl 		argp->buf.buffer = NULL;
1062830Sdjl 		argp->buf.buflen = 0;
1072830Sdjl 	} else {
1082830Sdjl 		/* the frontend expects the netmask data only */
1092830Sdjl 		if (res == NSS_SUCCESS)	 {
1102830Sdjl 			char	*m;
1112830Sdjl 			char	*s = (char *)argp->returnval;
1122830Sdjl 			int	l = 0;
1132830Sdjl 
1142830Sdjl 			m = s + argp->returnlen - 1;
1152830Sdjl 
1162830Sdjl 			/* skip trailing spaces */
1172830Sdjl 			while (s < m && isspace(*m))
1182830Sdjl 				m--;
1192830Sdjl 
1202830Sdjl 			for (; s <= m; m--) {
1212830Sdjl 				if (isspace(*m))
1222830Sdjl 					break;
1232830Sdjl 				l++;
1242830Sdjl 			}
1252830Sdjl 			m++;
1262830Sdjl 			(void) memmove(argp->returnval, m, l);
1272830Sdjl 			argp->returnlen = l;
1282830Sdjl 			*(s + l) = '\0';
1292830Sdjl 		}
1302830Sdjl 	}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	return (res);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate static files_backend_op_t netmasks_ops[] = {
1360Sstevel@tonic-gate 	_nss_files_destr,
1370Sstevel@tonic-gate 	getbynet
1380Sstevel@tonic-gate };
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /*ARGSUSED*/
1410Sstevel@tonic-gate nss_backend_t *
_nss_files_netmasks_constr(dummy1,dummy2,dummy3)1420Sstevel@tonic-gate _nss_files_netmasks_constr(dummy1, dummy2, dummy3)
1430Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	return (_nss_files_constr(netmasks_ops,
1460Sstevel@tonic-gate 	    sizeof (netmasks_ops) / sizeof (netmasks_ops[0]),
1470Sstevel@tonic-gate 	    _PATH_NETMASKS, NSS_LINELEN_NETMASKS, NULL));
1480Sstevel@tonic-gate }
149