1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Routines to handle getnet* calls in nscd 28 */ 29 30 #include <string.h> 31 #include <stdlib.h> 32 #include <sys/types.h> 33 #include <sys/socket.h> 34 #include <netinet/in.h> 35 #include <arpa/inet.h> 36 #include "cache.h" 37 38 #define nam_db ctx->nsc_db[0] 39 #define addr_db ctx->nsc_db[1] 40 41 #define NSC_NAME_NETWORKS_BYNAME "getnetbyname" 42 #define NSC_NAME_NETWORKS_BYADDR "getnetbyaddr" 43 44 static int netaddr_compar(const void *, const void *); 45 static uint_t netaddr_gethash(nss_XbyY_key_t *, int); 46 static void netaddr_getlogstr(char *, char *, size_t, nss_XbyY_args_t *); 47 48 void 49 net_init_ctx(nsc_ctx_t *ctx) { 50 ctx->dbname = NSS_DBNAM_NETWORKS; 51 ctx->db_count = 2; 52 ctx->file_name = "/etc/inet/networks"; 53 54 nam_db = make_cache(nsc_key_ces, 55 NSS_DBOP_NETWORKS_BYNAME, 56 NSC_NAME_NETWORKS_BYNAME, 57 NULL, NULL, NULL, nsc_ht_default, -1); 58 59 addr_db = make_cache(nsc_key_other, 60 NSS_DBOP_NETWORKS_BYADDR, 61 NSC_NAME_NETWORKS_BYADDR, 62 netaddr_compar, 63 netaddr_getlogstr, 64 netaddr_gethash, nsc_ht_default, -1); 65 } 66 67 static void 68 netaddr_getlogstr(char *name, char *whoami, size_t len, nss_XbyY_args_t *argp) { 69 if (argp->key.netaddr.type == AF_INET) { 70 uint32_t net; 71 uchar_t *up; 72 73 net = htonl(argp->key.netaddr.net); 74 up = (uchar_t *)&net; 75 76 if (up[0]) 77 (void) snprintf(whoami, len, "%s [key=%d.%d.%d.%d]", 78 name, 79 up[0], up[1], up[2], up[3]); 80 else if (up[1]) 81 (void) snprintf(whoami, len, "%s [key=%d.%d.%d]", 82 name, 83 up[1], up[2], up[3]); 84 else if (up[2]) 85 (void) snprintf(whoami, len, "%s [key=%d.%d]", 86 name, 87 up[2], up[3]); 88 else 89 (void) snprintf(whoami, len, "%s [key=%d]", 90 name, 91 up[3]); 92 } else { 93 (void) snprintf(whoami, len, "%s [key=%d, %d]", 94 name, 95 argp->key.netaddr.net, argp->key.netaddr.type); 96 } 97 } 98 99 static int 100 netaddr_compar(const void *n1, const void *n2) { 101 nsc_entry_t *e1, *e2; 102 103 e1 = (nsc_entry_t *)n1; 104 e2 = (nsc_entry_t *)n2; 105 106 if (e1->key.netaddr.type > e2->key.netaddr.type) 107 return (1); 108 else if (e1->key.netaddr.type < e2->key.netaddr.type) 109 return (-1); 110 111 return (_NSC_INT_KEY_CMP(e1->key.netaddr.net, e2->key.netaddr.net)); 112 } 113 114 static uint_t 115 netaddr_gethash(nss_XbyY_key_t *key, int htsize) { 116 return (db_gethash(&key->netaddr.net, 117 sizeof (key->netaddr.net), htsize)); 118 } 119