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