~ubuntu-branches/ubuntu/trusty/serf/trusty-security

« back to all changes in this revision

Viewing changes to ssltunnel.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Samuelson
  • Date: 2011-06-27 18:09:28 UTC
  • mfrom: (1.2.5 upstream)
  • mto: (3.3.1 sid)
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: james.westby@ubuntu.com-20110627180928-ybwzd3hmx82nu3ir
Tags: 1.0.0~0+svn1514-1
* New upstream snapshot.
  - patches/abi-0.x: Remove as obsolete.
  - patches/kqueue: Forward-port.
  - Bump ABI: libserf0.7{,-dbg} -> libserf1{,-dbg}
  - patches/ip6-localhost: New patch: temporary (I hope) workaround for
    IPv4 / IPv6 confusion in testsuite.
* Implement Multi-Arch: same.
* libserf-dev Conflicts: libserf-0-0-dev, not Breaks.  Thanks, lintian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2011 Justin Erenkrantz and Greg Stein
 
2
 *
 
3
 * Licensed under the Apache License, Version 2.0 (the "License");
 
4
 * you may not use this file except in compliance with the License.
 
5
 * You may obtain a copy of the License at
 
6
 *
 
7
 *     http://www.apache.org/licenses/LICENSE-2.0
 
8
 *
 
9
 * Unless required by applicable law or agreed to in writing, software
 
10
 * distributed under the License is distributed on an "AS IS" BASIS,
 
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
 * See the License for the specific language governing permissions and
 
13
 * limitations under the License.
 
14
 */
 
15
 
 
16
/*** Setup a SSL tunnel over a HTTP proxy, according to RFC 2817. ***/
 
17
 
 
18
#include <apr_pools.h>
 
19
#include <apr_strings.h>
 
20
 
 
21
#include "serf.h"
 
22
#include "serf_private.h"
 
23
 
 
24
 
 
25
/* Structure passed around as baton for the CONNECT request and respone. */
 
26
typedef struct {
 
27
    apr_pool_t *pool;
 
28
    const char *uri;
 
29
} req_ctx_t;
 
30
 
 
31
/* forward declaration. */
 
32
static apr_status_t setup_request(serf_request_t *request,
 
33
                                  void *setup_baton,
 
34
                                  serf_bucket_t **req_bkt,
 
35
                                  serf_response_acceptor_t *acceptor,
 
36
                                  void **acceptor_baton,
 
37
                                  serf_response_handler_t *handler,
 
38
                                  void **handler_baton,
 
39
                                  apr_pool_t *pool);
 
40
 
 
41
static serf_bucket_t* accept_response(serf_request_t *request,
 
42
                                      serf_bucket_t *stream,
 
43
                                      void *acceptor_baton,
 
44
                                      apr_pool_t *pool)
 
45
{
 
46
    serf_bucket_t *c;
 
47
    serf_bucket_alloc_t *bkt_alloc;
 
48
#if 0
 
49
    req_ctx_t *ctx = acceptor_baton;
 
50
#endif
 
51
 
 
52
    /* get the per-request bucket allocator */
 
53
    bkt_alloc = serf_request_get_alloc(request);
 
54
 
 
55
    /* Create a barrier so the response doesn't eat us! */
 
56
    c = serf_bucket_barrier_create(stream, bkt_alloc);
 
57
 
 
58
    return serf_bucket_response_create(c, bkt_alloc);
 
59
}
 
60
 
 
61
/* If a 200 OK was received for the CONNECT request, consider the connection
 
62
   as ready for use. */
 
63
static apr_status_t handle_response(serf_request_t *request,
 
64
                                    serf_bucket_t *response,
 
65
                                    void *handler_baton,
 
66
                                    apr_pool_t *pool)
 
67
{
 
68
    apr_status_t status;
 
69
    serf_status_line sl;
 
70
    req_ctx_t *ctx = handler_baton;
 
71
 
 
72
    if (! response) {
 
73
        serf_connection_request_create(request->conn,
 
74
                                       setup_request,
 
75
                                       ctx);
 
76
        return APR_SUCCESS;
 
77
    }
 
78
 
 
79
    status = serf_bucket_response_status(response, &sl);
 
80
    if (SERF_BUCKET_READ_ERROR(status)) {
 
81
        return status;
 
82
    }
 
83
    if (!sl.version && (APR_STATUS_IS_EOF(status) ||
 
84
                      APR_STATUS_IS_EAGAIN(status)))
 
85
    {
 
86
        return status;
 
87
    }
 
88
 
 
89
    status = serf_bucket_response_wait_for_headers(response);
 
90
    if (status && !APR_STATUS_IS_EOF(status)) {
 
91
        return status;
 
92
    }
 
93
 
 
94
    /* Body is supposed to be empty. */
 
95
    if (sl.code == 200) {
 
96
        request->conn->state = SERF_CONN_CONNECTED;
 
97
 
 
98
        apr_pool_destroy(ctx->pool);
 
99
        serf_bucket_destroy(request->conn->ssltunnel_ostream);
 
100
        request->conn->stream = NULL;
 
101
        ctx = NULL;
 
102
 
 
103
        return APR_EOF;
 
104
    }
 
105
 
 
106
    /* Authentication failure and 200 Ok are handled at this point,
 
107
       the rest are errors. */
 
108
    return APR_EGENERAL; /* TODO: better error code */
 
109
}
 
110
 
 
111
/* Prepare the CONNECT request. */
 
112
static apr_status_t setup_request(serf_request_t *request,
 
113
                                  void *setup_baton,
 
114
                                  serf_bucket_t **req_bkt,
 
115
                                  serf_response_acceptor_t *acceptor,
 
116
                                  void **acceptor_baton,
 
117
                                  serf_response_handler_t *handler,
 
118
                                  void **handler_baton,
 
119
                                  apr_pool_t *pool)
 
120
{
 
121
    req_ctx_t *ctx = setup_baton;
 
122
 
 
123
    *req_bkt =
 
124
        serf_request_bucket_request_create(request,
 
125
                                           "CONNECT", ctx->uri,
 
126
                                           NULL,
 
127
                                           serf_request_get_alloc(request));
 
128
    *acceptor = accept_response;
 
129
    *acceptor_baton = ctx;
 
130
    *handler = handle_response;
 
131
    *handler_baton = ctx;
 
132
 
 
133
    return APR_SUCCESS;
 
134
}
 
135
 
 
136
static apr_status_t detect_eof(void *baton, serf_bucket_t *aggregate_bucket)
 
137
{
 
138
    serf_connection_t *conn = baton;
 
139
    conn->hit_eof = 1;
 
140
    return APR_EAGAIN;
 
141
}
 
142
 
 
143
/* SSL tunnel is needed, push a CONNECT request on the connection. */
 
144
apr_status_t serf__ssltunnel_connect(serf_connection_t *conn)
 
145
{
 
146
    req_ctx_t *ctx;
 
147
    apr_pool_t *ssltunnel_pool;
 
148
 
 
149
    apr_pool_create(&ssltunnel_pool, conn->pool);
 
150
 
 
151
    ctx = apr_palloc(ssltunnel_pool, sizeof(*ctx));
 
152
    ctx->pool = ssltunnel_pool;
 
153
    ctx->uri = apr_psprintf(ctx->pool, "%s:%d", conn->host_info.hostinfo,
 
154
                            conn->host_info.port);
 
155
 
 
156
    conn->ssltunnel_ostream = serf__bucket_stream_create(conn->allocator,
 
157
                                                         detect_eof,
 
158
                                                         conn);
 
159
 
 
160
    /* TODO: should be the first request on the connection. */
 
161
    serf_connection_priority_request_create(conn,
 
162
                                            setup_request,
 
163
                                            ctx);
 
164
 
 
165
    conn->state = SERF_CONN_SETUP_SSLTUNNEL;
 
166
 
 
167
    return APR_SUCCESS;
 
168
}