Thursday, April 30, 2020

Google Chrome Asks Password to Unlock Login Keyring - A BASH Scripting Tutorial

Should you be using Chrome, it seems to have a "flaw" where it asks you for a password along with the follwing error message: "Enter Password to Unlock Your Login Keyring". A solution for how to overcome that "flaw" is located here:
Google Chrome Asks Password to Unlock Login Keyring. This fix, however, is temporary from the perspective that whenever Chrome issues an update, the new version wipes out the fix. After doing this manually, several times, I decided to create a BASH script to automate the installation of the "fix".

As is usual there were a number of unexpected problems. I also wanted a script that was a bit more than a simple dumb one liner as a learning experience.
  1. Non-standard ASCII characters. The first problem faced was getting the correct line identified for editing. Seems that the character string "%U" in the Chrome file cannot be identified by using the expected "%U".  The regex solution was to use three "." to represent the intervening space and the the "%U".
  2. Unique string. The string "Exec=/usr/bin/google-chrome-stable" appears three times in the file. Fortunately being able to use the three "." addressed that problem by making that string unique.
  3. Verifying that the string to be modified is unique. Technically, the code for doing that was not really that necessary, but then I was doing this to make the code a bit smarter.
  4. Has the "fix" already been applied? Technically, the code for doing that was not really that necessary, but then I was doing this to make the code a bit smarter.
  5. How to actually modify the line that needed to be changed. At first, I was considering a process of concatenating strings. But as I researched the issue, the simplest approach appeared to be to a) identify the line number that had to be changed and then b) overwrite that line.
  6. Escaping Special Characters. This proved to be a major stumbling block. What happened is that I did not recognize that the "\" character had to be escaped with "\/". After doing a bunch of internet searching it finally dawned on me that the "/" was sort-of a special character in that it was a SED delimiter!!! As such it needed to be escaped. 
  7.  If Statement. I did not attempt to use nested if statements. Consequently the program progressive from one if statement to the next and fails if any one of them do.

Code Below.
----------------------------------------------------------------------------------------------------------------------
#!/bin/bash
#
# Google Chrome Asks Password to Unlock Login Keyring
# This script must be run using sudo. "sudo bash fix_chrome.sh"
# https://tipsonubuntu.com/2017/12/20/google-chrome-asks-password-unlock-login-keyring/
# April 28, 2020
#
chrome_dir="/usr/share/applications"
chrome_filename="google-chrome.desktop"
# Check how to use regex in Linux
# ^ needed to limit search to string at begging of line.
# $ End of line anchor
# . Any one intervening characters
# Note the %U in the string "Exec=/usr/bin/google-chrome-stable %U". Does not appear to be normal ASCII characters
# Substituting ... for the space plus %U
# The / need to be escaped with \ result -> \/
# Exec=/usr/bin/google-chrome-stable --password-store=basic %U
string_to_find='^Exec=/usr/bin/google-chrome-stable...$'
string_to_insert='password-store=basic'
string_new='Exec=\/usr\/bin\/google-chrome-stable --password-store=basic %U'
#
# Move to the Directory the Chrome file is located in.
cd ${chrome_dir}

# Check if file exists
if [ -f ${chrome_filename} ]
    then
        printf "\nFile Exists"
    else
        printf "\nFile Does NOT Exist. Is Chrome installed? Program terminated\n"
        exit 1
    fi

# Verify whether google-chrome.desktop has or has not been already inserted
if grep -qw ${string_to_insert} ${chrome_filename}
    then
        printf "\n${string_to_insert} string found. Nothing needs to be done. Already fixed.\n"
        exit 1
    fi 

# Check if there is a unique occurrence of the string-to-find.
num_found=$(grep -c  ${string_to_find} ${chrome_filename})
if [ $num_found -eq 1 ]
    then
        printf "\nUnique occurance found.\n"
        line_number=$(grep -n  ${string_to_find} ${chrome_filename} |  cut -f1 -d: )
        printf "Line Number: ${line_number}\n"
    else
        printf "\nFAILED. A total of ${num_found} occurrence were found. Must have only one unique occurrence. Program terminated.\n"
        exit 1
    fi

# Make Modification to file
sed -i "${line_number}s/.*/${string_new}/" "${chrome_filename}"
    if [ $? -eq 0 ]
        then
            echo "Success. Correction Made."
        else
            echo "FAILED to update"
        fi

----------------------------------------------------------------------------------------    

No comments: