NeoMutt  2023-05-17-33-gce4425
Teaching an old dog new tricks
DOXYGEN
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/wait.h>
36#include <time.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 "lib.h"
43#include "globals.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(sizeof(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 {
90 const int fd_null = open("/dev/null", O_RDWR);
91 if ((fd_null < 0) || (dup2(pout[0], STDIN_FILENO) < 0) ||
92 (dup2(pin[1], STDOUT_FILENO) < 0) || (dup2(fd_null, STDERR_FILENO) < 0))
93 {
94 _exit(127);
95 }
96 close(pin[0]);
97 close(pin[1]);
98 close(pout[0]);
99 close(pout[1]);
100 close(fd_null);
101
102 /* Don't let the subprocess think it can use the controlling tty */
103 setsid();
104
105 execle(EXEC_SHELL, "sh", "-c", c_tunnel, NULL, EnvList);
106 _exit(127);
107 }
109
110 if (pid == -1)
111 {
112 mutt_perror("fork");
113 close(pin[0]);
114 close(pin[1]);
115 close(pout[0]);
116 close(pout[1]);
117 FREE(&conn->sockdata);
118 return -1;
119 }
120 if ((close(pin[1]) < 0) || (close(pout[0]) < 0))
121 mutt_perror("close");
122
123 fcntl(pin[0], F_SETFD, FD_CLOEXEC);
124 fcntl(pout[1], F_SETFD, FD_CLOEXEC);
125
126 tunnel->fd_read = pin[0];
127 tunnel->fd_write = pout[1];
128 tunnel->pid = pid;
129
130 conn->fd = 42; /* stupid hack */
131
132 /* Note we are using ssf as a boolean in this case. See the notes in
133 * conn/connection.h */
134 const bool c_tunnel_is_secure = cs_subset_bool(NeoMutt->sub, "tunnel_is_secure");
135 if (c_tunnel_is_secure)
136 conn->ssf = 1;
137
138 return 0;
139}
140
144static int tunnel_socket_read(struct Connection *conn, char *buf, size_t count)
145{
146 struct TunnelSockData *tunnel = conn->sockdata;
147 int rc;
148
149 do
150 {
151 rc = read(tunnel->fd_read, buf, count);
152 } while (rc < 0 && errno == EINTR);
153
154 if (rc < 0)
155 {
156 mutt_error(_("Tunnel error talking to %s: %s"), conn->account.host, strerror(errno));
157 return -1;
158 }
159
160 return rc;
161}
162
166static int tunnel_socket_write(struct Connection *conn, const char *buf, size_t count)
167{
168 struct TunnelSockData *tunnel = conn->sockdata;
169 int rc;
170 size_t sent = 0;
171
172 do
173 {
174 do
175 {
176 rc = write(tunnel->fd_write, buf + sent, count - sent);
177 } while (rc < 0 && errno == EINTR);
178
179 if (rc < 0)
180 {
181 mutt_error(_("Tunnel error talking to %s: %s"), conn->account.host, strerror(errno));
182 return -1;
183 }
184
185 sent += rc;
186 } while (sent < count);
187
188 return sent;
189}
190
194static int tunnel_socket_poll(struct Connection *conn, time_t wait_secs)
195{
196 struct TunnelSockData *tunnel = conn->sockdata;
197 int ofd;
198 int rc;
199
200 ofd = conn->fd;
201 conn->fd = tunnel->fd_read;
202 rc = raw_socket_poll(conn, wait_secs);
203 conn->fd = ofd;
204
205 return rc;
206}
207
211static int tunnel_socket_close(struct Connection *conn)
212{
213 struct TunnelSockData *tunnel = conn->sockdata;
214 if (!tunnel)
215 {
216 return 0;
217 }
218
219 int status;
220
221 close(tunnel->fd_read);
222 close(tunnel->fd_write);
223 waitpid(tunnel->pid, &status, 0);
224 if (!WIFEXITED(status) || WEXITSTATUS(status))
225 {
226 mutt_error(_("Tunnel to %s returned error %d (%s)"), conn->account.host,
227 WEXITSTATUS(status), NONULL(mutt_str_sysexit(WEXITSTATUS(status))));
228 }
229 FREE(&conn->sockdata);
230
231 return 0;
232}
233
241{
242 conn->open = tunnel_socket_open;
244 conn->read = tunnel_socket_read;
246 conn->poll = tunnel_socket_poll;
247}
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
#define EXEC_SHELL
Definition: filter.h:27
char ** EnvList
Private copy of the environment variables.
Definition: globals.c:91
static int tunnel_socket_close(struct Connection *conn)
Close a tunnel socket - Implements Connection::close() -.
Definition: tunnel.c:211
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)
Checks whether reads would block - Implements Connection::poll() -.
Definition: raw.c:340
static int tunnel_socket_poll(struct Connection *conn, time_t wait_secs)
Checks whether tunnel reads would block - Implements Connection::poll() -.
Definition: tunnel.c:194
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:144
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:166
#define mutt_error(...)
Definition: logging2.h:87
#define mutt_message(...)
Definition: logging2.h:86
#define mutt_perror(...)
Definition: logging2.h:88
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
#define FREE(x)
Definition: memory.h:43
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:167
GUI display the mailboxes in a side panel.
void mutt_sig_block_system(void)
Block signals before calling exec()
Definition: signal.c:197
void mutt_sig_unblock_system(bool restore)
Restore previously blocked signals.
Definition: signal.c:221
Key value store.
#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:56
int(* poll)(struct Connection *conn, time_t wait_secs)
Definition: connection.h:106
int(* write)(struct Connection *conn, const char *buf, size_t count)
Definition: connection.h:93
unsigned int ssf
Security strength factor, in bits (see notes)
Definition: connection.h:51
int(* close)(struct Connection *conn)
Definition: connection.h:117
struct ConnAccount account
Account details: username, password, etc.
Definition: connection.h:50
int(* open)(struct Connection *conn)
Definition: connection.h:67
int fd
Socket file descriptor.
Definition: connection.h:54
int(* read)(struct Connection *conn, char *buf, size_t count)
Definition: connection.h:80
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
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:240