forked from vab/mod_virgule
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreq.c
280 lines (240 loc) · 6.99 KB
/
req.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
#include <apr.h>
#include <apr_strings.h>
#include <httpd.h>
#include <http_log.h>
#include "private.h"
#include "buffer.h"
#include "db.h"
#include "apache_util.h"
#include "req.h"
#include "certs.h"
#include "auth.h"
#include "tmetric.h"
#include "util.h"
/* Send http header and buffer. Probably redunant at this point? */
int
virgule_send_response (VirguleReq *vr)
{
return virgule_buffer_send_response (vr->r, vr->b);
}
/**
* get_args_table: Get the uri args in table form.
* @vr: The request record.
*
* Gets the uri args, returning them in apache table form.
*
* Return value: The table containing the args, or NULL if error.
**/
apr_table_t *
virgule_get_args_table (VirguleReq *vr)
{
apr_pool_t *p = vr->r->pool;
char *args;
apr_table_t *result;
const char *arg_p, *entry;
char *key, *val;
args = vr->args;
if (args == NULL)
return NULL;
/* todo: check content-type for POSTed args */
result = apr_table_make (p, 8);
arg_p = args;
while (arg_p[0] != '\0' && (entry = ap_getword (p, &arg_p, '&')))
{
key = ap_getword (p, &entry, '=');
val = apr_pstrdup (p, entry);
ap_unescape_url (key);
virgule_unescape_url_info (val);
apr_table_merge (result, key, val);
}
return result;
}
/**
* req_get_tmetric: Get the trust metric results.
* @vr: The #VirguleReq context.
*
* Gets the trust metric results in simple username->level ap table
* form. Note: ap tables are (as of the time of writing) stored
* linearly and not in a tree or hash table format. Thus, if you're
* doing a lot of queries, performance could kinda suck.
*
* Currently, this only bothers with the default trust root, but that
* could easily change. In that case, this routine will suck trust
* root preference info out of the user's profile.
*
* The trust metric cache is loaded into a sub pool of the Apache thread
* private memory pool. This allows us to preserve the trust data across
* hits once it's loaded. A stat is done on each tmetric request and, if
* a newer cache is available, the current one is dumped, the sub pool is
* destroyed and a reload occurs. The sub pool also gets destroyed if the
* entire thread private pool gets destroyed, which occurs (rarely) when
* the mod_virgule config file has changed and needs to be reloaded.
*
* Return value: The trust metric results.
**/
char *
virgule_req_get_tmetric (VirguleReq *vr)
{
apr_finfo_t finfo;
/* Stat the trust metric cache file */
apr_stat(&finfo, ap_make_full_path (vr->r->pool, vr->priv->base_path, "tmetric/default"),
APR_FINFO_MIN, vr->r->pool);
/* Load the trust metric cache and reset timestamp */
if (vr->priv->tmetric == NULL || finfo.mtime != vr->priv->tm_mtime)
{
/* free existing memory if needed */
if(vr->priv->tmetric != NULL)
{
apr_pool_destroy (vr->priv->tm_pool);
vr->priv->tm_mtime = 0L;
}
/* allocate a sub pool and load the tmetric cache */
apr_pool_create(&vr->priv->tm_pool, vr->priv->pool);
vr->priv->tmetric = virgule_tmetric_get (vr);
vr->priv->tm_mtime = finfo.mtime;
}
return vr->priv->tmetric;
}
/**
* req_get_tmetric_level: Get the trust metric level of a user.
* @vr: The #VirguleReq context.
* @u: The account name.
*
* Gets the certification level of @u according to the default trust
* metric.
*
* Return value: The certification level, as a string.
**/
const char *
virgule_req_get_tmetric_level (VirguleReq *vr, const char *u)
{
char *result;
char *user = NULL;
char *tmetric = NULL;
int i, j;
if (u == NULL || *u == 0)
return virgule_cert_level_to_name (vr, CERT_LEVEL_NONE);
user = ap_escape_uri(vr->r->pool,u);
tmetric = virgule_req_get_tmetric (vr);
if (tmetric == NULL)
return virgule_cert_level_to_name (vr, CERT_LEVEL_NONE);
/* This is a hackish linear scan through the tmetric table.
At some point, we may want to use a real binary tree. */
for (i = 0; tmetric[i];)
{
for (j = 0; user[j]; j++)
if (tmetric[i++] != user[j])
break;
if (user[j] == 0 && tmetric[i++] == ' ')
{
/* found */
for (j = 0; tmetric[i + j] && tmetric[i + j] != '\n'; j++);
result = apr_palloc (vr->r->pool, j + 1);
memcpy (result, tmetric + i, j);
result[j] = 0;
return result;
}
else
{
/* skip to next line */
while (tmetric[i] && tmetric[i] != '\n')
i++;
if (tmetric[i] == '\n')
i++;
}
}
return virgule_cert_level_to_name (vr, CERT_LEVEL_NONE);
}
/**
* req_ok_to_post: Determine if it's ok for the user to post.
* @vr: The #VirguleReq context.
*
* Checks the users trust certification against the minimum cert level set
* in the config.xml file.
*
* Return value: TRUE if it's ok for the user to post.
**/
int
virgule_req_ok_to_post (VirguleReq *vr)
{
virgule_auth_user (vr);
if (vr->u == NULL)
return 0;
if(virgule_user_is_special(vr, vr->u))
return 1;
if(vr->priv->article_post_by_editors_only && *vr->priv->editors)
{
const char **s;
for (s = vr->priv->editors; *s; s++)
if(strcmp(vr->u,*s) == 0)
return 1;
}
else if(virgule_cert_level_from_name(vr, virgule_req_get_tmetric_level (vr, vr->u)) >= vr->priv->level_articlepost)
return 1;
return 0;
}
/**
* req_ok_to_reply: Determine if it's ok for the user to reply to posts.
* @vr: The #VirguleReq context.
*
* Checks the users trust certification against the minimum cert level set
* in the config.xml file.
*
* Return value: TRUE if it's ok for the user to reply to posts.
**/
int
virgule_req_ok_to_reply (VirguleReq *vr)
{
virgule_auth_user (vr);
if (vr->u == NULL)
return 0;
if (virgule_cert_level_from_name(vr, virgule_req_get_tmetric_level (vr, vr->u)) >= vr->priv->level_articlereply)
return 1;
if(virgule_user_is_special(vr, vr->u))
return 1;
return 0;
}
/**
* req_ok_to_create_project: Determine if it's ok for the user to create a
* new project.
* @vr: The #VirguleReq context.
*
* Checks the users trust certification against the minimum cert level set
* in the config.xml file.
*
* Return value: TRUE if it's ok for the user to create projects.
**/
int
virgule_req_ok_to_create_project (VirguleReq *vr)
{
virgule_auth_user (vr);
if (vr->u == NULL)
return 0;
if (virgule_cert_level_from_name(vr, virgule_req_get_tmetric_level (vr, vr->u)) >= vr->priv->level_projectcreate)
return 1;
if(virgule_user_is_special(vr, vr->u))
return 1;
return 0;
}
/**
* virgule_req_ok_to_syndicate_blog: Determine if it's ok for the user to
* setup blog syndication.
* @vr: The #VirguleReq context.
*
* Checks the users trust certification against the minimum cert level set
* in the config.xml file.
*
* Return value: TRUE if it's ok for the user to syndicate.
**/
int
virgule_req_ok_to_syndicate_blog (VirguleReq *vr)
{
virgule_auth_user (vr);
if (vr->u == NULL)
return 0;
if (virgule_cert_level_from_name(vr, virgule_req_get_tmetric_level (vr, vr->u)) >= vr->priv->level_blogsyndicate)
return 1;
if(virgule_user_is_special(vr, vr->u))
return 1;
return 0;
}