add ddns-scripts (package by Eric Bishop)
[openwrt/svn-archive/archive.git] / net / ddns-scripts / files / usr / lib / ddns / shell_get.sh
1 #######################################################################
2 #/usr/lib/ddns/shell_get.sh
3 #
4 # Written by Eric Bishop, January 2008
5 # Distributed under the terms of the GNU General Public License (GPL) version 2.0
6 #
7 # This implements a wget-like program ("shell_get 1.0") that can handle
8 # basic http username/password authentication.
9 # It is implemented using the netcat (nc) utility.
10 # This is necessary because the default busybox wget
11 # does not include username/password functionality (it really sucks)
12 ##########################################################################
13
14 to_ascii()
15 {
16 dec=$1
17 hex=""
18 if [ $dec -lt 26 ]; then
19 hex=$(($dec + 0x41))
20 elif [ $dec -ge 26 ] && [ $dec -lt 52 ]; then
21 hex=$(( ($dec-26) + 0x61))
22 elif [ $dec -ge 52 ] && [ $dec -lt 62 ]; then
23 hex=$(( ($dec-52) + 0x30))
24 elif [ $dec -eq 62 ]; then
25 hex=43
26 else
27 hex=47
28 fi
29 printf "%x" $hex
30 }
31
32 encode_base64()
33 {
34 original_str=$1
35
36 hex_str=$( echo -n "$original_str" | hexdump -v | awk '{ for ( i = 2; i <= NF; i++ ) { h1=substr($i, 3, 2); h2=substr($i,0,2); printf("%s%s", h1, h2); }}' | awk ' { $0~gsub(/00$/, "") };{ i=1; while(i <= length($0) ){ block= substr($0, i, 3); printf("%s ", block); i=i+3; }}' | awk ' {$0~gsub(/ $/, "")}; { print $0 }' )
37
38 length=$(echo $hex_str | awk '{$0~gsub(/ /, "")}; { print length($0) }')
39 remainder=$(($length % 3 ))
40 if [ $remainder -eq 1 ]; then
41 hex_str=$hex_str'00'
42 elif [ $remainder -eq 2 ]; then
43 hex_str=$hex_str'0'
44 fi
45
46
47
48
49 base_64_str=""
50 for hex_block in $hex_str
51 do
52 char1=$(to_ascii $((0x$hex_block / 64)))
53 char2=$(to_ascii $((0x$hex_block % 64)))
54 base_64_str=$(printf "$base_64_str\x$char1\x$char2")
55 done
56
57
58 if [ $remainder -eq 1 ]; then
59 base_64_str=$(echo "$base_64_str" | awk '{ $0~gsub(/A$/, "=");} { print $0 }' )
60 elif [ $remainder -eq 2 ]; then
61 base_64_str=$(echo "$base_64_str==")
62 fi
63
64
65 echo $base_64_str
66 }
67
68 shell_get()
69 {
70 full_url=$1
71
72 protocol_str=$(echo $full_url | awk ' BEGIN {FS="://"} { if($0~/:\/\//)print $1 }')
73 if [ "$protocol_str" != "http" ] && [ -n "$protocol_str" ] ; then
74 echo "protocol = $protocol_str"
75 echo "Error, unsupported Protocol"
76 echo "Only http connections are supported at this time"
77 return 1;
78 fi
79
80
81 if [ -n "$protocol_str" ] ; then
82 full_url=$(echo $full_url | awk ' {$0~gsub(/http:\/\//, "")}; {print $0}')
83 fi
84
85
86
87 user_pass=$(echo $full_url | awk ' BEGIN {FS="@"}; { if( $0~/@/ && $1~/^[^\?\/]+:[^\?\/]+$/ ) print $1 }')
88 host_and_args=""
89 if [ -n "$user_pass" ]; then
90 host_and_args=$(echo $full_url | awk ' $0~gsub(/^[^@]+@/, "") {print $0}')
91 else
92 host_and_args="$full_url"
93 fi
94
95 host_name=$(echo $host_and_args | awk ' BEGIN {FS="[:?/]"}; {print $1};')
96 port_num=$(echo $host_and_args | awk ' BEGIN {FS="[?/]"}; { if($1~/:/){$1~gsub(/.*:/, ""); print $1;}else {print "80"}};')
97
98 path=$(echo $host_and_args | awk ' { $0~gsub(/^[^\?\/]+/, "")}; {print $0};')
99 path_start_test=$(echo "$path" | grep "^/")
100 if [ -z "$path_start_test" ]; then
101 path="/$path"
102 fi
103
104
105 #echo "full_url=\"$full_url\""
106 #echo "user_pass=\"$user_pass\""
107 #echo "host_name=\"$host_name\""
108 #echo "port_num=\"$port_num\""
109 #echo "path=\"$path\""
110
111
112 retrieved_data=""
113 if [ -n "$user_pass" ]; then
114 auth_str=$(encode_base64 "$user_pass" )
115 #echo -e "GET $path HTTP/1.0\nHost: $host_name\nAuthorization: Basic $auth_str\nUser-Agent: shell_get 1.0\n\n"
116 retrieved_data=$(echo -e "GET $path HTTP/1.0\nHost: $host_name\nAuthorization: Basic $auth_str\nUser-Agent: shell_get 1.0\n\n" | nc "$host_name" $port_num | cat)
117
118 else
119 #echo -e "GET $path HTTP/1.0\nHost: $host_name\nUser-Agent: shell_get 1.0\n\n"
120 retrieved_data=$(echo -e "GET $path HTTP/1.0\nHost: $host_name\nUser-Agent: shell_get 1.0\n\n" | nc "$host_name" $port_num | cat)
121 fi
122
123 echo -e "$retrieved_data"
124
125 }
126