firetools/cookiefire

79 lines
1.5 KiB
Bash
Executable file

#!/bin/bash
# -*- mode:sh -*-
die() {
echo >&2 "$*"
exit 1
}
cleanup() {
rm -f "$tmpfile"
}
trap cleanup EXIT INT QUIT TERM
# Run older ld (pseudo condition)
if [ "$#" == "0" ]; then
profile=default
elif [ "$#" == "1" ]; then
profile=$1
else
die "usage $0 [profile]"
fi;
extract_cookies() {
if [ "$#" -ge 1 ]; then
sqlfile="$1"
else
if tty -s; then
sqlfile=$(ls -t ~/.mozilla/firefox/*/cookies.sqlite | head -1)
sqlfile="-" # Will use 'cat' below to read stdin
fi
fi
if [ "$sqlfile" != "-" -a ! -r "$sqlfile" ]; then
echo "Error. File $sqlfile is not readable." >&2
exit 1
fi
# We have to copy cookies.sqlite, because FireFox has a lock on it
cat "$sqlfile" >> $tmpfile
# This is the format of the sqlite database:
# CREATE TABLE moz_cookies (id INTEGER PRIMARY KEY, name TEXT, value TEXT, host TEXT, path TEXT,expiry INTEGER, lastAccessed INTEGER, isSecure INTEGER, isHttpOnly INTEGER);
echo "# Netscape HTTP Cookie File"
sqlite3 -separator $'\t' $tmpfile <<- EOF
.mode tabs
.header off
select host,
case substr(host,1,1)='.' when 0 then 'FALSE' else 'TRUE' end,
path,
case isSecure when 0 then 'FALSE' else 'TRUE' end,
expiry,
name,
value
from moz_cookies;
EOF
cleanup
}
tmpfile="$(mktemp /tmp/cookies.sqlite.XXXXXXXXXX)"
# try standard firefox format (randomchars.profilename), then bare name
cookie_file="$HOME/.mozilla/firefox/*.$profile/cookies.sqlite"
if ! ls $cookie_file &>/dev/null; then
cookie_file="$HOME/.mozilla/firefox/$profile/cookies.sqlite"
fi
extract_cookies $cookie_file