Nico Golde:
[apps/madmutt.git] / mutt_tunnel.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 2000 Manoj Kasichainula <manoj@io.com>
4  * Copyright (C) 2001 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 "mutt.h"
16 #include "mutt_socket.h"
17 #include "mutt_tunnel.h"
18
19 #include <netinet/in.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <fcntl.h>
24 #include <errno.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, size_t len);
37 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
38                                 size_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 = (TUNNEL_DATA *) safe_malloc (sizeof (TUNNEL_DATA));
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 (EXECSHELL, "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
118   close (tunnel->readfd);
119   close (tunnel->writefd);
120   waitpid (tunnel->pid, NULL, 0);
121   FREE (&conn->sockdata);
122
123   return 0;
124 }
125
126 static int tunnel_socket_read (CONNECTION * conn, char *buf, size_t len)
127 {
128   TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
129   int rc;
130
131   rc = read (tunnel->readfd, buf, len);
132   if (rc == -1) {
133     mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,
134                 strerror (errno));
135     mutt_sleep (1);
136   }
137
138   return rc;
139 }
140
141 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
142                                 size_t len)
143 {
144   TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
145   int rc;
146
147   rc = write (tunnel->writefd, buf, len);
148   if (rc == -1) {
149     mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,
150                 strerror (errno));
151     mutt_sleep (1);
152   }
153
154   return rc;
155 }