Coverage Report

Created: 2026-08-13 07:18

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