summaryrefslogtreecommitdiff
path: root/src/tipideed/cgi.c
blob: 343f7afeeb475b2814b598d2ce7db5523a998289 (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
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
/* ISC license. */

#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <signal.h>

#include <skalibs/gccattributes.h>
#include <skalibs/posixplz.h>
#include <skalibs/types.h>
#include <skalibs/bytestr.h>
#include <skalibs/buffer.h>
#include <skalibs/error.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/strerr.h>
#include <skalibs/stralloc.h>
#include <skalibs/cspawn.h>
#include <skalibs/djbunix.h>
#include <skalibs/iopause.h>
#include <skalibs/env.h>
#include <skalibs/exec.h>
#include <skalibs/unix-timed.h>

#include <tipidee/tipidee.h>
#include "tipideed-internal.h"

static void addenv_ (tipidee_rql const *rql, char const *docroot, char const *k, char const *v, int slash)
{
  if (!stralloc_cats(&g.sa, k)
   || !stralloc_catb(&g.sa, "=/", 1 + !!slash)
   || !stralloc_cats(&g.sa, v)
   || !stralloc_0(&g.sa))
    die500sys(rql, 111, docroot, "stralloc_catb") ;
}

#define addenv(rql, d, k, v) addenv_(rql, d, k, (v), 0)
#define addenvslash(rql, d, k, v) addenv_(rql, d, k, (v), 1)

static void delenv (tipidee_rql const *rql, char const *docroot, char const *k)
{
  if (!stralloc_cats(&g.sa, k)
   || !stralloc_0(&g.sa))
    die500sys(rql, 111, docroot, "stralloc_catb") ;
}

static inline void modify_env (tipidee_rql const *rql, char const *docroot, tipidee_headers const *hdr, size_t cl, char const *script, char const *infopath)
{
  uint32_t got = 0 ;
  addenv(rql, docroot, "REQUEST_METHOD", tipidee_method_tostr(rql->m)) ;
  if (cl)
  {
    char fmt[SIZE_FMT] ;
    fmt[size_fmt(fmt, cl)] = 0 ;
    addenv(rql, docroot, "CONTENT_LENGTH", fmt) ;
  }
  else delenv(rql, docroot, "CONTENT_LENGTH") ;

  if (infopath) addenvslash(rql, docroot, "PATH_INFO", infopath) ;
  else delenv(rql, docroot, "PATH_INFO") ;
  if (rql->uri.query) addenv(rql, docroot, "QUERY_STRING", rql->uri.query) ;
  else delenv(rql, docroot, "QUERY_STRING") ;
  addenv(rql, docroot, "SCRIPT_NAME", script) ;
  addenv(rql, docroot, "SERVER_NAME", rql->uri.host) ;
  {
    char proto[9] = "HTTP/1.1" ;
    if (!rql->http_minor) proto[7] = '0' ;
    addenv(rql, docroot, "SERVER_PROTOCOL", proto) ;
  }
  
  for (size_t i = 0 ; i < hdr->n ; i++)
  {
    char const *key = hdr->buf + hdr->list[i].left ;
    char const *val = hdr->buf + hdr->list[i].right ;
    if (!strcasecmp(key, "Authorization"))
    {
      size_t n = str_chr(val, ' ') ;
      if (n)
      {
        char scheme[n] ;
        memcpy(scheme, val, n-1) ;
        scheme[n-1] = 0 ;
        addenv(rql, docroot, "AUTH_TYPE", scheme) ;
        got |= 1 ;
      }
    }
    else if (!strcasecmp(key, "Content-Type")) { addenv(rql, docroot, "CONTENT_TYPE", val) ; got |= 2 ; }
    else if (!strcasecmp(key, "Content-Length") || !strcasecmp(key, "Connection")) ;
    else
    {
      size_t len = strlen(key), pos = g.sa.len + 5 ;
      if (!stralloc_catb(&g.sa, "HTTP_", 5)) die500sys(rql, 111, "stralloc_catb") ;
      addenv(rql, docroot, key, val) ;
      for (char *s = g.sa.s + pos ; len-- ; s++)
        if (*s == '-') *s = '_' ;
        else if (*s >= 'a' && *s <= 'z') *s -= 32 ;
    }
  }
  if (!(got & 1)) delenv(rql, docroot, "AUTH_TYPE") ;
  if (!(got & 2)) delenv(rql, docroot, "CONTENT_TYPE") ;
}

static inline int do_nph (tipidee_rql const *rql, char const *docroot, char const *const *argv, char const *const *envp, char const *body, size_t bodylen) gccattr_noreturn ;
static inline int do_nph (tipidee_rql const *rql, char const *docroot, char const *const *argv, char const *const *envp, char const *body, size_t bodylen)
{
  int p[2] ;
  if (pipe(p) == -1) die500sys(rql, 111, docroot, "pipe") ;
  if (bodylen)
  {
    switch (fork())
    {
      case -1 : die500sys(rql, 111, docroot, "fork") ;
      case 0 :      
      {
#define NAME "tipideed (nph helper for pid "
        tain deadline ;
        char buf[4096] ;
        buffer b = BUFFER_INIT(&buffer_write, p[1], buf, 4096) ;
        size_t m = sizeof(NAME) - 1 ;
        char progstr[sizeof(NAME) + PID_FMT] ;
        memcpy(progstr, NAME, m) ;
        m += pid_fmt(progstr + m, getppid()) ;
        progstr[m++] = ')' ; progstr[m++] = 0 ;
        PROG = progstr ;
        tain_add_g(&deadline, &g.cgitto) ;
        close(p[0]) ;
        if (ndelay_on(p[1]) == -1) die500sys(rql, 111, docroot, "set fd nonblocking") ;
        if (buffer_timed_put_g(&b, body, bodylen, &deadline) < bodylen
         || !buffer_timed_flush_g(&b, &deadline))
          die500sys(rql, 111, docroot, "write request body to nph ", argv[0]) ;
        _exit(0) ;
      }
      default : break ;
    }
  }
  close(p[1]) ;
  if (fd_move(0, p[0]) == -1) die500sys(rql, 111, docroot, "fd_move") ;
  exec_e(argv, envp) ;
  die500sys(rql, errno == ENOENT ? 127 : 126, docroot, "exec nph ", argv[0]) ;
}

static inline int run_cgi (tipidee_rql const *rql, char const *docroot, char const *const *argv, char const *const *envp, char const *body, size_t bodylen, tipidee_headers *hdr, stralloc *sa)
{
  iopause_fd x[2] = { { .events = IOPAUSE_READ }, { .events = IOPAUSE_WRITE } } ;
  size_t bodyw = 0 ;
  unsigned int rstate = 0 ;
  tain deadline ;
  pid_t pid ;
  disize curheader = DISIZE_ZERO ;
  uint32_t parserstate = 0 ;
  buffer b ;
  char buf[4096] ;
  {
    int fd[2] = { 0, 1 } ;
    pid = child_spawn2(argv[0], argv, envp, fd) ;
    if (!pid) die500sys(rql, 111, docroot, "spawn ", argv[0]) ;
    x[0].fd = fd[0] ; x[1].fd = fd[1] ;
  }
  if (!bodylen)
  {
    close(x[1].fd) ;
    x[1].fd = -1 ;
  }
  buffer_init(&b, &buffer_read, x[0].fd, buf, 4096) ;
  tain_add_g(&deadline, &g.cgitto) ;
  while (x[0].fd >= 0)
  {
    int r = iopause_g(x, 1 + (x[1].fd >= 0), &deadline) ;
    if (r == -1) die500sys(rql, 111, docroot, "iopause") ;
    if (!r)
    {
      kill(pid, SIGTERM) ;
      strerr_warnw3x("cgi ", argv[0], " timed out") ;
      respond_504(rql, docroot) ;
      break ;
    }
    if (x[1].fd >= 0 && x[1].revents & (IOPAUSE_WRITE | IOPAUSE_EXCEPT))
    {
      size_t len = allwrite(x[1].fd, body + bodyw, bodylen - bodyw) ;
      if (!len)
      {
        strerr_warnwu2sys("write request body to cgi ", argv[0]) ;
        bodyw = bodylen ;
      }
      else bodyw += len ;
      if (bodyw >= bodylen)
      {
        close(x[1].fd) ;
        x[1].fd = -1 ;
      }
    }
    if (x[0].fd >= 0 && x[0].revents & (IOPAUSE_READ | IOPAUSE_EXCEPT))
    {
      switch (rstate)
      {
        case 0 :
        {
          r = tipidee_headers_parse_nb(&b, hdr, &curheader, &parserstate) ;
          switch (r)
          {
            case -2 : break ;
            case -1 : die500sys(rql, 111, docroot, "read from cgi ", argv[0]) ;
            case 0 :
            {
              size_t n = buffer_len(&b) ;
              if (!stralloc_readyplus(sa, n)) die500sys(rql, 111, docroot, "stralloc_readyplus") ;
              buffer_getnofill(&b, sa->s + sa->len, n) ;
              sa->len += n ;
              rstate = 1 ;
              break ;
            }
            case 400 : die502x(rql, 2, docroot, "invalid output", " from cgi ", argv[0]) ;
            case 413 : die502x(rql, 2, docroot, hdr->n >= TIPIDEE_HEADERS_MAX ? "Too many headers" : "Too much header data", " from cgi ", argv[0]) ;
            case 500 : die500x(rql, 101, docroot, "can't happen: ", "avltreen_insert failed", " in do_cgi") ;
            default : die500x(rql, 101, docroot, "can't happen: ", "unknown tipidee_headers_parse return code", " in do_cgi") ;
          }
          if (!rstate) break ;
        }
        case 1 :
        {
          if (!slurpn(x[0].fd, sa, g.maxcgibody))
          {
            if (error_isagain(errno)) break ;
            else if (errno == ENOBUFS) die502x(rql, 2, docroot, "Too fat body", " from cgi ", argv[0]) ;
            else die500sys(rql, 111, docroot, "read body", " from cgi ", argv[0]) ;
          }
          close(x[0].fd) ;
          x[0].fd = -1 ;
          rstate = 2 ;
        }
      }
    }
  }
  if (x[1].fd >= 0) close(x[1].fd) ;
  if (x[0].fd >= 0) close(x[0].fd) ;
  return rstate == 2 ;
}

static inline int local_redirect (tipidee_rql *rql, char const *docroot, char const *loc, char *uribuf, char const *cginame)
{
  size_t n ;
  size_t hostlen = strlen(rql->uri.host) ;
  uint16_t port = rql->uri.port ;
  uint8_t ishttps = rql->uri.https ;
  char hosttmp[hostlen + 1] ;
  memcpy(hosttmp, rql->uri.host, hostlen + 1) ;
  n = tipidee_uri_parse(uribuf, URI_BUFSIZE, loc, &rql->uri) ;
  if (!n || n + hostlen + 1 > URI_BUFSIZE)
    die502x(rql, 2, docroot, "cgi ", cginame, " returned an invalid ", "Location", " value", " for local redirection") ;
  memcpy(uribuf + n, hosttmp, hostlen + 1) ;
  rql->uri.host = uribuf + n ;
  rql->uri.port = port ;
  rql->uri.https = ishttps ;
  tipidee_log_debug(g.logv, "cgi ", cginame, ": local redirect to ", rql->uri.path) ;
  return 1 ;
}

static inline int process_cgi_output (tipidee_rql *rql, char const *docroot, tipidee_headers const *hdr, char const *rbody, size_t rbodylen, char *uribuf, char const *cginame)
{
  char const *location = tipidee_headers_search(hdr, "Location") ;
  char const *x = tipidee_headers_search(hdr, "Status") ;
  char const *reason = "OK" ;
  unsigned int status = 0 ;
  tain deadline ;
  tain_add_g(&deadline, &g.writetto) ;
  if (x)
  {
    size_t m = uint_scan(x, &status) ;
    if (!m || (x[m] && x[m] != ' '))
      die502x(rql, 2, docroot, "cgi ", cginame, " returned an invalid ", "Status", " header") ;
    if (x[m]) reason = x + m + 1 ;
    else
    {
      tipidee_defaulttext dt ;
      reason = tipidee_util_defaulttext(status, &dt) ? dt.reason : "" ;
    }
    if (!location && (status == 301 || status == 302 || status == 307 || status == 308))
      die502x(rql, 2, docroot, "cgi ", cginame, " returned a redirection status code without a ", "Location", " header") ;
    if (status < 100 || status > 999)
      die502x(rql, 2, docroot, "cgi ", cginame, " returned an invalid ", "Status", " value") ;
  }
  if (location)
  {
    if (!location[0]) die502x(rql, 2, docroot, "cgi ", cginame, " returned an invalid ", "Location", " header") ;
    if (location[0] == '/' && location[1] != '/') return local_redirect(rql, docroot, location, uribuf, cginame) ;
    if (rbodylen)
    {
      if (!status)
        die502x(rql, 2, docroot, "cgi ", cginame, " didn't output a ", "Status", " header", " for a client redirect response with document") ;
      if (status < 300 || status > 399)
        die502x(rql, 2, docroot, "cgi ", cginame, " returned an invalid ", "Status", " value", " for a client redirect response with document") ;
    }
    else
    {
      for (size_t i = 0 ; i < hdr->n ; i++)
      {
        char const *key = hdr->buf + hdr->list[i].left ;
        if (!strcasecmp(key, "Location") || !strcasecmp(key, "Status")) continue ;
        if (str_start(key, "X-CGI-")) continue ;
        die502x(rql, 2, docroot, "cgi ", cginame, " returned extra headers", " for a client redirect response without document") ;
      }
      if (!status)
      {
        status = 302 ;
        reason = "Found" ;
      }
    }
  }
  else
  {
    if (!status) status = 200 ;
    if (status != 304 && !tipidee_headers_search(hdr, "Content-Type"))
      die502x(rql, 2, docroot, "cgi ", cginame, " didn't output a ", "Content-Type", " header") ;
  }
  x = tipidee_headers_search(hdr, "Content-Length") ;
  if (x)
  {
    size_t cln ;
    if (!size0_scan(x, &cln))
      die502x(rql, 2, docroot, "cgi ", cginame, " returned an invalid ", "Content-Length", " header") ;
    if (cln != rbodylen)
      die502x(rql, 2, docroot, "cgi ", cginame, " returned a mismatching ", "Content-Length", " header") ;
  }

  tipidee_response_status(buffer_1, rql, status, reason) ;
  tipidee_response_header_writemerge_G(buffer_1, g.rhdr, g.rhdrn, hdr, !g.cont) ;
  {
    char fmt[SIZE_FMT] ;
    fmt[size_fmt(fmt, rbodylen)] = 0 ;
    buffer_putsnoflush(buffer_1, "Content-Length: ") ;
    buffer_putsnoflush(buffer_1, fmt) ;
    buffer_putnoflush(buffer_1, "\r\n", 2) ;
  }
  if (buffer_timed_put_g(buffer_1, "\r\n", 2, &deadline) < 2)
    strerr_diefu1sys(111, "write to stdout") ;
  tipidee_log_answer(g.logv, rql, status, rbodylen) ;
  if (rbodylen)
  {
    if (buffer_timed_put_g(buffer_1, rbody, rbodylen, &deadline) < rbodylen)
      strerr_diefu1sys(111, "write to stdout") ;
  }
  if (!buffer_timed_flush_g(buffer_1, &deadline))
    strerr_diefu1sys(111, "write to stdout") ;
  return 0 ;
}

static inline int do_cgi (tipidee_rql *rql, char const *docroot, char const *const *argv, char const *const *envp, char const *body, size_t bodylen, char *uribuf)
{
  static stralloc sa = STRALLOC_ZERO ;
  tipidee_headers hdr ;
  char hdrbuf[4096] ;
  sa.len = 0 ;
  tipidee_headers_init(&hdr, hdrbuf, 4096) ;
  if (!run_cgi(rql, docroot, argv, envp, body, bodylen, &hdr, &sa)) return 0 ;
  return process_cgi_output(rql, docroot, &hdr, sa.s, sa.len, uribuf, argv[0]) ;
}

int respond_cgi (tipidee_rql *rql, char const *docroot, char const *fn, size_t docrootlen, char const *infopath, char *uribuf, tipidee_headers const *hdr, tipidee_resattr const *ra, char const *body, size_t bodylen)
{
  size_t sabase = g.sa.len ;
  size_t envmax = g.envlen + 16 + TIPIDEE_HEADERS_MAX ;
  char const *argv[2] = { fn, 0 } ;
  char const *envp[envmax] ;
  modify_env(rql, docroot, hdr, bodylen, fn + docrootlen, infopath) ;
  env_merge(envp, envmax, (char const *const *)environ, g.envlen, g.sa.s + g.cwdlen + 1, g.sa.len - (g.cwdlen+1)) ;
  g.sa.len = sabase ;
  return ra->flags & TIPIDEE_RA_FLAG_NPH ?
    do_nph(rql, docroot, argv, envp, body, bodylen) :
    do_cgi(rql, docroot, argv, envp, body, bodylen, uribuf) ;
}