| 1 | #!/bin/bashscip-shell filesystem . . `/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 | usagescip-shell shell shell-project 0.0.0 usage().() { |
| 17 | echoscip-shell system . . echo. "To generate or clean NIS/YP securenets setting for DRBL clients to access" |
| 18 | echoscip-shell system . . echo. "Usage: $0 [Options] {generate|clean}" |
| 19 | echoscip-shell system . . echo. "Options:" |
| 20 | echoscip-shell system . . echo. "-a, --all-subnet: Make all subnet can access to this NIS/YP server." |
| 21 | echoscip-shell system . . echo. "-n, --no-restart: Not restart NIS/YP service" |
| 22 | echoscip-shell system . . echo. "-v, --verbose: Verbose mode." |
| 23 | echoscip-shell system . . echo. "Example: To generate NIS/YP securenets for DRBL clients to access" |
| 24 | echoscip-shell system . . echo. "$0 generate" |
| 25 | } |
| 26 | |
| 27 | # default setting |
| 28 | all_subnet="no" |
| 29 | restart_yp="yes" |
| 30 | |
| 31 | while [scip-shell system . . `[`. $# -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 | -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2 |
| 43 | usagescip-shell shell shell-project 0.0.0 usage(). >& 2 |
| 44 | exit 2 ;; |
| 45 | *) break ;; |
| 46 | esac |
| 47 | done |
| 48 | switch=$1 |
| 49 | |
| 50 | [scip-shell system . . `[`. -z "$switch" ] && usagescip-shell shell shell-project 0.0.0 usage(). && exit 1 |
| 51 | |
| 52 | # |
| 53 | if [scip-shell system . . `[`. -e /etc/debian_versionscip-shell filesystem . . `/etc/debian_version`. ]; then |
| 54 | # Debian |
| 55 | securenets_file=/etc/ypserv.securenetsscip-shell filesystem . . `/etc/ypserv.securenets`. |
| 56 | else |
| 57 | # RH-like or SUSE |
| 58 | securenets_file=/var/yp/securenetsscip-shell filesystem . . `/var/yp/securenets`. |
| 59 | fi |
| 60 | case "$switch" in |
| 61 | "generate"|"on") |
| 62 | echoscip-shell system . . echo. "Now set the YP securenets..." |
| 63 | echoscip-shell system . . echo. "Backup the original $securenets_file as $securenets_file.drblsave" |
| 64 | [scip-shell system . . `[`. -f "$securenets_file" ] && mvscip-shell system . . mv. -f $securenets_file ${securenets_file}.drblsave |
| 65 | time_now="$(datescip-shell system . . date. "+%T %Y/%m/%d")" |
| 66 | catscip-shell system . . 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 | echoscip-shell system . . echo. "# For DRBL server" >> $securenets_file |
| 73 | private_ips="$(get-all-nic-ip --all-ip-address)" |
| 74 | for ip in $private_ips; do |
| 75 | catscip-shell system . . cat. <<EOF >> $securenets_file |
| 76 | 255.255.255.255 $ip |
| 77 | EOF |
| 78 | done |
| 79 | echoscip-shell system . . echo. >> $securenets_file |
| 80 | echoscip-shell system . . echo. "# For DRBL clients" >> $securenets_file |
| 81 | |
| 82 | if [scip-shell system . . `[`. "$all_subnet" = "yes" ]; then |
| 83 | # for DRBL clients |
| 84 | # open the subnet to clients |
| 85 | echoscip-shell system . . echo. "Exporting whole subnet to clients..." |
| 86 | subnet_list="$(get-client-ip-list | awkscip-shell system . . awk. -F"." '{print $1"."$2"."$3}' | sortscip-shell system . . sort. | uniqscip-shell system . . uniq. )" |
| 87 | for subnet in $subnet_list; do |
| 88 | catscip-shell system . . 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 | catscip-shell system . . cat. <<EOF >> $securenets_file |
| 96 | 255.255.255.255 $ip |
| 97 | EOF |
| 98 | done |
| 99 | fi |
| 100 | echoscip-shell system . . echo. "The $securenets_file setting is done!" |
| 101 | |
| 102 | # restart yp if necessary |
| 103 | if [scip-shell system . . `[`. "$restart_yp" = "yes" ]; then |
| 104 | echoscip-shell system . . echo. "Restarting NIS service..." |
| 105 | echoscip-shell system . . echo. "$msg_delimiter_star_line" |
| 106 | /etc/init.d/$scip-shell filesystem . . `/etc/init.d/$YP_SRV_NAME`.YP_SRV_NAME restart |
| 107 | echoscip-shell system . . echo. "$msg_delimiter_star_line" |
| 108 | fi |
| 109 | ;; |
| 110 | "clean"|"off") |
| 111 | echoscip-shell system . . echo. "Now disable the YP access for DRBL clients..." |
| 112 | # stop yp if necessary |
| 113 | if [scip-shell system . . `[`. "$restart_yp" = "yes" ]; then |
| 114 | echoscip-shell system . . echo. "Stopping NIS service..." |
| 115 | echoscip-shell system . . echo. "$msg_delimiter_star_line" |
| 116 | if [scip-shell system . . `[`. -n "$(command -v systemctl 2>/dev/nullscip-shell filesystem . . `/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 [scip-shell system . . `[`. -x "/etc/init.d/$YP_SRV_NAME" ]; then |
| 122 | # For SysV service |
| 123 | /etc/init.d/$scip-shell filesystem . . `/etc/init.d/$YP_SRV_NAME`.YP_SRV_NAME stop |
| 124 | fi |
| 125 | echoscip-shell system . . echo. "$msg_delimiter_star_line" |
| 126 | fi |
| 127 | echoscip-shell system . . echo. "done!" |
| 128 | if [scip-shell system . . `[`. -f $securenets_file ]; then |
| 129 | echoscip-shell system . . echo. "Remove the $securenets_file..." |
| 130 | mvscip-shell system . . mv. -f $securenets_file ${securenets_file}.drblsave |
| 131 | fi |
| 132 | ;; |
| 133 | *) |
| 134 | usagescip-shell shell shell-project 0.0.0 usage(). |
| 135 | exit 1 |
| 136 | ;; |
| 137 | esac |