add prompt if a service name is already being used then importing a key or adding...
authorJameson Rollins <jrollins@finestructure.net>
Mon, 18 Jan 2010 23:22:22 +0000 (18:22 -0500)
committerJameson Rollins <jrollins@finestructure.net>
Mon, 18 Jan 2010 23:22:22 +0000 (18:22 -0500)
This can be overridden with the MONKEYSPHERE_PROMPT var.

src/monkeysphere-host
src/share/mh/add_name
src/share/mh/import_key

index 1fa3aea32901cc858677116bc1dcf56dc45b1852..4f536e2365a784addde542c4407d073c3c9d7a06 100755 (executable)
@@ -79,9 +79,14 @@ gpg_host() {
 
 # list the info about the a key, in colon format, to stdout
 gpg_host_list_keys() {
-    gpg_host --list-keys --with-colons --fixed-list-mode \
-       --with-fingerprint --with-fingerprint \
-       "$1"
+    if [ "$1" ] ; then
+       gpg_host --list-keys --with-colons --fixed-list-mode \
+           --with-fingerprint --with-fingerprint \
+           "$1"
+    else
+       gpg_host --list-keys --with-colons --fixed-list-mode \
+           --with-fingerprint --with-fingerprint
+    fi
 }
 
 # edit key scripts, takes scripts on stdin, and keyID as first input
@@ -151,6 +156,25 @@ check_key_userid() {
        grep -q -x -F "$tmpuidMatch" 2>/dev/null
 }
 
+prompt_userid_exists() {
+    local userID="$1"
+    local gpgOut
+    local fingerprint
+
+    if gpgOut=$(gpg_host_list_keys "=${userID}" 2>/dev/null) ; then
+       fingerprint=$(echo "$gpgOut" | grep '^fpr:' | cut -d: -f10)
+       if [ "$PROMPT" = "true" ] ; then
+           printf "Service name '%s' is already being used by key '%s'.\nAre you sure you want to use it again? (y/N) " "$fingerprint" "$userID" >&2
+           read OK; OK=${OK:=N}
+           if [ "${OK/y/Y}" != 'Y' ] ; then
+               failure "Service name not added."
+           fi
+       else
+           log info "Key '%s' is already using the service name '%s'." "$fingerprint" "$userID" >&2
+       fi
+    fi
+}
+
 # run command looped over keys
 multi_key() {
     local cmd="$1"
index 0eeefb79404ab89c873a48fd10f5b2feda3f9034..754ced45f47774c9e18414a4467fc6e8c7ebf307 100644 (file)
@@ -34,6 +34,9 @@ keyID=$(check_key_input "$@")
 check_key_userid "$keyID" "$serviceName" && \
     failure "Service name '$serviceName' already exists on key '$keyID'."
 
+# test that a key with that user ID does not already exist
+prompt_userid_exists "$serviceName"
+
 check_service_name "$serviceName"
 
 if [ "$PROMPT" = "true" ] ; then
index 1b6f267b1e15c5c7c8e5a46775533661efc29cb3..0f362b8ce8aff7f1c95d8b56da9b9b3e963ce6ee 100644 (file)
@@ -27,8 +27,7 @@ if [ -z "$serviceName" ] ; then
 fi
 
 # test that a key with that user ID does not already exist
-check_key_userid "$serviceName" "$serviceName" && \
-    failure "A key with service name '$serviceName' already exists."
+prompt_userid_exists "$serviceName"
 
 # check that the service name is well formatted
 check_service_name "$serviceName"