Debian Codegraph @

modules/groupserv/main/database.c

raw
1 /* groupserv - group services.
2 * Copyright (c) 2010 Atheme Development Group
3 */
4
5 #include "groupserv_main.h"
6
7 #define GDBV_VERSION 4
8
9 static unsigned int loading_gdbv = -1;
10 static unsigned int their_ga_all;
11
12 static void write_groupdb(database_handle_t *db)
13 {
14 myentity_t *mt;
15 myentity_iteration_state_t state;
16 mowgli_patricia_iteration_state_t state2;
17 metadata_t *md;
18
19 db_start_row(db, "GDBV");
20 db_write_uint(db, GDBV_VERSION);
21 db_commit_row(db);
22
23 db_start_row(db, "GFA");
24 db_write_word(db, gflags_tostr(ga_flags, GA_ALL));
25 db_commit_row(db);
26
27 MYENTITY_FOREACH_T(mt, &state, ENT_GROUP)
28 {
29 continue_if_fail(mt != NULL);
30 mygroup_t *mg = group(mt);
31 continue_if_fail(mg != NULL);
32
33 char *mgflags = gflags_tostr(mg_flags, mg->flags);
34
35 db_start_row(db, "GRP");
36 db_write_word(db, entity(mg)->id);
37 db_write_word(db, entity(mg)->name);
38 db_write_time(db, mg->regtime);
39 db_write_word(db, mgflags);
40 db_commit_row(db);
41
42 if (object(mg)->metadata)
43 {
44 MOWGLI_PATRICIA_FOREACH(md, &state2, object(mg)->metadata)
45 {
46 db_start_row(db, "MDG");
47 db_write_word(db, entity(mg)->name);
48 db_write_word(db, md->name);
49 db_write_str(db, md->value);
50 db_commit_row(db);
51 }
52 }
53 }
54
55 MYENTITY_FOREACH_T(mt, &state, ENT_GROUP)
56 {
57 mowgli_node_t *n;
58
59 continue_if_fail(mt != NULL);
60 mygroup_t *mg = group(mt);
61 continue_if_fail(mg != NULL);
62
63 MOWGLI_ITER_FOREACH(n, mg->acs.head)
64 {
65 groupacs_t *ga = n->data;
66 char *flags = gflags_tostr(ga_flags, ga->flags);
67
68 db_start_row(db, "GACL");
69 db_write_word(db, entity(mg)->name);
70 db_write_word(db, ga->mt->name);
71 db_write_word(db, flags);
72 db_commit_row(db);
73 }
74 }
75 }
76
77 static void db_h_gdbv(database_handle_t *db, const char *type)
78 {
79 loading_gdbv = db_sread_uint(db);
80 slog(LG_INFO, "groupserv: opensex data schema version is %d.", loading_gdbv);
81
82 their_ga_all = GA_ALL_OLD;
83 }
84
85 static void db_h_gfa(database_handle_t *db, const char *type)
86 {
87 const char *flags = db_sread_word(db);
88
89 gflags_fromstr(ga_flags, flags, &their_ga_all);
90 if (their_ga_all & ~GA_ALL)
91 {
92 slog(LG_ERROR, "db-h-gfa: losing flags %s from file", gflags_tostr(ga_flags, their_ga_all & ~GA_ALL));
93 }
94 if (~their_ga_all & GA_ALL)
95 {
96 slog(LG_ERROR, "db-h-gfa: making up flags %s not present in file", gflags_tostr(ga_flags, ~their_ga_all & GA_ALL));
97 }
98 }
99
100 static void db_h_grp(database_handle_t *db, const char *type)
101 {
102 mygroup_t *mg;
103 const char *uid = NULL;
104 const char *name;
105 time_t regtime;
106 const char *flagset;
107
108 if (loading_gdbv >= 4)
109 uid = db_sread_word(db);
110
111 name = db_sread_word(db);
112
113 if (mygroup_find(name))
114 {
115 slog(LG_INFO, "db-h-grp: line %d: skipping duplicate group %s", db->line, name);
116 return;
117 }
118 if (uid && myentity_find_uid(uid))
119 {
120 slog(LG_INFO, "db-h-grp: line %d: skipping group %s with duplicate UID %s", db->line, name, uid);
121 return;
122 }
123
124 regtime = db_sread_time(db);
125
126 mg = mygroup_add_id(uid, name);
127 mg->regtime = regtime;
128
129 if (loading_gdbv >= 3)
130 {
131 flagset = db_sread_word(db);
132
133 if (!gflags_fromstr(mg_flags, flagset, &mg->flags))
134 slog(LG_INFO, "db-h-grp: line %d: confused by flags: %s", db->line, flagset);
135 }
136 }
137
138 static void db_h_gacl(database_handle_t *db, const char *type)
139 {
140 mygroup_t *mg;
141 myentity_t *mt;
142 unsigned int flags = GA_ALL; /* GDBV 1 entires had full access */
143
144 const char *name = db_sread_word(db);
145 const char *entity = db_sread_word(db);
146 const char *flagset;
147
148 mg = mygroup_find(name);
149 mt = myentity_find(entity);
150
151 if (mg == NULL)
152 {
153 slog(LG_INFO, "db-h-gacl: line %d: groupacs for nonexistent group %s", db->line, name);
154 return;
155 }
156
157 if (mt == NULL)
158 {
159 slog(LG_INFO, "db-h-gacl: line %d: groupacs for nonexistent entity %s", db->line, entity);
160 return;
161 }
162
163 if (loading_gdbv >= 2)
164 {
165 flagset = db_sread_word(db);
166
167 if (!gflags_fromstr(ga_flags, flagset, &flags))
168 slog(LG_INFO, "db-h-gacl: line %d: confused by flags: %s", db->line, flagset);
169
170 /* ACL view permission was added, so make up the permission (#279), but only if the database
171 * is from atheme 7.1 or earlier. --kaniini
172 */
173 if (!(their_ga_all & GA_ACLVIEW) && ((flags & GA_ALL_OLD) == their_ga_all))
174 flags |= GA_ACLVIEW;
175 }
176
177 groupacs_add(mg, mt, flags);
178 }
179
180 static void db_h_mdg(database_handle_t *db, const char *type)
181 {
182 const char *name = db_sread_word(db);
183 const char *prop = db_sread_word(db);
184 const char *value = db_sread_str(db);
185 void *obj = NULL;
186
187 obj = mygroup_find(name);
188
189 if (obj == NULL)
190 {
191 slog(LG_INFO, "db-h-mdg: attempting to add %s property to non-existant object %s",
192 prop, name);
193 return;
194 }
195
196 metadata_add(obj, prop, value);
197 }
198
199 void gs_db_init(void)
200 {
201 hook_add_db_write_pre_ca(write_groupdb);
202
203 db_register_type_handler("GDBV", db_h_gdbv);
204 db_register_type_handler("GRP", db_h_grp);
205 db_register_type_handler("GACL", db_h_gacl);
206 db_register_type_handler("MDG", db_h_mdg);
207 db_register_type_handler("GFA", db_h_gfa);
208 }
209
210 void gs_db_deinit(void)
211 {
212 hook_del_db_write_pre_ca(write_groupdb);
213
214 db_unregister_type_handler("GDBV");
215 db_unregister_type_handler("GRP");
216 db_unregister_type_handler("GACL");
217 db_unregister_type_handler("MDG");
218 db_unregister_type_handler("GFA");
219 }