2 * Copyright notice from original mutt:
3 * Copyright (C) 2000-5 Brendan Cully <brendan@kublai.com>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
10 /* common SASL helper routines */
18 #include "mutt_sasl.h"
19 #include "mutt_socket.h"
26 #include <sasl/sasl.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
34 static int getnameinfo_err (int ret)
38 dprint (1, (debugfile, "getnameinfo: "));
43 "The name could not be resolved at this time. Future attempts may succeed.\n"));
47 dprint (1, (debugfile, "The flags had an invalid value.\n"));
51 dprint (1, (debugfile, "A non-recoverable error occurred.\n"));
57 "The address family was not recognized or the address length was invalid for the specified family.\n"));
61 dprint (1, (debugfile, "There was a memory allocation failure.\n"));
67 "The name does not resolve for the supplied parameters. NI_NAMEREQD is set and the host's name cannot be located, or both nodename and servname were null.\n"));
68 err = SASL_FAIL; /* no real equivalent */
73 "A system error occurred. The error code can be found in errno(%d,%s)).\n",
74 errno, strerror (errno)));
75 err = SASL_FAIL; /* no real equivalent */
78 dprint (1, (debugfile, "Unknown error %d\n", ret));
79 err = SASL_FAIL; /* no real equivalent */
86 /* arbitrary. SASL will probably use a smaller buffer anyway. OTOH it's
87 * been a while since I've had access to an SASL server which negotiated
88 * a protection buffer. */
89 #define M_SASL_MAXBUF 65536
92 #define IP_PORT_BUFLEN 1024
95 static sasl_callback_t mutt_sasl_callbacks[5];
97 static int mutt_sasl_start (void);
100 static int mutt_sasl_cb_log (void *context, int priority,
101 const char *message);
102 static int mutt_sasl_cb_authname (void *context, int id, const char **result,
104 static int mutt_sasl_cb_pass (sasl_conn_t * conn, void *context, int id,
105 sasl_secret_t ** psecret);
107 /* socket wrappers for a SASL security layer */
108 static int mutt_sasl_conn_open (CONNECTION * conn);
109 static int mutt_sasl_conn_close (CONNECTION * conn);
110 static int mutt_sasl_conn_read (CONNECTION * conn, char *buf, size_t len);
111 static int mutt_sasl_conn_write (CONNECTION * conn, const char *buf,
115 /* utility function, stolen from sasl2 sample code */
116 static int iptostring (const struct sockaddr *addr, socklen_t addrlen,
117 char *out, unsigned outlen)
119 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
123 return SASL_BADPARAM;
125 ret = getnameinfo (addr, addrlen, hbuf, sizeof (hbuf), pbuf, sizeof (pbuf),
127 #ifdef NI_WITHSCOPEID
132 return getnameinfo_err (ret);
134 if (outlen < mutt_strlen (hbuf) + mutt_strlen (pbuf) + 2)
137 snprintf (out, outlen, "%s;%s", hbuf, pbuf);
143 /* mutt_sasl_start: called before doing a SASL exchange - initialises library
144 * (if neccessary). */
145 int mutt_sasl_start (void)
147 static unsigned char sasl_init = 0;
149 static sasl_callback_t callbacks[2];
155 /* set up default logging callback */
156 callbacks[0].id = SASL_CB_LOG;
157 callbacks[0].proc = mutt_sasl_cb_log;
158 callbacks[0].context = NULL;
160 callbacks[1].id = SASL_CB_LIST_END;
161 callbacks[1].proc = NULL;
162 callbacks[1].context = NULL;
164 rc = sasl_client_init (callbacks);
168 (debugfile, "mutt_sasl_start: libsasl initialisation failed.\n"));
177 /* mutt_sasl_client_new: wrapper for sasl_client_new which also sets various
178 * security properties. If this turns out to be fine for POP too we can
179 * probably stop exporting mutt_sasl_get_callbacks(). */
180 int mutt_sasl_client_new (CONNECTION * conn, sasl_conn_t ** saslconn)
182 sasl_security_properties_t secprops;
185 struct sockaddr_storage local, remote;
187 char iplocalport[IP_PORT_BUFLEN], ipremoteport[IP_PORT_BUFLEN];
189 sasl_external_properties_t extprops;
194 if (mutt_sasl_start () != SASL_OK)
197 switch (conn->account.type) {
198 case M_ACCT_TYPE_IMAP:
201 case M_ACCT_TYPE_POP:
205 dprint (1, (debugfile, "mutt_sasl_client_new: account type unset\n"));
210 size = sizeof (local);
211 if (getsockname (conn->fd, (struct sockaddr *) &local, &size)) {
214 "mutt_sasl_client_new: getsockname for local failed\n"));
219 ((struct sockaddr *) &local, size, iplocalport,
220 IP_PORT_BUFLEN) != SASL_OK) {
223 "mutt_sasl_client_new: iptostring for local failed\n"));
227 size = sizeof (remote);
228 if (getpeername (conn->fd, (struct sockaddr *) &remote, &size)) {
231 "mutt_sasl_client_new: getsockname for remote failed\n"));
236 ((struct sockaddr *) &remote, size, ipremoteport,
237 IP_PORT_BUFLEN) != SASL_OK) {
240 "mutt_sasl_client_new: iptostring for remote failed\n"));
245 (debugfile, "local ip: %s, remote ip:%s\n", iplocalport,
249 sasl_client_new (service, conn->account.host, iplocalport, ipremoteport,
250 mutt_sasl_get_callbacks (&conn->account), 0, saslconn);
253 rc = sasl_client_new (service, conn->account.host,
254 mutt_sasl_get_callbacks (&conn->account),
255 SASL_SECURITY_LAYER, saslconn);
259 dprint (1, (debugfile,
260 "mutt_sasl_client_new: Error allocating SASL connection\n"));
264 /*** set sasl IP properties, necessary for use with krb4 ***/
265 /* Do we need to fail if this fails? I would assume having these unset
266 * would just disable KRB4. Who wrote this code? I'm not sure how this
267 * interacts with the NSS code either, since that mucks with the fd. */
268 #ifndef USE_SASL2 /* with SASLv2 this all happens in sasl_client_new */
270 struct sockaddr_in local, remote;
273 size = sizeof (local);
274 if (getsockname (conn->fd, (struct sockaddr *) &local, &size))
277 size = sizeof (remote);
278 if (getpeername (conn->fd, (struct sockaddr *) &remote, &size))
282 if (sasl_setprop (*saslconn, SASL_IP_LOCAL, &local) != SASL_OK) {
283 dprint (1, (debugfile,
284 "mutt_sasl_client_new: Error setting local IP address\n"));
289 #ifdef SASL_IP_REMOTE
290 if (sasl_setprop (*saslconn, SASL_IP_REMOTE, &remote) != SASL_OK) {
291 dprint (1, (debugfile,
292 "mutt_sasl_client_new: Error setting remote IP address\n"));
299 /* set security properties. We use NOPLAINTEXT globally, since we can
300 * just fall back to LOGIN in the IMAP case anyway. If that doesn't
301 * work for POP, we can make it a flag or move this code into
302 * imap/auth_sasl.c */
303 memset (&secprops, 0, sizeof (secprops));
304 /* Work around a casting bug in the SASL krb4 module */
305 secprops.max_ssf = 0x7fff;
306 secprops.maxbufsize = M_SASL_MAXBUF;
307 secprops.security_flags |= SASL_SEC_NOPLAINTEXT;
308 if (sasl_setprop (*saslconn, SASL_SEC_PROPS, &secprops) != SASL_OK) {
309 dprint (1, (debugfile,
310 "mutt_sasl_client_new: Error setting security properties\n"));
314 /* we currently don't have an SSF finder for NSS (I don't know the API).
315 * If someone does it'd probably be trivial to write mutt_nss_get_ssf().
316 * I have a feeling more SSL code could be shared between those two files,
317 * but I haven't looked into it yet, since I still don't know the APIs. */
318 #if (defined(USE_SSL) || defined(USE_GNUTLS) && !defined(USE_NSS))
319 if (conn->account.flags & M_ACCT_SSL) {
320 #ifdef USE_SASL2 /* I'm not sure this actually has an effect, at least with SASLv2 */
321 dprint (2, (debugfile, "External SSF: %d\n", conn->ssf));
322 if (sasl_setprop (*saslconn, SASL_SSF_EXTERNAL, &(conn->ssf)) != SASL_OK)
324 memset (&extprops, 0, sizeof (extprops));
325 extprops.ssf = conn->ssf;
326 dprint (2, (debugfile, "External SSF: %d\n", extprops.ssf));
327 if (sasl_setprop (*saslconn, SASL_SSF_EXTERNAL, &extprops) != SASL_OK)
332 "mutt_sasl_client_new: Error setting external properties\n"));
337 (debugfile, "External authentication name: %s\n",
338 conn->account.user));
339 if (sasl_setprop (*saslconn, SASL_AUTH_EXTERNAL, conn->account.user) !=
343 "mutt_sasl_client_new: Error setting external properties\n"));
353 sasl_callback_t *mutt_sasl_get_callbacks (ACCOUNT * account)
355 sasl_callback_t *callback;
357 callback = mutt_sasl_callbacks;
359 callback->id = SASL_CB_AUTHNAME;
360 callback->proc = mutt_sasl_cb_authname;
361 callback->context = account;
364 callback->id = SASL_CB_USER;
365 callback->proc = mutt_sasl_cb_authname;
366 callback->context = account;
369 callback->id = SASL_CB_PASS;
370 callback->proc = mutt_sasl_cb_pass;
371 callback->context = account;
374 callback->id = SASL_CB_GETREALM;
375 callback->proc = NULL;
376 callback->context = NULL;
379 callback->id = SASL_CB_LIST_END;
380 callback->proc = NULL;
381 callback->context = NULL;
383 return mutt_sasl_callbacks;
386 int mutt_sasl_interact (sasl_interact_t * interaction)
388 char prompt[SHORT_STRING];
389 char resp[SHORT_STRING];
391 while (interaction->id != SASL_CB_LIST_END) {
394 "mutt_sasl_interact: filling in SASL interaction %ld.\n",
397 snprintf (prompt, sizeof (prompt), "%s: ", interaction->prompt);
399 if (mutt_get_field (prompt, resp, sizeof (resp), 0))
402 interaction->len = mutt_strlen (resp) + 1;
403 interaction->result = safe_malloc (interaction->len);
404 memcpy (interaction->result, resp, interaction->len);
412 /* SASL can stack a protection layer on top of an existing connection.
413 * To handle this, we store a saslconn_t in conn->sockdata, and write
414 * wrappers which en/decode the read/write stream, then replace sockdata
415 * with an embedded copy of the old sockdata and call the underlying
416 * functions (which we've also preserved). I thought about trying to make
417 * a general stackable connection system, but it seemed like overkill -
418 * something is wrong if we have 15 filters on top of a socket. Anyway,
419 * anything else which wishes to stack can use the same method. The only
420 * disadvantage is we have to write wrappers for all the socket methods,
421 * even if we only stack over read and write. Thinking about it, the
422 * abstraction problem is that there is more in CONNECTION than there
423 * needs to be. Ideally it would have only (void*)data and methods. */
425 /* mutt_sasl_setup_conn: replace connection methods, sockdata with
426 * SASL wrappers, for protection layers. Also get ssf, as a fastpath
427 * for the read/write methods. */
428 void mutt_sasl_setup_conn (CONNECTION * conn, sasl_conn_t * saslconn)
430 SASL_DATA *sasldata = (SASL_DATA *) safe_malloc (sizeof (SASL_DATA));
432 sasldata->saslconn = saslconn;
433 /* get ssf so we know whether we have to (en|de)code read/write */
435 sasl_getprop (saslconn, SASL_SSF, (const void **) &sasldata->ssf);
437 sasl_getprop (saslconn, SASL_SSF, (void **) &sasldata->ssf);
439 dprint (3, (debugfile, "SASL protection strength: %u\n", *sasldata->ssf));
440 /* Add SASL SSF to transport SSF */
441 conn->ssf += *sasldata->ssf;
443 sasl_getprop (saslconn, SASL_MAXOUTBUF,
444 (const void **) &sasldata->pbufsize);
446 sasl_getprop (saslconn, SASL_MAXOUTBUF, (void **) &sasldata->pbufsize);
449 (debugfile, "SASL protection buffer size: %u\n",
450 *sasldata->pbufsize));
452 /* clear input buffer */
453 sasldata->buf = NULL;
457 /* preserve old functions */
458 sasldata->sockdata = conn->sockdata;
459 sasldata->msasl_open = conn->conn_open;
460 sasldata->msasl_close = conn->conn_close;
461 sasldata->msasl_read = conn->conn_read;
462 sasldata->msasl_write = conn->conn_write;
464 /* and set up new functions */
465 conn->sockdata = sasldata;
466 conn->conn_open = mutt_sasl_conn_open;
467 conn->conn_close = mutt_sasl_conn_close;
468 conn->conn_read = mutt_sasl_conn_read;
469 conn->conn_write = mutt_sasl_conn_write;
472 /* mutt_sasl_cb_log: callback to log SASL messages */
473 static int mutt_sasl_cb_log (void *context, int priority, const char *message)
475 dprint (priority, (debugfile, "SASL: %s\n", message));
480 /* mutt_sasl_cb_authname: callback to retrieve authname or user (mutt
481 * doesn't distinguish, even if some SASL plugins do) from ACCOUNT */
482 static int mutt_sasl_cb_authname (void *context, int id, const char **result,
485 ACCOUNT *account = (ACCOUNT *) context;
492 return SASL_BADPARAM;
494 dprint (2, (debugfile, "mutt_sasl_cb_authname: getting %s for %s:%u\n",
495 id == SASL_CB_AUTHNAME ? "authname" : "user",
496 account->host, account->port));
498 if (mutt_account_getuser (account))
501 *result = account->user;
504 *len = mutt_strlen (*result);
509 static int mutt_sasl_cb_pass (sasl_conn_t * conn, void *context, int id,
510 sasl_secret_t ** psecret)
512 ACCOUNT *account = (ACCOUNT *) context;
515 if (!account || !psecret)
516 return SASL_BADPARAM;
518 dprint (2, (debugfile,
519 "mutt_sasl_cb_pass: getting password for %s@%s:%u\n",
520 account->user, account->host, account->port));
522 if (mutt_account_getpass (account))
525 len = mutt_strlen (account->pass);
527 *psecret = (sasl_secret_t *) safe_malloc (sizeof (sasl_secret_t) + len);
528 (*psecret)->len = len;
529 strcpy ((*psecret)->data, account->pass); /* __STRCPY_CHECKED__ */
534 /* mutt_sasl_conn_open: empty wrapper for underlying open function. We
535 * don't know in advance that a connection will use SASL, so we
536 * replace conn's methods with sasl methods when authentication
537 * is successful, using mutt_sasl_setup_conn */
538 static int mutt_sasl_conn_open (CONNECTION * conn)
543 sasldata = (SASL_DATA *) conn->sockdata;
544 conn->sockdata = sasldata->sockdata;
545 rc = (sasldata->msasl_open) (conn);
546 conn->sockdata = sasldata;
551 /* mutt_sasl_conn_close: calls underlying close function and disposes of
552 * the sasl_conn_t object, then restores connection to pre-sasl state */
553 static int mutt_sasl_conn_close (CONNECTION * conn)
558 sasldata = (SASL_DATA *) conn->sockdata;
560 /* restore connection's underlying methods */
561 conn->sockdata = sasldata->sockdata;
562 conn->conn_open = sasldata->msasl_open;
563 conn->conn_close = sasldata->msasl_close;
564 conn->conn_read = sasldata->msasl_read;
565 conn->conn_write = sasldata->msasl_write;
567 /* release sasl resources */
568 sasl_dispose (&sasldata->saslconn);
570 FREE (&sasldata->buf);
574 /* call underlying close */
575 rc = (conn->conn_close) (conn);
580 static int mutt_sasl_conn_read (CONNECTION * conn, char *buf, size_t len)
587 sasldata = (SASL_DATA *) conn->sockdata;
589 /* if we still have data in our read buffer, copy it into buf */
590 if (sasldata->blen > sasldata->bpos) {
591 olen = (sasldata->blen - sasldata->bpos > len) ? len :
592 sasldata->blen - sasldata->bpos;
594 memcpy (buf, sasldata->buf + sasldata->bpos, olen);
595 sasldata->bpos += olen;
600 conn->sockdata = sasldata->sockdata;
603 FREE (&sasldata->buf);
608 /* and decode the result, if necessary */
609 if (*sasldata->ssf) {
611 /* call the underlying read function to fill the buffer */
612 rc = (sasldata->msasl_read) (conn, buf, len);
616 rc = sasl_decode (sasldata->saslconn, buf, rc, &sasldata->buf,
619 dprint (1, (debugfile, "SASL decode failed: %s\n",
620 sasl_errstring (rc, NULL, NULL)));
624 while (!sasldata->blen);
626 olen = (sasldata->blen - sasldata->bpos > len) ? len :
627 sasldata->blen - sasldata->bpos;
629 memcpy (buf, sasldata->buf, olen);
630 sasldata->bpos += olen;
635 rc = (sasldata->msasl_read) (conn, buf, len);
638 conn->sockdata = sasldata;
643 static int mutt_sasl_conn_write (CONNECTION * conn, const char *buf,
654 unsigned int olen, plen;
656 sasldata = (SASL_DATA *) conn->sockdata;
657 conn->sockdata = sasldata->sockdata;
659 /* encode data, if necessary */
660 if (*sasldata->ssf) {
661 /* handle data larger than MAXOUTBUF */
663 olen = (len > *sasldata->pbufsize) ? *sasldata->pbufsize : len;
665 rc = sasl_encode (sasldata->saslconn, buf, olen, &pbuf, &plen);
667 dprint (1, (debugfile, "SASL encoding failed: %s\n",
668 sasl_errstring (rc, NULL, NULL)));
672 rc = (sasldata->msasl_write) (conn, pbuf, plen);
682 while (len > *sasldata->pbufsize);
685 /* just write using the underlying socket function */
686 rc = (sasldata->msasl_write) (conn, buf, len);
688 conn->sockdata = sasldata;
693 conn->sockdata = sasldata;