File: session_exchange.c

package info (click to toggle)
prayer 1.3.5-dfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,576 kB
  • sloc: ansic: 43,161; makefile: 817; sh: 445; perl: 166
file content (171 lines) | stat: -rw-r--r-- 5,865 bytes parent folder | download | duplicates (4)
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
/* $Cambridge: hermes/src/prayer/servers/session_exchange.c,v 1.7 2011/01/11 15:27:24 dpc22 Exp $ */
/************************************************
 *    Prayer - a Webmail Interface              *
 ************************************************/

/* Copyright (c) University of Cambridge 2000 - 2008 */
/* See the file NOTICE for conditions of use and distribution. */

#include "server.h"

/* session_exchange() ***************************************************
 *
 * Manage single HTTP exchange with user agent (can be initiated by
 * proxy frontend or direct connection to session server).
 ***********************************************************************/

BOOL session_exchange(struct session * session)
{
    struct prefs  *prefs  = session->options->prefs;
    struct config *config = session->config;
    struct request *request = session->request;
    struct user_agent *user_agent = request->user_agent;
    char *sequence;
    struct template_vals *tvals;
    char *template_set = config->template_set;

    /* Do something useful with HTTP/0.9 */
    if (request->major == 0) {
        response_0_9_error(request);
        return (NIL);
    }

    /* Check for valid host if one supplied */
    if (request->url_host) {
        BOOL rc = NIL;

        if (!strcasecmp(request->url_host, "localhost"))
            rc = T;
        else if (config->hostname &&
                 !strcasecmp(request->url_host, config->hostname))
            rc = T;
        else if (config->hostname_service &&
                 !strcasecmp(request->url_host, config->hostname_service))
            rc = T;

        if (!rc) {
            response_error(request, 403);       /* Forbidden */
            return (NIL);
        }
    }

    request_parse_argv(request);
    request_parse_charset(request);

    if (session->use_gzip) {
        request->allow_gzip = T;
        request_parse_encoding(request);
    } else
        request->allow_gzip = NIL;

    /* argv: /session/username/session/sequence... */

    if ((request->argc < 5) || (strcmp(request->argv[0], "session") != 0)) {
        response_error(request, 404);   /* Not found */
        return (NIL);
    }

    sequence = request->argv[3];
    request->argv = &request->argv[4];
    request->argc -= 4;

    session->sequence_okay = T;

    if (strcmp(sequence, "NOSEQ") != 0) {
        BOOL error = NIL;

        if (session->sequence != session_sequence(sequence))
            session->sequence_okay = NIL;

        session_bump_sequence(session);
        session_update_sequence(session);

        if (user_agent->use_substitution) {
            /* Check that the sequence number is correct:
             *   Browser back button doesn't work with page substituton enabled
             *   Spell checker and browser buttons don't get on at all well
             *    (flag forall methods would be sensible)
             */
            if (!session->sequence_okay)
                error = T;
        } else {
            /* Browser back button works fine without page substition
             * apart from folder changes.
             */

            if (session->sequence_last_change > session_sequence(sequence))
                error = T;
        }

        if (error) {
            char *last_cmd = session_last_cmd(session);

            if (last_cmd && last_cmd[0]) {
                session_message(session,
                                ("Browser history disabled as unsafe: "
                                 "use Webmail navigation icons and links"));
                session_redirect(session, request, last_cmd);
                return (T);
            }

            session_log(session, ("[session_exchange] "
                                  "Internal error: last command not recorded"));
            session_redirect(session, request, "restart");
            return (T);
        }
    } else {
        /* Just in case NOSEQ URL generates a redirect */
        session_update_sequence(session);
    }

    /* Only safe to call session_redirect() after this point */

    /* Check IMAP streams before processing each session request
     * (reopens streams if required + ping every once in a while
     * Have to do this here rather than in parent routines so that we
     * have a HTTP connection back to client for restart page...
     *
     * Don't make this test if it would cause infinite loop of redirects
     */
    if ((strcmp(request->argv[0], "restart") != 0) &&
        (strcmp(request->argv[0], "exit") != 0)) {
        if (!session_streams_check(session)) {
            session_redirect(session, request, "restart");
            return (T);
        }
    }

    /* Reopen local domains every once in a while */
    session_config_local_domain_ping(session->config);

    session_log(session, "[session_exchange] %s", request->argv[0]);

    if (config->template_list &&
        list_lookup_byname(config->template_list, prefs->template_set))
        template_set = prefs->template_set;
    else
        template_set = config->template_set;   /* Safe default */

    dlopen_templates(template_set, "");

    /* Set up template_vars ready for dispatch */
    session->template_vals = tvals
        = template_vals_create(request->pool,
                               config->template_path,
                               template_set,
                               config->template_use_compiled, T,
                               session_extract_urlstate(session));

    template_vals_string(tvals, "g_cmd", request->argv[0]);

    /* Look for defined session method */
    if (cmd_dispatch(session, request->argv[0]))
        return (request->persist);

    if ((request->method == GET) || (request->method == HEAD))
        response_error(request, 404);   /* Not found */
    else
        response_error(request, 501);   /* Not implemented */

    return (NIL);
}