~ubuntu-branches/ubuntu/wily/dovecot/wily

« back to all changes in this revision

Viewing changes to src/imap-login/client-authenticate.c

  • Committer: Package Import Robot
  • Author(s): Jaldhar H. Vyas
  • Date: 2013-09-09 00:57:32 UTC
  • mfrom: (1.13.11)
  • mto: (4.8.5 experimental) (1.16.1)
  • mto: This revision was merged to the branch mainline in revision 97.
  • Revision ID: package-import@ubuntu.com-20130909005732-dn1eell8srqbhh0e
Tags: upstream-2.2.5
ImportĀ upstreamĀ versionĀ 2.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2002-2012 Dovecot authors, see the included COPYING file */
 
1
/* Copyright (c) 2002-2013 Dovecot authors, see the included COPYING file */
2
2
 
3
3
#include "login-common.h"
4
4
#include "base64.h"
5
5
#include "buffer.h"
6
 
#include "hostpid.h"
7
6
#include "ioloop.h"
8
7
#include "istream.h"
9
8
#include "ostream.h"
10
9
#include "safe-memset.h"
11
10
#include "str.h"
12
11
#include "str-sanitize.h"
13
 
#include "time-util.h"
 
12
#include "net.h"
14
13
#include "imap-resp-code.h"
15
14
#include "imap-parser.h"
 
15
#include "imap-url.h"
16
16
#include "auth-client.h"
17
17
#include "client.h"
18
18
#include "client-authenticate.h"
33
33
        }
34
34
}
35
35
 
36
 
bool imap_client_auth_handle_reply(struct client *client,
37
 
                                   const struct client_auth_reply *reply)
 
36
void imap_client_auth_result(struct client *client,
 
37
                             enum client_auth_result result,
 
38
                             const struct client_auth_reply *reply,
 
39
                             const char *text)
38
40
{
39
 
        struct imap_client *imap_client = (struct imap_client *)client;
40
 
        string_t *str;
41
 
        const char *timestamp, *msg;
 
41
        struct imap_url url;
 
42
        string_t *referral;
42
43
 
43
 
        if (reply->host != NULL) {
 
44
        switch (result) {
 
45
        case CLIENT_AUTH_RESULT_SUCCESS:
 
46
                /* nothing to be done for IMAP */
 
47
                break;
 
48
        case CLIENT_AUTH_RESULT_REFERRAL_SUCCESS:
 
49
        case CLIENT_AUTH_RESULT_REFERRAL_NOLOGIN:
44
50
                /* IMAP referral
45
51
 
46
52
                   [nologin] referral host=.. [port=..] [destuser=..]
50
56
                   OK [...] Logged in, but you should use this server instead.
51
57
                   .. [REFERRAL ..] (Reason from auth server)
52
58
                */
53
 
                str = t_str_new(128);
54
 
                str_append(str, imap_client->cmd_tag);
55
 
                str_append_c(str, ' ');
56
 
                str_append(str, reply->nologin ? "NO " : "OK ");
57
 
                str_printfa(str, "[REFERRAL imap://%s;AUTH=%s@%s",
58
 
                            reply->destuser, client->auth_mech_name,
59
 
                            reply->host);
60
 
                if (reply->port != 143)
61
 
                        str_printfa(str, ":%u", reply->port);
62
 
                str_append(str, "/] ");
63
 
                if (reply->reason != NULL)
64
 
                        str_append(str, reply->reason);
65
 
                else if (reply->nologin)
66
 
                        str_append(str, "Try this server instead.");
67
 
                else {
68
 
                        str_append(str, "Logged in, but you should use "
69
 
                                   "this server instead.");
70
 
                }
71
 
                str_append(str, "\r\n");
72
 
                client_send_raw(client, str_c(str));
73
 
                if (!reply->nologin) {
74
 
                        client_destroy_success(client, "Login with referral");
75
 
                        return TRUE;
76
 
                }
77
 
        } else if (!reply->nologin) {
78
 
                /* normal login/failure */
79
 
                return FALSE;
80
 
        } else if (reply->reason != NULL) {
81
 
                client_send_line(client, CLIENT_CMD_REPLY_AUTH_FAIL_REASON,
82
 
                                 reply->reason);
83
 
        } else if (reply->temp) {
84
 
                timestamp = t_strflocaltime("%Y-%m-%d %H:%M:%S", ioloop_time);
85
 
                msg = t_strdup_printf(AUTH_TEMP_FAILED_MSG" [%s:%s]",
86
 
                                      my_hostname, timestamp);
87
 
                client_send_line(client,
88
 
                                 CLIENT_CMD_REPLY_AUTH_FAIL_TEMP, msg);
89
 
        } else if (reply->authz_failure) {
90
 
                client_send_line(client, CLIENT_CMD_REPLY_AUTHZ_FAILED,
91
 
                                 "Authorization failed");
92
 
        } else {
93
 
                client_send_line(client, CLIENT_CMD_REPLY_AUTH_FAILED,
94
 
                                 AUTH_FAILED_MSG);
 
59
                referral = t_str_new(128);
 
60
 
 
61
                memset(&url, 0, sizeof(url));
 
62
                url.userid = reply->destuser;
 
63
                url.auth_type = client->auth_mech_name;
 
64
                url.host_name = reply->host;
 
65
                if (reply->port != 143) {
 
66
                        url.have_port = TRUE;
 
67
                        url.port = reply->port;
 
68
                }
 
69
                str_append(referral, "REFERRAL ");
 
70
                str_append(referral, imap_url_create(&url));
 
71
 
 
72
                if (result == CLIENT_AUTH_RESULT_REFERRAL_SUCCESS) {
 
73
                        client_send_reply_code(client, IMAP_CMD_REPLY_OK,
 
74
                                               str_c(referral), text);
 
75
                } else {
 
76
                        client_send_reply_code(client, IMAP_CMD_REPLY_NO,
 
77
                                               str_c(referral), text);
 
78
                }
 
79
                break;
 
80
        case CLIENT_AUTH_RESULT_ABORTED:
 
81
                client_send_reply(client, IMAP_CMD_REPLY_BAD, text);
 
82
                break;
 
83
        case CLIENT_AUTH_RESULT_AUTHFAILED_REASON:
 
84
                client_send_reply_code(client, IMAP_CMD_REPLY_NO,
 
85
                                       "ALERT", text);
 
86
                break;
 
87
        case CLIENT_AUTH_RESULT_AUTHZFAILED:
 
88
                client_send_reply_code(client, IMAP_CMD_REPLY_NO,
 
89
                                       IMAP_RESP_CODE_AUTHZFAILED, text);
 
90
                break;
 
91
        case CLIENT_AUTH_RESULT_TEMPFAIL:
 
92
                client_send_reply_code(client, IMAP_CMD_REPLY_NO,
 
93
                                       IMAP_RESP_CODE_UNAVAILABLE, text);
 
94
                break;
 
95
        case CLIENT_AUTH_RESULT_SSL_REQUIRED:
 
96
                client_send_reply_code(client, IMAP_CMD_REPLY_NO,
 
97
                                       IMAP_RESP_CODE_PRIVACYREQUIRED, text);
 
98
                break;
 
99
        case CLIENT_AUTH_RESULT_AUTHFAILED:
 
100
                client_send_reply_code(client, IMAP_CMD_REPLY_NO,
 
101
                                       IMAP_RESP_CODE_AUTHFAILED, text);
 
102
                break;
95
103
        }
96
 
 
97
 
        i_assert(reply->nologin);
98
 
 
99
 
        if (!client->destroyed)
100
 
                client_auth_failed(client);
101
 
        return TRUE;
102
104
}
103
105
 
104
106
static int