NU-ITS / LAPSforMac

Local Administrator Password Solution for Mac
MIT License
138 stars 42 forks source link

Newline characters in password from curl #6

Open taylor-madeak opened 7 years ago

taylor-madeak commented 7 years ago

Opening this as an issue because a pull request with my current repo would include too many other changes.

ISSUE When using curl | xpath | awk to retrieve the LAPS password from the API, the resulting value contains multiple leading newline characters.

Example:

udid=$(/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Hardware UUID:/ { print $3 }')
extAttName="\"LAPS\""
oldPass=$(/usr/bin/curl -skfu "$apiUser":"$apiPass" -H "Accept: application/xml" "$apiURL/JSSResource/computers/udid/$udid/subset/extension_attributes" | /usr/bin/xpath "//extension_attribute[name=$extAttName]" 2>&1 | awk -F"<value>|</value>" '{ print $2 }')

echo "==$oldPass=="

Result:

==

aXyl47ctwer6==

This will work most of the time if you don't quote the variable when using it because bash will interpret it as "split using $IFS," but will break as soon as $IFS changes. However, proper syntax calls for quoting to prevent splitting and globbing, and that will break this method every time.

SOLUTION Trim the whitespace characters off before using the variable. This can be accomplished a number of ways, I chose to use bash parameter expansion syntax.

Example using the variable set with curl above:

oldPass=${oldPass//[$'\t\r\n ']}
echo "==$oldPass=="

Result: ==aXyl47ctwer6==