Skip to content

Commit b31d181

Browse files
committed
Conditionally use af_unix and fileio
1 parent 6dd4311 commit b31d181

File tree

7 files changed

+80
-10
lines changed

7 files changed

+80
-10
lines changed

authfd.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,13 @@ ssh_get_authentication_socket_path(const char *authsocket, int *fdp)
9797
sunaddr.sun_family = AF_UNIX;
9898
strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
9999

100-
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
100+
#ifdef HAVE_AFUNIX_H
101+
sock = w32_afunix_socket(&sunaddr);
102+
#else
103+
sock = socket(AF_UNIX, SOCK_STREAM, 0);
104+
#endif
105+
106+
if (sock == -1)
101107
return SSH_ERR_SYSTEM_ERROR;
102108

103109
/* close on exec */

contrib/win32/openssh/config.h.vs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,9 @@
15261526
/* Use PIPES instead of a socketpair() */
15271527
#define USE_PIPES 1
15281528

1529+
/* Define name for the ssh-agent Windows named pipe */
1530+
#define AGENT_PIPE_ID L"\\\\.\\pipe\\openssh-ssh-agent"
1531+
15291532
/* define 1 if afunix.h is available */
15301533
#define HAVE_AFUNIX_H 1
15311534

contrib/win32/win32compat/ssh-agent/agent.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ HANDLE sshagent_client_primary_token;
4343
static HANDLE ioc_port = NULL;
4444
static BOOL debug_mode = FALSE;
4545

46-
#define AGENT_PIPE_ID L"\\\\.\\pipe\\openssh-ssh-agent"
47-
4846
static HANDLE event_stop_agent;
4947
static OVERLAPPED ol;
5048
static HANDLE pipe;

contrib/win32/win32compat/w32fd.c

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,6 @@ w32_socket(int domain, int type, int protocol)
300300
if (min_index == -1)
301301
return -1;
302302

303-
#ifdef HAVE_AFUNIX_H
304-
pio = socketio_socket(domain, type, protocol);
305-
if (pio == NULL)
306-
return -1;
307-
pio->type = SOCK_FD;
308-
#else
309303
if (domain == AF_UNIX && type == SOCK_STREAM) {
310304
pio = fileio_afunix_socket();
311305
if (pio == NULL)
@@ -317,7 +311,65 @@ w32_socket(int domain, int type, int protocol)
317311
return -1;
318312
pio->type = SOCK_FD;
319313
}
314+
315+
fd_table_set(pio, min_index);
316+
debug4("socket:%d, socktype:%d, io:%p, fd:%d ", pio->sock, type, pio, min_index);
317+
return min_index;
318+
}
319+
320+
int
321+
w32_afunix_socket(struct sockaddr_un* addr)
322+
{
323+
#ifdef HAVE_AFUNIX_H
324+
/*
325+
If HAVE_AFUNIX_H is defined, we can be dealing with the ssh-agent named pipe or
326+
a AF_UNIX socket if ssh forwarding is enabled. If the addr->sun_path is the
327+
the well known named pipe, we open the socket with w32_fileio.
328+
*/
329+
if(strcmp(addr->sun_path, "\\\\.\\pipe\\openssh-ssh-agent") == 0)
330+
return w32_fileio_socket(SOCK_STREAM, 0);
331+
else
332+
return w32_unix_socket(SOCK_STREAM, 0);
333+
#else
334+
return w32_socket(AF_UNIX, SOCK_STREAM, 0);
320335
#endif
336+
}
337+
338+
int
339+
w32_unix_socket(int type, int protocol)
340+
{
341+
int domain = AF_UNIX;
342+
int min_index = fd_table_get_min_index();
343+
struct w32_io* pio = NULL;
344+
345+
errno = 0;
346+
if (min_index == -1)
347+
return -1;
348+
349+
pio = socketio_socket(domain, type, protocol);
350+
if (pio == NULL)
351+
return -1;
352+
pio->type = SOCK_FD;
353+
354+
fd_table_set(pio, min_index);
355+
debug4("socket:%d, socktype:%d, io:%p, fd:%d ", pio->sock, type, pio, min_index);
356+
return min_index;
357+
}
358+
359+
int
360+
w32_fileio_socket(int type, int protocol)
361+
{
362+
int min_index = fd_table_get_min_index();
363+
struct w32_io* pio = NULL;
364+
365+
errno = 0;
366+
if (min_index == -1)
367+
return -1;
368+
369+
pio = fileio_afunix_socket();
370+
if (pio == NULL)
371+
return -1;
372+
pio->type = NONSOCK_FD;
321373

322374
fd_table_set(pio, min_index);
323375
debug4("socket:%d, socktype:%d, io:%p, fd:%d ", pio->sock, type, pio, min_index);

contrib/win32/win32compat/w32fd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ struct w32_io {
125125
#define FILETYPE(pio) (GetFileType(WINHANDLE(pio)))
126126
extern HANDLE main_thread;
127127

128+
int w32_afunix_socket(struct sockaddr_un* addr);
128129
BOOL w32_io_is_blocking(struct w32_io*);
129130
BOOL w32_io_is_io_available(struct w32_io* pio, BOOL rd);
130131
int wait_for_any_event(HANDLE* events, int num_events, DWORD milli_seconds);

misc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,11 @@ unix_listener(const char *path, int backlog, int unlink_first)
19281928
return -1;
19291929
}
19301930

1931+
#ifdef HAVE_AFUNIX_H
1932+
sock = w32_afunix_socket(&sunaddr);
1933+
#else
19311934
sock = socket(PF_UNIX, SOCK_STREAM, 0);
1935+
#endif
19321936
if (sock == -1) {
19331937
saved_errno = errno;
19341938
error_f("socket: %.100s", strerror(errno));

mux.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2268,7 +2268,13 @@ muxclient(const char *path)
22682268
fatal("ControlPath too long ('%s' >= %u bytes)", path,
22692269
(unsigned int)sizeof(addr.sun_path));
22702270

2271-
if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
2271+
#ifdef HAVE_AFUNIX_H
2272+
sock = w32_afunix_socket(&addr);
2273+
#elif
2274+
sock = socket(PF_UNIX, SOCK_STREAM, 0);
2275+
#endif
2276+
2277+
if (sock == -1)
22722278
fatal_f("socket(): %s", strerror(errno));
22732279

22742280
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {

0 commit comments

Comments
 (0)