Coverage Report

Created: 2025-10-30 06:17

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 && !memcmp(line, "STLS", 4))
882
0
      pop3c->tls_supported = TRUE;
883
884
    /* Does the server support clear text authentication? */
885
0
    else if(len >= 4 && !memcmp(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 && !memcmp(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;
900
0
        unsigned short mechbit;
901
902
0
        while(len &&
903
0
              (*line == ' ' || *line == '\t' ||
904
0
               *line == '\r' || *line == '\n')) {
905
906
0
          line++;
907
0
          len--;
908
0
        }
909
910
0
        if(!len)
911
0
          break;
912
913
        /* Extract the word */
914
0
        for(wordlen = 0; wordlen < len && line[wordlen] != ' ' &&
915
0
              line[wordlen] != '\t' && line[wordlen] != '\r' &&
916
0
              line[wordlen] != '\n';)
917
0
          wordlen++;
918
919
        /* Test the word for a matching authentication mechanism */
920
0
        mechbit = Curl_sasl_decode_mech(line, wordlen, &llen);
921
0
        if(mechbit && llen == wordlen)
922
0
          pop3c->sasl.authmechs |= mechbit;
923
924
0
        line += wordlen;
925
0
        len -= wordlen;
926
0
      }
927
0
    }
928
0
  }
929
0
  else {
930
    /* Clear text is supported when CAPA is not recognised */
931
0
    if(pop3code != '+')
932
0
      pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
933
934
0
    if(!data->set.use_ssl || Curl_conn_is_ssl(conn, FIRSTSOCKET))
935
0
      result = pop3_perform_authentication(data, conn);
936
0
    else if(pop3code == '+' && pop3c->tls_supported)
937
      /* Switch to TLS connection now */
938
0
      result = pop3_perform_starttls(data, conn);
939
0
    else if(data->set.use_ssl <= CURLUSESSL_TRY)
940
      /* Fallback and carry on with authentication */
941
0
      result = pop3_perform_authentication(data, conn);
942
0
    else {
943
0
      failf(data, "STLS not supported.");
944
0
      result = CURLE_USE_SSL_FAILED;
945
0
    }
946
0
  }
947
948
0
  return result;
949
0
}
950
951
/* For STARTTLS responses */
952
static CURLcode pop3_state_starttls_resp(struct Curl_easy *data,
953
                                         struct connectdata *conn,
954
                                         int pop3code,
955
                                         pop3state instate)
956
0
{
957
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
958
0
  CURLcode result = CURLE_OK;
959
0
  (void)instate;
960
961
0
  if(!pop3c)
962
0
    return CURLE_FAILED_INIT;
963
964
  /* Pipelining in response is forbidden. */
965
0
  if(pop3c->pp.overflow)
966
0
    return CURLE_WEIRD_SERVER_REPLY;
967
968
0
  if(pop3code != '+') {
969
0
    if(data->set.use_ssl != CURLUSESSL_TRY) {
970
0
      failf(data, "STARTTLS denied");
971
0
      result = CURLE_USE_SSL_FAILED;
972
0
    }
973
0
    else
974
0
      result = pop3_perform_authentication(data, conn);
975
0
  }
976
0
  else
977
0
    pop3_state(data, POP3_UPGRADETLS);
978
979
0
  return result;
980
0
}
981
982
/* For SASL authentication responses */
983
static CURLcode pop3_state_auth_resp(struct Curl_easy *data,
984
                                     int pop3code,
985
                                     pop3state instate)
986
0
{
987
0
  CURLcode result = CURLE_OK;
988
0
  struct connectdata *conn = data->conn;
989
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
990
0
  saslprogress progress;
991
992
0
  (void)instate;
993
0
  if(!pop3c)
994
0
    return CURLE_FAILED_INIT;
995
996
0
  result = Curl_sasl_continue(&pop3c->sasl, data, pop3code, &progress);
997
0
  if(!result)
998
0
    switch(progress) {
999
0
    case SASL_DONE:
1000
0
      pop3_state(data, POP3_STOP);  /* Authenticated */
1001
0
      break;
1002
0
    case SASL_IDLE:            /* No mechanism left after cancellation */
1003
0
#ifndef CURL_DISABLE_DIGEST_AUTH
1004
0
      if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_APOP)
1005
        /* Perform APOP authentication */
1006
0
        result = pop3_perform_apop(data, conn);
1007
0
      else
1008
0
#endif
1009
0
      if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT)
1010
        /* Perform clear text authentication */
1011
0
        result = pop3_perform_user(data, conn);
1012
0
      else {
1013
0
        failf(data, "Authentication cancelled");
1014
0
        result = CURLE_LOGIN_DENIED;
1015
0
      }
1016
0
      break;
1017
0
    default:
1018
0
      break;
1019
0
    }
1020
1021
0
  return result;
1022
0
}
1023
1024
#ifndef CURL_DISABLE_DIGEST_AUTH
1025
/* For APOP responses */
1026
static CURLcode pop3_state_apop_resp(struct Curl_easy *data, int pop3code,
1027
                                     pop3state instate)
1028
0
{
1029
0
  CURLcode result = CURLE_OK;
1030
0
  (void)instate;
1031
1032
0
  if(pop3code != '+') {
1033
0
    failf(data, "Authentication failed: %d", pop3code);
1034
0
    result = CURLE_LOGIN_DENIED;
1035
0
  }
1036
0
  else
1037
    /* End of connect phase */
1038
0
    pop3_state(data, POP3_STOP);
1039
1040
0
  return result;
1041
0
}
1042
#endif
1043
1044
/* For USER responses */
1045
static CURLcode pop3_state_user_resp(struct Curl_easy *data, int pop3code,
1046
                                     pop3state instate)
1047
0
{
1048
0
  CURLcode result = CURLE_OK;
1049
0
  struct connectdata *conn = data->conn;
1050
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1051
0
  (void)instate;
1052
1053
0
  if(!pop3c)
1054
0
    return CURLE_FAILED_INIT;
1055
1056
0
  if(pop3code != '+') {
1057
0
    failf(data, "Access denied. %c", pop3code);
1058
0
    result = CURLE_LOGIN_DENIED;
1059
0
  }
1060
0
  else
1061
    /* Send the PASS command */
1062
0
    result = Curl_pp_sendf(data, &pop3c->pp, "PASS %s", conn->passwd);
1063
0
  if(!result)
1064
0
    pop3_state(data, POP3_PASS);
1065
1066
0
  return result;
1067
0
}
1068
1069
/* For PASS responses */
1070
static CURLcode pop3_state_pass_resp(struct Curl_easy *data, int pop3code,
1071
                                     pop3state instate)
1072
0
{
1073
0
  CURLcode result = CURLE_OK;
1074
0
  (void)instate;
1075
1076
0
  if(pop3code != '+') {
1077
0
    failf(data, "Access denied. %c", pop3code);
1078
0
    result = CURLE_LOGIN_DENIED;
1079
0
  }
1080
0
  else
1081
    /* End of connect phase */
1082
0
    pop3_state(data, POP3_STOP);
1083
1084
0
  return result;
1085
0
}
1086
1087
/* For command responses */
1088
static CURLcode pop3_state_command_resp(struct Curl_easy *data,
1089
                                        int pop3code,
1090
                                        pop3state instate)
1091
0
{
1092
0
  CURLcode result = CURLE_OK;
1093
0
  struct connectdata *conn = data->conn;
1094
0
  struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
1095
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1096
0
  struct pingpong *pp;
1097
1098
0
  (void)instate;
1099
0
  if(!pop3 || !pop3c)
1100
0
    return CURLE_FAILED_INIT;
1101
1102
0
  pp = &pop3c->pp;
1103
0
  if(pop3code != '+') {
1104
0
    pop3_state(data, POP3_STOP);
1105
0
    return CURLE_WEIRD_SERVER_REPLY;
1106
0
  }
1107
1108
  /* This 'OK' line ends with a CR LF pair which is the two first bytes of the
1109
     EOB string so count this is two matching bytes. This is necessary to make
1110
     the code detect the EOB if the only data than comes now is %2e CR LF like
1111
     when there is no body to return. */
1112
0
  pop3c->eob = 2;
1113
1114
  /* But since this initial CR LF pair is not part of the actual body, we set
1115
     the strip counter here so that these bytes will not be delivered. */
1116
0
  pop3c->strip = 2;
1117
1118
0
  if(pop3->transfer == PPTRANSFER_BODY) {
1119
    /* POP3 download */
1120
0
    Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
1121
1122
0
    if(pp->overflow) {
1123
      /* The recv buffer contains data that is actually body content so send
1124
         it as such. Note that there may even be additional "headers" after
1125
         the body */
1126
1127
      /* keep only the overflow */
1128
0
      curlx_dyn_tail(&pp->recvbuf, pp->overflow);
1129
0
      pp->nfinal = 0; /* done */
1130
1131
0
      if(!data->req.no_body) {
1132
0
        result = pop3_write(data, curlx_dyn_ptr(&pp->recvbuf),
1133
0
                            curlx_dyn_len(&pp->recvbuf), FALSE);
1134
0
        if(result)
1135
0
          return result;
1136
0
      }
1137
1138
      /* reset the buffer */
1139
0
      curlx_dyn_reset(&pp->recvbuf);
1140
0
      pp->overflow = 0;
1141
0
    }
1142
0
  }
1143
0
  else
1144
0
    pp->overflow = 0;
1145
1146
  /* End of DO phase */
1147
0
  pop3_state(data, POP3_STOP);
1148
1149
0
  return result;
1150
0
}
1151
1152
static CURLcode pop3_statemachine(struct Curl_easy *data,
1153
                                  struct connectdata *conn)
1154
0
{
1155
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1156
0
  CURLcode result = CURLE_OK;
1157
0
  int pop3code;
1158
0
  struct pingpong *pp;
1159
0
  size_t nread = 0;
1160
0
  (void)data;
1161
1162
0
  if(!pop3c)
1163
0
    return CURLE_FAILED_INIT;
1164
1165
0
  pp = &pop3c->pp;
1166
  /* Busy upgrading the connection; right now all I/O is SSL/TLS, not POP3 */
1167
0
upgrade_tls:
1168
0
  if(pop3c->state == POP3_UPGRADETLS) {
1169
0
    result = pop3_perform_upgrade_tls(data, conn);
1170
0
    if(result || (pop3c->state == POP3_UPGRADETLS))
1171
0
      return result;
1172
0
  }
1173
1174
  /* Flush any data that needs to be sent */
1175
0
  if(pp->sendleft)
1176
0
    return Curl_pp_flushsend(data, pp);
1177
1178
0
 do {
1179
    /* Read the response from the server */
1180
0
   result = Curl_pp_readresp(data, FIRSTSOCKET, pp, &pop3code, &nread);
1181
0
   if(result)
1182
0
     return result;
1183
1184
0
    if(!pop3code)
1185
0
      break;
1186
1187
    /* We have now received a full POP3 server response */
1188
0
    switch(pop3c->state) {
1189
0
    case POP3_SERVERGREET:
1190
0
      result = pop3_state_servergreet_resp(data, pop3code, pop3c->state);
1191
0
      break;
1192
1193
0
    case POP3_CAPA:
1194
0
      result = pop3_state_capa_resp(data, pop3code, pop3c->state);
1195
0
      break;
1196
1197
0
    case POP3_STARTTLS:
1198
0
      result = pop3_state_starttls_resp(data, conn, pop3code, pop3c->state);
1199
      /* During UPGRADETLS, leave the read loop as we need to connect
1200
       * (e.g. TLS handshake) before we continue sending/receiving. */
1201
0
      if(!result && (pop3c->state == POP3_UPGRADETLS))
1202
0
        goto upgrade_tls;
1203
0
      break;
1204
1205
0
    case POP3_AUTH:
1206
0
      result = pop3_state_auth_resp(data, pop3code, pop3c->state);
1207
0
      break;
1208
1209
0
#ifndef CURL_DISABLE_DIGEST_AUTH
1210
0
    case POP3_APOP:
1211
0
      result = pop3_state_apop_resp(data, pop3code, pop3c->state);
1212
0
      break;
1213
0
#endif
1214
1215
0
    case POP3_USER:
1216
0
      result = pop3_state_user_resp(data, pop3code, pop3c->state);
1217
0
      break;
1218
1219
0
    case POP3_PASS:
1220
0
      result = pop3_state_pass_resp(data, pop3code, pop3c->state);
1221
0
      break;
1222
1223
0
    case POP3_COMMAND:
1224
0
      result = pop3_state_command_resp(data, pop3code, pop3c->state);
1225
0
      break;
1226
1227
0
    case POP3_QUIT:
1228
0
      pop3_state(data, POP3_STOP);
1229
0
      break;
1230
1231
0
    default:
1232
      /* internal error */
1233
0
      pop3_state(data, POP3_STOP);
1234
0
      break;
1235
0
    }
1236
0
  } while(!result && pop3c->state != POP3_STOP && Curl_pp_moredata(pp));
1237
1238
0
  return result;
1239
0
}
1240
1241
/* Called repeatedly until done from multi.c */
1242
static CURLcode pop3_multi_statemach(struct Curl_easy *data, bool *done)
1243
0
{
1244
0
  CURLcode result = CURLE_OK;
1245
0
  struct connectdata *conn = data->conn;
1246
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1247
1248
0
  if(!pop3c)
1249
0
    return CURLE_FAILED_INIT;
1250
0
  result = Curl_pp_statemach(data, &pop3c->pp, FALSE, FALSE);
1251
0
  *done = (pop3c->state == POP3_STOP);
1252
1253
0
  return result;
1254
0
}
1255
1256
static CURLcode pop3_block_statemach(struct Curl_easy *data,
1257
                                     struct connectdata *conn,
1258
                                     bool disconnecting)
1259
0
{
1260
0
  CURLcode result = CURLE_OK;
1261
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1262
1263
0
  if(!pop3c)
1264
0
    return CURLE_FAILED_INIT;
1265
1266
0
  while(pop3c->state != POP3_STOP && !result)
1267
0
    result = Curl_pp_statemach(data, &pop3c->pp, TRUE, disconnecting);
1268
1269
0
  return result;
1270
0
}
1271
1272
/* For the POP3 "protocol connect" and "doing" phases only */
1273
static CURLcode pop3_pollset(struct Curl_easy *data,
1274
                             struct easy_pollset *ps)
1275
0
{
1276
0
  struct pop3_conn *pop3c =
1277
0
    Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN);
1278
0
  return pop3c ? Curl_pp_pollset(data, &pop3c->pp, ps) : CURLE_OK;
1279
0
}
1280
1281
/***********************************************************************
1282
 *
1283
 * pop3_connect()
1284
 *
1285
 * This function should do everything that is to be considered a part of the
1286
 * connection phase.
1287
 *
1288
 * The variable 'done' points to will be TRUE if the protocol-layer connect
1289
 * phase is done when this function returns, or FALSE if not.
1290
 */
1291
static CURLcode pop3_connect(struct Curl_easy *data, bool *done)
1292
0
{
1293
0
  CURLcode result = CURLE_OK;
1294
0
  struct connectdata *conn = data->conn;
1295
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1296
0
  struct pingpong *pp = pop3c ? &pop3c->pp : NULL;
1297
1298
0
  *done = FALSE; /* default to not done yet */
1299
0
  if(!pop3c)
1300
0
    return CURLE_FAILED_INIT;
1301
1302
  /* We always support persistent connections in POP3 */
1303
0
  connkeep(conn, "POP3 default");
1304
1305
0
  PINGPONG_SETUP(pp, pop3_statemachine, pop3_endofresp);
1306
1307
  /* Set the default preferred authentication type and mechanism */
1308
0
  pop3c->preftype = POP3_TYPE_ANY;
1309
0
  Curl_sasl_init(&pop3c->sasl, data, &saslpop3);
1310
1311
  /* Initialise the pingpong layer */
1312
0
  Curl_pp_init(pp);
1313
1314
  /* Parse the URL options */
1315
0
  result = pop3_parse_url_options(conn);
1316
0
  if(result)
1317
0
    return result;
1318
1319
  /* Start off waiting for the server greeting response */
1320
0
  pop3_state(data, POP3_SERVERGREET);
1321
1322
0
  result = pop3_multi_statemach(data, done);
1323
1324
0
  return result;
1325
0
}
1326
1327
/***********************************************************************
1328
 *
1329
 * pop3_done()
1330
 *
1331
 * The DONE function. This does what needs to be done after a single DO has
1332
 * performed.
1333
 *
1334
 * Input argument is already checked for validity.
1335
 */
1336
static CURLcode pop3_done(struct Curl_easy *data, CURLcode status,
1337
                          bool premature)
1338
0
{
1339
0
  CURLcode result = CURLE_OK;
1340
0
  struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
1341
1342
0
  (void)premature;
1343
1344
0
  if(!pop3)
1345
0
    return CURLE_OK;
1346
1347
0
  if(status) {
1348
0
    connclose(data->conn, "POP3 done with bad status");
1349
0
    result = status;         /* use the already set error code */
1350
0
  }
1351
1352
  /* Cleanup our per-request based variables */
1353
0
  Curl_safefree(pop3->id);
1354
0
  Curl_safefree(pop3->custom);
1355
1356
  /* Clear the transfer mode for the next request */
1357
0
  pop3->transfer = PPTRANSFER_BODY;
1358
1359
0
  return result;
1360
0
}
1361
1362
/***********************************************************************
1363
 *
1364
 * pop3_perform()
1365
 *
1366
 * This is the actual DO function for POP3. Get a message/listing according to
1367
 * the options previously setup.
1368
 */
1369
static CURLcode pop3_perform(struct Curl_easy *data, bool *connected,
1370
                             bool *dophase_done)
1371
0
{
1372
  /* This is POP3 and no proxy */
1373
0
  CURLcode result = CURLE_OK;
1374
0
  struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
1375
1376
0
  if(!pop3)
1377
0
    return CURLE_FAILED_INIT;
1378
1379
0
  DEBUGF(infof(data, "DO phase starts"));
1380
1381
  /* Start the first command in the DO phase, may alter data->req.no_body */
1382
0
  result = pop3_perform_command(data);
1383
0
  if(result)
1384
0
    return result;
1385
1386
0
  if(data->req.no_body)
1387
    /* Requested no body means no transfer */
1388
0
    pop3->transfer = PPTRANSFER_INFO;
1389
1390
0
  *dophase_done = FALSE; /* not done yet */
1391
1392
  /* Run the state-machine */
1393
0
  result = pop3_multi_statemach(data, dophase_done);
1394
0
  *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
1395
1396
0
  if(*dophase_done)
1397
0
    DEBUGF(infof(data, "DO phase is complete"));
1398
1399
0
  return result;
1400
0
}
1401
1402
/***********************************************************************
1403
 *
1404
 * pop3_do()
1405
 *
1406
 * This function is registered as 'curl_do' function. It decodes the path
1407
 * parts etc as a wrapper to the actual DO function (pop3_perform).
1408
 *
1409
 * The input argument is already checked for validity.
1410
 */
1411
static CURLcode pop3_do(struct Curl_easy *data, bool *done)
1412
0
{
1413
0
  CURLcode result = CURLE_OK;
1414
0
  *done = FALSE; /* default to false */
1415
1416
  /* Parse the URL path */
1417
0
  result = pop3_parse_url_path(data);
1418
0
  if(result)
1419
0
    return result;
1420
1421
  /* Parse the custom request */
1422
0
  result = pop3_parse_custom_request(data);
1423
0
  if(result)
1424
0
    return result;
1425
1426
0
  result = pop3_regular_transfer(data, done);
1427
1428
0
  return result;
1429
0
}
1430
1431
/***********************************************************************
1432
 *
1433
 * pop3_disconnect()
1434
 *
1435
 * Disconnect from an POP3 server. Cleanup protocol-specific per-connection
1436
 * resources. BLOCKING.
1437
 */
1438
static CURLcode pop3_disconnect(struct Curl_easy *data,
1439
                                struct connectdata *conn, bool dead_connection)
1440
0
{
1441
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1442
0
  (void)data;
1443
1444
0
  if(!pop3c)
1445
0
    return CURLE_FAILED_INIT;
1446
1447
  /* We cannot send quit unconditionally. If this connection is stale or
1448
     bad in any way, sending quit and waiting around here will make the
1449
     disconnect wait in vain and cause more problems than we need to. */
1450
1451
0
  if(!dead_connection && conn->bits.protoconnstart &&
1452
0
     !Curl_pp_needs_flush(data, &pop3c->pp)) {
1453
0
    if(!pop3_perform_quit(data, conn))
1454
0
      (void)pop3_block_statemach(data, conn, TRUE); /* ignore errors on QUIT */
1455
0
  }
1456
1457
  /* Disconnect from the server */
1458
0
  Curl_pp_disconnect(&pop3c->pp);
1459
1460
  /* Cleanup our connection based variables */
1461
0
  Curl_safefree(pop3c->apoptimestamp);
1462
1463
0
  return CURLE_OK;
1464
0
}
1465
1466
/* Call this when the DO phase has completed */
1467
static CURLcode pop3_dophase_done(struct Curl_easy *data, bool connected)
1468
0
{
1469
0
  (void)data;
1470
0
  (void)connected;
1471
1472
0
  return CURLE_OK;
1473
0
}
1474
1475
/* Called from multi.c while DOing */
1476
static CURLcode pop3_doing(struct Curl_easy *data, bool *dophase_done)
1477
0
{
1478
0
  CURLcode result = pop3_multi_statemach(data, dophase_done);
1479
1480
0
  if(result)
1481
0
    DEBUGF(infof(data, "DO phase failed"));
1482
0
  else if(*dophase_done) {
1483
0
    result = pop3_dophase_done(data, FALSE /* not connected */);
1484
1485
0
    DEBUGF(infof(data, "DO phase is complete"));
1486
0
  }
1487
1488
0
  return result;
1489
0
}
1490
1491
/***********************************************************************
1492
 *
1493
 * pop3_regular_transfer()
1494
 *
1495
 * The input argument is already checked for validity.
1496
 *
1497
 * Performs all commands done before a regular transfer between a local and a
1498
 * remote host.
1499
 */
1500
static CURLcode pop3_regular_transfer(struct Curl_easy *data,
1501
                                      bool *dophase_done)
1502
0
{
1503
0
  CURLcode result = CURLE_OK;
1504
0
  bool connected = FALSE;
1505
1506
  /* Make sure size is unknown at this point */
1507
0
  data->req.size = -1;
1508
1509
  /* Set the progress data */
1510
0
  Curl_pgrsSetUploadCounter(data, 0);
1511
0
  Curl_pgrsSetDownloadCounter(data, 0);
1512
0
  Curl_pgrsSetUploadSize(data, -1);
1513
0
  Curl_pgrsSetDownloadSize(data, -1);
1514
1515
  /* Carry out the perform */
1516
0
  result = pop3_perform(data, &connected, dophase_done);
1517
1518
  /* Perform post DO phase operations if necessary */
1519
0
  if(!result && *dophase_done)
1520
0
    result = pop3_dophase_done(data, connected);
1521
1522
0
  return result;
1523
0
}
1524
1525
static void pop3_easy_dtor(void *key, size_t klen, void *entry)
1526
0
{
1527
0
  struct POP3 *pop3 = entry;
1528
0
  (void)key;
1529
0
  (void)klen;
1530
0
  DEBUGASSERT(pop3);
1531
  /* Cleanup our per-request based variables */
1532
0
  Curl_safefree(pop3->id);
1533
0
  Curl_safefree(pop3->custom);
1534
0
  free(pop3);
1535
0
}
1536
1537
static void pop3_conn_dtor(void *key, size_t klen, void *entry)
1538
0
{
1539
0
  struct pop3_conn *pop3c = entry;
1540
0
  (void)key;
1541
0
  (void)klen;
1542
0
  DEBUGASSERT(pop3c);
1543
0
  Curl_pp_disconnect(&pop3c->pp);
1544
0
  Curl_safefree(pop3c->apoptimestamp);
1545
0
  free(pop3c);
1546
0
}
1547
1548
static CURLcode pop3_setup_connection(struct Curl_easy *data,
1549
                                      struct connectdata *conn)
1550
0
{
1551
0
  struct pop3_conn *pop3c;
1552
0
  struct POP3 *pop3 = calloc(1, sizeof(*pop3));
1553
0
  if(!pop3 ||
1554
0
     Curl_meta_set(data, CURL_META_POP3_EASY, pop3, pop3_easy_dtor))
1555
0
    return CURLE_OUT_OF_MEMORY;
1556
1557
0
  pop3c = calloc(1, sizeof(*pop3c));
1558
0
  if(!pop3c ||
1559
0
     Curl_conn_meta_set(conn, CURL_META_POP3_CONN, pop3c, pop3_conn_dtor))
1560
0
    return CURLE_OUT_OF_MEMORY;
1561
1562
0
  return CURLE_OK;
1563
0
}
1564
1565
/***********************************************************************
1566
 *
1567
 * pop3_parse_url_options()
1568
 *
1569
 * Parse the URL login options.
1570
 */
1571
static CURLcode pop3_parse_url_options(struct connectdata *conn)
1572
0
{
1573
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1574
0
  CURLcode result = CURLE_OK;
1575
0
  const char *ptr = conn->options;
1576
1577
0
  if(!pop3c)
1578
0
    return CURLE_FAILED_INIT;
1579
1580
0
  while(!result && ptr && *ptr) {
1581
0
    const char *key = ptr;
1582
0
    const char *value;
1583
1584
0
    while(*ptr && *ptr != '=')
1585
0
      ptr++;
1586
1587
0
    value = ptr + 1;
1588
1589
0
    while(*ptr && *ptr != ';')
1590
0
      ptr++;
1591
1592
0
    if(curl_strnequal(key, "AUTH=", 5)) {
1593
0
      result = Curl_sasl_parse_url_auth_option(&pop3c->sasl,
1594
0
                                               value, ptr - value);
1595
1596
0
      if(result && curl_strnequal(value, "+APOP", ptr - value)) {
1597
0
        pop3c->preftype = POP3_TYPE_APOP;
1598
0
        pop3c->sasl.prefmech = SASL_AUTH_NONE;
1599
0
        result = CURLE_OK;
1600
0
      }
1601
0
    }
1602
0
    else
1603
0
      result = CURLE_URL_MALFORMAT;
1604
1605
0
    if(*ptr == ';')
1606
0
      ptr++;
1607
0
  }
1608
1609
0
  if(pop3c->preftype != POP3_TYPE_APOP)
1610
0
    switch(pop3c->sasl.prefmech) {
1611
0
    case SASL_AUTH_NONE:
1612
0
      pop3c->preftype = POP3_TYPE_NONE;
1613
0
      break;
1614
0
    case SASL_AUTH_DEFAULT:
1615
0
      pop3c->preftype = POP3_TYPE_ANY;
1616
0
      break;
1617
0
    default:
1618
0
      pop3c->preftype = POP3_TYPE_SASL;
1619
0
      break;
1620
0
    }
1621
1622
0
  return result;
1623
0
}
1624
1625
/***********************************************************************
1626
 *
1627
 * pop3_parse_url_path()
1628
 *
1629
 * Parse the URL path into separate path components.
1630
 */
1631
static CURLcode pop3_parse_url_path(struct Curl_easy *data)
1632
0
{
1633
  /* The POP3 struct is already initialised in pop3_connect() */
1634
0
  struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
1635
0
  const char *path = &data->state.up.path[1]; /* skip leading path */
1636
1637
0
  if(!pop3)
1638
0
    return CURLE_FAILED_INIT;
1639
  /* URL decode the path for the message ID */
1640
0
  return Curl_urldecode(path, 0, &pop3->id, NULL, REJECT_CTRL);
1641
0
}
1642
1643
/***********************************************************************
1644
 *
1645
 * pop3_parse_custom_request()
1646
 *
1647
 * Parse the custom request.
1648
 */
1649
static CURLcode pop3_parse_custom_request(struct Curl_easy *data)
1650
0
{
1651
0
  CURLcode result = CURLE_OK;
1652
0
  struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
1653
0
  const char *custom = data->set.str[STRING_CUSTOMREQUEST];
1654
1655
0
  if(!pop3)
1656
0
    return CURLE_FAILED_INIT;
1657
  /* URL decode the custom request */
1658
0
  if(custom)
1659
0
    result = Curl_urldecode(custom, 0, &pop3->custom, NULL, REJECT_CTRL);
1660
1661
0
  return result;
1662
0
}
1663
1664
/***********************************************************************
1665
 *
1666
 * pop3_write()
1667
 *
1668
 * This function scans the body after the end-of-body and writes everything
1669
 * until the end is found.
1670
 */
1671
static CURLcode pop3_write(struct Curl_easy *data, const char *str,
1672
                           size_t nread, bool is_eos)
1673
0
{
1674
  /* This code could be made into a special function in the handler struct */
1675
0
  CURLcode result = CURLE_OK;
1676
0
  struct SingleRequest *k = &data->req;
1677
0
  struct connectdata *conn = data->conn;
1678
0
  struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1679
0
  bool strip_dot = FALSE;
1680
0
  size_t last = 0;
1681
0
  size_t i;
1682
0
  (void)is_eos;
1683
1684
0
  if(!pop3c)
1685
0
    return CURLE_FAILED_INIT;
1686
1687
  /* Search through the buffer looking for the end-of-body marker which is
1688
     5 bytes (0d 0a 2e 0d 0a). Note that a line starting with a dot matches
1689
     the eob so the server will have prefixed it with an extra dot which we
1690
     need to strip out. Additionally the marker could of course be spread out
1691
     over 5 different data chunks. */
1692
0
  for(i = 0; i < nread; i++) {
1693
0
    size_t prev = pop3c->eob;
1694
1695
0
    switch(str[i]) {
1696
0
    case 0x0d:
1697
0
      if(pop3c->eob == 0) {
1698
0
        pop3c->eob++;
1699
1700
0
        if(i) {
1701
          /* Write out the body part that did not match */
1702
0
          result = Curl_client_write(data, CLIENTWRITE_BODY, &str[last],
1703
0
                                     i - last);
1704
1705
0
          if(result)
1706
0
            return result;
1707
1708
0
          last = i;
1709
0
        }
1710
0
      }
1711
0
      else if(pop3c->eob == 3)
1712
0
        pop3c->eob++;
1713
0
      else
1714
        /* If the character match was not at position 0 or 3 then restart the
1715
           pattern matching */
1716
0
        pop3c->eob = 1;
1717
0
      break;
1718
1719
0
    case 0x0a:
1720
0
      if(pop3c->eob == 1 || pop3c->eob == 4)
1721
0
        pop3c->eob++;
1722
0
      else
1723
        /* If the character match was not at position 1 or 4 then start the
1724
           search again */
1725
0
        pop3c->eob = 0;
1726
0
      break;
1727
1728
0
    case 0x2e:
1729
0
      if(pop3c->eob == 2)
1730
0
        pop3c->eob++;
1731
0
      else if(pop3c->eob == 3) {
1732
        /* We have an extra dot after the CRLF which we need to strip off */
1733
0
        strip_dot = TRUE;
1734
0
        pop3c->eob = 0;
1735
0
      }
1736
0
      else
1737
        /* If the character match was not at position 2 then start the search
1738
           again */
1739
0
        pop3c->eob = 0;
1740
0
      break;
1741
1742
0
    default:
1743
0
      pop3c->eob = 0;
1744
0
      break;
1745
0
    }
1746
1747
    /* Did we have a partial match which has subsequently failed? */
1748
0
    if(prev && prev >= pop3c->eob) {
1749
      /* Strip can only be non-zero for the very first mismatch after CRLF
1750
         and then both prev and strip are equal and nothing will be output
1751
         below */
1752
0
      while(prev && pop3c->strip) {
1753
0
        prev--;
1754
0
        pop3c->strip--;
1755
0
      }
1756
1757
0
      if(prev) {
1758
        /* If the partial match was the CRLF and dot then only write the CRLF
1759
           as the server would have inserted the dot */
1760
0
        if(strip_dot && prev - 1 > 0) {
1761
0
          result = Curl_client_write(data, CLIENTWRITE_BODY, POP3_EOB,
1762
0
                                     prev - 1);
1763
0
        }
1764
0
        else if(!strip_dot) {
1765
0
          result = Curl_client_write(data, CLIENTWRITE_BODY, POP3_EOB,
1766
0
                                     prev);
1767
0
        }
1768
0
        else {
1769
0
          result = CURLE_OK;
1770
0
        }
1771
1772
0
        if(result)
1773
0
          return result;
1774
1775
0
        last = i;
1776
0
        strip_dot = FALSE;
1777
0
      }
1778
0
    }
1779
0
  }
1780
1781
0
  if(pop3c->eob == POP3_EOB_LEN) {
1782
    /* We have a full match so the transfer is done, however we must transfer
1783
    the CRLF at the start of the EOB as this is considered to be part of the
1784
    message as per RFC-1939, sect. 3 */
1785
0
    result = Curl_client_write(data, CLIENTWRITE_BODY, POP3_EOB, 2);
1786
1787
0
    k->keepon &= ~KEEP_RECV;
1788
0
    pop3c->eob = 0;
1789
1790
0
    return result;
1791
0
  }
1792
1793
0
  if(pop3c->eob)
1794
    /* While EOB is matching nothing should be output */
1795
0
    return CURLE_OK;
1796
1797
0
  if(nread - last) {
1798
0
    result = Curl_client_write(data, CLIENTWRITE_BODY, &str[last],
1799
0
                               nread - last);
1800
0
  }
1801
1802
0
  return result;
1803
0
}
1804
1805
#endif /* CURL_DISABLE_POP3 */