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.
11 #include <lib-lib/lib-lib.h>
13 #include <netinet/in.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
17 #include <lib-ui/curses.h>
22 #include "mutt_socket.h"
23 #include "mutt_tunnel.h"
24 #include "mutt_signal.h"
26 /* -- data types -- */
33 /* forward declarations */
34 static int tunnel_socket_open (CONNECTION *);
35 static int tunnel_socket_close (CONNECTION *);
36 static int tunnel_socket_read (CONNECTION * conn, char *buf, ssize_t len);
37 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
40 /* -- public functions -- */
41 int mutt_tunnel_socket_setup (CONNECTION * conn)
43 conn->conn_open = tunnel_socket_open;
44 conn->conn_close = tunnel_socket_close;
45 conn->conn_read = tunnel_socket_read;
46 conn->conn_write = tunnel_socket_write;
51 static int tunnel_socket_open (CONNECTION * conn)
58 tunnel = p_new(TUNNEL_DATA, 1);
59 conn->sockdata = tunnel;
61 mutt_message (_("Connecting with \"%s\"..."), Tunnel);
63 if ((rc = pipe (pin)) == -1) {
67 if ((rc = pipe (pout)) == -1) {
72 mutt_block_signals_system ();
73 if ((pid = fork ()) == 0) {
74 mutt_unblock_signals_system (0);
75 if (dup2 (pout[0], STDIN_FILENO) < 0 || dup2 (pin[1], STDOUT_FILENO) < 0)
81 close (STDERR_FILENO);
83 /* Don't let the subprocess think it can use the controlling tty */
86 execl ("/bin/sh", "sh", "-c", Tunnel, NULL);
89 mutt_unblock_signals_system (1);
99 if (close (pin[1]) < 0 || close (pout[0]) < 0)
100 mutt_perror ("close");
102 fcntl (pin[0], F_SETFD, FD_CLOEXEC);
103 fcntl (pout[1], F_SETFD, FD_CLOEXEC);
105 tunnel->readfd = pin[0];
106 tunnel->writefd = pout[1];
109 conn->fd = 42; /* stupid hack */
114 static int tunnel_socket_close (CONNECTION * conn)
116 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
119 close (tunnel->readfd);
120 close (tunnel->writefd);
121 waitpid (tunnel->pid, &status, 0);
122 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
123 mutt_error(_("Tunnel to %s returned error %d (%s)"),
124 conn->account.host, WEXITSTATUS(status),
125 m_strsysexit(WEXITSTATUS(status)));
128 p_delete(&conn->sockdata);
132 static int tunnel_socket_read (CONNECTION * conn, char *buf, ssize_t len)
134 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
137 rc = read (tunnel->readfd, buf, len);
139 mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,
147 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
150 TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
153 rc = write (tunnel->writefd, buf, len);
155 mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,