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"
24 #include <sasl/sasl.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
32 static int getnameinfo_err (int ret)
36 dprint (1, (debugfile, "getnameinfo: "));
41 "The name could not be resolved at this time. Future attempts may succeed.\n"));
45 dprint (1, (debugfile, "The flags had an invalid value.\n"));
49 dprint (1, (debugfile, "A non-recoverable error occurred.\n"));
55 "The address family was not recognized or the address length was invalid for the specified family.\n"));
59 dprint (1, (debugfile, "There was a memory allocation failure.\n"));
65 "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"));
66 err = SASL_FAIL; /* no real equivalent */
71 "A system error occurred. The error code can be found in errno(%d,%s)).\n",
72 errno, strerror (errno)));
73 err = SASL_FAIL; /* no real equivalent */
76 dprint (1, (debugfile, "Unknown error %d\n", ret));
77 err = SASL_FAIL; /* no real equivalent */
84 /* arbitrary. SASL will probably use a smaller buffer anyway. OTOH it's
85 * been a while since I've had access to an SASL server which negotiated
86 * a protection buffer. */
87 #define M_SASL_MAXBUF 65536
90 #define IP_PORT_BUFLEN 1024
93 static sasl_callback_t mutt_sasl_callbacks[5];
95 static int mutt_sasl_start (void);
98 static int mutt_sasl_cb_log (void *context, int priority,
100 static int mutt_sasl_cb_authname (void *context, int id, const char **result,
102 static int mutt_sasl_cb_pass (sasl_conn_t * conn, void *context, int id,
103 sasl_secret_t ** psecret);
105 /* socket wrappers for a SASL security layer */
106 static int mutt_sasl_conn_open (CONNECTION * conn);
107 static int mutt_sasl_conn_close (CONNECTION * conn);
108 static int mutt_sasl_conn_read (CONNECTION * conn, char *buf, size_t len);
109 static int mutt_sasl_conn_write (CONNECTION * conn, const char *buf,
113 /* utility function, stolen from sasl2 sample code */
114 static int iptostring (const struct sockaddr *addr, socklen_t addrlen,
115 char *out, unsigned outlen)
117 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
121 return SASL_BADPARAM;
123 ret = getnameinfo (addr, addrlen, hbuf, sizeof (hbuf), pbuf, sizeof (pbuf),
125 #ifdef NI_WITHSCOPEID
130 return getnameinfo_err (ret);
132 if (outlen < mutt_strlen (hbuf) + mutt_strlen (pbuf) + 2)
135 snprintf (out, outlen, "%s;%s", hbuf, pbuf);
141 /* mutt_sasl_start: called before doing a SASL exchange - initialises library
142 * (if neccessary). */
143 int mutt_sasl_start (void)
145 static unsigned char sasl_init = 0;
147 static sasl_callback_t callbacks[2];
153 /* set up default logging callback */
154 callbacks[0].id = SASL_CB_LOG;
155 callbacks[0].proc = mutt_sasl_cb_log;
156 callbacks[0].context = NULL;
158 callbacks[1].id = SASL_CB_LIST_END;
159 callbacks[1].proc = NULL;
160 callbacks[1].context = NULL;
162 rc = sasl_client_init (callbacks);
166 (debugfile, "mutt_sasl_start: libsasl initialisation failed.\n"));
175 /* mutt_sasl_client_new: wrapper for sasl_client_new which also sets various
176 * security properties. If this turns out to be fine for POP too we can
177 * probably stop exporting mutt_sasl_get_callbacks(). */
178 int mutt_sasl_client_new (CONNECTION * conn, sasl_conn_t ** saslconn)
180 sasl_security_properties_t secprops;
183 struct sockaddr_storage local, remote;
185 char iplocalport[IP_PORT_BUFLEN], ipremoteport[IP_PORT_BUFLEN];
187 sasl_external_properties_t extprops;
192 if (mutt_sasl_start () != SASL_OK)
195 switch (conn->account.type) {
196 case M_ACCT_TYPE_IMAP:
199 case M_ACCT_TYPE_POP:
203 dprint (1, (debugfile, "mutt_sasl_client_new: account type unset\n"));
208 size = sizeof (local);
209 if (getsockname (conn->fd, (struct sockaddr *) &local, &size)) {
212 "mutt_sasl_client_new: getsockname for local failed\n"));
217 ((struct sockaddr *) &local, size, iplocalport,
218 IP_PORT_BUFLEN) != SASL_OK) {
221 "mutt_sasl_client_new: iptostring for local failed\n"));
225 size = sizeof (remote);
226 if (getpeername (conn->fd, (struct sockaddr *) &remote, &size)) {
229 "mutt_sasl_client_new: getsockname for remote failed\n"));
234 ((struct sockaddr *) &remote, size, ipremoteport,
235 IP_PORT_BUFLEN) != SASL_OK) {
238 "mutt_sasl_client_new: iptostring for remote failed\n"));
243 (debugfile, "local ip: %s, remote ip:%s\n", iplocalport,
247 sasl_client_new (service, conn->account.host, iplocalport, ipremoteport,
248 mutt_sasl_get_callbacks (&conn->account), 0, saslconn);
251 rc = sasl_client_new (service, conn->account.host,
252 mutt_sasl_get_callbacks (&conn->account),
253 SASL_SECURITY_LAYER, saslconn);
257 dprint (1, (debugfile,
258 "mutt_sasl_client_new: Error allocating SASL connection\n"));
262 /*** set sasl IP properties, necessary for use with krb4 ***/
263 /* Do we need to fail if this fails? I would assume having these unset
264 * would just disable KRB4. Who wrote this code? I'm not sure how this
265 * interacts with the NSS code either, since that mucks with the fd. */
266 #ifndef USE_SASL2 /* with SASLv2 this all happens in sasl_client_new */
268 struct sockaddr_in local, remote;
271 size = sizeof (local);
272 if (getsockname (conn->fd, (struct sockaddr *) &local, &size))
275 size = sizeof (remote);
276 if (getpeername (conn->fd, (struct sockaddr *) &remote, &size))
280 if (sasl_setprop (*saslconn, SASL_IP_LOCAL, &local) != SASL_OK) {
281 dprint (1, (debugfile,
282 "mutt_sasl_client_new: Error setting local IP address\n"));
287 #ifdef SASL_IP_REMOTE
288 if (sasl_setprop (*saslconn, SASL_IP_REMOTE, &remote) != SASL_OK) {
289 dprint (1, (debugfile,
290 "mutt_sasl_client_new: Error setting remote IP address\n"));
297 /* set security properties. We use NOPLAINTEXT globally, since we can
298 * just fall back to LOGIN in the IMAP case anyway. If that doesn't
299 * work for POP, we can make it a flag or move this code into
300 * imap/auth_sasl.c */
301 memset (&secprops, 0, sizeof (secprops));
302 /* Work around a casting bug in the SASL krb4 module */
303 secprops.max_ssf = 0x7fff;
304 secprops.maxbufsize = M_SASL_MAXBUF;
305 secprops.security_flags |= SASL_SEC_NOPLAINTEXT;
306 if (sasl_setprop (*saslconn, SASL_SEC_PROPS, &secprops) != SASL_OK) {
307 dprint (1, (debugfile,
308 "mutt_sasl_client_new: Error setting security properties\n"));
312 /* we currently don't have an SSF finder for NSS (I don't know the API).
313 * If someone does it'd probably be trivial to write mutt_nss_get_ssf().
314 * I have a feeling more SSL code could be shared between those two files,
315 * but I haven't looked into it yet, since I still don't know the APIs. */
316 #if (defined(USE_SSL) || defined(USE_GNUTLS) && !defined(USE_NSS))
317 if (conn->account.flags & M_ACCT_SSL) {
318 #ifdef USE_SASL2 /* I'm not sure this actually has an effect, at least with SASLv2 */
319 dprint (2, (debugfile, "External SSF: %d\n", conn->ssf));
320 if (sasl_setprop (*saslconn, SASL_SSF_EXTERNAL, &(conn->ssf)) != SASL_OK)
322 memset (&extprops, 0, sizeof (extprops));
323 extprops.ssf = conn->ssf;
324 dprint (2, (debugfile, "External SSF: %d\n", extprops.ssf));
325 if (sasl_setprop (*saslconn, SASL_SSF_EXTERNAL, &extprops) != SASL_OK)
330 "mutt_sasl_client_new: Error setting external properties\n"));
335 (debugfile, "External authentication name: %s\n",
336 conn->account.user));
337 if (sasl_setprop (*saslconn, SASL_AUTH_EXTERNAL, conn->account.user) !=
341 "mutt_sasl_client_new: Error setting external properties\n"));
351 sasl_callback_t *mutt_sasl_get_callbacks (ACCOUNT * account)
353 sasl_callback_t *callback;
355 callback = mutt_sasl_callbacks;
357 callback->id = SASL_CB_AUTHNAME;
358 callback->proc = mutt_sasl_cb_authname;
359 callback->context = account;
362 callback->id = SASL_CB_USER;
363 callback->proc = mutt_sasl_cb_authname;
364 callback->context = account;
367 callback->id = SASL_CB_PASS;
368 callback->proc = mutt_sasl_cb_pass;
369 callback->context = account;
372 callback->id = SASL_CB_GETREALM;
373 callback->proc = NULL;
374 callback->context = NULL;
377 callback->id = SASL_CB_LIST_END;
378 callback->proc = NULL;
379 callback->context = NULL;
381 return mutt_sasl_callbacks;
384 int mutt_sasl_interact (sasl_interact_t * interaction)
386 char prompt[SHORT_STRING];
387 char resp[SHORT_STRING];
389 while (interaction->id != SASL_CB_LIST_END) {
392 "mutt_sasl_interact: filling in SASL interaction %ld.\n",
395 snprintf (prompt, sizeof (prompt), "%s: ", interaction->prompt);
397 if (mutt_get_field (prompt, resp, sizeof (resp), 0))
400 interaction->len = mutt_strlen (resp) + 1;
401 interaction->result = safe_malloc (interaction->len);
402 memcpy (interaction->result, resp, interaction->len);
410 /* SASL can stack a protection layer on top of an existing connection.
411 * To handle this, we store a saslconn_t in conn->sockdata, and write
412 * wrappers which en/decode the read/write stream, then replace sockdata
413 * with an embedded copy of the old sockdata and call the underlying
414 * functions (which we've also preserved). I thought about trying to make
415 * a general stackable connection system, but it seemed like overkill -
416 * something is wrong if we have 15 filters on top of a socket. Anyway,
417 * anything else which wishes to stack can use the same method. The only
418 * disadvantage is we have to write wrappers for all the socket methods,
419 * even if we only stack over read and write. Thinking about it, the
420 * abstraction problem is that there is more in CONNECTION than there
421 * needs to be. Ideally it would have only (void*)data and methods. */
423 /* mutt_sasl_setup_conn: replace connection methods, sockdata with
424 * SASL wrappers, for protection layers. Also get ssf, as a fastpath
425 * for the read/write methods. */
426 void mutt_sasl_setup_conn (CONNECTION * conn, sasl_conn_t * saslconn)
428 SASL_DATA *sasldata = (SASL_DATA *) safe_malloc (sizeof (SASL_DATA));
430 sasldata->saslconn = saslconn;
431 /* get ssf so we know whether we have to (en|de)code read/write */
433 sasl_getprop (saslconn, SASL_SSF, (const void **) &sasldata->ssf);
435 sasl_getprop (saslconn, SASL_SSF, (void **) &sasldata->ssf);
437 dprint (3, (debugfile, "SASL protection strength: %u\n", *sasldata->ssf));
438 /* Add SASL SSF to transport SSF */
439 conn->ssf += *sasldata->ssf;
441 sasl_getprop (saslconn, SASL_MAXOUTBUF,
442 (const void **) &sasldata->pbufsize);
444 sasl_getprop (saslconn, SASL_MAXOUTBUF, (void **) &sasldata->pbufsize);
447 (debugfile, "SASL protection buffer size: %u\n",
448 *sasldata->pbufsize));
450 /* clear input buffer */
451 sasldata->buf = NULL;
455 /* preserve old functions */
456 sasldata->sockdata = conn->sockdata;
457 sasldata->msasl_open = conn->conn_open;
458 sasldata->msasl_close = conn->conn_close;
459 sasldata->msasl_read = conn->conn_read;
460 sasldata->msasl_write = conn->conn_write;
462 /* and set up new functions */
463 conn->sockdata = sasldata;
464 conn->conn_open = mutt_sasl_conn_open;
465 conn->conn_close = mutt_sasl_conn_close;
466 conn->conn_read = mutt_sasl_conn_read;
467 conn->conn_write = mutt_sasl_conn_write;
470 /* mutt_sasl_cb_log: callback to log SASL messages */
471 static int mutt_sasl_cb_log (void *context, int priority, const char *message)
473 dprint (priority, (debugfile, "SASL: %s\n", message));
478 /* mutt_sasl_cb_authname: callback to retrieve authname or user (mutt
479 * doesn't distinguish, even if some SASL plugins do) from ACCOUNT */
480 static int mutt_sasl_cb_authname (void *context, int id, const char **result,
483 ACCOUNT *account = (ACCOUNT *) context;
490 return SASL_BADPARAM;
492 dprint (2, (debugfile, "mutt_sasl_cb_authname: getting %s for %s:%u\n",
493 id == SASL_CB_AUTHNAME ? "authname" : "user",
494 account->host, account->port));
496 if (mutt_account_getuser (account))
499 *result = account->user;
502 *len = mutt_strlen (*result);
507 static int mutt_sasl_cb_pass (sasl_conn_t * conn, void *context, int id,
508 sasl_secret_t ** psecret)
510 ACCOUNT *account = (ACCOUNT *) context;
513 if (!account || !psecret)
514 return SASL_BADPARAM;
516 dprint (2, (debugfile,
517 "mutt_sasl_cb_pass: getting password for %s@%s:%u\n",
518 account->user, account->host, account->port));
520 if (mutt_account_getpass (account))
523 len = mutt_strlen (account->pass);
525 *psecret = (sasl_secret_t *) safe_malloc (sizeof (sasl_secret_t) + len);
526 (*psecret)->len = len;
527 strcpy ((*psecret)->data, account->pass); /* __STRCPY_CHECKED__ */
532 /* mutt_sasl_conn_open: empty wrapper for underlying open function. We
533 * don't know in advance that a connection will use SASL, so we
534 * replace conn's methods with sasl methods when authentication
535 * is successful, using mutt_sasl_setup_conn */
536 static int mutt_sasl_conn_open (CONNECTION * conn)
541 sasldata = (SASL_DATA *) conn->sockdata;
542 conn->sockdata = sasldata->sockdata;
543 rc = (sasldata->msasl_open) (conn);
544 conn->sockdata = sasldata;
549 /* mutt_sasl_conn_close: calls underlying close function and disposes of
550 * the sasl_conn_t object, then restores connection to pre-sasl state */
551 static int mutt_sasl_conn_close (CONNECTION * conn)
556 sasldata = (SASL_DATA *) conn->sockdata;
558 /* restore connection's underlying methods */
559 conn->sockdata = sasldata->sockdata;
560 conn->conn_open = sasldata->msasl_open;
561 conn->conn_close = sasldata->msasl_close;
562 conn->conn_read = sasldata->msasl_read;
563 conn->conn_write = sasldata->msasl_write;
565 /* release sasl resources */
566 sasl_dispose (&sasldata->saslconn);
568 FREE (&sasldata->buf);
572 /* call underlying close */
573 rc = (conn->conn_close) (conn);
578 static int mutt_sasl_conn_read (CONNECTION * conn, char *buf, size_t len)
585 sasldata = (SASL_DATA *) conn->sockdata;
587 /* if we still have data in our read buffer, copy it into buf */
588 if (sasldata->blen > sasldata->bpos) {
589 olen = (sasldata->blen - sasldata->bpos > len) ? len :
590 sasldata->blen - sasldata->bpos;
592 memcpy (buf, sasldata->buf + sasldata->bpos, olen);
593 sasldata->bpos += olen;
598 conn->sockdata = sasldata->sockdata;
601 FREE (&sasldata->buf);
606 /* and decode the result, if necessary */
607 if (*sasldata->ssf) {
609 /* call the underlying read function to fill the buffer */
610 rc = (sasldata->msasl_read) (conn, buf, len);
614 rc = sasl_decode (sasldata->saslconn, buf, rc, &sasldata->buf,
617 dprint (1, (debugfile, "SASL decode failed: %s\n",
618 sasl_errstring (rc, NULL, NULL)));
622 while (!sasldata->blen);
624 olen = (sasldata->blen - sasldata->bpos > len) ? len :
625 sasldata->blen - sasldata->bpos;
627 memcpy (buf, sasldata->buf, olen);
628 sasldata->bpos += olen;
633 rc = (sasldata->msasl_read) (conn, buf, len);
636 conn->sockdata = sasldata;
641 static int mutt_sasl_conn_write (CONNECTION * conn, const char *buf,
652 unsigned int olen, plen;
654 sasldata = (SASL_DATA *) conn->sockdata;
655 conn->sockdata = sasldata->sockdata;
657 /* encode data, if necessary */
658 if (*sasldata->ssf) {
659 /* handle data larger than MAXOUTBUF */
661 olen = (len > *sasldata->pbufsize) ? *sasldata->pbufsize : len;
663 rc = sasl_encode (sasldata->saslconn, buf, olen, &pbuf, &plen);
665 dprint (1, (debugfile, "SASL encoding failed: %s\n",
666 sasl_errstring (rc, NULL, NULL)));
670 rc = (sasldata->msasl_write) (conn, pbuf, plen);
680 while (len > *sasldata->pbufsize);
683 /* just write using the underlying socket function */
684 rc = (sasldata->msasl_write) (conn, buf, len);
686 conn->sockdata = sasldata;
691 conn->sockdata = sasldata;