66# attachment services for tcp listeners.
77# Interactive and preconfigured modes supported.
88#
9- # Copyright (C) 2019-2020 remot3.it, Inc. All rights reserved.
9+ # Copyright (C) 2019-2021 remot3.it, Inc. All rights reserved.
1010
1111##### Settings #####
12- LIBVERSION=lib_v2.1.26
12+ LIBVERSION=lib_v2.6.29
1313AUTHOR="Gary Worsham"
14- LIBMODIFIED="November 10, 2020 "
14+ LIBMODIFIED="April 24, 2021 "
1515GREPFLAGS=
1616apikey="remote.it.developertoolsHW9iHnd"
1717
@@ -48,6 +48,19 @@ projectCreateURL="${apiMethod}${apiServer}${apiVersion}"/project/create
4848projectFileLinkURL="${apiMethod}${apiServer}${apiVersion}"/project/provisioning
4949
5050# ----------------------------------------
51+ # the variables in this section are used by the login by access key function only.
52+ key_alias=""
53+ secret_base64=""
54+ HOST="device.remote.it"
55+ URL_PATH="api/user/info"
56+ URL="https://${HOST}/${URL_PATH}"
57+ DATA=""
58+ VERB="GET"
59+ CONTENT_TYPE="application/json"
60+ LC_VERB=`echo "${VERB}" | tr '[:upper:]' '[:lower:]'`
61+ CONTENT_LENGTH=0
62+ #
63+ #
5164##### End Settings #####
5265
5366##### Version #####
@@ -983,22 +996,25 @@ askRegisterLogin()
983996# printf "\n\n"
984997 printf "********************* Sign In Menu *********************\n"
985998 printf " \n"
986- printf " 1) Sign in to your remote.it account \n"
987- printf " 2) Exit \n"
999+ printf " 1) Sign in to your remote.it account with username and password \n"
1000+ printf " 2) Sign in to your remote.it account with access key and secret \n"
1001+ printf " 3) Exit \n"
9881002 printf " \n"
9891003 printf "********************************************************\n"
9901004 printf "* To create a remote.it account, please visit *\n"
9911005 printf "* https://remote.it *\n"
9921006 printf "********************************************************\n\n"
993-
994- getNumRange 1 2 "Choose a menu selection"
1007+ getNumRange 1 3 "Choose a menu selection"
9951008 get_num="$getNumRangeValue"
9961009 debug "get_num: $get_num"
9971010
9981011 if [ "$get_num" = 1 ]; then
9991012 userLogin
10001013 testLogin
10011014 elif [ "$get_num" = 2 ]; then
1015+ userLogin key
1016+ debug "token: $token"
1017+ elif [ "$get_num" = 3 ]; then
10021018 if ask "Are you sure?"; then
10031019 exit 0
10041020 fi
@@ -1008,69 +1024,110 @@ askRegisterLogin()
10081024######### End askRegisterLogin #########
10091025
10101026######### Begin Portal Login #########
1011- userLogin() #Portal login function
1027+ # if parameter "key" is passed in, then it uses access key and secret
1028+ # otherwise it uses account username and password.
1029+ #
1030+ userLogin()
10121031{
1013- # echo "connectd_library Username = $USERNAME Password = $PASSWORD"
1014-
1015- if [ "$USERNAME" != "" ]; then
1016- username="$USERNAME"
1017- else
1018- printf "Please enter your remote.it Username (e-mail address): \n"
1019- read username
1020- fi
1021- if [ "$AUTHHASH" != "REPLACE_AUTHHASH" ]; then
1022- authhash="$AUTHHASH"
1032+ # use access key and secret
1033+
1034+ if [ "$1" = "key" ]; then
1035+ if [ "$ACCESSKEY" != "" ]; then
1036+ key_alias="$ACCESSKEY"
1037+ else
1038+ printf "Please enter your remote.it access key: \n"
1039+ read key_alias
1040+ fi
1041+ if [ "$KEYSECRET" != "" ]; then
1042+ secret_base64="$KEYSECRET"
1043+ else
1044+ printf "Please enter your remote.it access key secret: \n"
1045+ read secret_base64
1046+ fi
1047+ # use account username and password
10231048 else
1024- if [ "$PASSWORD" != "" ]; then
1025- password="$PASSWORD"
1049+ if [ "$USERNAME" != "" ]; then
1050+ username="$USERNAME"
1051+ else
1052+ printf "Please enter your remote.it Username (e-mail address): \n"
1053+ read username
1054+ fi
1055+ if [ "$AUTHHASH" != "REPLACE_AUTHHASH" ]; then
1056+ authhash="$AUTHHASH"
10261057 else
1058+ if [ "$PASSWORD" != "" ]; then
1059+ password="$PASSWORD"
1060+ else
10271061 # use stty to suppress password, if stty exists.
1028- if [ "$(which stty)" != "" ]; then
1029- stty -echo
1030- fi
1031- printf "\nPlease enter your remote.it password: \n"
1032- password=""
1033- read password
1034- if [ "$(which stty)" != "" ]; then
1035- stty echo
1036- fi
1037- fi
1062+ if [ "$(which stty)" != "" ]; then
1063+ stty -echo
1064+ fi
1065+ printf "\nPlease enter your remote.it password: \n"
1066+ password=""
1067+ read password
1068+ if [ "$(which stty)" != "" ]; then
1069+ stty echo
1070+ fi
1071+ fi
1072+ fi
1073+ debug $username $password $authhash
10381074 fi
1039- debug $username $password $authhash
10401075 signInAPI
10411076}
10421077######### End Portal Login #########
10431078
10441079####### SignInAPI ###################
10451080signInAPI()
10461081{
1047- debug "U:$username P:$password A:$authhash D:$apikey"
1048- # if AUTHHASH is REPLACE_AUTHHASH it means user just wants to use password
1049- if [ "$AUTHHASH" = "REPLACE_AUTHHASH" ]; then
1050- resp=$(curl ${CURL_OPTS} 'POST' -H "apikey:$apikey" -H "Content-Type:application/json" -H "Cache-Control:no-cache" -d "{ \"username\" : \"$username\", \"password\" : \"$password\" }" "$loginURL" 2> "$TMP_DIR"/.curlerr)
1082+ if [ "$key_alias" != "" ]; then
1083+ DATE=$(LANG=en_US date "+%a, %d %b %Y %H:%M:%S %Z")
1084+ # note that the formatting of the SIGNING_STRING is critical and should not be changed.
1085+ SIGNING_STRING="(request-target): ${LC_VERB} /${URL_PATH}
1086+ host: ${HOST}
1087+ date: ${DATE}
1088+ content-type: ${CONTENT_TYPE}
1089+ content-length: ${CONTENT_LENGTH}"
1090+ SECRET=`echo ${secret_base64} | base64 --decode`
1091+ SIGNATURE=`echo -n "${SIGNING_STRING}" | openssl dgst -binary -sha256 -hmac "${SECRET}" | base64`
1092+ SIGNATURE_HEADER="Signature keyId=\"$key_alias\",algorithm=\"hmac-sha256\",headers=\"(request-target) host date content-type content-length\",signature=\"${SIGNATURE}\""
1093+ resp=$(curl -s -X ${VERB} -H "Authorization:${SIGNATURE_HEADER}" -H "apikey:${apikey}" -H "Date:${DATE}" -H "Content-Type:${CONTENT_TYPE}" ${URL} -d "${DATA}" --insecure 2> "$TMP_DIR"/.curlerr)
1094+ debug "Resp = $resp"
1095+ checkAPIResult "${URL}" "$resp"
1096+ status=$(jsonval "$resp" 'status')
1097+ if [ "$status" = 'true' ]; then
1098+ token=$(jsonval "$resp" "token")
1099+ debug "Login token: $token"
1100+ else
1101+ exit 1
1102+ fi
10511103 else
1104+ # if AUTHHASH is REPLACE_AUTHHASH it means user just wants to use password
1105+ if [ "$AUTHHASH" = "REPLACE_AUTHHASH" ]; then
1106+ resp=$(curl ${CURL_OPTS} 'POST' -H "apikey:$apikey" -H "Content-Type:application/json" -H "Cache-Control:no-cache" -d "{ \"username\" : \"$username\", \"password\" : \"$password\" }" "$loginURL" 2> "$TMP_DIR"/.curlerr)
1107+ else
10521108 resp=$(curl ${CURL_OPTS} 'POST' -H "apikey:$apikey" -H "Content-Type:application/json" -H "Cache-Control:no-cache" -d "{ \"username\" : \"$username\", \"authhash\" : \"$AUTHHASH\" }" "$loginAuthURL" 2> "$TMP_DIR"/.curlerr)
1053- fi
1109+ fi
10541110# checkAPIResult 'login' "$resp"
10551111
1056- debug "Resp = $resp"
1057- status=$(jsonval "$resp" 'status')
1058- debug "Status = $status"
1059- if [ "$status" = 'true' ]; then
1060- token=$(jsonval "$resp" "token")
1061- debug "Login token: $token"
1062- else
1063- noSuchUser=$(echo "$resp" | grep "missing user" | sed 's/"//g')
1112+ debug "Resp = $resp"
1113+ status=$(jsonval "$resp" 'status')
1114+ debug "Status = $status"
1115+ if [ "$status" = 'true' ]; then
1116+ token=$(jsonval "$resp" "token")
1117+ debug "Login token: $token"
1118+ else
1119+ noSuchUser=$(echo "$resp" | grep "missing user" | sed 's/"//g')
10641120 # look for [0102] api return for invalid login
1065- loginFailed=$(echo "$resp" | grep "\[0102\]" | sed 's/"//g')
1066- slimError=$(echo "$resp" | grep "Slim Application Error" | sed 's/"//g')
1121+ loginFailed=$(echo "$resp" | grep "\[0102\]" | sed 's/"//g')
1122+ slimError=$(echo "$resp" | grep "Slim Application Error" | sed 's/"//g')
10671123# 404 is triggered when you enter some special character in e-mail. this specific search may fail if API
10681124# "$apiServer" is moved
1069- login404=$(echo "$resp" | grep "404 Page Not Found" | sed 's/"//g')
1070- login400=$(echo "$resp" | grep "400 Bad Request" | sed 's/"//g')
1071- apikeyerror=$(echo "$resp" | grep "The API application key is invalid" | sed 's/"//g')
1072- devkeyerror=$(echo "$resp" | grep "api key failed validation" | sed 's/"//g')
1073- debug "Login Error: $noSuchUser $loginFailed $slimError $login404 $login400 $apikeyerror $devkeyerror"
1125+ login404=$(echo "$resp" | grep "404 Page Not Found" | sed 's/"//g')
1126+ login400=$(echo "$resp" | grep "400 Bad Request" | sed 's/"//g')
1127+ apikeyerror=$(echo "$resp" | grep "The API application key is invalid" | sed 's/"//g')
1128+ devkeyerror=$(echo "$resp" | grep "api key failed validation" | sed 's/"//g')
1129+ debug "Login Error: $noSuchUser $loginFailed $slimError $login404 $login400 $apikeyerror $devkeyerror"
1130+ fi
10741131 fi
10751132 # invalid cert can happen if system date is set to before current date
10761133 invalidCert=$(grep "SSL certificate problem" "$TMP_DIR"/.curlerr)
0 commit comments