/* * Send data from buffer and receive answer to the same buffer * 0 - successful, * -1 - conection lost, * -2 - invalid command or execution error. */ int pop_query_d (POP_DATA *pop_data, char *buf, size_t buflen, char *msg) { int dbg = M_SOCK_LOG_CMD; char *c; if (pop_data->status != POP_CONNECTED) return -1; #ifdef DEBUG /* print msg instaed of real command */ if (msg) { dbg = M_SOCK_LOG_FULL; dprint (M_SOCK_LOG_CMD, (debugfile, "> %s", msg)); } #endif mutt_socket_write_d (pop_data->conn, buf, -1, dbg); c = strpbrk (buf, " \r\n"); *c = '\0'; snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf); if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) { pop_data->status = POP_DISCONNECTED; return -1; } if (!mutt_strncmp (buf, "+OK", 3)) return 0; pop_error (pop_data, buf); return -2; }
/* * Open connection * 0 - successful, * -1 - conection lost, * -2 - invalid response. */ int pop_connect (POP_DATA *pop_data) { char buf[LONG_STRING]; pop_data->status = POP_NONE; if (mutt_socket_open (pop_data->conn) < 0 || mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) { mutt_error (_("Error connecting to server: %s"), pop_data->conn->account.host); return -1; } pop_data->status = POP_CONNECTED; if (mutt_strncmp (buf, "+OK", 3)) { *pop_data->err_msg = '\0'; pop_error (pop_data, buf); mutt_error ("%s", pop_data->err_msg); return -2; } pop_apop_timestamp (pop_data, buf); return 0; }
/* Reads a command response from the SMTP server. * Returns: * 0 on success (2xx code) or continue (354 code) * -1 write error, or any other response code */ static int smtp_get_resp (CONNECTION * conn) { int n; char buf[1024]; do { n = mutt_socket_readln (buf, sizeof (buf), conn); if (n < 4) { /* read error, or no response code */ return smtp_err_read; } if (!ascii_strncasecmp ("8BITMIME", buf + 4, 8)) mutt_bit_set (Capabilities, EIGHTBITMIME); else if (!ascii_strncasecmp ("AUTH ", buf + 4, 5)) { mutt_bit_set (Capabilities, AUTH); FREE (&AuthMechs); AuthMechs = safe_strdup (buf + 9); } else if (!ascii_strncasecmp ("DSN", buf + 4, 3)) mutt_bit_set (Capabilities, DSN); else if (!ascii_strncasecmp ("STARTTLS", buf + 4, 8)) mutt_bit_set (Capabilities, STARTTLS); if (smtp_code (buf, n, &n) < 0) return smtp_err_code; } while (buf[3] == '-'); if (smtp_success (n) || n == smtp_continue) return 0; mutt_error (_("SMTP session failed: %s"), buf); return -1; }
static int smtp_auth_sasl (CONNECTION* conn, const char* mechlist) { sasl_conn_t* saslconn; sasl_interact_t* interaction = NULL; const char* mech; const char* data = NULL; unsigned int len; char buf[HUGE_STRING]; int rc, saslrc; if (mutt_sasl_client_new (conn, &saslconn) < 0) return SMTP_AUTH_FAIL; do { rc = sasl_client_start (saslconn, mechlist, &interaction, &data, &len, &mech); if (rc == SASL_INTERACT) mutt_sasl_interact (interaction); } while (rc == SASL_INTERACT); if (rc != SASL_OK && rc != SASL_CONTINUE) { dprint (2, (debugfile, "smtp_auth_sasl: %s unavailable\n", mech)); sasl_dispose (&saslconn); return SMTP_AUTH_UNAVAIL; } if (!option(OPTNOCURSES)) mutt_message (_("Authenticating (%s)..."), mech); snprintf (buf, sizeof (buf), "AUTH %s", mech); if (len) { safe_strcat (buf, sizeof (buf), " "); if (sasl_encode64 (data, len, buf + mutt_strlen (buf), sizeof (buf) - mutt_strlen (buf), &len) != SASL_OK) { dprint (1, (debugfile, "smtp_auth_sasl: error base64-encoding client response.\n")); goto fail; } } safe_strcat (buf, sizeof (buf), "\r\n"); do { if (mutt_socket_write (conn, buf) < 0) goto fail; if ((rc = mutt_socket_readln (buf, sizeof (buf), conn)) < 0) goto fail; if (smtp_code (buf, rc, &rc) < 0) goto fail; if (rc != smtp_ready) break; if (sasl_decode64 (buf+4, strlen (buf+4), buf, sizeof (buf), &len) != SASL_OK) { dprint (1, (debugfile, "smtp_auth_sasl: error base64-decoding server response.\n")); goto fail; } do { saslrc = sasl_client_step (saslconn, buf, len, &interaction, &data, &len); if (saslrc == SASL_INTERACT) mutt_sasl_interact (interaction); } while (saslrc == SASL_INTERACT); if (len) { if (sasl_encode64 (data, len, buf, sizeof (buf), &len) != SASL_OK) { dprint (1, (debugfile, "smtp_auth_sasl: error base64-encoding client response.\n")); goto fail; } } strfcpy (buf + len, "\r\n", sizeof (buf) - len); } while (rc == smtp_ready && saslrc != SASL_FAIL); if (smtp_success (rc)) { mutt_sasl_setup_conn (conn, saslconn); return SMTP_AUTH_SUCCESS; } fail: sasl_dispose (&saslconn); return SMTP_AUTH_FAIL; }