Coverage Report

Created: 2025-11-24 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PROJ/curl/lib/pop3.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 * RFC1734 POP3 Authentication
24
 * RFC1939 POP3 protocol
25
 * RFC2195 CRAM-MD5 authentication
26
 * RFC2384 POP URL Scheme
27
 * RFC2449 POP3 Extension Mechanism
28
 * RFC2595 Using TLS with IMAP, POP3 and ACAP
29
 * RFC2831 DIGEST-MD5 authentication
30
 * RFC4422 Simple Authentication and Security Layer (SASL)
31
 * RFC4616 PLAIN authentication
32
 * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
33
 * RFC5034 POP3 SASL Authentication Mechanism
34
 * RFC6749 OAuth 2.0 Authorization Framework
35
 * RFC8314 Use of TLS for Email Submission and Access
36
 * Draft   LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
37
 *
38
 ***************************************************************************/
39
40
#include "curl_setup.h"
41
42
#ifndef CURL_DISABLE_POP3
43
44
#ifdef HAVE_NETINET_IN_H
45
#include <netinet/in.h>
46
#endif
47
#ifdef HAVE_ARPA_INET_H
48
#include <arpa/inet.h>
49
#endif
50
#ifdef HAVE_NETDB_H
51
#include <netdb.h>
52
#endif
53
#ifdef __VMS
54
#include <in.h>
55
#include <inet.h>
56
#endif
57
58
#include <curl/curl.h>
59
#include "urldata.h"
60
#include "sendf.h"
61
#include "hostip.h"
62
#include "progress.h"
63
#include "transfer.h"
64
#include "escape.h"
65
#include "http.h" /* for HTTP proxy tunnel stuff */
66
#include "socks.h"
67
#include "pingpong.h"
68
#include "pop3.h"
69
#include "vtls/vtls.h"
70
#include "cfilters.h"
71
#include "connect.h"
72
#include "select.h"
73
#include "multiif.h"
74
#include "url.h"
75
#include "bufref.h"
76
#include "curl_sasl.h"
77
#include "curl_md5.h"
78
#include "curlx/warnless.h"
79
#include "strdup.h"
80
81
/* The last 2 #include files should be in this order */
82
#include "curl_memory.h"
83
#include "memdebug.h"
84
85
/* Authentication type flags */
86
0
#define POP3_TYPE_CLEARTEXT (1 << 0)
87
0
#define POP3_TYPE_APOP      (1 << 1)
88
0
#define POP3_TYPE_SASL      (1 << 2)
89
90
/* Authentication type values */
91
0
#define POP3_TYPE_NONE      0
92
0
#define POP3_TYPE_ANY       (POP3_TYPE_CLEARTEXT|POP3_TYPE_APOP|POP3_TYPE_SASL)
93
94
/* This is the 5-bytes End-Of-Body marker for POP3 */
95
0
#define POP3_EOB "\x0d\x0a\x2e\x0d\x0a"
96
0
#define POP3_EOB_LEN 5
97
98
/* meta key for storing protocol meta at easy handle */
99
0
#define CURL_META_POP3_EASY   "meta:proto:pop3:easy"
100
/* meta key for storing protocol meta at connection */
101
0
#define CURL_META_POP3_CONN   "meta:proto:pop3:conn"
102
103
/*
104
 * POP3 easy handle state
105
 */
106
struct POP3 {
107
  curl_pp_transfer transfer;
108
  char *id;               /* Message ID */
109
  char *custom;           /* Custom Request */
110
};
111
112
/*
113
 * POP3 connection state
114
 */
115
typedef enum {
116
  POP3_STOP,         /* do nothing state, stops the state machine */
117
  POP3_SERVERGREET,  /* waiting for the initial greeting immediately after
118
                        a connect */
119
  POP3_CAPA,
120
  POP3_STARTTLS,
121
  POP3_UPGRADETLS,   /* asynchronously upgrade the connection to SSL/TLS
122
                       (multi mode only) */
123
  POP3_AUTH,
124
  POP3_APOP,
125
  POP3_USER,
126
  POP3_PASS,
127
  POP3_COMMAND,
128
  POP3_QUIT,
129
  POP3_LAST          /* never used */
130
} pop3state;
131
132
struct pop3_conn {
133
  struct pingpong pp;
134
  pop3state state;        /* Always use pop3.c:state() to change state! */
135
  size_t eob;             /* Number of bytes of the EOB (End Of Body) that
136
                             have been received so far */
137
  size_t strip;           /* Number of bytes from the start to ignore as
138
                             non-body */
139
  struct SASL sasl;       /* SASL-related storage */
140
  char *apoptimestamp;    /* APOP timestamp from the server greeting */
141
  unsigned char authtypes; /* Accepted authentication types */
142
  unsigned char preftype;  /* Preferred authentication type */
143
  BIT(ssldone);           /* Is connect() over SSL done? */
144
  BIT(tls_supported);     /* StartTLS capability supported by server */
145
};
146
147
/* Local API functions */
148
static CURLcode pop3_regular_transfer(struct Curl_easy *data, bool *done);
149
static CURLcode pop3_do(struct Curl_easy *data, bool *done);
150
static CURLcode pop3_done(struct Curl_easy *data, CURLcode status,
151
                          bool premature);
152
static CURLcode pop3_connect(struct Curl_easy *data, bool *done);
153
static CURLcode pop3_disconnect(struct Curl_easy *data,
154
                                struct connectdata *conn, bool dead);
155
static CURLcode pop3_multi_statemach(struct Curl_easy *data, bool *done);
156
static CURLcode pop3_pollset(struct Curl_easy *data,
157
                             struct easy_pollset *ps);
158
static CURLcode pop3_doing(struct Curl_easy *data, bool *dophase_done);
159
static CURLcode pop3_setup_connection(struct Curl_easy *data,
160
                                      struct connectdata *conn);
161
static CURLcode pop3_parse_url_options(struct connectdata *conn);
162
static CURLcode pop3_parse_url_path(struct Curl_easy *data);
163
static CURLcode pop3_parse_custom_request(struct Curl_easy *data);
164
static CURLcode pop3_perform_auth(struct Curl_easy *data, const char *mech,
165
                                  const struct bufref *initresp);
166
static CURLcode pop3_continue_auth(struct Curl_easy *data, const char *mech,
167
                                   const struct bufref *resp);
168
static CURLcode pop3_cancel_auth(struct Curl_easy *data, const char *mech);
169
static CURLcode pop3_get_message(struct Curl_easy *data, struct bufref *out);
170
171
/* This function scans the body after the end-of-body and writes everything
172
 * until the end is found */
173
static CURLcode pop3_write(struct Curl_easy *data,
174
                           const char *str, size_t nread, bool is_eos);
175
176
/*
177
 * POP3 protocol handler.
178
 */
179
180
const struct Curl_handler Curl_handler_pop3 = {
181
  "pop3",                           /* scheme */
182
  pop3_setup_connection,            /* setup_connection */
183
  pop3_do,                          /* do_it */
184
  pop3_done,                        /* done */
185
  ZERO_NULL,                        /* do_more */
186
  pop3_connect,                     /* connect_it */
187
  pop3_multi_statemach,             /* connecting */
188
  pop3_doing,                       /* doing */
189
  pop3_pollset,                     /* proto_pollset */
190
  pop3_pollset,                     /* doing_pollset */
191
  ZERO_NULL,                        /* domore_pollset */
192
  ZERO_NULL,                        /* perform_pollset */
193
  pop3_disconnect,                  /* disconnect */
194
  pop3_write,                       /* write_resp */
195
  ZERO_NULL,                        /* write_resp_hd */
196
  ZERO_NULL,                        /* connection_check */
197
  ZERO_NULL,                        /* attach connection */
198
  ZERO_NULL,                        /* follow */
199
  PORT_POP3,                        /* defport */
200
  CURLPROTO_POP3,                   /* protocol */
201
  CURLPROTO_POP3,                   /* family */
202
  PROTOPT_CLOSEACTION | PROTOPT_NOURLQUERY | /* flags */
203
  PROTOPT_URLOPTIONS | PROTOPT_SSL_REUSE
204
};
205
206
#ifdef USE_SSL
207
/*
208
 * POP3S protocol handler.
209
 */
210
211
const struct Curl_handler Curl_handler_pop3s = {
212
  "pop3s",                          /* scheme */
213
  pop3_setup_connection,            /* setup_connection */
214
  pop3_do,                          /* do_it */
215
  pop3_done,                        /* done */
216
  ZERO_NULL,                        /* do_more */
217
  pop3_connect,                     /* connect_it */
218
  pop3_multi_statemach,             /* connecting */
219
  pop3_doing,                       /* doing */
220
  pop3_pollset,                     /* proto_pollset */
221
  pop3_pollset,                     /* doing_pollset */
222
  ZERO_NULL,                        /* domore_pollset */
223
  ZERO_NULL,                        /* perform_pollset */
224
  pop3_disconnect,                  /* disconnect */
225
  pop3_write,                       /* write_resp */
226
  ZERO_NULL,                        /* write_resp_hd */
227
  ZERO_NULL,                        /* connection_check */
228
  ZERO_NULL,                        /* attach connection */
229
  ZERO_NULL,                        /* follow */
230
  PORT_POP3S,                       /* defport */
231
  CURLPROTO_POP3S,                  /* protocol */
232
  CURLPROTO_POP3,                   /* family */
233
  PROTOPT_CLOSEACTION | PROTOPT_SSL
234
  | PROTOPT_NOURLQUERY | PROTOPT_URLOPTIONS /* flags */
235
};
236
#endif
237
238
/* SASL parameters for the pop3 protocol */
239
static const struct SASLproto saslpop3 = {
240
  "pop",                /* The service name */
241
  pop3_perform_auth,    /* Send authentication command */
242
  pop3_continue_auth,   /* Send authentication continuation */
243
  pop3_cancel_auth,     /* Send authentication cancellation */
244
  pop3_get_message,     /* Get SASL response message */
245
  255 - 8,              /* Max line len - strlen("AUTH ") - 1 space - crlf */
246
  '*',                  /* Code received when continuation is expected */
247
  '+',                  /* Code to receive upon authentication success */
248
  SASL_AUTH_DEFAULT,    /* Default mechanisms */
249
  SASL_FLAG_BASE64      /* Configuration flags */
250
};
251
252
struct pop3_cmd {
253
  const char *name;
254
  unsigned short nlen;
255
  BIT(multiline); /* response is multi-line with last '.' line */
256
  BIT(multiline_with_args); /* is multi-line when command has args */
257
};
258
259
static const struct pop3_cmd pop3cmds[] = {
260
  { "APOP", 4, FALSE, FALSE },
261
  { "AUTH", 4, FALSE, FALSE },
262
  { "CAPA", 4, TRUE, TRUE },
263
  { "DELE", 4, FALSE, FALSE },
264
  { "LIST", 4, TRUE, FALSE },
265
  { "MSG",  3, TRUE, TRUE },
266
  { "NOOP", 4, FALSE, FALSE },
267
  { "PASS", 4, FALSE, FALSE },
268
  { "QUIT", 4, FALSE, FALSE },
269
  { "RETR", 4, TRUE, TRUE },
270
  { "RSET", 4, FALSE, FALSE },
271
  { "STAT", 4, FALSE, FALSE },
272
  { "STLS", 4, FALSE, FALSE },
273
  { "TOP",  3, TRUE, TRUE },
274
  { "UIDL", 4, TRUE, FALSE },
275
  { "USER", 4, FALSE, FALSE },
276
  { "UTF8", 4, FALSE, FALSE },
277
  { "XTND", 4, TRUE, TRUE },
278
};
279
280
/* Return iff a command is defined as "multi-line" (RFC 1939),
281
 * has a response terminated by a last line with a '.'.
282
 */
283
static bool pop3_is_multiline(const char *cmdline)
284
0
{
285
0
  size_t i;
286
0
  for(i = 0; i < CURL_ARRAYSIZE(pop3cmds); ++i) {
287
0
    if(curl_strnequal(pop3cmds[i].name, cmdline, pop3cmds[i].nlen)) {
288
0
      if(!cmdline[pop3cmds[i].nlen])
289
0
        return pop3cmds[i].multiline;
290
0
      else if(cmdline[pop3cmds[i].nlen] == ' ')
291
0
        return pop3cmds[i].multiline_with_args;
292
0
    }
293
0
  }
294
  /* Unknown command, assume multi-line for backward compatibility with
295
   * earlier curl versions that only could do multi-line responses. */
296
0
  return TRUE;
297
0
}
298
299
/***********************************************************************
300
 *
301
 * pop3_endofresp()
302
 *
303
 * Checks for an ending POP3 status code at the start of the given string, but
304
 * also detects the APOP timestamp from the server greeting and various
305
 * capabilities from the CAPA response including the supported authentication
306
 * types and allowed SASL mechanisms.
307
 */
308
static bool pop3_endofresp(struct Curl_easy *data, struct connectdata *conn,
309
                           const char *line, size_t len, int *resp)
310
0
{
311
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
312
0
  (void)data;
313
0
  DEBUGASSERT(pop3c);
314
0
  if(!pop3c) /* internal error */
315
0
    return TRUE;
316
317
  /* Do we have an error response? */
318
0
  if(len >= 4 && !memcmp("-ERR", line, 4)) {
319
0
    *resp = '-';
320
321
0
    return TRUE;
322
0
  }
323
324
  /* Are we processing CAPA command responses? */
325
0
  if(pop3c->state == POP3_CAPA) {
326
    /* Do we have the terminating line? Per RFC 2449 this is a line
327
       containing only a single dot */
328
0
    if((len == 3 && line[0] == '.' && line[1] == '\r') ||
329
0
       (len == 2 && line[0] == '.' && line[1] == '\n'))
330
      /* Treat the response as a success */
331
0
      *resp = '+';
332
0
    else
333
      /* Treat the response as an untagged continuation */
334
0
      *resp = '*';
335
336
0
    return TRUE;
337
0
  }
338
339
  /* Do we have a success response? */
340
0
  if(len >= 3 && !memcmp("+OK", line, 3)) {
341
0
    *resp = '+';
342
343
0
    return TRUE;
344
0
  }
345
346
  /* Do we have a continuation response? */
347
0
  if(len >= 1 && line[0] == '+') {
348
0
    *resp = '*';
349
350
0
    return TRUE;
351
0
  }
352
353
0
  return FALSE; /* Nothing for us */
354
0
}
355
356
/***********************************************************************
357
 *
358
 * pop3_get_message()
359
 *
360
 * Gets the authentication message from the response buffer.
361
 */
362
static CURLcode pop3_get_message(struct Curl_easy *data, struct bufref *out)
363
0
{
364
0
  struct pop3_conn *pop3c =
365
0
    Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN);
366
0
  char *message;
367
0
  size_t len;
368
369
0
  if(!pop3c)
370
0
    return CURLE_FAILED_INIT;
371
0
  message = curlx_dyn_ptr(&pop3c->pp.recvbuf);
372
0
  len = pop3c->pp.nfinal;
373
0
  if(len > 2) {
374
    /* Find the start of the message */
375
0
    len -= 2;
376
0
    for(message += 2; *message == ' ' || *message == '\t'; message++, len--)
377
0
      ;
378
379
    /* Find the end of the message */
380
0
    while(len--)
381
0
      if(message[len] != '\r' && message[len] != '\n' && message[len] != ' ' &&
382
0
         message[len] != '\t')
383
0
        break;
384
385
    /* Terminate the message */
386
0
    message[++len] = '\0';
387
0
    Curl_bufref_set(out, message, len, NULL);
388
0
  }
389
0
  else
390
    /* junk input => zero length output */
391
0
    Curl_bufref_set(out, "", 0, NULL);
392
393
0
  return CURLE_OK;
394
0
}
395
396
/***********************************************************************
397
 *
398
 * pop3_state()
399
 *
400
 * This is the ONLY way to change POP3 state!
401
 */
402
static void pop3_state(struct Curl_easy *data, pop3state newstate)
403
0
{
404
0
  struct pop3_conn *pop3c =
405
0
    Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN);
406
0
  if(pop3c) {
407
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
408
    /* for debug purposes */
409
    static const char * const names[] = {
410
      "STOP",
411
      "SERVERGREET",
412
      "CAPA",
413
      "STARTTLS",
414
      "UPGRADETLS",
415
      "AUTH",
416
      "APOP",
417
      "USER",
418
      "PASS",
419
      "COMMAND",
420
      "QUIT",
421
      /* LAST */
422
    };
423
424
    if(pop3c->state != newstate)
425
      infof(data, "POP3 %p state change from %s to %s",
426
            (void *)pop3c, names[pop3c->state], names[newstate]);
427
#endif
428
429
0
    pop3c->state = newstate;
430
0
  }
431
0
}
432
433
/***********************************************************************
434
 *
435
 * pop3_perform_capa()
436
 *
437
 * Sends the CAPA command in order to obtain a list of server side supported
438
 * capabilities.
439
 */
440
static CURLcode pop3_perform_capa(struct Curl_easy *data,
441
                                  struct connectdata *conn)
442
0
{
443
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
444
0
  CURLcode result = CURLE_OK;
445
446
0
  if(!pop3c)
447
0
    return CURLE_FAILED_INIT;
448
449
0
  pop3c->sasl.authmechs = SASL_AUTH_NONE; /* No known auth. mechanisms yet */
450
0
  pop3c->sasl.authused = SASL_AUTH_NONE;  /* Clear the auth. mechanism used */
451
0
  pop3c->tls_supported = FALSE;           /* Clear the TLS capability */
452
453
  /* Send the CAPA command */
454
0
  result = Curl_pp_sendf(data, &pop3c->pp, "%s", "CAPA");
455
456
0
  if(!result)
457
0
    pop3_state(data, POP3_CAPA);
458
459
0
  return result;
460
0
}
461
462
/***********************************************************************
463
 *
464
 * pop3_perform_starttls()
465
 *
466
 * Sends the STLS command to start the upgrade to TLS.
467
 */
468
static CURLcode pop3_perform_starttls(struct Curl_easy *data,
469
                                      struct connectdata *conn)
470
0
{
471
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
472
0
  CURLcode result;
473
474
0
  if(!pop3c)
475
0
    return CURLE_FAILED_INIT;
476
477
  /* Send the STLS command */
478
0
  result = Curl_pp_sendf(data, &pop3c->pp, "%s", "STLS");
479
0
  if(!result)
480
0
    pop3_state(data, POP3_STARTTLS);
481
482
0
  return result;
483
0
}
484
485
/***********************************************************************
486
 *
487
 * pop3_perform_upgrade_tls()
488
 *
489
 * Performs the upgrade to TLS.
490
 */
491
static CURLcode pop3_perform_upgrade_tls(struct Curl_easy *data,
492
                                         struct connectdata *conn)
493
0
{
494
0
#ifdef USE_SSL
495
  /* Start the SSL connection */
496
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
497
0
  CURLcode result;
498
0
  bool ssldone = FALSE;
499
500
0
  if(!pop3c)
501
0
    return CURLE_FAILED_INIT;
502
503
0
  if(!Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
504
0
    result = Curl_ssl_cfilter_add(data, conn, FIRSTSOCKET);
505
0
    if(result)
506
0
      goto out;
507
    /* Change the connection handler */
508
0
    conn->handler = &Curl_handler_pop3s;
509
0
  }
510
511
0
  DEBUGASSERT(!pop3c->ssldone);
512
0
  result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone);
513
0
  DEBUGF(infof(data, "pop3_perform_upgrade_tls, connect -> %d, %d",
514
0
         result, ssldone));
515
0
  if(!result && ssldone) {
516
0
    pop3c->ssldone = ssldone;
517
     /* perform CAPA now, changes pop3c->state out of POP3_UPGRADETLS */
518
0
    result = pop3_perform_capa(data, conn);
519
0
  }
520
0
out:
521
0
  return result;
522
#else
523
  (void)data;
524
  (void)conn;
525
  return CURLE_NOT_BUILT_IN;
526
#endif
527
0
}
528
529
/***********************************************************************
530
 *
531
 * pop3_perform_user()
532
 *
533
 * Sends a clear text USER command to authenticate with.
534
 */
535
static CURLcode pop3_perform_user(struct Curl_easy *data,
536
                                  struct connectdata *conn)
537
0
{
538
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
539
0
  CURLcode result = CURLE_OK;
540
541
0
  if(!pop3c)
542
0
    return CURLE_FAILED_INIT;
543
544
  /* Check we have a username and password to authenticate with and end the
545
     connect phase if we do not */
546
0
  if(!data->state.aptr.user) {
547
0
    pop3_state(data, POP3_STOP);
548
549
0
    return result;
550
0
  }
551
552
  /* Send the USER command */
553
0
  result = Curl_pp_sendf(data, &pop3c->pp, "USER %s",
554
0
                         conn->user ? conn->user : "");
555
0
  if(!result)
556
0
    pop3_state(data, POP3_USER);
557
558
0
  return result;
559
0
}
560
561
#ifndef CURL_DISABLE_DIGEST_AUTH
562
/***********************************************************************
563
 *
564
 * pop3_perform_apop()
565
 *
566
 * Sends an APOP command to authenticate with.
567
 */
568
static CURLcode pop3_perform_apop(struct Curl_easy *data,
569
                                  struct connectdata *conn)
570
0
{
571
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
572
0
  CURLcode result = CURLE_OK;
573
0
  size_t i;
574
0
  struct MD5_context *ctxt;
575
0
  unsigned char digest[MD5_DIGEST_LEN];
576
0
  char secret[2 * MD5_DIGEST_LEN + 1];
577
578
0
  if(!pop3c)
579
0
    return CURLE_FAILED_INIT;
580
581
  /* Check we have a username and password to authenticate with and end the
582
     connect phase if we do not */
583
0
  if(!data->state.aptr.user) {
584
0
    pop3_state(data, POP3_STOP);
585
586
0
    return result;
587
0
  }
588
589
  /* Create the digest */
590
0
  ctxt = Curl_MD5_init(&Curl_DIGEST_MD5);
591
0
  if(!ctxt)
592
0
    return CURLE_OUT_OF_MEMORY;
593
594
0
  Curl_MD5_update(ctxt, (const unsigned char *) pop3c->apoptimestamp,
595
0
                  curlx_uztoui(strlen(pop3c->apoptimestamp)));
596
597
0
  Curl_MD5_update(ctxt, (const unsigned char *) conn->passwd,
598
0
                  curlx_uztoui(strlen(conn->passwd)));
599
600
  /* Finalise the digest */
601
0
  Curl_MD5_final(ctxt, digest);
602
603
  /* Convert the calculated 16 octet digest into a 32 byte hex string */
604
0
  for(i = 0; i < MD5_DIGEST_LEN; i++)
605
0
    curl_msnprintf(&secret[2 * i], 3, "%02x", digest[i]);
606
607
0
  result = Curl_pp_sendf(data, &pop3c->pp, "APOP %s %s", conn->user, secret);
608
609
0
  if(!result)
610
0
    pop3_state(data, POP3_APOP);
611
612
0
  return result;
613
0
}
614
#endif
615
616
/***********************************************************************
617
 *
618
 * pop3_perform_auth()
619
 *
620
 * Sends an AUTH command allowing the client to login with the given SASL
621
 * authentication mechanism.
622
 */
623
static CURLcode pop3_perform_auth(struct Curl_easy *data,
624
                                  const char *mech,
625
                                  const struct bufref *initresp)
626
0
{
627
0
  struct pop3_conn *pop3c =
628
0
    Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN);
629
0
  CURLcode result = CURLE_OK;
630
0
  const char *ir = (const char *) Curl_bufref_ptr(initresp);
631
632
0
  if(!pop3c)
633
0
    return CURLE_FAILED_INIT;
634
635
0
  if(ir) {                                  /* AUTH <mech> ...<crlf> */
636
    /* Send the AUTH command with the initial response */
637
0
    result = Curl_pp_sendf(data, &pop3c->pp, "AUTH %s %s", mech, ir);
638
0
  }
639
0
  else {
640
    /* Send the AUTH command */
641
0
    result = Curl_pp_sendf(data, &pop3c->pp, "AUTH %s", mech);
642
0
  }
643
644
0
  return result;
645
0
}
646
647
/***********************************************************************
648
 *
649
 * pop3_continue_auth()
650
 *
651
 * Sends SASL continuation data.
652
 */
653
static CURLcode pop3_continue_auth(struct Curl_easy *data,
654
                                   const char *mech,
655
                                   const struct bufref *resp)
656
0
{
657
0
  struct pop3_conn *pop3c =
658
0
    Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN);
659
660
0
  (void)mech;
661
0
  if(!pop3c)
662
0
    return CURLE_FAILED_INIT;
663
664
0
  return Curl_pp_sendf(data, &pop3c->pp,
665
0
                       "%s", (const char *) Curl_bufref_ptr(resp));
666
0
}
667
668
/***********************************************************************
669
 *
670
 * pop3_cancel_auth()
671
 *
672
 * Sends SASL cancellation.
673
 */
674
static CURLcode pop3_cancel_auth(struct Curl_easy *data, const char *mech)
675
0
{
676
0
  struct pop3_conn *pop3c =
677
0
    Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN);
678
679
0
  (void)mech;
680
0
  if(!pop3c)
681
0
    return CURLE_FAILED_INIT;
682
683
0
  return Curl_pp_sendf(data, &pop3c->pp, "*");
684
0
}
685
686
/***********************************************************************
687
 *
688
 * pop3_perform_authentication()
689
 *
690
 * Initiates the authentication sequence, with the appropriate SASL
691
 * authentication mechanism, falling back to APOP and clear text should a
692
 * common mechanism not be available between the client and server.
693
 */
694
static CURLcode pop3_perform_authentication(struct Curl_easy *data,
695
                                            struct connectdata *conn)
696
0
{
697
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
698
0
  CURLcode result = CURLE_OK;
699
0
  saslprogress progress = SASL_IDLE;
700
701
0
  if(!pop3c)
702
0
    return CURLE_FAILED_INIT;
703
704
  /* Check we have enough data to authenticate with and end the
705
     connect phase if we do not */
706
0
  if(!Curl_sasl_can_authenticate(&pop3c->sasl, data)) {
707
0
    pop3_state(data, POP3_STOP);
708
0
    return result;
709
0
  }
710
711
0
  if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_SASL) {
712
    /* Calculate the SASL login details */
713
0
    result = Curl_sasl_start(&pop3c->sasl, data, FALSE, &progress);
714
715
0
    if(!result)
716
0
      if(progress == SASL_INPROGRESS)
717
0
        pop3_state(data, POP3_AUTH);
718
0
  }
719
720
0
  if(!result && progress == SASL_IDLE) {
721
0
#ifndef CURL_DISABLE_DIGEST_AUTH
722
0
    if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_APOP)
723
      /* Perform APOP authentication */
724
0
      result = pop3_perform_apop(data, conn);
725
0
    else
726
0
#endif
727
0
    if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT)
728
      /* Perform clear text authentication */
729
0
      result = pop3_perform_user(data, conn);
730
0
    else
731
0
      result = Curl_sasl_is_blocked(&pop3c->sasl, data);
732
0
  }
733
734
0
  return result;
735
0
}
736
737
/***********************************************************************
738
 *
739
 * pop3_perform_command()
740
 *
741
 * Sends a POP3 based command.
742
 */
743
static CURLcode pop3_perform_command(struct Curl_easy *data)
744
0
{
745
0
  CURLcode result = CURLE_OK;
746
0
  struct connectdata *conn = data->conn;
747
0
  struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
748
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
749
0
  const char *command = NULL;
750
751
0
  if(!pop3 || !pop3c)
752
0
    return CURLE_FAILED_INIT;
753
754
  /* Calculate the default command */
755
0
  if(pop3->id[0] == '\0' || data->set.list_only) {
756
0
    command = "LIST";
757
758
0
    if(pop3->id[0] != '\0')
759
      /* Message specific LIST so skip the BODY transfer */
760
0
      pop3->transfer = PPTRANSFER_INFO;
761
0
  }
762
0
  else
763
0
    command = "RETR";
764
765
0
  if(pop3->custom && pop3->custom[0] != '\0')
766
0
    command = pop3->custom;
767
768
  /* Send the command */
769
0
  if(pop3->id[0] != '\0')
770
0
    result = Curl_pp_sendf(data, &pop3c->pp, "%s %s", command, pop3->id);
771
0
  else
772
0
    result = Curl_pp_sendf(data, &pop3c->pp, "%s", command);
773
774
0
  if(!result) {
775
0
    pop3_state(data, POP3_COMMAND);
776
0
    data->req.no_body = !pop3_is_multiline(command);
777
0
  }
778
779
0
  return result;
780
0
}
781
782
/***********************************************************************
783
 *
784
 * pop3_perform_quit()
785
 *
786
 * Performs the quit action prior to sclose() be called.
787
 */
788
static CURLcode pop3_perform_quit(struct Curl_easy *data,
789
                                  struct connectdata *conn)
790
0
{
791
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
792
0
  CURLcode result;
793
794
0
  if(!pop3c)
795
0
    return CURLE_FAILED_INIT;
796
797
  /* Send the QUIT command */
798
0
  result = Curl_pp_sendf(data, &pop3c->pp, "%s", "QUIT");
799
0
  if(!result)
800
0
    pop3_state(data, POP3_QUIT);
801
802
0
  return result;
803
0
}
804
805
/* For the initial server greeting */
806
static CURLcode pop3_state_servergreet_resp(struct Curl_easy *data,
807
                                            int pop3code,
808
                                            pop3state instate)
809
0
{
810
0
  CURLcode result = CURLE_OK;
811
0
  struct connectdata *conn = data->conn;
812
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
813
0
  const char *line;
814
0
  size_t len;
815
816
0
  (void)instate;
817
0
  if(!pop3c)
818
0
    return CURLE_FAILED_INIT;
819
820
0
  line = curlx_dyn_ptr(&pop3c->pp.recvbuf);
821
0
  len = pop3c->pp.nfinal;
822
823
0
  if(pop3code != '+') {
824
0
    failf(data, "Got unexpected pop3-server response");
825
0
    result = CURLE_WEIRD_SERVER_REPLY;
826
0
  }
827
0
  else if(len > 3) {
828
    /* Does the server support APOP authentication? */
829
0
    char *lt;
830
0
    char *gt = NULL;
831
832
    /* Look for the APOP timestamp */
833
0
    lt = memchr(line, '<', len);
834
0
    if(lt)
835
      /* search the remainder for '>' */
836
0
      gt = memchr(lt, '>', len - (lt - line));
837
0
    if(gt) {
838
      /* the length of the timestamp, including the brackets */
839
0
      size_t timestamplen = gt - lt + 1;
840
0
      char *at = memchr(lt, '@', timestamplen);
841
      /* If the timestamp does not contain '@' it is not (as required by
842
         RFC-1939) conformant to the RFC-822 message id syntax, and we
843
         therefore do not use APOP authentication. */
844
0
      if(at) {
845
        /* dupe the timestamp */
846
0
        pop3c->apoptimestamp = Curl_memdup0(lt, timestamplen);
847
0
        if(!pop3c->apoptimestamp)
848
0
          return CURLE_OUT_OF_MEMORY;
849
        /* Store the APOP capability */
850
0
        pop3c->authtypes |= POP3_TYPE_APOP;
851
0
      }
852
0
    }
853
854
0
    if(!result)
855
0
      result = pop3_perform_capa(data, conn);
856
0
  }
857
858
0
  return result;
859
0
}
860
861
/* For CAPA responses */
862
static CURLcode pop3_state_capa_resp(struct Curl_easy *data, int pop3code,
863
                                     pop3state instate)
864
0
{
865
0
  CURLcode result = CURLE_OK;
866
0
  struct connectdata *conn = data->conn;
867
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
868
0
  const char *line;
869
0
  size_t len;
870
871
0
  (void)instate;
872
0
  if(!pop3c)
873
0
    return CURLE_FAILED_INIT;
874
875
0
  line = curlx_dyn_ptr(&pop3c->pp.recvbuf);
876
0
  len = pop3c->pp.nfinal;
877
878
  /* Do we have an untagged continuation response? */
879
0
  if(pop3code == '*') {
880
    /* Does the server support the STLS capability? */
881
0
    if(len >= 4 && curl_strnequal(line, "STLS", 4))
882
0
      pop3c->tls_supported = TRUE;
883
884
    /* Does the server support clear text authentication? */
885
0
    else if(len >= 4 && curl_strnequal(line, "USER", 4))
886
0
      pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
887
888
    /* Does the server support SASL based authentication? */
889
0
    else if(len >= 5 && curl_strnequal(line, "SASL ", 5)) {
890
0
      pop3c->authtypes |= POP3_TYPE_SASL;
891
892
      /* Advance past the SASL keyword */
893
0
      line += 5;
894
0
      len -= 5;
895
896
      /* Loop through the data line */
897
0
      for(;;) {
898
0
        size_t llen;
899
0
        size_t wordlen = 0;
900
0
        unsigned short mechbit;
901
902
0
        while(len && (ISBLANK(*line) || ISNEWLINE(*line))) {
903
0
          line++;
904
0
          len--;
905
0
        }
906
907
0
        if(!len)
908
0
          break;
909
910
        /* Extract the word */
911
0
        while(wordlen < len && !ISBLANK(line[wordlen]) &&
912
0
              !ISNEWLINE(line[wordlen]))
913
0
          wordlen++;
914
915
        /* Test the word for a matching authentication mechanism */
916
0
        mechbit = Curl_sasl_decode_mech(line, wordlen, &llen);
917
0
        if(mechbit && llen == wordlen)
918
0
          pop3c->sasl.authmechs |= mechbit;
919
920
0
        line += wordlen;
921
0
        len -= wordlen;
922
0
      }
923
0
    }
924
0
  }
925
0
  else {
926
    /* Clear text is supported when CAPA is not recognised */
927
0
    if(pop3code != '+')
928
0
      pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
929
930
0
    if(!data->set.use_ssl || Curl_conn_is_ssl(conn, FIRSTSOCKET))
931
0
      result = pop3_perform_authentication(data, conn);
932
0
    else if(pop3code == '+' && pop3c->tls_supported)
933
      /* Switch to TLS connection now */
934
0
      result = pop3_perform_starttls(data, conn);
935
0
    else if(data->set.use_ssl <= CURLUSESSL_TRY)
936
      /* Fallback and carry on with authentication */
937
0
      result = pop3_perform_authentication(data, conn);
938
0
    else {
939
0
      failf(data, "STLS not supported.");
940
0
      result = CURLE_USE_SSL_FAILED;
941
0
    }
942
0
  }
943
944
0
  return result;
945
0
}
946
947
/* For STARTTLS responses */
948
static CURLcode pop3_state_starttls_resp(struct Curl_easy *data,
949
                                         struct connectdata *conn,
950
                                         int pop3code,
951
                                         pop3state instate)
952
0
{
953
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
954
0
  CURLcode result = CURLE_OK;
955
0
  (void)instate;
956
957
0
  if(!pop3c)
958
0
    return CURLE_FAILED_INIT;
959
960
  /* Pipelining in response is forbidden. */
961
0
  if(pop3c->pp.overflow)
962
0
    return CURLE_WEIRD_SERVER_REPLY;
963
964
0
  if(pop3code != '+') {
965
0
    if(data->set.use_ssl != CURLUSESSL_TRY) {
966
0
      failf(data, "STARTTLS denied");
967
0
      result = CURLE_USE_SSL_FAILED;
968
0
    }
969
0
    else
970
0
      result = pop3_perform_authentication(data, conn);
971
0
  }
972
0
  else
973
0
    pop3_state(data, POP3_UPGRADETLS);
974
975
0
  return result;
976
0
}
977
978
/* For SASL authentication responses */
979
static CURLcode pop3_state_auth_resp(struct Curl_easy *data,
980
                                     int pop3code,
981
                                     pop3state instate)
982
0
{
983
0
  CURLcode result = CURLE_OK;
984
0
  struct connectdata *conn = data->conn;
985
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
986
0
  saslprogress progress;
987
988
0
  (void)instate;
989
0
  if(!pop3c)
990
0
    return CURLE_FAILED_INIT;
991
992
0
  result = Curl_sasl_continue(&pop3c->sasl, data, pop3code, &progress);
993
0
  if(!result)
994
0
    switch(progress) {
995
0
    case SASL_DONE:
996
0
      pop3_state(data, POP3_STOP);  /* Authenticated */
997
0
      break;
998
0
    case SASL_IDLE:            /* No mechanism left after cancellation */
999
0
#ifndef CURL_DISABLE_DIGEST_AUTH
1000
0
      if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_APOP)
1001
        /* Perform APOP authentication */
1002
0
        result = pop3_perform_apop(data, conn);
1003
0
      else
1004
0
#endif
1005
0
      if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT)
1006
        /* Perform clear text authentication */
1007
0
        result = pop3_perform_user(data, conn);
1008
0
      else {
1009
0
        failf(data, "Authentication cancelled");
1010
0
        result = CURLE_LOGIN_DENIED;
1011
0
      }
1012
0
      break;
1013
0
    default:
1014
0
      break;
1015
0
    }
1016
1017
0
  return result;
1018
0
}
1019
1020
#ifndef CURL_DISABLE_DIGEST_AUTH
1021
/* For APOP responses */
1022
static CURLcode pop3_state_apop_resp(struct Curl_easy *data, int pop3code,
1023
                                     pop3state instate)
1024
0
{
1025
0
  CURLcode result = CURLE_OK;
1026
0
  (void)instate;
1027
1028
0
  if(pop3code != '+') {
1029
0
    failf(data, "Authentication failed: %d", pop3code);
1030
0
    result = CURLE_LOGIN_DENIED;
1031
0
  }
1032
0
  else
1033
    /* End of connect phase */
1034
0
    pop3_state(data, POP3_STOP);
1035
1036
0
  return result;
1037
0
}
1038
#endif
1039
1040
/* For USER responses */
1041
static CURLcode pop3_state_user_resp(struct Curl_easy *data, int pop3code,
1042
                                     pop3state instate)
1043
0
{
1044
0
  CURLcode result = CURLE_OK;
1045
0
  struct connectdata *conn = data->conn;
1046
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1047
0
  (void)instate;
1048
1049
0
  if(!pop3c)
1050
0
    return CURLE_FAILED_INIT;
1051
1052
0
  if(pop3code != '+') {
1053
0
    failf(data, "Access denied. %c", pop3code);
1054
0
    result = CURLE_LOGIN_DENIED;
1055
0
  }
1056
0
  else
1057
    /* Send the PASS command */
1058
0
    result = Curl_pp_sendf(data, &pop3c->pp, "PASS %s", conn->passwd);
1059
0
  if(!result)
1060
0
    pop3_state(data, POP3_PASS);
1061
1062
0
  return result;
1063
0
}
1064
1065
/* For PASS responses */
1066
static CURLcode pop3_state_pass_resp(struct Curl_easy *data, int pop3code,
1067
                                     pop3state instate)
1068
0
{
1069
0
  CURLcode result = CURLE_OK;
1070
0
  (void)instate;
1071
1072
0
  if(pop3code != '+') {
1073
0
    failf(data, "Access denied. %c", pop3code);
1074
0
    result = CURLE_LOGIN_DENIED;
1075
0
  }
1076
0
  else
1077
    /* End of connect phase */
1078
0
    pop3_state(data, POP3_STOP);
1079
1080
0
  return result;
1081
0
}
1082
1083
/* For command responses */
1084
static CURLcode pop3_state_command_resp(struct Curl_easy *data,
1085
                                        int pop3code,
1086
                                        pop3state instate)
1087
0
{
1088
0
  CURLcode result = CURLE_OK;
1089
0
  struct connectdata *conn = data->conn;
1090
0
  struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
1091
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1092
0
  struct pingpong *pp;
1093
1094
0
  (void)instate;
1095
0
  if(!pop3 || !pop3c)
1096
0
    return CURLE_FAILED_INIT;
1097
1098
0
  pp = &pop3c->pp;
1099
0
  if(pop3code != '+') {
1100
0
    pop3_state(data, POP3_STOP);
1101
0
    return CURLE_WEIRD_SERVER_REPLY;
1102
0
  }
1103
1104
  /* This 'OK' line ends with a CR LF pair which is the two first bytes of the
1105
     EOB string so count this is two matching bytes. This is necessary to make
1106
     the code detect the EOB if the only data than comes now is %2e CR LF like
1107
     when there is no body to return. */
1108
0
  pop3c->eob = 2;
1109
1110
  /* But since this initial CR LF pair is not part of the actual body, we set
1111
     the strip counter here so that these bytes will not be delivered. */
1112
0
  pop3c->strip = 2;
1113
1114
0
  if(pop3->transfer == PPTRANSFER_BODY) {
1115
    /* POP3 download */
1116
0
    Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
1117
1118
0
    if(pp->overflow) {
1119
      /* The recv buffer contains data that is actually body content so send
1120
         it as such. Note that there may even be additional "headers" after
1121
         the body */
1122
1123
      /* keep only the overflow */
1124
0
      curlx_dyn_tail(&pp->recvbuf, pp->overflow);
1125
0
      pp->nfinal = 0; /* done */
1126
1127
0
      if(!data->req.no_body) {
1128
0
        result = pop3_write(data, curlx_dyn_ptr(&pp->recvbuf),
1129
0
                            curlx_dyn_len(&pp->recvbuf), FALSE);
1130
0
        if(result)
1131
0
          return result;
1132
0
      }
1133
1134
      /* reset the buffer */
1135
0
      curlx_dyn_reset(&pp->recvbuf);
1136
0
      pp->overflow = 0;
1137
0
    }
1138
0
  }
1139
0
  else
1140
0
    pp->overflow = 0;
1141
1142
  /* End of DO phase */
1143
0
  pop3_state(data, POP3_STOP);
1144
1145
0
  return result;
1146
0
}
1147
1148
static CURLcode pop3_statemachine(struct Curl_easy *data,
1149
                                  struct connectdata *conn)
1150
0
{
1151
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1152
0
  CURLcode result = CURLE_OK;
1153
0
  int pop3code;
1154
0
  struct pingpong *pp;
1155
0
  size_t nread = 0;
1156
0
  (void)data;
1157
1158
0
  if(!pop3c)
1159
0
    return CURLE_FAILED_INIT;
1160
1161
0
  pp = &pop3c->pp;
1162
  /* Busy upgrading the connection; right now all I/O is SSL/TLS, not POP3 */
1163
0
upgrade_tls:
1164
0
  if(pop3c->state == POP3_UPGRADETLS) {
1165
0
    result = pop3_perform_upgrade_tls(data, conn);
1166
0
    if(result || (pop3c->state == POP3_UPGRADETLS))
1167
0
      return result;
1168
0
  }
1169
1170
  /* Flush any data that needs to be sent */
1171
0
  if(pp->sendleft)
1172
0
    return Curl_pp_flushsend(data, pp);
1173
1174
0
 do {
1175
    /* Read the response from the server */
1176
0
   result = Curl_pp_readresp(data, FIRSTSOCKET, pp, &pop3code, &nread);
1177
0
   if(result)
1178
0
     return result;
1179
1180
0
    if(!pop3code)
1181
0
      break;
1182
1183
    /* We have now received a full POP3 server response */
1184
0
    switch(pop3c->state) {
1185
0
    case POP3_SERVERGREET:
1186
0
      result = pop3_state_servergreet_resp(data, pop3code, pop3c->state);
1187
0
      break;
1188
1189
0
    case POP3_CAPA:
1190
0
      result = pop3_state_capa_resp(data, pop3code, pop3c->state);
1191
0
      break;
1192
1193
0
    case POP3_STARTTLS:
1194
0
      result = pop3_state_starttls_resp(data, conn, pop3code, pop3c->state);
1195
      /* During UPGRADETLS, leave the read loop as we need to connect
1196
       * (e.g. TLS handshake) before we continue sending/receiving. */
1197
0
      if(!result && (pop3c->state == POP3_UPGRADETLS))
1198
0
        goto upgrade_tls;
1199
0
      break;
1200
1201
0
    case POP3_AUTH:
1202
0
      result = pop3_state_auth_resp(data, pop3code, pop3c->state);
1203
0
      break;
1204
1205
0
#ifndef CURL_DISABLE_DIGEST_AUTH
1206
0
    case POP3_APOP:
1207
0
      result = pop3_state_apop_resp(data, pop3code, pop3c->state);
1208
0
      break;
1209
0
#endif
1210
1211
0
    case POP3_USER:
1212
0
      result = pop3_state_user_resp(data, pop3code, pop3c->state);
1213
0
      break;
1214
1215
0
    case POP3_PASS:
1216
0
      result = pop3_state_pass_resp(data, pop3code, pop3c->state);
1217
0
      break;
1218
1219
0
    case POP3_COMMAND:
1220
0
      result = pop3_state_command_resp(data, pop3code, pop3c->state);
1221
0
      break;
1222
1223
0
    case POP3_QUIT:
1224
0
      pop3_state(data, POP3_STOP);
1225
0
      break;
1226
1227
0
    default:
1228
      /* internal error */
1229
0
      pop3_state(data, POP3_STOP);
1230
0
      break;
1231
0
    }
1232
0
  } while(!result && pop3c->state != POP3_STOP && Curl_pp_moredata(pp));
1233
1234
0
  return result;
1235
0
}
1236
1237
/* Called repeatedly until done from multi.c */
1238
static CURLcode pop3_multi_statemach(struct Curl_easy *data, bool *done)
1239
0
{
1240
0
  CURLcode result = CURLE_OK;
1241
0
  struct connectdata *conn = data->conn;
1242
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1243
1244
0
  if(!pop3c)
1245
0
    return CURLE_FAILED_INIT;
1246
0
  result = Curl_pp_statemach(data, &pop3c->pp, FALSE, FALSE);
1247
0
  *done = (pop3c->state == POP3_STOP);
1248
1249
0
  return result;
1250
0
}
1251
1252
static CURLcode pop3_block_statemach(struct Curl_easy *data,
1253
                                     struct connectdata *conn,
1254
                                     bool disconnecting)
1255
0
{
1256
0
  CURLcode result = CURLE_OK;
1257
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1258
1259
0
  if(!pop3c)
1260
0
    return CURLE_FAILED_INIT;
1261
1262
0
  while(pop3c->state != POP3_STOP && !result)
1263
0
    result = Curl_pp_statemach(data, &pop3c->pp, TRUE, disconnecting);
1264
1265
0
  return result;
1266
0
}
1267
1268
/* For the POP3 "protocol connect" and "doing" phases only */
1269
static CURLcode pop3_pollset(struct Curl_easy *data,
1270
                             struct easy_pollset *ps)
1271
0
{
1272
0
  struct pop3_conn *pop3c =
1273
0
    Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN);
1274
0
  return pop3c ? Curl_pp_pollset(data, &pop3c->pp, ps) : CURLE_OK;
1275
0
}
1276
1277
/***********************************************************************
1278
 *
1279
 * pop3_connect()
1280
 *
1281
 * This function should do everything that is to be considered a part of the
1282
 * connection phase.
1283
 *
1284
 * The variable 'done' points to will be TRUE if the protocol-layer connect
1285
 * phase is done when this function returns, or FALSE if not.
1286
 */
1287
static CURLcode pop3_connect(struct Curl_easy *data, bool *done)
1288
0
{
1289
0
  CURLcode result = CURLE_OK;
1290
0
  struct connectdata *conn = data->conn;
1291
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1292
0
  struct pingpong *pp = pop3c ? &pop3c->pp : NULL;
1293
1294
0
  *done = FALSE; /* default to not done yet */
1295
0
  if(!pop3c)
1296
0
    return CURLE_FAILED_INIT;
1297
1298
  /* We always support persistent connections in POP3 */
1299
0
  connkeep(conn, "POP3 default");
1300
1301
0
  PINGPONG_SETUP(pp, pop3_statemachine, pop3_endofresp);
1302
1303
  /* Set the default preferred authentication type and mechanism */
1304
0
  pop3c->preftype = POP3_TYPE_ANY;
1305
0
  Curl_sasl_init(&pop3c->sasl, data, &saslpop3);
1306
1307
  /* Initialise the pingpong layer */
1308
0
  Curl_pp_init(pp);
1309
1310
  /* Parse the URL options */
1311
0
  result = pop3_parse_url_options(conn);
1312
0
  if(result)
1313
0
    return result;
1314
1315
  /* Start off waiting for the server greeting response */
1316
0
  pop3_state(data, POP3_SERVERGREET);
1317
1318
0
  result = pop3_multi_statemach(data, done);
1319
1320
0
  return result;
1321
0
}
1322
1323
/***********************************************************************
1324
 *
1325
 * pop3_done()
1326
 *
1327
 * The DONE function. This does what needs to be done after a single DO has
1328
 * performed.
1329
 *
1330
 * Input argument is already checked for validity.
1331
 */
1332
static CURLcode pop3_done(struct Curl_easy *data, CURLcode status,
1333
                          bool premature)
1334
0
{
1335
0
  CURLcode result = CURLE_OK;
1336
0
  struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
1337
1338
0
  (void)premature;
1339
1340
0
  if(!pop3)
1341
0
    return CURLE_OK;
1342
1343
0
  if(status) {
1344
0
    connclose(data->conn, "POP3 done with bad status");
1345
0
    result = status;         /* use the already set error code */
1346
0
  }
1347
1348
  /* Cleanup our per-request based variables */
1349
0
  Curl_safefree(pop3->id);
1350
0
  Curl_safefree(pop3->custom);
1351
1352
  /* Clear the transfer mode for the next request */
1353
0
  pop3->transfer = PPTRANSFER_BODY;
1354
1355
0
  return result;
1356
0
}
1357
1358
/***********************************************************************
1359
 *
1360
 * pop3_perform()
1361
 *
1362
 * This is the actual DO function for POP3. Get a message/listing according to
1363
 * the options previously setup.
1364
 */
1365
static CURLcode pop3_perform(struct Curl_easy *data, bool *connected,
1366
                             bool *dophase_done)
1367
0
{
1368
  /* This is POP3 and no proxy */
1369
0
  CURLcode result = CURLE_OK;
1370
0
  struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
1371
1372
0
  if(!pop3)
1373
0
    return CURLE_FAILED_INIT;
1374
1375
0
  DEBUGF(infof(data, "DO phase starts"));
1376
1377
  /* Start the first command in the DO phase, may alter data->req.no_body */
1378
0
  result = pop3_perform_command(data);
1379
0
  if(result)
1380
0
    return result;
1381
1382
0
  if(data->req.no_body)
1383
    /* Requested no body means no transfer */
1384
0
    pop3->transfer = PPTRANSFER_INFO;
1385
1386
0
  *dophase_done = FALSE; /* not done yet */
1387
1388
  /* Run the state-machine */
1389
0
  result = pop3_multi_statemach(data, dophase_done);
1390
0
  *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
1391
1392
0
  if(*dophase_done)
1393
0
    DEBUGF(infof(data, "DO phase is complete"));
1394
1395
0
  return result;
1396
0
}
1397
1398
/***********************************************************************
1399
 *
1400
 * pop3_do()
1401
 *
1402
 * This function is registered as 'curl_do' function. It decodes the path
1403
 * parts etc as a wrapper to the actual DO function (pop3_perform).
1404
 *
1405
 * The input argument is already checked for validity.
1406
 */
1407
static CURLcode pop3_do(struct Curl_easy *data, bool *done)
1408
0
{
1409
0
  CURLcode result = CURLE_OK;
1410
0
  *done = FALSE; /* default to false */
1411
1412
  /* Parse the URL path */
1413
0
  result = pop3_parse_url_path(data);
1414
0
  if(result)
1415
0
    return result;
1416
1417
  /* Parse the custom request */
1418
0
  result = pop3_parse_custom_request(data);
1419
0
  if(result)
1420
0
    return result;
1421
1422
0
  result = pop3_regular_transfer(data, done);
1423
1424
0
  return result;
1425
0
}
1426
1427
/***********************************************************************
1428
 *
1429
 * pop3_disconnect()
1430
 *
1431
 * Disconnect from an POP3 server. Cleanup protocol-specific per-connection
1432
 * resources. BLOCKING.
1433
 */
1434
static CURLcode pop3_disconnect(struct Curl_easy *data,
1435
                                struct connectdata *conn, bool dead_connection)
1436
0
{
1437
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1438
0
  (void)data;
1439
1440
0
  if(!pop3c)
1441
0
    return CURLE_FAILED_INIT;
1442
1443
  /* We cannot send quit unconditionally. If this connection is stale or
1444
     bad in any way, sending quit and waiting around here will make the
1445
     disconnect wait in vain and cause more problems than we need to. */
1446
1447
0
  if(!dead_connection && conn->bits.protoconnstart &&
1448
0
     !Curl_pp_needs_flush(data, &pop3c->pp)) {
1449
0
    if(!pop3_perform_quit(data, conn))
1450
0
      (void)pop3_block_statemach(data, conn, TRUE); /* ignore errors on QUIT */
1451
0
  }
1452
1453
  /* Disconnect from the server */
1454
0
  Curl_pp_disconnect(&pop3c->pp);
1455
1456
  /* Cleanup our connection based variables */
1457
0
  Curl_safefree(pop3c->apoptimestamp);
1458
1459
0
  return CURLE_OK;
1460
0
}
1461
1462
/* Call this when the DO phase has completed */
1463
static CURLcode pop3_dophase_done(struct Curl_easy *data, bool connected)
1464
0
{
1465
0
  (void)data;
1466
0
  (void)connected;
1467
1468
0
  return CURLE_OK;
1469
0
}
1470
1471
/* Called from multi.c while DOing */
1472
static CURLcode pop3_doing(struct Curl_easy *data, bool *dophase_done)
1473
0
{
1474
0
  CURLcode result = pop3_multi_statemach(data, dophase_done);
1475
1476
0
  if(result)
1477
0
    DEBUGF(infof(data, "DO phase failed"));
1478
0
  else if(*dophase_done) {
1479
0
    result = pop3_dophase_done(data, FALSE /* not connected */);
1480
1481
0
    DEBUGF(infof(data, "DO phase is complete"));
1482
0
  }
1483
1484
0
  return result;
1485
0
}
1486
1487
/***********************************************************************
1488
 *
1489
 * pop3_regular_transfer()
1490
 *
1491
 * The input argument is already checked for validity.
1492
 *
1493
 * Performs all commands done before a regular transfer between a local and a
1494
 * remote host.
1495
 */
1496
static CURLcode pop3_regular_transfer(struct Curl_easy *data,
1497
                                      bool *dophase_done)
1498
0
{
1499
0
  CURLcode result = CURLE_OK;
1500
0
  bool connected = FALSE;
1501
1502
  /* Make sure size is unknown at this point */
1503
0
  data->req.size = -1;
1504
1505
  /* Set the progress data */
1506
0
  Curl_pgrsSetUploadCounter(data, 0);
1507
0
  Curl_pgrsSetDownloadCounter(data, 0);
1508
0
  Curl_pgrsSetUploadSize(data, -1);
1509
0
  Curl_pgrsSetDownloadSize(data, -1);
1510
1511
  /* Carry out the perform */
1512
0
  result = pop3_perform(data, &connected, dophase_done);
1513
1514
  /* Perform post DO phase operations if necessary */
1515
0
  if(!result && *dophase_done)
1516
0
    result = pop3_dophase_done(data, connected);
1517
1518
0
  return result;
1519
0
}
1520
1521
static void pop3_easy_dtor(void *key, size_t klen, void *entry)
1522
0
{
1523
0
  struct POP3 *pop3 = entry;
1524
0
  (void)key;
1525
0
  (void)klen;
1526
0
  DEBUGASSERT(pop3);
1527
  /* Cleanup our per-request based variables */
1528
0
  Curl_safefree(pop3->id);
1529
0
  Curl_safefree(pop3->custom);
1530
0
  free(pop3);
1531
0
}
1532
1533
static void pop3_conn_dtor(void *key, size_t klen, void *entry)
1534
0
{
1535
0
  struct pop3_conn *pop3c = entry;
1536
0
  (void)key;
1537
0
  (void)klen;
1538
0
  DEBUGASSERT(pop3c);
1539
0
  Curl_pp_disconnect(&pop3c->pp);
1540
0
  Curl_safefree(pop3c->apoptimestamp);
1541
0
  free(pop3c);
1542
0
}
1543
1544
static CURLcode pop3_setup_connection(struct Curl_easy *data,
1545
                                      struct connectdata *conn)
1546
0
{
1547
0
  struct pop3_conn *pop3c;
1548
0
  struct POP3 *pop3 = calloc(1, sizeof(*pop3));
1549
0
  if(!pop3 ||
1550
0
     Curl_meta_set(data, CURL_META_POP3_EASY, pop3, pop3_easy_dtor))
1551
0
    return CURLE_OUT_OF_MEMORY;
1552
1553
0
  pop3c = calloc(1, sizeof(*pop3c));
1554
0
  if(!pop3c ||
1555
0
     Curl_conn_meta_set(conn, CURL_META_POP3_CONN, pop3c, pop3_conn_dtor))
1556
0
    return CURLE_OUT_OF_MEMORY;
1557
1558
0
  return CURLE_OK;
1559
0
}
1560
1561
/***********************************************************************
1562
 *
1563
 * pop3_parse_url_options()
1564
 *
1565
 * Parse the URL login options.
1566
 */
1567
static CURLcode pop3_parse_url_options(struct connectdata *conn)
1568
0
{
1569
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1570
0
  CURLcode result = CURLE_OK;
1571
0
  const char *ptr = conn->options;
1572
1573
0
  if(!pop3c)
1574
0
    return CURLE_FAILED_INIT;
1575
1576
0
  while(!result && ptr && *ptr) {
1577
0
    const char *key = ptr;
1578
0
    const char *value;
1579
1580
0
    while(*ptr && *ptr != '=')
1581
0
      ptr++;
1582
1583
0
    value = ptr + 1;
1584
1585
0
    while(*ptr && *ptr != ';')
1586
0
      ptr++;
1587
1588
0
    if(curl_strnequal(key, "AUTH=", 5)) {
1589
0
      result = Curl_sasl_parse_url_auth_option(&pop3c->sasl,
1590
0
                                               value, ptr - value);
1591
1592
0
      if(result && curl_strnequal(value, "+APOP", ptr - value)) {
1593
0
        pop3c->preftype = POP3_TYPE_APOP;
1594
0
        pop3c->sasl.prefmech = SASL_AUTH_NONE;
1595
0
        result = CURLE_OK;
1596
0
      }
1597
0
    }
1598
0
    else
1599
0
      result = CURLE_URL_MALFORMAT;
1600
1601
0
    if(*ptr == ';')
1602
0
      ptr++;
1603
0
  }
1604
1605
0
  if(pop3c->preftype != POP3_TYPE_APOP)
1606
0
    switch(pop3c->sasl.prefmech) {
1607
0
    case SASL_AUTH_NONE:
1608
0
      pop3c->preftype = POP3_TYPE_NONE;
1609
0
      break;
1610
0
    case SASL_AUTH_DEFAULT:
1611
0
      pop3c->preftype = POP3_TYPE_ANY;
1612
0
      break;
1613
0
    default:
1614
0
      pop3c->preftype = POP3_TYPE_SASL;
1615
0
      break;
1616
0
    }
1617
1618
0
  return result;
1619
0
}
1620
1621
/***********************************************************************
1622
 *
1623
 * pop3_parse_url_path()
1624
 *
1625
 * Parse the URL path into separate path components.
1626
 */
1627
static CURLcode pop3_parse_url_path(struct Curl_easy *data)
1628
0
{
1629
  /* The POP3 struct is already initialised in pop3_connect() */
1630
0
  struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
1631
0
  const char *path = &data->state.up.path[1]; /* skip leading path */
1632
1633
0
  if(!pop3)
1634
0
    return CURLE_FAILED_INIT;
1635
  /* URL decode the path for the message ID */
1636
0
  return Curl_urldecode(path, 0, &pop3->id, NULL, REJECT_CTRL);
1637
0
}
1638
1639
/***********************************************************************
1640
 *
1641
 * pop3_parse_custom_request()
1642
 *
1643
 * Parse the custom request.
1644
 */
1645
static CURLcode pop3_parse_custom_request(struct Curl_easy *data)
1646
0
{
1647
0
  CURLcode result = CURLE_OK;
1648
0
  struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
1649
0
  const char *custom = data->set.str[STRING_CUSTOMREQUEST];
1650
1651
0
  if(!pop3)
1652
0
    return CURLE_FAILED_INIT;
1653
  /* URL decode the custom request */
1654
0
  if(custom)
1655
0
    result = Curl_urldecode(custom, 0, &pop3->custom, NULL, REJECT_CTRL);
1656
1657
0
  return result;
1658
0
}
1659
1660
/***********************************************************************
1661
 *
1662
 * pop3_write()
1663
 *
1664
 * This function scans the body after the end-of-body and writes everything
1665
 * until the end is found.
1666
 */
1667
static CURLcode pop3_write(struct Curl_easy *data, const char *str,
1668
                           size_t nread, bool is_eos)
1669
0
{
1670
  /* This code could be made into a special function in the handler struct */
1671
0
  CURLcode result = CURLE_OK;
1672
0
  struct SingleRequest *k = &data->req;
1673
0
  struct connectdata *conn = data->conn;
1674
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1675
0
  bool strip_dot = FALSE;
1676
0
  size_t last = 0;
1677
0
  size_t i;
1678
0
  (void)is_eos;
1679
1680
0
  if(!pop3c)
1681
0
    return CURLE_FAILED_INIT;
1682
1683
  /* Search through the buffer looking for the end-of-body marker which is
1684
     5 bytes (0d 0a 2e 0d 0a). Note that a line starting with a dot matches
1685
     the eob so the server will have prefixed it with an extra dot which we
1686
     need to strip out. Additionally the marker could of course be spread out
1687
     over 5 different data chunks. */
1688
0
  for(i = 0; i < nread; i++) {
1689
0
    size_t prev = pop3c->eob;
1690
1691
0
    switch(str[i]) {
1692
0
    case 0x0d:
1693
0
      if(pop3c->eob == 0) {
1694
0
        pop3c->eob++;
1695
1696
0
        if(i) {
1697
          /* Write out the body part that did not match */
1698
0
          result = Curl_client_write(data, CLIENTWRITE_BODY, &str[last],
1699
0
                                     i - last);
1700
1701
0
          if(result)
1702
0
            return result;
1703
1704
0
          last = i;
1705
0
        }
1706
0
      }
1707
0
      else if(pop3c->eob == 3)
1708
0
        pop3c->eob++;
1709
0
      else
1710
        /* If the character match was not at position 0 or 3 then restart the
1711
           pattern matching */
1712
0
        pop3c->eob = 1;
1713
0
      break;
1714
1715
0
    case 0x0a:
1716
0
      if(pop3c->eob == 1 || pop3c->eob == 4)
1717
0
        pop3c->eob++;
1718
0
      else
1719
        /* If the character match was not at position 1 or 4 then start the
1720
           search again */
1721
0
        pop3c->eob = 0;
1722
0
      break;
1723
1724
0
    case 0x2e:
1725
0
      if(pop3c->eob == 2)
1726
0
        pop3c->eob++;
1727
0
      else if(pop3c->eob == 3) {
1728
        /* We have an extra dot after the CRLF which we need to strip off */
1729
0
        strip_dot = TRUE;
1730
0
        pop3c->eob = 0;
1731
0
      }
1732
0
      else
1733
        /* If the character match was not at position 2 then start the search
1734
           again */
1735
0
        pop3c->eob = 0;
1736
0
      break;
1737
1738
0
    default:
1739
0
      pop3c->eob = 0;
1740
0
      break;
1741
0
    }
1742
1743
    /* Did we have a partial match which has subsequently failed? */
1744
0
    if(prev && prev >= pop3c->eob) {
1745
      /* Strip can only be non-zero for the very first mismatch after CRLF
1746
         and then both prev and strip are equal and nothing will be output
1747
         below */
1748
0
      while(prev && pop3c->strip) {
1749
0
        prev--;
1750
0
        pop3c->strip--;
1751
0
      }
1752
1753
0
      if(prev) {
1754
        /* If the partial match was the CRLF and dot then only write the CRLF
1755
           as the server would have inserted the dot */
1756
0
        if(strip_dot && prev - 1 > 0) {
1757
0
          result = Curl_client_write(data, CLIENTWRITE_BODY, POP3_EOB,
1758
0
                                     prev - 1);
1759
0
        }
1760
0
        else if(!strip_dot) {
1761
0
          result = Curl_client_write(data, CLIENTWRITE_BODY, POP3_EOB,
1762
0
                                     prev);
1763
0
        }
1764
0
        else {
1765
0
          result = CURLE_OK;
1766
0
        }
1767
1768
0
        if(result)
1769
0
          return result;
1770
1771
0
        last = i;
1772
0
        strip_dot = FALSE;
1773
0
      }
1774
0
    }
1775
0
  }
1776
1777
0
  if(pop3c->eob == POP3_EOB_LEN) {
1778
    /* We have a full match so the transfer is done, however we must transfer
1779
    the CRLF at the start of the EOB as this is considered to be part of the
1780
    message as per RFC-1939, sect. 3 */
1781
0
    result = Curl_client_write(data, CLIENTWRITE_BODY, POP3_EOB, 2);
1782
1783
0
    k->keepon &= ~KEEP_RECV;
1784
0
    pop3c->eob = 0;
1785
1786
0
    return result;
1787
0
  }
1788
1789
0
  if(pop3c->eob)
1790
    /* While EOB is matching nothing should be output */
1791
0
    return CURLE_OK;
1792
1793
0
  if(nread - last) {
1794
0
    result = Curl_client_write(data, CLIENTWRITE_BODY, &str[last],
1795
0
                               nread - last);
1796
0
  }
1797
1798
0
  return result;
1799
0
}
1800
1801
#endif /* CURL_DISABLE_POP3 */