Debian Codegraph @

src/dbverify/main.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2012 William Pitcock <[email protected]>.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
11 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
12 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
14 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
16 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
17 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
18 * POSSIBILITY OF SUCH DAMAGE.
19 */
20
21 #include "atheme.h"
22 #include "libathemecore.h"
23
24 static unsigned int verify_entity_uids(void)
25 {
26 unsigned int errcnt = 0;
27 mowgli_patricia_t *known = mowgli_patricia_create(strcasecanon);
28 myentity_iteration_state_t state;
29 myentity_t *mt;
30
31 MYENTITY_FOREACH_T(mt, &state, ENT_ANY)
32 {
33 myentity_t *mt2;
34
35 if ((mt2 = mowgli_patricia_retrieve(known, mt->id)) != NULL)
36 {
37 mowgli_strlcpy(mt->id, myentity_alloc_uid(), sizeof mt->id);
38
39 slog(LG_INFO, "*** phase 4: entity '%s' has duplicate EID '%s' (belonging to '%s'); regenerating as '%s'",
40 mt->name, mt2->id, mt2->name, mt->id);
41
42 errcnt++;
43 continue;
44 }
45
46 mowgli_patricia_add(known, mt->id, mt);
47 }
48
49 mowgli_patricia_destroy(known, NULL, NULL);
50 return errcnt;
51 }
52
53 static void verify_channel_registrations(void)
54 {
55 mowgli_patricia_iteration_state_t state;
56 mychan_t *mc;
57
58 MOWGLI_PATRICIA_FOREACH(mc, &state, mclist)
59 {
60 mowgli_node_t *n, *tn;
61 mowgli_patricia_t *known = mowgli_patricia_create(strcasecanon);
62
63 slog(LG_INFO, "*** phase 3: checking %s for consistency", mc->name);
64
65 MOWGLI_ITER_FOREACH_SAFE(n, tn, mc->chanacs.head)
66 {
67 chanacs_t *ca = n->data, *ca2;
68 stringref key = ca->entity != NULL ? ca->entity->name : ca->host;
69
70 if (key == NULL)
71 {
72 slog(LG_INFO, "*** phase 3: %s: chanacs entry %p is dangling; unlinking from object store", mc->name, ca);
73 mowgli_node_delete(&ca->cnode, &mc->chanacs);
74 continue;
75 }
76
77 if ((ca2 = mowgli_patricia_retrieve(known, key)) != NULL)
78 {
79 slog(LG_INFO, "*** phase 3: %s: chanacs entry '%s' (%p) duplicates chanacs entry %p", mc->name, ca->entity != NULL ? ca->entity->name : ca->host, ca, ca2);
80 mowgli_node_delete(&ca->cnode, &mc->chanacs);
81 continue;
82 }
83
84 if ((ca->level & CA_AKICK) && ca->level != CA_AKICK)
85 {
86 unsigned int flags = ca->level & ~CA_AKICK;
87
88 ca->level = CA_AKICK;
89 slog(LG_INFO, "*** phase 3: %s: chanacs entry '%s' (%p) is an AKICK but has other flags -- removing %s from it",
90 mc->name, ca->entity != NULL ? ca->entity->name : ca->host, ca, bitmask_to_flags(flags));
91 }
92
93 mowgli_patricia_add(known, key, ca);
94 }
95
96 mowgli_patricia_destroy(known, NULL, NULL);
97 }
98 }
99
100 static void handle_mdep(database_handle_t *db, const char *type)
101 {
102 const char *modname = db_sread_word(db);
103
104 module_load(modname);
105 }
106
107 int main(int argc, char *argv[])
108 {
109 atheme_bootstrap();
110 atheme_init(argv[0], LOGDIR "/dbverify.log");
111 atheme_setup();
112 module_t *m;
113 unsigned int errcnt;
114 char *filename = argv[1] ? argv[1] : "services.db";
115
116 runflags = RF_LIVE;
117 datadir = DATADIR;
118 strict_mode = false;
119 offline_mode = true;
120
121 slog(LG_INFO, "dbverify is operating on %s", filename);
122
123 m = module_load("backend/opensex");
124
125 db_unregister_type_handler("MDEP");
126 db_register_type_handler("MDEP", handle_mdep);
127
128 slog(LG_INFO, "*** phase 1: demarshaling objects from opensex datastore");
129
130 runflags &= ~RF_LIVE;
131 db_load(filename);
132 runflags |= RF_LIVE;
133
134 slog(LG_INFO, "*** phase 2: doing basic atheme database consistency check");
135
136 db_check();
137
138 slog(LG_INFO, "*** phase 3: verifying channel registration integrity");
139
140 verify_channel_registrations();
141
142 slog(LG_INFO, "*** phase 4: verifying entity UID integrity");
143
144 while ((errcnt = verify_entity_uids()) != 0)
145 slog(LG_INFO, "*** phase 4: %u error(s) were found; running another pass", errcnt);
146
147 slog(LG_INFO, "*** phase 5: writing corrected state to object store");
148
149 db_save(filename);
150
151 return EXIT_SUCCESS;
152 }