2 * Copyright notice from original mutt:
3 * Copyright (C) 2000 Manoj Kasichainula <manoj@io.com>
4 * Copyright (C) 2001,2005 Brendan Cully <brendan@kublai.com>
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
15 #include <netinet/in.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
22 #include <lib-lib/mem.h>
23 #include <lib-lib/str.h>
24 #include <lib-lib/macros.h>
29 #include "mutt_socket.h"
30 #include "mutt_tunnel.h"
31 #include "mutt_signal.h"
33 /* -- data types -- */
40 /* forward declarations */
41 static int tunnel_socket_open (CONNECTION *);
42 static int tunnel_socket_close (CONNECTION *);
43 static int tunnel_socket_read (CONNECTION * conn, char *buf, ssize_t len);
44 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
47 /* -- public functions -- */
48 int mutt_tunnel_socket_setup (CONNECTION * conn)
50 conn->conn_open = tunnel_socket_open;
51 conn->conn_close = tunnel_socket_close;
52 conn->conn_read = tunnel_socket_read;
53 conn->conn_write = tunnel_socket_write;
58 static int tunnel_socket_open (CONNECTION * conn)
65 tunnel = p_new(TUNNEL_DATA, 1);
66 conn->sockdata = tunnel;
68 mutt_message (_("Connecting with \"%s\"..."), Tunnel);
70 if ((rc = pipe (pin)) == -1) {
74 if ((rc = pipe (pout)) == -1) {
79 mutt_block_signals_system ();
80 if ((pid = fork ()) == 0) {
81 mutt_unblock_signals_system (0);
82 if (dup2 (pout[0], STDIN_FILENO) < 0 || dup2 (pin[1], STDOUT_FILENO) < 0)
88 close (STDERR_FILENO);
90 /* Don't let the subprocess think it can use the controlling tty */
93 execl ("/bin/sh", "sh", "-c", Tunnel, NULL);
96 mutt_unblock_signals_system (1);
103 mutt_perror ("fork");
106 if (close (pin[1]) < 0 || close (pout[0]) < 0)
107 mutt_perror ("close");
109 fcntl (pin[0], F_SETFD, FD_CLOEXEC);
110 fcntl (pout[1], F_SETFD, FD_CLOEXEC);
112 tunnel->readfd = pin[0];
113 tunnel->writefd = pout[1];
116 conn->fd = 42; /* stupid hack */
121 static int tunnel_socket_close (CONNECTION * conn)
123 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
126 close (tunnel->readfd);
127 close (tunnel->writefd);
128 waitpid (tunnel->pid, &status, 0);
129 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
130 mutt_error(_("Tunnel to %s returned error %d (%s)"),
131 conn->account.host, WEXITSTATUS(status),
132 m_strsysexit(WEXITSTATUS(status)));
135 p_delete(&conn->sockdata);
139 static int tunnel_socket_read (CONNECTION * conn, char *buf, ssize_t len)
141 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
144 rc = read (tunnel->readfd, buf, len);
146 mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,
154 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
157 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
160 rc = write (tunnel->writefd, buf, len);
162 mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,