| 1 | #!/bin/bash |
| 2 | # Author: Steven Shiau <steven _at_ clonezilla org> |
| 3 | # License: GPL |
| 4 | |
| 5 | # Load DRBL setting and functions |
| 6 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 7 | |
| 8 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 9 | |
| 10 | # slat for SHA1, better to leave it empty, let the program pick a random one. |
| 11 | salt="" |
| 12 | |
| 13 | # |
| 14 | usage() { |
| 15 | echo "Usage:" |
| 16 | echo "To set the pxelinux password for clients:" |
| 17 | echo "`basename $0` [OPTION]" |
| 18 | echo "Options:" |
| 19 | echo "--stdin PASSWORD set the root's password for clients as PASSWORD" |
| 20 | echo "-h, --host IP_ADDRESS: set only for the host with IP_ADDRESS instead of all DRBL clients" |
| 21 | echo "-d, --disable disable the menu password" |
| 22 | echo "-e, --enable enable the menu password" |
| 23 | echo "-o, --overwrite-pxelinux-passwd [y/n] when a PXELinux password already exists in config file, overwrite it or not." |
| 24 | echo "-b, --label LABEL enable/disable the LABEL block only in PXELinux config file" |
| 25 | echo "-v, --verbose prints out verbose information" |
| 26 | echo "Example:" |
| 27 | echo "To assign the PXELinux password for all label blocks, run" |
| 28 | echo "`basename $0` -e" |
| 29 | echo "To assign the PXELinux password for menu label \"drbl\" and \"local\" only, run" |
| 30 | echo "`basename $0` -e -b \"drbl local\"" |
| 31 | echo "To disable the authentication mechanism, run" |
| 32 | echo "`basename $0` -d" |
| 33 | } |
| 34 | |
| 35 | # |
| 36 | check_if_root |
| 37 | |
| 38 | # main |
| 39 | unalias ls 2>/dev/null |
| 40 | |
| 41 | specified_host='' |
| 42 | # Parse command-line options |
| 43 | while [ $# -gt 0 ]; do |
| 44 | case "$1" in |
| 45 | --stdin) |
| 46 | shift; |
| 47 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 48 | # skip the -xx option, in case |
| 49 | pxelinux_passwd="$1" |
| 50 | fi |
| 51 | shift;; |
| 52 | -h|--host) |
| 53 | shift; specified_host="$specified_host $1" |
| 54 | shift;; |
| 55 | -d|--disable) |
| 56 | shift; mode="disable" |
| 57 | ;; |
| 58 | -e|--enable) |
| 59 | shift; mode="enable" |
| 60 | ;; |
| 61 | -o|--overwrite-pxelinux-passwd) |
| 62 | shift |
| 63 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 64 | # skip the -xx option, in case |
| 65 | overwrite_pxelinux_passwd="$1" |
| 66 | shift |
| 67 | fi |
| 68 | ;; |
| 69 | -b|--label) |
| 70 | shift |
| 71 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 72 | # skip the -xx option, in case |
| 73 | labels="$1" |
| 74 | shift |
| 75 | fi |
| 76 | ;; |
| 77 | -v|--verbose) |
| 78 | shift; VERBOSE="on" |
| 79 | ;; |
| 80 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 81 | usage >& 2 |
| 82 | exit 2 ;; |
| 83 | *) break ;; |
| 84 | esac |
| 85 | done |
| 86 | |
| 87 | if [ -z "$mode" ]; then |
| 88 | usage |
| 89 | exit 1 |
| 90 | fi |
| 91 | |
| 92 | # |
| 93 | switch_label_block_menu_passwd() { |
| 94 | local LAB="$1" |
| 95 | local ACT="$2" |
| 96 | local PXE_CONF_TMP="$3" |
| 97 | local PASSWD_STR="$4" |
| 98 | check_img_in_pxe_cfg $LAB $PXE_CONF_TMP |
| 99 | # turn on/off MENU PASSWD for specified label block |
| 100 | lines=$(get_pxecfg_image_block $LAB $PXE_CONF_TMP) |
| 101 | begin_line=$(echo $lines | awk -F" " '{print $1}') |
| 102 | end_line=$(echo $lines | awk -F" " '{print $2}') |
| 103 | case "$ACT" in |
| 104 | "on") |
| 105 | [ -n "$VERBOSE" ] && echo "Turn on 'MENU PASSWD' for label block $LAB in $PXE_CONF_TMP... " |
| 106 | sub_act_cmd="if ($begin_line..$end_line) {s/^(#|[[:space:]])*MENU PASSWD.*/ MENU PASSWD $PASSWD_STR/i}" |
| 107 | ;; |
| 108 | "off") |
| 109 | [ -n "$VERBOSE" ] && echo "Turn off 'MENU PASSWD' for label block $LAB in $PXE_CONF_TMP... " |
| 110 | sub_act_cmd="if ($begin_line..$end_line) {s/^(#|[[:space:]])*(MENU PASSWD.*)/ # \$2/i}" |
| 111 | ;; |
| 112 | esac |
| 113 | perl -pi -e "$sub_act_cmd" $PXE_CONF_TMP |
| 114 | } # end of switch_label_block_menu_passwd |
| 115 | # |
| 116 | input_new_passwd() { |
| 117 | # interactive |
| 118 | echo "New password: (It will not be echoed in the screen)" |
| 119 | read -s pass1 |
| 120 | echo "Retype new password: (It will not be echoed in the screen)" |
| 121 | read -s pass2 |
| 122 | while [ "$pass1" != "$pass2" ]; do |
| 123 | echo "Sorry, passwords do not match" |
| 124 | echo "New password: (It will not be echoed in the screen)" |
| 125 | read -s pass1 |
| 126 | echo "Retype new password: (It will not be echoed in the screen)" |
| 127 | read -s pass2 |
| 128 | done |
| 129 | # |
| 130 | [ -z "$pass1" ] && echo "Password can NOT be empty!!! Program terminated" && exit 1 |
| 131 | # set the matched password |
| 132 | new_passwd="$pass1" |
| 133 | } |
| 134 | |
| 135 | get_pxelinux_master_sha1passwd() { |
| 136 | local CONFILE="$1" |
| 137 | sha1_passwd="$(grep -i "^[[:space:]]*[#]*[[:space:]]*MENU MASTER PASSWD.*" $PXELINUX_DIR/$CONFILE | sed -e "s/^[[:space:]]*[#]*[[:space:]]*MENU MASTER PASSWD[[:space:]]*//g")" |
| 138 | } |
| 139 | |
| 140 | get_pxelinux_sha1passwd_in_label_block() { |
| 141 | local PXE_CONF_TMP="$1" # Use file name without path. Ex. default |
| 142 | local LAB="$2" |
| 143 | check_img_in_pxe_cfg $LAB $PXELINUX_DIR/$PXE_CONF_TMP |
| 144 | # MENU PASSWD for specified label block |
| 145 | lines=$(get_pxecfg_image_block $LAB $PXELINUX_DIR/$PXE_CONF_TMP) |
| 146 | begin_line=$(echo $lines | awk -F" " '{print $1}') |
| 147 | end_line=$(echo $lines | awk -F" " '{print $2}') |
| 148 | search_cmd="if ($begin_line..$end_line) {print}" |
| 149 | sha1_passwd="$(perl -n -e "$search_cmd" $PXELINUX_DIR/$PXE_CONF_TMP | grep -i "^[[:space:]]*[#]*[[:space:]]*MENU PASSWD.*" | sed -e "s/^[[:space:]]*[#]*[[:space:]]*MENU PASSWD[[:space:]]*//g")" |
| 150 | } |
| 151 | |
| 152 | get_new_master_passwd_or_old_master_sha1_passwd() { |
| 153 | local CONFILE="$1" |
| 154 | get_pxelinux_master_sha1passwd $CONFILE |
| 155 | if [ -z "$pxelinux_passwd" ]; then |
| 156 | if [ -z "$sha1_passwd" ]; then |
| 157 | # ask user the input |
| 158 | input_new_passwd |
| 159 | else |
| 160 | # confirm since old sha1_passwd exists |
| 161 | if [ -z "$overwrite_pxelinux_passwd" ]; then |
| 162 | echo "Previous password for pxelinux exists! Do you want to overwrite that ?" |
| 163 | echo -n "[y/N] " |
| 164 | read overwrite_pxelinux_passwd |
| 165 | fi |
| 166 | case "$overwrite_pxelinux_passwd" in |
| 167 | y|Y|[yY][eE][sS]) |
| 168 | input_new_passwd |
| 169 | ;; |
| 170 | *) |
| 171 | echo "We will use the old password." |
| 172 | ;; |
| 173 | esac |
| 174 | fi |
| 175 | else |
| 176 | # enter from stdin |
| 177 | new_passwd="$pxelinux_passwd" |
| 178 | fi |
| 179 | } |
| 180 | # |
| 181 | get_new_block_passwd_or_old_block_sha1_passwd() { |
| 182 | local CONFILE="$1" |
| 183 | local LAB="$2" |
| 184 | get_pxelinux_sha1passwd_in_label_block $CONFILE $LAB |
| 185 | if [ -z "$pxelinux_passwd" ]; then |
| 186 | if [ -z "$sha1_passwd" ]; then |
| 187 | # ask user the input |
| 188 | input_new_passwd |
| 189 | else |
| 190 | # confirm since old sha1_passwd exists |
| 191 | if [ -z "$overwrite_pxelinux_passwd" ]; then |
| 192 | echo "Previous PXELinux password for label block $LAB exists! Do you want to overwrite that ?" |
| 193 | echo -n "[y/N] " |
| 194 | read overwrite_pxelinux_passwd |
| 195 | fi |
| 196 | case "$overwrite_pxelinux_passwd" in |
| 197 | y|Y|[yY][eE][sS]) |
| 198 | input_new_passwd |
| 199 | ;; |
| 200 | *) |
| 201 | echo "We will use the old password." |
| 202 | ;; |
| 203 | esac |
| 204 | fi |
| 205 | else |
| 206 | # enter from stdin |
| 207 | new_passwd="$pxelinux_passwd" |
| 208 | fi |
| 209 | } |
| 210 | |
| 211 | disable_pxepasswd_in_config() { |
| 212 | local CONFILE="$1" |
| 213 | local labels="$2" |
| 214 | echo -n "Disabling PXE password in config file $PXELINUX_DIR/$CONFILE... " |
| 215 | perl -pi -e 's/^[[:space:]]*[#]*[[:space:]]*(timeout .*)/$1/gi' $PXELINUX_DIR/$CONFILE |
| 216 | perl -pi -e 's/^[[:space:]]*[#]*[[:space:]]*(MENU MASTER PASSWD.*)/# $1/gi' $PXELINUX_DIR/$CONFILE |
| 217 | if [ -z "$labels" ]; then |
| 218 | # set all label blocks. |
| 219 | perl -pi -e 's/^[[:space:]]*[#]*[[:space:]]*(MENU PASSWD.*)/ # $1/gi' $PXELINUX_DIR/$CONFILE |
| 220 | else |
| 221 | # set specific label blocks. |
| 222 | echo -n "For label block: " |
| 223 | for ib in $labels; do |
| 224 | echo -n "$ib " |
| 225 | switch_label_block_menu_passwd "$ib" off $PXELINUX_DIR/$CONFILE |
| 226 | done |
| 227 | fi |
| 228 | echo |
| 229 | } # end of disable_pxepasswd_in_config |
| 230 | # |
| 231 | enable_pxepasswd_in_config() { |
| 232 | local CONFILE="$1" |
| 233 | local PASSWD_STR="$2" |
| 234 | local LAB="$3" |
| 235 | echo -n "Enabling PXE password in config file $PXELINUX_DIR/$CONFILE " |
| 236 | perl -pi -e 's/^[[:space:]]*(timeout .*)/# $1/gi' $PXELINUX_DIR/$CONFILE |
| 237 | if [ -z "$LAB" ]; then |
| 238 | # set all label blocks. |
| 239 | perl -pi -e "s/^[[:space:]]*[#]*[[:space:]]*MENU MASTER PASSWD.*/MENU MASTER PASSWD $PASSWD_STR/gi" $PXELINUX_DIR/$CONFILE |
| 240 | perl -pi -e 's/^[[:space:]]*[#]*[[:space:]]*MENU PASSWD.*/ MENU PASSWD/gi' $PXELINUX_DIR/$CONFILE |
| 241 | else |
| 242 | # set specific label blocks. |
| 243 | echo -n "for label block: $LAB " |
| 244 | switch_label_block_menu_passwd "$LAB" on $PXELINUX_DIR/$CONFILE $PASSWD_STR |
| 245 | fi |
| 246 | echo |
| 247 | } # end of enable_pxepasswd_in_config |
| 248 | |
| 249 | enable_pxe_passwd() { |
| 250 | # |
| 251 | if [ -n "$specified_host" ]; then |
| 252 | PXE_CONF="$PXELINUX_DIR/default_skeleton" |
| 253 | cp -f $PXELINUX_DIR/default $PXE_CONF |
| 254 | # check every hosts |
| 255 | for ip in $specified_host; do |
| 256 | if [ ! -d "$drblroot/$ip" ]; then |
| 257 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 258 | echo "Warning! Can NOT find DRBL client $ip (i.e. no $drblroot/$ip)! Program terminated!" |
| 259 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 260 | fi |
| 261 | done |
| 262 | [ -n "$verbose" ] && echo "specified_host: $specified_host" |
| 263 | fi |
| 264 | |
| 265 | # set the host to be processed |
| 266 | # host_list is the IP address of client, like 192.168.1.1... |
| 267 | if [ -n "$specified_host" ]; then |
| 268 | # set the host path |
| 269 | for ip in $specified_host; do |
| 270 | hex_ip="$(drbl-gethostip $ip)" |
| 271 | echo "Enabling the pxelinux simple menu for DRBL client $ip..." |
| 272 | cp -f $PXE_CONF $PXELINUX_DIR/$hex_ip |
| 273 | if [ -z "$labels" ]; then |
| 274 | # master password |
| 275 | get_new_master_passwd_or_old_master_sha1_passwd $hex_ip |
| 276 | if [ -n "$new_passwd" ]; then |
| 277 | sha1_passwd="$(drbl-sha1pass $new_passwd $salt)" |
| 278 | fi |
| 279 | sha1_passwd_escape="$(echo "$sha1_passwd" | sed -e 's|\$|\\\$|g' -e 's|/|\\/|g')" |
| 280 | # comment the timeout |
| 281 | enable_pxepasswd_in_config $hex_ip $sha1_passwd_escape "$labels" |
| 282 | else |
| 283 | # password for every label block |
| 284 | for ib in $labels; do |
| 285 | get_new_block_passwd_or_old_block_sha1_passwd $hex_ip $ib |
| 286 | if [ -n "$new_passwd" ]; then |
| 287 | sha1_passwd="$(drbl-sha1pass $new_passwd $salt)" |
| 288 | fi |
| 289 | sha1_passwd_escape="$(echo "$sha1_passwd" | sed -e 's|\$|\\\$|g' -e 's|/|\\/|g')" |
| 290 | # comment the timeout |
| 291 | enable_pxepasswd_in_config $hex_ip $sha1_passwd_escape "$labels" |
| 292 | done |
| 293 | fi |
| 294 | done |
| 295 | else |
| 296 | # withoud specified_host, it must be all clients, create the default |
| 297 | echo "Enabling the default pxelinux simple menu for all DRBL clients..." |
| 298 | if [ -z "$labels" ]; then |
| 299 | # master password |
| 300 | get_new_master_passwd_or_old_master_sha1_passwd default |
| 301 | if [ -n "$new_passwd" ]; then |
| 302 | sha1_passwd="$(drbl-sha1pass $new_passwd $salt)" |
| 303 | fi |
| 304 | sha1_passwd_escape="$(echo "$sha1_passwd" | sed -e 's|\$|\\\$|g' -e 's|/|\\/|g')" |
| 305 | # comment the timeout |
| 306 | enable_pxepasswd_in_config default $sha1_passwd_escape |
| 307 | else |
| 308 | # password for every label block |
| 309 | for ib in $labels; do |
| 310 | echo "Now process label block $ib..." |
| 311 | get_new_block_passwd_or_old_block_sha1_passwd default $ib |
| 312 | if [ -n "$new_passwd" ]; then |
| 313 | sha1_passwd="$(drbl-sha1pass $new_passwd $salt)" |
| 314 | fi |
| 315 | sha1_passwd_escape="$(echo "$sha1_passwd" | sed -e 's|\$|\\\$|g' -e 's|/|\\/|g')" |
| 316 | # comment the timeout |
| 317 | enable_pxepasswd_in_config default $sha1_passwd_escape $ib |
| 318 | done |
| 319 | fi |
| 320 | fi |
| 321 | } # end of enable_pxe_passwd |
| 322 | # |
| 323 | disable_pxe_passwd() { |
| 324 | # set the host to be processed |
| 325 | # host_list is the IP address of client, like 192.168.1.1... |
| 326 | if [ -n "$specified_host" ]; then |
| 327 | # set the host path |
| 328 | for ip in $specified_host; do |
| 329 | hex_ip="$(drbl-gethostip $ip)" |
| 330 | echo "Disabling the password in gxelinux simple menu for client $ip... " |
| 331 | cp -f $PXE_CONF $PXELINUX_DIR/$hex_ip |
| 332 | # comment the timeout |
| 333 | disable_pxepasswd_in_config $hex_ip "$labels" |
| 334 | done |
| 335 | else |
| 336 | # withoud specified_host, it must be all clients, create the default |
| 337 | echo "Disable the password in pxelinux simple menu for all clients... " |
| 338 | # comment the timeout |
| 339 | disable_pxepasswd_in_config default "$labels" |
| 340 | # TODO: clean all hexip and mac config ? |
| 341 | if [ -n "$PXELINUX_DIR" ]; then |
| 342 | find $PXELINUX_DIR/ -maxdepth 1 ! -name "default" -type f -exec rm -f {} \; |
| 343 | fi |
| 344 | fi |
| 345 | } # end of disable_pxe_passwd |
| 346 | |
| 347 | case "$mode" in |
| 348 | "disable") |
| 349 | disable_pxe_passwd |
| 350 | ;; |
| 351 | "enable") |
| 352 | enable_pxe_passwd |
| 353 | ;; |
| 354 | esac |
| 355 | echo "done!" |
| 356 | exit 0 |