Skip to content

Commit c8b8c4b

Browse files
committed
Source snapshot from Powershell/openssh-portable:latestw_all
1 parent e404237 commit c8b8c4b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+6314
-5970
lines changed

auth-passwd.c

+18-19
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "hostfile.h"
5555
#include "auth.h"
5656
#include "auth-options.h"
57+
#include "authfd.h"
5758

5859
extern Buffer loginmsg;
5960
extern ServerOptions options;
@@ -225,38 +226,36 @@ sys_auth_passwd(Authctxt *authctxt, const char *password)
225226

226227
#elif defined(WINDOWS)
227228
/*
228-
* Authenticate on Windows - Pass creds to ssh-agent and retrieve token
229+
* Authenticate on Windows - Pass credentials to ssh-agent and retrieve token
229230
* upon succesful authentication
230231
*/
231232
extern int auth_sock;
232233
int sys_auth_passwd(Authctxt *authctxt, const char *password)
233234
{
234-
u_char *blob = NULL;
235235
size_t blen = 0;
236236
DWORD token = 0;
237237
struct sshbuf *msg = NULL;
238+
int r;
238239

239240
msg = sshbuf_new();
240241
if (!msg)
241-
return 0;
242-
if (sshbuf_put_u8(msg, 100) != 0 ||
243-
sshbuf_put_cstring(msg, "password") != 0 ||
244-
sshbuf_put_cstring(msg, authctxt->user) != 0 ||
245-
sshbuf_put_cstring(msg, password) != 0 ||
246-
ssh_request_reply(auth_sock, msg, msg) != 0 ||
247-
sshbuf_get_u32(msg, &token) != 0) {
248-
debug("auth agent did not authorize client %s", authctxt->pw->pw_name);
249-
return 0;
250-
}
242+
fatal("%s: out of memory", __func__);
251243

252-
253-
if (blob)
254-
free(blob);
244+
if (sshbuf_put_u8(msg, SSH_AGENT_AUTHENTICATE) != 0 ||
245+
sshbuf_put_cstring(msg, PASSWD_AUTH_REQUEST) != 0 ||
246+
sshbuf_put_cstring(msg, authctxt->pw->pw_name) != 0 ||
247+
sshbuf_put_cstring(msg, password) != 0 ||
248+
ssh_request_reply(auth_sock, msg, msg) != 0 ||
249+
sshbuf_get_u32(msg, &token) != 0) {
250+
debug("auth agent did not authorize client %s", authctxt->user);
251+
r = 0;
252+
goto done;
253+
}
254+
authctxt->methoddata = (void*)(INT_PTR)token;
255+
r = 1;
256+
done:
255257
if (msg)
256258
sshbuf_free(msg);
257-
258-
authctxt->methoddata = (void*)(INT_PTR)token;
259-
260-
return 1;
259+
return r;
261260
}
262261
#endif /* WINDOWS */

auth2-pubkey.c

+25-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: auth2-pubkey.c,v 1.61 2016/12/30 22:08:02 djm Exp $ */
1+
/* $OpenBSD: auth2-pubkey.c,v 1.62 2017/01/30 01:03:00 djm Exp $ */
22
/*
33
* Copyright (c) 2000 Markus Friedl. All rights reserved.
44
*
@@ -68,6 +68,7 @@
6868
#include "ssherr.h"
6969
#include "channels.h" /* XXX for session.h */
7070
#include "session.h" /* XXX for child_set_env(); refactor? */
71+
#include "authfd.h"
7172

7273
/* import */
7374
extern ServerOptions options;
@@ -189,21 +190,21 @@ userauth_pubkey(Authctxt *authctxt)
189190
while (1) {
190191
msg = sshbuf_new();
191192
if (!msg)
192-
break;
193-
if ((r = sshbuf_put_u8(msg, 100)) != 0 ||
194-
(r = sshbuf_put_cstring(msg, "pubkey")) != 0 ||
195-
(r = sshkey_to_blob(key, &blob, &blen)) != 0 ||
196-
(r = sshbuf_put_string(msg, blob, blen)) != 0 ||
197-
(r = sshbuf_put_cstring(msg, authctxt->pw->pw_name)) != 0 ||
198-
(r = sshbuf_put_string(msg, sig, slen)) != 0 ||
199-
(r = sshbuf_put_string(msg, buffer_ptr(&b), buffer_len(&b))) != 0 ||
200-
(r = ssh_request_reply(auth_sock, msg, msg)) != 0 ||
201-
(r = sshbuf_get_u32(msg, &token)) != 0) {
202-
debug("auth agent did not authorize client %s", authctxt->pw->pw_name);
193+
fatal("%s: out of memory", __func__);
194+
if ((r = sshbuf_put_u8(msg, SSH_AGENT_AUTHENTICATE)) != 0 ||
195+
(r = sshbuf_put_cstring(msg, PUBKEY_AUTH_REQUEST)) != 0 ||
196+
(r = sshkey_to_blob(key, &blob, &blen)) != 0 ||
197+
(r = sshbuf_put_string(msg, blob, blen)) != 0 ||
198+
(r = sshbuf_put_cstring(msg, authctxt->pw->pw_name)) != 0 ||
199+
(r = sshbuf_put_string(msg, sig, slen)) != 0 ||
200+
(r = sshbuf_put_string(msg, buffer_ptr(&b), buffer_len(&b))) != 0 ||
201+
(r = ssh_request_reply(auth_sock, msg, msg)) != 0 ||
202+
(r = sshbuf_get_u32(msg, &token)) != 0) {
203+
debug("auth agent did not authorize client %s", authctxt->user);
203204
break;
204205
}
205206

206-
debug3("auth agent authenticated %s", authctxt->pw->pw_name);
207+
debug3("auth agent authenticated %s", authctxt->user);
207208
break;
208209

209210
}
@@ -620,9 +621,12 @@ process_principals(FILE *f, char *file, struct passwd *pw,
620621
{
621622
char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
622623
u_long linenum = 0;
623-
u_int i;
624+
u_int i, found_principal = 0;
624625

625626
while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
627+
/* Always consume entire input */
628+
if (found_principal)
629+
continue;
626630
/* Skip leading whitespace. */
627631
for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
628632
;
@@ -655,11 +659,12 @@ process_principals(FILE *f, char *file, struct passwd *pw,
655659
if (auth_parse_options(pw, line_opts,
656660
file, linenum) != 1)
657661
continue;
658-
return 1;
662+
found_principal = 1;
663+
continue;
659664
}
660665
}
661666
}
662-
return 0;
667+
return found_principal;
663668
}
664669

665670
static int
@@ -827,6 +832,9 @@ check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw)
827832
char *cp, *key_options = NULL, *fp = NULL;
828833
const char *reason = NULL;
829834

835+
/* Always consume entrire file */
836+
if (found_key)
837+
continue;
830838
if (found != NULL)
831839
key_free(found);
832840
found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
@@ -913,7 +921,7 @@ check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw)
913921
file, linenum, key_type(found), fp);
914922
free(fp);
915923
found_key = 1;
916-
break;
924+
continue;
917925
}
918926
}
919927
if (found != NULL)

auth2.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: auth2.c,v 1.136 2016/05/02 08:49:03 djm Exp $ */
1+
/* $OpenBSD: auth2.c,v 1.137 2017/02/03 23:05:57 djm Exp $ */
22
/*
33
* Copyright (c) 2000 Markus Friedl. All rights reserved.
44
*
@@ -212,6 +212,7 @@ input_service_request(int type, u_int32_t seq, void *ctxt)
212212
static int
213213
input_userauth_request(int type, u_int32_t seq, void *ctxt)
214214
{
215+
struct ssh *ssh = active_state; /* XXX */
215216
Authctxt *authctxt = ctxt;
216217
Authmethod *m = NULL;
217218
char *user, *service, *method, *style = NULL;
@@ -235,9 +236,10 @@ input_userauth_request(int type, u_int32_t seq, void *ctxt)
235236
authctxt->user = xstrdup(user);
236237
if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
237238
authctxt->valid = 1;
238-
debug2("input_userauth_request: setting up authctxt for %s", user);
239+
debug2("%s: setting up authctxt for %s",
240+
__func__, user);
239241
} else {
240-
logit("input_userauth_request: invalid user %s", user);
242+
/* Invalid user, fake password information */
241243
authctxt->pw = fakepw();
242244
#ifdef SSH_AUDIT_EVENTS
243245
PRIVSEP(audit_event(SSH_INVALID_USER));
@@ -247,6 +249,8 @@ input_userauth_request(int type, u_int32_t seq, void *ctxt)
247249
if (options.use_pam)
248250
PRIVSEP(start_pam(authctxt));
249251
#endif
252+
ssh_packet_set_log_preamble(ssh, "%suser %s",
253+
authctxt->valid ? "authenticating " : "invalid ", user);
250254
setproctitle("%s%s", authctxt->valid ? user : "unknown",
251255
use_privsep ? " [net]" : "");
252256
authctxt->service = xstrdup(service);
@@ -292,6 +296,7 @@ void
292296
userauth_finish(Authctxt *authctxt, int authenticated, const char *method,
293297
const char *submethod)
294298
{
299+
struct ssh *ssh = active_state; /* XXX */
295300
char *methods;
296301
int partial = 0;
297302

@@ -353,6 +358,7 @@ userauth_finish(Authctxt *authctxt, int authenticated, const char *method,
353358
packet_write_wait();
354359
/* now we can break out */
355360
authctxt->success = 1;
361+
ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
356362
} else {
357363

358364
/* Allow initial try of "none" auth without failure penalty */

authfd.c

+14-10
Original file line numberDiff line numberDiff line change
@@ -97,35 +97,39 @@ ssh_get_authentication_socket(int *fdp)
9797
#ifdef WINDOWS
9898
/* Auth socket in Windows is a static-named pipe listener in ssh-agent */
9999
{
100-
#define SSH_AGENT_REG_ROOT L"SOFTWARE\\SSH\\Agent"
101-
#define SSH_AGENT_PIPE_NAME L"\\\\.\\pipe\\ssh-agent"
102100
HKEY agent_root = 0;
103101
DWORD agent_pid = 0, tmp_size = 4, pipe_server_pid = 0xff;
102+
DWORD connection_attempts = 0;
104103
HANDLE h;
105-
RegOpenKeyExW(HKEY_LOCAL_MACHINE, SSH_AGENT_REG_ROOT, 0, KEY_QUERY_VALUE, &agent_root);
104+
RegOpenKeyExW(HKEY_LOCAL_MACHINE, SSH_AGENT_REG_ROOT,
105+
0, KEY_QUERY_VALUE, &agent_root);
106106
if (agent_root) {
107-
RegQueryValueEx(agent_root, "ProcessId", 0, NULL, (LPBYTE)&agent_pid, &tmp_size);
107+
RegQueryValueEx(agent_root, "ProcessId", 0,
108+
NULL, (LPBYTE)&agent_pid, &tmp_size);
108109
RegCloseKey(agent_root);
109110
}
110111

111112
do {
112113
h = CreateFileW(SSH_AGENT_PIPE_NAME, GENERIC_READ | GENERIC_WRITE, 0,
113-
NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
114-
if (h != INVALID_HANDLE_VALUE || GetLastError() != ERROR_PIPE_BUSY)
114+
NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
115+
if (h != INVALID_HANDLE_VALUE || GetLastError() != ERROR_PIPE_BUSY ||
116+
++connection_attempts > 10)
115117
break;
116118
Sleep(100);
117119
} while(1);
118120

119121
if (h == INVALID_HANDLE_VALUE) {
120-
debug("ssh_get_authentication_socket - CreateFileW failed error %d", GetLastError());
122+
debug("ssh_get_authentication_socket - CreateFileW failed error %d",
123+
GetLastError());
121124
return SSH_ERR_AGENT_NOT_PRESENT;
122125
}
123126

124127
/*
125-
* ensure that connected server pid matches published pid. this provides service side
126-
* auth and prevents mitm
128+
* ensure that connected server pid matches published pid.
129+
* this provides service side auth and prevents mitm
127130
*/
128-
if (!GetNamedPipeServerProcessId(h, &pipe_server_pid) || (agent_pid != pipe_server_pid)) {
131+
if (!GetNamedPipeServerProcessId(h, &pipe_server_pid) ||
132+
(agent_pid != pipe_server_pid)) {
129133
debug("agent pid mismatch");
130134
CloseHandle(h);
131135
return SSH_ERR_AGENT_COMMUNICATION;

authfd.h

+10
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,14 @@ int ssh_agent_sign(int sock, struct sshkey *key,
8989
#define SSH_AGENT_RSA_SHA2_256 0x02
9090
#define SSH_AGENT_RSA_SHA2_512 0x04
9191

92+
/*
93+
* Following are used in Windows implementation
94+
* ssh-agent in Windows also serves user authentication
95+
*/
96+
#define SSH_AGENT_AUTHENTICATE 200
97+
#define PUBKEY_AUTH_REQUEST "pubkey"
98+
#define PASSWD_AUTH_REQUEST "password"
99+
#define SSH_AGENT_REG_ROOT L"SOFTWARE\\SSH\\Agent"
100+
#define SSH_AGENT_PIPE_NAME L"\\\\.\\pipe\\ssh-agent"
101+
92102
#endif /* AUTHFD_H */

channels.c

+26-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: channels.c,v 1.356 2016/10/18 17:32:54 dtucker Exp $ */
1+
/* $OpenBSD: channels.c,v 1.357 2017/02/01 02:59:09 dtucker Exp $ */
22
/*
33
* Author: Tatu Ylonen <[email protected]>
44
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
@@ -3067,7 +3067,7 @@ channel_input_port_open(int type, u_int32_t seq, void *ctxt)
30673067
}
30683068
packet_check_eom();
30693069
c = channel_connect_to_port(host, host_port,
3070-
"connected socket", originator_string);
3070+
"connected socket", originator_string, NULL, NULL);
30713071
free(originator_string);
30723072
free(host);
30733073
if (c == NULL) {
@@ -4028,9 +4028,13 @@ channel_connect_ctx_free(struct channel_connect *cctx)
40284028
memset(cctx, 0, sizeof(*cctx));
40294029
}
40304030

4031-
/* Return CONNECTING channel to remote host:port or local socket path */
4031+
/*
4032+
* Return CONNECTING channel to remote host:port or local socket path,
4033+
* passing back the failure reason if appropriate.
4034+
*/
40324035
static Channel *
4033-
connect_to(const char *name, int port, char *ctype, char *rname)
4036+
connect_to_reason(const char *name, int port, char *ctype, char *rname,
4037+
int *reason, const char **errmsg)
40344038
{
40354039
struct addrinfo hints;
40364040
int gaierr;
@@ -4071,7 +4075,12 @@ connect_to(const char *name, int port, char *ctype, char *rname)
40714075
hints.ai_family = IPv4or6;
40724076
hints.ai_socktype = SOCK_STREAM;
40734077
snprintf(strport, sizeof strport, "%d", port);
4074-
if ((gaierr = getaddrinfo(name, strport, &hints, &cctx.aitop)) != 0) {
4078+
if ((gaierr = getaddrinfo(name, strport, &hints, &cctx.aitop))
4079+
!= 0) {
4080+
if (errmsg != NULL)
4081+
*errmsg = ssh_gai_strerror(gaierr);
4082+
if (reason != NULL)
4083+
*reason = SSH2_OPEN_CONNECT_FAILED;
40754084
error("connect_to %.100s: unknown host (%s)", name,
40764085
ssh_gai_strerror(gaierr));
40774086
return NULL;
@@ -4094,6 +4103,13 @@ connect_to(const char *name, int port, char *ctype, char *rname)
40944103
return c;
40954104
}
40964105

4106+
/* Return CONNECTING channel to remote host:port or local socket path */
4107+
static Channel *
4108+
connect_to(const char *name, int port, char *ctype, char *rname)
4109+
{
4110+
return connect_to_reason(name, port, ctype, rname, NULL, NULL);
4111+
}
4112+
40974113
/*
40984114
* returns either the newly connected channel or the downstream channel
40994115
* that needs to deal with this connection.
@@ -4138,7 +4154,8 @@ channel_connect_by_listen_path(const char *path, char *ctype, char *rname)
41384154

41394155
/* Check if connecting to that port is permitted and connect. */
41404156
Channel *
4141-
channel_connect_to_port(const char *host, u_short port, char *ctype, char *rname)
4157+
channel_connect_to_port(const char *host, u_short port, char *ctype,
4158+
char *rname, int *reason, const char **errmsg)
41424159
{
41434160
int i, permit, permit_adm = 1;
41444161

@@ -4163,9 +4180,11 @@ channel_connect_to_port(const char *host, u_short port, char *ctype, char *rname
41634180
if (!permit || !permit_adm) {
41644181
logit("Received request to connect to host %.100s port %d, "
41654182
"but the request was denied.", host, port);
4183+
if (reason != NULL)
4184+
*reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
41664185
return NULL;
41674186
}
4168-
return connect_to(host, port, ctype, rname);
4187+
return connect_to_reason(host, port, ctype, rname, reason, errmsg);
41694188
}
41704189

41714190
/* Check if connecting to that path is permitted and connect. */

channels.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: channels.h,v 1.120 2016/10/18 17:32:54 dtucker Exp $ */
1+
/* $OpenBSD: channels.h,v 1.121 2017/02/01 02:59:09 dtucker Exp $ */
22

33
/*
44
* Author: Tatu Ylonen <[email protected]>
@@ -275,7 +275,8 @@ void channel_update_permitted_opens(int, int);
275275
void channel_clear_permitted_opens(void);
276276
void channel_clear_adm_permitted_opens(void);
277277
void channel_print_adm_permitted_opens(void);
278-
Channel *channel_connect_to_port(const char *, u_short, char *, char *);
278+
Channel *channel_connect_to_port(const char *, u_short, char *, char *, int *,
279+
const char **);
279280
Channel *channel_connect_to_path(const char *, char *, char *);
280281
Channel *channel_connect_stdio_fwd(const char*, u_short, int, int);
281282
Channel *channel_connect_by_listen_address(const char *, u_short,

clientloop.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: clientloop.c,v 1.289 2016/09/30 09:19:13 markus Exp $ */
1+
/* $OpenBSD: clientloop.c,v 1.290 2017/01/29 21:35:23 dtucker Exp $ */
22
/*
33
* Author: Tatu Ylonen <[email protected]>
44
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
@@ -991,7 +991,7 @@ process_cmdline(void)
991991
CHANNEL_CANCEL_PORT_STATIC,
992992
&options.fwd_opts) > 0;
993993
if (!ok) {
994-
logit("Unkown port forwarding.");
994+
logit("Unknown port forwarding.");
995995
goto out;
996996
}
997997
logit("Canceled forwarding.");

0 commit comments

Comments
 (0)