2 * Copyright (C) 2000-7 Brendan Cully <brendan@kublai.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 /* common SASL helper routines */
27 #include "mutt_sasl.h"
28 #include "mutt_socket.h"
32 #include <sasl/sasl.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
36 static int getnameinfo_err(int ret)
39 dprint (1, (debugfile, "getnameinfo: "));
43 dprint (1, (debugfile, "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"));
55 dprint (1, (debugfile, "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"));
63 dprint (1, (debugfile, "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"));
64 err=SASL_FAIL; /* no real equivalent */
67 dprint (1, (debugfile, "A system error occurred. The error code can be found in errno(%d,%s)).\n",errno,strerror(errno)));
68 err=SASL_FAIL; /* no real equivalent */
71 dprint (1, (debugfile, "Unknown error %d\n",ret));
72 err=SASL_FAIL; /* no real equivalent */
78 /* arbitrary. SASL will probably use a smaller buffer anyway. OTOH it's
79 * been a while since I've had access to an SASL server which negotiated
80 * a protection buffer. */
81 #define M_SASL_MAXBUF 65536
83 #define IP_PORT_BUFLEN 1024
85 static sasl_callback_t mutt_sasl_callbacks[5];
87 static int mutt_sasl_start (void);
90 static int mutt_sasl_cb_log (void* context, int priority, const char* message);
91 static int mutt_sasl_cb_authname (void* context, int id, const char** result,
93 static int mutt_sasl_cb_pass (sasl_conn_t* conn, void* context, int id,
94 sasl_secret_t** psecret);
96 /* socket wrappers for a SASL security layer */
97 static int mutt_sasl_conn_open (CONNECTION* conn);
98 static int mutt_sasl_conn_close (CONNECTION* conn);
99 static int mutt_sasl_conn_read (CONNECTION* conn, char* buf, size_t len);
100 static int mutt_sasl_conn_write (CONNECTION* conn, const char* buf,
102 static int mutt_sasl_conn_poll (CONNECTION* conn);
104 /* utility function, stolen from sasl2 sample code */
105 static int iptostring(const struct sockaddr *addr, socklen_t addrlen,
106 char *out, unsigned outlen) {
107 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
110 if(!addr || !out) return SASL_BADPARAM;
112 ret=getnameinfo(addr, addrlen, hbuf, sizeof(hbuf), pbuf, sizeof(pbuf),
114 #ifdef NI_WITHSCOPEID
119 return getnameinfo_err(ret);
121 if(outlen < strlen(hbuf) + strlen(pbuf) + 2)
124 snprintf(out, outlen, "%s;%s", hbuf, pbuf);
129 /* mutt_sasl_start: called before doing a SASL exchange - initialises library
131 int mutt_sasl_start (void)
133 static unsigned char sasl_init = 0;
135 static sasl_callback_t callbacks[2];
141 /* set up default logging callback */
142 callbacks[0].id = SASL_CB_LOG;
143 callbacks[0].proc = mutt_sasl_cb_log;
144 callbacks[0].context = NULL;
146 callbacks[1].id = SASL_CB_LIST_END;
147 callbacks[1].proc = NULL;
148 callbacks[1].context = NULL;
150 rc = sasl_client_init (callbacks);
154 dprint (1, (debugfile, "mutt_sasl_start: libsasl initialisation failed.\n"));
163 /* mutt_sasl_client_new: wrapper for sasl_client_new which also sets various
164 * security properties. If this turns out to be fine for POP too we can
165 * probably stop exporting mutt_sasl_get_callbacks(). */
166 int mutt_sasl_client_new (CONNECTION* conn, sasl_conn_t** saslconn)
168 sasl_security_properties_t secprops;
169 struct sockaddr_storage local, remote;
171 char iplocalport[IP_PORT_BUFLEN], ipremoteport[IP_PORT_BUFLEN];
177 if (mutt_sasl_start () != SASL_OK)
180 switch (conn->account.type)
182 case M_ACCT_TYPE_IMAP:
185 case M_ACCT_TYPE_POP:
188 case M_ACCT_TYPE_SMTP:
192 mutt_error (_("Unknown SASL profile"));
196 size = sizeof (local);
197 if (!getsockname (conn->fd, (struct sockaddr *)&local, &size)) {
198 if (!iptostring((struct sockaddr *)&local, size, iplocalport,
199 IP_PORT_BUFLEN) != SASL_OK)
202 dprint (2, (debugfile, "SASL failed to parse local IP address\n"));
205 dprint (2, (debugfile, "SASL failed to get local IP address\n"));
207 size = sizeof (remote);
208 if (!getpeername (conn->fd, (struct sockaddr *)&remote, &size)){
209 if (!iptostring((struct sockaddr *)&remote, size, ipremoteport,
210 IP_PORT_BUFLEN) != SASL_OK)
213 dprint (2, (debugfile, "SASL failed to parse remote IP address\n"));
216 dprint (2, (debugfile, "SASL failed to get remote IP address\n"));
218 dprint (2, (debugfile, "SASL local ip: %s, remote ip:%s\n", NONULL(plp),
221 rc = sasl_client_new (service, conn->account.host, plp, prp,
222 mutt_sasl_get_callbacks (&conn->account), 0, saslconn);
226 mutt_error (_("Error allocating SASL connection"));
230 memset (&secprops, 0, sizeof (secprops));
231 /* Work around a casting bug in the SASL krb4 module */
232 secprops.max_ssf = 0x7fff;
233 secprops.maxbufsize = M_SASL_MAXBUF;
234 if (sasl_setprop (*saslconn, SASL_SEC_PROPS, &secprops) != SASL_OK)
236 mutt_error (_("Error setting SASL security properties"));
242 /* I'm not sure this actually has an effect, at least with SASLv2 */
243 dprint (2, (debugfile, "External SSF: %d\n", conn->ssf));
244 if (sasl_setprop (*saslconn, SASL_SSF_EXTERNAL, &(conn->ssf)) != SASL_OK)
246 mutt_error (_("Error setting SASL external security strength"));
249 dprint (2, (debugfile, "External authentication name: %s\n", conn->account.user));
250 if (sasl_setprop (*saslconn, SASL_AUTH_EXTERNAL, conn->account.user) != SASL_OK)
252 mutt_error (_("Error setting SASL external user name"));
260 sasl_callback_t* mutt_sasl_get_callbacks (ACCOUNT* account)
262 sasl_callback_t* callback;
264 callback = mutt_sasl_callbacks;
266 callback->id = SASL_CB_USER;
267 callback->proc = mutt_sasl_cb_authname;
268 callback->context = account;
271 callback->id = SASL_CB_AUTHNAME;
272 callback->proc = mutt_sasl_cb_authname;
273 callback->context = account;
276 callback->id = SASL_CB_PASS;
277 callback->proc = mutt_sasl_cb_pass;
278 callback->context = account;
281 callback->id = SASL_CB_GETREALM;
282 callback->proc = NULL;
283 callback->context = NULL;
286 callback->id = SASL_CB_LIST_END;
287 callback->proc = NULL;
288 callback->context = NULL;
290 return mutt_sasl_callbacks;
293 int mutt_sasl_interact (sasl_interact_t* interaction)
295 char prompt[SHORT_STRING];
296 char resp[SHORT_STRING];
298 while (interaction->id != SASL_CB_LIST_END)
300 dprint (2, (debugfile, "mutt_sasl_interact: filling in SASL interaction %ld.\n", interaction->id));
302 snprintf (prompt, sizeof (prompt), "%s: ", interaction->prompt);
304 if (mutt_get_field (prompt, resp, sizeof (resp), 0))
307 interaction->len = mutt_strlen (resp)+1;
308 interaction->result = safe_malloc (interaction->len);
309 memcpy ((char *)interaction->result, resp, interaction->len);
317 /* SASL can stack a protection layer on top of an existing connection.
318 * To handle this, we store a saslconn_t in conn->sockdata, and write
319 * wrappers which en/decode the read/write stream, then replace sockdata
320 * with an embedded copy of the old sockdata and call the underlying
321 * functions (which we've also preserved). I thought about trying to make
322 * a general stackable connection system, but it seemed like overkill -
323 * something is wrong if we have 15 filters on top of a socket. Anyway,
324 * anything else which wishes to stack can use the same method. The only
325 * disadvantage is we have to write wrappers for all the socket methods,
326 * even if we only stack over read and write. Thinking about it, the
327 * abstraction problem is that there is more in CONNECTION than there
328 * needs to be. Ideally it would have only (void*)data and methods. */
330 /* mutt_sasl_setup_conn: replace connection methods, sockdata with
331 * SASL wrappers, for protection layers. Also get ssf, as a fastpath
332 * for the read/write methods. */
333 void mutt_sasl_setup_conn (CONNECTION* conn, sasl_conn_t* saslconn)
335 SASL_DATA* sasldata = (SASL_DATA*) safe_malloc (sizeof (SASL_DATA));
337 sasldata->saslconn = saslconn;
338 /* get ssf so we know whether we have to (en|de)code read/write */
339 sasl_getprop (saslconn, SASL_SSF, (const void**) &sasldata->ssf);
340 dprint (3, (debugfile, "SASL protection strength: %u\n", *sasldata->ssf));
341 /* Add SASL SSF to transport SSF */
342 conn->ssf += *sasldata->ssf;
343 sasl_getprop (saslconn, SASL_MAXOUTBUF, (const void**) &sasldata->pbufsize);
344 dprint (3, (debugfile, "SASL protection buffer size: %u\n", *sasldata->pbufsize));
346 /* clear input buffer */
347 sasldata->buf = NULL;
351 /* preserve old functions */
352 sasldata->sockdata = conn->sockdata;
353 sasldata->msasl_open = conn->conn_open;
354 sasldata->msasl_close = conn->conn_close;
355 sasldata->msasl_read = conn->conn_read;
356 sasldata->msasl_write = conn->conn_write;
357 sasldata->msasl_poll = conn->conn_poll;
359 /* and set up new functions */
360 conn->sockdata = sasldata;
361 conn->conn_open = mutt_sasl_conn_open;
362 conn->conn_close = mutt_sasl_conn_close;
363 conn->conn_read = mutt_sasl_conn_read;
364 conn->conn_write = mutt_sasl_conn_write;
365 conn->conn_poll = mutt_sasl_conn_poll;
368 /* mutt_sasl_cb_log: callback to log SASL messages */
369 static int mutt_sasl_cb_log (void* context, int priority, const char* message)
371 dprint (priority, (debugfile, "SASL: %s\n", message));
376 void mutt_sasl_done (void)
381 /* mutt_sasl_cb_authname: callback to retrieve authname or user from ACCOUNT */
382 static int mutt_sasl_cb_authname (void* context, int id, const char** result,
385 ACCOUNT* account = (ACCOUNT*) context;
392 return SASL_BADPARAM;
394 dprint (2, (debugfile, "mutt_sasl_cb_authname: getting %s for %s:%u\n",
395 id == SASL_CB_AUTHNAME ? "authname" : "user",
396 account->host, account->port));
398 if (id == SASL_CB_AUTHNAME)
400 if (mutt_account_getlogin (account))
402 *result = account->login;
406 if (mutt_account_getuser (account))
408 *result = account->user;
412 *len = strlen (*result);
417 static int mutt_sasl_cb_pass (sasl_conn_t* conn, void* context, int id,
418 sasl_secret_t** psecret)
420 ACCOUNT* account = (ACCOUNT*) context;
423 if (!account || !psecret)
424 return SASL_BADPARAM;
426 dprint (2, (debugfile,
427 "mutt_sasl_cb_pass: getting password for %s@%s:%u\n", account->login,
428 account->host, account->port));
430 if (mutt_account_getpass (account))
433 len = strlen (account->pass);
435 *psecret = (sasl_secret_t*) safe_malloc (sizeof (sasl_secret_t) + len);
436 (*psecret)->len = len;
437 strcpy ((char*)(*psecret)->data, account->pass); /* __STRCPY_CHECKED__ */
442 /* mutt_sasl_conn_open: empty wrapper for underlying open function. We
443 * don't know in advance that a connection will use SASL, so we
444 * replace conn's methods with sasl methods when authentication
445 * is successful, using mutt_sasl_setup_conn */
446 static int mutt_sasl_conn_open (CONNECTION* conn)
451 sasldata = (SASL_DATA*) conn->sockdata;
452 conn->sockdata = sasldata->sockdata;
453 rc = (sasldata->msasl_open) (conn);
454 conn->sockdata = sasldata;
459 /* mutt_sasl_conn_close: calls underlying close function and disposes of
460 * the sasl_conn_t object, then restores connection to pre-sasl state */
461 static int mutt_sasl_conn_close (CONNECTION* conn)
466 sasldata = (SASL_DATA*) conn->sockdata;
468 /* restore connection's underlying methods */
469 conn->sockdata = sasldata->sockdata;
470 conn->conn_open = sasldata->msasl_open;
471 conn->conn_close = sasldata->msasl_close;
472 conn->conn_read = sasldata->msasl_read;
473 conn->conn_write = sasldata->msasl_write;
475 /* release sasl resources */
476 sasl_dispose (&sasldata->saslconn);
479 /* call underlying close */
480 rc = (conn->conn_close) (conn);
485 static int mutt_sasl_conn_read (CONNECTION* conn, char* buf, size_t len)
492 sasldata = (SASL_DATA*) conn->sockdata;
494 /* if we still have data in our read buffer, copy it into buf */
495 if (sasldata->blen > sasldata->bpos)
497 olen = (sasldata->blen - sasldata->bpos > len) ? len :
498 sasldata->blen - sasldata->bpos;
500 memcpy (buf, sasldata->buf+sasldata->bpos, olen);
501 sasldata->bpos += olen;
506 conn->sockdata = sasldata->sockdata;
511 /* and decode the result, if necessary */
516 /* call the underlying read function to fill the buffer */
517 rc = (sasldata->msasl_read) (conn, buf, len);
521 rc = sasl_decode (sasldata->saslconn, buf, rc, &sasldata->buf,
525 dprint (1, (debugfile, "SASL decode failed: %s\n",
526 sasl_errstring (rc, NULL, NULL)));
530 while (!sasldata->blen);
532 olen = (sasldata->blen - sasldata->bpos > len) ? len :
533 sasldata->blen - sasldata->bpos;
535 memcpy (buf, sasldata->buf, olen);
536 sasldata->bpos += olen;
541 rc = (sasldata->msasl_read) (conn, buf, len);
544 conn->sockdata = sasldata;
549 static int mutt_sasl_conn_write (CONNECTION* conn, const char* buf,
556 unsigned int olen, plen;
558 sasldata = (SASL_DATA*) conn->sockdata;
559 conn->sockdata = sasldata->sockdata;
561 /* encode data, if necessary */
564 /* handle data larger than MAXOUTBUF */
567 olen = (len > *sasldata->pbufsize) ? *sasldata->pbufsize : len;
569 rc = sasl_encode (sasldata->saslconn, buf, olen, &pbuf, &plen);
572 dprint (1, (debugfile, "SASL encoding failed: %s\n",
573 sasl_errstring (rc, NULL, NULL)));
577 rc = (sasldata->msasl_write) (conn, pbuf, plen);
584 while (len > *sasldata->pbufsize);
587 /* just write using the underlying socket function */
588 rc = (sasldata->msasl_write) (conn, buf, len);
590 conn->sockdata = sasldata;
595 conn->sockdata = sasldata;
599 static int mutt_sasl_conn_poll (CONNECTION* conn)
601 SASL_DATA* sasldata = conn->sockdata;
604 conn->sockdata = sasldata->sockdata;
605 rc = sasldata->msasl_poll (conn);
606 conn->sockdata = sasldata;