File: http_cgi.c

package info (click to toggle)
cyrus-imapd 3.6.1-4%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,688 kB
  • sloc: ansic: 255,928; perl: 97,730; javascript: 9,266; sh: 5,537; yacc: 2,651; cpp: 2,128; makefile: 2,099; lex: 660; xml: 621; python: 388; awk: 303; asm: 262
file content (370 lines) | stat: -rw-r--r-- 13,203 bytes parent folder | download
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
/* http_cgi.c -- Routines for handling Common Gateway Interface scripts in httpd
 *
 * Copyright (c) 1994-2019 Carnegie Mellon University.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The name "Carnegie Mellon University" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For permission or any legal
 *    details, please contact
 *      Carnegie Mellon University
 *      Center for Technology Transfer and Enterprise Creation
 *      4615 Forbes Avenue
 *      Suite 302
 *      Pittsburgh, PA  15213
 *      (412) 268-7393, fax: (412) 268-7395
 *      innovation@andrew.cmu.edu
 *
 * 4. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by Computing Services
 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
 *
 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include <config.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <ctype.h>
#include <string.h>
#include <syslog.h>
#include <assert.h>

#include "command.h"
#include "global.h"
#include "httpd.h"
#include "http_proxy.h"
#include "proxy.h"
#include "tok.h"
#include "util.h"
#include "version.h"

/* generated headers are not necessarily in current directory */
#include "imap/http_err.h"
#include "imap/imap_err.h"

static void cgi_init(struct buf *serverinfo);
static int meth_get(struct transaction_t *txn, void *params);
static int meth_post(struct transaction_t *txn, void *params);


/* Namespace for CGI */
struct namespace_t namespace_cgi = {
    URL_NS_CGI, 0, "cgi", "/cgi-bin", NULL,
    http_allow_noauth, /*authschemes*/0,
    /*mbtype*/0,
    ALLOW_READ | ALLOW_POST,
    cgi_init, NULL, NULL, NULL, NULL,
    {
        { NULL,                 NULL },                 /* ACL          */
        { NULL,                 NULL },                 /* BIND         */
        { NULL,                 NULL },                 /* CONNECT      */
        { NULL,                 NULL },                 /* COPY         */
        { NULL,                 NULL },                 /* DELETE       */
        { &meth_get,            NULL },                 /* GET          */
        { &meth_get,            NULL },                 /* HEAD         */
        { NULL,                 NULL },                 /* LOCK         */
        { NULL,                 NULL },                 /* MKCALENDAR   */
        { NULL,                 NULL },                 /* MKCOL        */
        { NULL,                 NULL },                 /* MOVE         */
        { &meth_options,        NULL },                 /* OPTIONS      */
        { NULL,                 NULL },                 /* PATCH        */
        { &meth_post,           NULL },                 /* POST         */
        { NULL,                 NULL },                 /* PROPFIND     */
        { NULL,                 NULL },                 /* PROPPATCH    */
        { NULL,                 NULL },                 /* PUT          */
        { NULL,                 NULL },                 /* REPORT       */
        { &meth_trace,          NULL },                 /* TRACE        */
        { NULL,                 NULL },                 /* UNBIND       */
        { NULL,                 NULL }                  /* UNLOCK       */
    }
};


static void cgi_init(struct buf *serverinfo __attribute__((unused)))
{
    namespace_cgi.enabled = config_httpmodules & IMAP_ENUM_HTTPMODULES_CGI;
}

/* Add request headers to environment for script */
static void req_hdr_to_env(const char *name, const char *contents,
                           const char *raw __attribute__((unused)), void *rock)
{
    struct buf *environ = (struct buf *) rock;
    const char **hdr, *skip[] =
        { "authorization", "cookie", "expect", "http2-settings",
          "proxy-authorization", "transfer-encoding", "upgrade", NULL };

    /* Ignore private headers in our cache */
    if (name[0] == ':') return;

    for (hdr = skip; *hdr && strcasecmp(name, *hdr); hdr++);

    if (!*hdr) {
        static struct buf env_name = BUF_INITIALIZER;
        const char *env_str = buf_cstring(environ);
        const char *exists;

        buf_reset(&env_name);
        buf_printf(&env_name, "\tHTTP_%s=", name);
        buf_ucase(&env_name);
        buf_replace_all(&env_name, "-", "_");

        /* See if this name already exists */
        exists = strstr(env_str, buf_cstring(&env_name));
        if (exists) {
            /* Append value to existing value(s) */
            const char *next = strchr(exists + 1, '\t');
            unsigned offset = next ? next - env_str : (unsigned) strlen(env_str);

            buf_insertcstr(environ, offset, ", ");
            buf_insertcstr(environ, offset + 2, contents);
        }
        else {
            /* Add name and value */
            buf_appendcstr(environ, buf_cstring(&env_name));
            buf_appendcstr(environ, contents);
        }
    }
}

/* Perform a GET/HEAD request */
static int meth_get(struct transaction_t *txn,
                    void *params __attribute__((unused)))
{
    int ret = 0;
    const char *prefix, *urls, *errstr = NULL;
    const char **hdr, *extra, *port, *query;
    char *script, *cwd;
    struct stat sbuf;
    strarray_t *env = NULL;
    struct command *cmd = NULL;
    hdrcache_t resp_hdrs = NULL;
    struct body_t resp_body;
    long code = 0;
    extern char **environ;
    
    memset(&resp_body, 0, sizeof(struct body_t));

    prefix = config_getstring(IMAPOPT_HTTPDOCROOT);
    if (!prefix) return HTTP_NOT_FOUND;

    if ((urls = config_getstring(IMAPOPT_HTTPALLOWEDURLS))) {
        tok_t tok = TOK_INITIALIZER(urls, " \t", TOK_TRIMLEFT|TOK_TRIMRIGHT);
        char *token;

        while ((token = tok_next(&tok)) && strcmp(token, txn->req_uri->path));
        tok_fini(&tok);

        if (!token) return HTTP_NOT_FOUND;
    }

    /* Construct path to script */
    extra = strchr(txn->req_uri->path + strlen(namespace_cgi.prefix) + 1, '/');
    buf_setcstr(&txn->buf, prefix);
    if (extra)
        buf_appendmap(&txn->buf, txn->req_uri->path, extra - txn->req_uri->path);
    else buf_appendcstr(&txn->buf, txn->req_uri->path);
    script = buf_release(&txn->buf);
    cwd = strconcat(prefix, namespace_cgi.prefix, NULL);

    /* See if script exists */
    if (stat(script, &sbuf) || !S_ISREG(sbuf.st_mode)) {
        ret = HTTP_NOT_FOUND;
        goto done;
    }

    /* See if script is executable */
    if (access(script, X_OK)) {
        syslog(LOG_ERR, "CGI script %s is not executable",
               txn->req_uri->path);
        txn->error.desc = "CGI script is not executable";
        ret = HTTP_SERVER_ERROR;
        goto done;
    }

    /* Construct environment for script */
    buf_setcstr(&txn->buf, "GATEWAY_INTERFACE=CGI/1.1");
    if ((hdr = spool_getheader(txn->req_hdrs, "Content-Length"))) {
        buf_printf(&txn->buf, "\tCONTENT_LENGTH=%s", hdr[0]);
    }
    if ((hdr = spool_getheader(txn->req_hdrs, "Content-Type"))) {
        buf_printf(&txn->buf, "\tCONTENT_TYPE=%s", hdr[0]);
    }
    buf_printf(&txn->buf, "\tPATH_INFO=%s", extra ? extra : "");
    if (extra) {
        buf_printf(&txn->buf, "\tPATH_TRANSLATED=%s%s", prefix, extra);
    }
    buf_printf(&txn->buf, "\tQUERY_STRING=%s",
               (query = URI_QUERY(txn->req_uri)) ? query : "");
    buf_printf(&txn->buf, "\tREMOTE_ADDR=%.*s",
               (int) (httpd_remoteip ? strcspn(httpd_remoteip, ";") : 0),
               httpd_remoteip);
    buf_printf(&txn->buf, "\tREMOTE_HOST=%.*s",
               (int) (txn->conn->clienthost ?
                      strcspn(txn->conn->clienthost, " ") : 0),
               txn->conn->clienthost);
    if (httpd_userid) {
        buf_printf(&txn->buf, "\tREMOTE_USER=%s", httpd_userid);

        if (txn->auth_chal.scheme) {
            buf_printf(&txn->buf, "\tAUTH_TYPE=%s", txn->auth_chal.scheme->name);
        }
    }
    buf_printf(&txn->buf, "\tREQUEST_METHOD=%s", http_methods[txn->meth].name);
    buf_printf(&txn->buf, "\tSCRIPT_NAME=%s", script + strlen(prefix));
    buf_printf(&txn->buf, "\tSERVER_NAME=%s", config_servername);
    buf_printf(&txn->buf, "\tSERVER_PORT=%s",
               (port = strchr(httpd_localip ? httpd_localip : "", ';')) ?
               port + 1 : (https ? "443" : "80"));
    buf_printf(&txn->buf, "\tSERVER_PROTOCOL=%s", HTTP_VERSION);
    buf_printf(&txn->buf, "\tSERVER_SOFTWARE=%s", buf_cstring(&serverinfo));

    /* Add some HTTP headers from request */
    spool_enum_hdrcache(txn->req_hdrs, &req_hdr_to_env, &txn->buf);

    env = strarray_splitm(buf_release(&txn->buf), "\t", 0);
    strarray_append(env, NULL);
    environ = env->data;

    /* Run script */
    if (command_popen(&cmd, "rw", cwd, script, NULL)) {
        ret = HTTP_SERVER_ERROR;
        goto done;
    }

    /* Send request body */
    prot_putbuf(cmd->stdin_prot, &txn->req_body.payload);
    prot_flush(cmd->stdin_prot);

    /* Read response headers */
    ret = http_read_headers(cmd->stdout_prot,
                            0 /* read_sep */, &resp_hdrs, &errstr);
    if (ret) {
        syslog(LOG_ERR, "Failed to read headers from CGI script %s",
               txn->req_uri->path);
        txn->error.desc = "Failed to read headers from CGI script";
        ret = HTTP_SERVER_ERROR;
    }
    else {
        /* Read [CR]LF separating headers and body */
        int c = prot_getc(cmd->stdout_prot);

        if (c == '\r') c = prot_getc(cmd->stdout_prot);
        if (c != '\n') {
            syslog(LOG_ERR, "Failed to read newline from CGI script %s",
                   txn->req_uri->path);
            txn->error.desc = "Failed to read newline from CGI script";
            ret = HTTP_SERVER_ERROR;
        }

        if (!ret) {
            /* Check for and read response body */
            hdr = spool_getheader(resp_hdrs, "Content-Type");
            if (hdr) {
                txn->resp_body.type = hdr[0];

                resp_body.flags = BODY_RESPONSE | BODY_CLOSE;
                ret = http_read_body(cmd->stdout_prot,
                                     resp_hdrs, &resp_body, &errstr);
                if (ret) {
                    syslog(LOG_ERR, "Failed to body from CGI script %s",
                           txn->req_uri->path);
                    txn->error.desc = "Failed to read body from CGI script";
                    ret = HTTP_SERVER_ERROR;
                }
            }
        }
    }

    if (command_pclose(&cmd)) ret = HTTP_SERVER_ERROR;

    if (ret) goto done;

    /* Check for a status code */
    hdr = spool_getheader(resp_hdrs, "Status");
    if (hdr) code = http_status_to_code(atoi(hdr[0]));

    /* Check the type of the CGI response */
    hdr = spool_getheader(resp_hdrs, "Location");
    if (hdr) {
        /* Redirect */
        if (hdr[0][0] == '/') {
            /* Local Redirect */
            txn->flags.redirect = 1;

            /* Reset the URI part of our current transaction */
            xmlFreeURI(txn->req_uri);
            free_hash_table(&txn->req_qparams, (void (*)(void *)) &freestrlist);

            /* Examine new request */
            ret = examine_request(txn, hdr[0]);

            /* Reprocess the requested method on new URI */
            if (!ret) ret = process_request(txn);

            goto done;
        }
        else {
            /* Client Redirect */
            if (!code) code = HTTP_FOUND;
            txn->location = hdr[0];
        }
    }
    else {
        /* Document */
        if (!code) code = HTTP_OK;
    }

    /* Output response */
    txn->resp_body.extra_hdrs = resp_hdrs;
    write_body(code, txn,
               buf_base(&resp_body.payload), buf_len(&resp_body.payload));

  done:
    spool_free_hdrcache(resp_hdrs);
    buf_free(&resp_body.payload);
    strarray_free(env);
    free(script);
    free(cwd);

    return ret;
}

/* Perform a POST request */
static int meth_post(struct transaction_t *txn, void *params)
{
    int ret;

    /* Read request body */
    txn->req_body.flags |= BODY_DECODE;
    ret = http_read_req_body(txn);

    if (ret) {
        txn->flags.conn = CONN_CLOSE;
        return ret;
    }

    return meth_get(txn, params);
}