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
5*3957Sth199096 * Common Development and Distribution License (the "License").
6*3957Sth199096 * 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 */
21*3957Sth199096
220Sstevel@tonic-gate /*
23*3957Sth199096 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <syslog.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <rpc/rpc.h>
360Sstevel@tonic-gate #include <sys/stat.h>
370Sstevel@tonic-gate #include <netconfig.h>
380Sstevel@tonic-gate #include <netdir.h>
390Sstevel@tonic-gate #include <sys/file.h>
400Sstevel@tonic-gate #include <sys/time.h>
410Sstevel@tonic-gate #include <sys/errno.h>
420Sstevel@tonic-gate #include <sys/resource.h>
430Sstevel@tonic-gate #include <rpcsvc/mount.h>
440Sstevel@tonic-gate #include <sys/pathconf.h>
450Sstevel@tonic-gate #include <sys/systeminfo.h>
460Sstevel@tonic-gate #include <sys/utsname.h>
470Sstevel@tonic-gate #include <signal.h>
480Sstevel@tonic-gate #include <locale.h>
490Sstevel@tonic-gate #include <unistd.h>
500Sstevel@tonic-gate #include <thread.h>
51*3957Sth199096 #include <sharefs/share.h>
520Sstevel@tonic-gate #include "../lib/sharetab.h"
530Sstevel@tonic-gate #include "mountd.h"
540Sstevel@tonic-gate
550Sstevel@tonic-gate struct cache_entry {
560Sstevel@tonic-gate char *cache_host;
570Sstevel@tonic-gate time_t cache_time;
580Sstevel@tonic-gate int cache_belong;
590Sstevel@tonic-gate char **cache_grl;
600Sstevel@tonic-gate int cache_grc;
610Sstevel@tonic-gate struct cache_entry *cache_next;
620Sstevel@tonic-gate };
630Sstevel@tonic-gate
640Sstevel@tonic-gate static struct cache_entry *cache_head;
650Sstevel@tonic-gate
660Sstevel@tonic-gate #define VALID_TIME 60 /* seconds */
670Sstevel@tonic-gate
680Sstevel@tonic-gate static rwlock_t cache_lock; /* protect the cache chain */
690Sstevel@tonic-gate
700Sstevel@tonic-gate static void cache_free(struct cache_entry *entry);
710Sstevel@tonic-gate static int cache_check(char *host, char **grl, int grc, int *belong);
720Sstevel@tonic-gate static void cache_enter(char *host, char **grl, int grc, int belong);
730Sstevel@tonic-gate
740Sstevel@tonic-gate
750Sstevel@tonic-gate void
netgroup_init()760Sstevel@tonic-gate netgroup_init()
770Sstevel@tonic-gate {
780Sstevel@tonic-gate (void) rwlock_init(&cache_lock, USYNC_THREAD, NULL);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate * Check whether any of the hostnames in clnames are
830Sstevel@tonic-gate * members (or non-members) of the netgroups in glist.
840Sstevel@tonic-gate * Since the innetgr lookup is rather expensive, the
850Sstevel@tonic-gate * result is cached. The cached entry is valid only
860Sstevel@tonic-gate * for VALID_TIME seconds. This works well because
870Sstevel@tonic-gate * typically these lookups occur in clusters when
880Sstevel@tonic-gate * a client is mounting.
890Sstevel@tonic-gate *
900Sstevel@tonic-gate * Note that this routine establishes a host membership
910Sstevel@tonic-gate * in a list of netgroups - we've no idea just which
920Sstevel@tonic-gate * netgroup in the list it is a member of.
930Sstevel@tonic-gate *
940Sstevel@tonic-gate * glist is a character array containing grc strings
950Sstevel@tonic-gate * representing netgroup names (optionally prefixed
960Sstevel@tonic-gate * with '-'). Each string is ended with '\0' and
970Sstevel@tonic-gate * followed immediately by the next string.
980Sstevel@tonic-gate */
990Sstevel@tonic-gate int
netgroup_check(struct nd_hostservlist * clnames,char * glist,int grc)1000Sstevel@tonic-gate netgroup_check(struct nd_hostservlist *clnames, char *glist, int grc)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate char **grl;
1030Sstevel@tonic-gate char *gr;
1040Sstevel@tonic-gate int nhosts = clnames->h_cnt;
1050Sstevel@tonic-gate char *host0, *host;
1060Sstevel@tonic-gate int i, j, n;
1070Sstevel@tonic-gate int response;
1080Sstevel@tonic-gate int belong = 0;
1090Sstevel@tonic-gate static char *domain;
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate if (domain == NULL) {
1120Sstevel@tonic-gate int ssize;
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate domain = exmalloc(SYS_NMLN);
1150Sstevel@tonic-gate ssize = sysinfo(SI_SRPC_DOMAIN, domain, SYS_NMLN);
1160Sstevel@tonic-gate if (ssize > SYS_NMLN) {
1170Sstevel@tonic-gate free(domain);
1180Sstevel@tonic-gate domain = exmalloc(ssize);
1190Sstevel@tonic-gate ssize = sysinfo(SI_SRPC_DOMAIN, domain, ssize);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate /* Check for error in syscall or NULL domain name */
1220Sstevel@tonic-gate if (ssize <= 1) {
1230Sstevel@tonic-gate syslog(LOG_ERR, "No default domain set");
1240Sstevel@tonic-gate return (0);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate grl = calloc(grc, sizeof (char *));
1290Sstevel@tonic-gate if (grl == NULL)
1300Sstevel@tonic-gate return (0);
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate for (i = 0, gr = glist; i < grc && !belong; ) {
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate * If the netgroup name has a '-' prepended
1350Sstevel@tonic-gate * then a match of this name implies a failure
1360Sstevel@tonic-gate * instead of success.
1370Sstevel@tonic-gate */
1380Sstevel@tonic-gate response = (*gr != '-') ? 1 : 0;
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate * Subsequent names with or without a '-' (but no mix)
1420Sstevel@tonic-gate * can be grouped together for a single check.
1430Sstevel@tonic-gate */
1440Sstevel@tonic-gate for (n = 0; i < grc; i++, n++, gr += strlen(gr) + 1) {
1450Sstevel@tonic-gate if (response && *gr == '-' || !response && *gr != '-')
1460Sstevel@tonic-gate break;
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate grl[n] = response ? gr : gr + 1;
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate host0 = clnames->h_hostservs[0].h_host;
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * If not in cache check the netgroup for each
1550Sstevel@tonic-gate * of the hosts names (usually just one).
1560Sstevel@tonic-gate * Enter the result into the cache.
1570Sstevel@tonic-gate */
1580Sstevel@tonic-gate if (!cache_check(host0, grl, n, &belong)) {
1590Sstevel@tonic-gate for (j = 0; j < nhosts && !belong; j++) {
1600Sstevel@tonic-gate host = clnames->h_hostservs[j].h_host;
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate if (__multi_innetgr(n, grl,
1630Sstevel@tonic-gate 1, &host,
1640Sstevel@tonic-gate 0, NULL,
1650Sstevel@tonic-gate 1, &domain))
1660Sstevel@tonic-gate belong = 1;
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate cache_enter(host0, grl, n, belong);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate free(grl);
1740Sstevel@tonic-gate return (belong ? response : 0);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate /*
1780Sstevel@tonic-gate * Free a cache entry and all entries
1790Sstevel@tonic-gate * further down the chain since they
1800Sstevel@tonic-gate * will also be expired.
1810Sstevel@tonic-gate */
1820Sstevel@tonic-gate static void
cache_free(struct cache_entry * entry)1830Sstevel@tonic-gate cache_free(struct cache_entry *entry)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate struct cache_entry *ce, *next;
1860Sstevel@tonic-gate int i;
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate for (ce = entry; ce; ce = next) {
1890Sstevel@tonic-gate if (ce->cache_host)
1900Sstevel@tonic-gate free(ce->cache_host);
1910Sstevel@tonic-gate for (i = 0; i < ce->cache_grc; i++)
1920Sstevel@tonic-gate if (ce->cache_grl[i])
1930Sstevel@tonic-gate free(ce->cache_grl[i]);
1940Sstevel@tonic-gate if (ce->cache_grl)
1950Sstevel@tonic-gate free(ce->cache_grl);
1960Sstevel@tonic-gate next = ce->cache_next;
1970Sstevel@tonic-gate free(ce);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate * Search the entries in the cache chain looking
2030Sstevel@tonic-gate * for an entry with a matching hostname and group
2040Sstevel@tonic-gate * list. If a match is found then return the "belong"
2050Sstevel@tonic-gate * value which may be 1 or 0 depending on whether the
2060Sstevel@tonic-gate * client is a member of the list or not. This is
2070Sstevel@tonic-gate * both a positive and negative cache.
2080Sstevel@tonic-gate *
2090Sstevel@tonic-gate * Cache entries have a validity of VALID_TIME seconds.
2100Sstevel@tonic-gate * If we find an expired entry then blow away the entry
2110Sstevel@tonic-gate * and the rest of the chain since entries further down
2120Sstevel@tonic-gate * the chain will be expired too because we always add
2130Sstevel@tonic-gate * new entries to the head of the chain.
2140Sstevel@tonic-gate */
2150Sstevel@tonic-gate static int
cache_check(char * host,char ** grl,int grc,int * belong)2160Sstevel@tonic-gate cache_check(char *host, char **grl, int grc, int *belong)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate struct cache_entry *ce, *prev;
2190Sstevel@tonic-gate time_t timenow = time(NULL);
2200Sstevel@tonic-gate int i;
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate (void) rw_rdlock(&cache_lock);
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate for (ce = cache_head; ce; ce = ce->cache_next) {
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate * If we find a stale entry, there can't
2280Sstevel@tonic-gate * be any valid entries from here on.
2290Sstevel@tonic-gate * Acquire a write lock, search the chain again
2300Sstevel@tonic-gate * and delete the stale entry and all following
2310Sstevel@tonic-gate * entries.
2320Sstevel@tonic-gate */
2330Sstevel@tonic-gate if (timenow > ce->cache_time) {
2340Sstevel@tonic-gate (void) rw_unlock(&cache_lock);
2350Sstevel@tonic-gate (void) rw_wrlock(&cache_lock);
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate for (prev = NULL, ce = cache_head; ce;
2380Sstevel@tonic-gate prev = ce, ce = ce->cache_next)
2390Sstevel@tonic-gate if (timenow > ce->cache_time)
2400Sstevel@tonic-gate break;
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate if (ce != NULL) {
2430Sstevel@tonic-gate if (prev)
2440Sstevel@tonic-gate prev->cache_next = NULL;
2450Sstevel@tonic-gate else
2460Sstevel@tonic-gate cache_head = NULL;
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate cache_free(ce);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate (void) rw_unlock(&cache_lock);
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate return (0);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate if (ce->cache_grc != grc)
2550Sstevel@tonic-gate continue; /* no match */
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate if (strcasecmp(host, ce->cache_host) != 0)
2580Sstevel@tonic-gate continue; /* no match */
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate for (i = 0; i < grc; i++)
2610Sstevel@tonic-gate if (strcasecmp(ce->cache_grl[i], grl[i]) != 0)
2620Sstevel@tonic-gate break; /* no match */
2630Sstevel@tonic-gate if (i < grc)
2640Sstevel@tonic-gate continue;
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate *belong = ce->cache_belong;
2670Sstevel@tonic-gate (void) rw_unlock(&cache_lock);
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate return (1);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate (void) rw_unlock(&cache_lock);
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate return (0);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate /*
2780Sstevel@tonic-gate * Put a new entry in the cache chain by
2790Sstevel@tonic-gate * prepending it to the front.
2800Sstevel@tonic-gate * If there isn't enough memory then just give up.
2810Sstevel@tonic-gate */
2820Sstevel@tonic-gate static void
cache_enter(char * host,char ** grl,int grc,int belong)2830Sstevel@tonic-gate cache_enter(char *host, char **grl, int grc, int belong)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate struct cache_entry *entry;
2860Sstevel@tonic-gate int i;
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate entry = malloc(sizeof (*entry));
2890Sstevel@tonic-gate if (entry == NULL)
2900Sstevel@tonic-gate return;
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate (void) memset((caddr_t)entry, 0, sizeof (*entry));
2930Sstevel@tonic-gate entry->cache_host = strdup(host);
2940Sstevel@tonic-gate if (entry->cache_host == NULL) {
2950Sstevel@tonic-gate cache_free(entry);
2960Sstevel@tonic-gate return;
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate entry->cache_time = time(NULL) + VALID_TIME;
3000Sstevel@tonic-gate entry->cache_belong = belong;
3010Sstevel@tonic-gate entry->cache_grl = malloc(grc * sizeof (char *));
3020Sstevel@tonic-gate if (entry->cache_grl == NULL) {
3030Sstevel@tonic-gate cache_free(entry);
3040Sstevel@tonic-gate return;
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate for (i = 0; i < grc; i++) {
3080Sstevel@tonic-gate entry->cache_grl[i] = strdup(grl[i]);
3090Sstevel@tonic-gate if (entry->cache_grl[i] == NULL) {
3100Sstevel@tonic-gate entry->cache_grc = i;
3110Sstevel@tonic-gate cache_free(entry);
3120Sstevel@tonic-gate return;
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate entry->cache_grc = grc;
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate (void) rw_wrlock(&cache_lock);
3190Sstevel@tonic-gate entry->cache_next = cache_head;
3200Sstevel@tonic-gate cache_head = entry;
3210Sstevel@tonic-gate (void) rw_unlock(&cache_lock);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate /*
3250Sstevel@tonic-gate * Full cache flush
3260Sstevel@tonic-gate */
3270Sstevel@tonic-gate void
netgrp_cache_flush(void)3280Sstevel@tonic-gate netgrp_cache_flush(void)
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate (void) rw_wrlock(&cache_lock);
3310Sstevel@tonic-gate cache_free(cache_head);
3320Sstevel@tonic-gate cache_head = NULL;
3330Sstevel@tonic-gate (void) rw_unlock(&cache_lock);
3340Sstevel@tonic-gate }
335