Sunday, April 12, 2009

What I hate about programming

Some months ago when it reached Kris Moore's attention (late as usual) that I had brought up security issues with his Firefox3 PBI, he changed it to something almost as bad. A couple weeks ago, I heard back from Kris that he had [naively] changed the code for making Fx3 the users default browser would no longer run as "root". After a little more conversation he split it off to something better.

Originally it was a part of the script that runs during PBI installation (and worse then the below script), probably tired of my replies he made an extra wrapper around the Firefox3, that asks the user if they want Firefox3 set default or not, rather then workin' the user database at install time. (I refuse comment on the following scripts predecessors: if you want to know more, read his SVN). The solution he came up for that wrapper, was to invokes the following code as the user when necessary:

#!/bin/sh
# Helper script to make FF the default browser for a user
##############################################################################

# Get the users homedir
USER="`whoami`"
HOMEDIR="`cat /etc/passwd | grep ^${USER}: | cut -d ":" -f 6`"

if [ -e "${HOMEDIR}/.kde4" ]
then
   KDEDIR=".kde4"
else
   KDEDIR=".kde"
fi

if [ ! -e "${HOMEDIR}/${KDEDIR}/share/config/kdeglobals" ]
then
  echo "ERROR: No kdeglobals file for $USER"
  exit 1
fi


TMPKFILE="${HOMEDIR}/.kdeglobals.$$"
TMPKFILE2="${HOMEDIR}/.kdeglobals2.$$"
rm ${TMPKFILE} >/dev/null 2>/dev/null

cat ${HOMEDIR}/${KDEDIR}/share/config/kdeglobals | grep -v '^BrowserApplication' > ${TMPKFILE}

rm ${TMPKFILE2} >/dev/null 2>/dev/null
touch ${TMPKFILE2}
while read line
do
  if [ "$line" = "[General]" ]
  then
    echo "$line" >> ${TMPKFILE2}
    if [ "${KDEDIR}" = ".kde4" ]
    then
      echo "BrowserApplication[$e]=!/Programs/bin/firefox3" >> ${TMPKFILE2}
    else
      echo "BrowserApplication=!/Programs/bin/firefox3" >> ${TMPKFILE2}
    fi
  else
    echo "$line" >> ${TMPKFILE2}
  fi
done < ${TMPKFILE}

# all finished, now move it back over kdeglobals
rm ${TMPKFILE}
mv ${TMPKFILE2} ${HOME}/${KDEDIR}/share/config/kdeglobals

exit 0
which is more secure then the original implementation, and more efficient also. Tonight I sent Kris a casual (read: adapt to need, don't take as is) suggestion from yours truly:
#!/bin/sh
# Helper script to make FF the default browser for a user
# Should work for KDE3 and KDE4.
##############################################################################

PROG="!/Programs/bin/firefox3"
FILE="./share/config/kdeglobals"

for D in "${HOME}/.kde" "${HOME}/.kde4"
do
   cd $D 2>/dev/null || break;

   if [ ! -e "$FILE" ]
   then
       echo "ERROR: No kdeglobals file, unable to set $PROG as default"
       exit 1
   fi

   ed -s "$FILE" <<EOF
       /\[General\]/
       /BrowserApplication.*=/
       s/=.*/=${PROG}/
       wq
   EOF
   # write your own error handlers
done

exit 0
which should work as far as I can test; since I lack a working KDE install (compiling KDE4.2+ is on my todo list). It's not perfect, but it sure is nicer then what he had a few months back. I included the a diff of the two scripts in my last message, which may very well go against my decision to "never" send these people patches. But I really don't care if he accepts it or not, because while I believe in being helpful, I also I do not like doing peoples jobs for them.





I'm a lazy good for nothing creep, but I am lazy of muscle - not lazy of mind. The most productive code I have ever written, is the code I was smart enough /not/ to write in the first place.

No comments:

Post a Comment