~ubuntu-branches/ubuntu/jaunty/nginx/jaunty-updates

« back to all changes in this revision

Viewing changes to src/mail/ngx_mail_auth_http_module.c

  • Committer: Bazaar Package Importer
  • Author(s): Jose Parrella
  • Date: 2007-12-08 11:27:54 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20071208112754-23ohg2b1zshkdayw
Tags: 0.5.33-1
* New stable upstream release (Closes: #451173)
* nginx now provides httpd, httpd-cgi virtual packages
  (Closes: #439468, #452025)
* sites-enabled/default link is now provided only on fresh 
  installations (Closes: #432961)
* Updated code for online upgrading of nginx (Closes: #445246)
* Reviewed maintainer scripts for correct behaviour on updates
  (Closes: #452787, #435965)
* Removed debian/nginx.links and debian/preinst.
* Changing Maintainer address to bureado@debian.org.
* Welcoming Fabio Tranchitella <kobold@debian.org> as an nginx 
  uploader for Debian. Thanks for your patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
    ngx_str_t                       header;
22
22
 
23
23
    ngx_array_t                    *headers;
 
24
 
 
25
    u_char                         *file;
 
26
    ngx_uint_t                      line;
24
27
} ngx_mail_auth_http_conf_t;
25
28
 
26
29
 
108
111
 
109
112
 
110
113
static ngx_mail_module_t  ngx_mail_auth_http_module_ctx = {
 
114
    NULL,                                  /* protocol */
 
115
 
111
116
    NULL,                                  /* create main configuration */
112
117
    NULL,                                  /* init main configuration */
113
118
 
132
137
};
133
138
 
134
139
 
135
 
static char       *ngx_mail_auth_http_protocol[] = { "pop3", "imap", "smtp" };
136
140
static ngx_str_t   ngx_mail_auth_http_method[] = {
137
141
    ngx_string("plain"),
138
142
    ngx_string("plain"),
142
146
 
143
147
static ngx_str_t   ngx_mail_smtp_errcode = ngx_string("535 5.7.0");
144
148
 
 
149
 
145
150
void
146
151
ngx_mail_auth_http_init(ngx_mail_session_t *s)
147
152
{
236
241
    if (wev->timedout) {
237
242
        ngx_log_error(NGX_LOG_ERR, wev->log, NGX_ETIMEDOUT,
238
243
                      "auth http server %V timed out", ctx->peer.name);
239
 
        ngx_close_connection(ctx->peer.connection);
 
244
        ngx_close_connection(c);
240
245
        ngx_destroy_pool(ctx->pool);
241
246
        ngx_mail_session_internal_server_error(s);
242
247
        return;
247
252
    n = ngx_send(c, ctx->request->pos, size);
248
253
 
249
254
    if (n == NGX_ERROR) {
250
 
        ngx_close_connection(ctx->peer.connection);
 
255
        ngx_close_connection(c);
251
256
        ngx_destroy_pool(ctx->pool);
252
257
        ngx_mail_session_internal_server_error(s);
253
258
        return;
264
269
            }
265
270
 
266
271
            if (ngx_handle_write_event(wev, 0) == NGX_ERROR) {
267
 
                ngx_close_connection(ctx->peer.connection);
 
272
                ngx_close_connection(c);
268
273
                ngx_destroy_pool(ctx->pool);
269
274
                ngx_mail_session_internal_server_error(s);
270
275
            }
299
304
    if (rev->timedout) {
300
305
        ngx_log_error(NGX_LOG_ERR, rev->log, NGX_ETIMEDOUT,
301
306
                      "auth http server %V timed out", ctx->peer.name);
302
 
        ngx_close_connection(ctx->peer.connection);
 
307
        ngx_close_connection(c);
303
308
        ngx_destroy_pool(ctx->pool);
304
309
        ngx_mail_session_internal_server_error(s);
305
310
        return;
308
313
    if (ctx->response == NULL) {
309
314
        ctx->response = ngx_create_temp_buf(ctx->pool, 1024);
310
315
        if (ctx->response == NULL) {
311
 
            ngx_close_connection(ctx->peer.connection);
 
316
            ngx_close_connection(c);
312
317
            ngx_destroy_pool(ctx->pool);
313
318
            ngx_mail_session_internal_server_error(s);
314
319
            return;
330
335
        return;
331
336
    }
332
337
 
333
 
    ngx_close_connection(ctx->peer.connection);
 
338
    ngx_close_connection(c);
334
339
    ngx_destroy_pool(ctx->pool);
335
340
    ngx_mail_session_internal_server_error(s);
336
341
}
746
751
                return;
747
752
            }
748
753
 
749
 
            if (s->passwd.data == NULL && s->protocol != NGX_MAIL_SMTP_PROTOCOL)
 
754
            if (s->passwd.data == NULL
 
755
                && s->protocol != NGX_MAIL_SMTP_PROTOCOL)
750
756
            {
751
757
                ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
752
758
                              "auth http server %V did not send password",
865
871
            return;
866
872
        }
867
873
 
868
 
        switch (s->protocol) {
869
 
 
870
 
        case NGX_MAIL_POP3_PROTOCOL:
871
 
            s->mail_state = ngx_pop3_start;
872
 
            s->connection->read->handler = ngx_pop3_auth_state;
873
 
            break;
874
 
 
875
 
        case NGX_MAIL_IMAP_PROTOCOL:
876
 
            s->mail_state = ngx_imap_start;
877
 
            s->connection->read->handler = ngx_imap_auth_state;
878
 
            break;
879
 
 
880
 
        default: /* NGX_MAIL_SMTP_PROTOCOL */
881
 
            s->mail_state = ngx_smtp_start;
882
 
            s->connection->read->handler = ngx_smtp_auth_state;
883
 
            break;
884
 
        }
885
 
 
 
874
        cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
 
875
 
 
876
        rev->handler = cscf->protocol->auth_state;
 
877
 
 
878
        s->mail_state = 0;
886
879
        s->auth_method = NGX_MAIL_AUTH_PLAIN;
887
880
 
888
881
        c->log->action = "in auth state";
889
882
 
890
 
        ngx_mail_send(s->connection->write);
 
883
        ngx_mail_send(c->write);
891
884
 
892
885
        if (c->destroyed) {
893
886
            return;
894
887
        }
895
888
 
896
 
        cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
897
 
 
898
889
        ngx_add_timer(rev, cscf->timeout);
899
890
 
900
891
        if (rev->ready) {
901
 
            s->connection->read->handler(rev);
 
892
            rev->handler(rev);
902
893
            return;
903
894
        }
904
895
 
905
896
        if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
906
 
            ngx_mail_close_connection(s->connection);
 
897
            ngx_mail_close_connection(c);
907
898
        }
908
899
 
909
900
        return;
911
902
 
912
903
    if (rev->active) {
913
904
        if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
914
 
            ngx_mail_close_connection(s->connection);
 
905
            ngx_mail_close_connection(c);
915
906
        }
916
907
    }
917
908
}
1144
1135
ngx_mail_auth_http_create_request(ngx_mail_session_t *s, ngx_pool_t *pool,
1145
1136
    ngx_mail_auth_http_conf_t *ahcf)
1146
1137
{
1147
 
    size_t      len;
1148
 
    ngx_buf_t  *b;
1149
 
    ngx_str_t   login, passwd;
 
1138
    size_t                     len;
 
1139
    ngx_buf_t                 *b;
 
1140
    ngx_str_t                  login, passwd;
 
1141
    ngx_mail_core_srv_conf_t  *cscf;
1150
1142
 
1151
1143
    if (ngx_mail_auth_http_escape(pool, &s->login, &login) != NGX_OK) {
1152
1144
        return NULL;
1156
1148
        return NULL;
1157
1149
    }
1158
1150
 
 
1151
    cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
 
1152
 
1159
1153
    len = sizeof("GET ") - 1 + ahcf->uri.len + sizeof(" HTTP/1.0" CRLF) - 1
1160
1154
          + sizeof("Host: ") - 1 + ahcf->host_header.len + sizeof(CRLF) - 1
1161
1155
          + sizeof("Auth-Method: ") - 1
1164
1158
          + sizeof("Auth-User: ") - 1 + login.len + sizeof(CRLF) - 1
1165
1159
          + sizeof("Auth-Pass: ") - 1 + passwd.len + sizeof(CRLF) - 1
1166
1160
          + sizeof("Auth-Salt: ") - 1 + s->salt.len
1167
 
          + sizeof("Auth-Protocol: imap" CRLF) - 1
 
1161
          + sizeof("Auth-Protocol: ") - 1 + cscf->protocol->name.len
 
1162
                + sizeof(CRLF) - 1
1168
1163
          + sizeof("Auth-Login-Attempt: ") - 1 + NGX_INT_T_LEN
1169
1164
                + sizeof(CRLF) - 1
1170
1165
          + sizeof("Client-IP: ") - 1 + s->connection->addr_text.len
1171
1166
                + sizeof(CRLF) - 1
 
1167
          + ahcf->header.len
1172
1168
          + sizeof(CRLF) - 1;
1173
1169
 
1174
1170
    b = ngx_create_temp_buf(pool, len);
1210
1206
 
1211
1207
    b->last = ngx_cpymem(b->last, "Auth-Protocol: ",
1212
1208
                         sizeof("Auth-Protocol: ") - 1);
1213
 
    b->last = ngx_cpymem(b->last, ngx_mail_auth_http_protocol[s->protocol],
1214
 
                         sizeof("imap") - 1);
 
1209
    b->last = ngx_cpymem(b->last, cscf->protocol->name.data,
 
1210
                         cscf->protocol->name.len);
1215
1211
    *b->last++ = CR; *b->last++ = LF;
1216
1212
 
1217
1213
    b->last = ngx_sprintf(b->last, "Auth-Login-Attempt: %ui" CRLF,
1247
1243
static ngx_int_t
1248
1244
ngx_mail_auth_http_escape(ngx_pool_t *pool, ngx_str_t *text, ngx_str_t *escaped)
1249
1245
{
1250
 
    u_char      ch, *p;
1251
 
    ngx_uint_t  i, n;
1252
 
 
1253
 
    n = 0;
1254
 
 
1255
 
    for (i = 0; i < text->len; i++) {
1256
 
        ch = text->data[i];
1257
 
 
1258
 
        if (ch == CR || ch == LF) {
1259
 
            n++;
1260
 
        }
1261
 
    }
 
1246
    u_char     *p;
 
1247
    uintptr_t   n;
 
1248
 
 
1249
    n = ngx_escape_uri(NULL, text->data, text->len, NGX_ESCAPE_MAIL_AUTH);
1262
1250
 
1263
1251
    if (n == 0) {
1264
1252
        *escaped = *text;
1272
1260
        return NGX_ERROR;
1273
1261
    }
1274
1262
 
 
1263
    (void) ngx_escape_uri(p, text->data, text->len, NGX_ESCAPE_MAIL_AUTH);
 
1264
 
1275
1265
    escaped->data = p;
1276
1266
 
1277
 
    for (i = 0; i < text->len; i++) {
1278
 
        ch = text->data[i];
1279
 
 
1280
 
        if (ch == CR) {
1281
 
            *p++ = '%';
1282
 
            *p++ = '0';
1283
 
            *p++ = 'D';
1284
 
            continue;
1285
 
        }
1286
 
 
1287
 
        if (ch == LF) {
1288
 
            *p++ = '%';
1289
 
            *p++ = '0';
1290
 
            *p++ = 'A';
1291
 
            continue;
1292
 
        }
1293
 
 
1294
 
        *p++ = ch;
1295
 
    }
1296
 
 
1297
1267
    return NGX_OK;
1298
1268
}
1299
1269
 
1310
1280
 
1311
1281
    ahcf->timeout = NGX_CONF_UNSET_MSEC;
1312
1282
 
 
1283
    ahcf->file = cf->conf_file->file.name.data;
 
1284
    ahcf->line = cf->conf_file->line;
 
1285
 
1313
1286
    return ahcf;
1314
1287
}
1315
1288
 
1329
1302
        conf->peer = prev->peer;
1330
1303
        conf->host_header = prev->host_header;
1331
1304
        conf->uri = prev->uri;
 
1305
 
 
1306
        if (conf->peer == NULL) {
 
1307
            ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
 
1308
                          "no \"http_auth\" is defined for server in %s:%ui",
 
1309
                          conf->file, conf->line);
 
1310
 
 
1311
            return NGX_CONF_ERROR;
 
1312
        }
1332
1313
    }
1333
1314
 
1334
1315
    ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 60000);
1382
1363
    u.uri_part = 1;
1383
1364
    u.one_addr = 1;
1384
1365
 
 
1366
    if (ngx_strncmp(u.url.data, "http://", 7) == 0) {
 
1367
        u.url.len -= 7;
 
1368
        u.url.data += 7;
 
1369
    }
 
1370
 
1385
1371
    if (ngx_parse_url(cf, &u) != NGX_OK) {
1386
1372
        if (u.err) {
1387
1373
            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1388
1374
                               "%s in auth_http \"%V\"", u.err, &u.url);
1389
1375
        }
 
1376
 
 
1377
        return NGX_CONF_ERROR;
1390
1378
    }
1391
1379
 
1392
1380
    ahcf->peer = u.addrs;