Commit b0ccfea9 b0ccfea9cb042f8a95fe6dbad2a5597362bc2b65 by Wojciech Polak

Added TLS/SSL support (via GnuTLS)

1 parent 681f85c7
1 2003-01-13 Wojciech Polak
2
3 * auth/tls.c: New file. Added main TLS/SSL code (via GnuTLS).
4 * include/mailutils/tls.h: New file. TLS header file.
5 * auth/Makefile.am: Updated.
6 * include/mailutils/Makefile.am: Likewise.
7
8 * pop3d/pop3d.c: Added TLS support.
9 * pop3d/pop3d.h: include <mailutils/tls.h>
10 (pop3d_stls): New prototype.
11 * pop3d/stls.c: New file. The STLS command support.
12 * pop3d/extra.c (pop3d_init_tls_server): New function.
13 (pop3d_deinit_tls_server): New function.
14 (pop3d_outf): Added TLS support (GnuTLS).
15 (pop3d_readline): Added TLS support (GnuTLS).
16 * pop3d/capa.c (pop3d_capa): Added STLS.
17 * pop3d/Makefile.am: Updated.
18
19 * m4/tls.m4: New file. Implements MU_CHECK_TLS defun.
20 * m4/Makefile.am: Updated.
21 * configure.ac: Added GnuTLS support (--with-gnutls).
22
1 2003-01-13 Sergey Poznyakoff 23 2003-01-13 Sergey Poznyakoff
2 24
3 * m4/gssapi.m4: Fixed typo. 25 * m4/gssapi.m4: Fixed typo.
......
1 ## Process this file with GNU Automake to create Makefile.in 1 ## Process this file with GNU Automake to create Makefile.in
2 2
3 ## Copyright (C) 2002 Free Software Foundation, Inc. 3 ## Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4 ## 4 ##
5 ## GNU Mailtuils is free software; you can redistribute it and/or 5 ## GNU Mailutils is free software; you can redistribute it and/or
6 ## modify it under the terms of the GNU General Public License as 6 ## modify it under the terms of the GNU General Public License as
7 ## published by the Free Software Foundation; either version 2, or (at 7 ## published by the Free Software Foundation; either version 2, or (at
8 ## your option) any later version. 8 ## your option) any later version.
9 ## 9 ##
10 ## This program is distributed in the hope that it will be useful, but 10 ## GNU Mailutils is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details. 13 ## General Public License for more details.
14 ## 14 ##
15 ## You should have received a copy of the GNU General Public License 15 ## You should have received a copy of the GNU General Public License
16 ## along with this program; if not, write to the Free Software 16 ## along with GNU Mailutils; if not, write to the Free Software
17 ## Foundation, Inc. 17 ## Foundation, Inc.
18 ## 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 ## 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 19
...@@ -25,6 +25,7 @@ lib_LTLIBRARIES = libmuauth.la ...@@ -25,6 +25,7 @@ lib_LTLIBRARIES = libmuauth.la
25 libmuauth_la_SOURCES = \ 25 libmuauth_la_SOURCES = \
26 pam.c \ 26 pam.c \
27 sql.c \ 27 sql.c \
28 tls.c \
28 virtual.c 29 virtual.c
29 30
30 libmuauth_la_LIBADD = @LTLIBINTL@ 31 libmuauth_la_LIBADD = @LTLIBINTL@
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 GNU Mailutils is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Library Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 GNU Mailutils is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Library General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Mailutils; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25
26 #include <mailutils/argp.h>
27 #include <mailutils/error.h>
28 #include <mailutils/mu_auth.h>
29 #include <mailutils/tls.h>
30 #include <mailutils/nls.h>
31
32 #ifdef WITH_TLS
33
34 #define DH_BITS 768
35
36 static gnutls_dh_params dh_params;
37 static gnutls_certificate_server_credentials x509_cred;
38 static char *ssl_cert = NULL;
39 static char *ssl_key = NULL;
40 static char *ssl_cafile = NULL;
41
42 #define ARG_SSL_CERT 1
43 #define ARG_SSL_KEY 2
44 #define ARG_SSL_CAFILE 3
45
46 static struct argp_option _tls_argp_options[] = {
47 {NULL, 0, NULL, 0, N_("Encryption options"), 0},
48 {"ssl-cert", ARG_SSL_CERT, N_("FILE"), 0,
49 N_("Specify SSL certificate file"), 0},
50 {"ssl-key", ARG_SSL_KEY, N_("KEY"), 0,
51 N_("Specify SSL certificate key"), 0},
52 {"ssl-cafile", ARG_SSL_CAFILE, N_("FILE"), 0,
53 N_("Specify trusted CAs file"), 0},
54 {NULL, 0, NULL, 0, NULL, 0}
55 };
56
57 static error_t
58 _tls_argp_parser (int key, char *arg, struct argp_state *state)
59 {
60 switch (key)
61 {
62 case ARG_SSL_CERT:
63 ssl_cert = arg;
64 break;
65
66 case ARG_SSL_KEY:
67 ssl_key = arg;
68 break;
69
70 case ARG_SSL_CAFILE:
71 ssl_cafile = arg;
72 break;
73
74 default:
75 return ARGP_ERR_UNKNOWN;
76 }
77 return 0;
78 }
79
80 static struct argp _tls_argp = {
81 _tls_argp_options,
82 _tls_argp_parser
83 };
84
85 static struct argp_child _tls_argp_child = {
86 &_tls_argp,
87 0,
88 NULL,
89 0
90 };
91
92 void
93 mu_tls_init_argp ()
94 {
95 if (mu_register_capa ("tls", &_tls_argp_child))
96 {
97 mu_error (_("INTERNAL ERROR: cannot register argp capability tls"));
98 abort ();
99 }
100 }
101
102 int
103 mu_check_tls_environment (void)
104 {
105 /* Return: zero means NOT READY, one means READY */
106
107 if (ssl_cert && ssl_key)
108 {
109 struct stat st;
110
111 if (stat (ssl_cert, &st) == -1)
112 return 0;
113 if (!(st.st_mode & S_IFREG) || !(st.st_mode & S_IFLNK))
114 {
115 mu_error (_("%s is not a regular file or a symbolic link."),
116 ssl_cert);
117 return 0;
118 }
119 if ((st.st_mode & S_IRWXG) || (st.st_mode & S_IRWXO))
120 {
121 mu_error (_("Wrong permissions on %s. Set 0600."), ssl_cert);
122 return 0;
123 }
124
125 if (stat (ssl_key, &st) == -1)
126 return 0;
127 if (!(st.st_mode & S_IFREG) || !(st.st_mode & S_IFLNK))
128 {
129 mu_error (_("%s is not a regular file or a symbolic link."),
130 ssl_key);
131 return 0;
132 }
133 if ((st.st_mode & S_IRWXG) || (st.st_mode & S_IRWXO))
134 {
135 mu_error (_("Wrong permissions on %s. Set 0600."), ssl_key);
136 return 0;
137 }
138 }
139 else
140 return 0;
141
142 return 1;
143 }
144
145 int
146 mu_init_tls_libs (void)
147 {
148 int rs = 1;
149 rs = gnutls_global_init ();
150
151 if (rs == 0) /* Reverse for tls_available */
152 rs = 1;
153
154 return rs; /* Returns 1 on success */
155 }
156
157 int
158 mu_deinit_tls_libs (void)
159 {
160 gnutls_global_deinit ();
161 }
162
163 static void
164 generate_dh_params (void)
165 {
166 gnutls_datum prime, generator;
167
168 gnutls_dh_params_init (&dh_params);
169 gnutls_dh_params_generate (&prime, &generator, DH_BITS);
170 gnutls_dh_params_set (dh_params, prime, generator, DH_BITS);
171
172 free (prime.data);
173 free (generator.data);
174 }
175
176 static gnutls_session
177 initialize_tls_session (void)
178 {
179 gnutls_session session = 0;
180
181 gnutls_init (&session, GNUTLS_SERVER);
182 gnutls_set_default_priority (session);
183 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
184 gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUEST);
185 gnutls_dh_set_prime_bits (session, DH_BITS);
186
187 return (gnutls_session) session;
188 }
189
190 gnutls_session
191 mu_init_tls_server (int fd_in, int fd_out)
192 {
193 int rs = 0;
194 gnutls_session session = 0;
195
196 gnutls_certificate_allocate_credentials (&x509_cred);
197
198 if (ssl_cafile)
199 gnutls_certificate_set_x509_trust_file (x509_cred, ssl_cafile,
200 GNUTLS_X509_FMT_PEM);
201
202 gnutls_certificate_set_x509_key_file (x509_cred,
203 ssl_cert, ssl_key,
204 GNUTLS_X509_FMT_PEM);
205
206 generate_dh_params ();
207 gnutls_certificate_set_dh_params (x509_cred, dh_params);
208
209 session = initialize_tls_session ();
210 gnutls_transport_set_ptr2 (session, fd_in, fd_out);
211
212 rs = gnutls_handshake (session);
213 if (rs < 0)
214 {
215 gnutls_deinit (session);
216 mu_error (_("TLS/SSL handshake failed!"));
217 gnutls_perror (rs);
218 return 0; /* failed */
219 }
220 return (gnutls_session) session;
221 }
222
223 void
224 mu_deinit_tls_server (gnutls_session session)
225 {
226 if (session)
227 {
228 gnutls_bye (session, GNUTLS_SHUT_RDWR);
229 gnutls_deinit (session);
230 }
231 }
232
233 #endif /* WITH_TLS */
234
235 /* EOF */
1 # Configuration for GNU mailutils - a suite of utilities for electronic mail 1 # Configuration for GNU mailutils - a suite of utilities for electronic mail
2 # 2 #
3 # Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc. 3 # Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 # 4 #
5 # GNU Mailutils is free software; you can redistribute it and/or modify 5 # GNU Mailutils is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by 6 # it under the terms of the GNU General Public License as published by
...@@ -113,6 +113,13 @@ if test "x$WITH_GSSAPI" != "xno"; then ...@@ -113,6 +113,13 @@ if test "x$WITH_GSSAPI" != "xno"; then
113 fi 113 fi
114 AC_SUBST(AUTHOBJS) 114 AC_SUBST(AUTHOBJS)
115 115
116 MU_CHECK_TLS
117 if test "$WITH_GNUTLS" != "no"; then
118 AUTHLIBS="$AUTHLIBS $TLS_LIBS"
119 AC_DEFINE(WITH_GNUTLS,1,[Define if mailutils is using GNU TLS])
120 AC_DEFINE(WITH_TLS,1,[Define if mailutils is using TLS])
121 fi
122
116 AC_ARG_WITH([guile], 123 AC_ARG_WITH([guile],
117 AC_HELP_STRING([--without-guile], 124 AC_HELP_STRING([--without-guile],
118 [do not build guile interface]), 125 [do not build guile interface]),
......
1 ## Process this file with GNU Automake to create Makefile.in 1 ## Process this file with GNU Automake to create Makefile.in
2 2
3 ## Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc. 3 ## Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 ## 4 ##
5 ## GNU Mailtuils is free software; you can redistribute it and/or 5 ## GNU Mailutils is free software; you can redistribute it and/or
6 ## modify it under the terms of the GNU General Public License as 6 ## modify it under the terms of the GNU General Public License as
7 ## published by the Free Software Foundation; either version 2, or (at 7 ## published by the Free Software Foundation; either version 2, or (at
8 ## your option) any later version. 8 ## your option) any later version.
9 ## 9 ##
10 ## This program is distributed in the hope that it will be useful, but 10 ## GNU Mailutils is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details. 13 ## General Public License for more details.
14 ## 14 ##
15 ## You should have received a copy of the GNU General Public License 15 ## You should have received a copy of the GNU General Public License
16 ## along with this program; if not, write to the Free Software 16 ## along with GNU Mailutils; if not, write to the Free Software
17 ## Foundation, Inc. 17 ## Foundation, Inc.
18 ## 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 ## 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 19
...@@ -50,6 +50,7 @@ pkginclude_HEADERS = \ ...@@ -50,6 +50,7 @@ pkginclude_HEADERS = \
50 property.h \ 50 property.h \
51 registrar.h \ 51 registrar.h \
52 stream.h \ 52 stream.h \
53 tls.h \
53 types.h \ 54 types.h \
54 url.h 55 url.h
55 56
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 GNU Mailutils is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Library Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 GNU Mailutils is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Library General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Mailutils; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifndef _MAILUTILS_TLS_H
19 #define _MAILUTILS_TLS_H
20
21 #ifdef WITH_TLS
22
23 #ifdef WITH_GNUTLS
24 # include <gnutls/gnutls.h>
25 #endif /* WITH_GNUTLS */
26
27 #endif /* WITH_TLS */
28
29 #endif /* _MAILUTILS_TLS_H */
30
1 ## Process this file with GNU Automake to create Makefile.in 1 ## Process this file with GNU Automake to create Makefile.in
2 2
3 ## Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc. 3 ## Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 ## 4 ##
5 ## GNU Mailtuils is free software; you can redistribute it and/or 5 ## GNU Mailutils is free software; you can redistribute it and/or
6 ## modify it under the terms of the GNU General Public License as 6 ## modify it under the terms of the GNU General Public License as
7 ## published by the Free Software Foundation; either version 2, or (at 7 ## published by the Free Software Foundation; either version 2, or (at
8 ## your option) any later version. 8 ## your option) any later version.
9 ## 9 ##
10 ## This program is distributed in the hope that it will be useful, but 10 ## GNU Mailutils is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details. 13 ## General Public License for more details.
14 ## 14 ##
15 ## You should have received a copy of the GNU General Public License 15 ## You should have received a copy of the GNU General Public License
16 ## along with this program; if not, write to the Free Software 16 ## along with GNU Mailutils; if not, write to the Free Software
17 ## Foundation, Inc. 17 ## Foundation, Inc.
18 ## 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 ## 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 19
...@@ -27,6 +27,7 @@ EXTRA_DIST = \ ...@@ -27,6 +27,7 @@ EXTRA_DIST = \
27 realloc.m4 \ 27 realloc.m4 \
28 regex.m4 \ 28 regex.m4 \
29 utmp.m4 \ 29 utmp.m4 \
30 tls.m4 \
30 iconv.m4 \ 31 iconv.m4 \
31 codeset.m4 \ 32 codeset.m4 \
32 gettext.m4 \ 33 gettext.m4 \
......
1 AC_DEFUN(MU_CHECK_TLS,
2 [
3 if test "x$WITH_GNUTLS" = x; then
4 cached=""
5 AC_ARG_WITH([gnutls],
6 AC_HELP_STRING([--with-gnutls],
7 [use GNU TLS library]),
8 [WITH_GNUTLS=$withval],
9 [WITH_GNUTLS=no])
10
11 if test "$WITH_GNUTLS" != "no"; then
12 AC_CHECK_HEADER(gnutls/gnutls.h,
13 [:],
14 [WITH_GNUTLS=no])
15 if test "$WITH_GNUTLS" != "no"; then
16 saved_LIBS=$LIBS
17 AC_CHECK_LIB(gnutls, gnutls_global_init,
18 [TLS_LIBS="-lgnutls"],
19 [WITH_GNUTLS=no])
20 AC_CHECK_LIB(gcrypt, main,
21 [TLS_LIBS="$TLS_LIBS -lgcrypt"],
22 [WITH_GNUTLS=no])
23 LIBS=$saved_LIBS
24 fi
25 fi
26 else
27 cached=" (cached) "
28 fi
29 AC_MSG_CHECKING([whether to use TLS libraries])
30 AC_MSG_RESULT(${cached}${WITH_GNUTLS})])
1 ## Process this file with GNU Automake to create Makefile.in 1 ## Process this file with GNU Automake to create Makefile.in
2 2
3 ## Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc. 3 ## Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 ## 4 ##
5 ## GNU Mailtuils is free software; you can redistribute it and/or 5 ## GNU Mailutils is free software; you can redistribute it and/or
6 ## modify it under the terms of the GNU General Public License as 6 ## modify it under the terms of the GNU General Public License as
7 ## published by the Free Software Foundation; either version 2, or (at 7 ## published by the Free Software Foundation; either version 2, or (at
8 ## your option) any later version. 8 ## your option) any later version.
9 ## 9 ##
10 ## This program is distributed in the hope that it will be useful, but 10 ## GNU Mailutils is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details. 13 ## General Public License for more details.
14 ## 14 ##
15 ## You should have received a copy of the GNU General Public License 15 ## You should have received a copy of the GNU General Public License
16 ## along with this program; if not, write to the Free Software 16 ## along with GNU Mailutils; if not, write to the Free Software
17 ## Foundation, Inc. 17 ## Foundation, Inc.
18 ## 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 ## 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 19
...@@ -25,8 +25,8 @@ EXTRA_PROGRAMS = popauth ...@@ -25,8 +25,8 @@ EXTRA_PROGRAMS = popauth
25 SUBDIRS = testsuite 25 SUBDIRS = testsuite
26 26
27 pop3d_SOURCES = apop.c auth.c capa.c dele.c extra.c pop3d.c pop3d.h \ 27 pop3d_SOURCES = apop.c auth.c capa.c dele.c extra.c pop3d.c pop3d.h \
28 list.c lock.c noop.c quit.c retr.c rset.c stat.c signal.c top.c uidl.c \ 28 list.c lock.c noop.c quit.c retr.c rset.c stat.c stls.c signal.c top.c \
29 user.c 29 uidl.c user.c
30 30
31 pop3d_LDADD = ../mailbox/libmailbox.la @AUTHLIBS@ ../lib/libmailutils.la @LTLIBINTL@ 31 pop3d_LDADD = ../mailbox/libmailbox.la @AUTHLIBS@ ../lib/libmailutils.la @LTLIBINTL@
32 32
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail 1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2001 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001, 2003 Free Software Foundation, Inc.
3 3
4 GNU Mailutils is free software; you can redistribute it and/or modify 4 GNU Mailutils is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 5 it under the terms of the GNU General Public License as published by
...@@ -41,6 +41,12 @@ pop3d_capa (const char *arg) ...@@ -41,6 +41,12 @@ pop3d_capa (const char *arg)
41 pop3d_outf ("UIDL\r\n"); 41 pop3d_outf ("UIDL\r\n");
42 pop3d_outf ("RESP-CODES\r\n"); 42 pop3d_outf ("RESP-CODES\r\n");
43 pop3d_outf ("PIPELINING\r\n"); 43 pop3d_outf ("PIPELINING\r\n");
44
45 #ifdef WITH_TLS
46 if (tls_available && tls_done == 0)
47 pop3d_outf ("STLS\r\n");
48 #endif /* WITH_TLS */
49
44 /* FIXME: This can be Implemented by setting an header field on the 50 /* FIXME: This can be Implemented by setting an header field on the
45 message. */ 51 message. */
46 /*pop3d_outf ("EXPIRE NEVER\r\n"); */ 52 /*pop3d_outf ("EXPIRE NEVER\r\n"); */
......
...@@ -19,6 +19,9 @@ ...@@ -19,6 +19,9 @@
19 19
20 static FILE *ifile; 20 static FILE *ifile;
21 static FILE *ofile; 21 static FILE *ofile;
22 #ifdef WITH_TLS
23 static gnutls_session sfile;
24 #endif /* WITH_TLS */
22 25
23 /* Takes a string as input and returns either the remainder of the string 26 /* Takes a string as input and returns either the remainder of the string
24 after the first space, or a zero length string if no space */ 27 after the first space, or a zero length string if no space */
...@@ -89,7 +92,7 @@ int ...@@ -89,7 +92,7 @@ int
89 pop3d_abquit (int reason) 92 pop3d_abquit (int reason)
90 { 93 {
91 /* Unlock spool */ 94 /* Unlock spool */
92 pop3d_unlock(); 95 pop3d_unlock ();
93 mailbox_close (mbox); 96 mailbox_close (mbox);
94 mailbox_destroy (&mbox); 97 mailbox_destroy (&mbox);
95 98
...@@ -119,7 +122,8 @@ pop3d_abquit (int reason) ...@@ -119,7 +122,8 @@ pop3d_abquit (int reason)
119 122
120 case ERR_MBOX_SYNC: 123 case ERR_MBOX_SYNC:
121 syslog (LOG_ERR, _("Mailbox was updated by other party: %s"), username); 124 syslog (LOG_ERR, _("Mailbox was updated by other party: %s"), username);
122 pop3d_outf ("-ERR [OUT-SYNC] Mailbox updated by other party or corrupt\r\n"); 125 pop3d_outf
126 ("-ERR [OUT-SYNC] Mailbox updated by other party or corrupt\r\n");
123 break; 127 break;
124 128
125 default: 129 default:
...@@ -128,12 +132,12 @@ pop3d_abquit (int reason) ...@@ -128,12 +132,12 @@ pop3d_abquit (int reason)
128 break; 132 break;
129 } 133 }
130 134
131 closelog(); 135 closelog ();
132 exit (EXIT_FAILURE); 136 exit (EXIT_FAILURE);
133 } 137 }
134 138
135 void 139 void
136 pop3d_setio (FILE *in, FILE *out) 140 pop3d_setio (FILE * in, FILE * out)
137 { 141 {
138 if (!in || !out) 142 if (!in || !out)
139 pop3d_abquit (ERR_NO_OFILE); 143 pop3d_abquit (ERR_NO_OFILE);
...@@ -142,6 +146,26 @@ pop3d_setio (FILE *in, FILE *out) ...@@ -142,6 +146,26 @@ pop3d_setio (FILE *in, FILE *out)
142 ofile = out; 146 ofile = out;
143 } 147 }
144 148
149 #ifdef WITH_TLS
150
151 int
152 pop3d_init_tls_server ()
153 {
154 sfile =
155 (gnutls_session) mu_init_tls_server (fileno (ifile), fileno (ofile));
156 if (!sfile)
157 return 0;
158 return 1;
159 }
160
161 void
162 pop3d_deinit_tls_server ()
163 {
164 mu_deinit_tls_server (sfile);
165 }
166
167 #endif /* WITH_TLS */
168
145 void 169 void
146 pop3d_flush_output () 170 pop3d_flush_output ()
147 { 171 {
...@@ -169,7 +193,22 @@ pop3d_outf (const char *fmt, ...) ...@@ -169,7 +193,22 @@ pop3d_outf (const char *fmt, ...)
169 free (buf); 193 free (buf);
170 } 194 }
171 } 195 }
196
197 #ifdef WITH_TLS
198 if (tls_done)
199 {
200 char *buf;
201 vasprintf (&buf, fmt, ap);
202 if (buf)
203 {
204 gnutls_record_send (sfile, buf, strlen (buf));
205 free (buf);
206 }
207 }
208 else
209 #endif /* WITH_TLS */
172 vfprintf (ofile, fmt, ap); 210 vfprintf (ofile, fmt, ap);
211
173 va_end (ap); 212 va_end (ap);
174 } 213 }
175 214
...@@ -181,6 +220,14 @@ pop3d_readline (char *buffer, size_t size) ...@@ -181,6 +220,14 @@ pop3d_readline (char *buffer, size_t size)
181 char *ptr; 220 char *ptr;
182 221
183 alarm (daemon_param.timeout); 222 alarm (daemon_param.timeout);
223 #ifdef WITH_TLS
224 if (tls_done)
225 {
226 gnutls_record_recv (sfile, buffer, size - 1);
227 ptr = buffer;
228 }
229 else
230 #endif /* WITH_TLS */
184 ptr = fgets (buffer, size, ifile); 231 ptr = fgets (buffer, size, ifile);
185 alarm (0); 232 alarm (0);
186 233
......
...@@ -32,6 +32,11 @@ struct daemon_param daemon_param = { ...@@ -32,6 +32,11 @@ struct daemon_param daemon_param = {
32 32
33 int debug_mode; 33 int debug_mode;
34 34
35 #ifdef WITH_TLS
36 int tls_available;
37 int tls_done;
38 #endif /* WITH_TLS */
39
35 /* Number of child processes. */ 40 /* Number of child processes. */
36 volatile size_t children; 41 volatile size_t children;
37 42
...@@ -57,6 +62,9 @@ static struct argp argp = { ...@@ -57,6 +62,9 @@ static struct argp argp = {
57 static const char *pop3d_argp_capa[] = { 62 static const char *pop3d_argp_capa[] = {
58 "daemon", 63 "daemon",
59 "auth", 64 "auth",
65 #ifdef WITH_TLS
66 "tls",
67 #endif /* WITH_TLS */
60 "common", 68 "common",
61 "mailbox", 69 "mailbox",
62 "logging", 70 "logging",
...@@ -90,6 +98,9 @@ main (int argc, char **argv) ...@@ -90,6 +98,9 @@ main (int argc, char **argv)
90 mu_init_nls (); 98 mu_init_nls ();
91 99
92 MU_AUTH_REGISTER_ALL_MODULES(); 100 MU_AUTH_REGISTER_ALL_MODULES();
101 #ifdef WITH_TLS
102 mu_tls_init_argp ();
103 #endif /* WITH_TLS */
93 mu_argp_parse (&argp, &argc, &argv, 0, pop3d_argp_capa, NULL, &daemon_param); 104 mu_argp_parse (&argp, &argc, &argv, 0, pop3d_argp_capa, NULL, &daemon_param);
94 105
95 #ifdef USE_LIBPAM 106 #ifdef USE_LIBPAM
...@@ -154,6 +165,13 @@ main (int argc, char **argv) ...@@ -154,6 +165,13 @@ main (int argc, char **argv)
154 165
155 umask (S_IROTH | S_IWOTH | S_IXOTH); /* 007 */ 166 umask (S_IROTH | S_IWOTH | S_IXOTH); /* 007 */
156 167
168 /* Check TLS environment, i.e. cert and key files */
169 #ifdef WITH_TLS
170 tls_available = mu_check_tls_environment ();
171 if (tls_available)
172 tls_available = mu_init_tls_libs ();
173 #endif /* WITH_TLS */
174
157 /* Actually run the daemon. */ 175 /* Actually run the daemon. */
158 if (daemon_param.mode == MODE_DAEMON) 176 if (daemon_param.mode == MODE_DAEMON)
159 pop3d_daemon (daemon_param.maxchildren, daemon_param.port); 177 pop3d_daemon (daemon_param.maxchildren, daemon_param.port);
...@@ -330,6 +348,10 @@ pop3d_mainloop (int fd, FILE *infile, FILE *outfile) ...@@ -330,6 +348,10 @@ pop3d_mainloop (int fd, FILE *infile, FILE *outfile)
330 status = pop3d_uidl (arg); 348 status = pop3d_uidl (arg);
331 else if (strncasecmp (cmd, "CAPA", 4) == 0) 349 else if (strncasecmp (cmd, "CAPA", 4) == 0)
332 status = pop3d_capa (arg); 350 status = pop3d_capa (arg);
351 #ifdef WITH_TLS
352 else if ((strncasecmp (cmd, "STLS", 4) == 0) && tls_available)
353 status = pop3d_stls (arg);
354 #endif /* WITH_TLS */
333 else 355 else
334 status = ERR_BAD_CMD; 356 status = ERR_BAD_CMD;
335 357
...@@ -362,6 +384,11 @@ pop3d_mainloop (int fd, FILE *infile, FILE *outfile) ...@@ -362,6 +384,11 @@ pop3d_mainloop (int fd, FILE *infile, FILE *outfile)
362 free (arg); 384 free (arg);
363 } 385 }
364 386
387 #ifdef WITH_TLS
388 pop3d_deinit_tls_server ();
389 mu_deinit_tls_libs ();
390 #endif /* WITH_TLS */
391
365 return (status != OK); 392 return (status != OK);
366 } 393 }
367 394
......
...@@ -123,6 +123,7 @@ ...@@ -123,6 +123,7 @@
123 #include <mailutils/registrar.h> 123 #include <mailutils/registrar.h>
124 #include <mailutils/url.h> 124 #include <mailutils/url.h>
125 #include <mailutils/mu_auth.h> 125 #include <mailutils/mu_auth.h>
126 #include <mailutils/tls.h>
126 #include <mailutils/nls.h> 127 #include <mailutils/nls.h>
127 128
128 /* For Berkley DB2 APOP password file */ 129 /* For Berkley DB2 APOP password file */
...@@ -175,6 +176,8 @@ extern char *md5shared; ...@@ -175,6 +176,8 @@ extern char *md5shared;
175 extern volatile size_t children; 176 extern volatile size_t children;
176 extern struct daemon_param daemon_param; 177 extern struct daemon_param daemon_param;
177 extern int debug_mode; 178 extern int debug_mode;
179 extern int tls_available;
180 extern int tls_done;
178 181
179 extern int pop3d_abquit __P ((int)); 182 extern int pop3d_abquit __P ((int));
180 extern int pop3d_apop __P ((const char *)); 183 extern int pop3d_apop __P ((const char *));
...@@ -193,6 +196,9 @@ extern int pop3d_rset __P ((const char *)); ...@@ -193,6 +196,9 @@ extern int pop3d_rset __P ((const char *));
193 extern RETSIGTYPE pop3d_sigchld __P ((int)); 196 extern RETSIGTYPE pop3d_sigchld __P ((int));
194 extern RETSIGTYPE pop3d_signal __P ((int)); 197 extern RETSIGTYPE pop3d_signal __P ((int));
195 extern int pop3d_stat __P ((const char *)); 198 extern int pop3d_stat __P ((const char *));
199 #ifdef WITH_TLS
200 extern int pop3d_stls __P ((const char *));
201 #endif /* WITH_TLS */
196 extern int pop3d_top __P ((const char *)); 202 extern int pop3d_top __P ((const char *));
197 extern int pop3d_touchlock __P ((void)); 203 extern int pop3d_touchlock __P ((void));
198 extern int pop3d_uidl __P ((const char *)); 204 extern int pop3d_uidl __P ((const char *));
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 GNU Mailutils is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 GNU Mailutils is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Mailutils; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #include "pop3d.h"
19
20 /* STLS command -- TLS/SSL encryption */
21
22 #ifdef WITH_TLS
23
24 int
25 pop3d_stls (const char *arg)
26 {
27 if (strlen (arg) != 0)
28 return ERR_BAD_ARGS;
29
30 if (state != AUTHORIZATION)
31 return ERR_WRONG_STATE;
32
33 pop3d_outf ("+OK Begin TLS negotiation\r\n");
34 pop3d_flush_output ();
35
36 tls_done = pop3d_init_tls_server ();
37
38 if (!tls_done)
39 return ERR_UNKNOWN;
40
41 return OK;
42 }
43
44 #endif /* WITH_TLS */
45