NeoMutt  2024-04-25-89-g194907
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
tunnel.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <errno.h>
32#include <fcntl.h>
33#include <stdbool.h>
34#include <string.h>
35#include <sys/types.h>
36#include <sys/wait.h>
37#include <unistd.h>
38#include "private.h"
39#include "mutt/lib.h"
40#include "config/lib.h"
41#include "core/lib.h"
42#include "connaccount.h"
43#include "connection.h"
44#include "globals.h"
45
50{
51 pid_t pid;
52 int fd_read;
54};
55
59static int tunnel_socket_open(struct Connection *conn)
60{
61 int pin[2], pout[2];
62
63 struct TunnelSockData *tunnel = mutt_mem_malloc(sizeof(struct TunnelSockData));
64 conn->sockdata = tunnel;
65
66 const char *const c_tunnel = cs_subset_string(NeoMutt->sub, "tunnel");
67 mutt_message(_("Connecting with \"%s\"..."), c_tunnel);
68
69 int rc = pipe(pin);
70 if (rc == -1)
71 {
72 mutt_perror("pipe");
73 FREE(&conn->sockdata);
74 return -1;
75 }
76 rc = pipe(pout);
77 if (rc == -1)
78 {
79 mutt_perror("pipe");
80 close(pin[0]);
81 close(pin[1]);
82 FREE(&conn->sockdata);
83 return -1;
84 }
85
87 int pid = fork();
88 if (pid == 0)
89 {
92 const int fd_null = open("/dev/null", O_RDWR);
93 if ((fd_null < 0) || (dup2(pout[0], STDIN_FILENO) < 0) ||
94 (dup2(pin[1], STDOUT_FILENO) < 0) || (dup2(fd_null, STDERR_FILENO) < 0))
95 {
96 _exit(127);
97 }
98 close(pin[0]);
99 close(pin[1]);
100 close(pout[0]);
101 close(pout[1]);
102 close(fd_null);
103
104 /* Don't let the subprocess think it can use the controlling tty */
105 setsid();
106
107 execle(EXEC_SHELL, "sh", "-c", c_tunnel, NULL, EnvList);
108 _exit(127);
109 }
111
112 if (pid == -1)
113 {
114 mutt_perror("fork");
115 close(pin[0]);
116 close(pin[1]);
117 close(pout[0]);
118 close(pout[1]);
119 FREE(&conn->sockdata);
120 return -1;
121 }
122 if ((close(pin[1]) < 0) || (close(pout[0]) < 0))
123 mutt_perror("close");
124
125 fcntl(pin[0], F_SETFD, FD_CLOEXEC);
126 fcntl(pout[1], F_SETFD, FD_CLOEXEC);
127
128 tunnel->fd_read = pin[0];
129 tunnel->fd_write = pout[1];
130 tunnel->pid = pid;
131
132 conn->fd = 42; /* stupid hack */
133
134 /* Note we are using ssf as a boolean in this case. See the notes in
135 * conn/connection.h */
136 const bool c_tunnel_is_secure = cs_subset_bool(NeoMutt->sub, "tunnel_is_secure");
137 if (c_tunnel_is_secure)
138 conn->ssf = 1;
139
140 return 0;
141}
142
146static int tunnel_socket_read(struct Connection *conn, char *buf, size_t count)
147{
148 struct TunnelSockData *tunnel = conn->sockdata;
149 int rc;
150
151 do
152 {
153 rc = read(tunnel->fd_read, buf, count);
154 } while (rc < 0 && errno == EINTR);
155
156 if (rc < 0)
157 {
158 mutt_error(_("Tunnel error talking to %s: %s"), conn->account.host, strerror(errno));
159 return -1;
160 }
161
162 return rc;
163}
164
168static int tunnel_socket_write(struct Connection *conn, const char *buf, size_t count)
169{
170 struct TunnelSockData *tunnel = conn->sockdata;
171 int rc;
172 size_t sent = 0;
173
174 do
175 {
176 do
177 {
178 rc = write(tunnel->fd_write, buf + sent, count - sent);
179 } while (rc < 0 && errno == EINTR);
180
181 if (rc < 0)
182 {
183 mutt_error(_("Tunnel error talking to %s: %s"), conn->account.host, strerror(errno));
184 return -1;
185 }
186
187 sent += rc;
188 } while (sent < count);
189
190 return sent;
191}
192
196static int tunnel_socket_poll(struct Connection *conn, time_t wait_secs)
197{
198 struct TunnelSockData *tunnel = conn->sockdata;
199 int ofd;
200 int rc;
201
202 ofd = conn->fd;
203 conn->fd = tunnel->fd_read;
204 rc = raw_socket_poll(conn, wait_secs);
205 conn->fd = ofd;
206
207 return rc;
208}
209
213static int tunnel_socket_close(struct Connection *conn)
214{
215 struct TunnelSockData *tunnel = conn->sockdata;
216 if (!tunnel)
217 {
218 return 0;
219 }
220
221 int status;
222
223 close(tunnel->fd_read);
224 close(tunnel->fd_write);
225 waitpid(tunnel->pid, &status, 0);
226 if (!WIFEXITED(status) || WEXITSTATUS(status))
227 {
228 mutt_error(_("Tunnel to %s returned error %d (%s)"), conn->account.host,
229 WEXITSTATUS(status), NONULL(mutt_str_sysexit(WEXITSTATUS(status))));
230 }
231 FREE(&conn->sockdata);
232
233 return 0;
234}
235
243{
244 conn->open = tunnel_socket_open;
246 conn->read = tunnel_socket_read;
248 conn->poll = tunnel_socket_poll;
249}
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:291
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
Convenience wrapper for the config headers.
Connection Credentials.
An open network connection (socket)
Convenience wrapper for the core headers.
char ** EnvList
Private copy of the environment variables.
Definition: globals.c:78
static int tunnel_socket_close(struct Connection *conn)
Close a tunnel socket - Implements Connection::close() -.
Definition: tunnel.c:213
static int tunnel_socket_open(struct Connection *conn)
Open a tunnel socket - Implements Connection::open() -.
Definition: tunnel.c:59
int raw_socket_poll(struct Connection *conn, time_t wait_secs)
Check if any data is waiting on a socket - Implements Connection::poll() -.
Definition: raw.c:355
static int tunnel_socket_poll(struct Connection *conn, time_t wait_secs)
Check if any data is waiting on a socket - Implements Connection::poll() -.
Definition: tunnel.c:196
static int tunnel_socket_read(struct Connection *conn, char *buf, size_t count)
Read data from a tunnel socket - Implements Connection::read() -.
Definition: tunnel.c:146
static int tunnel_socket_write(struct Connection *conn, const char *buf, size_t count)
Write data to a tunnel socket - Implements Connection::write() -.
Definition: tunnel.c:168
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_message(...)
Definition: logging2.h:91
#define mutt_perror(...)
Definition: logging2.h:93
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:91
#define FREE(x)
Definition: memory.h:45
#define EXEC_SHELL
Definition: filter.h:29
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
const char * mutt_str_sysexit(int err_num)
Return a string matching an error code.
Definition: string.c:169
GUI display the mailboxes in a side panel.
void mutt_sig_reset_child_signals(void)
Reset ignored signals back to the default.
Definition: signal.c:321
void mutt_sig_block_system(void)
Block signals before calling exec()
Definition: signal.c:245
void mutt_sig_unblock_system(bool restore)
Restore previously blocked signals.
Definition: signal.c:269
#define NONULL(x)
Definition: string2.h:37
char host[128]
Server to login to.
Definition: connaccount.h:54
void * sockdata
Backend-specific socket data.
Definition: connection.h:55
int(* poll)(struct Connection *conn, time_t wait_secs)
Definition: connection.h:105
int(* write)(struct Connection *conn, const char *buf, size_t count)
Definition: connection.h:92
unsigned int ssf
Security strength factor, in bits (see notes)
Definition: connection.h:50
int(* close)(struct Connection *conn)
Definition: connection.h:116
struct ConnAccount account
Account details: username, password, etc.
Definition: connection.h:49
int(* open)(struct Connection *conn)
Definition: connection.h:66
int fd
Socket file descriptor.
Definition: connection.h:53
int(* read)(struct Connection *conn, char *buf, size_t count)
Definition: connection.h:79
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
A network tunnel (pair of sockets)
Definition: tunnel.c:50
int fd_read
File descriptor to read from.
Definition: tunnel.c:52
pid_t pid
Process ID of tunnel program.
Definition: tunnel.c:51
int fd_write
File descriptor to write to.
Definition: tunnel.c:53
void mutt_tunnel_socket_setup(struct Connection *conn)
Sets up tunnel connection functions.
Definition: tunnel.c:242