chiark / gitweb /
reorg cgi code a bit...
[disorder] / server / login.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2008 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20
21 #include "disorder-cgi.h"
22
23 /** @brief Client used by CGI
24  *
25  * The caller should arrange for this to be created before any of
26  * these expansions are used (if it cannot connect then it's safe to
27  * leave it as NULL).
28  */
29 disorder_client *dcgi_client;
30
31 /** @brief Login cookie */
32 char *dcgi_cookie;
33
34 /** @brief Return a Cookie: header */
35 char *dcgi_cookie_header(void) {
36   struct dynstr d[1];
37   struct url u;
38   char *s;
39
40   memset(&u, 0, sizeof u);
41   dynstr_init(d);
42   parse_url(config->url, &u);
43   if(dcgi_cookie) {
44     dynstr_append_string(d, "disorder=");
45     dynstr_append_string(d, dcgi_cookie);
46   } else {
47     /* Force browser to discard cookie */
48     dynstr_append_string(d, "disorder=none;Max-Age=0");
49   }
50   if(u.path) {
51     /* The default domain matches the request host, so we need not override
52      * that.  But the default path only goes up to the rightmost /, which would
53      * cause the browser to expose the cookie to other CGI programs on the same
54      * web server. */
55     dynstr_append_string(d, ";Version=1;Path=");
56     /* Formally we are supposed to quote the path, since it invariably has a
57      * slash in it.  However Safari does not parse quoted paths correctly, so
58      * this won't work.  Fortunately nothing else seems to care about proper
59      * quoting of paths, so in practice we get with it.  (See also
60      * parse_cookie() where we are liberal about cookie paths on the way back
61      * in.) */
62     dynstr_append_string(d, u.path);
63   }
64   dynstr_terminate(d);
65   byte_xasprintf(&s, "Set-Cookie: %s", d->vec);
66   return s;
67 }
68
69 /** @brief Log in as the current user or guest if none */
70 void dcgi_login(void) {
71   /* Junk old data */
72   dcgi_lookup_reset();
73   /* Junk the old connection if there is one */
74   if(dcgi_client)
75     disorder_close(dcgi_client);
76   /* Create a new connection */
77   dcgi_client = disorder_new(0);
78   /* Reconnect */
79   if(disorder_connect_cookie(dcgi_client, dcgi_cookie)) {
80     dcgi_error("Cannot connect to server");
81     exit(0);
82   }
83   /* If there was a cookie but it went bad, we forget it */
84   if(dcgi_cookie && !strcmp(disorder_user(dcgi_client), "guest"))
85     dcgi_cookie = 0;
86 }
87
88 /*
89 Local Variables:
90 c-basic-offset:2
91 comment-column:40
92 fill-column:79
93 indent-tabs-mode:nil
94 End:
95 */