cb5caa98562cf06753163f558cbcfe30b8f4673adjl/*
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * CDDL HEADER START
cb5caa98562cf06753163f558cbcfe30b8f4673adjl *
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * The contents of this file are subject to the terms of the
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Common Development and Distribution License (the "License").
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * You may not use this file except in compliance with the License.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl *
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * or http://www.opensolaris.org/os/licensing.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * See the License for the specific language governing permissions
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * and limitations under the License.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl *
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * When distributing Covered Code, include this CDDL HEADER in each
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * If applicable, add the following below this CDDL HEADER, with the
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * fields enclosed by brackets "[]" replaced with your own identifying
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * information: Portions Copyright [yyyy] [name of copyright owner]
cb5caa98562cf06753163f558cbcfe30b8f4673adjl *
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * CDDL HEADER END
cb5caa98562cf06753163f558cbcfe30b8f4673adjl */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl/*
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Use is subject to license terms.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjl#pragma ident "%Z%%M% %I% %E% SMI"
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjl/*
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Routines to handle getnet* calls in nscd
cb5caa98562cf06753163f558cbcfe30b8f4673adjl */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjl#include <string.h>
cb5caa98562cf06753163f558cbcfe30b8f4673adjl#include <stdlib.h>
cb5caa98562cf06753163f558cbcfe30b8f4673adjl#include <sys/types.h>
cb5caa98562cf06753163f558cbcfe30b8f4673adjl#include <sys/socket.h>
cb5caa98562cf06753163f558cbcfe30b8f4673adjl#include <netinet/in.h>
cb5caa98562cf06753163f558cbcfe30b8f4673adjl#include <arpa/inet.h>
cb5caa98562cf06753163f558cbcfe30b8f4673adjl#include "cache.h"
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjl#define nam_db ctx->nsc_db[0]
cb5caa98562cf06753163f558cbcfe30b8f4673adjl#define addr_db ctx->nsc_db[1]
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjl#define NSC_NAME_NETWORKS_BYNAME "getnetbyname"
cb5caa98562cf06753163f558cbcfe30b8f4673adjl#define NSC_NAME_NETWORKS_BYADDR "getnetbyaddr"
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjlstatic int netaddr_compar(const void *, const void *);
cb5caa98562cf06753163f558cbcfe30b8f4673adjlstatic uint_t netaddr_gethash(nss_XbyY_key_t *, int);
cb5caa98562cf06753163f558cbcfe30b8f4673adjlstatic void netaddr_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjlvoid
cb5caa98562cf06753163f558cbcfe30b8f4673adjlnet_init_ctx(nsc_ctx_t *ctx) {
cb5caa98562cf06753163f558cbcfe30b8f4673adjl ctx->dbname = NSS_DBNAM_NETWORKS;
cb5caa98562cf06753163f558cbcfe30b8f4673adjl ctx->db_count = 2;
cb5caa98562cf06753163f558cbcfe30b8f4673adjl ctx->file_name = "/etc/inet/networks";
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjl nam_db = make_cache(nsc_key_ces,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl NSS_DBOP_NETWORKS_BYNAME,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl NSC_NAME_NETWORKS_BYNAME,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl NULL, NULL, NULL, nsc_ht_default, -1);
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjl addr_db = make_cache(nsc_key_other,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl NSS_DBOP_NETWORKS_BYADDR,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl NSC_NAME_NETWORKS_BYADDR,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl netaddr_compar,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl netaddr_getlogstr,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl netaddr_gethash, nsc_ht_default, -1);
cb5caa98562cf06753163f558cbcfe30b8f4673adjl}
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjlstatic void
cb5caa98562cf06753163f558cbcfe30b8f4673adjlnetaddr_getlogstr(char *name, char *whoami, size_t len, nss_XbyY_args_t *argp) {
cb5caa98562cf06753163f558cbcfe30b8f4673adjl if (argp->key.netaddr.type == AF_INET) {
cb5caa98562cf06753163f558cbcfe30b8f4673adjl uint32_t net;
cb5caa98562cf06753163f558cbcfe30b8f4673adjl uchar_t *up;
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjl net = htonl(argp->key.netaddr.net);
cb5caa98562cf06753163f558cbcfe30b8f4673adjl up = (uchar_t *)&net;
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjl if (up[0])
cb5caa98562cf06753163f558cbcfe30b8f4673adjl (void) snprintf(whoami, len, "%s [key=%d.%d.%d.%d]",
cb5caa98562cf06753163f558cbcfe30b8f4673adjl name,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl up[0], up[1], up[2], up[3]);
cb5caa98562cf06753163f558cbcfe30b8f4673adjl else if (up[1])
cb5caa98562cf06753163f558cbcfe30b8f4673adjl (void) snprintf(whoami, len, "%s [key=%d.%d.%d]",
cb5caa98562cf06753163f558cbcfe30b8f4673adjl name,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl up[1], up[2], up[3]);
cb5caa98562cf06753163f558cbcfe30b8f4673adjl else if (up[2])
cb5caa98562cf06753163f558cbcfe30b8f4673adjl (void) snprintf(whoami, len, "%s [key=%d.%d]",
cb5caa98562cf06753163f558cbcfe30b8f4673adjl name,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl up[2], up[3]);
cb5caa98562cf06753163f558cbcfe30b8f4673adjl else
cb5caa98562cf06753163f558cbcfe30b8f4673adjl (void) snprintf(whoami, len, "%s [key=%d]",
cb5caa98562cf06753163f558cbcfe30b8f4673adjl name,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl up[3]);
cb5caa98562cf06753163f558cbcfe30b8f4673adjl } else {
cb5caa98562cf06753163f558cbcfe30b8f4673adjl (void) snprintf(whoami, len, "%s [key=%d, %d]",
cb5caa98562cf06753163f558cbcfe30b8f4673adjl name,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl argp->key.netaddr.net, argp->key.netaddr.type);
cb5caa98562cf06753163f558cbcfe30b8f4673adjl }
cb5caa98562cf06753163f558cbcfe30b8f4673adjl}
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjlstatic int
cb5caa98562cf06753163f558cbcfe30b8f4673adjlnetaddr_compar(const void *n1, const void *n2) {
cb5caa98562cf06753163f558cbcfe30b8f4673adjl nsc_entry_t *e1, *e2;
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjl e1 = (nsc_entry_t *)n1;
cb5caa98562cf06753163f558cbcfe30b8f4673adjl e2 = (nsc_entry_t *)n2;
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjl if (e1->key.netaddr.type > e2->key.netaddr.type)
cb5caa98562cf06753163f558cbcfe30b8f4673adjl return (1);
cb5caa98562cf06753163f558cbcfe30b8f4673adjl else if (e1->key.netaddr.type < e2->key.netaddr.type)
cb5caa98562cf06753163f558cbcfe30b8f4673adjl return (-1);
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjl return (_NSC_INT_KEY_CMP(e1->key.netaddr.net, e2->key.netaddr.net));
cb5caa98562cf06753163f558cbcfe30b8f4673adjl}
cb5caa98562cf06753163f558cbcfe30b8f4673adjl
cb5caa98562cf06753163f558cbcfe30b8f4673adjlstatic uint_t
cb5caa98562cf06753163f558cbcfe30b8f4673adjlnetaddr_gethash(nss_XbyY_key_t *key, int htsize) {
cb5caa98562cf06753163f558cbcfe30b8f4673adjl return (db_gethash(&key->netaddr.net,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl sizeof (key->netaddr.net), htsize));
cb5caa98562cf06753163f558cbcfe30b8f4673adjl}