NeoMutt  2025-01-09-117-gace867
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
49{
50 pid_t pid;
51 int fd_read;
53};
54
58static int tunnel_socket_open(struct Connection *conn)
59{
60 int pin[2], pout[2];
61
62 struct TunnelSockData *tunnel = MUTT_MEM_MALLOC(1, struct TunnelSockData);
63 conn->sockdata = tunnel;
64
65 const char *const c_tunnel = cs_subset_string(NeoMutt->sub, "tunnel");
66 mutt_message(_("Connecting with \"%s\"..."), c_tunnel);
67
68 int rc = pipe(pin);
69 if (rc == -1)
70 {
71 mutt_perror("pipe");
72 FREE(&conn->sockdata);
73 return -1;
74 }
75 rc = pipe(pout);
76 if (rc == -1)
77 {
78 mutt_perror("pipe");
79 close(pin[0]);
80 close(pin[1]);
81 FREE(&conn->sockdata);
82 return -1;
83 }
84
86 int pid = fork();
87 if (pid == 0)
88 {
91 const int fd_null = open("/dev/null", O_RDWR);
92 if ((fd_null < 0) || (dup2(pout[0], STDIN_FILENO) < 0) ||
93 (dup2(pin[1], STDOUT_FILENO) < 0) || (dup2(fd_null, STDERR_FILENO) < 0))
94 {
95 _exit(127);
96 }
97 close(pin[0]);
98 close(pin[1]);
99 close(pout[0]);
100 close(pout[1]);
101 close(fd_null);
102
103 /* Don't let the subprocess think it can use the controlling tty */
104 setsid();
105
106 execle(EXEC_SHELL, "sh", "-c", c_tunnel, NULL, NeoMutt->env);
107 _exit(127);
108 }
110
111 if (pid == -1)
112 {
113 mutt_perror("fork");
114 close(pin[0]);
115 close(pin[1]);
116 close(pout[0]);
117 close(pout[1]);
118 FREE(&conn->sockdata);
119 return -1;
120 }
121 if ((close(pin[1]) < 0) || (close(pout[0]) < 0))
122 mutt_perror("close");
123
124 // These are unlikely to fail
125 (void) fcntl(pin[0], F_SETFD, FD_CLOEXEC);
126 (void) 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.
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:58
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:93
#define mutt_message(...)
Definition: logging2.h:92
#define mutt_perror(...)
Definition: logging2.h:94
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_MALLOC(n, type)
Definition: memory.h:41
#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:170
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:337
void mutt_sig_block_system(void)
Block signals before calling exec()
Definition: signal.c:261
void mutt_sig_unblock_system(bool restore)
Restore previously blocked signals.
Definition: signal.c:285
#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:43
char ** env
Private copy of the environment variables.
Definition: neomutt.h:55
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:47
A network tunnel (pair of sockets)
Definition: tunnel.c:49
int fd_read
File descriptor to read from.
Definition: tunnel.c:51
pid_t pid
Process ID of tunnel program.
Definition: tunnel.c:50
int fd_write
File descriptor to write to.
Definition: tunnel.c:52
void mutt_tunnel_socket_setup(struct Connection *conn)
Sets up tunnel connection functions.
Definition: tunnel.c:242