11.1Schristos#!/usr/bin/awk -f
21.1Schristos#-
31.1Schristos# Copyright (c) 2017 G. Paul Ziemba
41.1Schristos# All rights reserved.
51.1Schristos#
61.1Schristos#  Redistribution and use in source and binary forms, with or without
71.1Schristos#  modification, are permitted provided that the following conditions
81.1Schristos#  are met:
91.1Schristos#  1. Redistributions of source code must retain the above copyright
101.1Schristos#     notice, this list of conditions and the following disclaimer.
111.1Schristos#  2. Redistributions in binary form must reproduce the above copyright
121.1Schristos#     notice, this list of conditions and the following disclaimer in the
131.1Schristos#     documentation and/or other materials provided with the distribution.
141.1Schristos#
151.1Schristos#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
161.1Schristos#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171.1Schristos#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
181.1Schristos#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191.1Schristos#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201.1Schristos#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
211.1Schristos#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221.1Schristos#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231.1Schristos#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241.1Schristos#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251.1Schristos#  SUCH DAMAGE.
261.1Schristos#
271.1Schristos# $NetBSD: include_nis_nullfs,v 1.1 2018/01/09 03:31:14 christos Exp $
281.1Schristos#
291.1Schristos
301.1Schristos#
311.1Schristos# /etc/autofs/include_nis_nullfs
321.1Schristos#
331.1Schristos# automountd Directory Services script for NIS
341.1Schristos#
351.1Schristos# SYNOPSIS
361.1Schristos#       include_nis_nullfs <mapname>
371.1Schristos#
381.1Schristos#       include_nis_nullfs <mapname> <key>
391.1Schristos#
401.1Schristos# DESCRIPTION
411.1Schristos#
421.1Schristos#       This script provides a Directory Services map for automountd
431.1Schristos#       based on NIS. Please see auto_master(5) for general information.
441.1Schristos#
451.1Schristos#       The first form, with one argument, emits the entire named NIS map.
461.1Schristos#       The second form, with two arguments, emits the map entry for the
471.1Schristos#       key given in the second argument.
481.1Schristos#
491.1Schristos#       This script attempts to determine the names and IP addresses
501.1Schristos#       of the local host. Map entries matching the local host are
511.1Schristos#       rewritten to specify nullfs mounts (instead of the default
521.1Schristos#       NFS) to reduce access overhead in the kernel.
531.1Schristos#
541.1Schristos#	If a map entry contains multiple location fields, it is not changed.
551.1Schristos#
561.1Schristos
571.1Schristos
581.1Schristos# Populate list of names and IP addrs thet mean "this host"
591.1Schristos# into myhostnames array
601.1SchristosBEGIN {
611.1Schristos    #
621.1Schristos    # Set self hostnames
631.1Schristos    #
641.1Schristos
651.1Schristos    "hostname -s" | getline;
661.1Schristos    myhostnames[$0] = 1;
671.1Schristos
681.1Schristos    "hostname -f" | getline;
691.1Schristos    myhostnames[$0] = 1;
701.1Schristos
711.1Schristos    myhostnames["localhost"] = 1
721.1Schristos
731.1Schristos    "hostname -f" | getline;
741.1Schristos    localdomain=$0
751.1Schristos    myhostnames["localhost."localdomain] = 1
761.1Schristos
771.1Schristos    while ("ifconfig" | getline) {
781.1Schristos	if ($1 == "inet") {
791.1Schristos	    myhostnames[$2] = 1;
801.1Schristos	}
811.1Schristos    }
821.1Schristos
831.1Schristos    # debug
841.1Schristos#            print "--- hostname list start ----"
851.1Schristos#            for (i in myhostnames) {
861.1Schristos#                print i
871.1Schristos#            }
881.1Schristos#            print "--- hostname list end ----"
891.1Schristos
901.1Schristos    if (ARGC == 2) {
911.1Schristos	# mapname only
921.1Schristos	while ("ypcat -k " ARGV[1] | getline) {
931.1Schristos	    proc_mapline(1)
941.1Schristos	}
951.1Schristos    }
961.1Schristos    if (ARGC == 3) {
971.1Schristos	# mapname and keyname
981.1Schristos	while ("ypmatch " ARGV[2] " " ARGV[1] | getline) {
991.1Schristos	    proc_mapline(0)
1001.1Schristos	}
1011.1Schristos    }
1021.1Schristos    exit 0
1031.1Schristos}
1041.1Schristos
1051.1Schristosfunction is_self(hostname)
1061.1Schristos{
1071.1Schristos    if (myhostnames[hostname]) {
1081.1Schristos	return 1
1091.1Schristos    }
1101.1Schristos    return 0
1111.1Schristos}
1121.1Schristos
1131.1Schristos#
1141.1Schristos# Lines are of the form [key] [-opts] location1 [... locationN]
1151.1Schristos#
1161.1Schristos# indicate index of key field with first positional parameter
1171.1Schristos# 1 means keyfield is the first field
1181.1Schristos# 0 means keyfield is not present
1191.1Schristos#
1201.1Schristosfunction proc_mapline(keyfield)
1211.1Schristos{
1221.1Schristos    optionsfield = 0
1231.1Schristos    locationfield = 0
1241.1Schristos    locationcount = 0
1251.1Schristos
1261.1Schristos    for (i=keyfield+1; i <= NF; ++i) {
1271.1Schristos	if (!optionsfield) {
1281.1Schristos	    if ($i ~ /^-/) {
1291.1Schristos		# the first options field found on the line
1301.1Schristos		optionsfield = i;
1311.1Schristos		continue
1321.1Schristos	    }
1331.1Schristos	}
1341.1Schristos	# Assumption: location contains colon (":")
1351.1Schristos	if (optionsfield && ($i ~ /:/) && ($i !~ /^-/)) {
1361.1Schristos	    ++locationcount
1371.1Schristos	    if (!locationfield) {
1381.1Schristos		# the first location field found on the line
1391.1Schristos		locationfield = i
1401.1Schristos	    }
1411.1Schristos	}
1421.1Schristos    }
1431.1Schristos
1441.1Schristos    #
1451.1Schristos    # If location not found, do not modify.
1461.1Schristos    #
1471.1Schristos    # If there is more than one location, do not modify. Rationale:
1481.1Schristos    # Options are applied to all locations. We ca not have "nullfs"
1491.1Schristos    # for only some locations and "nfs" for others for a given
1501.1Schristos    # map key (i.e., a line). The usual reason for multiple
1511.1Schristos    # locations is for redundancy using replicated volumes on
1521.1Schristos    # multiple hosts, so multiple hosts imply fstype=nfs (the
1531.1Schristos    # FreeBSD default for automounter maps).
1541.1Schristos    #
1551.1Schristos    # Hypothetically there could be a map entry with multiple
1561.1Schristos    # locations all with host parts matching "me". In that case,
1571.1Schristos    # it would be safe to rewrite the locations and specify
1581.1Schristos    # nullfs, but the code does not handle this case.
1591.1Schristos    #
1601.1Schristos    if (locationcount == 1) {
1611.1Schristos	#
1621.1Schristos	# We have a line with exactly one location field
1631.1Schristos	#
1641.1Schristos	# Assumption: location has no more than one colon (":")
1651.1Schristos	#
1661.1Schristos	n=split($locationfield,location,":")
1671.1Schristos	if (is_self(location[1])) {
1681.1Schristos	    $locationfield = ":" location[2]
1691.1Schristos	    if (optionsfield) {
1701.1Schristos		# append to existing options
1711.1Schristos		$optionsfield = $optionsfield ",fstype=nullfs"
1721.1Schristos	    } else {
1731.1Schristos		# sneak in ahead of location
1741.1Schristos		$locationfield = "-fstype=nullfs " $locationfield
1751.1Schristos	    }
1761.1Schristos	}
1771.1Schristos    }
1781.1Schristos
1791.1Schristos    print
1801.1Schristos}
181