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