and less size_t's
[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 #include "mutt_signal.h"
32
33 /* -- data types -- */
34 typedef struct {
35   pid_t pid;
36   int readfd;
37   int writefd;
38 } TUNNEL_DATA;
39
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,
45                                 ssize_t len);
46
47 /* -- public functions -- */
48 int mutt_tunnel_socket_setup (CONNECTION * conn)
49 {
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;
54
55   return 0;
56 }
57
58 static int tunnel_socket_open (CONNECTION * conn)
59 {
60   TUNNEL_DATA *tunnel;
61   int pid;
62   int rc;
63   int pin[2], pout[2];
64
65   tunnel = p_new(TUNNEL_DATA, 1);
66   conn->sockdata = tunnel;
67
68   mutt_message (_("Connecting with \"%s\"..."), Tunnel);
69
70   if ((rc = pipe (pin)) == -1) {
71     mutt_perror ("pipe");
72     return -1;
73   }
74   if ((rc = pipe (pout)) == -1) {
75     mutt_perror ("pipe");
76     return -1;
77   }
78
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)
83       _exit (127);
84     close (pin[0]);
85     close (pin[1]);
86     close (pout[0]);
87     close (pout[1]);
88     close (STDERR_FILENO);
89
90     /* Don't let the subprocess think it can use the controlling tty */
91     setsid ();
92
93     execl ("/bin/sh", "sh", "-c", Tunnel, NULL);
94     _exit (127);
95   }
96   mutt_unblock_signals_system (1);
97
98   if (pid == -1) {
99     close (pin[0]);
100     close (pin[1]);
101     close (pout[0]);
102     close (pout[1]);
103     mutt_perror ("fork");
104     return -1;
105   }
106   if (close (pin[1]) < 0 || close (pout[0]) < 0)
107     mutt_perror ("close");
108
109   fcntl (pin[0], F_SETFD, FD_CLOEXEC);
110   fcntl (pout[1], F_SETFD, FD_CLOEXEC);
111
112   tunnel->readfd = pin[0];
113   tunnel->writefd = pout[1];
114   tunnel->pid = pid;
115
116   conn->fd = 42;                /* stupid hack */
117
118   return 0;
119 }
120
121 static int tunnel_socket_close (CONNECTION * conn)
122 {
123   TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
124   int status;
125
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)));
133     mutt_sleep (2);
134   }
135   p_delete(&conn->sockdata);
136   return 0;
137 }
138
139 static int tunnel_socket_read (CONNECTION * conn, char *buf, ssize_t len)
140 {
141   TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
142   int rc;
143
144   rc = read (tunnel->readfd, buf, len);
145   if (rc == -1) {
146     mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,
147                 strerror (errno));
148     mutt_sleep (1);
149   }
150
151   return rc;
152 }
153
154 static int tunnel_socket_write (CONNECTION * conn, const char *buf,
155                                 ssize_t len)
156 {
157   TUNNEL_DATA *tunnel = (TUNNEL_DATA *) conn->sockdata;
158   int rc;
159
160   rc = write (tunnel->writefd, buf, len);
161   if (rc == -1) {
162     mutt_error (_("Tunnel error talking to %s: %s"), conn->account.host,
163                 strerror (errno));
164     mutt_sleep (1);
165   }
166
167   return rc;
168 }