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