b74c5c5e1f9d7a9ce37a1f094336d05a1c39fca8
[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 #if HAVE_CONFIG_H
12 # include "config.h"
13 #endif
14
15 #include <netinet/in.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <sys/wait.h>
19 #include <fcntl.h>
20 #include <errno.h>
21
22 #include <lib-lib/mem.h>
23 #include <lib-lib/str.h>
24 #include <lib-lib/macros.h>
25
26 #include "mutt.h"
27
28 #include "exit.h"
29 #include "mutt_socket.h"
30 #include "mutt_tunnel.h"
31
32 /* -- data types -- */
33 typedef struct {
34   pid_t pid;
35   int readfd;
36   int writefd;
37 } TUNNEL_DATA;
38
39 /* forward declarations */
40 static int tunnel_socket_open (CONNECTION *);
41 static int tunnel_socket_close (CONNECTION *);
42 static int tunnel_socket_read (CONNECTION * conn, char *buf, size_t len);
43 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
44                                 size_t len);
45
46 /* -- public functions -- */
47 int mutt_tunnel_socket_setup (CONNECTION * conn)
48 {
49   conn->conn_open = tunnel_socket_open;
50   conn->conn_close = tunnel_socket_close;
51   conn->conn_read = tunnel_socket_read;
52   conn->conn_write = tunnel_socket_write;
53
54   return 0;
55 }
56
57 static int tunnel_socket_open (CONNECTION * conn)
58 {
59   TUNNEL_DATA *tunnel;
60   int pid;
61   int rc;
62   int pin[2], pout[2];
63
64   tunnel = p_new(TUNNEL_DATA, 1);
65   conn->sockdata = tunnel;
66
67   mutt_message (_("Connecting with \"%s\"..."), Tunnel);
68
69   if ((rc = pipe (pin)) == -1) {
70     mutt_perror ("pipe");
71     return -1;
72   }
73   if ((rc = pipe (pout)) == -1) {
74     mutt_perror ("pipe");
75     return -1;
76   }
77
78   mutt_block_signals_system ();
79   if ((pid = fork ()) == 0) {
80     mutt_unblock_signals_system (0);
81     if (dup2 (pout[0], STDIN_FILENO) < 0 || dup2 (pin[1], STDOUT_FILENO) < 0)
82       _exit (127);
83     close (pin[0]);
84     close (pin[1]);
85     close (pout[0]);
86     close (pout[1]);
87     close (STDERR_FILENO);
88
89     /* Don't let the subprocess think it can use the controlling tty */
90     setsid ();
91
92     execl ("/bin/sh", "sh", "-c", Tunnel, NULL);
93     _exit (127);
94   }
95   mutt_unblock_signals_system (1);
96
97   if (pid == -1) {
98     close (pin[0]);
99     close (pin[1]);
100     close (pout[0]);
101     close (pout[1]);
102     mutt_perror ("fork");
103     return -1;
104   }
105   if (close (pin[1]) < 0 || close (pout[0]) < 0)
106     mutt_perror ("close");
107
108   fcntl (pin[0], F_SETFD, FD_CLOEXEC);
109   fcntl (pout[1], F_SETFD, FD_CLOEXEC);
110
111   tunnel->readfd = pin[0];
112   tunnel->writefd = pout[1];
113   tunnel->pid = pid;
114
115   conn->fd = 42;                /* stupid hack */
116
117   return 0;
118 }
119
120 static int tunnel_socket_close (CONNECTION * conn)
121 {
122   TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
123   int status;
124
125   close (tunnel->readfd);
126   close (tunnel->writefd);
127   waitpid (tunnel->pid, &status, 0);
128   if (!WIFEXITED(status) || WEXITSTATUS(status)) {
129     mutt_error(_("Tunnel to %s returned error %d (%s)"),
130                conn->account.host, WEXITSTATUS(status),
131                m_strsysexit(WEXITSTATUS(status)));
132     mutt_sleep (2);
133   }
134   p_delete(&conn->sockdata);
135   return 0;
136 }
137
138 static int tunnel_socket_read (CONNECTION * conn, char *buf, size_t len)
139 {
140   TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
141   int rc;
142
143   rc = read (tunnel->readfd, buf, len);
144   if (rc == -1) {
145     mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,
146                 strerror (errno));
147     mutt_sleep (1);
148   }
149
150   return rc;
151 }
152
153 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
154                                 size_t len)
155 {
156   TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
157   int rc;
158
159   rc = write (tunnel->writefd, buf, len);
160   if (rc == -1) {
161     mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,
162                 strerror (errno));
163     mutt_sleep (1);
164   }
165
166   return rc;
167 }