File: imap-auth.c

package info (click to toggle)
balsa 2.4.12-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 21,456 kB
  • ctags: 8,303
  • sloc: ansic: 93,724; xml: 13,662; sh: 11,146; makefile: 615; awk: 60
file content (234 lines) | stat: -rw-r--r-- 7,109 bytes parent folder | download | duplicates (6)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* libimap library.
 * Copyright (C) 2003-2004 Pawel Salek.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option) 
 * any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  
 * 02111-1307, USA.
 */
/* imap_authenticate: Attempt to authenticate using either user-specified
 *   authentication method if specified, or any.
 * returns 0 on success */

#include "config.h"

#include <stdio.h>
#include <string.h>

#include "imap-handle.h"
#include "imap-auth.h"
#include "util.h"
#include "imap_private.h"
#include "siobuf.h"

static ImapResult imap_auth_anonymous(ImapMboxHandle* handle);
static ImapResult imap_auth_plain(ImapMboxHandle* handle);

/* ordered from strongest to weakest. Auth anonymous does not really
 * belong here, does it? */
static ImapAuthenticator imap_authenticators_arr[] = {
  imap_auth_anonymous, /* will be tried only if enabled */
  imap_auth_gssapi,
  imap_auth_cram,
  imap_auth_plain,
  imap_auth_login, /* login is deprecated */
  NULL
};

ImapResult
imap_authenticate(ImapMboxHandle* handle)
{
  ImapAuthenticator* authenticator;
  ImapResult r = IMAP_AUTH_UNAVAIL;

  g_return_val_if_fail(handle, IMAP_AUTH_UNAVAIL);

  if (imap_mbox_is_authenticated(handle) || imap_mbox_is_selected(handle))
    return IMAP_SUCCESS;

  for(authenticator = imap_authenticators_arr;
      *authenticator; authenticator++) {
    if ((r = (*authenticator)(handle)) 
        != IMAP_AUTH_UNAVAIL) {
      if (r == IMAP_SUCCESS)
	imap_mbox_handle_set_state(handle, IMHS_AUTHENTICATED);
      return r;
    }
  }
  imap_mbox_handle_set_msg(handle, "No way to authenticate is known");
  return r;
}

/* =================================================================== */
/*                           AUTHENTICATORS                            */
/* =================================================================== */
#define SHORT_STRING 64
/* imap_auth_login: Plain LOGIN support */
ImapResult
imap_auth_login(ImapMboxHandle* handle)
{
  char q_user[SHORT_STRING], q_pass[SHORT_STRING];
  char buf[2*SHORT_STRING+7];
  char *user = NULL, *pass = NULL;
  ImapResponse rc;
  int ok;
  
  if (imap_mbox_handle_can_do(handle, IMCAP_LOGINDISABLED))
    return IMAP_AUTH_UNAVAIL;
  
  ok = 0;
  if(!ok && handle->user_cb)
    handle->user_cb(IME_GET_USER_PASS, handle->user_arg,
                    "LOGIN", &user, &pass, &ok);
  if(!ok || user == NULL || pass == NULL) {
    imap_mbox_handle_set_msg(handle, "Authentication cancelled");
    return IMAP_AUTH_CANCELLED;
  }

  imap_quote_string(q_user, sizeof (q_user), user);
  imap_quote_string(q_pass, sizeof (q_pass), pass);
  g_free(user); g_free(pass); /* FIXME: clean passwd first */
  g_snprintf (buf, sizeof (buf), "LOGIN %s %s", q_user, q_pass);
  rc = imap_cmd_exec(handle, buf);
  return  (rc== IMR_OK) ?  IMAP_SUCCESS : IMAP_AUTH_FAILURE;
}

/* =================================================================== */
/* SASL PLAIN RFC-2595                                                 */
/* =================================================================== */
#define WAIT_FOR_PROMPT(rc,handle,cmdno) \
    do (rc) = imap_cmd_step((handle), (cmdno)); while((rc) == IMR_UNTAGGED);

static ImapResult
imap_auth_sasl(ImapMboxHandle* handle, ImapCapability cap,
	       const char *sasl_cmd,
	       gboolean (*getmsg)(ImapMboxHandle *h, char **msg, int *msglen))
{
  char *msg = NULL, *msg64;
  ImapResponse rc;
  int msglen, msg64len;
  unsigned cmdno;
  gboolean sasl_ir;
  
  if (!imap_mbox_handle_can_do(handle, cap))
    return IMAP_AUTH_UNAVAIL;
  sasl_ir = imap_mbox_handle_can_do(handle, IMCAP_SASLIR);
  
  if(!getmsg(handle, &msg, &msglen)) {
    imap_mbox_handle_set_msg(handle, "Authentication cancelled");
    return IMAP_AUTH_CANCELLED;
  }
  
  msg64len = 2*(msglen+2);
  msg64 = g_malloc(msg64len);
  lit_conv_to_base64(msg64, msg, msglen, msg64len);
  g_free(msg);

  if(sasl_ir) { /* save one RTT */
    ImapCmdTag tag;
    if(IMAP_MBOX_IS_DISCONNECTED(handle))
      return IMAP_AUTH_UNAVAIL;
    cmdno = imap_make_tag(tag);
    sio_write(handle->sio, tag, strlen(tag));
    sio_write(handle->sio, " ", 1);
    sio_write(handle->sio, sasl_cmd, strlen(sasl_cmd));
    sio_write(handle->sio, " ", 1);
  } else {
    int c;
    if(imap_cmd_start(handle, sasl_cmd, &cmdno) <0) {
      g_free(msg64);
      return IMAP_AUTH_FAILURE;
    }
    imap_handle_flush(handle);
    WAIT_FOR_PROMPT(rc,handle,cmdno);
    
    if (rc != IMR_RESPOND) {
      g_warning("imap %s: unexpected response.\n", sasl_cmd);
      g_free(msg64);
      return IMAP_AUTH_FAILURE;
    }
    while( (c=sio_getc((handle)->sio)) != EOF && c != '\n');
    if(c == EOF) {
      imap_handle_disconnect(handle);
      g_free(msg64);
      return IMAP_AUTH_FAILURE;
    }
  }
  sio_write(handle->sio, msg64, strlen(msg64));
  sio_write(handle->sio, "\r\n", 2);
  g_free(msg64);
  imap_handle_flush(handle);
  do
    rc = imap_cmd_step (handle, cmdno);
  while (rc == IMR_UNTAGGED);
  return  (rc== IMR_OK) ?  IMAP_SUCCESS : IMAP_AUTH_FAILURE;
}

static gboolean
getmsg_plain(ImapMboxHandle *h, char **retmsg, int *retmsglen)
{
  char *user = NULL, *pass = NULL, *msg;
  int ok, userlen, passlen, msglen;
  ok = 0;
  if(!ok && h->user_cb)
    h->user_cb(IME_GET_USER_PASS, h->user_arg,
                    "LOGIN", &user, &pass, &ok);
  if(!ok || user == NULL || pass == NULL)
    return 0;

  userlen = strlen(user);
  passlen = strlen(pass);
  msglen  = 2*userlen + passlen + 2;
  msg     = g_malloc(msglen+1);
  strcpy(msg, user); 
  strcpy(msg+userlen+1, user);
  strcpy(msg+userlen*2+2, pass); 
  g_free(user); g_free(pass);
  *retmsg = msg;
  *retmsglen = msglen;
  return 1;
}

static ImapResult
imap_auth_plain(ImapMboxHandle* handle)
{
  return imap_auth_sasl(handle, IMCAP_APLAIN, "AUTHENTICATE PLAIN",
			getmsg_plain);
}


/* =================================================================== */
/* SASL ANONYMOUS RFC-2245                                             */
/* =================================================================== */
static gboolean
getmsg_anonymous(ImapMboxHandle *h, char **retmsg, int *retmsglen)
{
  int ok = 0;
  if(!ok && h->user_cb)
    h->user_cb(IME_GET_USER, h->user_arg,
	       "ANONYMOUS", retmsg, &ok);
  if(!ok || *retmsg == NULL)
    return 0;
  *retmsglen = strlen(*retmsg);
  return 1;
}

static ImapResult
imap_auth_anonymous(ImapMboxHandle* handle)
{
  if(!handle->enable_anonymous)
    return IMAP_AUTH_UNAVAIL;
  return imap_auth_sasl(handle, IMCAP_AANONYMOUS, "AUTHENTICATE ANONYMOUS",
			getmsg_anonymous);
}