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