Debian Codegraph @

contrib/mod_ifversion.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_ifversion -- a module supporting conditional configuration
3 * depending on the proftpd server version
4 *
5 * Copyright (c) 2009-2015 TJ Saunders
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
20 *
21 * As a special exemption, TJ Saunders and other respective copyright holders
22 * give permission to link this program with OpenSSL, and distribute the
23 * resulting executable, without including the source code for OpenSSL in the
24 * source distribution.
25 *
26 * This is mod_ifversion, contrib software for proftpd 1.3.x and above.
27 * For more information contact TJ Saunders <[email protected]>.
28 */
29
30 #include "conf.h"
31
32 #define MOD_IFVERSION_VERSION "mod_ifversion/0.1"
33
34 /* Make sure the version of proftpd is as necessary. */
35 #if PROFTPD_VERSION_NUMBER < 0x0001030602
36 # error "ProFTPD 1.3.6rc2 or later required"
37 #endif
38
39 /* Support routines
40 */
41
42 /* These status values are arbitrary; the important part is the values
43 * reflect the relationships:
44 *
45 * RC < stable < maint
46 */
47 #define IFVERSION_STATUS_RC 100
48 #define IFVERSION_STATUS_STABLE 300
49
50 module ifversion_module;
51
52 static int parse_version(char *version_str, unsigned int *version,
53 unsigned int *version_status) {
54 register unsigned int i;
55 char c, *ptr, *tmp;
56 int have_suffix = FALSE;
57 size_t revision_len = 0;
58
59 /* Parse the given version string. We expect to see:
60 *
61 * major[.minor[.revision[suffix]]]
62 *
63 * Examples:
64 *
65 * "1"
66 * "1.3"
67 * "1.3.3"
68 * "1.3.3rc1"
69 * "1.3.3a"
70 */
71
72 /* Quick sanity check */
73 if (!PR_ISDIGIT(version_str[0])) {
74 return -1;
75 }
76
77 /* Parse the major number */
78 ptr = strchr(version_str, '.');
79 if (ptr) {
80 *ptr = '\0';
81
82 tmp = NULL;
83 version[0] = (int) strtoul(version_str, &tmp, 10);
84 if (tmp && *tmp) {
85 *ptr = '.';
86 return -1;
87 }
88
89 *ptr = '.';
90
91 /* Make sure that there is a character following the period. */
92 if (*(ptr + 1) != '\0') {
93 version_str = ptr + 1;
94
95 } else {
96 return -1;
97 }
98
99 } else {
100 tmp = NULL;
101 version[0] = (int) strtoul(version_str, &tmp, 10);
102 if (tmp && *tmp) {
103 return -1;
104 }
105
106 return 0;
107 }
108
109 /* Parse the minor number */
110 ptr = strchr(version_str, '.');
111 if (ptr) {
112 *ptr = '\0';
113
114 tmp = NULL;
115 version[1] = (int) strtoul(version_str, &tmp, 10);
116 if (tmp && *tmp) {
117 *ptr = '.';
118 return -1;
119 }
120
121 *ptr = '.';
122
123 /* Make sure that there is a character following the period. */
124 if (*(ptr + 1) != '\0') {
125 version_str = ptr + 1;
126
127 } else {
128 return -1;
129 }
130
131 } else {
132 tmp = NULL;
133 version[1] = (int) strtoul(version_str, &tmp, 10);
134 if (tmp && *tmp) {
135 return -1;
136 }
137
138 return 0;
139 }
140
141 /* Parse the revision number. This is trickier, since we have to also
142 * account for the suffix, and there is no delimiter between the revision
143 * number and the suffix characters.
144 *
145 * We thus scan every character from here on out. If they are all digits,
146 * then it is a "stable" release (no suffix). Otherwise, it's either an
147 * RC release, a maintenance release, or it's a badly formatted version
148 * string.
149 */
150
151 for (i = 0; i < strlen(version_str); i++) {
152 if (!PR_ISDIGIT(version_str[i])) {
153 if (i > 0) {
154 have_suffix = TRUE;
155 break;
156 }
157
158 /* Syntax error */
159 return -1;
160
161 } else {
162 /* Keep track of the number of characters in the revision number; this
163 * is handy for afterwards, assuming we do have a suffix.
164 */
165 revision_len++;
166 }
167 }
168
169 if (!have_suffix) {
170 tmp = NULL;
171 version[2] = (int) strtoul(version_str, &tmp, 10);
172 if (tmp && *tmp) {
173 return -1;
174 }
175
176 /* Stable release */
177 *version_status = IFVERSION_STATUS_STABLE;
178 return 0;
179 }
180
181 ptr = version_str + revision_len;
182 c = *ptr;
183 *ptr = '\0';
184
185 tmp = NULL;
186 version[2] = (int) strtoul(version_str, &tmp, 10);
187
188 if (tmp && *tmp) {
189 *ptr = c;
190 return -1;
191 }
192
193 *ptr = c;
194
195 /* We already know, based on the suffix check, that there are characters
196 * after the revision number.
197 */
198 version_str = ptr;
199
200 /* If the next two characters are "rc" (case-insensitive) followed by
201 * digits, it's an RC release. (If there are no digits, it's a syntax
202 * error.)
203 *
204 * If there only a single character left, it is a maintenance release.
205 */
206
207 if (strlen(version_str) == 1) {
208 if (!PR_ISALPHA(version_str[0])) {
209 /* Syntax error */
210 return -1;
211 }
212
213 /* Maintenance release. */
214 c = toupper(version_str[0]);
215 *version_status = IFVERSION_STATUS_STABLE + (c - 'A');
216 return 0;
217 }
218
219 if (strncasecmp(version_str, "rc", 2) != 0) {
220 return -1;
221 }
222
223 /* RC release */
224
225 *version_status = IFVERSION_STATUS_RC;
226
227 if (strlen(version_str) == 2) {
228 return 0;
229 }
230
231 version_str += 2;
232
233 for (i = 0; i < strlen(version_str); i++) {
234 if (!PR_ISDIGIT(version_str[i])) {
235 /* Syntax error */
236 return -1;
237 }
238 }
239
240 tmp = NULL;
241 *version_status += strtoul(version_str, &tmp, 10);
242
243 if (tmp && *tmp) {
244 return -1;
245 }
246
247 return 0;
248 }
249
250 static int compare_version(pool *p, char *version_str, char **error) {
251 char *server_version_str;
252 unsigned int version_status = 0, server_version_status = 0;
253 unsigned int version[3] = { 0, 0, 0 }, server_version[3] = { 0, 0, 0 };
254 int res;
255
256 res = parse_version(version_str, version, &version_status);
257 if (res < 0) {
258 *error = pstrcat(p, "badly formatted configured version '", version_str,
259 "'", NULL);
260 return -1;
261 }
262
263 server_version_str = pstrdup(p, pr_version_get_str());
264 res = parse_version(server_version_str, server_version,
265 &server_version_status);
266 if (res < 0) {
267 *error = pstrcat(p, "badly formatted server version '", server_version_str,
268 "'", NULL);
269 return -1;
270 }
271
272 *error = NULL;
273
274 if (server_version[0] > version[0]) {
275 return 1;
276
277 } else if (server_version[0] < version[0]) {
278 return -1;
279
280 } else if (server_version[1] > version[1]) {
281 return 1;
282
283 } else if (server_version[1] < version[1]) {
284 return -1;
285
286 } else if (server_version[2] > version[2]) {
287 return 1;
288
289 } else if (server_version[2] < version[2]) {
290 return -1;
291
292 } else if (server_version_status > version_status) {
293 return 1;
294
295 } else if (server_version_status < version_status) {
296 return -1;
297 }
298
299 /* Appear to be the same versions. */
300 return 0;
301 }
302
303 static int match_version(pool *p, const char *pattern_str, char **error) {
304 #ifdef PR_USE_REGEX
305 pr_regex_t *pre;
306 int res;
307
308 pre = pr_regexp_alloc(&ifversion_module);
309
310 res = pr_regexp_compile(pre, pattern_str, REG_EXTENDED|REG_NOSUB|REG_ICASE);
311 if (res != 0) {
312 char errstr[256];
313
314 memset(errstr, '\0', sizeof(errstr));
315 pr_regexp_error(res, pre, errstr, sizeof(errstr)-1);
316
317 pr_regexp_free(NULL, pre);
318 *error = pstrcat(p, "unable to compile pattern '", pattern_str, "': ",
319 errstr, NULL);
320
321 return 0;
322 }
323
324 res = pr_regexp_exec(pre, pr_version_get_str(), 0, NULL, 0, 0, 0);
325 if (res != 0) {
326 *error = pstrcat(p, "server version '", pr_version_get_str(),
327 "' failed to match pattern '", pattern_str, "'", NULL);
328 }
329
330 pr_regexp_free(NULL, pre);
331 return (res == 0 ? 1 : 0);
332
333 #else
334 *error = pstrdup(p, "system does not support POSIX regular expressions");
335 return 0;
336 #endif /* regex support */
337 }
338
339 /* Configuration handlers
340 */
341
342 /* Usage: <IfVersion [!]op version-string|regex> */
343 MODRET start_ifversion(cmd_rec *cmd) {
344 unsigned int ifversion_ctx_count = 1;
345 int compared, matched = FALSE, negated = FALSE;
346 char buf[PR_TUNABLE_BUFFER_SIZE], *config_line = NULL;
347 char *error = NULL, *version_str = NULL, *op_str = NULL;
348 size_t op_len;
349
350 if (cmd->argc-1 == 0 ||
351 cmd->argc-1 > 2) {
352 CONF_ERROR(cmd, "wrong number of parameters");
353 }
354
355 if (cmd->argc-1 == 2) {
356 op_str = cmd->argv[1];
357
358 if (*op_str == '!' &&
359 strlen(op_str) > 1) {
360 negated = TRUE;
361 op_str++;
362 }
363
364 op_len = strlen(op_str);
365 version_str = cmd->argv[2];
366
367 } else {
368 /* Assume that if only a version-string was supplied, the operator
369 * is intended to be the equality operator.
370 */
371 op_str = "=";
372 op_len = 1;
373 version_str = cmd->argv[1];
374 }
375
376 switch (*op_str) {
377 case '=':
378 if (*version_str != '/') {
379 /* Normal equality comparison */
380 compared = compare_version(cmd->tmp_pool, version_str, &error);
381 if (error != NULL) {
382 CONF_ERROR(cmd, error);
383 }
384
385 matched = (compared == 0);
386 break;
387 }
388
389 /* Otherwise, it's a regular expression */
390 if (version_str[strlen(version_str)-1] != '/') {
391 CONF_ERROR(cmd, "Missing terminating '/' of regular expression");
392 }
393
394 /* Fall through to the next case in order to handle/evaluate the
395 * regular expression. Be sure to remove the bracketing '/' characters
396 * for the regex compilation.
397 */
398 version_str[strlen(version_str)-1] = '\0';
399 version_str++;
400
401 case '~':
402 /* Regular expression */
403 matched = match_version(cmd->tmp_pool, version_str, &error);
404 if (error != NULL) {
405 CONF_ERROR(cmd, error);
406 }
407
408 break;
409
410 case '<':
411 compared = compare_version(cmd->tmp_pool, version_str, &error);
412 if (error != NULL) {
413 CONF_ERROR(cmd, error);
414 }
415
416 if (compared == -1 ||
417 (op_len == 2 && compared == 0)) {
418 matched = TRUE;
419 }
420
421 break;
422
423 case '>':
424 compared = compare_version(cmd->tmp_pool, version_str, &error);
425 if (error != NULL) {
426 CONF_ERROR(cmd, error);
427 }
428
429 if (compared == 1 ||
430 (op_len == 2 && compared == 0)) {
431 matched = TRUE;
432 }
433
434 break;
435
436 default:
437 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unknown comparison operator '",
438 op_str, "'", NULL));
439 }
440
441 if ((matched && !negated) ||
442 (!matched && negated)) {
443 pr_log_debug(DEBUG3, "%s: using '%s %s' section at line %u",
444 (char *) cmd->argv[0], (char *) cmd->argv[1], (char *) cmd->argv[2],
445 pr_parser_get_lineno()); return PR_HANDLED(cmd);
446 }
447
448 pr_log_debug(DEBUG3, "%s: skipping '%s %s' section at line %u",
449 (char *) cmd->argv[0], (char *) cmd->argv[1], (char *) cmd->argv[2],
450 pr_parser_get_lineno());
451
452 while (ifversion_ctx_count > 0 &&
453 (config_line = pr_parser_read_line(buf, sizeof(buf))) != NULL) {
454 pr_signals_handle();
455
456 if (strncasecmp(config_line, "<IfVersion", 10) == 0) {
457 ifversion_ctx_count++;
458 }
459
460 if (strcasecmp(config_line, "</IfVersion>") == 0) {
461 ifversion_ctx_count--;
462 }
463 }
464
465 /* If there are still unclosed <IfVersion> sections, signal an error.
466 */
467 if (ifversion_ctx_count > 0) {
468 CONF_ERROR(cmd, "unclosed <IfVersion> section");
469 }
470
471 return PR_HANDLED(cmd);
472 }
473
474 MODRET end_ifversion(cmd_rec *cmd) {
475 return PR_HANDLED(cmd);
476 }
477
478 /* Module API tables
479 */
480
481 static conftable ifversion_conftab[] = {
482 { "<IfVersion>", start_ifversion, NULL },
483 { "</IfVersion>", end_ifversion, NULL },
484 { NULL }
485 };
486
487 module ifversion_module = {
488 NULL, NULL,
489
490 /* Module API version 2.0 */
491 0x20,
492
493 /* Module name */
494 "ifversion",
495
496 /* Module configuration handler table */
497 ifversion_conftab,
498
499 /* Module command handler table */
500 NULL,
501
502 /* Module authentication handler table */
503 NULL,
504
505 /* Module initialization function */
506 NULL,
507
508 /* Session initialization function */
509 NULL,
510
511 /* Module version */
512 MOD_IFVERSION_VERSION
513 };