]> the.earth.li Git - onak.git/blob - cleankey.c
Add subkey support to keydb_fs backend.
[onak.git] / cleankey.c
1 /*
2  * cleankey.c - Routines to look for common key problems and clean them up.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2004 Project Purple
7  */
8
9 #include <assert.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #include "cleankey.h"
15 #include "keystructs.h"
16 #include "mem.h"
17 #include "merge.h"
18 #include "log.h"
19
20 /**
21  *      dedupuids - Merge duplicate uids on a key.
22  *      @key: The key to de-dup uids on.
23  *
24  *      This function attempts to merge duplicate IDs on a key. It returns 0
25  *      if the key is unchanged, otherwise the number of dups merged.
26  */
27 int dedupuids(struct openpgp_publickey *key)
28 {
29         struct openpgp_signedpacket_list *curuid = NULL;
30         struct openpgp_signedpacket_list *dup = NULL;
31         struct openpgp_signedpacket_list *tmp = NULL;
32         int                               merged = 0;
33
34         assert(key != NULL);
35         curuid = key->uids;
36         while (curuid != NULL) {
37                 dup = find_signed_packet(curuid->next, curuid->packet);
38                 while (dup != NULL) {
39                         logthing(LOGTHING_INFO, "Found duplicate uid: %.*s",
40                                         curuid->packet->length,
41                                         curuid->packet->data);
42                         merged++;
43                         merge_packet_sigs(curuid, dup);
44                         /*
45                          * Remove the duplicate uid.
46                          */
47                         tmp = curuid;
48                         while (tmp != NULL && tmp->next != dup) {
49                                 tmp = tmp->next;
50                         }
51                         assert(tmp != NULL);
52                         tmp->next = dup->next;
53                         dup->next = NULL;
54                         free_signedpacket_list(dup);
55
56                         dup = find_signed_packet(curuid->next, curuid->packet);
57                 }
58                 curuid = curuid->next;
59         }
60
61         return merged;
62 }
63
64 /**
65  *      cleankeys - Apply all available cleaning options on a list of keys.
66  *      @keys: The list of keys to clean.
67  *
68  *      Applies all the cleaning options we can (eg duplicate key ids) to a
69  *      list of keys. Returns 0 if no changes were made, otherwise the number
70  *      of keys cleaned.
71  */
72 int cleankeys(struct openpgp_publickey *keys)
73 {
74         int changed = 0;
75
76         while (keys != NULL) {
77                 if (dedupuids(keys) > 0) {
78                         changed++;
79                 }
80                 keys = keys->next;
81         }
82
83         return changed;
84 }