chiark / gitweb /
gpg: Fix searching for mail addresses in keyrings.
[gnupg2.git] / debian / patches / 0022-gpg-Only-print-out-TOFU-statistics-for-conflicts-in-.patch
1 From: "Neal H. Walfield" <neal@g10code.com>
2 Date: Thu, 2 Feb 2017 13:24:57 +0100
3 Subject: gpg: Only print out TOFU statistics for conflicts in interactive mode
4
5 * g10/tofu.c (get_trust): Add arguments POLICYP and CONFLICT_SETP.  If
6 they are not NULL, return the policy and conflict set (if there is
7 one), respectively.  Update callers.  If MAY_ASK is FALSE, don't print
8 out the statistics.
9 (tofu_register_encryption): If there is a conflict and we haven't yet
10 printed the statistics about the conflicting bindings, do so now.
11 (tofu_get_validity): Likewise.
12
13 Signed-off-by: Neal H. Walfield <neal@g10code.com>
14 GnuPG-bug-id: 2914
15 (cherry picked from commit 027b81b35fe36692005b8dba22d9eb2db05e8c80)
16 ---
17  g10/tofu.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++-----------
18  1 file changed, 69 insertions(+), 14 deletions(-)
19
20 diff --git a/g10/tofu.c b/g10/tofu.c
21 index 9f5f40694..fc03c5a7d 100644
22 --- a/g10/tofu.c
23 +++ b/g10/tofu.c
24 @@ -2644,7 +2644,9 @@ get_policy (tofu_dbs_t dbs, PKT_public_key *pk,
25  static enum tofu_policy
26  get_trust (ctrl_t ctrl, PKT_public_key *pk,
27             const char *fingerprint, const char *email,
28 -          const char *user_id, int may_ask, time_t now)
29 +           const char *user_id, int may_ask,
30 +           enum tofu_policy *policyp, strlist_t *conflict_setp,
31 +           time_t now)
32  {
33    tofu_dbs_t dbs = ctrl->tofu.dbs;
34    int in_transaction = 0;
35 @@ -2683,6 +2685,7 @@ get_trust (ctrl_t ctrl, PKT_public_key *pk,
36      if (tdb_keyid_is_utk (kid))
37        {
38          trust_level = TRUST_ULTIMATE;
39 +        policy = TOFU_POLICY_GOOD;
40          goto out;
41        }
42    }
43 @@ -2690,7 +2693,8 @@ get_trust (ctrl_t ctrl, PKT_public_key *pk,
44    begin_transaction (ctrl, 0);
45    in_transaction = 1;
46  
47 -  policy = get_policy (dbs, pk, fingerprint, user_id, email, &conflict_set, now);
48 +  policy = get_policy (dbs, pk, fingerprint, user_id, email,
49 +                       &conflict_set, now);
50    if (policy == TOFU_POLICY_AUTO)
51      {
52        policy = opt.tofu_default_policy;
53 @@ -2758,10 +2762,6 @@ get_trust (ctrl_t ctrl, PKT_public_key *pk,
54      }
55    else
56      {
57 -      for (iter = conflict_set; iter; iter = iter->next)
58 -        show_statistics (dbs, iter->d, email,
59 -                         TOFU_POLICY_ASK, NULL, 1, now);
60 -
61        trust_level = TRUST_UNDEFINED;
62      }
63  
64 @@ -2807,7 +2807,13 @@ get_trust (ctrl_t ctrl, PKT_public_key *pk,
65    if (in_transaction)
66      end_transaction (ctrl, 0);
67  
68 -  free_strlist (conflict_set);
69 +  if (policyp)
70 +    *policyp = policy;
71 +
72 +  if (conflict_setp)
73 +    *conflict_setp = conflict_set;
74 +  else
75 +    free_strlist (conflict_set);
76  
77    return trust_level;
78  }
79 @@ -3326,7 +3332,8 @@ tofu_register_signature (ctrl_t ctrl,
80  
81        /* Make sure the binding exists and record any TOFU
82           conflicts.  */
83 -      if (get_trust (ctrl, pk, fingerprint, email, user_id->d, 0, now)
84 +      if (get_trust (ctrl, pk, fingerprint, email, user_id->d,
85 +                     0, NULL, NULL, now)
86            == _tofu_GET_TRUST_ERROR)
87          {
88            rc = gpg_error (GPG_ERR_GENERAL);
89 @@ -3492,11 +3499,13 @@ tofu_register_encryption (ctrl_t ctrl,
90    for (user_id = user_id_list; user_id; user_id = user_id->next)
91      {
92        char *email = email_from_user_id (user_id->d);
93 +      strlist_t conflict_set = NULL;
94 +      enum tofu_policy policy;
95  
96        /* Make sure the binding exists and that we recognize any
97           conflicts.  */
98        int tl = get_trust (ctrl, pk, fingerprint, email, user_id->d,
99 -                          may_ask, now);
100 +                          may_ask, &policy, &conflict_set, now);
101        if (tl == _tofu_GET_TRUST_ERROR)
102          {
103            /* An error.  */
104 @@ -3505,6 +3514,28 @@ tofu_register_encryption (ctrl_t ctrl,
105            goto die;
106          }
107  
108 +
109 +      /* If there is a conflict and MAY_ASK is true, we need to show
110 +       * the TOFU statistics for the current binding and the
111 +       * conflicting bindings.  But, if we are not in batch mode, then
112 +       * they have already been printed (this is required to make sure
113 +       * the information is available to the caller before cpr_get is
114 +       * called).  */
115 +      if (policy == TOFU_POLICY_ASK && may_ask && opt.batch)
116 +        {
117 +          strlist_t iter;
118 +
119 +          /* The conflict set should contain at least the current
120 +           * key.  */
121 +          log_assert (conflict_set);
122 +
123 +          for (iter = conflict_set; iter; iter = iter->next)
124 +            show_statistics (dbs, iter->d, email,
125 +                             TOFU_POLICY_ASK, NULL, 1, now);
126 +        }
127 +
128 +      free_strlist (conflict_set);
129 +
130        rc = gpgsql_stepx
131          (dbs->db, &dbs->s.register_encryption, NULL, NULL, &err,
132           "insert into encryptions\n"
133 @@ -3681,11 +3712,13 @@ tofu_get_validity (ctrl_t ctrl, PKT_public_key *pk, strlist_t user_id_list,
134    for (user_id = user_id_list; user_id; user_id = user_id->next, bindings ++)
135      {
136        char *email = email_from_user_id (user_id->d);
137 +      strlist_t conflict_set = NULL;
138 +      enum tofu_policy policy;
139  
140        /* Always call get_trust to make sure the binding is
141           registered.  */
142        int tl = get_trust (ctrl, pk, fingerprint, email, user_id->d,
143 -                          may_ask, now);
144 +                          may_ask, &policy, &conflict_set, now);
145        if (tl == _tofu_GET_TRUST_ERROR)
146          {
147            /* An error.  */
148 @@ -3708,13 +3741,35 @@ tofu_get_validity (ctrl_t ctrl, PKT_public_key *pk, strlist_t user_id_list,
149  
150        if (may_ask && tl != TRUST_ULTIMATE && tl != TRUST_EXPIRED)
151          {
152 -          enum tofu_policy policy =
153 -            get_policy (dbs, pk, fingerprint, user_id->d, email, NULL, now);
154 +          /* If policy is ask, then we already printed out the
155 +           * conflict information in ask_about_binding or will do so
156 +           * in a moment.  */
157 +          if (policy != TOFU_POLICY_ASK)
158 +            need_warning |=
159 +              show_statistics (dbs, fingerprint, email, policy, NULL, 0, now);
160 +
161 +          /* If there is a conflict and MAY_ASK is true, we need to
162 +           * show the TOFU statistics for the current binding and the
163 +           * conflicting bindings.  But, if we are not in batch mode,
164 +           * then they have already been printed (this is required to
165 +           * make sure the information is available to the caller
166 +           * before cpr_get is called).  */
167 +          if (policy == TOFU_POLICY_ASK && opt.batch)
168 +            {
169 +              strlist_t iter;
170  
171 -          need_warning |=
172 -            show_statistics (dbs, fingerprint, email, policy, NULL, 0, now);
173 +              /* The conflict set should contain at least the current
174 +               * key.  */
175 +              log_assert (conflict_set);
176 +
177 +              for (iter = conflict_set; iter; iter = iter->next)
178 +                show_statistics (dbs, iter->d, email,
179 +                                 TOFU_POLICY_ASK, NULL, 1, now);
180 +            }
181          }
182  
183 +      free_strlist (conflict_set);
184 +
185        if (tl == TRUST_NEVER)
186          trust_level = TRUST_NEVER;
187        else if (tl == TRUST_EXPIRED)