# void escape_regex_string_param(char* param) # # Escape special regex characters function escape_regex_string_param() { local escaped_result=$@ escaped_result=${escaped_result//\//[\/]} escaped_result=${escaped_result//./[.]} escaped_result=${escaped_result//\*/[\*]} escaped_result=${escaped_result//\\/\\\\} echo $escaped_result } # bool update_secrets_file(char* filepath, char* username, char* remotename, char* password) # # Add/update PAP/CHAP authentication information function update_secrets_file() { local filepath=${1} username=${2} remotename=${3} password=${4} if [[ ! -f ${filepath} ]]; then touch ${filepath} && \ chmod 0600 ${filepath} || \ return 1 fi #escape username and remotename, used in following sed calls local regex_username=$(escape_regex_string_param ${username}) local regex_remotename=$(escape_regex_string_param ${remotename}) local regex_password=${password//\\/\\\\} #read old password, including " chars #for being able to distinct when we need to add or update auth info local old_password=$( sed -e "/^[ \t]*\"\?${regex_username}\"\?[ \t]*${regex_remotename}[ \t]*\".*\"[ \t]*\$/\ {s/^[ \t]*\"\?${regex_username}\"\?[ \t]*${regex_remotename}[ \t]*\(\".*\"\)[ \t]*\$/\1/;q;};\ d;" \ ${filepath} ) if [[ -z "${old_password}" ]]; then regex_username=${username//\\/\\\\} regex_remotename=${remotename//\\/\\\\} sed -i -e "\$a\"${regex_username}\" ${regex_remotename} \"${regex_password}\"" ${filepath} ewarn "Authentication info has been added to ${filepath}" elif [[ "\"${password}\"" != "${old_password}" ]]; then sed -i -e "/^[ \t]*\"\?${regex_username}\"\?[ \t]*${regex_remotename}[ \t]*\".*\"[ \t]*\$/\ {s/^\([ \t]*\"\?${regex_username}\"\?[ \t]*${regex_remotename}[ \t]*\"\).*\(\"[ \t]*\)\$/\1${regex_password}\2/;q;};" \ ${filepath} ewarn "Authentication info has been updated in ${filepath}" fi return 0 }