1 /* $OpenBSD: auth1.c,v 1.82 2014/07/15 15:54:14 millert Exp $ */
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
15 #include <sys/types.h>
23 #include "openbsd-compat/sys-queue.h"
42 #include "monitor_wrap.h"
46 extern ServerOptions options
;
47 extern Buffer loginmsg
;
49 static int auth1_process_password(Authctxt
*);
50 static int auth1_process_rsa(Authctxt
*);
51 static int auth1_process_rhosts_rsa(Authctxt
*);
52 static int auth1_process_tis_challenge(Authctxt
*);
53 static int auth1_process_tis_response(Authctxt
*);
55 static char *client_user
= NULL
; /* Used to fill in remote user for PAM */
61 int (*method
)(Authctxt
*);
64 const struct AuthMethod1 auth1_methods
[] = {
66 SSH_CMSG_AUTH_PASSWORD
, "password",
67 &options
.password_authentication
, auth1_process_password
70 SSH_CMSG_AUTH_RSA
, "rsa",
71 &options
.rsa_authentication
, auth1_process_rsa
74 SSH_CMSG_AUTH_RHOSTS_RSA
, "rhosts-rsa",
75 &options
.rhosts_rsa_authentication
, auth1_process_rhosts_rsa
78 SSH_CMSG_AUTH_TIS
, "challenge-response",
79 &options
.challenge_response_authentication
,
80 auth1_process_tis_challenge
83 SSH_CMSG_AUTH_TIS_RESPONSE
, "challenge-response",
84 &options
.challenge_response_authentication
,
85 auth1_process_tis_response
87 { -1, NULL
, NULL
, NULL
}
90 static const struct AuthMethod1
91 *lookup_authmethod1(int type
)
95 for (i
= 0; auth1_methods
[i
].name
!= NULL
; i
++)
96 if (auth1_methods
[i
].type
== type
)
97 return (&(auth1_methods
[i
]));
103 get_authname(int type
)
105 const struct AuthMethod1
*a
;
108 if ((a
= lookup_authmethod1(type
)) != NULL
)
110 snprintf(buf
, sizeof(buf
), "bad-auth-msg-%d", type
);
116 auth1_process_password(Authctxt
*authctxt
)
118 int authenticated
= 0;
123 * Read user password. It is in plain text, but was
124 * transmitted over the encrypted channel so it is
125 * not visible to an outside observer.
127 password
= packet_get_string(&dlen
);
130 /* Try authentication with the password. */
131 authenticated
= PRIVSEP(auth_password(authctxt
, password
));
133 explicit_bzero(password
, dlen
);
136 return (authenticated
);
141 auth1_process_rsa(Authctxt
*authctxt
)
143 int authenticated
= 0;
146 /* RSA authentication requested. */
147 if ((n
= BN_new()) == NULL
)
148 fatal("do_authloop: BN_new failed");
149 packet_get_bignum(n
);
151 authenticated
= auth_rsa(authctxt
, n
);
154 return (authenticated
);
159 auth1_process_rhosts_rsa(Authctxt
*authctxt
)
161 int keybits
, authenticated
= 0;
163 Key
*client_host_key
;
167 * Get client user name. Note that we just have to
168 * trust the client; root on the client machine can
169 * claim to be any user.
171 client_user
= packet_get_cstring(&ulen
);
173 /* Get the client host key. */
174 client_host_key
= key_new(KEY_RSA1
);
175 bits
= packet_get_int();
176 packet_get_bignum(client_host_key
->rsa
->e
);
177 packet_get_bignum(client_host_key
->rsa
->n
);
179 keybits
= BN_num_bits(client_host_key
->rsa
->n
);
180 if (keybits
< 0 || bits
!= (u_int
)keybits
) {
181 verbose("Warning: keysize mismatch for client_host_key: "
182 "actual %d, announced %d",
183 BN_num_bits(client_host_key
->rsa
->n
), bits
);
187 authenticated
= auth_rhosts_rsa(authctxt
, client_user
,
189 key_free(client_host_key
);
191 auth_info(authctxt
, "ruser %.100s", client_user
);
193 return (authenticated
);
198 auth1_process_tis_challenge(Authctxt
*authctxt
)
202 if ((challenge
= get_challenge(authctxt
)) == NULL
)
205 debug("sending challenge '%s'", challenge
);
206 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE
);
207 packet_put_cstring(challenge
);
217 auth1_process_tis_response(Authctxt
*authctxt
)
219 int authenticated
= 0;
223 response
= packet_get_string(&dlen
);
225 authenticated
= verify_response(authctxt
, response
);
226 explicit_bzero(response
, dlen
);
229 return (authenticated
);
233 * read packets, try to authenticate the user and
234 * return only if authentication is successful
237 do_authloop(Authctxt
*authctxt
)
239 int authenticated
= 0;
240 int prev
= 0, type
= 0;
241 const struct AuthMethod1
*meth
;
243 debug("Attempting authentication for %s%.100s.",
244 authctxt
->valid
? "" : "invalid user ", authctxt
->user
);
246 /* If the user has no password, accept authentication immediately. */
247 if (options
.permit_empty_passwd
&& options
.password_authentication
&&
249 (!options
.kerberos_authentication
|| options
.kerberos_or_local_passwd
) &&
251 PRIVSEP(auth_password(authctxt
, ""))) {
253 if (options
.use_pam
&& (PRIVSEP(do_pam_account())))
256 auth_log(authctxt
, 1, 0, "without authentication",
262 /* Indicate that authentication is needed. */
263 packet_start(SSH_SMSG_FAILURE
);
268 /* default to fail */
272 /* Get a packet from the client. */
274 type
= packet_read();
277 * If we started challenge-response authentication but the
278 * next packet is not a response to our challenge, release
279 * the resources allocated by get_challenge() (which would
280 * normally have been released by verify_response() had we
281 * received such a response)
283 if (prev
== SSH_CMSG_AUTH_TIS
&&
284 type
!= SSH_CMSG_AUTH_TIS_RESPONSE
)
285 abandon_challenge_response(authctxt
);
287 if (authctxt
->failures
>= options
.max_authtries
)
289 if ((meth
= lookup_authmethod1(type
)) == NULL
) {
290 logit("Unknown message during authentication: "
295 if (!*(meth
->enabled
)) {
296 verbose("%s authentication disabled.", meth
->name
);
300 authenticated
= meth
->method(authctxt
);
301 if (authenticated
== -1)
302 continue; /* "postponed" */
306 auth_close(authctxt
->as
);
310 if (!authctxt
->valid
&& authenticated
)
311 fatal("INTERNAL ERROR: authenticated invalid user %s",
315 if (authenticated
&& cray_access_denied(authctxt
->user
)) {
317 fatal("Access denied for user %s.",authctxt
->user
);
322 /* Special handling for root */
323 if (authenticated
&& authctxt
->pw
->pw_uid
== 0 &&
324 !auth_root_allowed(meth
->name
)) {
326 # ifdef SSH_AUDIT_EVENTS
327 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED
));
333 if (options
.use_pam
&& authenticated
&&
334 !PRIVSEP(do_pam_account())) {
338 error("Access denied for user %s by PAM account "
339 "configuration", authctxt
->user
);
340 len
= buffer_len(&loginmsg
);
341 buffer_append(&loginmsg
, "\0", 1);
342 msg
= buffer_ptr(&loginmsg
);
343 /* strip trailing newlines */
345 while (len
> 0 && msg
[--len
] == '\n')
348 msg
= "Access denied.";
349 packet_disconnect("%s", msg
);
354 /* Log before sending the reply */
355 auth_log(authctxt
, authenticated
, 0, get_authname(type
), NULL
);
363 if (++authctxt
->failures
>= options
.max_authtries
) {
364 #ifdef SSH_AUDIT_EVENTS
365 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES
));
367 auth_maxtries_exceeded(authctxt
);
370 packet_start(SSH_SMSG_FAILURE
);
377 * Performs authentication of an incoming connection. Session key has already
378 * been exchanged and encryption is enabled.
381 do_authentication(Authctxt
*authctxt
)
384 char *user
, *style
= NULL
;
386 /* Get the name of the user that we wish to log in as. */
387 packet_read_expect(SSH_CMSG_USER
);
389 /* Get the user name. */
390 user
= packet_get_cstring(&ulen
);
393 if ((style
= strchr(user
, ':')) != NULL
)
396 authctxt
->user
= user
;
397 authctxt
->style
= style
;
399 /* Verify that the user is a valid user. */
400 if ((authctxt
->pw
= PRIVSEP(getpwnamallow(user
))) != NULL
)
403 debug("do_authentication: invalid user %s", user
);
404 authctxt
->pw
= fakepw();
407 /* Configuration may have changed as a result of Match */
408 if (options
.num_auth_methods
!= 0)
409 fatal("AuthenticationMethods is not supported with SSH "
412 setproctitle("%s%s", authctxt
->valid
? user
: "unknown",
413 use_privsep
? " [net]" : "");
417 PRIVSEP(start_pam(authctxt
));
421 * If we are not running as root, the user must have the same uid as
425 if (!use_privsep
&& getuid() != 0 && authctxt
->pw
&&
426 authctxt
->pw
->pw_uid
!= getuid())
427 packet_disconnect("Cannot change user when server not running as root.");
431 * Loop until the user has been authenticated or the connection is
432 * closed, do_authloop() returns only if authentication is successful
434 do_authloop(authctxt
);
436 /* The user has been authenticated and accepted. */
437 packet_start(SSH_SMSG_SUCCESS
);