8200fe25ffab8b2032d046c88710a949f361b700rmesta/*
8200fe25ffab8b2032d046c88710a949f361b700rmesta * CDDL HEADER START
8200fe25ffab8b2032d046c88710a949f361b700rmesta *
8200fe25ffab8b2032d046c88710a949f361b700rmesta * The contents of this file are subject to the terms of the
8200fe25ffab8b2032d046c88710a949f361b700rmesta * Common Development and Distribution License (the "License").
8200fe25ffab8b2032d046c88710a949f361b700rmesta * You may not use this file except in compliance with the License.
8200fe25ffab8b2032d046c88710a949f361b700rmesta *
8200fe25ffab8b2032d046c88710a949f361b700rmesta * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
8200fe25ffab8b2032d046c88710a949f361b700rmesta * or http://www.opensolaris.org/os/licensing.
8200fe25ffab8b2032d046c88710a949f361b700rmesta * See the License for the specific language governing permissions
8200fe25ffab8b2032d046c88710a949f361b700rmesta * and limitations under the License.
8200fe25ffab8b2032d046c88710a949f361b700rmesta *
8200fe25ffab8b2032d046c88710a949f361b700rmesta * When distributing Covered Code, include this CDDL HEADER in each
8200fe25ffab8b2032d046c88710a949f361b700rmesta * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
8200fe25ffab8b2032d046c88710a949f361b700rmesta * If applicable, add the following below this CDDL HEADER, with the
8200fe25ffab8b2032d046c88710a949f361b700rmesta * fields enclosed by brackets "[]" replaced with your own identifying
8200fe25ffab8b2032d046c88710a949f361b700rmesta * information: Portions Copyright [yyyy] [name of copyright owner]
8200fe25ffab8b2032d046c88710a949f361b700rmesta *
8200fe25ffab8b2032d046c88710a949f361b700rmesta * CDDL HEADER END
8200fe25ffab8b2032d046c88710a949f361b700rmesta */
8200fe25ffab8b2032d046c88710a949f361b700rmesta/*
dd51520e127b452179a2ce4ea3bd8dee949f9afePavan Mettu - Oracle Corporation - Menlo Park United States * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
8200fe25ffab8b2032d046c88710a949f361b700rmesta */
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#ifndef _MAPID_H
8200fe25ffab8b2032d046c88710a949f361b700rmesta#define _MAPID_H
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#ifdef __cplusplus
8200fe25ffab8b2032d046c88710a949f361b700rmestaextern "C" {
8200fe25ffab8b2032d046c88710a949f361b700rmesta#endif
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <stdio.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <stdlib.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <unistd.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <string.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <strings.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <sys/types.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <sys/stat.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <rpc/types.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <netinet/in.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <arpa/nameser.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <resolv.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <netdb.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <errno.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <ctype.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <sys/socket.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <arpa/inet.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <assert.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <synch.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <syslog.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <locale.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <thread.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <deflt.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta#include <nfs/nfs4.h>
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#define DNAMEMAX (NS_MAXCDNAME + 1)
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmestatypedef struct {
8200fe25ffab8b2032d046c88710a949f361b700rmesta void *(*fcn)(void *);
8200fe25ffab8b2032d046c88710a949f361b700rmesta int signal;
8200fe25ffab8b2032d046c88710a949f361b700rmesta} cb_t;
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#ifdef __LIBMAPID_IMPL
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta/*
8200fe25ffab8b2032d046c88710a949f361b700rmesta * Error Messages
8200fe25ffab8b2032d046c88710a949f361b700rmesta */
8200fe25ffab8b2032d046c88710a949f361b700rmesta#define EMSG_NETDB_INTERNAL "Internal Resolver Error: %s"
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#define EMSG_TRY_AGAIN "\"%s\" DNS nameserver(s) not responding" \
8200fe25ffab8b2032d046c88710a949f361b700rmesta "...\tRetrying"
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#define EMSG_NO_RECOVERY "Unrecoverable Resolver Error: %s"
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#define EMSG_HOST_NOT_FOUND "Authoritative nameserver unresponsive " \
8200fe25ffab8b2032d046c88710a949f361b700rmesta "to queries for domain \"%s\""
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#define EMSG_NO_DATA "\"%s\" DNS TXT record not found: "\
8200fe25ffab8b2032d046c88710a949f361b700rmesta "Defaulting to \"%s\""
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#define EMSG_DNS_THREAD_ERROR "Unable to create DNS query thread"
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#define EMSG_DNS_DISABLE "%s: Further DNS queries disabled !"
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#define EMSG_DNS_RR_INVAL "\"%s\" Invalid DNS TXT record: "\
8200fe25ffab8b2032d046c88710a949f361b700rmesta "Defaulting to \"%s\""
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta/*
8200fe25ffab8b2032d046c88710a949f361b700rmesta * DNS related info
8200fe25ffab8b2032d046c88710a949f361b700rmesta */
8200fe25ffab8b2032d046c88710a949f361b700rmesta#define NFSMAPID_DNS_RR "_nfsv4idmapdomain"
8200fe25ffab8b2032d046c88710a949f361b700rmesta#define NFSMAPID_DNS_TOUT_SECS (30LL)
8200fe25ffab8b2032d046c88710a949f361b700rmesta#define NFSMAPID_SLOG_RATE 20 /* ~10 mins */
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#define NS_ERRS 6 /* netdb.h */
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmestatypedef union {
8200fe25ffab8b2032d046c88710a949f361b700rmesta HEADER hdr;
8200fe25ffab8b2032d046c88710a949f361b700rmesta uchar_t buf[PACKETSZ];
8200fe25ffab8b2032d046c88710a949f361b700rmesta} ans_t;
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta/*
8200fe25ffab8b2032d046c88710a949f361b700rmesta * NOTE: All s_ prefixed variables are only to be used by the DNS
8200fe25ffab8b2032d046c88710a949f361b700rmesta * feature implementation (mapid.c). The exported globals
8200fe25ffab8b2032d046c88710a949f361b700rmesta * (ie. seen by nfsmapid.c/nfsmapid_server.c) are the
8200fe25ffab8b2032d046c88710a949f361b700rmesta * dns_ prefixed variables along with sysdns_domain.
8200fe25ffab8b2032d046c88710a949f361b700rmesta */
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic ans_t s_ans;
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic int s_anslen;
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic char s_dname[DNAMEMAX] = {0};
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic char s_txt_rr[DNAMEMAX] = {0};
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic rwlock_t s_dns_data_lock = DEFAULTRWLOCK;
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic rwlock_t s_dns_impl_lock = DEFAULTRWLOCK;
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic mutex_t s_res_lock = ERRORCHECKMUTEX;
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic uint32_t s_dns_tout = 0;
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic thread_t s_dns_qthread;
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic bool_t s_dns_qthr_created = FALSE;
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic bool_t s_dns_disabled = FALSE;
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic struct __res_state s_res = {0};
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic thread_key_t s_thr_key;
8200fe25ffab8b2032d046c88710a949f361b700rmestaint lib_init_done = 0;
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic int resolv_init(void);
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic void resolv_decode(void);
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic int resolv_error(void);
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic void resolv_get_txt_data(void);
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic void resolv_txt_reset(void);
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic void resolve_process_txt(uchar_t *, int);
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic int resolv_search(void);
bfa62c284402a2e7ee6a349df0ef8d1aa2e908e8Vallish Vaidyeshwarastatic void resolv_destroy(void);
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic uchar_t *resolv_skip_rr(uchar_t *, uchar_t *);
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic void domain_sync(cb_t *, char *);
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic int get_mtime(const char *, timestruc_t *);
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic void get_nfs_domain(void);
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic void get_dns_domain(void);
8200fe25ffab8b2032d046c88710a949f361b700rmestastatic void get_dns_txt_domain(cb_t *);
8200fe25ffab8b2032d046c88710a949f361b700rmestavoid _lib_init(void);
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#ifdef DEBUG
8200fe25ffab8b2032d046c88710a949f361b700rmestabool_t nfsmapid_debug = FALSE;
8200fe25ffab8b2032d046c88710a949f361b700rmesta#endif /* DEBUG */
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta/*
8200fe25ffab8b2032d046c88710a949f361b700rmesta * mapid_domain_lock: rwlock used to serialize access/changes
8200fe25ffab8b2032d046c88710a949f361b700rmesta * to the library's mapid_domain global var.
8200fe25ffab8b2032d046c88710a949f361b700rmesta *
8200fe25ffab8b2032d046c88710a949f361b700rmesta * mapid_domain: Library variable used to store the current
8200fe25ffab8b2032d046c88710a949f361b700rmesta * domain configured for use in decoding/encoding
8200fe25ffab8b2032d046c88710a949f361b700rmesta * outbound and inbound attr strings, accordingly.
8200fe25ffab8b2032d046c88710a949f361b700rmesta *
dd51520e127b452179a2ce4ea3bd8dee949f9afePavan Mettu - Oracle Corporation - Menlo Park United States * nfs_domain: If nfsmapid_domain var in SMF
8200fe25ffab8b2032d046c88710a949f361b700rmesta * has been set, nfs_domain will hold this
8200fe25ffab8b2032d046c88710a949f361b700rmesta * value for the duration of the instance;
8200fe25ffab8b2032d046c88710a949f361b700rmesta * If the value ever changes, the change is
8200fe25ffab8b2032d046c88710a949f361b700rmesta * detected via the use of nfs_mtime and
8200fe25ffab8b2032d046c88710a949f361b700rmesta * nfs_domain is updated accordingly.
8200fe25ffab8b2032d046c88710a949f361b700rmesta *
8200fe25ffab8b2032d046c88710a949f361b700rmesta * dns_domain: If the system's resolver (/etc/resolv.conf)
8200fe25ffab8b2032d046c88710a949f361b700rmesta * has been configured, dns_domain will hold
8200fe25ffab8b2032d046c88710a949f361b700rmesta * the configured DNS domain as reported by the
8200fe25ffab8b2032d046c88710a949f361b700rmesta * res_ninit() resolver interface. If the system's
8200fe25ffab8b2032d046c88710a949f361b700rmesta * /etc/resolv.conf file is updated, the change
8200fe25ffab8b2032d046c88710a949f361b700rmesta * is detected via the use of dns_mtime and
8200fe25ffab8b2032d046c88710a949f361b700rmesta * dns_domain is updated accordingly.
8200fe25ffab8b2032d046c88710a949f361b700rmesta */
8200fe25ffab8b2032d046c88710a949f361b700rmestarwlock_t mapid_domain_lock = DEFAULTRWLOCK;
8200fe25ffab8b2032d046c88710a949f361b700rmestauint32_t mapid_domain_len = 0;
8200fe25ffab8b2032d046c88710a949f361b700rmestachar mapid_domain[DNAMEMAX] = {0};
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmestatimestruc_t nfs_mtime = {0};
8200fe25ffab8b2032d046c88710a949f361b700rmestauint32_t nfs_domain_len = 0;
8200fe25ffab8b2032d046c88710a949f361b700rmestachar nfs_domain[DNAMEMAX] = {0};
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmestatimestruc_t dns_mtime = {0};
8200fe25ffab8b2032d046c88710a949f361b700rmestauint32_t dns_domain_len = 0;
8200fe25ffab8b2032d046c88710a949f361b700rmestachar dns_domain[DNAMEMAX] = {0};
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmestaint dns_txt_cached = 0;
8200fe25ffab8b2032d046c88710a949f361b700rmestauint32_t dns_txt_domain_len = 0;
8200fe25ffab8b2032d046c88710a949f361b700rmestachar dns_txt_domain[DNAMEMAX] = {0};
8200fe25ffab8b2032d046c88710a949f361b700rmestachar sysdns_domain[DNAMEMAX] = {0};
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmestatimestruc_t zapped_mtime = {0};
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#define ZAP_DOMAIN(which) \
8200fe25ffab8b2032d046c88710a949f361b700rmesta { \
8200fe25ffab8b2032d046c88710a949f361b700rmesta bzero(which##_domain, DNAMEMAX);\
8200fe25ffab8b2032d046c88710a949f361b700rmesta which##_domain_len = 0; \
8200fe25ffab8b2032d046c88710a949f361b700rmesta which##_mtime = zapped_mtime; \
8200fe25ffab8b2032d046c88710a949f361b700rmesta }
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#define TIMESTRUC_EQ(a, b) \
8200fe25ffab8b2032d046c88710a949f361b700rmesta (((a).tv_sec == (b).tv_sec) && \
8200fe25ffab8b2032d046c88710a949f361b700rmesta ((a).tv_nsec == (b).tv_nsec))
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#endif /* __LIBMAPID_IMPL */
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta/*
8200fe25ffab8b2032d046c88710a949f361b700rmesta * PSARC 2005/487 Consolidation Private Interfaces
8200fe25ffab8b2032d046c88710a949f361b700rmesta * mapid_reeval_domain(), mapid_get_domain()
8200fe25ffab8b2032d046c88710a949f361b700rmesta * Changes must be reviewed by Solaris File Sharing
8200fe25ffab8b2032d046c88710a949f361b700rmesta */
8200fe25ffab8b2032d046c88710a949f361b700rmestaextern void mapid_reeval_domain(cb_t *);
8200fe25ffab8b2032d046c88710a949f361b700rmestaextern char *mapid_get_domain(void);
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta/*
8200fe25ffab8b2032d046c88710a949f361b700rmesta * PSARC 2005/487 Contracted Sun Private Interface
8200fe25ffab8b2032d046c88710a949f361b700rmesta * mapid_derive_domain(), mapid_stdchk_domain()
8200fe25ffab8b2032d046c88710a949f361b700rmesta * Changes must be reviewed by Solaris File Sharing
8200fe25ffab8b2032d046c88710a949f361b700rmesta * Changes must be communicated to contract-2005-487-01@sun.com
8200fe25ffab8b2032d046c88710a949f361b700rmesta */
8200fe25ffab8b2032d046c88710a949f361b700rmestaextern int mapid_stdchk_domain(const char *);
8200fe25ffab8b2032d046c88710a949f361b700rmestaextern char *mapid_derive_domain(void);
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#ifdef __cplusplus
8200fe25ffab8b2032d046c88710a949f361b700rmesta}
8200fe25ffab8b2032d046c88710a949f361b700rmesta#endif
8200fe25ffab8b2032d046c88710a949f361b700rmesta
8200fe25ffab8b2032d046c88710a949f361b700rmesta#endif /* _MAPID_H */