| 1 | #!/bin/bash |
| 2 | # Author: Steven Shiau <steven _at_ clonezilla org>, Ceasar Sun <ceasar _at_ nchc org tw> |
| 3 | # License: GPL |
| 4 | # |
| 5 | # To generate or clean NIS/YP securenets setting for DRBL clients to access |
| 6 | |
| 7 | # Load DRBL setting and functions |
| 8 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 9 | |
| 10 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 11 | |
| 12 | # |
| 13 | check_if_root |
| 14 | |
| 15 | # main |
| 16 | usage() { |
| 17 | echo "To generate or clean NIS/YP securenets setting for DRBL clients to access" |
| 18 | echo "Usage: $0 [Options] {generate|clean}" |
| 19 | echo "Options:" |
| 20 | echo "-a, --all-subnet: Make all subnet can access to this NIS/YP server." |
| 21 | echo "-n, --no-restart: Not restart NIS/YP service" |
| 22 | echo "-v, --verbose: Verbose mode." |
| 23 | echo "Example: To generate NIS/YP securenets for DRBL clients to access" |
| 24 | echo "$0 generate" |
| 25 | } |
| 26 | |
| 27 | # default setting |
| 28 | all_subnet="no" |
| 29 | restart_yp="yes" |
| 30 | |
| 31 | while [ $# -gt 0 ]; do |
| 32 | case "$1" in |
| 33 | -a|--all-subnet) |
| 34 | all_subnet="yes" |
| 35 | shift;; |
| 36 | -n|--no-restart) |
| 37 | restart_yp="no" |
| 38 | shift;; |
| 39 | -v|--verbose) |
| 40 | shift; verbose="on" |
| 41 | ;; |
| 42 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 43 | usage >& 2 |
| 44 | exit 2 ;; |
| 45 | *) break ;; |
| 46 | esac |
| 47 | done |
| 48 | switch=$1 |
| 49 | |
| 50 | [ -z "$switch" ] && usage && exit 1 |
| 51 | |
| 52 | # |
| 53 | if [ -e /etc/debian_version ]; then |
| 54 | # Debian |
| 55 | securenets_file=/etc/ypserv.securenets |
| 56 | else |
| 57 | # RH-like or SUSE |
| 58 | securenets_file=/var/yp/securenets |
| 59 | fi |
| 60 | case "$switch" in |
| 61 | "generate"|"on") |
| 62 | echo "Now set the YP securenets..." |
| 63 | echo "Backup the original $securenets_file as $securenets_file.drblsave" |
| 64 | [ -f "$securenets_file" ] && mv -f $securenets_file ${securenets_file}.drblsave |
| 65 | time_now="$(date "+%T %Y/%m/%d")" |
| 66 | cat <<EOF > $securenets_file |
| 67 | # Generated by DRBL at $time_now |
| 68 | 255.0.0.0 127.0.0.0 |
| 69 | EOF |
| 70 | |
| 71 | # for DRBL server |
| 72 | echo "# For DRBL server" >> $securenets_file |
| 73 | private_ips="$(get-all-nic-ip --all-ip-address)" |
| 74 | for ip in $private_ips; do |
| 75 | cat <<EOF >> $securenets_file |
| 76 | 255.255.255.255 $ip |
| 77 | EOF |
| 78 | done |
| 79 | echo >> $securenets_file |
| 80 | echo "# For DRBL clients" >> $securenets_file |
| 81 | |
| 82 | if [ "$all_subnet" = "yes" ]; then |
| 83 | # for DRBL clients |
| 84 | # open the subnet to clients |
| 85 | echo "Exporting whole subnet to clients..." |
| 86 | subnet_list="$(get-client-ip-list | awk -F"." '{print $1"."$2"."$3}' | sort | uniq )" |
| 87 | for subnet in $subnet_list; do |
| 88 | cat <<EOF >> $securenets_file |
| 89 | 255.255.255.0 ${subnet}.0 |
| 90 | EOF |
| 91 | done |
| 92 | else |
| 93 | # line by line set |
| 94 | for ip in `get-client-ip-list`; do |
| 95 | cat <<EOF >> $securenets_file |
| 96 | 255.255.255.255 $ip |
| 97 | EOF |
| 98 | done |
| 99 | fi |
| 100 | echo "The $securenets_file setting is done!" |
| 101 | |
| 102 | # restart yp if necessary |
| 103 | if [ "$restart_yp" = "yes" ]; then |
| 104 | echo "Restarting NIS service..." |
| 105 | echo "$msg_delimiter_star_line" |
| 106 | /etc/init.d/$YP_SRV_NAME restart |
| 107 | echo "$msg_delimiter_star_line" |
| 108 | fi |
| 109 | ;; |
| 110 | "clean"|"off") |
| 111 | echo "Now disable the YP access for DRBL clients..." |
| 112 | # stop yp if necessary |
| 113 | if [ "$restart_yp" = "yes" ]; then |
| 114 | echo "Stopping NIS service..." |
| 115 | echo "$msg_delimiter_star_line" |
| 116 | if [ -n "$(command -v systemctl 2>/dev/null)" -a \ |
| 117 | -e "/lib/systemd/system/$YP_SRV_NAME.service" ]; then |
| 118 | # For systemd |
| 119 | systemctl stop $YP_SRV_NAME.service |
| 120 | fi |
| 121 | if [ -x "/etc/init.d/$YP_SRV_NAME" ]; then |
| 122 | # For SysV service |
| 123 | /etc/init.d/$YP_SRV_NAME stop |
| 124 | fi |
| 125 | echo "$msg_delimiter_star_line" |
| 126 | fi |
| 127 | echo "done!" |
| 128 | if [ -f $securenets_file ]; then |
| 129 | echo "Remove the $securenets_file..." |
| 130 | mv -f $securenets_file ${securenets_file}.drblsave |
| 131 | fi |
| 132 | ;; |
| 133 | *) |
| 134 | usage |
| 135 | exit 1 |
| 136 | ;; |
| 137 | esac |