Updated.
[gnupg.git] / g10 / trustdb.c
index f2e1096..e372bf8 100644 (file)
@@ -28,7 +28,7 @@
 
 #ifndef DISABLE_REGEX
 #include <sys/types.h>
-#ifdef USE_GNU_REGEX
+#ifdef USE_INTERNAL_REGEX
 #include "_regex.h"
 #else
 #include <regex.h>
@@ -1574,14 +1574,17 @@ mark_usable_uid_certs (KBNODE keyblock, KBNODE uidnode,
     }
 }
 
-int
-clean_sigs_from_uid(KBNODE keyblock,KBNODE uidnode,int noisy)
+static int
+clean_sigs_from_uid(KBNODE keyblock,KBNODE uidnode,int noisy,int self_only)
 {
   int deleted=0;
   KBNODE node;
+  u32 keyid[2];
 
   assert(keyblock->pkt->pkttype==PKT_PUBLIC_KEY);
 
+  keyid_from_pk(keyblock->pkt->pkt.public_key,keyid);
+
   /* Passing in a 0 for current time here means that we'll never weed
      out an expired sig.  This is correct behavior since we want to
      keep the most recent expired sig in a series. */
@@ -1607,28 +1610,37 @@ clean_sigs_from_uid(KBNODE keyblock,KBNODE uidnode,int noisy)
       node && node->pkt->pkttype==PKT_SIGNATURE;
       node=node->next)
     {
+      int keep=self_only?(node->pkt->pkt.signature->keyid[0]==keyid[0]
+                         && node->pkt->pkt.signature->keyid[1]==keyid[1]):1;
+
       /* Keep usable uid sigs ... */
-      if(node->flag & (1<<8))
+      if((node->flag & (1<<8)) && keep)
        continue;
 
       /* ... and usable revocations... */
-      if(node->flag & (1<<11))
+      if((node->flag & (1<<11)) && keep)
        continue;
 
       /* ... and sigs from unavailable keys. */
-      if(node->flag & (1<<12))
+      /* disabled for now since more people seem to want sigs from
+        unavailable keys removed altogether.  */
+      /*
+       if(node->flag & (1<<12))
        continue;
+      */
 
       /* Everything else we delete */
 
-      /* if 9 or 10 is set, but we get this far, it's superceded,
-        otherwise, it's invalid */
+      /* At this point, if 12 is set, the signing key was unavailable.
+        If 9 or 10 is set, it's superceded.  Otherwise, it's
+        invalid. */
 
       if(noisy)
-       log_info("removing signature from %s on uid \"%s\": %s\n",
+       log_info("removing signature from key %s on user ID \"%s\": %s\n",
                 keystr(node->pkt->pkt.signature->keyid),
                 uidnode->pkt->pkt.user_id->name,
-                node->flag&(1<<9)?"superceded":"invalid");
+                node->flag&(1<<12)?"key unavailable":
+                node->flag&(1<<9)?"signature superceded":"invalid signature");
 
       delete_kbnode(node);
       deleted++;
@@ -1641,98 +1653,102 @@ clean_sigs_from_uid(KBNODE keyblock,KBNODE uidnode,int noisy)
    have to establish if the uid has a valid self-sig, is not revoked,
    and is not expired.  Note that this does not take into account
    whether the uid has a trust path to it - just whether the keyholder
-   themselves has certified the uid.  Returns how many user IDs were
-   removed.  To "remove" a user ID, we simply remove ALL signatures
+   themselves has certified the uid.  Returns true if the uid was
+   compacted.  To "compact" a user ID, we simply remove ALL signatures
    except the self-sig that caused the user ID to be remove-worthy.
    We don't actually remove the user ID packet itself since it might
-   be ressurected in a later merge.
+   be ressurected in a later merge.  Note that this function requires
+   that the caller has already done a merge_keys_and_selfsig().
 
-   If this self-sig is a revocation, we also include the most recent
-   valid regular sig since it is hard to import the user ID otherwise.
    TODO: change the import code to allow importing a uid with only a
    revocation if the uid already exists on the keyring. */
-int
-clean_uids_from_key(KBNODE keyblock,int noisy)
+
+static int
+clean_uid_from_key(KBNODE keyblock,KBNODE uidnode,int noisy)
 {
-  int delete_until_next=0,deleting=0,deleted=0;
-  KBNODE node,signode=NULL;
-  u32 keyid[2],sigdate=0;
+  KBNODE node;
+  PKT_user_id *uid=uidnode->pkt->pkt.user_id;
+  int deleted=0;
 
   assert(keyblock->pkt->pkttype==PKT_PUBLIC_KEY);
+  assert(uidnode->pkt->pkttype==PKT_USER_ID);
 
-  keyid_from_pk(keyblock->pkt->pkt.public_key,keyid);
-
-  merge_keys_and_selfsig(keyblock);
+  /* Skip valid user IDs, compacted user IDs, and non-self-signed user
+     IDs if --allow-non-selfsigned-uid is set. */
+  if(uid->created || uid->flags.compacted
+     || (!uid->is_expired && !uid->is_revoked
+        && opt.allow_non_selfsigned_uid))
+    return 0;
 
-  for(node=keyblock->next;
-      node && node->pkt->pkttype!=PKT_PUBLIC_SUBKEY;
+  for(node=uidnode->next;
+      node && node->pkt->pkttype==PKT_SIGNATURE;
       node=node->next)
+    if(!node->pkt->pkt.signature->flags.chosen_selfsig)
+      {
+       delete_kbnode(node);
+       deleted=1;
+       uidnode->pkt->pkt.user_id->flags.compacted=1;
+      }
+
+  if(noisy)
     {
-      if(node->pkt->pkttype==PKT_USER_ID)
-       {
-         PKT_user_id *uid=node->pkt->pkt.user_id;
-
-         sigdate=0;
-         signode=NULL;
-
-         /* Skip valid user IDs, and non-self-signed user IDs if
-            --allow-non-selfsigned-uid is set. */
-         if(uid->created
-            || (!uid->is_expired && !uid->is_revoked 
-                && opt.allow_non_selfsigned_uid))
-           delete_until_next=0;
-         else
-           {
-             delete_until_next=1;
-             deleting=1;
+      const char *reason;
+      char *user=utf8_to_native(uid->name,uid->len,0);
 
-             if(noisy)
-               {
-                 const char *reason;
-                 char *user=utf8_to_native(uid->name,uid->len,0);
+      if(uid->is_revoked)
+       reason=_("revoked");
+      else if(uid->is_expired)
+       reason=_("expired");
+      else
+       reason=_("invalid");
 
-                 if(uid->is_revoked)
-                   reason=_("revoked");
-                 else if(uid->is_expired)
-                   reason=_("expired");
-                 else
-                   reason=_("invalid");
+      log_info("compacting user ID \"%s\" on key %s: %s\n",
+              user,keystr_from_pk(keyblock->pkt->pkt.public_key),
+              reason);
 
-                 log_info("compacting user ID \"%s\" on key %s: %s\n",
-                          user,keystr(keyblock->pkt->pkt.public_key->keyid),
-                          reason);
+      xfree(user);
+    }
 
-                 xfree(user);
-               }
-           }
-       }
-      else if(node->pkt->pkttype==PKT_SIGNATURE)
-       {
-         PKT_signature *sig=node->pkt->pkt.signature;
+  return deleted;
+}
 
-         /* This isn't actually slow - the key signature validation
-            is cached from merge_keys_and_selfsig() */
-         if(IS_UID_SIG(sig) && sig->timestamp>sigdate
-            && keyid[0]==sig->keyid[0] && keyid[1]==sig->keyid[1]
-            && check_key_signature(keyblock,node,NULL)==0)
-           {
-             sigdate=sig->timestamp;
-             signode=node;
-           }
+/* Needs to be called after a merge_keys_and_selfsig() */
+void
+clean_one_uid(KBNODE keyblock,KBNODE uidnode,int noisy,int self_only,
+             int *uids_cleaned,int *sigs_cleaned)
+{
+  int dummy;
 
-         if(delete_until_next && !sig->flags.chosen_selfsig)
-           {
-             delete_kbnode(node);
-             if(deleting)
-               {
-                 deleted++;
-                 deleting=0;
-               }
-           }
-       }
-    }
+  assert(keyblock->pkt->pkttype==PKT_PUBLIC_KEY);
+  assert(uidnode->pkt->pkttype==PKT_USER_ID);
 
-  return deleted;
+  if(!uids_cleaned)
+    uids_cleaned=&dummy;
+
+  if(!sigs_cleaned)
+    sigs_cleaned=&dummy;
+
+  /* Do clean_uid_from_key first since if it fires off, we don't
+     have to bother with the other */
+  *uids_cleaned+=clean_uid_from_key(keyblock,uidnode,noisy);
+  if(!uidnode->pkt->pkt.user_id->flags.compacted)
+    *sigs_cleaned+=clean_sigs_from_uid(keyblock,uidnode,noisy,self_only);
+}
+
+void
+clean_key(KBNODE keyblock,int noisy,int self_only,
+         int *uids_cleaned,int *sigs_cleaned)
+{
+  KBNODE uidnode;
+
+  merge_keys_and_selfsig(keyblock);
+
+  for(uidnode=keyblock->next;
+      uidnode && uidnode->pkt->pkttype!=PKT_PUBLIC_SUBKEY;
+      uidnode=uidnode->next)
+    if(uidnode->pkt->pkttype==PKT_USER_ID)
+      clean_one_uid(keyblock,uidnode,noisy,self_only,
+                   uids_cleaned,sigs_cleaned);
 }
 
 /* Used by validate_one_keyblock to confirm a regexp within a trust
@@ -2140,7 +2156,8 @@ validate_keys (int interactive)
    * here when needed */
   if (!utk_list)
     {
-      log_info (_("no ultimately trusted keys found\n"));
+      if (!opt.quiet)
+        log_info (_("no ultimately trusted keys found\n"));
       goto leave;
     }