4
# Copyright 2011 Marco Ceppi <marco@ceppi.net>
6
# This file is part of Charm Helpers.
8
# Charm Helpers is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# Charm Helpers is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with Charm Helpers. If not, see <http://www.gnu.org/licenses/>.
23
# Globally overridable settings. Make sure to set them before sourcing
25
CH_WGET_ARGS=${CH_WGET_ARGS:-"-q --content-disposition"}
29
# Retrives a file and compares the file to a hash
31
# param FILE URL or Path to file
32
# param HASH URL, Path, or HASH string for file's hash
34
# return filepath|null
40
local CH_DOWNLOAD_DIR=${CH_DOWNLOAD_DIR:-"`mktemp -d /tmp/ch-downloads.XXXXXX`"}
42
if [ `ch_is_url "$FILE"` ]; then
43
wget $CH_WGET_ARGS --directory-prefix="$CH_DOWNLOAD_DIR/" "$FILE"
44
FILE=$CH_DOWNLOAD_DIR/$(ls -tr $CH_DOWNLOAD_DIR|head -n 1)
47
if [ ! -f "$FILE" ]; then
51
if [ -z "$HASH" ];then
52
#echo "Warning, no has specified. The file will be downloaded but not cryptographically checked!" > 2
55
elif [ `ch_is_url "$HASH"` ]; then
56
local HASHNAME=$(basename $HASH)
57
wget $CH_WGET_ARGS "$HASH" -O /tmp/$HASHNAME
58
HASH=$(cat /tmp/$HASHNAME | awk '{ print $1 }')
59
elif [ -f "$HASH" ]; then
60
HASH=$(cat "$HASH" | awk '{ print $1 }')
63
local HASH_TYPE=$(ch_type_hash $HASH)
65
if [ -z "$HASH_TYPE" ]; then
68
local FILE_HASH=$(${HASH_TYPE}sum $FILE | awk '{ print $1 }')
70
if [ "$FILE_HASH" != "$HASH" ]; then
81
# Determine, using best approximation, if the hash is valid and what type
82
# of hashing algorithm was used
93
*[![:xdigit:]]* | "" )
115
# Checks if the string passed is a valid URL (http(s), ftp)
117
# param URL The URL to be checked
126
"http://"* | "https://"* | "ftp://"*)
138
# Checks if the string passed is an IP address
140
# param IP The IP addressed to be checked
147
local IP=$(echo $DIRTY | awk -F. '$1 <=255 && $2 <= 255 && $3 <= 255 && $4 <= 255 ')
149
if [ -z "$IP" ]; then
159
# Returns the first IP match to the hostname provided
161
# param HOSTNAME Host for which to retrieve an IP
168
#So, if there's multiple IP addresses, just grab the first
170
if [ `ch_is_ip "$HOST"` ]; then
175
local CHECK_IP=$(host -t A $HOST | awk 'NR==1{ print $4 }')
177
if [ ! `ch_is_ip "$CHECK_IP"` ]; then
178
# Try a dig, why not?
179
CHECK_IP=$(dig +short $HOST | awk 'NR==1{ print $1 }')
181
if [ ! `ch_is_ip "$CHECK_IP"` ]; then