making invocations of mktemp portable (FreeBSD does not have a default value for...
[monkeysphere.git] / src / common
index 4e731e14b51469bea1f389ecd0e761f3172e4107..c8a7db65425523a59d84ec7aab094e384db96100 100644 (file)
@@ -4,6 +4,8 @@
 #
 # Written by
 # Jameson Rollins <jrollins@fifthhorseman.net>
+# Jamie McClelland <jm@mayfirst.org>
+# Daniel Kahn Gillmor <dkg@fifthhorseman.net>
 #
 # Copyright 2008, released under the GPL, version 3 or later
 
@@ -33,11 +35,15 @@ log() {
     local priority
     local level
     local output
-    # don't include SILENT in alllevels: it's handled separately
-    # list in decreasing verbosity (all caps)
-    local alllevels="DEBUG INFO ERROR"
+    local alllevels
     local found=
 
+    # don't include SILENT in alllevels: it's handled separately
+    # list in decreasing verbosity (all caps).
+    # separate with $IFS explicitly, since we do some fancy footwork
+    # elsewhere.
+    alllevels="DEBUG${IFS}VERBOSE${IFS}INFO${IFS}ERROR"
+
     # translate lowers to uppers in global log level
     LOG_LEVEL=$(echo "$LOG_LEVEL" | tr "[:lower:]" "[:upper:]")
 
@@ -46,7 +52,7 @@ log() {
        return
     fi
 
-    for level in $alllevels; do 
+    for level in $alllevels ; do 
        if [ "$LOG_LEVEL" = "$level" ] ; then
            found=true
        fi
@@ -85,6 +91,49 @@ cutline() {
     head --line="$1" "$2" | tail -1
 }
 
+# this is a wrapper for doing lock functions.
+#
+# it lets us depend on either lockfile-progs (preferred) or procmail's
+# lockfile, and should
+lock() {
+    local use_lockfileprogs=true
+    local action="$1"
+    local file="$2"
+
+    if ! ( which lockfile-create >/dev/null 2>/dev/null ) ; then
+       if ! ( which lockfile >/dev/null ); then
+           failure "Neither lockfile-create nor lockfile are in the path!"
+       fi
+       use_lockfileprogs=
+    fi
+    
+    case "$action" in
+       create)
+           if [ -n "$use_lockfileprogs" ] ; then
+               lockfile-create "$file" || failure "unable to lock '$file'"
+           else
+               lockfile -r 20 "${file}.lock" || failure "unable to lock '$file'"
+           fi
+           ;;
+       touch)  
+           if [ -n "$use_lockfileprogs" ] ; then
+               lockfile-touch --oneshot "$file"
+           else
+               : Nothing to do here
+           fi
+           ;;
+       remove)
+           if [ -n "$use_lockfileprogs" ] ; then
+               lockfile-remove "$file"
+           else
+               rm -f "${file}.lock"
+           fi
+           ;;
+       *)
+           failure "bad argument for lock subfunction '$action'"
+    esac
+}
+
 # check that characters are in a string (in an AND fashion).
 # used for checking key capability
 # check_capability capability a [b...]
@@ -216,6 +265,7 @@ vnQCFl3+QFSe4zinqykHnLwGPMXv428d/ZjkIc2ju8dRsn4=
 remove_line() {
     local file
     local string
+    local tempfile
 
     file="$1"
     string="$2"
@@ -230,8 +280,13 @@ remove_line() {
 
     # if the string is in the file...
     if grep -q -F "$string" "$file" 2> /dev/null ; then
+       tempfile=$(mktemp "${file}.XXXXXXX") || \
+           failure "Unable to make temp file '${file}.XXXXXXX'"
+       
        # remove the line with the string, and return 0
-       grep -v -F "$string" "$file" | sponge "$file"
+       grep -v -F "$string" "$file" >"$tempfile"
+       cat "$tempfile" > "$file"
+       rm "$tempfile"
        return 0
     # otherwise return 1
     else
@@ -242,6 +297,7 @@ remove_line() {
 # remove all lines with MonkeySphere strings in file
 remove_monkeysphere_lines() {
     local file
+    local tempfile
 
     file="$1"
 
@@ -253,8 +309,13 @@ remove_monkeysphere_lines() {
        return 1
     fi
 
+    tempfile=$(mktemp "${file}.XXXXXXX") || \
+       failure "Could not make temporary file '${file}.XXXXXXX'."
+
     egrep -v '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$' \
-       "$file" | sponge "$file"
+       "$file" >"$tempfile"
+    cat "$tempfile" > "$file"
+    rm "$tempfile"
 }
 
 # translate ssh-style path variables %h and %u
@@ -479,7 +540,7 @@ process_user_id() {
 
     # if the gpg query return code is not 0, return 1
     if [ "$?" -ne 0 ] ; then
-        log error " no primary keys found."
+        log verbose " no primary keys found."
         return 1
     fi
 
@@ -496,7 +557,7 @@ process_user_id() {
                lastKeyOK=
                fingerprint=
 
-               log error " primary key found: $keyid"
+               log verbose " primary key found: $keyid"
 
                # if overall key is not valid, skip
                if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
@@ -545,7 +606,7 @@ process_user_id() {
                # output a line for the primary key
                # 0 = ok, 1 = bad
                if [ "$keyOK" -a "$uidOK" -a "$lastKeyOK" ] ; then
-                   log error "  * acceptable primary key."
+                   log verbose "  * acceptable primary key."
                    if [ -z "$sshKey" ] ; then
                        log error "    ! primary key could not be translated (not RSA or DSA?)."
                    else
@@ -601,7 +662,7 @@ process_user_id() {
                # output a line for the sub key
                # 0 = ok, 1 = bad
                if [ "$keyOK" -a "$uidOK" -a "$lastKeyOK" ] ; then
-                   log error "  * acceptable sub key."
+                   log verbose "  * acceptable sub key."
                    if [ -z "$sshKey" ] ; then
                        log error "    ! sub key could not be translated (not RSA or DSA?)."
                    else
@@ -636,7 +697,7 @@ process_host_known_hosts() {
     host="$1"
     userID="ssh://${host}"
 
-    log info "processing: $host"
+    log verbose "processing: $host"
 
     nKeys=0
     nKeysOK=0
@@ -665,7 +726,7 @@ process_host_known_hosts() {
            if [ "$HASH_KNOWN_HOSTS" = 'true' ] ; then
                # FIXME: this is really hackish cause ssh-keygen won't
                # hash from stdin to stdout
-               tmpfile=$(mktemp)
+               tmpfile=$(mktemp ${TMPDIR:-/tmp}/tmp.XXXXXXXXXX)
                ssh2known_hosts "$host" "$sshKey" > "$tmpfile"
                ssh-keygen -H -f "$tmpfile" 2> /dev/null
                cat "$tmpfile" >> "$KNOWN_HOSTS"
@@ -706,11 +767,10 @@ update_known_hosts() {
     nHostsOK=0
     nHostsBAD=0
 
-    # set the trap to remove any lockfiles on exit
-    trap "lockfile-remove $KNOWN_HOSTS" EXIT
-
-    # create a lockfile on known_hosts
-    lockfile-create "$KNOWN_HOSTS"
+    # create a lockfile on known_hosts:
+    lock create "$KNOWN_HOSTS"
+    # FIXME: we're discarding any pre-existing EXIT trap; is this bad?
+    trap "lock remove $KNOWN_HOSTS" EXIT
 
     # note pre update file checksum
     fileCheck="$(file_hash "$KNOWN_HOSTS")"
@@ -729,15 +789,16 @@ update_known_hosts() {
        esac
 
        # touch the lockfile, for good measure.
-       lockfile-touch --oneshot "$KNOWN_HOSTS"
+       lock touch "$KNOWN_HOSTS"
     done
 
-    # remove the lockfile
-    lockfile-remove "$KNOWN_HOSTS"
+    # remove the lockfile and the trap
+    lock remove "$KNOWN_HOSTS"
+    trap - EXIT
 
     # note if the known_hosts file was updated
     if [ "$(file_hash "$KNOWN_HOSTS")" != "$fileCheck" ] ; then
-       log info "known_hosts file updated."
+       log verbose "known_hosts file updated."
     fi
 
     # if an acceptable host was found, return 0
@@ -760,7 +821,7 @@ update_known_hosts() {
 process_known_hosts() {
     local hosts
 
-    log info "processing known_hosts file..."
+    log verbose "processing known_hosts file..."
 
     hosts=$(meat "$KNOWN_HOSTS" | cut -d ' ' -f 1 | grep -v '^|.*$' | tr , ' ' | tr '\n' ' ')
 
@@ -785,7 +846,7 @@ process_uid_authorized_keys() {
 
     userID="$1"
 
-    log info "processing: $userID"
+    log verbose "processing: $userID"
 
     nKeys=0
     nKeysOK=0
@@ -844,11 +905,10 @@ update_authorized_keys() {
     nIDsOK=0
     nIDsBAD=0
 
-    # set the trap to remove any lockfiles on exit
-    trap "lockfile-remove $AUTHORIZED_KEYS" EXIT
-
     # create a lockfile on authorized_keys
-    lockfile-create "$AUTHORIZED_KEYS"
+    lock create "$AUTHORIZED_KEYS"
+    # FIXME: we're discarding any pre-existing EXIT trap; is this bad?
+    trap "lock remove $AUTHORIZED_KEYS" EXIT
 
     # note pre update file checksum
     fileCheck="$(file_hash "$AUTHORIZED_KEYS")"
@@ -872,15 +932,16 @@ update_authorized_keys() {
        esac
 
        # touch the lockfile, for good measure.
-       lockfile-touch --oneshot "$AUTHORIZED_KEYS"
+       lock touch "$AUTHORIZED_KEYS"
     done
 
-    # remove the lockfile
-    lockfile-remove "$AUTHORIZED_KEYS"
+    # remove the lockfile and the trap
+    lock remove "$AUTHORIZED_KEYS"
+    trap - EXIT
 
     # note if the authorized_keys file was updated
     if [ "$(file_hash "$AUTHORIZED_KEYS")" != "$fileCheck" ] ; then
-       log info "authorized_keys file updated."
+       log verbose "authorized_keys file updated."
     fi
 
     # if an acceptable id was found, return 0
@@ -907,7 +968,7 @@ process_authorized_user_ids() {
 
     authorizedUserIDs="$1"
 
-    log info "processing authorized_user_ids file..."
+    log verbose "processing authorized_user_ids file..."
 
     if ! meat "$authorizedUserIDs" > /dev/null ; then
        log error "no user IDs to process."