summaryrefslogtreecommitdiffstatsabout
path: root/utility/util.c
blob: 7ecb9025fc538c77be810e4d0cd7bd8bca4536ef (plain)
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
#include "util.h"
#include "apr_strings.h"
#include "apr_lib.h"
#include "apr_file_io.h"

#include "config.h"

#include <stdarg.h>

char *lowerstr(apr_pool_t *pool, const char *input)
{
    char *temp;
    char *itr;
    temp = apr_pstrdup(pool, input);
    for (itr=temp; *itr!='\0'; itr++) {
        *itr = apr_tolower(*itr);
    }
    return temp;
}

void line_chomp(char *str)
{
    int len;
    // chomp off newline
    len = strlen(str);
    if (len) {
        while (str[len-1] == '\r' || str[len-1] == '\n') {
            str[len-1] = '\0';
            len--;
        }
    }
}

/*
 * *** Ripped from HTTPD util.c (why are so many PORTABLE things not in APR UTIL?)
 */
static char x2c(const char *what)
{
    register char digit;

    digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10
             : (what[0] - '0'));
    digit *= 16;
    digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10
              : (what[1] - '0'));
    return (digit);
}

/*
 * *** Ripped from HTTPD util.c (why are so many PORTABLE things not in APR UTIL?)
 *
 * Unescapes a URL, leaving reserved characters intact.
 * Returns 0 on success, non-zero on error
 * Failure is due to
 *   bad % escape       returns HTTP_BAD_REQUEST
 *
 *   decoding %00 or a forbidden character returns HTTP_NOT_FOUND
 */
static int unescape_url(char *url, const char *forbid, const char *reserved)
{
    register int badesc, badpath;
    char *x, *y;

    badesc = 0;
    badpath = 0;
    /* Initial scan for first '%'. Don't bother writing values before
     * seeing a '%' */
    y = strchr(url, '%');
    if (y == NULL) {
        return APR_SUCCESS;
    }
    for (x = y; *y; ++x, ++y) {
        if (*y != '%') {
            *x = *y;
        }
        else {
            if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) {
                badesc = 1;
                *x = '%';
            }
            else {
                char decoded;
                decoded = x2c(y + 1);
                if ((decoded == '\0')
                    || (forbid && strchr(forbid, decoded))) {
                    badpath = 1;
                    *x = decoded;
                    y += 2;
                }
                else if (reserved && strchr(reserved, decoded)) {
                    *x++ = *y++;
                    *x++ = *y++;
                    *x = *y;
                }
                else {
                    *x = decoded;
                    y += 2;
                }
            }
        }
    }
    *x = '\0';
    if (badesc) {
        return APR_EINVAL;
    }
    else if (badpath) {
        return APR_EINVAL;
    }
    else {
        return APR_SUCCESS;
    }
}

/*
 * *** Ripped from HTTPD util.c (why are so many PORTABLE things not in APR UTIL?)
 */
int ap_unescape_url(char *url)
{
    /* Traditional */
#ifdef CASE_BLIND_FILESYSTEM
    return unescape_url(url, "/\\", NULL);
#else
    return unescape_url(url, "/", NULL);
#endif
}

void logging_init(config_t *cfg)
{
    apr_status_t rv;
    apr_pool_create(&cfg->errorlog_p, cfg->pool);
    apr_file_open_stderr(&cfg->errorlog_fperr, cfg->pool);
    if (cfg->errorlog) {
        rv = apr_file_open(&cfg->errorlog_fp, cfg->errorlog,
                APR_FOPEN_CREATE | APR_FOPEN_WRITE | APR_FOPEN_APPEND,
                APR_OS_DEFAULT,
                cfg->pool);
        if (rv) {
            printf("Error opening %s\n",cfg->errorlog);
            cfg->loglevel = LOGLEVEL_NONE;
        }
        logging_log(cfg, LOGLEVEL_ERROR, "Log file Opened");
    } else {
        cfg->loglevel = LOGLEVEL_NONE;
        logging_log(cfg, LOGLEVEL_NOISE, "No Log file specified, disabled logging");
    }
}

const char *logging_strerror(apr_status_t rv)
{
    char buff[256];
    return apr_strerror(rv, buff, 256);
}

void logging_log(config_t *cfg, loglevel_e level, const char *fmt, ...)
{
    va_list ap;
    struct iovec vec[2];
    apr_size_t blen;

    if (cfg->loglevel < level) return;

    va_start(ap, fmt);
    apr_pool_clear(cfg->errorlog_p);

    vec[0].iov_base = apr_pvsprintf(cfg->errorlog_p, fmt, ap);
    vec[0].iov_len = strlen(vec[0].iov_base);
    vec[1].iov_base = "\n";
    vec[1].iov_len = 1;

    if (level == LOGLEVEL_NOISE) {
        apr_file_writev(cfg->errorlog_fperr,vec,2,&blen);
    }
    if (cfg->loglevel > LOGLEVEL_NONE) {
        apr_file_writev(cfg->errorlog_fp,vec,2,&blen);
    }

    va_end(ap);
}