2009-12-13 05:54:01 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2009-12-13 05:54:01 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2009-12-13 05:54:01 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
2013-02-08 04:02:06 +08:00
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2013-01-07 15:35:49 +08:00
|
|
|
* RFC2195 CRAM-MD5 authentication
|
2013-01-08 02:07:04 +08:00
|
|
|
* RFC2595 Using TLS with IMAP, POP3 and ACAP
|
2013-01-07 19:01:05 +08:00
|
|
|
* RFC2831 DIGEST-MD5 authentication
|
2009-12-13 05:54:01 +08:00
|
|
|
* RFC3501 IMAPv4 protocol
|
2013-01-23 02:37:12 +08:00
|
|
|
* RFC4422 Simple Authentication and Security Layer (SASL)
|
2013-01-07 06:25:14 +08:00
|
|
|
* RFC4616 PLAIN authentication
|
2014-08-10 06:38:08 +08:00
|
|
|
* RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
|
2013-02-08 04:02:06 +08:00
|
|
|
* RFC4959 IMAP Extension for SASL Initial Client Response
|
2009-12-13 05:54:01 +08:00
|
|
|
* RFC5092 IMAP URL Scheme
|
2013-08-26 01:17:35 +08:00
|
|
|
* RFC6749 OAuth 2.0 Authorization Framework
|
2019-04-11 08:19:15 +08:00
|
|
|
* RFC8314 Use of TLS for Email Submission and Access
|
2013-10-26 21:35:27 +08:00
|
|
|
* Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
|
2009-12-13 05:54:01 +08:00
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-07 02:06:49 +08:00
|
|
|
#include "curl_setup.h"
|
2009-12-13 05:54:01 +08:00
|
|
|
|
|
|
|
#ifndef CURL_DISABLE_IMAP
|
|
|
|
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UTSNAME_H
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
2009-12-31 01:59:56 +08:00
|
|
|
#ifdef __VMS
|
2009-12-13 05:54:01 +08:00
|
|
|
#include <in.h>
|
|
|
|
#include <inet.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "hostip.h"
|
|
|
|
#include "progress.h"
|
|
|
|
#include "transfer.h"
|
|
|
|
#include "escape.h"
|
|
|
|
#include "http.h" /* for HTTP proxy tunnel stuff */
|
|
|
|
#include "socks.h"
|
|
|
|
#include "imap.h"
|
2017-09-03 00:47:10 +08:00
|
|
|
#include "mime.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "strtoofft.h"
|
2016-09-30 23:15:05 +08:00
|
|
|
#include "strcase.h"
|
2013-12-18 06:32:47 +08:00
|
|
|
#include "vtls/vtls.h"
|
2022-11-11 18:45:34 +08:00
|
|
|
#include "cfilters.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "connect.h"
|
|
|
|
#include "select.h"
|
|
|
|
#include "multiif.h"
|
|
|
|
#include "url.h"
|
2021-10-03 00:05:26 +08:00
|
|
|
#include "bufref.h"
|
2013-01-07 03:13:58 +08:00
|
|
|
#include "curl_sasl.h"
|
2013-07-23 21:59:07 +08:00
|
|
|
#include "warnless.h"
|
2022-08-10 15:35:38 +08:00
|
|
|
#include "curl_ctype.h"
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2016-04-29 21:46:40 +08:00
|
|
|
/* The last 3 #include files should be in this order */
|
|
|
|
#include "curl_printf.h"
|
2009-12-13 05:54:01 +08:00
|
|
|
#include "curl_memory.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "memdebug.h"
|
2009-12-13 05:54:01 +08:00
|
|
|
|
|
|
|
/* Local API functions */
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_regular_transfer(struct Curl_easy *data, bool *done);
|
|
|
|
static CURLcode imap_do(struct Curl_easy *data, bool *done);
|
|
|
|
static CURLcode imap_done(struct Curl_easy *data, CURLcode status,
|
2012-12-29 05:24:36 +08:00
|
|
|
bool premature);
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_connect(struct Curl_easy *data, bool *done);
|
|
|
|
static CURLcode imap_disconnect(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn, bool dead);
|
|
|
|
static CURLcode imap_multi_statemach(struct Curl_easy *data, bool *done);
|
|
|
|
static int imap_getsock(struct Curl_easy *data, struct connectdata *conn,
|
|
|
|
curl_socket_t *socks);
|
|
|
|
static CURLcode imap_doing(struct Curl_easy *data, bool *dophase_done);
|
|
|
|
static CURLcode imap_setup_connection(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn);
|
2015-11-07 19:20:34 +08:00
|
|
|
static char *imap_atom(const char *str, bool escape_only);
|
2021-10-03 00:05:26 +08:00
|
|
|
static CURLcode imap_sendf(struct Curl_easy *data, const char *fmt, ...);
|
2013-04-13 22:58:15 +08:00
|
|
|
static CURLcode imap_parse_url_options(struct connectdata *conn);
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_parse_url_path(struct Curl_easy *data);
|
|
|
|
static CURLcode imap_parse_custom_request(struct Curl_easy *data);
|
|
|
|
static CURLcode imap_perform_authenticate(struct Curl_easy *data,
|
2015-01-21 00:33:05 +08:00
|
|
|
const char *mech,
|
2021-10-03 00:05:26 +08:00
|
|
|
const struct bufref *initresp);
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_continue_authenticate(struct Curl_easy *data,
|
2021-10-03 00:05:26 +08:00
|
|
|
const char *mech,
|
|
|
|
const struct bufref *resp);
|
|
|
|
static CURLcode imap_cancel_authenticate(struct Curl_easy *data,
|
|
|
|
const char *mech);
|
|
|
|
static CURLcode imap_get_message(struct Curl_easy *data, struct bufref *out);
|
2009-12-13 05:54:01 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* IMAP protocol handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_imap = {
|
|
|
|
"IMAP", /* scheme */
|
|
|
|
imap_setup_connection, /* setup_connection */
|
|
|
|
imap_do, /* do_it */
|
|
|
|
imap_done, /* done */
|
|
|
|
ZERO_NULL, /* do_more */
|
|
|
|
imap_connect, /* connect_it */
|
|
|
|
imap_multi_statemach, /* connecting */
|
|
|
|
imap_doing, /* doing */
|
|
|
|
imap_getsock, /* proto_getsock */
|
|
|
|
imap_getsock, /* doing_getsock */
|
2011-10-22 05:36:54 +08:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2009-12-13 05:54:01 +08:00
|
|
|
ZERO_NULL, /* perform_getsock */
|
|
|
|
imap_disconnect, /* disconnect */
|
2011-05-05 22:27:03 +08:00
|
|
|
ZERO_NULL, /* readwrite */
|
2017-05-31 19:09:56 +08:00
|
|
|
ZERO_NULL, /* connection_check */
|
2021-05-17 14:54:00 +08:00
|
|
|
ZERO_NULL, /* attach connection */
|
2009-12-13 05:54:01 +08:00
|
|
|
PORT_IMAP, /* defport */
|
2011-03-15 05:52:14 +08:00
|
|
|
CURLPROTO_IMAP, /* protocol */
|
2020-09-21 19:45:24 +08:00
|
|
|
CURLPROTO_IMAP, /* family */
|
2017-02-10 17:50:19 +08:00
|
|
|
PROTOPT_CLOSEACTION| /* flags */
|
|
|
|
PROTOPT_URLOPTIONS
|
2009-12-13 05:54:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef USE_SSL
|
|
|
|
/*
|
|
|
|
* IMAPS protocol handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_imaps = {
|
|
|
|
"IMAPS", /* scheme */
|
|
|
|
imap_setup_connection, /* setup_connection */
|
|
|
|
imap_do, /* do_it */
|
|
|
|
imap_done, /* done */
|
|
|
|
ZERO_NULL, /* do_more */
|
|
|
|
imap_connect, /* connect_it */
|
|
|
|
imap_multi_statemach, /* connecting */
|
|
|
|
imap_doing, /* doing */
|
|
|
|
imap_getsock, /* proto_getsock */
|
|
|
|
imap_getsock, /* doing_getsock */
|
2011-10-22 05:36:54 +08:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2009-12-13 05:54:01 +08:00
|
|
|
ZERO_NULL, /* perform_getsock */
|
|
|
|
imap_disconnect, /* disconnect */
|
2011-05-05 22:27:03 +08:00
|
|
|
ZERO_NULL, /* readwrite */
|
2017-05-31 19:09:56 +08:00
|
|
|
ZERO_NULL, /* connection_check */
|
2021-05-17 14:54:00 +08:00
|
|
|
ZERO_NULL, /* attach connection */
|
2009-12-13 05:54:01 +08:00
|
|
|
PORT_IMAPS, /* defport */
|
2014-04-21 01:37:54 +08:00
|
|
|
CURLPROTO_IMAPS, /* protocol */
|
2020-09-21 19:45:24 +08:00
|
|
|
CURLPROTO_IMAP, /* family */
|
2018-08-05 17:51:07 +08:00
|
|
|
PROTOPT_CLOSEACTION | PROTOPT_SSL | /* flags */
|
|
|
|
PROTOPT_URLOPTIONS
|
2009-12-13 05:54:01 +08:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2017-08-24 19:39:07 +08:00
|
|
|
#define IMAP_RESP_OK 1
|
|
|
|
#define IMAP_RESP_NOT_OK 2
|
|
|
|
#define IMAP_RESP_PREAUTH 3
|
|
|
|
|
2015-01-21 00:33:05 +08:00
|
|
|
/* SASL parameters for the imap protocol */
|
|
|
|
static const struct SASLproto saslimap = {
|
|
|
|
"imap", /* The service name */
|
|
|
|
imap_perform_authenticate, /* Send authentication command */
|
|
|
|
imap_continue_authenticate, /* Send authentication continuation */
|
2021-10-03 00:05:26 +08:00
|
|
|
imap_cancel_authenticate, /* Send authentication cancellation */
|
|
|
|
imap_get_message, /* Get SASL response message */
|
|
|
|
0, /* No maximum initial response length */
|
|
|
|
'+', /* Code received when continuation is expected */
|
|
|
|
IMAP_RESP_OK, /* Code to receive upon authentication success */
|
|
|
|
SASL_AUTH_DEFAULT, /* Default mechanisms */
|
|
|
|
SASL_FLAG_BASE64 /* Configuration flags */
|
2015-01-21 00:33:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-02-12 07:13:50 +08:00
|
|
|
#ifdef USE_SSL
|
|
|
|
static void imap_to_imaps(struct connectdata *conn)
|
|
|
|
{
|
2016-03-06 04:10:11 +08:00
|
|
|
/* Change the connection handler */
|
2013-02-12 07:13:50 +08:00
|
|
|
conn->handler = &Curl_handler_imaps;
|
2016-03-06 04:10:11 +08:00
|
|
|
|
|
|
|
/* Set the connection's upgraded to TLS flag */
|
2020-05-26 20:42:47 +08:00
|
|
|
conn->bits.tls_upgraded = TRUE;
|
2013-02-12 07:13:50 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define imap_to_imaps(x) Curl_nop_stmt
|
|
|
|
#endif
|
|
|
|
|
2013-03-10 20:56:45 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_matchresp()
|
|
|
|
*
|
|
|
|
* Determines whether the untagged response is related to the specified
|
|
|
|
* command by checking if it is in format "* <command-name> ..." or
|
|
|
|
* "* <number> <command-name> ...".
|
|
|
|
*
|
|
|
|
* The "* " marker is assumed to have already been checked by the caller.
|
|
|
|
*/
|
2013-02-27 01:52:19 +08:00
|
|
|
static bool imap_matchresp(const char *line, size_t len, const char *cmd)
|
|
|
|
{
|
|
|
|
const char *end = line + len;
|
|
|
|
size_t cmd_len = strlen(cmd);
|
|
|
|
|
|
|
|
/* Skip the untagged response marker */
|
|
|
|
line += 2;
|
|
|
|
|
|
|
|
/* Do we have a number after the marker? */
|
|
|
|
if(line < end && ISDIGIT(*line)) {
|
|
|
|
/* Skip the number */
|
|
|
|
do
|
|
|
|
line++;
|
|
|
|
while(line < end && ISDIGIT(*line));
|
|
|
|
|
|
|
|
/* Do we have the space character? */
|
|
|
|
if(line == end || *line != ' ')
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
line++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does the command name match and is it followed by a space character or at
|
|
|
|
the end of line? */
|
2016-10-01 00:54:02 +08:00
|
|
|
if(line + cmd_len <= end && strncasecompare(line, cmd, cmd_len) &&
|
2014-04-18 22:01:57 +08:00
|
|
|
(line[cmd_len] == ' ' || line + cmd_len + 2 == end))
|
2013-02-27 01:52:19 +08:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-03-10 20:56:45 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_endofresp()
|
|
|
|
*
|
|
|
|
* Checks whether the given string is a valid tagged, untagged or continuation
|
|
|
|
* response which can be processed by the response handler.
|
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static bool imap_endofresp(struct Curl_easy *data, struct connectdata *conn,
|
|
|
|
char *line, size_t len, int *resp)
|
2009-12-13 05:54:01 +08:00
|
|
|
{
|
2021-01-09 00:58:15 +08:00
|
|
|
struct IMAP *imap = data->req.p.imap;
|
2013-02-13 02:08:48 +08:00
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
2013-02-07 06:22:57 +08:00
|
|
|
const char *id = imapc->resptag;
|
2009-12-13 05:54:01 +08:00
|
|
|
size_t id_len = strlen(id);
|
|
|
|
|
2013-02-24 19:21:53 +08:00
|
|
|
/* Do we have a tagged command response? */
|
2013-02-24 19:48:43 +08:00
|
|
|
if(len >= id_len + 1 && !memcmp(id, line, id_len) && line[id_len] == ' ') {
|
2013-02-24 20:45:04 +08:00
|
|
|
line += id_len + 1;
|
|
|
|
len -= id_len + 1;
|
2013-02-24 19:48:43 +08:00
|
|
|
|
2013-02-24 20:45:04 +08:00
|
|
|
if(len >= 2 && !memcmp(line, "OK", 2))
|
2017-08-24 19:39:07 +08:00
|
|
|
*resp = IMAP_RESP_OK;
|
2017-08-23 06:08:18 +08:00
|
|
|
else if(len >= 7 && !memcmp(line, "PREAUTH", 7))
|
2017-08-24 19:39:07 +08:00
|
|
|
*resp = IMAP_RESP_PREAUTH;
|
|
|
|
else
|
|
|
|
*resp = IMAP_RESP_NOT_OK;
|
2013-02-13 07:00:34 +08:00
|
|
|
|
2013-02-24 19:23:06 +08:00
|
|
|
return TRUE;
|
2012-12-30 20:44:09 +08:00
|
|
|
}
|
|
|
|
|
2013-02-27 04:40:22 +08:00
|
|
|
/* Do we have an untagged command response? */
|
2013-02-24 20:27:49 +08:00
|
|
|
if(len >= 2 && !memcmp("* ", line, 2)) {
|
2013-02-27 01:10:54 +08:00
|
|
|
switch(imapc->state) {
|
|
|
|
/* States which are interested in untagged responses */
|
|
|
|
case IMAP_CAPABILITY:
|
2013-02-27 02:22:42 +08:00
|
|
|
if(!imap_matchresp(line, len, "CAPABILITY"))
|
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
|
2013-03-06 05:36:25 +08:00
|
|
|
case IMAP_LIST:
|
2013-03-09 21:26:59 +08:00
|
|
|
if((!imap->custom && !imap_matchresp(line, len, "LIST")) ||
|
|
|
|
(imap->custom && !imap_matchresp(line, len, imap->custom) &&
|
2017-11-09 07:26:21 +08:00
|
|
|
(!strcasecompare(imap->custom, "STORE") ||
|
2013-03-09 21:26:59 +08:00
|
|
|
!imap_matchresp(line, len, "FETCH")) &&
|
2017-11-09 07:26:21 +08:00
|
|
|
!strcasecompare(imap->custom, "SELECT") &&
|
|
|
|
!strcasecompare(imap->custom, "EXAMINE") &&
|
|
|
|
!strcasecompare(imap->custom, "SEARCH") &&
|
|
|
|
!strcasecompare(imap->custom, "EXPUNGE") &&
|
|
|
|
!strcasecompare(imap->custom, "LSUB") &&
|
|
|
|
!strcasecompare(imap->custom, "UID") &&
|
2021-04-27 09:52:54 +08:00
|
|
|
!strcasecompare(imap->custom, "GETQUOTAROOT") &&
|
2017-11-09 07:26:21 +08:00
|
|
|
!strcasecompare(imap->custom, "NOOP")))
|
2013-03-06 05:36:25 +08:00
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
|
2013-02-27 04:56:14 +08:00
|
|
|
case IMAP_SELECT:
|
2013-03-09 21:25:15 +08:00
|
|
|
/* SELECT is special in that its untagged responses do not have a
|
2013-02-27 04:56:14 +08:00
|
|
|
common prefix so accept anything! */
|
|
|
|
break;
|
|
|
|
|
2013-02-27 01:10:54 +08:00
|
|
|
case IMAP_FETCH:
|
2013-02-27 02:22:42 +08:00
|
|
|
if(!imap_matchresp(line, len, "FETCH"))
|
|
|
|
return FALSE;
|
2013-02-27 01:10:54 +08:00
|
|
|
break;
|
2013-02-27 04:37:08 +08:00
|
|
|
|
2014-04-18 23:42:45 +08:00
|
|
|
case IMAP_SEARCH:
|
|
|
|
if(!imap_matchresp(line, len, "SEARCH"))
|
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
|
2013-02-27 01:10:54 +08:00
|
|
|
/* Ignore other untagged responses */
|
|
|
|
default:
|
|
|
|
return FALSE;
|
2013-01-07 03:13:58 +08:00
|
|
|
}
|
2013-02-13 07:05:14 +08:00
|
|
|
|
2013-02-27 02:22:42 +08:00
|
|
|
*resp = '*';
|
2013-02-27 01:10:54 +08:00
|
|
|
return TRUE;
|
2009-12-13 05:54:01 +08:00
|
|
|
}
|
2012-12-29 05:24:36 +08:00
|
|
|
|
2013-09-25 03:49:19 +08:00
|
|
|
/* Do we have a continuation response? This should be a + symbol followed by
|
|
|
|
a space and optionally some text as per RFC-3501 for the AUTHENTICATE and
|
|
|
|
APPEND commands and as outlined in Section 4. Examples of RFC-4959 but
|
2021-12-17 18:02:18 +08:00
|
|
|
some email servers ignore this and only send a single + instead. */
|
2019-01-21 04:59:04 +08:00
|
|
|
if(imap && !imap->custom && ((len == 3 && line[0] == '+') ||
|
2015-10-13 21:55:05 +08:00
|
|
|
(len >= 2 && !memcmp("+ ", line, 2)))) {
|
2013-02-25 00:14:16 +08:00
|
|
|
switch(imapc->state) {
|
|
|
|
/* States which are interested in continuation responses */
|
2015-01-21 00:33:05 +08:00
|
|
|
case IMAP_AUTHENTICATE:
|
2013-03-02 03:06:18 +08:00
|
|
|
case IMAP_APPEND:
|
2013-02-25 00:14:16 +08:00
|
|
|
*resp = '+';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-01-09 00:58:15 +08:00
|
|
|
failf(data, "Unexpected continuation response");
|
2013-02-25 00:14:16 +08:00
|
|
|
*resp = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-01-07 03:13:58 +08:00
|
|
|
return FALSE; /* Nothing for us */
|
2009-12-13 05:54:01 +08:00
|
|
|
}
|
|
|
|
|
2013-10-27 17:00:41 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_get_message()
|
|
|
|
*
|
|
|
|
* Gets the authentication message from the response buffer.
|
|
|
|
*/
|
2021-10-03 00:05:26 +08:00
|
|
|
static CURLcode imap_get_message(struct Curl_easy *data, struct bufref *out)
|
2013-10-27 17:00:41 +08:00
|
|
|
{
|
2021-10-03 00:05:26 +08:00
|
|
|
char *message = data->state.buffer;
|
|
|
|
size_t len = strlen(message);
|
2013-10-27 17:00:41 +08:00
|
|
|
|
2017-12-01 07:45:46 +08:00
|
|
|
if(len > 2) {
|
|
|
|
/* Find the start of the message */
|
2018-01-13 13:35:12 +08:00
|
|
|
len -= 2;
|
2021-10-03 00:05:26 +08:00
|
|
|
for(message += 2; *message == ' ' || *message == '\t'; message++, len--)
|
2017-12-01 07:45:46 +08:00
|
|
|
;
|
2013-10-27 17:00:41 +08:00
|
|
|
|
2017-12-01 07:45:46 +08:00
|
|
|
/* Find the end of the message */
|
2021-10-03 00:05:26 +08:00
|
|
|
while(len--)
|
2017-12-01 07:45:46 +08:00
|
|
|
if(message[len] != '\r' && message[len] != '\n' && message[len] != ' ' &&
|
|
|
|
message[len] != '\t')
|
|
|
|
break;
|
2013-10-27 17:00:41 +08:00
|
|
|
|
2017-12-01 07:45:46 +08:00
|
|
|
/* Terminate the message */
|
2021-10-03 00:05:26 +08:00
|
|
|
message[++len] = '\0';
|
|
|
|
Curl_bufref_set(out, message, len, NULL);
|
2013-10-27 17:00:41 +08:00
|
|
|
}
|
2017-12-01 07:45:46 +08:00
|
|
|
else
|
|
|
|
/* junk input => zero length output */
|
2021-10-03 00:05:26 +08:00
|
|
|
Curl_bufref_set(out, "", 0, NULL);
|
2013-10-27 17:00:41 +08:00
|
|
|
|
2021-10-03 00:05:26 +08:00
|
|
|
return CURLE_OK;
|
2013-10-27 17:00:41 +08:00
|
|
|
}
|
|
|
|
|
2013-03-10 20:56:45 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* state()
|
|
|
|
*
|
|
|
|
* This is the ONLY way to change IMAP state!
|
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static void state(struct Curl_easy *data, imapstate newstate)
|
2009-12-13 05:54:01 +08:00
|
|
|
{
|
2021-01-09 00:58:15 +08:00
|
|
|
struct imap_conn *imapc = &data->conn->proto.imapc;
|
2009-12-13 05:54:01 +08:00
|
|
|
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
|
|
|
/* for debug purposes */
|
|
|
|
static const char * const names[]={
|
|
|
|
"STOP",
|
|
|
|
"SERVERGREET",
|
2013-02-11 05:18:30 +08:00
|
|
|
"CAPABILITY",
|
2009-12-13 05:54:01 +08:00
|
|
|
"STARTTLS",
|
2011-03-13 21:19:16 +08:00
|
|
|
"UPGRADETLS",
|
2015-01-21 00:33:05 +08:00
|
|
|
"AUTHENTICATE",
|
2012-12-30 20:44:09 +08:00
|
|
|
"LOGIN",
|
2013-03-06 05:34:53 +08:00
|
|
|
"LIST",
|
2009-12-13 05:54:01 +08:00
|
|
|
"SELECT",
|
|
|
|
"FETCH",
|
2013-03-01 02:16:33 +08:00
|
|
|
"FETCH_FINAL",
|
2013-03-02 03:04:56 +08:00
|
|
|
"APPEND",
|
|
|
|
"APPEND_FINAL",
|
2014-04-18 22:43:04 +08:00
|
|
|
"SEARCH",
|
2009-12-13 05:54:01 +08:00
|
|
|
"LOGOUT",
|
|
|
|
/* LAST */
|
|
|
|
};
|
2012-12-29 05:24:36 +08:00
|
|
|
|
2009-12-13 05:54:01 +08:00
|
|
|
if(imapc->state != newstate)
|
2021-07-06 23:05:17 +08:00
|
|
|
infof(data, "IMAP %p state change from %s to %s",
|
2013-07-23 21:59:07 +08:00
|
|
|
(void *)imapc, names[imapc->state], names[newstate]);
|
2009-12-13 05:54:01 +08:00
|
|
|
#endif
|
2012-12-29 05:24:36 +08:00
|
|
|
|
2009-12-13 05:54:01 +08:00
|
|
|
imapc->state = newstate;
|
|
|
|
}
|
|
|
|
|
2013-03-09 22:02:56 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2013-03-10 02:50:41 +08:00
|
|
|
* imap_perform_capability()
|
2013-03-09 22:02:56 +08:00
|
|
|
*
|
|
|
|
* Sends the CAPABILITY command in order to obtain a list of server side
|
|
|
|
* supported capabilities.
|
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_perform_capability(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn)
|
2013-01-07 03:13:58 +08:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
2015-01-20 21:14:26 +08:00
|
|
|
imapc->sasl.authmechs = SASL_AUTH_NONE; /* No known auth. mechanisms yet */
|
|
|
|
imapc->sasl.authused = SASL_AUTH_NONE; /* Clear the auth. mechanism used */
|
|
|
|
imapc->tls_supported = FALSE; /* Clear the TLS capability */
|
2013-01-07 03:13:58 +08:00
|
|
|
|
|
|
|
/* Send the CAPABILITY command */
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_sendf(data, "CAPABILITY");
|
2013-01-07 03:13:58 +08:00
|
|
|
|
2013-03-01 06:46:22 +08:00
|
|
|
if(!result)
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_CAPABILITY);
|
2013-01-07 03:13:58 +08:00
|
|
|
|
2013-03-01 07:09:28 +08:00
|
|
|
return result;
|
2013-01-07 03:13:58 +08:00
|
|
|
}
|
|
|
|
|
2013-03-09 22:02:56 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2013-03-10 02:50:41 +08:00
|
|
|
* imap_perform_starttls()
|
2013-03-09 22:02:56 +08:00
|
|
|
*
|
|
|
|
* Sends the STARTTLS command to start the upgrade to TLS.
|
|
|
|
*/
|
2021-10-03 00:05:26 +08:00
|
|
|
static CURLcode imap_perform_starttls(struct Curl_easy *data)
|
2013-02-12 06:41:08 +08:00
|
|
|
{
|
|
|
|
/* Send the STARTTLS command */
|
2021-10-03 00:05:26 +08:00
|
|
|
CURLcode result = imap_sendf(data, "STARTTLS");
|
2013-02-12 06:41:08 +08:00
|
|
|
|
|
|
|
if(!result)
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_STARTTLS);
|
2013-02-12 06:41:08 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-09 22:02:56 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2013-03-10 02:50:41 +08:00
|
|
|
* imap_perform_upgrade_tls()
|
2013-03-09 22:02:56 +08:00
|
|
|
*
|
|
|
|
* Performs the upgrade to TLS.
|
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_perform_upgrade_tls(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn)
|
2013-02-12 07:13:50 +08:00
|
|
|
{
|
2013-02-14 04:12:21 +08:00
|
|
|
/* Start the SSL connection */
|
2019-05-25 16:06:08 +08:00
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
2022-11-11 18:45:34 +08:00
|
|
|
CURLcode result;
|
2023-01-01 18:26:22 +08:00
|
|
|
bool ssldone = FALSE;
|
2013-02-12 07:13:50 +08:00
|
|
|
|
2022-12-30 16:14:55 +08:00
|
|
|
if(!Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
|
2022-11-26 19:43:56 +08:00
|
|
|
result = Curl_ssl_cfilter_add(data, conn, FIRSTSOCKET);
|
2022-11-11 18:45:34 +08:00
|
|
|
if(result)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2023-01-01 18:26:22 +08:00
|
|
|
result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone);
|
2013-02-12 07:13:50 +08:00
|
|
|
if(!result) {
|
2023-01-01 18:26:22 +08:00
|
|
|
imapc->ssldone = ssldone;
|
2013-02-12 07:13:50 +08:00
|
|
|
if(imapc->state != IMAP_UPGRADETLS)
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_UPGRADETLS);
|
2013-02-12 07:13:50 +08:00
|
|
|
|
|
|
|
if(imapc->ssldone) {
|
|
|
|
imap_to_imaps(conn);
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_perform_capability(data, conn);
|
2013-02-12 07:13:50 +08:00
|
|
|
}
|
|
|
|
}
|
2022-11-11 18:45:34 +08:00
|
|
|
out:
|
2013-02-12 07:13:50 +08:00
|
|
|
return result;
|
|
|
|
}
|
2013-02-12 06:41:08 +08:00
|
|
|
|
2013-03-09 22:02:56 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2013-03-10 02:50:41 +08:00
|
|
|
* imap_perform_login()
|
2013-03-09 22:02:56 +08:00
|
|
|
*
|
|
|
|
* Sends a clear text LOGIN command to authenticate with.
|
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_perform_login(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn)
|
2009-12-13 05:54:01 +08:00
|
|
|
{
|
2013-02-10 23:41:56 +08:00
|
|
|
CURLcode result = CURLE_OK;
|
2013-02-11 04:32:36 +08:00
|
|
|
char *user;
|
|
|
|
char *passwd;
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-02-10 23:41:56 +08:00
|
|
|
/* Check we have a username and password to authenticate with and end the
|
|
|
|
connect phase if we don't */
|
2022-02-15 00:33:50 +08:00
|
|
|
if(!data->state.aptr.user) {
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_STOP);
|
2013-02-10 23:41:56 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-02-11 04:32:36 +08:00
|
|
|
/* Make sure the username and password are in the correct atom format */
|
2015-11-07 19:20:34 +08:00
|
|
|
user = imap_atom(conn->user, false);
|
|
|
|
passwd = imap_atom(conn->passwd, false);
|
2013-02-11 04:32:36 +08:00
|
|
|
|
2013-02-24 04:02:05 +08:00
|
|
|
/* Send the LOGIN command */
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_sendf(data, "LOGIN %s %s", user ? user : "",
|
2013-02-07 08:18:23 +08:00
|
|
|
passwd ? passwd : "");
|
2012-12-28 22:49:30 +08:00
|
|
|
|
2015-03-16 22:01:15 +08:00
|
|
|
free(user);
|
|
|
|
free(passwd);
|
2012-12-28 22:49:30 +08:00
|
|
|
|
2013-03-01 06:46:22 +08:00
|
|
|
if(!result)
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_LOGIN);
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-03-01 07:09:28 +08:00
|
|
|
return result;
|
2009-12-13 05:54:01 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 15:00:00 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_perform_authenticate()
|
|
|
|
*
|
|
|
|
* Sends an AUTHENTICATE command allowing the client to login with the given
|
|
|
|
* SASL authentication mechanism.
|
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_perform_authenticate(struct Curl_easy *data,
|
2013-12-18 15:00:00 +08:00
|
|
|
const char *mech,
|
2021-10-03 00:05:26 +08:00
|
|
|
const struct bufref *initresp)
|
2013-12-18 15:00:00 +08:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2021-10-03 00:05:26 +08:00
|
|
|
const char *ir = (const char *) Curl_bufref_ptr(initresp);
|
2013-12-18 15:00:00 +08:00
|
|
|
|
2021-10-03 00:05:26 +08:00
|
|
|
if(ir) {
|
2013-12-18 15:00:00 +08:00
|
|
|
/* Send the AUTHENTICATE command with the initial response */
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_sendf(data, "AUTHENTICATE %s %s", mech, ir);
|
2013-12-18 15:00:00 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Send the AUTHENTICATE command */
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_sendf(data, "AUTHENTICATE %s", mech);
|
2013-12-18 15:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-01-21 00:33:05 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_continue_authenticate()
|
|
|
|
*
|
2021-10-03 00:05:26 +08:00
|
|
|
* Sends SASL continuation data.
|
2015-01-21 00:33:05 +08:00
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_continue_authenticate(struct Curl_easy *data,
|
2021-10-03 00:05:26 +08:00
|
|
|
const char *mech,
|
|
|
|
const struct bufref *resp)
|
2015-01-21 00:33:05 +08:00
|
|
|
{
|
2021-10-03 00:05:26 +08:00
|
|
|
struct imap_conn *imapc = &data->conn->proto.imapc;
|
|
|
|
|
|
|
|
(void)mech;
|
2015-01-21 00:33:05 +08:00
|
|
|
|
2021-10-03 00:05:26 +08:00
|
|
|
return Curl_pp_sendf(data, &imapc->pp,
|
|
|
|
"%s", (const char *) Curl_bufref_ptr(resp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_cancel_authenticate()
|
|
|
|
*
|
|
|
|
* Sends SASL cancellation.
|
|
|
|
*/
|
|
|
|
static CURLcode imap_cancel_authenticate(struct Curl_easy *data,
|
|
|
|
const char *mech)
|
|
|
|
{
|
|
|
|
struct imap_conn *imapc = &data->conn->proto.imapc;
|
|
|
|
|
|
|
|
(void)mech;
|
|
|
|
|
|
|
|
return Curl_pp_sendf(data, &imapc->pp, "*");
|
2015-01-21 00:33:05 +08:00
|
|
|
}
|
|
|
|
|
2013-03-09 22:02:56 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2013-12-18 06:48:09 +08:00
|
|
|
* imap_perform_authentication()
|
2013-03-09 22:02:56 +08:00
|
|
|
*
|
2013-12-18 06:48:09 +08:00
|
|
|
* Initiates the authentication sequence, with the appropriate SASL
|
|
|
|
* authentication mechanism, falling back to clear text should a common
|
|
|
|
* mechanism not be available between the client and server.
|
2013-03-09 22:02:56 +08:00
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_perform_authentication(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn)
|
2013-01-07 06:25:14 +08:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
2015-01-21 00:33:05 +08:00
|
|
|
saslprogress progress;
|
2013-01-07 06:25:14 +08:00
|
|
|
|
2017-08-23 06:08:18 +08:00
|
|
|
/* Check if already authenticated OR if there is enough data to authenticate
|
|
|
|
with and end the connect phase if we don't */
|
|
|
|
if(imapc->preauth ||
|
2022-02-15 00:33:50 +08:00
|
|
|
!Curl_sasl_can_authenticate(&imapc->sasl, data)) {
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_STOP);
|
2013-02-10 23:41:56 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-12-18 20:29:46 +08:00
|
|
|
/* Calculate the SASL login details */
|
2021-10-03 00:05:26 +08:00
|
|
|
result = Curl_sasl_start(&imapc->sasl, data, imapc->ir_supported, &progress);
|
2013-01-07 06:25:14 +08:00
|
|
|
|
2013-04-28 19:57:42 +08:00
|
|
|
if(!result) {
|
2015-01-21 00:33:05 +08:00
|
|
|
if(progress == SASL_INPROGRESS)
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_AUTHENTICATE);
|
2015-01-21 00:33:05 +08:00
|
|
|
else if(!imapc->login_disabled && (imapc->preftype & IMAP_TYPE_CLEARTEXT))
|
2013-04-28 19:57:42 +08:00
|
|
|
/* Perform clear text authentication */
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_perform_login(data, conn);
|
2013-02-08 05:06:53 +08:00
|
|
|
else {
|
2013-04-28 19:57:42 +08:00
|
|
|
/* Other mechanisms not supported */
|
2022-04-16 17:55:05 +08:00
|
|
|
infof(data, "No known authentication mechanisms supported");
|
2013-04-28 19:57:42 +08:00
|
|
|
result = CURLE_LOGIN_DENIED;
|
2013-02-08 05:06:53 +08:00
|
|
|
}
|
2013-02-04 07:58:03 +08:00
|
|
|
}
|
2013-01-07 06:25:14 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-09 22:02:56 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2013-03-10 02:50:41 +08:00
|
|
|
* imap_perform_list()
|
2013-03-09 22:02:56 +08:00
|
|
|
*
|
|
|
|
* Sends a LIST command or an alternative custom request.
|
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_perform_list(struct Curl_easy *data)
|
2013-03-06 05:36:25 +08:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2020-11-23 15:32:41 +08:00
|
|
|
struct IMAP *imap = data->req.p.imap;
|
2013-03-06 05:36:25 +08:00
|
|
|
|
2013-03-09 21:26:59 +08:00
|
|
|
if(imap->custom)
|
|
|
|
/* Send the custom request */
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_sendf(data, "%s%s", imap->custom,
|
2013-03-09 21:26:59 +08:00
|
|
|
imap->custom_params ? imap->custom_params : "");
|
|
|
|
else {
|
2015-11-07 05:49:26 +08:00
|
|
|
/* Make sure the mailbox is in the correct atom format if necessary */
|
2018-06-03 04:52:56 +08:00
|
|
|
char *mailbox = imap->mailbox ? imap_atom(imap->mailbox, true)
|
|
|
|
: strdup("");
|
2013-03-09 21:26:59 +08:00
|
|
|
if(!mailbox)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2013-03-06 05:36:25 +08:00
|
|
|
|
2013-03-09 21:26:59 +08:00
|
|
|
/* Send the LIST command */
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_sendf(data, "LIST \"%s\" *", mailbox);
|
2013-03-06 05:36:25 +08:00
|
|
|
|
2015-03-16 22:01:15 +08:00
|
|
|
free(mailbox);
|
2013-03-09 21:26:59 +08:00
|
|
|
}
|
2013-03-06 05:36:25 +08:00
|
|
|
|
|
|
|
if(!result)
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_LIST);
|
2013-03-06 05:36:25 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-09 22:02:56 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2013-03-10 02:50:41 +08:00
|
|
|
* imap_perform_select()
|
2013-03-09 22:02:56 +08:00
|
|
|
*
|
|
|
|
* Sends a SELECT command to ask the server to change the selected mailbox.
|
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_perform_select(struct Curl_easy *data)
|
2013-02-26 01:06:02 +08:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-09 00:58:15 +08:00
|
|
|
struct connectdata *conn = data->conn;
|
2020-11-23 15:32:41 +08:00
|
|
|
struct IMAP *imap = data->req.p.imap;
|
2013-02-27 04:56:14 +08:00
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
2013-02-26 01:06:02 +08:00
|
|
|
char *mailbox;
|
|
|
|
|
2013-02-28 02:02:53 +08:00
|
|
|
/* Invalidate old information as we are switching mailboxes */
|
|
|
|
Curl_safefree(imapc->mailbox);
|
2013-02-27 04:56:14 +08:00
|
|
|
Curl_safefree(imapc->mailbox_uidvalidity);
|
|
|
|
|
2013-03-06 03:52:11 +08:00
|
|
|
/* Check we have a mailbox */
|
|
|
|
if(!imap->mailbox) {
|
2021-01-09 00:58:15 +08:00
|
|
|
failf(data, "Cannot SELECT without a mailbox.");
|
2013-03-06 03:52:11 +08:00
|
|
|
return CURLE_URL_MALFORMAT;
|
|
|
|
}
|
|
|
|
|
2013-03-01 06:51:32 +08:00
|
|
|
/* Make sure the mailbox is in the correct atom format */
|
2015-11-07 19:20:34 +08:00
|
|
|
mailbox = imap_atom(imap->mailbox, false);
|
2013-02-26 01:06:02 +08:00
|
|
|
if(!mailbox)
|
2013-03-06 03:59:53 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* Send the SELECT command */
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_sendf(data, "SELECT %s", mailbox);
|
2013-02-26 01:06:02 +08:00
|
|
|
|
2015-03-16 22:01:15 +08:00
|
|
|
free(mailbox);
|
2013-03-01 06:51:32 +08:00
|
|
|
|
2013-03-01 06:46:22 +08:00
|
|
|
if(!result)
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_SELECT);
|
2013-02-26 01:06:02 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-09 22:02:56 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2013-03-10 02:50:41 +08:00
|
|
|
* imap_perform_fetch()
|
2013-03-09 22:02:56 +08:00
|
|
|
*
|
|
|
|
* Sends a FETCH command to initiate the download of a message.
|
|
|
|
*/
|
2021-10-03 00:05:26 +08:00
|
|
|
static CURLcode imap_perform_fetch(struct Curl_easy *data)
|
2013-02-26 01:06:02 +08:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-09 00:58:15 +08:00
|
|
|
struct IMAP *imap = data->req.p.imap;
|
2013-03-07 03:33:49 +08:00
|
|
|
/* Check we have a UID */
|
2018-07-31 19:12:18 +08:00
|
|
|
if(imap->uid) {
|
|
|
|
|
|
|
|
/* Send the FETCH command */
|
|
|
|
if(imap->partial)
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_sendf(data, "UID FETCH %s BODY[%s]<%s>",
|
2021-01-09 00:58:15 +08:00
|
|
|
imap->uid, imap->section ? imap->section : "",
|
|
|
|
imap->partial);
|
2018-07-31 19:12:18 +08:00
|
|
|
else
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_sendf(data, "UID FETCH %s BODY[%s]",
|
2021-01-09 00:58:15 +08:00
|
|
|
imap->uid, imap->section ? imap->section : "");
|
2018-07-31 19:12:18 +08:00
|
|
|
}
|
|
|
|
else if(imap->mindex) {
|
|
|
|
/* Send the FETCH command */
|
|
|
|
if(imap->partial)
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_sendf(data, "FETCH %s BODY[%s]<%s>",
|
2021-01-09 00:58:15 +08:00
|
|
|
imap->mindex, imap->section ? imap->section : "",
|
|
|
|
imap->partial);
|
2018-07-31 19:12:18 +08:00
|
|
|
else
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_sendf(data, "FETCH %s BODY[%s]",
|
2021-01-09 00:58:15 +08:00
|
|
|
imap->mindex, imap->section ? imap->section : "");
|
2018-07-31 19:12:18 +08:00
|
|
|
}
|
|
|
|
else {
|
2021-01-09 00:58:15 +08:00
|
|
|
failf(data, "Cannot FETCH without a UID.");
|
|
|
|
return CURLE_URL_MALFORMAT;
|
2013-03-07 03:33:49 +08:00
|
|
|
}
|
2013-03-01 06:46:22 +08:00
|
|
|
if(!result)
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_FETCH);
|
2013-02-26 01:06:02 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-09 22:02:56 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2013-03-10 02:50:41 +08:00
|
|
|
* imap_perform_append()
|
2013-03-09 22:02:56 +08:00
|
|
|
*
|
|
|
|
* Sends an APPEND command to initiate the upload of a message.
|
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_perform_append(struct Curl_easy *data)
|
2013-03-03 04:24:49 +08:00
|
|
|
{
|
2013-03-08 06:44:10 +08:00
|
|
|
CURLcode result = CURLE_OK;
|
2020-11-23 15:32:41 +08:00
|
|
|
struct IMAP *imap = data->req.p.imap;
|
2013-03-03 04:24:49 +08:00
|
|
|
char *mailbox;
|
|
|
|
|
2013-03-06 03:52:11 +08:00
|
|
|
/* Check we have a mailbox */
|
|
|
|
if(!imap->mailbox) {
|
2017-09-03 00:47:10 +08:00
|
|
|
failf(data, "Cannot APPEND without a mailbox.");
|
2013-03-06 03:52:11 +08:00
|
|
|
return CURLE_URL_MALFORMAT;
|
|
|
|
}
|
|
|
|
|
2017-09-03 00:47:10 +08:00
|
|
|
/* Prepare the mime data if some. */
|
|
|
|
if(data->set.mimepost.kind != MIMEKIND_NONE) {
|
|
|
|
/* Use the whole structure as data. */
|
|
|
|
data->set.mimepost.flags &= ~MIME_BODY_ONLY;
|
|
|
|
|
|
|
|
/* Add external headers and mime version. */
|
|
|
|
curl_mime_headers(&data->set.mimepost, data->set.headers, 0);
|
2022-11-17 00:40:11 +08:00
|
|
|
result = Curl_mime_prepare_headers(data, &data->set.mimepost, NULL,
|
2017-09-03 00:47:10 +08:00
|
|
|
NULL, MIMESTRATEGY_MAIL);
|
|
|
|
|
|
|
|
if(!result)
|
2022-02-09 07:57:00 +08:00
|
|
|
if(!Curl_checkheaders(data, STRCONST("Mime-Version")))
|
2017-09-03 00:47:10 +08:00
|
|
|
result = Curl_mime_add_header(&data->set.mimepost.curlheaders,
|
|
|
|
"Mime-Version: 1.0");
|
|
|
|
|
|
|
|
/* Make sure we will read the entire mime structure. */
|
|
|
|
if(!result)
|
|
|
|
result = Curl_mime_rewind(&data->set.mimepost);
|
|
|
|
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
data->state.infilesize = Curl_mime_size(&data->set.mimepost);
|
|
|
|
|
|
|
|
/* Read from mime structure. */
|
|
|
|
data->state.fread_func = (curl_read_callback) Curl_mime_read;
|
|
|
|
data->state.in = (void *) &data->set.mimepost;
|
|
|
|
}
|
|
|
|
|
2013-03-04 00:44:53 +08:00
|
|
|
/* Check we know the size of the upload */
|
2017-09-03 00:47:10 +08:00
|
|
|
if(data->state.infilesize < 0) {
|
2020-12-24 06:41:13 +08:00
|
|
|
failf(data, "Cannot APPEND with unknown input file size");
|
2013-03-03 04:24:49 +08:00
|
|
|
return CURLE_UPLOAD_FAILED;
|
|
|
|
}
|
|
|
|
|
2013-03-04 00:44:53 +08:00
|
|
|
/* Make sure the mailbox is in the correct atom format */
|
2015-11-07 19:20:34 +08:00
|
|
|
mailbox = imap_atom(imap->mailbox, false);
|
2013-03-03 04:24:49 +08:00
|
|
|
if(!mailbox)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2013-03-04 00:44:53 +08:00
|
|
|
/* Send the APPEND command */
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_sendf(data,
|
2021-01-09 00:58:15 +08:00
|
|
|
"APPEND %s (\\Seen) {%" CURL_FORMAT_CURL_OFF_T "}",
|
2017-09-03 00:47:10 +08:00
|
|
|
mailbox, data->state.infilesize);
|
2013-03-03 04:24:49 +08:00
|
|
|
|
2015-03-16 22:01:15 +08:00
|
|
|
free(mailbox);
|
2013-03-03 04:24:49 +08:00
|
|
|
|
|
|
|
if(!result)
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_APPEND);
|
2013-03-03 04:24:49 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-04-18 23:42:45 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_perform_search()
|
|
|
|
*
|
|
|
|
* Sends a SEARCH command.
|
|
|
|
*/
|
2021-10-03 00:05:26 +08:00
|
|
|
static CURLcode imap_perform_search(struct Curl_easy *data)
|
2014-04-18 23:42:45 +08:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-09 00:58:15 +08:00
|
|
|
struct IMAP *imap = data->req.p.imap;
|
2014-04-18 23:42:45 +08:00
|
|
|
|
|
|
|
/* Check we have a query string */
|
|
|
|
if(!imap->query) {
|
2021-01-09 00:58:15 +08:00
|
|
|
failf(data, "Cannot SEARCH without a query string.");
|
2014-04-18 23:42:45 +08:00
|
|
|
return CURLE_URL_MALFORMAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the SEARCH command */
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_sendf(data, "SEARCH %s", imap->query);
|
2014-04-18 23:42:45 +08:00
|
|
|
|
|
|
|
if(!result)
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_SEARCH);
|
2014-04-18 23:42:45 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-09 20:12:20 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2013-03-10 02:50:41 +08:00
|
|
|
* imap_perform_logout()
|
2013-03-09 20:12:20 +08:00
|
|
|
*
|
|
|
|
* Performs the logout action prior to sclose() being called.
|
|
|
|
*/
|
2021-10-03 00:05:26 +08:00
|
|
|
static CURLcode imap_perform_logout(struct Curl_easy *data)
|
2013-03-09 20:12:20 +08:00
|
|
|
{
|
|
|
|
/* Send the LOGOUT command */
|
2021-10-03 00:05:26 +08:00
|
|
|
CURLcode result = imap_sendf(data, "LOGOUT");
|
2013-03-09 20:12:20 +08:00
|
|
|
|
|
|
|
if(!result)
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_LOGOUT);
|
2013-03-09 20:12:20 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-12-29 05:24:36 +08:00
|
|
|
/* For the initial server greeting */
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_state_servergreet_resp(struct Curl_easy *data,
|
2012-03-24 19:55:34 +08:00
|
|
|
int imapcode,
|
|
|
|
imapstate instate)
|
|
|
|
{
|
2021-01-09 00:58:15 +08:00
|
|
|
struct connectdata *conn = data->conn;
|
2012-03-24 19:55:34 +08:00
|
|
|
(void)instate; /* no use for this yet */
|
|
|
|
|
2017-08-24 19:39:07 +08:00
|
|
|
if(imapcode == IMAP_RESP_PREAUTH) {
|
2017-08-23 06:08:18 +08:00
|
|
|
/* PREAUTH */
|
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
|
|
|
imapc->preauth = TRUE;
|
2022-04-16 17:55:05 +08:00
|
|
|
infof(data, "PREAUTH connection, already authenticated");
|
2017-08-23 06:08:18 +08:00
|
|
|
}
|
2017-08-24 19:39:07 +08:00
|
|
|
else if(imapcode != IMAP_RESP_OK) {
|
2012-03-24 19:55:34 +08:00
|
|
|
failf(data, "Got unexpected imap-server response");
|
2017-08-23 06:08:18 +08:00
|
|
|
return CURLE_WEIRD_SERVER_REPLY;
|
2012-03-24 19:55:34 +08:00
|
|
|
}
|
|
|
|
|
2021-01-09 00:58:15 +08:00
|
|
|
return imap_perform_capability(data, conn);
|
2012-03-24 19:55:34 +08:00
|
|
|
}
|
|
|
|
|
2013-02-12 06:41:08 +08:00
|
|
|
/* For CAPABILITY responses */
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_state_capability_resp(struct Curl_easy *data,
|
2013-02-12 06:41:08 +08:00
|
|
|
int imapcode,
|
|
|
|
imapstate instate)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-09 00:58:15 +08:00
|
|
|
struct connectdata *conn = data->conn;
|
2013-02-12 06:41:08 +08:00
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
2013-02-27 01:10:54 +08:00
|
|
|
const char *line = data->state.buffer;
|
2013-02-12 06:41:08 +08:00
|
|
|
|
|
|
|
(void)instate; /* no use for this yet */
|
|
|
|
|
2013-02-27 01:10:54 +08:00
|
|
|
/* Do we have a untagged response? */
|
|
|
|
if(imapcode == '*') {
|
|
|
|
line += 2;
|
|
|
|
|
|
|
|
/* Loop through the data line */
|
|
|
|
for(;;) {
|
2018-06-03 04:52:56 +08:00
|
|
|
size_t wordlen;
|
2013-02-27 01:10:54 +08:00
|
|
|
while(*line &&
|
|
|
|
(*line == ' ' || *line == '\t' ||
|
|
|
|
*line == '\r' || *line == '\n')) {
|
|
|
|
|
|
|
|
line++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!*line)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Extract the word */
|
|
|
|
for(wordlen = 0; line[wordlen] && line[wordlen] != ' ' &&
|
|
|
|
line[wordlen] != '\t' && line[wordlen] != '\r' &&
|
|
|
|
line[wordlen] != '\n';)
|
|
|
|
wordlen++;
|
|
|
|
|
|
|
|
/* Does the server support the STARTTLS capability? */
|
|
|
|
if(wordlen == 8 && !memcmp(line, "STARTTLS", 8))
|
|
|
|
imapc->tls_supported = TRUE;
|
|
|
|
|
|
|
|
/* Has the server explicitly disabled clear text authentication? */
|
|
|
|
else if(wordlen == 13 && !memcmp(line, "LOGINDISABLED", 13))
|
|
|
|
imapc->login_disabled = TRUE;
|
|
|
|
|
|
|
|
/* Does the server support the SASL-IR capability? */
|
|
|
|
else if(wordlen == 7 && !memcmp(line, "SASL-IR", 7))
|
|
|
|
imapc->ir_supported = TRUE;
|
|
|
|
|
|
|
|
/* Do we have a SASL based authentication mechanism? */
|
|
|
|
else if(wordlen > 5 && !memcmp(line, "AUTH=", 5)) {
|
2015-01-20 22:27:25 +08:00
|
|
|
size_t llen;
|
2021-05-11 17:11:40 +08:00
|
|
|
unsigned short mechbit;
|
2015-01-20 22:27:25 +08:00
|
|
|
|
2013-02-27 01:10:54 +08:00
|
|
|
line += 5;
|
|
|
|
wordlen -= 5;
|
|
|
|
|
|
|
|
/* Test the word for a matching authentication mechanism */
|
2016-03-20 01:15:53 +08:00
|
|
|
mechbit = Curl_sasl_decode_mech(line, wordlen, &llen);
|
|
|
|
if(mechbit && llen == wordlen)
|
2015-01-20 22:27:25 +08:00
|
|
|
imapc->sasl.authmechs |= mechbit;
|
2013-02-27 01:10:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
line += wordlen;
|
|
|
|
}
|
|
|
|
}
|
2022-12-30 16:14:55 +08:00
|
|
|
else if(data->set.use_ssl && !Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
|
2021-09-08 17:56:22 +08:00
|
|
|
/* PREAUTH is not compatible with STARTTLS. */
|
|
|
|
if(imapcode == IMAP_RESP_OK && imapc->tls_supported && !imapc->preauth) {
|
|
|
|
/* Switch to TLS connection now */
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_perform_starttls(data);
|
2013-02-12 06:41:08 +08:00
|
|
|
}
|
2021-09-08 17:56:22 +08:00
|
|
|
else if(data->set.use_ssl <= CURLUSESSL_TRY)
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_perform_authentication(data, conn);
|
2021-09-08 17:56:22 +08:00
|
|
|
else {
|
|
|
|
failf(data, "STARTTLS not available.");
|
|
|
|
result = CURLE_USE_SSL_FAILED;
|
|
|
|
}
|
2013-02-12 06:41:08 +08:00
|
|
|
}
|
|
|
|
else
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_perform_authentication(data, conn);
|
2013-02-12 06:41:08 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-12-29 05:24:36 +08:00
|
|
|
/* For STARTTLS responses */
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_state_starttls_resp(struct Curl_easy *data,
|
2009-12-13 05:54:01 +08:00
|
|
|
int imapcode,
|
|
|
|
imapstate instate)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-09 00:58:15 +08:00
|
|
|
struct connectdata *conn = data->conn;
|
2012-12-29 05:24:36 +08:00
|
|
|
|
2009-12-13 05:54:01 +08:00
|
|
|
(void)instate; /* no use for this yet */
|
|
|
|
|
2021-09-07 19:26:42 +08:00
|
|
|
/* Pipelining in response is forbidden. */
|
|
|
|
if(data->conn->proto.imapc.pp.cache_size)
|
|
|
|
return CURLE_WEIRD_SERVER_REPLY;
|
|
|
|
|
2017-08-24 19:39:07 +08:00
|
|
|
if(imapcode != IMAP_RESP_OK) {
|
2012-01-04 07:17:08 +08:00
|
|
|
if(data->set.use_ssl != CURLUSESSL_TRY) {
|
2017-01-12 15:22:41 +08:00
|
|
|
failf(data, "STARTTLS denied");
|
2012-01-04 07:17:08 +08:00
|
|
|
result = CURLE_USE_SSL_FAILED;
|
|
|
|
}
|
|
|
|
else
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_perform_authentication(data, conn);
|
2009-12-13 05:54:01 +08:00
|
|
|
}
|
2013-01-25 04:22:20 +08:00
|
|
|
else
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_perform_upgrade_tls(data, conn);
|
2013-01-19 17:49:17 +08:00
|
|
|
|
2009-12-13 05:54:01 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-01-21 00:33:05 +08:00
|
|
|
/* For SASL authentication responses */
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_state_auth_resp(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn,
|
2015-01-21 00:33:05 +08:00
|
|
|
int imapcode,
|
|
|
|
imapstate instate)
|
2014-08-11 04:32:56 +08:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
2015-01-21 00:33:05 +08:00
|
|
|
saslprogress progress;
|
2014-08-11 04:32:56 +08:00
|
|
|
|
|
|
|
(void)instate; /* no use for this yet */
|
|
|
|
|
2021-10-03 00:05:26 +08:00
|
|
|
result = Curl_sasl_continue(&imapc->sasl, data, imapcode, &progress);
|
2015-01-21 00:33:05 +08:00
|
|
|
if(!result)
|
|
|
|
switch(progress) {
|
|
|
|
case SASL_DONE:
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_STOP); /* Authenticated */
|
2015-01-21 00:33:05 +08:00
|
|
|
break;
|
|
|
|
case SASL_IDLE: /* No mechanism left after cancellation */
|
|
|
|
if((!imapc->login_disabled) && (imapc->preftype & IMAP_TYPE_CLEARTEXT))
|
|
|
|
/* Perform clear text authentication */
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_perform_login(data, conn);
|
2015-01-21 00:33:05 +08:00
|
|
|
else {
|
|
|
|
failf(data, "Authentication cancelled");
|
|
|
|
result = CURLE_LOGIN_DENIED;
|
2014-08-11 04:32:56 +08:00
|
|
|
}
|
2015-01-21 00:33:05 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2014-08-11 04:32:56 +08:00
|
|
|
}
|
2013-01-07 06:25:14 +08:00
|
|
|
|
|
|
|
return result;
|
2013-01-07 03:13:58 +08:00
|
|
|
}
|
|
|
|
|
2012-12-29 05:24:36 +08:00
|
|
|
/* For LOGIN responses */
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_state_login_resp(struct Curl_easy *data,
|
2009-12-13 05:54:01 +08:00
|
|
|
int imapcode,
|
|
|
|
imapstate instate)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
(void)instate; /* no use for this yet */
|
|
|
|
|
2017-08-24 19:39:07 +08:00
|
|
|
if(imapcode != IMAP_RESP_OK) {
|
2009-12-13 05:54:01 +08:00
|
|
|
failf(data, "Access denied. %c", imapcode);
|
|
|
|
result = CURLE_LOGIN_DENIED;
|
|
|
|
}
|
2012-12-30 03:23:10 +08:00
|
|
|
else
|
|
|
|
/* End of connect phase */
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_STOP);
|
2012-03-24 19:55:34 +08:00
|
|
|
|
2009-12-13 05:54:01 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-03-31 21:12:22 +08:00
|
|
|
/* For LIST and SEARCH responses */
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_state_listsearch_resp(struct Curl_easy *data,
|
2016-03-31 21:12:22 +08:00
|
|
|
int imapcode,
|
|
|
|
imapstate instate)
|
2013-03-06 05:36:25 +08:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-09 00:58:15 +08:00
|
|
|
char *line = data->state.buffer;
|
2013-03-06 05:36:25 +08:00
|
|
|
size_t len = strlen(line);
|
|
|
|
|
|
|
|
(void)instate; /* No use for this yet */
|
|
|
|
|
|
|
|
if(imapcode == '*') {
|
2013-03-07 04:10:53 +08:00
|
|
|
/* Temporarily add the LF character back and send as body to the client */
|
2013-03-06 05:36:25 +08:00
|
|
|
line[len] = '\n';
|
2021-01-09 00:58:15 +08:00
|
|
|
result = Curl_client_write(data, CLIENTWRITE_BODY, line, len + 1);
|
2013-03-06 05:36:25 +08:00
|
|
|
line[len] = '\0';
|
|
|
|
}
|
2017-08-24 19:39:07 +08:00
|
|
|
else if(imapcode != IMAP_RESP_OK)
|
2019-05-14 22:36:15 +08:00
|
|
|
result = CURLE_QUOTE_ERROR;
|
2013-03-07 04:10:53 +08:00
|
|
|
else
|
2013-03-06 05:36:25 +08:00
|
|
|
/* End of DO phase */
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_STOP);
|
2013-03-06 05:36:25 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-12-29 05:41:28 +08:00
|
|
|
/* For SELECT responses */
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_state_select_resp(struct Curl_easy *data, int imapcode,
|
2012-12-29 05:41:28 +08:00
|
|
|
imapstate instate)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-09 00:58:15 +08:00
|
|
|
struct connectdata *conn = data->conn;
|
|
|
|
struct IMAP *imap = data->req.p.imap;
|
2013-02-27 04:56:14 +08:00
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
|
|
|
const char *line = data->state.buffer;
|
2012-12-29 07:59:11 +08:00
|
|
|
|
2012-12-29 05:41:28 +08:00
|
|
|
(void)instate; /* no use for this yet */
|
|
|
|
|
2013-02-27 04:56:14 +08:00
|
|
|
if(imapcode == '*') {
|
|
|
|
/* See if this is an UIDVALIDITY response */
|
2018-06-03 04:52:56 +08:00
|
|
|
char tmp[20];
|
2013-02-27 04:56:14 +08:00
|
|
|
if(sscanf(line + 2, "OK [UIDVALIDITY %19[0123456789]]", tmp) == 1) {
|
|
|
|
Curl_safefree(imapc->mailbox_uidvalidity);
|
|
|
|
imapc->mailbox_uidvalidity = strdup(tmp);
|
|
|
|
}
|
|
|
|
}
|
2017-08-24 19:39:07 +08:00
|
|
|
else if(imapcode == IMAP_RESP_OK) {
|
2013-02-27 05:12:24 +08:00
|
|
|
/* Check if the UIDVALIDITY has been specified and matches */
|
|
|
|
if(imap->uidvalidity && imapc->mailbox_uidvalidity &&
|
2017-11-09 07:26:21 +08:00
|
|
|
!strcasecompare(imap->uidvalidity, imapc->mailbox_uidvalidity)) {
|
2021-01-09 00:58:15 +08:00
|
|
|
failf(data, "Mailbox UIDVALIDITY has changed");
|
2013-02-27 05:12:24 +08:00
|
|
|
result = CURLE_REMOTE_FILE_NOT_FOUND;
|
|
|
|
}
|
2013-02-28 02:02:53 +08:00
|
|
|
else {
|
|
|
|
/* Note the currently opened mailbox on this connection */
|
|
|
|
imapc->mailbox = strdup(imap->mailbox);
|
|
|
|
|
2013-03-03 18:27:18 +08:00
|
|
|
if(imap->custom)
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_perform_list(data);
|
2014-04-18 23:42:45 +08:00
|
|
|
else if(imap->query)
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_perform_search(data);
|
2013-03-03 18:27:18 +08:00
|
|
|
else
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_perform_fetch(data);
|
2013-02-28 02:02:53 +08:00
|
|
|
}
|
2013-02-27 05:12:24 +08:00
|
|
|
}
|
2013-02-28 04:53:34 +08:00
|
|
|
else {
|
|
|
|
failf(data, "Select failed");
|
|
|
|
result = CURLE_LOGIN_DENIED;
|
|
|
|
}
|
2012-12-29 05:41:28 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-03 05:22:44 +08:00
|
|
|
/* For the (first line of the) FETCH responses */
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_state_fetch_resp(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn, int imapcode,
|
2009-12-13 05:54:01 +08:00
|
|
|
imapstate instate)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
|
|
|
struct pingpong *pp = &imapc->pp;
|
|
|
|
const char *ptr = data->state.buffer;
|
2013-03-02 02:24:56 +08:00
|
|
|
bool parsed = FALSE;
|
2016-03-19 19:01:36 +08:00
|
|
|
curl_off_t size = 0;
|
2012-03-24 19:55:34 +08:00
|
|
|
|
2009-12-13 05:54:01 +08:00
|
|
|
(void)instate; /* no use for this yet */
|
|
|
|
|
2013-03-03 04:29:27 +08:00
|
|
|
if(imapcode != '*') {
|
2014-08-30 05:48:03 +08:00
|
|
|
Curl_pgrsSetDownloadSize(data, -1);
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_STOP);
|
2019-05-14 22:36:15 +08:00
|
|
|
return CURLE_REMOTE_FILE_NOT_FOUND;
|
2009-12-13 05:54:01 +08:00
|
|
|
}
|
|
|
|
|
2013-03-01 04:32:10 +08:00
|
|
|
/* Something like this is received "* 1 FETCH (BODY[TEXT] {2021}\r" so parse
|
|
|
|
the continuation data contained within the curly brackets */
|
2009-12-13 05:54:01 +08:00
|
|
|
while(*ptr && (*ptr != '{'))
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
if(*ptr == '{') {
|
2013-03-02 02:24:56 +08:00
|
|
|
char *endptr;
|
2017-08-15 05:33:23 +08:00
|
|
|
if(!curlx_strtoofft(ptr + 1, &endptr, 10, &size)) {
|
|
|
|
if(endptr - ptr > 1 && endptr[0] == '}' &&
|
|
|
|
endptr[1] == '\r' && endptr[2] == '\0')
|
|
|
|
parsed = TRUE;
|
|
|
|
}
|
2013-03-02 02:24:56 +08:00
|
|
|
}
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-03-02 02:24:56 +08:00
|
|
|
if(parsed) {
|
2021-07-06 23:05:17 +08:00
|
|
|
infof(data, "Found %" CURL_FORMAT_CURL_OFF_T " bytes to download",
|
2013-12-31 20:16:33 +08:00
|
|
|
size);
|
2013-03-02 02:24:56 +08:00
|
|
|
Curl_pgrsSetDownloadSize(data, size);
|
2009-12-13 05:54:01 +08:00
|
|
|
|
|
|
|
if(pp->cache) {
|
|
|
|
/* At this point there is a bunch of data in the header "cache" that is
|
|
|
|
actually body content, send it as body and then skip it. Do note
|
|
|
|
that there may even be additional "headers" after the body. */
|
|
|
|
size_t chunk = pp->cache_size;
|
|
|
|
|
2013-02-23 17:48:52 +08:00
|
|
|
if(chunk > (size_t)size)
|
2013-03-01 04:32:10 +08:00
|
|
|
/* The conversion from curl_off_t to size_t is always fine here */
|
2013-02-23 17:48:52 +08:00
|
|
|
chunk = (size_t)size;
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2017-10-07 06:11:31 +08:00
|
|
|
if(!chunk) {
|
|
|
|
/* no size, we're done with the data */
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_STOP);
|
2017-10-07 06:11:31 +08:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2021-01-09 00:58:15 +08:00
|
|
|
result = Curl_client_write(data, CLIENTWRITE_BODY, pp->cache, chunk);
|
2009-12-13 05:54:01 +08:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
2013-03-02 02:38:27 +08:00
|
|
|
data->req.bytecount += chunk;
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2018-05-29 03:46:22 +08:00
|
|
|
infof(data, "Written %zu bytes, %" CURL_FORMAT_CURL_OFF_TU
|
2021-07-06 23:05:17 +08:00
|
|
|
" bytes are left for transfer", chunk, size - chunk);
|
2013-03-01 04:32:10 +08:00
|
|
|
|
|
|
|
/* Have we used the entire cache or just part of it?*/
|
2009-12-13 05:54:01 +08:00
|
|
|
if(pp->cache_size > chunk) {
|
2013-03-01 04:32:10 +08:00
|
|
|
/* Only part of it so shrink the cache to fit the trailing data */
|
|
|
|
memmove(pp->cache, pp->cache + chunk, pp->cache_size - chunk);
|
2009-12-13 05:54:01 +08:00
|
|
|
pp->cache_size -= chunk;
|
|
|
|
}
|
|
|
|
else {
|
2013-03-01 04:32:10 +08:00
|
|
|
/* Free the cache */
|
2012-12-29 05:24:36 +08:00
|
|
|
Curl_safefree(pp->cache);
|
2013-03-01 04:32:10 +08:00
|
|
|
|
|
|
|
/* Reset the cache size */
|
2009-12-13 05:54:01 +08:00
|
|
|
pp->cache_size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-08 17:30:43 +08:00
|
|
|
if(data->req.bytecount == size)
|
2013-03-01 04:32:10 +08:00
|
|
|
/* The entire data is already transferred! */
|
2019-02-28 18:36:26 +08:00
|
|
|
Curl_setup_transfer(data, -1, -1, FALSE, -1);
|
2013-03-02 02:38:27 +08:00
|
|
|
else {
|
2009-12-13 05:54:01 +08:00
|
|
|
/* IMAP download */
|
2013-03-02 02:38:27 +08:00
|
|
|
data->req.maxdownload = size;
|
2020-05-08 03:20:26 +08:00
|
|
|
/* force a recv/send check of this connection, as the data might've been
|
|
|
|
read off the socket already */
|
|
|
|
data->conn->cselect_bits = CURL_CSELECT_IN;
|
2019-02-28 18:36:26 +08:00
|
|
|
Curl_setup_transfer(data, FIRSTSOCKET, size, FALSE, -1);
|
2013-03-02 02:38:27 +08:00
|
|
|
}
|
2009-12-13 05:54:01 +08:00
|
|
|
}
|
2013-03-02 02:24:56 +08:00
|
|
|
else {
|
2009-12-13 05:54:01 +08:00
|
|
|
/* We don't know how to parse this line */
|
2021-01-09 00:58:15 +08:00
|
|
|
failf(data, "Failed to parse FETCH response.");
|
2016-08-23 03:37:03 +08:00
|
|
|
result = CURLE_WEIRD_SERVER_REPLY;
|
2013-03-02 02:24:56 +08:00
|
|
|
}
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-03-01 04:32:10 +08:00
|
|
|
/* End of DO phase */
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_STOP);
|
2012-03-24 19:55:34 +08:00
|
|
|
|
2009-12-13 05:54:01 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-03 05:22:44 +08:00
|
|
|
/* For final FETCH responses performed after the download */
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_state_fetch_final_resp(struct Curl_easy *data,
|
2013-03-01 02:42:42 +08:00
|
|
|
int imapcode,
|
|
|
|
imapstate instate)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
|
|
|
|
(void)instate; /* No use for this yet */
|
|
|
|
|
2017-08-24 19:39:07 +08:00
|
|
|
if(imapcode != IMAP_RESP_OK)
|
2016-08-23 03:37:03 +08:00
|
|
|
result = CURLE_WEIRD_SERVER_REPLY;
|
2013-03-01 02:42:42 +08:00
|
|
|
else
|
2013-03-07 04:19:46 +08:00
|
|
|
/* End of DONE phase */
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_STOP);
|
2013-03-01 02:42:42 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-02 03:06:18 +08:00
|
|
|
/* For APPEND responses */
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_state_append_resp(struct Curl_easy *data, int imapcode,
|
2013-03-02 03:06:18 +08:00
|
|
|
imapstate instate)
|
|
|
|
{
|
2013-03-03 04:40:38 +08:00
|
|
|
CURLcode result = CURLE_OK;
|
2013-03-02 03:06:18 +08:00
|
|
|
(void)instate; /* No use for this yet */
|
|
|
|
|
2013-03-03 04:35:03 +08:00
|
|
|
if(imapcode != '+') {
|
2013-03-03 04:40:38 +08:00
|
|
|
result = CURLE_UPLOAD_FAILED;
|
2013-03-03 04:35:03 +08:00
|
|
|
}
|
|
|
|
else {
|
2013-03-07 04:31:53 +08:00
|
|
|
/* Set the progress upload size */
|
2014-04-15 19:49:18 +08:00
|
|
|
Curl_pgrsSetUploadSize(data, data->state.infilesize);
|
2013-03-03 05:22:44 +08:00
|
|
|
|
|
|
|
/* IMAP upload */
|
2019-02-28 18:36:26 +08:00
|
|
|
Curl_setup_transfer(data, -1, -1, FALSE, FIRSTSOCKET);
|
2013-03-03 04:40:38 +08:00
|
|
|
|
2013-03-07 04:31:53 +08:00
|
|
|
/* End of DO phase */
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_STOP);
|
2013-03-07 04:31:53 +08:00
|
|
|
}
|
2013-03-03 04:40:38 +08:00
|
|
|
|
|
|
|
return result;
|
2013-03-02 03:06:18 +08:00
|
|
|
}
|
|
|
|
|
2013-03-02 03:34:51 +08:00
|
|
|
/* For final APPEND responses performed after the upload */
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_state_append_final_resp(struct Curl_easy *data,
|
2013-03-02 03:34:51 +08:00
|
|
|
int imapcode,
|
|
|
|
imapstate instate)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
|
|
|
|
(void)instate; /* No use for this yet */
|
|
|
|
|
2017-08-24 19:39:07 +08:00
|
|
|
if(imapcode != IMAP_RESP_OK)
|
2013-03-02 03:34:51 +08:00
|
|
|
result = CURLE_UPLOAD_FAILED;
|
2013-03-07 04:31:53 +08:00
|
|
|
else
|
|
|
|
/* End of DONE phase */
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_STOP);
|
2013-03-02 03:34:51 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_statemachine(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn)
|
2009-12-13 05:54:01 +08:00
|
|
|
{
|
2013-02-17 07:37:50 +08:00
|
|
|
CURLcode result = CURLE_OK;
|
2009-12-13 05:54:01 +08:00
|
|
|
curl_socket_t sock = conn->sock[FIRSTSOCKET];
|
|
|
|
int imapcode;
|
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
|
|
|
struct pingpong *pp = &imapc->pp;
|
|
|
|
size_t nread = 0;
|
2021-01-09 00:58:15 +08:00
|
|
|
(void)data;
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2012-12-29 05:24:36 +08:00
|
|
|
/* Busy upgrading the connection; right now all I/O is SSL/TLS, not IMAP */
|
2011-02-16 07:56:46 +08:00
|
|
|
if(imapc->state == IMAP_UPGRADETLS)
|
2021-01-09 00:58:15 +08:00
|
|
|
return imap_perform_upgrade_tls(data, conn);
|
2011-02-16 07:56:46 +08:00
|
|
|
|
2012-12-29 05:24:36 +08:00
|
|
|
/* Flush any data that needs to be sent */
|
2009-12-13 05:54:01 +08:00
|
|
|
if(pp->sendleft)
|
2021-01-18 18:56:50 +08:00
|
|
|
return Curl_pp_flushsend(data, pp);
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-02-28 02:49:00 +08:00
|
|
|
do {
|
|
|
|
/* Read the response from the server */
|
2021-01-09 00:58:15 +08:00
|
|
|
result = Curl_pp_readresp(data, sock, pp, &imapcode, &nread);
|
2013-02-28 02:49:00 +08:00
|
|
|
if(result)
|
|
|
|
return result;
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-02-28 02:49:00 +08:00
|
|
|
/* Was there an error parsing the response line? */
|
|
|
|
if(imapcode == -1)
|
2016-08-23 03:37:03 +08:00
|
|
|
return CURLE_WEIRD_SERVER_REPLY;
|
2013-02-28 02:49:00 +08:00
|
|
|
|
|
|
|
if(!imapcode)
|
|
|
|
break;
|
2013-02-25 03:23:33 +08:00
|
|
|
|
2012-12-29 05:24:36 +08:00
|
|
|
/* We have now received a full IMAP server response */
|
|
|
|
switch(imapc->state) {
|
|
|
|
case IMAP_SERVERGREET:
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_state_servergreet_resp(data, imapcode, imapc->state);
|
2012-12-29 05:24:36 +08:00
|
|
|
break;
|
|
|
|
|
2013-01-07 03:13:58 +08:00
|
|
|
case IMAP_CAPABILITY:
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_state_capability_resp(data, imapcode, imapc->state);
|
2013-01-07 03:13:58 +08:00
|
|
|
break;
|
|
|
|
|
2013-02-11 05:18:30 +08:00
|
|
|
case IMAP_STARTTLS:
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_state_starttls_resp(data, imapcode, imapc->state);
|
2013-02-11 05:18:30 +08:00
|
|
|
break;
|
|
|
|
|
2015-01-21 00:33:05 +08:00
|
|
|
case IMAP_AUTHENTICATE:
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_state_auth_resp(data, conn, imapcode, imapc->state);
|
2013-01-07 06:25:14 +08:00
|
|
|
break;
|
|
|
|
|
2012-12-30 20:44:09 +08:00
|
|
|
case IMAP_LOGIN:
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_state_login_resp(data, imapcode, imapc->state);
|
2012-12-30 20:44:09 +08:00
|
|
|
break;
|
|
|
|
|
2013-03-06 05:36:25 +08:00
|
|
|
case IMAP_LIST:
|
2019-09-19 15:52:01 +08:00
|
|
|
case IMAP_SEARCH:
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_state_listsearch_resp(data, imapcode, imapc->state);
|
2013-03-06 05:36:25 +08:00
|
|
|
break;
|
|
|
|
|
2012-12-29 05:24:36 +08:00
|
|
|
case IMAP_SELECT:
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_state_select_resp(data, imapcode, imapc->state);
|
2012-12-29 05:24:36 +08:00
|
|
|
break;
|
|
|
|
|
2013-02-26 00:59:05 +08:00
|
|
|
case IMAP_FETCH:
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_state_fetch_resp(data, conn, imapcode, imapc->state);
|
2013-02-26 00:59:05 +08:00
|
|
|
break;
|
|
|
|
|
2013-03-01 02:42:42 +08:00
|
|
|
case IMAP_FETCH_FINAL:
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_state_fetch_final_resp(data, imapcode, imapc->state);
|
2013-03-01 02:42:42 +08:00
|
|
|
break;
|
|
|
|
|
2013-03-02 03:06:18 +08:00
|
|
|
case IMAP_APPEND:
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_state_append_resp(data, imapcode, imapc->state);
|
2013-03-02 03:06:18 +08:00
|
|
|
break;
|
|
|
|
|
2013-03-02 03:34:51 +08:00
|
|
|
case IMAP_APPEND_FINAL:
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_state_append_final_resp(data, imapcode, imapc->state);
|
2013-03-03 18:09:44 +08:00
|
|
|
break;
|
|
|
|
|
2012-12-29 05:24:36 +08:00
|
|
|
case IMAP_LOGOUT:
|
|
|
|
/* fallthrough, just stop! */
|
|
|
|
default:
|
|
|
|
/* internal error */
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_STOP);
|
2012-12-29 05:24:36 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-02-28 02:49:00 +08:00
|
|
|
} while(!result && imapc->state != IMAP_STOP && Curl_pp_moredata(pp));
|
2009-12-13 05:54:01 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
/* Called repeatedly until done from multi.c */
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_multi_statemach(struct Curl_easy *data, bool *done)
|
2009-12-13 05:54:01 +08:00
|
|
|
{
|
2013-02-17 07:37:50 +08:00
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-09 00:58:15 +08:00
|
|
|
struct connectdata *conn = data->conn;
|
2009-12-13 05:54:01 +08:00
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
2011-02-15 02:41:42 +08:00
|
|
|
|
2013-09-11 02:40:11 +08:00
|
|
|
if((conn->handler->flags & PROTOPT_SSL) && !imapc->ssldone) {
|
2023-01-01 18:26:22 +08:00
|
|
|
bool ssldone = FALSE;
|
|
|
|
result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone);
|
|
|
|
imapc->ssldone = ssldone;
|
|
|
|
if(result || !ssldone)
|
2013-09-11 02:40:11 +08:00
|
|
|
return result;
|
|
|
|
}
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2021-01-09 00:58:15 +08:00
|
|
|
result = Curl_pp_statemach(data, &imapc->pp, FALSE, FALSE);
|
2011-09-06 02:46:09 +08:00
|
|
|
*done = (imapc->state == IMAP_STOP) ? TRUE : FALSE;
|
2009-12-13 05:54:01 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_block_statemach(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn,
|
2018-12-14 16:21:47 +08:00
|
|
|
bool disconnecting)
|
2009-12-13 05:54:01 +08:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2013-02-17 07:37:50 +08:00
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-03-08 06:44:10 +08:00
|
|
|
while(imapc->state != IMAP_STOP && !result)
|
2021-01-09 00:58:15 +08:00
|
|
|
result = Curl_pp_statemach(data, &imapc->pp, TRUE, disconnecting);
|
2009-12-13 05:54:01 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:47:12 +08:00
|
|
|
/* Allocate and initialize the struct IMAP for the current Curl_easy if
|
2012-12-29 05:24:36 +08:00
|
|
|
required */
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_init(struct Curl_easy *data)
|
2009-12-13 05:54:01 +08:00
|
|
|
{
|
2013-03-10 06:25:40 +08:00
|
|
|
CURLcode result = CURLE_OK;
|
2013-08-05 01:34:16 +08:00
|
|
|
struct IMAP *imap;
|
2012-12-29 05:24:36 +08:00
|
|
|
|
2020-11-23 15:32:41 +08:00
|
|
|
imap = data->req.p.imap = calloc(sizeof(struct IMAP), 1);
|
2013-08-05 01:34:16 +08:00
|
|
|
if(!imap)
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-03-10 06:25:40 +08:00
|
|
|
return result;
|
2009-12-13 05:54:01 +08:00
|
|
|
}
|
|
|
|
|
2013-02-12 07:13:50 +08:00
|
|
|
/* For the IMAP "protocol connect" and "doing" phases only */
|
2021-01-09 00:58:15 +08:00
|
|
|
static int imap_getsock(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn,
|
|
|
|
curl_socket_t *socks)
|
2013-02-12 07:13:50 +08:00
|
|
|
{
|
2021-01-26 00:23:33 +08:00
|
|
|
return Curl_pp_getsock(data, &conn->proto.imapc.pp, socks);
|
2013-02-12 07:13:50 +08:00
|
|
|
}
|
|
|
|
|
2012-12-29 05:24:36 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2013-04-13 01:01:03 +08:00
|
|
|
* imap_connect()
|
|
|
|
*
|
|
|
|
* This function should do everything that is to be considered a part of the
|
|
|
|
* connection phase.
|
2009-12-13 05:54:01 +08:00
|
|
|
*
|
|
|
|
* The variable 'done' points to will be TRUE if the protocol-layer connect
|
2013-08-10 05:39:09 +08:00
|
|
|
* phase is done when this function returns, or FALSE if not.
|
2009-12-13 05:54:01 +08:00
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_connect(struct Curl_easy *data, bool *done)
|
2009-12-13 05:54:01 +08:00
|
|
|
{
|
2013-02-17 07:37:50 +08:00
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-09 00:58:15 +08:00
|
|
|
struct connectdata *conn = data->conn;
|
2009-12-13 05:54:01 +08:00
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
|
|
|
struct pingpong *pp = &imapc->pp;
|
|
|
|
|
|
|
|
*done = FALSE; /* default to not done yet */
|
|
|
|
|
2013-02-14 04:12:21 +08:00
|
|
|
/* We always support persistent connections in IMAP */
|
2014-05-20 16:32:23 +08:00
|
|
|
connkeep(conn, "IMAP default");
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2021-01-09 00:58:15 +08:00
|
|
|
PINGPONG_SETUP(pp, imap_statemachine, imap_endofresp);
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-12-14 06:57:13 +08:00
|
|
|
/* Set the default preferred authentication type and mechanism */
|
|
|
|
imapc->preftype = IMAP_TYPE_ANY;
|
2021-10-03 00:05:26 +08:00
|
|
|
Curl_sasl_init(&imapc->sasl, data, &saslimap);
|
2013-04-13 22:58:15 +08:00
|
|
|
|
2020-09-25 05:04:15 +08:00
|
|
|
Curl_dyn_init(&imapc->dyn, DYN_IMAP_CMD);
|
2013-02-14 04:12:21 +08:00
|
|
|
/* Initialise the pingpong layer */
|
2020-09-23 15:22:02 +08:00
|
|
|
Curl_pp_setup(pp);
|
2021-01-09 00:58:15 +08:00
|
|
|
Curl_pp_init(data, pp);
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-04-13 22:58:15 +08:00
|
|
|
/* Parse the URL options */
|
|
|
|
result = imap_parse_url_options(conn);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
2012-12-29 05:24:36 +08:00
|
|
|
/* Start off waiting for the server greeting response */
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_SERVERGREET);
|
2012-12-29 05:24:36 +08:00
|
|
|
|
2013-02-07 08:18:23 +08:00
|
|
|
/* Start off with an response id of '*' */
|
|
|
|
strcpy(imapc->resptag, "*");
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_multi_statemach(data, done);
|
2009-12-13 05:54:01 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_done()
|
|
|
|
*
|
|
|
|
* The DONE function. This does what needs to be done after a single DO has
|
|
|
|
* performed.
|
|
|
|
*
|
|
|
|
* Input argument is already checked for validity.
|
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_done(struct Curl_easy *data, CURLcode status,
|
2009-12-13 05:54:01 +08:00
|
|
|
bool premature)
|
|
|
|
{
|
2013-02-17 07:37:50 +08:00
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-09 00:58:15 +08:00
|
|
|
struct connectdata *conn = data->conn;
|
2020-11-23 15:32:41 +08:00
|
|
|
struct IMAP *imap = data->req.p.imap;
|
2012-12-29 05:24:36 +08:00
|
|
|
|
2009-12-13 05:54:01 +08:00
|
|
|
(void)premature;
|
|
|
|
|
|
|
|
if(!imap)
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
if(status) {
|
2014-05-20 16:32:23 +08:00
|
|
|
connclose(conn, "IMAP done with bad status"); /* marked for closure */
|
2012-12-29 05:24:36 +08:00
|
|
|
result = status; /* use the already set error code */
|
2009-12-13 05:54:01 +08:00
|
|
|
}
|
2013-03-06 06:04:03 +08:00
|
|
|
else if(!data->set.connect_only && !imap->custom &&
|
2018-07-31 19:12:18 +08:00
|
|
|
(imap->uid || imap->mindex || data->set.upload ||
|
2017-09-03 00:47:10 +08:00
|
|
|
data->set.mimepost.kind != MIMEKIND_NONE)) {
|
2013-03-02 03:34:51 +08:00
|
|
|
/* Handle responses after FETCH or APPEND transfer has finished */
|
2018-07-31 19:12:18 +08:00
|
|
|
|
2017-09-03 00:47:10 +08:00
|
|
|
if(!data->set.upload && data->set.mimepost.kind == MIMEKIND_NONE)
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_FETCH_FINAL);
|
2013-03-02 03:34:51 +08:00
|
|
|
else {
|
|
|
|
/* End the APPEND command first by sending an empty line */
|
2021-01-09 00:58:15 +08:00
|
|
|
result = Curl_pp_sendf(data, &conn->proto.imapc.pp, "%s", "");
|
2013-03-02 03:34:51 +08:00
|
|
|
if(!result)
|
2021-01-09 00:58:15 +08:00
|
|
|
state(data, IMAP_APPEND_FINAL);
|
2013-03-02 03:34:51 +08:00
|
|
|
}
|
2013-03-01 02:42:42 +08:00
|
|
|
|
2019-05-14 22:36:15 +08:00
|
|
|
/* Run the state-machine */
|
2013-03-02 03:34:51 +08:00
|
|
|
if(!result)
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_block_statemach(data, conn, FALSE);
|
2013-03-01 02:42:42 +08:00
|
|
|
}
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-02-23 07:29:33 +08:00
|
|
|
/* Cleanup our per-request based variables */
|
2013-02-23 17:29:40 +08:00
|
|
|
Curl_safefree(imap->mailbox);
|
2013-02-24 01:24:53 +08:00
|
|
|
Curl_safefree(imap->uidvalidity);
|
|
|
|
Curl_safefree(imap->uid);
|
2018-07-31 19:12:18 +08:00
|
|
|
Curl_safefree(imap->mindex);
|
2013-02-24 01:24:53 +08:00
|
|
|
Curl_safefree(imap->section);
|
2014-04-19 00:42:40 +08:00
|
|
|
Curl_safefree(imap->partial);
|
2014-04-18 22:58:33 +08:00
|
|
|
Curl_safefree(imap->query);
|
2013-03-03 17:12:27 +08:00
|
|
|
Curl_safefree(imap->custom);
|
|
|
|
Curl_safefree(imap->custom_params);
|
2013-02-23 07:29:33 +08:00
|
|
|
|
2013-02-23 19:34:52 +08:00
|
|
|
/* Clear the transfer mode for the next request */
|
2021-02-04 20:06:17 +08:00
|
|
|
imap->transfer = PPTRANSFER_BODY;
|
2009-12-13 05:54:01 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_perform()
|
|
|
|
*
|
2013-03-03 18:21:29 +08:00
|
|
|
* This is the actual DO function for IMAP. Fetch or append a message, or do
|
|
|
|
* other things according to the options previously setup.
|
2009-12-13 05:54:01 +08:00
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_perform(struct Curl_easy *data, bool *connected,
|
2012-12-29 05:24:36 +08:00
|
|
|
bool *dophase_done)
|
2009-12-13 05:54:01 +08:00
|
|
|
{
|
2012-12-29 05:24:36 +08:00
|
|
|
/* This is IMAP and no proxy */
|
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-09 00:58:15 +08:00
|
|
|
struct connectdata *conn = data->conn;
|
2020-11-23 15:32:41 +08:00
|
|
|
struct IMAP *imap = data->req.p.imap;
|
2013-02-28 02:02:53 +08:00
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
2013-03-03 18:21:29 +08:00
|
|
|
bool selected = FALSE;
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2021-07-06 23:05:17 +08:00
|
|
|
DEBUGF(infof(data, "DO phase starts"));
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2022-11-11 17:57:04 +08:00
|
|
|
if(data->req.no_body) {
|
2012-12-29 05:24:36 +08:00
|
|
|
/* Requested no body means no transfer */
|
2021-02-04 20:06:17 +08:00
|
|
|
imap->transfer = PPTRANSFER_INFO;
|
2009-12-13 05:54:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*dophase_done = FALSE; /* not done yet */
|
|
|
|
|
2013-03-03 18:21:29 +08:00
|
|
|
/* Determine if the requested mailbox (with the same UIDVALIDITY if set)
|
|
|
|
has already been selected on this connection */
|
|
|
|
if(imap->mailbox && imapc->mailbox &&
|
2017-11-09 07:26:21 +08:00
|
|
|
strcasecompare(imap->mailbox, imapc->mailbox) &&
|
2013-03-03 18:21:29 +08:00
|
|
|
(!imap->uidvalidity || !imapc->mailbox_uidvalidity ||
|
2017-11-09 07:26:21 +08:00
|
|
|
strcasecompare(imap->uidvalidity, imapc->mailbox_uidvalidity)))
|
2013-03-03 18:21:29 +08:00
|
|
|
selected = TRUE;
|
|
|
|
|
2012-12-29 05:24:36 +08:00
|
|
|
/* Start the first command in the DO phase */
|
2021-01-09 00:58:15 +08:00
|
|
|
if(data->set.upload || data->set.mimepost.kind != MIMEKIND_NONE)
|
2013-03-02 03:49:14 +08:00
|
|
|
/* APPEND can be executed directly */
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_perform_append(data);
|
2013-03-03 18:21:29 +08:00
|
|
|
else if(imap->custom && (selected || !imap->mailbox))
|
|
|
|
/* Custom command using the same mailbox or no mailbox */
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_perform_list(data);
|
2018-07-31 19:12:18 +08:00
|
|
|
else if(!imap->custom && selected && (imap->uid || imap->mindex))
|
2013-03-03 18:21:29 +08:00
|
|
|
/* FETCH from the same mailbox */
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_perform_fetch(data);
|
2014-04-18 23:42:45 +08:00
|
|
|
else if(!imap->custom && selected && imap->query)
|
|
|
|
/* SEARCH the current mailbox */
|
2021-10-03 00:05:26 +08:00
|
|
|
result = imap_perform_search(data);
|
2014-04-18 23:42:45 +08:00
|
|
|
else if(imap->mailbox && !selected &&
|
2018-07-31 19:12:18 +08:00
|
|
|
(imap->custom || imap->uid || imap->mindex || imap->query))
|
2013-03-03 18:21:29 +08:00
|
|
|
/* SELECT the mailbox */
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_perform_select(data);
|
2013-03-06 06:04:03 +08:00
|
|
|
else
|
|
|
|
/* LIST */
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_perform_list(data);
|
2013-02-28 18:23:50 +08:00
|
|
|
|
2009-12-13 05:54:01 +08:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
2013-02-24 04:02:05 +08:00
|
|
|
/* Run the state-machine */
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_multi_statemach(data, dophase_done);
|
2013-01-17 19:59:23 +08:00
|
|
|
|
2022-11-22 16:55:41 +08:00
|
|
|
*connected = Curl_conn_is_connected(conn, FIRSTSOCKET);
|
2009-12-13 05:54:01 +08:00
|
|
|
|
|
|
|
if(*dophase_done)
|
2021-07-06 23:05:17 +08:00
|
|
|
DEBUGF(infof(data, "DO phase is complete"));
|
2009-12-13 05:54:01 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_do()
|
|
|
|
*
|
|
|
|
* This function is registered as 'curl_do' function. It decodes the path
|
|
|
|
* parts etc as a wrapper to the actual DO function (imap_perform).
|
|
|
|
*
|
|
|
|
* The input argument is already checked for validity.
|
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_do(struct Curl_easy *data, bool *done)
|
2009-12-13 05:54:01 +08:00
|
|
|
{
|
2013-02-17 07:37:50 +08:00
|
|
|
CURLcode result = CURLE_OK;
|
2009-12-13 05:54:01 +08:00
|
|
|
*done = FALSE; /* default to false */
|
|
|
|
|
2012-12-29 05:24:36 +08:00
|
|
|
/* Parse the URL path */
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_parse_url_path(data);
|
2013-02-17 07:37:50 +08:00
|
|
|
if(result)
|
|
|
|
return result;
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-03-03 17:49:37 +08:00
|
|
|
/* Parse the custom request */
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_parse_custom_request(data);
|
2013-03-03 17:49:37 +08:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_regular_transfer(data, done);
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-02-17 07:37:50 +08:00
|
|
|
return result;
|
2009-12-13 05:54:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_disconnect()
|
|
|
|
*
|
|
|
|
* Disconnect from an IMAP server. Cleanup protocol-specific per-connection
|
|
|
|
* resources. BLOCKING.
|
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_disconnect(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn, bool dead_connection)
|
2009-12-13 05:54:01 +08:00
|
|
|
{
|
2013-03-03 21:13:59 +08:00
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
2021-01-09 00:58:15 +08:00
|
|
|
(void)data;
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2012-12-29 05:24:36 +08:00
|
|
|
/* We cannot send quit unconditionally. If this connection is stale or
|
|
|
|
bad in any way, sending quit and waiting around here will make the
|
2013-02-24 04:02:05 +08:00
|
|
|
disconnect wait in vain and cause more problems than we need to. */
|
2012-12-29 05:24:36 +08:00
|
|
|
|
2009-12-13 05:54:01 +08:00
|
|
|
/* The IMAP session may or may not have been allocated/setup at this
|
|
|
|
point! */
|
2021-01-09 00:58:15 +08:00
|
|
|
if(!dead_connection && conn->bits.protoconnstart) {
|
2021-10-03 00:05:26 +08:00
|
|
|
if(!imap_perform_logout(data))
|
2021-01-09 00:58:15 +08:00
|
|
|
(void)imap_block_statemach(data, conn, TRUE); /* ignore errors */
|
|
|
|
}
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2012-12-29 05:24:36 +08:00
|
|
|
/* Disconnect from the server */
|
2009-12-13 05:54:01 +08:00
|
|
|
Curl_pp_disconnect(&imapc->pp);
|
2020-09-25 05:04:15 +08:00
|
|
|
Curl_dyn_free(&imapc->dyn);
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-01-07 06:25:14 +08:00
|
|
|
/* Cleanup the SASL module */
|
2015-01-20 21:14:26 +08:00
|
|
|
Curl_sasl_cleanup(conn, imapc->sasl.authused);
|
2013-01-07 06:25:14 +08:00
|
|
|
|
2013-02-27 04:42:01 +08:00
|
|
|
/* Cleanup our connection based variables */
|
2013-02-28 01:31:08 +08:00
|
|
|
Curl_safefree(imapc->mailbox);
|
2013-02-27 04:42:01 +08:00
|
|
|
Curl_safefree(imapc->mailbox_uidvalidity);
|
|
|
|
|
2009-12-13 05:54:01 +08:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-29 05:24:36 +08:00
|
|
|
/* Call this when the DO phase has completed */
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_dophase_done(struct Curl_easy *data, bool connected)
|
2009-12-13 05:54:01 +08:00
|
|
|
{
|
2021-01-09 00:58:15 +08:00
|
|
|
struct IMAP *imap = data->req.p.imap;
|
2012-12-29 05:24:36 +08:00
|
|
|
|
2009-12-13 05:54:01 +08:00
|
|
|
(void)connected;
|
|
|
|
|
2021-02-04 20:06:17 +08:00
|
|
|
if(imap->transfer != PPTRANSFER_BODY)
|
2009-12-13 05:54:01 +08:00
|
|
|
/* no data to transfer */
|
2021-01-09 00:58:15 +08:00
|
|
|
Curl_setup_transfer(data, -1, -1, FALSE, -1);
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2010-04-17 05:43:04 +08:00
|
|
|
return CURLE_OK;
|
2009-12-13 05:54:01 +08:00
|
|
|
}
|
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
/* Called from multi.c while DOing */
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_doing(struct Curl_easy *data, bool *dophase_done)
|
2009-12-13 05:54:01 +08:00
|
|
|
{
|
2021-01-09 00:58:15 +08:00
|
|
|
CURLcode result = imap_multi_statemach(data, dophase_done);
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2012-12-29 05:41:28 +08:00
|
|
|
if(result)
|
2021-07-06 23:05:17 +08:00
|
|
|
DEBUGF(infof(data, "DO phase failed"));
|
2013-03-09 07:02:20 +08:00
|
|
|
else if(*dophase_done) {
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_dophase_done(data, FALSE /* not connected */);
|
2012-12-29 05:41:28 +08:00
|
|
|
|
2021-07-06 23:05:17 +08:00
|
|
|
DEBUGF(infof(data, "DO phase is complete"));
|
2009-12-13 05:54:01 +08:00
|
|
|
}
|
2012-12-29 05:24:36 +08:00
|
|
|
|
2009-12-13 05:54:01 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_regular_transfer()
|
|
|
|
*
|
|
|
|
* The input argument is already checked for validity.
|
|
|
|
*
|
|
|
|
* Performs all commands done before a regular transfer between a local and a
|
|
|
|
* remote host.
|
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_regular_transfer(struct Curl_easy *data,
|
2012-12-29 05:24:36 +08:00
|
|
|
bool *dophase_done)
|
2009-12-13 05:54:01 +08:00
|
|
|
{
|
2012-12-29 05:24:36 +08:00
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
bool connected = FALSE;
|
|
|
|
|
|
|
|
/* Make sure size is unknown at this point */
|
|
|
|
data->req.size = -1;
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-03-09 07:25:04 +08:00
|
|
|
/* Set the progress data */
|
2009-12-13 05:54:01 +08:00
|
|
|
Curl_pgrsSetUploadCounter(data, 0);
|
|
|
|
Curl_pgrsSetDownloadCounter(data, 0);
|
2014-08-30 05:48:03 +08:00
|
|
|
Curl_pgrsSetUploadSize(data, -1);
|
|
|
|
Curl_pgrsSetDownloadSize(data, -1);
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-03-09 07:25:04 +08:00
|
|
|
/* Carry out the perform */
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_perform(data, &connected, dophase_done);
|
2009-12-13 05:54:01 +08:00
|
|
|
|
2013-03-09 07:25:04 +08:00
|
|
|
/* Perform post DO phase operations if necessary */
|
|
|
|
if(!result && *dophase_done)
|
2021-01-09 00:58:15 +08:00
|
|
|
result = imap_dophase_done(data, connected);
|
2009-12-13 05:54:01 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_setup_connection(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn)
|
2009-12-13 05:54:01 +08:00
|
|
|
{
|
2013-08-05 01:34:16 +08:00
|
|
|
/* Initialise the IMAP layer */
|
2021-01-09 00:58:15 +08:00
|
|
|
CURLcode result = imap_init(data);
|
2013-08-05 01:34:16 +08:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
2016-03-06 04:10:11 +08:00
|
|
|
/* Clear the TLS upgraded flag */
|
2020-05-26 20:42:47 +08:00
|
|
|
conn->bits.tls_upgraded = FALSE;
|
2009-12-13 05:54:01 +08:00
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-13 01:05:53 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_sendf()
|
|
|
|
*
|
2017-03-26 23:02:22 +08:00
|
|
|
* Sends the formatted string as an IMAP command to the server.
|
2013-04-13 01:05:53 +08:00
|
|
|
*
|
|
|
|
* Designed to never block.
|
|
|
|
*/
|
2021-10-03 00:05:26 +08:00
|
|
|
static CURLcode imap_sendf(struct Curl_easy *data, const char *fmt, ...)
|
2013-04-13 01:05:53 +08:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2021-10-03 00:05:26 +08:00
|
|
|
struct imap_conn *imapc = &data->conn->proto.imapc;
|
2013-07-24 22:44:37 +08:00
|
|
|
|
|
|
|
DEBUGASSERT(fmt);
|
2013-04-13 01:05:53 +08:00
|
|
|
|
|
|
|
/* Calculate the tag based on the connection ID and command ID */
|
2018-11-22 16:01:24 +08:00
|
|
|
msnprintf(imapc->resptag, sizeof(imapc->resptag), "%c%03d",
|
2021-10-03 00:05:26 +08:00
|
|
|
'A' + curlx_sltosi(data->conn->connection_id % 26),
|
2023-01-01 18:26:22 +08:00
|
|
|
++imapc->cmdid);
|
2013-04-13 01:05:53 +08:00
|
|
|
|
2020-09-25 05:04:15 +08:00
|
|
|
/* start with a blank buffer */
|
|
|
|
Curl_dyn_reset(&imapc->dyn);
|
2013-04-13 01:05:53 +08:00
|
|
|
|
2020-09-25 05:04:15 +08:00
|
|
|
/* append tag + space + fmt */
|
|
|
|
result = Curl_dyn_addf(&imapc->dyn, "%s %s", imapc->resptag, fmt);
|
|
|
|
if(!result) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2021-01-09 00:58:15 +08:00
|
|
|
result = Curl_pp_vsendf(data, &imapc->pp, Curl_dyn_ptr(&imapc->dyn), ap);
|
2020-09-25 05:04:15 +08:00
|
|
|
va_end(ap);
|
|
|
|
}
|
2013-04-13 01:05:53 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_atom()
|
|
|
|
*
|
|
|
|
* Checks the input string for characters that need escaping and returns an
|
|
|
|
* atom ready for sending to the server.
|
|
|
|
*
|
|
|
|
* The returned string needs to be freed.
|
|
|
|
*
|
|
|
|
*/
|
2015-11-07 19:20:34 +08:00
|
|
|
static char *imap_atom(const char *str, bool escape_only)
|
2013-04-13 01:05:53 +08:00
|
|
|
{
|
2016-04-19 14:31:11 +08:00
|
|
|
/* !checksrc! disable PARENBRACE 1 */
|
2015-11-07 19:59:32 +08:00
|
|
|
const char atom_specials[] = "(){ %*]";
|
2013-04-13 01:05:53 +08:00
|
|
|
const char *p1;
|
|
|
|
char *p2;
|
|
|
|
size_t backsp_count = 0;
|
|
|
|
size_t quote_count = 0;
|
2015-11-07 19:59:32 +08:00
|
|
|
bool others_exists = FALSE;
|
2013-04-13 01:05:53 +08:00
|
|
|
size_t newlen = 0;
|
|
|
|
char *newstr = NULL;
|
|
|
|
|
|
|
|
if(!str)
|
|
|
|
return NULL;
|
|
|
|
|
2015-11-07 19:59:32 +08:00
|
|
|
/* Look for "atom-specials", counting the backslash and quote characters as
|
2019-05-14 18:38:09 +08:00
|
|
|
these will need escaping */
|
2013-04-13 01:05:53 +08:00
|
|
|
p1 = str;
|
|
|
|
while(*p1) {
|
|
|
|
if(*p1 == '\\')
|
|
|
|
backsp_count++;
|
|
|
|
else if(*p1 == '"')
|
|
|
|
quote_count++;
|
2015-11-07 19:59:32 +08:00
|
|
|
else if(!escape_only) {
|
|
|
|
const char *p3 = atom_specials;
|
|
|
|
|
2015-11-08 06:21:29 +08:00
|
|
|
while(*p3 && !others_exists) {
|
2015-11-07 19:59:32 +08:00
|
|
|
if(*p1 == *p3)
|
|
|
|
others_exists = TRUE;
|
|
|
|
|
|
|
|
p3++;
|
|
|
|
}
|
|
|
|
}
|
2013-04-13 01:05:53 +08:00
|
|
|
|
|
|
|
p1++;
|
|
|
|
}
|
|
|
|
|
2015-11-07 19:59:32 +08:00
|
|
|
/* Does the input contain any "atom-special" characters? */
|
|
|
|
if(!backsp_count && !quote_count && !others_exists)
|
2013-04-13 01:05:53 +08:00
|
|
|
return strdup(str);
|
|
|
|
|
|
|
|
/* Calculate the new string length */
|
2017-09-21 15:17:06 +08:00
|
|
|
newlen = strlen(str) + backsp_count + quote_count + (escape_only ? 0 : 2);
|
2013-04-13 01:05:53 +08:00
|
|
|
|
|
|
|
/* Allocate the new string */
|
|
|
|
newstr = (char *) malloc((newlen + 1) * sizeof(char));
|
|
|
|
if(!newstr)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Surround the string in quotes if necessary */
|
|
|
|
p2 = newstr;
|
2017-09-21 15:17:06 +08:00
|
|
|
if(!escape_only) {
|
2013-04-13 01:05:53 +08:00
|
|
|
newstr[0] = '"';
|
|
|
|
newstr[newlen - 1] = '"';
|
|
|
|
p2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the string, escaping backslash and quote characters along the way */
|
|
|
|
p1 = str;
|
|
|
|
while(*p1) {
|
|
|
|
if(*p1 == '\\' || *p1 == '"') {
|
|
|
|
*p2 = '\\';
|
|
|
|
p2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p2 = *p1;
|
|
|
|
|
|
|
|
p1++;
|
|
|
|
p2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate the string */
|
|
|
|
newstr[newlen] = '\0';
|
|
|
|
|
|
|
|
return newstr;
|
|
|
|
}
|
|
|
|
|
2013-04-13 01:12:11 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_is_bchar()
|
|
|
|
*
|
|
|
|
* Portable test of whether the specified char is a "bchar" as defined in the
|
|
|
|
* grammar of RFC-5092.
|
|
|
|
*/
|
|
|
|
static bool imap_is_bchar(char ch)
|
|
|
|
{
|
2022-08-31 22:31:01 +08:00
|
|
|
/* Performing the alnum check with this macro is faster because of ASCII
|
|
|
|
arithmetic */
|
2022-08-10 15:35:38 +08:00
|
|
|
if(ISALNUM(ch))
|
|
|
|
return true;
|
|
|
|
|
2013-04-13 01:12:11 +08:00
|
|
|
switch(ch) {
|
|
|
|
/* bchar */
|
|
|
|
case ':': case '@': case '/':
|
|
|
|
/* bchar -> achar */
|
|
|
|
case '&': case '=':
|
2022-08-10 15:35:38 +08:00
|
|
|
/* bchar -> achar -> uchar -> unreserved (without alphanumeric) */
|
2013-04-13 01:12:11 +08:00
|
|
|
case '-': case '.': case '_': case '~':
|
|
|
|
/* bchar -> achar -> uchar -> sub-delims-sh */
|
|
|
|
case '!': case '$': case '\'': case '(': case ')': case '*':
|
|
|
|
case '+': case ',':
|
|
|
|
/* bchar -> achar -> uchar -> pct-encoded */
|
|
|
|
case '%': /* HEXDIG chars are already included above */
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-13 22:58:15 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_parse_url_options()
|
|
|
|
*
|
|
|
|
* Parse the URL login options.
|
|
|
|
*/
|
|
|
|
static CURLcode imap_parse_url_options(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
struct imap_conn *imapc = &conn->proto.imapc;
|
2015-01-20 22:27:25 +08:00
|
|
|
const char *ptr = conn->options;
|
|
|
|
|
|
|
|
while(!result && ptr && *ptr) {
|
2013-04-13 22:58:15 +08:00
|
|
|
const char *key = ptr;
|
2015-01-20 22:27:25 +08:00
|
|
|
const char *value;
|
2013-04-13 22:58:15 +08:00
|
|
|
|
|
|
|
while(*ptr && *ptr != '=')
|
|
|
|
ptr++;
|
|
|
|
|
2015-01-20 22:27:25 +08:00
|
|
|
value = ptr + 1;
|
2013-12-22 07:34:45 +08:00
|
|
|
|
2015-01-20 22:27:25 +08:00
|
|
|
while(*ptr && *ptr != ';')
|
|
|
|
ptr++;
|
2013-12-22 07:34:45 +08:00
|
|
|
|
2016-09-30 23:15:05 +08:00
|
|
|
if(strncasecompare(key, "AUTH=", 5))
|
2015-01-20 22:27:25 +08:00
|
|
|
result = Curl_sasl_parse_url_auth_option(&imapc->sasl,
|
|
|
|
value, ptr - value);
|
2013-04-13 22:58:15 +08:00
|
|
|
else
|
|
|
|
result = CURLE_URL_MALFORMAT;
|
2015-01-20 22:27:25 +08:00
|
|
|
|
|
|
|
if(*ptr == ';')
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(imapc->sasl.prefmech) {
|
|
|
|
case SASL_AUTH_NONE:
|
|
|
|
imapc->preftype = IMAP_TYPE_NONE;
|
|
|
|
break;
|
2015-01-28 00:24:55 +08:00
|
|
|
case SASL_AUTH_DEFAULT:
|
2015-01-20 22:27:25 +08:00
|
|
|
imapc->preftype = IMAP_TYPE_ANY;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
imapc->preftype = IMAP_TYPE_SASL;
|
|
|
|
break;
|
2013-04-13 22:58:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-04-13 01:08:09 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_parse_url_path()
|
|
|
|
*
|
|
|
|
* Parse the URL path into separate path components.
|
|
|
|
*
|
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_parse_url_path(struct Curl_easy *data)
|
2013-04-13 01:08:09 +08:00
|
|
|
{
|
|
|
|
/* The imap struct is already initialised in imap_connect() */
|
|
|
|
CURLcode result = CURLE_OK;
|
2020-11-23 15:32:41 +08:00
|
|
|
struct IMAP *imap = data->req.p.imap;
|
2018-09-15 05:33:28 +08:00
|
|
|
const char *begin = &data->state.up.path[1]; /* skip leading slash */
|
2013-04-13 01:08:09 +08:00
|
|
|
const char *ptr = begin;
|
|
|
|
|
|
|
|
/* See how much of the URL is a valid path and decode it */
|
|
|
|
while(imap_is_bchar(*ptr))
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
if(ptr != begin) {
|
|
|
|
/* Remove the trailing slash if present */
|
|
|
|
const char *end = ptr;
|
|
|
|
if(end > begin && end[-1] == '/')
|
|
|
|
end--;
|
|
|
|
|
2022-02-03 20:04:30 +08:00
|
|
|
result = Curl_urldecode(begin, end - begin, &imap->mailbox, NULL,
|
2020-06-23 22:13:50 +08:00
|
|
|
REJECT_CTRL);
|
2013-04-13 01:08:09 +08:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
imap->mailbox = NULL;
|
|
|
|
|
|
|
|
/* There can be any number of parameters in the form ";NAME=VALUE" */
|
|
|
|
while(*ptr == ';') {
|
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
size_t valuelen;
|
|
|
|
|
|
|
|
/* Find the length of the name parameter */
|
|
|
|
begin = ++ptr;
|
|
|
|
while(*ptr && *ptr != '=')
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
if(!*ptr)
|
|
|
|
return CURLE_URL_MALFORMAT;
|
|
|
|
|
|
|
|
/* Decode the name parameter */
|
2022-02-03 20:04:30 +08:00
|
|
|
result = Curl_urldecode(begin, ptr - begin, &name, NULL,
|
2020-06-23 22:13:50 +08:00
|
|
|
REJECT_CTRL);
|
2013-04-13 01:08:09 +08:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
/* Find the length of the value parameter */
|
|
|
|
begin = ++ptr;
|
|
|
|
while(imap_is_bchar(*ptr))
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
/* Decode the value parameter */
|
2022-02-03 20:04:30 +08:00
|
|
|
result = Curl_urldecode(begin, ptr - begin, &value, &valuelen,
|
2020-06-23 22:13:50 +08:00
|
|
|
REJECT_CTRL);
|
2013-04-13 01:08:09 +08:00
|
|
|
if(result) {
|
2015-03-16 22:01:15 +08:00
|
|
|
free(name);
|
2013-04-13 01:08:09 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-07-06 23:05:17 +08:00
|
|
|
DEBUGF(infof(data, "IMAP URL parameter '%s' = '%s'", name, value));
|
2013-04-13 01:08:09 +08:00
|
|
|
|
2014-04-19 00:42:40 +08:00
|
|
|
/* Process the known hierarchical parameters (UIDVALIDITY, UID, SECTION and
|
|
|
|
PARTIAL) stripping of the trailing slash character if it is present.
|
2013-04-13 01:08:09 +08:00
|
|
|
|
|
|
|
Note: Unknown parameters trigger a URL_MALFORMAT error. */
|
2016-10-01 00:54:02 +08:00
|
|
|
if(strcasecompare(name, "UIDVALIDITY") && !imap->uidvalidity) {
|
2013-04-13 01:08:09 +08:00
|
|
|
if(valuelen > 0 && value[valuelen - 1] == '/')
|
|
|
|
value[valuelen - 1] = '\0';
|
|
|
|
|
|
|
|
imap->uidvalidity = value;
|
|
|
|
value = NULL;
|
|
|
|
}
|
2016-10-01 00:54:02 +08:00
|
|
|
else if(strcasecompare(name, "UID") && !imap->uid) {
|
2013-04-13 01:08:09 +08:00
|
|
|
if(valuelen > 0 && value[valuelen - 1] == '/')
|
|
|
|
value[valuelen - 1] = '\0';
|
|
|
|
|
|
|
|
imap->uid = value;
|
|
|
|
value = NULL;
|
|
|
|
}
|
2018-07-31 19:12:18 +08:00
|
|
|
else if(strcasecompare(name, "MAILINDEX") && !imap->mindex) {
|
|
|
|
if(valuelen > 0 && value[valuelen - 1] == '/')
|
|
|
|
value[valuelen - 1] = '\0';
|
|
|
|
|
|
|
|
imap->mindex = value;
|
|
|
|
value = NULL;
|
|
|
|
}
|
2016-10-01 00:54:02 +08:00
|
|
|
else if(strcasecompare(name, "SECTION") && !imap->section) {
|
2013-04-13 01:08:09 +08:00
|
|
|
if(valuelen > 0 && value[valuelen - 1] == '/')
|
|
|
|
value[valuelen - 1] = '\0';
|
|
|
|
|
|
|
|
imap->section = value;
|
|
|
|
value = NULL;
|
|
|
|
}
|
2016-10-01 00:54:02 +08:00
|
|
|
else if(strcasecompare(name, "PARTIAL") && !imap->partial) {
|
2014-04-19 00:42:40 +08:00
|
|
|
if(valuelen > 0 && value[valuelen - 1] == '/')
|
|
|
|
value[valuelen - 1] = '\0';
|
|
|
|
|
|
|
|
imap->partial = value;
|
|
|
|
value = NULL;
|
|
|
|
}
|
2013-04-13 01:08:09 +08:00
|
|
|
else {
|
2015-03-16 22:01:15 +08:00
|
|
|
free(name);
|
|
|
|
free(value);
|
2013-04-13 01:08:09 +08:00
|
|
|
|
|
|
|
return CURLE_URL_MALFORMAT;
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:01:15 +08:00
|
|
|
free(name);
|
|
|
|
free(value);
|
2013-04-13 01:08:09 +08:00
|
|
|
}
|
|
|
|
|
2014-04-18 22:58:33 +08:00
|
|
|
/* Does the URL contain a query parameter? Only valid when we have a mailbox
|
|
|
|
and no UID as per RFC-5092 */
|
2018-09-15 05:33:28 +08:00
|
|
|
if(imap->mailbox && !imap->uid && !imap->mindex) {
|
|
|
|
/* Get the query parameter, URL decoded */
|
|
|
|
(void)curl_url_get(data->state.uh, CURLUPART_QUERY, &imap->query,
|
|
|
|
CURLU_URLDECODE);
|
2014-04-18 22:58:33 +08:00
|
|
|
}
|
|
|
|
|
2013-04-13 01:08:09 +08:00
|
|
|
/* Any extra stuff at the end of the URL is an error */
|
|
|
|
if(*ptr)
|
|
|
|
return CURLE_URL_MALFORMAT;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-13 01:20:20 +08:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* imap_parse_custom_request()
|
|
|
|
*
|
|
|
|
* Parse the custom request.
|
|
|
|
*/
|
2021-01-09 00:58:15 +08:00
|
|
|
static CURLcode imap_parse_custom_request(struct Curl_easy *data)
|
2013-04-13 01:08:09 +08:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2020-11-23 15:32:41 +08:00
|
|
|
struct IMAP *imap = data->req.p.imap;
|
2013-04-13 01:08:09 +08:00
|
|
|
const char *custom = data->set.str[STRING_CUSTOMREQUEST];
|
|
|
|
|
|
|
|
if(custom) {
|
|
|
|
/* URL decode the custom request */
|
2022-02-03 20:04:30 +08:00
|
|
|
result = Curl_urldecode(custom, 0, &imap->custom, NULL, REJECT_CTRL);
|
2013-04-13 01:08:09 +08:00
|
|
|
|
|
|
|
/* Extract the parameters if specified */
|
|
|
|
if(!result) {
|
|
|
|
const char *params = imap->custom;
|
|
|
|
|
|
|
|
while(*params && *params != ' ')
|
|
|
|
params++;
|
|
|
|
|
|
|
|
if(*params) {
|
|
|
|
imap->custom_params = strdup(params);
|
|
|
|
imap->custom[params - imap->custom] = '\0';
|
|
|
|
|
|
|
|
if(!imap->custom_params)
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-12-13 05:54:01 +08:00
|
|
|
#endif /* CURL_DISABLE_IMAP */
|