add -lpthread in the MUTTLIBS
[apps/madmutt.git] / mutt_sasl.c
index 374082b..8938afa 100644 (file)
@@ -16,7 +16,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 
-#include <lib-ui/curses.h>
+#include <lib-ui/lib-ui.h>
 #include <lib-sys/mutt_socket.h>
 
 #include "mutt.h"
  * a protection buffer. */
 #define M_SASL_MAXBUF 65536
 
-#define IP_PORT_BUFLEN 1024
+typedef struct {
+    sasl_conn_t *saslconn;
+    const sasl_ssf_t *ssf;
+    const unsigned int *pbufsize;
+
+    /* read buffer */
+    char *buf;
+    unsigned blen;
+    unsigned int bpos;
+
+    /* underlying socket data */
+    void *sockdata;
+    int (*msasl_open) (CONNECTION * conn);
+    int (*msasl_close) (CONNECTION * conn);
+    int (*msasl_read) (CONNECTION * conn, char *buf, ssize_t len);
+    int (*msasl_write) (CONNECTION * conn, const char *buf, ssize_t count);
+} SASL_DATA;
 
 static sasl_callback_t mutt_sasl_callbacks[5];
 
@@ -115,7 +131,7 @@ int mutt_sasl_client_new (CONNECTION * conn, sasl_conn_t ** saslconn)
 
     struct sockaddr_storage local, remote;
     socklen_t size;
-    char iplocalport[IP_PORT_BUFLEN], ipremoteport[IP_PORT_BUFLEN];
+    char iplocalport[STRING], ipremoteport[STRING];
     const char *service;
     int rc;
 
@@ -137,14 +153,14 @@ int mutt_sasl_client_new (CONNECTION * conn, sasl_conn_t ** saslconn)
 
     size = sizeof(local);
     if (getsockname(conn->fd, (struct sockaddr *) &local, &size)
-    ||  iptostring(&local, iplocalport, IP_PORT_BUFLEN) != SASL_OK)
+    ||  iptostring(&local, iplocalport, STRING) != SASL_OK)
     {
         return -1;
     }
 
     size = sizeof(remote);
     if (getpeername(conn->fd, (struct sockaddr *) &remote, &size)
-    ||  iptostring(&remote, ipremoteport, IP_PORT_BUFLEN) != SASL_OK)
+    ||  iptostring(&remote, ipremoteport, STRING) != SASL_OK)
     {
         return -1;
     }
@@ -186,8 +202,8 @@ int mutt_sasl_client_new (CONNECTION * conn, sasl_conn_t ** saslconn)
 
 int mutt_sasl_interact (sasl_interact_t * interaction)
 {
-    char prompt[SHORT_STRING];
-    char resp[SHORT_STRING];
+    char prompt[STRING];
+    char resp[STRING];
 
     while (interaction->id != SASL_CB_LIST_END) {
         snprintf (prompt, sizeof (prompt), "%s: ", interaction->prompt);
@@ -252,8 +268,8 @@ void mutt_sasl_setup_conn (CONNECTION * conn, sasl_conn_t * saslconn)
     conn->conn_write = mutt_sasl_conn_write;
 }
 
-void mutt_sasl_done (void) {
-    sasl_done ();
+void mutt_sasl_shutdown(void) {
+    sasl_done();
 }
 
 /* mutt_sasl_cb_authname: callback to retrieve authname or user from ACCOUNT */
@@ -285,7 +301,8 @@ static int mutt_sasl_cb_authname (void *context, int id, const char **result,
     return SASL_OK;
 }
 
-static int mutt_sasl_cb_pass(sasl_conn_t *conn, void *context, int id,
+static int mutt_sasl_cb_pass(sasl_conn_t *conn __attribute__ ((unused)),
+                             void *context, int id __attribute__ ((unused)),
                              sasl_secret_t **psecret)
 {
     ACCOUNT *account = (ACCOUNT *) context;
@@ -301,7 +318,7 @@ static int mutt_sasl_cb_pass(sasl_conn_t *conn, void *context, int id,
 
     *psecret = xmalloc(sizeof(sasl_secret_t) + len);
     (*psecret)->len = len;
-    strcpy((char*)(*psecret)->data, account->pass);
+    memcpy((char*)(*psecret)->data, account->pass, len);
 
     return SASL_OK;
 }
@@ -433,9 +450,9 @@ mutt_sasl_conn_write(CONNECTION * conn, const char *buf, ssize_t len)
                 goto fail;
             }
 
-            rc = (sasldata->msasl_write)(conn, pbuf, plen);
+            plen -= (sasldata->msasl_write)(conn, pbuf, plen);
             p_delete(&pbuf);
-            if (rc != plen)
+            if (plen)
                 goto fail;
 
             len -= olen;