-
Notifications
You must be signed in to change notification settings - Fork 0
/
certs.c
412 lines (360 loc) · 10.2 KB
/
certs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include <string.h>
#include <apr.h>
#include <apr_strings.h>
#include <httpd.h>
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>
#include "private.h"
#include "buffer.h"
#include "db.h"
#include "req.h"
#include "db_xml.h"
#include "xml_util.h"
#include "acct_maint.h"
#include "style.h"
#include "util.h"
#include "certs.h"
int
virgule_cert_num_levels (VirguleReq *vr)
{
int count;
for (count = 0;; count )
if (!vr->priv->cert_level_names[count])
break;
return count;
}
CertLevel
virgule_cert_level_from_name (VirguleReq *vr, const char *name)
{
int i;
for (i = 0;; i ) {
if (!vr->priv->cert_level_names[i])
break;
if (!strcmp (name, vr->priv->cert_level_names[i]))
return i;
}
return CERT_LEVEL_NONE;
}
const char *
virgule_cert_level_to_name (VirguleReq *vr, CertLevel level)
{
if (level >= 0 && level < virgule_cert_num_levels (vr))
return vr->priv->cert_level_names[level];
return "None";
}
/**
* virgule_cert_verify_outbound - Checks the specified outbound certification
* for symmetry with an inbound cert record within the subject's profile.
* Unlike other cert functions, existing certification dates are preserved.
*
* Returns:
* 0 No action needed, certs are symmetric
* 1 Cert levels asymmetric, normalized to issuer level
* 2 Subject cert missing, created from issuer cert
* 3 Subject profile does not exist
**/
int
virgule_cert_verify_outbound (VirguleReq *vr, apr_pool_t *p, const char *issuer, const char *subject, const char *level, const char *date)
{
xmlDoc *profile;
xmlNode *certs, *cert;
int rc = 0;
char *sname;
char *slevel = NULL;
char *db_key = apr_pstrcat (p, "acct/", subject, "/profile.xml", NULL);
profile = virgule_db_xml_get (p, vr->db, db_key);
if (profile == NULL)
return 3;
certs = virgule_xml_ensure_child (profile->xmlRootNode, "certs-in");
if (certs != NULL)
{
for (cert = certs->children; cert != NULL; cert = cert->next)
{
if (cert->type != XML_ELEMENT_NODE || xmlStrcmp (cert->name, (xmlChar *)"cert"))
continue;
sname = (char *)xmlGetProp (cert, (xmlChar *)"issuer");
if (sname)
{
if (!strcmp (sname, issuer))
{
slevel = (char *)xmlGetProp (cert, (xmlChar *)"level");
break;
}
xmlFree (sname);
}
}
}
if(slevel != NULL)
{
/* subject inbound cert exists and matches issuer cert! */
if (!strcmp (slevel, level))
{
xmlFree(slevel);
return 0;
}
/* subject inbound cert exists but is incorrect */
xmlFree(slevel);
rc = 1;
}
else
{
/* subject inbound cert is missing, create new cert */
cert = xmlNewChild (certs, NULL, (xmlChar *)"cert", NULL);
rc = 2;
}
/* correct the cert */
xmlSetProp (cert, (xmlChar *)"issuer", (xmlChar *)issuer);
xmlSetProp (cert, (xmlChar *)"level", (xmlChar *)level);
if(date != NULL)
xmlSetProp (cert, (xmlChar *)"date", (xmlChar *)date);
virgule_db_xml_put (p, vr->db, db_key, profile);
virgule_db_xml_free (p, profile);
return rc;
}
/**
* virgule_cert_verify_inbound - Checks the specified inbound certification
* for symmetry with an outbound cert record within the issuer's profile.
* Unlike other cert functions, existing certification dates are preserved.
* Note: error numbers are shared with virgule_cert_verify_outbound.
*
* Returns:
* 0 No action needed, certs are symmetric
* 4 Issuer cert missing, created from subject cert
* 5 Issuer cert exists but levels don't match
* 6 Issuer profile does not exist
**/
int
virgule_cert_verify_inbound (VirguleReq *vr, apr_pool_t *p, const char *subject, const char *issuer, const char *level, const char *date)
{
xmlDoc *profile;
xmlNode *certs, *cert;
int rc = 0;
char *sname;
char *slevel = NULL;
char *db_key = apr_pstrcat (p, "acct/", issuer, "/profile.xml", NULL);
profile = virgule_db_xml_get (p, vr->db, db_key);
if (profile == NULL)
return 6;
certs = virgule_xml_ensure_child (profile->xmlRootNode, "certs");
if (certs != NULL)
{
for (cert = certs->children; cert != NULL; cert = cert->next)
{
if (cert->type != XML_ELEMENT_NODE || xmlStrcmp (cert->name, (xmlChar *)"cert"))
continue;
sname = (char *)xmlGetProp (cert, (xmlChar *)"subj");
if (sname)
{
if (!strcmp (sname, subject))
{
slevel = (char *)xmlGetProp (cert, (xmlChar *)"level");
break;
}
xmlFree (sname);
}
}
}
if(slevel != NULL)
{
/* issuer outbound cert exists and matches subject cert! */
if (!strcmp (slevel, level))
rc = 0;
/* issuer outbound cert exists but level is incorrect */
else
rc = 5;
xmlFree(slevel);
}
else
{
/* subject inbound cert is missing, create new cert */
cert = xmlNewChild (certs, NULL, (xmlChar *)"cert", NULL);
xmlSetProp (cert, (xmlChar *)"subj", (xmlChar *)subject);
xmlSetProp (cert, (xmlChar *)"level", (xmlChar *)level);
if(date != NULL)
xmlSetProp (cert, (xmlChar *)"date", (xmlChar *)date);
virgule_db_xml_put (p, vr->db, db_key, profile);
virgule_db_xml_free (p, profile);
rc = 4;
}
return rc;
}
CertLevel
virgule_cert_get (VirguleReq *vr, const char *issuer, const char *subject)
{
apr_pool_t *p = vr->r->pool;
Db *db = vr->db;
char *db_key;
xmlDoc *profile;
xmlNode *tree;
xmlNode *cert;
CertLevel result = CERT_LEVEL_NONE;
db_key = virgule_acct_dbkey (vr, issuer);
profile = virgule_db_xml_get (p, db, db_key);
tree = virgule_xml_find_child (profile->xmlRootNode, "certs");
if (tree == NULL)
return CERT_LEVEL_NONE;
for (cert = tree->children; cert != NULL; cert = cert->next)
{
if (cert->type == XML_ELEMENT_NODE &&
!xmlStrcmp (cert->name, (xmlChar *)"cert"))
{
char *cert_subj;
cert_subj = (char *)xmlGetProp (cert, (xmlChar *)"subj");
if (cert_subj)
{
if (!strcmp (cert_subj, subject))
{
char *cert_level;
cert_level = (char *)xmlGetProp (cert, (xmlChar *)"level");
result = virgule_cert_level_from_name (vr, cert_level);
xmlFree (cert_level);
xmlFree (cert_subj);
break;
}
xmlFree (cert_subj);
}
}
}
virgule_db_xml_free (p, profile);
return result;
}
/**
* Adds a certification of level to subject from issuer
*/
int
virgule_cert_set (VirguleReq *vr, const char *issuer, const char *subject, CertLevel level)
{
apr_pool_t *p = vr->r->pool;
Db *db = vr->db;
char *db_key;
xmlDoc *profile;
xmlNode *tree;
xmlNode *cert;
int status;
/* update subject first because it's more likely not to exist. */
db_key = virgule_acct_dbkey (vr, subject);
profile = virgule_db_xml_get (p, db, db_key);
if (profile == NULL)
return -1;
tree = virgule_xml_ensure_child (profile->xmlRootNode, "certs-in");
for (cert = tree->children; cert != NULL; cert = cert->next)
{
if (cert->type == XML_ELEMENT_NODE &&
!xmlStrcmp (cert->name, (xmlChar *)"cert"))
{
char *cert_issuer;
cert_issuer = (char *)xmlGetProp (cert, (xmlChar *)"issuer");
if (cert_issuer)
{
if (!strcmp (cert_issuer, issuer))
{
xmlFree (cert_issuer);
break;
}
xmlFree (cert_issuer);
}
}
}
if (cert == NULL)
{
cert = xmlNewChild (tree, NULL, (xmlChar *)"cert", NULL);
xmlSetProp (cert, (xmlChar *)"issuer", (xmlChar *)issuer);
}
if (level == CERT_LEVEL_NONE)
{
xmlUnlinkNode(cert);
xmlFreeNode(cert);
}
else
{
xmlSetProp (cert, (xmlChar *)"level", (xmlChar *)virgule_cert_level_to_name (vr, level));
xmlSetProp (cert, (xmlChar *)"date", (xmlChar *)virgule_iso_now(vr->r->pool));
}
status = virgule_db_xml_put (p, db, db_key, profile);
virgule_db_xml_free (p, profile);
/* then, update issuer */
db_key = virgule_acct_dbkey (vr, issuer);
profile = virgule_db_xml_get (p, db, db_key);
if (profile == NULL)
return -1;
tree = virgule_xml_ensure_child (profile->xmlRootNode, "certs");
for (cert = tree->children; cert != NULL; cert = cert->next)
{
if (cert->type == XML_ELEMENT_NODE &&
!xmlStrcmp (cert->name, (xmlChar *)"cert"))
{
char *cert_subj;
cert_subj = (char *)xmlGetProp (cert, (xmlChar *)"subj");
if (cert_subj)
{
if (!strcmp (cert_subj, subject))
{
xmlFree (cert_subj);
break;
}
xmlFree (cert_subj);
}
}
}
if (cert == NULL)
{
cert = xmlNewChild (tree, NULL, (xmlChar *)"cert", NULL);
xmlSetProp (cert, (xmlChar *)"subj", (xmlChar *)subject);
}
if (level == CERT_LEVEL_NONE)
{
xmlUnlinkNode(cert);
xmlFreeNode(cert);
}
else
{
xmlSetProp (cert, (xmlChar *)"level", (xmlChar *)virgule_cert_level_to_name (vr, level));
xmlSetProp (cert, (xmlChar *)"date", (xmlChar *)virgule_iso_now(vr->r->pool));
}
status = virgule_db_xml_put (p, db, db_key, profile);
virgule_db_xml_free (p, profile);
return status;
}
/**
* render_cert_level_begin: Begin rendering cert level.
* @vr: The #VirguleReq context.
* @user: The username.
*
* Renders the beginning of the cert level, currently by beginning a table
* with the corresponding background color. Rendering goes to vr->b.
**/
CertLevel
virgule_render_cert_level_begin (VirguleReq *vr, const char *user, CertStyle cs)
{
const char *level;
CertLevel cert_level;
if (virgule_user_is_special(vr,user))
cert_level = virgule_cert_num_levels (vr);
else
{
level = virgule_req_get_tmetric_level (vr, user);
cert_level = virgule_cert_level_from_name (vr, level);
}
virgule_buffer_printf (vr->b, "<%s class=\"level%d\">", cs, cert_level);
return cert_level;
}
/**
* render_cert_level_end: End rendering cert level.
* @vr: The #VirguleReq context.
* @level: The cert level, as a string.
*
* Closes the html opened by render_cert_level_begin().
**/
void
virgule_render_cert_level_end (VirguleReq *vr, CertStyle cs)
{
virgule_buffer_printf (vr->b, "</%s>\n", cs);
}
void
virgule_render_cert_level_text (VirguleReq *vr, const char *user)
{
Buffer *b = vr->b;
const char *level;
level = virgule_req_get_tmetric_level (vr, user);
virgule_buffer_printf (b, " <span style=\"display: none\">(%s)</span>", level);
}