1 /*
2 * libEtPan! -- a mail stuff library
3 *
4 * Copyright (C) 2001, 2005 - DINH Viet Hoa
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the libEtPan! project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * $Id: mailpop3_ssl.c,v 1.17 2009/12/19 00:57:31 hoa Exp $
34 */
35
36 #ifdef HAVE_CONFIG_H
37 # include <config.h>
38 #endif
39
40 #include "mailpop3_ssl.h"
41
42 #include "mailpop3.h"
43 #include "mailstream_cfstream.h"
44
45 #include "connect.h"
46 #ifdef HAVE_NETINET_IN_H
47 # include <netinet/in.h>
48 #endif
49 #ifdef HAVE_UNISTD_H
50 # include <unistd.h>
51 #endif
52 #include <stdlib.h>
53
54 #define DEFAULT_POP3S_PORT 995
55 #define SERVICE_NAME_POP3S "pop3s"
56 #define SERVICE_TYPE_TCP "tcp"
57
58 #if HAVE_CFNETWORK
59 static int mailpop3_cfssl_connect(mailpop3 * f, const char * server, uint16_t port);
60 #endif
61
mailpop3_ssl_connect(mailpop3 * f,const char * server,uint16_t port)62 int mailpop3_ssl_connect(mailpop3 * f, const char * server, uint16_t port)
63 {
64 return mailpop3_ssl_connect_with_callback(f, server, port,
65 NULL, NULL);
66 }
67
mailpop3_ssl_connect_with_callback(mailpop3 * f,const char * server,uint16_t port,void (* callback)(struct mailstream_ssl_context * ssl_context,void * data),void * data)68 int mailpop3_ssl_connect_with_callback(mailpop3 * f, const char * server, uint16_t port,
69 void (* callback)(struct mailstream_ssl_context * ssl_context, void * data), void * data)
70 {
71 int s;
72 mailstream * stream;
73
74 #if HAVE_CFNETWORK
75 if (mailstream_cfstream_enabled) {
76 if (callback == NULL) {
77 return mailpop3_cfssl_connect(f, server, port);
78 }
79 }
80 #endif
81
82 if (port == 0) {
83 port = mail_get_service_port(SERVICE_NAME_POP3S, SERVICE_TYPE_TCP);
84 if (port == 0)
85 port = DEFAULT_POP3S_PORT;
86 }
87
88 /* Connection */
89
90 s = mail_tcp_connect_timeout(server, port, f->pop3_timeout);
91 if (s == -1)
92 return MAILPOP3_ERROR_CONNECTION_REFUSED;
93
94 stream = mailstream_ssl_open_with_callback_timeout(s, f->pop3_timeout, callback, data);
95 if (stream == NULL) {
96 #ifdef WIN32
97 closesocket(s);
98 #else
99 close(s);
100 #endif
101 return MAILPOP3_ERROR_SSL;
102 }
103
104 return mailpop3_connect(f, stream);
105 }
106
107 #if HAVE_CFNETWORK
mailpop3_cfssl_connect_ssl_level(mailpop3 * f,const char * server,uint16_t port,int ssl_level)108 static int mailpop3_cfssl_connect_ssl_level(mailpop3 * f, const char * server, uint16_t port, int ssl_level)
109 {
110 mailstream * stream;
111 int r;
112
113 stream = mailstream_cfstream_open_timeout(server, port, f->pop3_timeout);
114 if (stream == NULL) {
115 return MAILPOP3_ERROR_CONNECTION_REFUSED;
116 }
117 mailstream_cfstream_set_ssl_level(stream, ssl_level);
118 mailstream_cfstream_set_ssl_verification_mask(stream, MAILSTREAM_CFSTREAM_SSL_NO_VERIFICATION);
119 r = mailstream_cfstream_set_ssl_enabled(stream, 1);
120 if (r < 0) {
121 mailstream_close(stream);
122 return MAILPOP3_ERROR_SSL;
123 }
124
125 return mailpop3_connect(f, stream);
126 }
127
mailpop3_cfssl_connect(mailpop3 * f,const char * server,uint16_t port)128 static int mailpop3_cfssl_connect(mailpop3 * f, const char * server, uint16_t port)
129 {
130 return mailpop3_cfssl_connect_ssl_level(f, server, port, MAILSTREAM_CFSTREAM_SSL_LEVEL_NEGOCIATED_SSL);
131 }
132 #endif
133