made monkeysphere subkey-to-ssh-agent more user-friendly.
[monkeysphere.git] / src / common
index bb988f785e983ad5bcb90492f3390b618ce2c737..44bdb670f515384646bd6d92a5f11ad6e6b5329a 100644 (file)
@@ -83,6 +83,89 @@ gpg_escape() {
     sed 's/:/\\x3a/g'
 }
 
+# prompt for GPG-formatted expiration, and emit result on stdout
+get_gpg_expiration() {
+    local keyExpire=
+
+    cat >&2 <<EOF
+Please specify how long the key should be valid.
+         0 = key does not expire
+      <n>  = key expires in n days
+      <n>w = key expires in n weeks
+      <n>m = key expires in n months
+      <n>y = key expires in n years
+EOF
+    while [ -z "$keyExpire" ] ; do
+       read -p "Key is valid for? (0) " keyExpire
+       if ! test_gpg_expire ${keyExpire:=0} ; then
+           echo "invalid value" >&2
+           unset keyExpire
+       fi
+    done
+    echo "$keyExpire"
+}
+
+passphrase_prompt() {
+    local prompt="$1"
+    local fifo="$2"
+    local PASS
+
+    if [ "$DISPLAY" ] && which "${SSH_ASKPASS:-ssh-askpass}" >/dev/null; then
+       "${SSH_ASKPASS:-ssh-askpass}" "$prompt" > "$fifo"
+    else
+       read -s -p "$prompt" PASS
+       # Uses the builtin echo, so should not put the passphrase into
+       # the process table.  I think. --dkg
+       echo "$PASS" > "$fifo"
+    fi
+}
+
+test_gnu_dummy_s2k_extension() {
+
+# this block contains a demonstration private key that has had the
+# primary key stripped out using the GNU S2K extension known as
+# "gnu-dummy" (see /usr/share/doc/gnupg/DETAILS.gz).  The subkey is
+# present in cleartext, however.
+
+# openpgp2ssh will be able to deal with this based on whether the
+# local copy of GnuTLS contains read_s2k support that can handle it.
+
+# read up on that here:
+
+# http://lists.gnu.org/archive/html/gnutls-devel/2008-08/msg00005.html
+
+echo "
+-----BEGIN PGP PRIVATE KEY BLOCK-----
+Version: GnuPG v1.4.9 (GNU/Linux)
+
+lQCVBEO3YdABBACRqqEnucag4+vyZny2M67Pai5+5suIRRvY+Ly8Ms5MvgCi3EVV
+xT05O/+0ShiRaf+QicCOFrhbU9PZzzU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88idnhDPqPoiKUEGAECAA8FAkO3YdACGyAFCQHhM4AACgkQQZUwSa4UDezm
+vQP/ZhK+2ly9oI2z7ZcNC/BJRch0/ybQ3haahII8pXXmOThpZohr/LUgoWgCZdXg
+vP6yiszNk2tIs8KphCAw7Lw/qzDC2hEORjWO4f46qk73RAgSqG/GyzI4ltWiDhqn
+vnQCFl3+QFSe4zinqykHnLwGPMXv428d/ZjkIc2ju8dRsn4=
+=CR5w
+-----END PGP PRIVATE KEY BLOCK-----
+" | openpgp2ssh 4129E89D17C1D591 >/dev/null 2>/dev/null
+
+}
+
 # remove all lines with specified string from specified file
 remove_line() {
     local file
@@ -393,29 +476,24 @@ process_user_id() {
                ;;
            'uid') # user ids
                if [ "$lastKey" != pub ] ; then
-                   log " - got a user ID after a sub key!  user IDs should only follow primary keys!"
-                   continue
-               fi
-               # don't bother with a uid if there is no valid or reasonable primary key.
-               if [ "$keyOK" != true ] ; then
+                   log " - got a user ID after a sub key?!  user IDs should only follow primary keys!"
                    continue
                fi
                # if an acceptable user ID was already found, skip
-               if [ "$uidOK" ] ; then
-                   continue
-               fi
-               # if the user ID does not match, skip
-               if [ "$(echo "$uidfpr" | gpg_unescape)" != "$userID" ] ; then
+               if [ "$uidOK" = 'true' ] ; then
                    continue
                fi
-               # if the user ID validity is not ok, skip
-               if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
+               # if the user ID does matches...
+               if [ "$(echo "$uidfpr" | gpg_unescape)" = "$userID" ] ; then
+                   # and the user ID validity is ok
+                   if [ "$validity" = 'u' -o "$validity" = 'f' ] ; then
+                       # mark user ID acceptable
+                       uidOK=true
+                   fi
+               else
                    continue
                fi
 
-               # mark user ID acceptable
-               uidOK=true
-
                # output a line for the primary key
                # 0 = ok, 1 = bad
                if [ "$keyOK" -a "$uidOK" -a "$lastKeyOK" ] ; then