mutt_*mktemp--
[apps/madmutt.git] / lib-sys / mutt_tunnel.c
1 /*
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>
5  *
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.
9  */
10
11 #include <lib-lib/lib-lib.h>
12
13 #include <netinet/in.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16
17 #include <lib-ui/curses.h>
18
19 #include "mutt.h"
20
21 #include "exit.h"
22 #include "mutt_socket.h"
23 #include "mutt_tunnel.h"
24 #include "mutt_signal.h"
25
26 /* -- data types -- */
27 typedef struct {
28   pid_t pid;
29   int readfd;
30   int writefd;
31 } TUNNEL_DATA;
32
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,
38                                 ssize_t len);
39
40 /* -- public functions -- */
41 int mutt_tunnel_socket_setup (CONNECTION * conn)
42 {
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;
47
48   return 0;
49 }
50
51 static int tunnel_socket_open (CONNECTION * conn)
52 {
53   TUNNEL_DATA *tunnel;
54   int pid;
55   int rc;
56   int pin[2], pout[2];
57
58   tunnel = p_new(TUNNEL_DATA, 1);
59   conn->sockdata = tunnel;
60
61   mutt_message (_("Connecting with \"%s\"..."), Tunnel);
62
63   if ((rc = pipe (pin)) == -1) {
64     mutt_perror ("pipe");
65     return -1;
66   }
67   if ((rc = pipe (pout)) == -1) {
68     mutt_perror ("pipe");
69     return -1;
70   }
71
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)
76       _exit (127);
77     close (pin[0]);
78     close (pin[1]);
79     close (pout[0]);
80     close (pout[1]);
81     close (STDERR_FILENO);
82
83     /* Don't let the subprocess think it can use the controlling tty */
84     setsid ();
85
86     execl ("/bin/sh", "sh", "-c", Tunnel, NULL);
87     _exit (127);
88   }
89   mutt_unblock_signals_system (1);
90
91   if (pid == -1) {
92     close (pin[0]);
93     close (pin[1]);
94     close (pout[0]);
95     close (pout[1]);
96     mutt_perror ("fork");
97     return -1;
98   }
99   if (close (pin[1]) < 0 || close (pout[0]) < 0)
100     mutt_perror ("close");
101
102   fcntl (pin[0], F_SETFD, FD_CLOEXEC);
103   fcntl (pout[1], F_SETFD, FD_CLOEXEC);
104
105   tunnel->readfd = pin[0];
106   tunnel->writefd = pout[1];
107   tunnel->pid = pid;
108
109   conn->fd = 42;                /* stupid hack */
110
111   return 0;
112 }
113
114 static int tunnel_socket_close (CONNECTION * conn)
115 {
116   TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
117   int status;
118
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)));
126     mutt_sleep (2);
127   }
128   p_delete(&conn->sockdata);
129   return 0;
130 }
131
132 static int tunnel_socket_read (CONNECTION * conn, char *buf, ssize_t len)
133 {
134   TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
135   int rc;
136
137   rc = read (tunnel->readfd, buf, len);
138   if (rc == -1) {
139     mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,
140                 strerror (errno));
141     mutt_sleep (1);
142   }
143
144   return rc;
145 }
146
147 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
148                                 ssize_t len)
149 {
150   TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
151   int rc;
152
153   rc = write (tunnel->writefd, buf, len);
154   if (rc == -1) {
155     mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,
156                 strerror (errno));
157     mutt_sleep (1);
158   }
159
160   return rc;
161 }