Thu, 24 Jan 2008 15:05:45 +0800
Fixed output in die()
die() was using too many newlines after re-formatting
functions to fit inside 80 columns. The output was
confusing, now fixed.
Signed-off-by: Tim Post (tinkertim@gmail.com)
| tinkertim@12 | 1 | # Functions used by install.sh, also shared by setup. |
| tinkertim@0 | 2 | |
| tinkertim@0 | 3 | # Ensure that we exit properly, paying attention to --force |
| tinkertim@7 | 4 | # Syntax: safe_exit EXIT_SUCCESS or EXIT_FAILURE, --force will over-ride |
| tinkertim@10 | 5 | # safe_exit $EXIT_SUCCESS or $EXIT_FAILURE, exits regardless of --force |
| tinkertim@0 | 6 | safe_exit() |
| tinkertim@0 | 7 | { |
| tinkertim@0 | 8 | local mode="$1" |
| tinkertim@0 | 9 | |
| tinkertim@0 | 10 | # If called with EXIT_FAILURE and $FORCE is true, we don't exit. |
| tinkertim@0 | 11 | # If called with $EXIT_FAILURE, we exit no matter the value of $FORCE |
| tinkertim@0 | 12 | # Simple work-around so that we can die if we really need to, even if |
| tinkertim@10 | 13 | # --force was passed. Note, EXIT_FAILURE and $EXIT_FAILURE are different |
| tinkertim@0 | 14 | |
| tinkertim@0 | 15 | # TODO: Make me sain with a simple EXIT_FORCE macro, duh! |
| tinkertim@0 | 16 | |
| tinkertim@0 | 17 | [ -z "$EXIT_FAILURE" ] || [ "$EXIT_FAILURE" = "" ] && EXIT_FAILURE="1" |
| tinkertim@0 | 18 | |
| tinkertim@0 | 19 | [ "$FORCE" = "yes" ] && [ "$mode" != "$EXIT_FAILURE" ] && return 0 |
| tinkertim@0 | 20 | |
| tinkertim@8 | 21 | [ -z "$mode" ] && safe_exit EXIT_SUCCESS |
| tinkertim@0 | 22 | |
| tinkertim@0 | 23 | case $mode in |
| tinkertim@0 | 24 | 0|EXIT_SUCCESS) |
| tinkertim@0 | 25 | # if EXIT_SUCCESS undefined, just exit and rely on the shell |
| tinkertim@0 | 26 | # to invoke the proper EXIT_ macro |
| tinkertim@0 | 27 | [ -z "$EXIT_SUCCESS" ] || [ "$EXIT_SUCCESS" = "" ] && { |
| tinkertim@0 | 28 | exit |
| tinkertim@0 | 29 | } |
| tinkertim@0 | 30 | exit $EXIT_SUCCESS |
| tinkertim@0 | 31 | ;; |
| tinkertim@0 | 32 | *) |
| tinkertim@0 | 33 | # Exit with whatever EXIT_FAILURE is defined as, or with |
| tinkertim@0 | 34 | # whatever argument was passed to safe_exit() |
| tinkertim@0 | 35 | [ $mode = "EXIT_FAILURE" ] && exit $EXIT_FAILURE |
| tinkertim@0 | 36 | exit $mode |
| tinkertim@0 | 37 | ;; |
| tinkertim@0 | 38 | |
| tinkertim@0 | 39 | esac |
| tinkertim@0 | 40 | } |
| tinkertim@0 | 41 | |
| tinkertim@0 | 42 | # 99 ways to die. |
| tinkertim@7 | 43 | # Syntax: die [reason] [why] |
| tinkertim@7 | 44 | # Reason can be one of the pre-defined causes, specifying "why", or just |
| tinkertim@7 | 45 | # die "reason" |
| tinkertim@7 | 46 | # Syntax: die prog "gcc" -> Complains that the program gcc can not be found |
| tinkertim@0 | 47 | die() |
| tinkertim@0 | 48 | { |
| tinkertim@0 | 49 | local mode=$1 |
| tinkertim@0 | 50 | local why=$2 |
| tinkertim@0 | 51 | |
| tinkertim@0 | 52 | case $mode in |
| tinkertim@0 | 53 | prog) |
| tinkertim@10 | 54 | printf "\nThe program %s could not be found in your path\n" \ |
| tinkertim@10 | 55 | "$why" |
| tinkertim@17 | 56 | printf "Please install '%s' (or ensure its in your path) " \ |
| tinkertim@10 | 57 | "$why" |
| tinkertim@0 | 58 | printf "and try again.\n" |
| tinkertim@17 | 59 | printf "If you are running this as an ordinary user, make sure " |
| tinkertim@17 | 60 | printf "that /sbin and /usr/sbin\nare in your path.\n" |
| tinkertim@0 | 61 | logg "FATAL: Could not find the program $why" |
| tinkertim@0 | 62 | safe_exit EXIT_FAILURE |
| tinkertim@0 | 63 | ;; |
| tinkertim@0 | 64 | lib) |
| tinkertim@0 | 65 | printf "\nThe library %s could not be located.\n" "$why" |
| tinkertim@0 | 66 | printf "Please install the library '%s' and try again.\n" "$why" |
| tinkertim@0 | 67 | logg "FATAL: Could not find the library $why" |
| tinkertim@0 | 68 | safe_exit EXIT_FAILURE |
| tinkertim@0 | 69 | ;; |
| tinkertim@0 | 70 | evar) |
| tinkertim@0 | 71 | printf "\nThe environmental variable %s is not set.\n" "$why" |
| tinkertim@0 | 72 | printf "Please set %s properly and try again.\n" "$why" |
| tinkertim@0 | 73 | logg "FATAL: env setting $why is null or undefined." |
| tinkertim@0 | 74 | safe_exit EXIT_FAILURE |
| tinkertim@0 | 75 | ;; |
| tinkertim@0 | 76 | err) |
| tinkertim@0 | 77 | printf "\n%s\n" "$why" |
| tinkertim@0 | 78 | logg "FATAL: $why" |
| tinkertim@0 | 79 | safe_exit EXIT_FAILURE |
| tinkertim@0 | 80 | ;; |
| tinkertim@0 | 81 | arch) |
| tinkertim@10 | 82 | printf "\nYour architecture, %s, is not supported by %s.\n" \ |
| tinkertim@10 | 83 | "$try" "$PROGNAME" |
| tinkertim@10 | 84 | printf "You can contact %s to see if this will change soon.\n" \ |
| tinkertim@10 | 85 | "$BUGMAIL" |
| tinkertim@0 | 86 | logg "FATAL: Arch $why is not supported." |
| tinkertim@0 | 87 | safe_exit EXIT_FAILURE |
| tinkertim@0 | 88 | ;; |
| tinkertim@0 | 89 | kernel) |
| tinkertim@10 | 90 | printf "\nYour kernel, version %s, is not supported by %s.\n" \ |
| tinkertim@10 | 91 | "$why" "$PROGNAME" |
| tinkertim@10 | 92 | printf "You can contact %s to see if this will change soon.\n" \ |
| tinkertim@10 | 93 | "$BUGMAIL" |
| tinkertim@0 | 94 | logg "FATAL: Kernel $why is not supported." |
| tinkertim@0 | 95 | safe_exit EXIT_FAILURE |
| tinkertim@0 | 96 | ;; |
| tinkertim@0 | 97 | internal) |
| tinkertim@10 | 98 | printf "\nThe POSIX invocation of your shell lacks the %s " |
| tinkertim@10 | 99 | printf "built-in.\n" "$why" |
| tinkertim@10 | 100 | printf "Please link /bin/sh to something supporting %s\n" "$why" |
| tinkertim@0 | 101 | logg "FATAL: Internal shell error, $why doesn't seem to work." |
| tinkertim@0 | 102 | safe_exit EXIT_FAILURE |
| tinkertim@0 | 103 | ;; |
| tinkertim@0 | 104 | package) |
| tinkertim@10 | 105 | printf "\nI could not install the package %s with %s.\n" \ |
| tinkertim@10 | 106 | "$why" "$DIST_PACKAGER" |
| tinkertim@10 | 107 | printf "Perhaps missing sources in your packager config?\n" |
| tinkertim@0 | 108 | logg "FATAL: Could not install the mandatory package '$why'" |
| tinkertim@0 | 109 | safe_exit EXIT_FAILURE |
| tinkertim@0 | 110 | ;; |
| tinkertim@0 | 111 | *) |
| tinkertim@0 | 112 | printf "\n%s\n" "$*" |
| tinkertim@0 | 113 | logg "FATAL: $*" |
| tinkertim@0 | 114 | safe_exit EXIT_FAILURE |
| tinkertim@0 | 115 | ;; |
| tinkertim@0 | 116 | esac |
| tinkertim@0 | 117 | } |
| tinkertim@0 | 118 | |
| tinkertim@0 | 119 | # returns non-zero if uid !=0 |
| tinkertim@7 | 120 | # Syntax: check_root || die "You must be root to run this program" |
| tinkertim@0 | 121 | check_root() |
| tinkertim@0 | 122 | { |
| tinkertim@0 | 123 | [ $(id -u) = 0 ] || return 1 |
| tinkertim@0 | 124 | return 0 |
| tinkertim@0 | 125 | } |
| tinkertim@0 | 126 | |
| tinkertim@7 | 127 | # Loggs $* (all arguments) to the defined logfile, if defined. |
| tinkertim@7 | 128 | # Syntax: logg Do re me fa so la ti do |
| tinkertim@0 | 129 | logg() |
| tinkertim@0 | 130 | { |
| tinkertim@0 | 131 | local str="$*" |
| tinkertim@0 | 132 | |
| tinkertim@0 | 133 | [ -z $INSTALL_LOG ] || [ "$INSTALL_LOG" = "" ] && return 0 |
| tinkertim@0 | 134 | |
| tinkertim@0 | 135 | local stamp=$(date +%s) |
| tinkertim@0 | 136 | |
| tinkertim@0 | 137 | printf "%d %s\n" "$stamp" "$str" >> $INSTALL_LOG |
| tinkertim@0 | 138 | } |
| tinkertim@0 | 139 | |
| tinkertim@0 | 140 | # Writes a newline to the logfile |
| tinkertim@0 | 141 | logg_nl() |
| tinkertim@0 | 142 | { |
| tinkertim@0 | 143 | [ -z $INSTALL_LOG ] || [ "$INSTALL_LOG" = "" ] && return 0 |
| tinkertim@0 | 144 | |
| tinkertim@0 | 145 | printf "\n" >> $INSTALL_LOG |
| tinkertim@0 | 146 | } |
| tinkertim@0 | 147 | |
| tinkertim@6 | 148 | # Much like check_prog, however sets PROGNAME=/path/to/progname |
| tinkertim@6 | 149 | # as a global (r/w) variable. (does not write to paths.in) |
| tinkertim@7 | 150 | # Syntax: check_prog_export "progname" , or more typical |
| tinkertim@7 | 151 | # check_prog_export "progname" || handle_failure_somehow |
| tinkertim@6 | 152 | check_prog_export() |
| tinkertim@0 | 153 | { |
| tinkertim@0 | 154 | local target="$1" |
| tinkertim@0 | 155 | local dir="" |
| tinkertim@6 | 156 | local prog="" |
| tinkertim@0 | 157 | |
| tinkertim@0 | 158 | printf "Checking for the program %s ... " "$target" |
| tinkertim@9 | 159 | for dir in $(echo $PATH | $TR -d '\n' | $TR ':' ' '); do |
| tinkertim@0 | 160 | [ -x ${dir}/${target} ] && { |
| tinkertim@0 | 161 | printf "${dir}/${target} [ok]\n" |
| tinkertim@6 | 162 | prog="${dir}/${target}" |
| tinkertim@11 | 163 | target="$(echo $target | $TR 'a-z' 'A-Z' | \ |
| tinkertim@14 | 164 | $TR '\-' '_' | $TR '\.' '_')" |
| tinkertim@6 | 165 | export $target="$prog" |
| tinkertim@0 | 166 | return 0 |
| tinkertim@0 | 167 | } |
| tinkertim@0 | 168 | done |
| tinkertim@0 | 169 | printf "[no]\n" |
| tinkertim@0 | 170 | return 1 |
| tinkertim@0 | 171 | } |
| tinkertim@0 | 172 | |
| tinkertim@7 | 173 | # "Smart" init updater, calls the appropriate tool |
| tinkertim@7 | 174 | # Syntax: update_init initscript on/off |
| tinkertim@0 | 175 | update_init() |
| tinkertim@0 | 176 | { |
| tinkertim@0 | 177 | local prog="$1" |
| tinkertim@0 | 178 | local mode="$2" |
| tinkertim@0 | 179 | local retval=0 |
| tinkertim@0 | 180 | |
| tinkertim@0 | 181 | case $mode in |
| tinkertim@0 | 182 | on|start) |
| tinkertim@0 | 183 | printf "Setting %s to start on boot ... " "$prog" |
| tinkertim@0 | 184 | case $DIST_INITCTL in |
| tinkertim@0 | 185 | chkconfig) |
| tinkertim@0 | 186 | $CHKCONFIG $prog on >/dev/null 2>&1 |
| tinkertim@0 | 187 | retval=$? |
| tinkertim@0 | 188 | ;; |
| tinkertim@0 | 189 | update-rc.d) |
| tinkertim@0 | 190 | $UPDATE_RC_D $prog defaults >/dev/null 2>&1 |
| tinkertim@0 | 191 | retval=$? |
| tinkertim@0 | 192 | ;; |
| tinkertim@0 | 193 | *) |
| tinkertim@0 | 194 | retval=1 |
| tinkertim@0 | 195 | ;; |
| tinkertim@0 | 196 | esac |
| tinkertim@0 | 197 | ;; |
| tinkertim@0 | 198 | off|stop) |
| tinkertim@0 | 199 | printf "Setting %s to NOT start on boot ... " "$prog" |
| tinkertim@0 | 200 | case $DIST_INITCTL in |
| tinkertim@0 | 201 | chkconfig) |
| tinkertim@0 | 202 | $CHKCONFIG $prog off >/dev/null 2>&1 |
| tinkertim@0 | 203 | retval=$? |
| tinkertim@0 | 204 | ;; |
| tinkertim@0 | 205 | update-rc.d) |
| tinkertim@0 | 206 | $UPDATE_RC_D -f $prog remove >/dev/null 2>&1 |
| tinkertim@0 | 207 | retval=$? |
| tinkertim@0 | 208 | ;; |
| tinkertim@0 | 209 | *) |
| tinkertim@0 | 210 | retval=1 |
| tinkertim@0 | 211 | ;; |
| tinkertim@0 | 212 | esac |
| tinkertim@0 | 213 | ;; |
| tinkertim@0 | 214 | *) |
| tinkertim@0 | 215 | retval=1 |
| tinkertim@0 | 216 | ;; |
| tinkertim@0 | 217 | esac |
| tinkertim@0 | 218 | [ $retval = 0 ] && { |
| tinkertim@0 | 219 | printf "[ok]\n" |
| tinkertim@0 | 220 | logg "Modified init, $prog set to $mode" |
| tinkertim@0 | 221 | return 0 |
| tinkertim@0 | 222 | } |
| tinkertim@0 | 223 | printf "[failed]\n" |
| tinkertim@0 | 224 | logg "Could not modify init settings for $prog - attempted $mode" |
| tinkertim@0 | 225 | return $retval |
| tinkertim@0 | 226 | } |
| tinkertim@0 | 227 | |
| tinkertim@7 | 228 | # Generates a PEM encoded self signed certificate |
| tinkertim@7 | 229 | # Syntax: generate_pem /path/to/cert.pem |
| tinkertim@7 | 230 | # returns non-zero on failure. |
| tinkertim@6 | 231 | generate_pem() |
| tinkertim@6 | 232 | { |
| tinkertim@6 | 233 | # Country Name (2 letter code) [AU]: |
| tinkertim@6 | 234 | local A="." |
| tinkertim@6 | 235 | # State or Province Name (full name) [Some-State]: |
| tinkertim@6 | 236 | local B="." |
| tinkertim@6 | 237 | # Locality Name (eg, city) []: |
| tinkertim@6 | 238 | local C="." |
| tinkertim@6 | 239 | # Organization Name (eg, company) [Internet Widgits Pty Ltd]: |
| tinkertim@6 | 240 | local D="$(hostname)" |
| tinkertim@6 | 241 | # Organizational Unit Name (eg, section) []: |
| tinkertim@6 | 242 | local E="User Services" |
| tinkertim@6 | 243 | # Common Name (eg, YOUR name) []: |
| tinkertim@6 | 244 | local F="GNUPanel" |
| tinkertim@6 | 245 | # Email Address []: |
| tinkertim@6 | 246 | local G="ssl@$(hostname)" |
| tinkertim@6 | 247 | |
| tinkertim@6 | 248 | local target=$1 |
| tinkertim@6 | 249 | |
| tinkertim@6 | 250 | [ -z "$OPENSSL" ] && { |
| tinkertim@6 | 251 | check_prog_export "openssl" || { |
| tinkertim@10 | 252 | printf "\nOpenSSL could not be found, can't generate %s\n" \ |
| tinkertim@10 | 253 | "$target" |
| tinkertim@6 | 254 | return 1 |
| tinkertim@6 | 255 | } |
| tinkertim@6 | 256 | } |
| tinkertim@6 | 257 | |
| tinkertim@6 | 258 | printf "Generating PEM encoded self signed certificate $target ... " |
| tinkertim@6 | 259 | |
| tinkertim@6 | 260 | printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n" "$A" "$B" "$C" "$D" "$E" \ |
| tinkertim@6 | 261 | "$F" "$G" 2>&1 | \ |
| tinkertim@10 | 262 | $OPENSSL req -days 1000 -new -x509 \ |
| tinkertim@10 | 263 | -nodes -out "$target" -keyout $target >/dev/null 2>&1 |
| tinkertim@6 | 264 | [ $? != 0 ] && { |
| tinkertim@6 | 265 | printf "[failed]\n" |
| tinkertim@6 | 266 | return 1 |
| tinkertim@6 | 267 | } |
| tinkertim@6 | 268 | |
| tinkertim@6 | 269 | $CHMOD 0600 $target |
| tinkertim@10 | 270 | openssl verify "$target" >/dev/null 2>&1 || { |
| tinkertim@10 | 271 | printf "\n %s failed verification and has been deleted.\n" \ |
| tinkertim@10 | 272 | "$target" |
| tinkertim@6 | 273 | $RM -f $target |
| tinkertim@6 | 274 | return 1 |
| tinkertim@6 | 275 | } |
| tinkertim@6 | 276 | |
| tinkertim@6 | 277 | printf "[success]\n" |
| tinkertim@6 | 278 | return 0 |
| tinkertim@7 | 279 | } |