| 1 | #!/bin/bashscip-shell filesystem . . `/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 | usagescip-shell shell shell-project 0.0.0 usage().() { |
| 15 | echoscip-shell system . . echo. "Usage:" |
| 16 | echoscip-shell system . . echo. "To set the pxelinux password for clients:" |
| 17 | echoscip-shell system . . echo. "`basenamescip-shell system . . basename. $0` [OPTION]" |
| 18 | echoscip-shell system . . echo. "Options:" |
| 19 | echoscip-shell system . . echo. "--stdin PASSWORD set the root's password for clients as PASSWORD" |
| 20 | echoscip-shell system . . echo. "-h, --host IP_ADDRESS: set only for the host with IP_ADDRESS instead of all DRBL clients" |
| 21 | echoscip-shell system . . echo. "-d, --disable disable the menu password" |
| 22 | echoscip-shell system . . echo. "-e, --enable enable the menu password" |
| 23 | echoscip-shell system . . echo. "-o, --overwrite-pxelinux-passwd [y/n] when a PXELinux password already exists in config file, overwrite it or not." |
| 24 | echoscip-shell system . . echo. "-b, --label LABEL enable/disable the LABEL block only in PXELinux config file" |
| 25 | echoscip-shell system . . echo. "-v, --verbose prints out verbose information" |
| 26 | echoscip-shell system . . echo. "Example:" |
| 27 | echoscip-shell system . . echo. "To assign the PXELinux password for all label blocks, run" |
| 28 | echoscip-shell system . . echo. "`basenamescip-shell system . . basename. $0` -e" |
| 29 | echoscip-shell system . . echo. "To assign the PXELinux password for menu label \"drbl\" and \"local\" only, run" |
| 30 | echoscip-shell system . . echo. "`basenamescip-shell system . . basename. $0` -e -b \"drbl local\"" |
| 31 | echoscip-shell system . . echo. "To disable the authentication mechanism, run" |
| 32 | echoscip-shell system . . echo. "`basenamescip-shell system . . basename. $0` -d" |
| 33 | } |
| 34 | |
| 35 | # |
| 36 | check_if_root |
| 37 | |
| 38 | # main |
| 39 | unalias ls 2>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 40 | |
| 41 | specified_host='' |
| 42 | # Parse command-line options |
| 43 | while [scip-shell system . . `[`. $# -gt 0 ]; do |
| 44 | case "$1" in |
| 45 | --stdin) |
| 46 | shift; |
| 47 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . 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 [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . 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 [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . 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 | -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2 |
| 81 | usagescip-shell shell shell-project 0.0.0 usage(). >& 2 |
| 82 | exit 2 ;; |
| 83 | *) break ;; |
| 84 | esac |
| 85 | done |
| 86 | |
| 87 | if [scip-shell system . . `[`. -z "$mode" ]; then |
| 88 | usagescip-shell shell shell-project 0.0.0 usage(). |
| 89 | exit 1 |
| 90 | fi |
| 91 | |
| 92 | # |
| 93 | switch_label_block_menu_passwdscip-shell shell shell-project 0.0.0 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=$(echoscip-shell system . . echo. $lines | awkscip-shell system . . awk. -F" " '{print $1}') |
| 102 | end_line=$(echoscip-shell system . . echo. $lines | awkscip-shell system . . awk. -F" " '{print $2}') |
| 103 | case "$ACT" in |
| 104 | "on") |
| 105 | [scip-shell system . . `[`. -n "$VERBOSE" ] && echoscip-shell system . . 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 | [scip-shell system . . `[`. -n "$VERBOSE" ] && echoscip-shell system . . 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 | perlscip-shell system . . perl. -pi -e "$sub_act_cmd" $PXE_CONF_TMP |
| 114 | } # end of switch_label_block_menu_passwd |
| 115 | # |
| 116 | input_new_passwdscip-shell shell shell-project 0.0.0 input_new_passwd().() { |
| 117 | # interactive |
| 118 | echoscip-shell system . . echo. "New password: (It will not be echoed in the screen)" |
| 119 | read -s pass1 |
| 120 | echoscip-shell system . . echo. "Retype new password: (It will not be echoed in the screen)" |
| 121 | read -s pass2 |
| 122 | while [scip-shell system . . `[`. "$pass1" != "$pass2" ]; do |
| 123 | echoscip-shell system . . echo. "Sorry, passwords do not match" |
| 124 | echoscip-shell system . . echo. "New password: (It will not be echoed in the screen)" |
| 125 | read -s pass1 |
| 126 | echoscip-shell system . . echo. "Retype new password: (It will not be echoed in the screen)" |
| 127 | read -s pass2 |
| 128 | done |
| 129 | # |
| 130 | [scip-shell system . . `[`. -z "$pass1" ] && echoscip-shell system . . 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_sha1passwdscip-shell shell shell-project 0.0.0 get_pxelinux_master_sha1passwd().() { |
| 136 | local CONFILE="$1" |
| 137 | sha1_passwd="$(grepscip-shell system . . grep. -i "^[[:space:]]*[#]*[[:space:]]*MENU MASTER PASSWD.*" $PXELINUX_DIR/$CONFILE | sedscip-shell system . . sed. -e "s/^[[:space:]]*[#]*[[:space:]]*MENU MASTER PASSWD[[:space:]]*//g")" |
| 138 | } |
| 139 | |
| 140 | get_pxelinux_sha1passwd_in_label_blockscip-shell shell shell-project 0.0.0 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=$(echoscip-shell system . . echo. $lines | awkscip-shell system . . awk. -F" " '{print $1}') |
| 147 | end_line=$(echoscip-shell system . . echo. $lines | awkscip-shell system . . awk. -F" " '{print $2}') |
| 148 | search_cmd="if ($begin_line..$end_line) {print}" |
| 149 | sha1_passwd="$(perlscip-shell system . . perl. -n -e "$search_cmd" $PXELINUX_DIR/$PXE_CONF_TMP | grepscip-shell system . . grep. -i "^[[:space:]]*[#]*[[:space:]]*MENU PASSWD.*" | sedscip-shell system . . sed. -e "s/^[[:space:]]*[#]*[[:space:]]*MENU PASSWD[[:space:]]*//g")" |
| 150 | } |
| 151 | |
| 152 | get_new_master_passwd_or_old_master_sha1_passwdscip-shell shell shell-project 0.0.0 get_new_master_passwd_or_old_master_sha1_passwd().() { |
| 153 | local CONFILE="$1" |
| 154 | get_pxelinux_master_sha1passwdscip-shell shell shell-project 0.0.0 get_pxelinux_master_sha1passwd(). $CONFILE |
| 155 | if [scip-shell system . . `[`. -z "$pxelinux_passwd" ]; then |
| 156 | if [scip-shell system . . `[`. -z "$sha1_passwd" ]; then |
| 157 | # ask user the input |
| 158 | input_new_passwdscip-shell shell shell-project 0.0.0 input_new_passwd(). |
| 159 | else |
| 160 | # confirm since old sha1_passwd exists |
| 161 | if [scip-shell system . . `[`. -z "$overwrite_pxelinux_passwd" ]; then |
| 162 | echoscip-shell system . . echo. "Previous password for pxelinux exists! Do you want to overwrite that ?" |
| 163 | echoscip-shell system . . 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_passwdscip-shell shell shell-project 0.0.0 input_new_passwd(). |
| 169 | ;; |
| 170 | *) |
| 171 | echoscip-shell system . . 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_passwdscip-shell shell shell-project 0.0.0 get_new_block_passwd_or_old_block_sha1_passwd().() { |
| 182 | local CONFILE="$1" |
| 183 | local LAB="$2" |
| 184 | get_pxelinux_sha1passwd_in_label_blockscip-shell shell shell-project 0.0.0 get_pxelinux_sha1passwd_in_label_block(). $CONFILE $LAB |
| 185 | if [scip-shell system . . `[`. -z "$pxelinux_passwd" ]; then |
| 186 | if [scip-shell system . . `[`. -z "$sha1_passwd" ]; then |
| 187 | # ask user the input |
| 188 | input_new_passwdscip-shell shell shell-project 0.0.0 input_new_passwd(). |
| 189 | else |
| 190 | # confirm since old sha1_passwd exists |
| 191 | if [scip-shell system . . `[`. -z "$overwrite_pxelinux_passwd" ]; then |
| 192 | echoscip-shell system . . echo. "Previous PXELinux password for label block $LAB exists! Do you want to overwrite that ?" |
| 193 | echoscip-shell system . . 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_passwdscip-shell shell shell-project 0.0.0 input_new_passwd(). |
| 199 | ;; |
| 200 | *) |
| 201 | echoscip-shell system . . 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_configscip-shell shell shell-project 0.0.0 disable_pxepasswd_in_config().() { |
| 212 | local CONFILE="$1" |
| 213 | local labels="$2" |
| 214 | echoscip-shell system . . echo. -n "Disabling PXE password in config file $PXELINUX_DIR/$CONFILE... " |
| 215 | perlscip-shell system . . perl. -pi -e 's/^[[:space:]]*[#]*[[:space:]]*(timeout .*)/$1/gi' $PXELINUX_DIR/$CONFILE |
| 216 | perlscip-shell system . . perl. -pi -e 's/^[[:space:]]*[#]*[[:space:]]*(MENU MASTER PASSWD.*)/# $1/gi' $PXELINUX_DIR/$CONFILE |
| 217 | if [scip-shell system . . `[`. -z "$labels" ]; then |
| 218 | # set all label blocks. |
| 219 | perlscip-shell system . . perl. -pi -e 's/^[[:space:]]*[#]*[[:space:]]*(MENU PASSWD.*)/ # $1/gi' $PXELINUX_DIR/$CONFILE |
| 220 | else |
| 221 | # set specific label blocks. |
| 222 | echoscip-shell system . . echo. -n "For label block: " |
| 223 | for ib in $labels; do |
| 224 | echoscip-shell system . . echo. -n "$ib " |
| 225 | switch_label_block_menu_passwdscip-shell shell shell-project 0.0.0 switch_label_block_menu_passwd(). "$ib" off $PXELINUX_DIR/$CONFILE |
| 226 | done |
| 227 | fi |
| 228 | echoscip-shell system . . echo. |
| 229 | } # end of disable_pxepasswd_in_config |
| 230 | # |
| 231 | enable_pxepasswd_in_configscip-shell shell shell-project 0.0.0 enable_pxepasswd_in_config().() { |
| 232 | local CONFILE="$1" |
| 233 | local PASSWD_STR="$2" |
| 234 | local LAB="$3" |
| 235 | echoscip-shell system . . echo. -n "Enabling PXE password in config file $PXELINUX_DIR/$CONFILE " |
| 236 | perlscip-shell system . . perl. -pi -e 's/^[[:space:]]*(timeout .*)/# $1/gi' $PXELINUX_DIR/$CONFILE |
| 237 | if [scip-shell system . . `[`. -z "$LAB" ]; then |
| 238 | # set all label blocks. |
| 239 | perlscip-shell system . . perl. -pi -e "s/^[[:space:]]*[#]*[[:space:]]*MENU MASTER PASSWD.*/MENU MASTER PASSWD $PASSWD_STR/gi" $PXELINUX_DIR/$CONFILE |
| 240 | perlscip-shell system . . perl. -pi -e 's/^[[:space:]]*[#]*[[:space:]]*MENU PASSWD.*/ MENU PASSWD/gi' $PXELINUX_DIR/$CONFILE |
| 241 | else |
| 242 | # set specific label blocks. |
| 243 | echoscip-shell system . . echo. -n "for label block: $LAB " |
| 244 | switch_label_block_menu_passwdscip-shell shell shell-project 0.0.0 switch_label_block_menu_passwd(). "$LAB" on $PXELINUX_DIR/$CONFILE $PASSWD_STR |
| 245 | fi |
| 246 | echoscip-shell system . . echo. |
| 247 | } # end of enable_pxepasswd_in_config |
| 248 | |
| 249 | enable_pxe_passwdscip-shell shell shell-project 0.0.0 enable_pxe_passwd().() { |
| 250 | # |
| 251 | if [scip-shell system . . `[`. -n "$specified_host" ]; then |
| 252 | PXE_CONF="$PXELINUX_DIR/default_skeleton" |
| 253 | cpscip-shell system . . cp. -f $PXELINUX_DIR/default $PXE_CONF |
| 254 | # check every hosts |
| 255 | for ip in $specified_host; do |
| 256 | if [scip-shell system . . `[`. ! -d "$drblroot/$ip" ]; then |
| 257 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 258 | echoscip-shell system . . echo. "Warning! Can NOT find DRBL client $ip (i.e. no $drblroot/$ip)! Program terminated!" |
| 259 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 260 | fi |
| 261 | done |
| 262 | [scip-shell system . . `[`. -n "$verbose" ] && echoscip-shell system . . 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 [scip-shell system . . `[`. -n "$specified_host" ]; then |
| 268 | # set the host path |
| 269 | for ip in $specified_host; do |
| 270 | hex_ip="$(drbl-gethostip $ip)" |
| 271 | echoscip-shell system . . echo. "Enabling the pxelinux simple menu for DRBL client $ip..." |
| 272 | cpscip-shell system . . cp. -f $PXE_CONF $PXELINUX_DIR/$hex_ip |
| 273 | if [scip-shell system . . `[`. -z "$labels" ]; then |
| 274 | # master password |
| 275 | get_new_master_passwd_or_old_master_sha1_passwdscip-shell shell shell-project 0.0.0 get_new_master_passwd_or_old_master_sha1_passwd(). $hex_ip |
| 276 | if [scip-shell system . . `[`. -n "$new_passwd" ]; then |
| 277 | sha1_passwd="$(drbl-sha1pass $new_passwd $salt)" |
| 278 | fi |
| 279 | sha1_passwd_escape="$(echoscip-shell system . . echo. "$sha1_passwd" | sedscip-shell system . . sed. -e 's|\$|\\\$|g' -e 's|/|\\/|g')" |
| 280 | # comment the timeout |
| 281 | enable_pxepasswd_in_configscip-shell shell shell-project 0.0.0 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_passwdscip-shell shell shell-project 0.0.0 get_new_block_passwd_or_old_block_sha1_passwd(). $hex_ip $ib |
| 286 | if [scip-shell system . . `[`. -n "$new_passwd" ]; then |
| 287 | sha1_passwd="$(drbl-sha1pass $new_passwd $salt)" |
| 288 | fi |
| 289 | sha1_passwd_escape="$(echoscip-shell system . . echo. "$sha1_passwd" | sedscip-shell system . . sed. -e 's|\$|\\\$|g' -e 's|/|\\/|g')" |
| 290 | # comment the timeout |
| 291 | enable_pxepasswd_in_configscip-shell shell shell-project 0.0.0 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 | echoscip-shell system . . echo. "Enabling the default pxelinux simple menu for all DRBL clients..." |
| 298 | if [scip-shell system . . `[`. -z "$labels" ]; then |
| 299 | # master password |
| 300 | get_new_master_passwd_or_old_master_sha1_passwdscip-shell shell shell-project 0.0.0 get_new_master_passwd_or_old_master_sha1_passwd(). default |
| 301 | if [scip-shell system . . `[`. -n "$new_passwd" ]; then |
| 302 | sha1_passwd="$(drbl-sha1pass $new_passwd $salt)" |
| 303 | fi |
| 304 | sha1_passwd_escape="$(echoscip-shell system . . echo. "$sha1_passwd" | sedscip-shell system . . sed. -e 's|\$|\\\$|g' -e 's|/|\\/|g')" |
| 305 | # comment the timeout |
| 306 | enable_pxepasswd_in_configscip-shell shell shell-project 0.0.0 enable_pxepasswd_in_config(). default $sha1_passwd_escape |
| 307 | else |
| 308 | # password for every label block |
| 309 | for ib in $labels; do |
| 310 | echoscip-shell system . . echo. "Now process label block $ib..." |
| 311 | get_new_block_passwd_or_old_block_sha1_passwdscip-shell shell shell-project 0.0.0 get_new_block_passwd_or_old_block_sha1_passwd(). default $ib |
| 312 | if [scip-shell system . . `[`. -n "$new_passwd" ]; then |
| 313 | sha1_passwd="$(drbl-sha1pass $new_passwd $salt)" |
| 314 | fi |
| 315 | sha1_passwd_escape="$(echoscip-shell system . . echo. "$sha1_passwd" | sedscip-shell system . . sed. -e 's|\$|\\\$|g' -e 's|/|\\/|g')" |
| 316 | # comment the timeout |
| 317 | enable_pxepasswd_in_configscip-shell shell shell-project 0.0.0 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_passwdscip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. -n "$specified_host" ]; then |
| 327 | # set the host path |
| 328 | for ip in $specified_host; do |
| 329 | hex_ip="$(drbl-gethostip $ip)" |
| 330 | echoscip-shell system . . echo. "Disabling the password in gxelinux simple menu for client $ip... " |
| 331 | cpscip-shell system . . cp. -f $PXE_CONF $PXELINUX_DIR/$hex_ip |
| 332 | # comment the timeout |
| 333 | disable_pxepasswd_in_configscip-shell shell shell-project 0.0.0 disable_pxepasswd_in_config(). $hex_ip "$labels" |
| 334 | done |
| 335 | else |
| 336 | # withoud specified_host, it must be all clients, create the default |
| 337 | echoscip-shell system . . echo. "Disable the password in pxelinux simple menu for all clients... " |
| 338 | # comment the timeout |
| 339 | disable_pxepasswd_in_configscip-shell shell shell-project 0.0.0 disable_pxepasswd_in_config(). default "$labels" |
| 340 | # TODO: clean all hexip and mac config ? |
| 341 | if [scip-shell system . . `[`. -n "$PXELINUX_DIR" ]; then |
| 342 | findscip-shell system . . 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_passwdscip-shell shell shell-project 0.0.0 disable_pxe_passwd(). |
| 350 | ;; |
| 351 | "enable") |
| 352 | enable_pxe_passwdscip-shell shell shell-project 0.0.0 enable_pxe_passwd(). |
| 353 | ;; |
| 354 | esac |
| 355 | echoscip-shell system . . echo. "done!" |
| 356 | exit 0 |