Home | History | Annotate | Line # | Download | only in autofs
      1  1.1  christos #!/usr/bin/awk -f
      2  1.1  christos #-
      3  1.1  christos # Copyright (c) 2017 G. Paul Ziemba
      4  1.1  christos # All rights reserved.
      5  1.1  christos #
      6  1.1  christos #  Redistribution and use in source and binary forms, with or without
      7  1.1  christos #  modification, are permitted provided that the following conditions
      8  1.1  christos #  are met:
      9  1.1  christos #  1. Redistributions of source code must retain the above copyright
     10  1.1  christos #     notice, this list of conditions and the following disclaimer.
     11  1.1  christos #  2. Redistributions in binary form must reproduce the above copyright
     12  1.1  christos #     notice, this list of conditions and the following disclaimer in the
     13  1.1  christos #     documentation and/or other materials provided with the distribution.
     14  1.1  christos #
     15  1.1  christos #  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  1.1  christos #  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  1.1  christos #  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  1.1  christos #  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1  christos #  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1  christos #  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  1.1  christos #  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  christos #  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1  christos #  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  christos #  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  christos #  SUCH DAMAGE.
     26  1.1  christos #
     27  1.1  christos # $NetBSD: include_nis_nullfs,v 1.1 2018/01/09 03:31:14 christos Exp $
     28  1.1  christos #
     29  1.1  christos 
     30  1.1  christos #
     31  1.1  christos # /etc/autofs/include_nis_nullfs
     32  1.1  christos #
     33  1.1  christos # automountd Directory Services script for NIS
     34  1.1  christos #
     35  1.1  christos # SYNOPSIS
     36  1.1  christos #       include_nis_nullfs <mapname>
     37  1.1  christos #
     38  1.1  christos #       include_nis_nullfs <mapname> <key>
     39  1.1  christos #
     40  1.1  christos # DESCRIPTION
     41  1.1  christos #
     42  1.1  christos #       This script provides a Directory Services map for automountd
     43  1.1  christos #       based on NIS. Please see auto_master(5) for general information.
     44  1.1  christos #
     45  1.1  christos #       The first form, with one argument, emits the entire named NIS map.
     46  1.1  christos #       The second form, with two arguments, emits the map entry for the
     47  1.1  christos #       key given in the second argument.
     48  1.1  christos #
     49  1.1  christos #       This script attempts to determine the names and IP addresses
     50  1.1  christos #       of the local host. Map entries matching the local host are
     51  1.1  christos #       rewritten to specify nullfs mounts (instead of the default
     52  1.1  christos #       NFS) to reduce access overhead in the kernel.
     53  1.1  christos #
     54  1.1  christos #	If a map entry contains multiple location fields, it is not changed.
     55  1.1  christos #
     56  1.1  christos 
     57  1.1  christos 
     58  1.1  christos # Populate list of names and IP addrs thet mean "this host"
     59  1.1  christos # into myhostnames array
     60  1.1  christos BEGIN {
     61  1.1  christos     #
     62  1.1  christos     # Set self hostnames
     63  1.1  christos     #
     64  1.1  christos 
     65  1.1  christos     "hostname -s" | getline;
     66  1.1  christos     myhostnames[$0] = 1;
     67  1.1  christos 
     68  1.1  christos     "hostname -f" | getline;
     69  1.1  christos     myhostnames[$0] = 1;
     70  1.1  christos 
     71  1.1  christos     myhostnames["localhost"] = 1
     72  1.1  christos 
     73  1.1  christos     "hostname -f" | getline;
     74  1.1  christos     localdomain=$0
     75  1.1  christos     myhostnames["localhost."localdomain] = 1
     76  1.1  christos 
     77  1.1  christos     while ("ifconfig" | getline) {
     78  1.1  christos 	if ($1 == "inet") {
     79  1.1  christos 	    myhostnames[$2] = 1;
     80  1.1  christos 	}
     81  1.1  christos     }
     82  1.1  christos 
     83  1.1  christos     # debug
     84  1.1  christos #            print "--- hostname list start ----"
     85  1.1  christos #            for (i in myhostnames) {
     86  1.1  christos #                print i
     87  1.1  christos #            }
     88  1.1  christos #            print "--- hostname list end ----"
     89  1.1  christos 
     90  1.1  christos     if (ARGC == 2) {
     91  1.1  christos 	# mapname only
     92  1.1  christos 	while ("ypcat -k " ARGV[1] | getline) {
     93  1.1  christos 	    proc_mapline(1)
     94  1.1  christos 	}
     95  1.1  christos     }
     96  1.1  christos     if (ARGC == 3) {
     97  1.1  christos 	# mapname and keyname
     98  1.1  christos 	while ("ypmatch " ARGV[2] " " ARGV[1] | getline) {
     99  1.1  christos 	    proc_mapline(0)
    100  1.1  christos 	}
    101  1.1  christos     }
    102  1.1  christos     exit 0
    103  1.1  christos }
    104  1.1  christos 
    105  1.1  christos function is_self(hostname)
    106  1.1  christos {
    107  1.1  christos     if (myhostnames[hostname]) {
    108  1.1  christos 	return 1
    109  1.1  christos     }
    110  1.1  christos     return 0
    111  1.1  christos }
    112  1.1  christos 
    113  1.1  christos #
    114  1.1  christos # Lines are of the form [key] [-opts] location1 [... locationN]
    115  1.1  christos #
    116  1.1  christos # indicate index of key field with first positional parameter
    117  1.1  christos # 1 means keyfield is the first field
    118  1.1  christos # 0 means keyfield is not present
    119  1.1  christos #
    120  1.1  christos function proc_mapline(keyfield)
    121  1.1  christos {
    122  1.1  christos     optionsfield = 0
    123  1.1  christos     locationfield = 0
    124  1.1  christos     locationcount = 0
    125  1.1  christos 
    126  1.1  christos     for (i=keyfield+1; i <= NF; ++i) {
    127  1.1  christos 	if (!optionsfield) {
    128  1.1  christos 	    if ($i ~ /^-/) {
    129  1.1  christos 		# the first options field found on the line
    130  1.1  christos 		optionsfield = i;
    131  1.1  christos 		continue
    132  1.1  christos 	    }
    133  1.1  christos 	}
    134  1.1  christos 	# Assumption: location contains colon (":")
    135  1.1  christos 	if (optionsfield && ($i ~ /:/) && ($i !~ /^-/)) {
    136  1.1  christos 	    ++locationcount
    137  1.1  christos 	    if (!locationfield) {
    138  1.1  christos 		# the first location field found on the line
    139  1.1  christos 		locationfield = i
    140  1.1  christos 	    }
    141  1.1  christos 	}
    142  1.1  christos     }
    143  1.1  christos 
    144  1.1  christos     #
    145  1.1  christos     # If location not found, do not modify.
    146  1.1  christos     #
    147  1.1  christos     # If there is more than one location, do not modify. Rationale:
    148  1.1  christos     # Options are applied to all locations. We ca not have "nullfs"
    149  1.1  christos     # for only some locations and "nfs" for others for a given
    150  1.1  christos     # map key (i.e., a line). The usual reason for multiple
    151  1.1  christos     # locations is for redundancy using replicated volumes on
    152  1.1  christos     # multiple hosts, so multiple hosts imply fstype=nfs (the
    153  1.1  christos     # FreeBSD default for automounter maps).
    154  1.1  christos     #
    155  1.1  christos     # Hypothetically there could be a map entry with multiple
    156  1.1  christos     # locations all with host parts matching "me". In that case,
    157  1.1  christos     # it would be safe to rewrite the locations and specify
    158  1.1  christos     # nullfs, but the code does not handle this case.
    159  1.1  christos     #
    160  1.1  christos     if (locationcount == 1) {
    161  1.1  christos 	#
    162  1.1  christos 	# We have a line with exactly one location field
    163  1.1  christos 	#
    164  1.1  christos 	# Assumption: location has no more than one colon (":")
    165  1.1  christos 	#
    166  1.1  christos 	n=split($locationfield,location,":")
    167  1.1  christos 	if (is_self(location[1])) {
    168  1.1  christos 	    $locationfield = ":" location[2]
    169  1.1  christos 	    if (optionsfield) {
    170  1.1  christos 		# append to existing options
    171  1.1  christos 		$optionsfield = $optionsfield ",fstype=nullfs"
    172  1.1  christos 	    } else {
    173  1.1  christos 		# sneak in ahead of location
    174  1.1  christos 		$locationfield = "-fstype=nullfs " $locationfield
    175  1.1  christos 	    }
    176  1.1  christos 	}
    177  1.1  christos     }
    178  1.1  christos 
    179  1.1  christos     print
    180  1.1  christos }
    181