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".
17 #include <sys/types.h>
25 #include "openbsd-compat/sys-queue.h"
44 #include "monitor_wrap.h"
48 extern ServerOptions options
;
49 extern Buffer loginmsg
;
51 static int auth1_process_password(Authctxt
*);
52 static int auth1_process_rsa(Authctxt
*);
53 static int auth1_process_rhosts_rsa(Authctxt
*);
54 static int auth1_process_tis_challenge(Authctxt
*);
55 static int auth1_process_tis_response(Authctxt
*);
57 static char *client_user
= NULL
; /* Used to fill in remote user for PAM */
63 int (*method
)(Authctxt
*);
66 const struct AuthMethod1 auth1_methods
[] = {
68 SSH_CMSG_AUTH_PASSWORD
, "password",
69 &options
.password_authentication
, auth1_process_password
72 SSH_CMSG_AUTH_RSA
, "rsa",
73 &options
.rsa_authentication
, auth1_process_rsa
76 SSH_CMSG_AUTH_RHOSTS_RSA
, "rhosts-rsa",
77 &options
.rhosts_rsa_authentication
, auth1_process_rhosts_rsa
80 SSH_CMSG_AUTH_TIS
, "challenge-response",
81 &options
.challenge_response_authentication
,
82 auth1_process_tis_challenge
85 SSH_CMSG_AUTH_TIS_RESPONSE
, "challenge-response",
86 &options
.challenge_response_authentication
,
87 auth1_process_tis_response
89 { -1, NULL
, NULL
, NULL
}
92 static const struct AuthMethod1
93 *lookup_authmethod1(int type
)
97 for (i
= 0; auth1_methods
[i
].name
!= NULL
; i
++)
98 if (auth1_methods
[i
].type
== type
)
99 return (&(auth1_methods
[i
]));
105 get_authname(int type
)
107 const struct AuthMethod1
*a
;
110 if ((a
= lookup_authmethod1(type
)) != NULL
)
112 snprintf(buf
, sizeof(buf
), "bad-auth-msg-%d", type
);
118 auth1_process_password(Authctxt
*authctxt
)
120 int authenticated
= 0;
125 * Read user password. It is in plain text, but was
126 * transmitted over the encrypted channel so it is
127 * not visible to an outside observer.
129 password
= packet_get_string(&dlen
);
132 /* Try authentication with the password. */
133 authenticated
= PRIVSEP(auth_password(authctxt
, password
));
135 explicit_bzero(password
, dlen
);
138 return (authenticated
);
143 auth1_process_rsa(Authctxt
*authctxt
)
145 int authenticated
= 0;
148 /* RSA authentication requested. */
149 if ((n
= BN_new()) == NULL
)
150 fatal("do_authloop: BN_new failed");
151 packet_get_bignum(n
);
153 authenticated
= auth_rsa(authctxt
, n
);
156 return (authenticated
);
161 auth1_process_rhosts_rsa(Authctxt
*authctxt
)
163 int keybits
, authenticated
= 0;
165 Key
*client_host_key
;
169 * Get client user name. Note that we just have to
170 * trust the client; root on the client machine can
171 * claim to be any user.
173 client_user
= packet_get_cstring(&ulen
);
175 /* Get the client host key. */
176 client_host_key
= key_new(KEY_RSA1
);
177 bits
= packet_get_int();
178 packet_get_bignum(client_host_key
->rsa
->e
);
179 packet_get_bignum(client_host_key
->rsa
->n
);
181 keybits
= BN_num_bits(client_host_key
->rsa
->n
);
182 if (keybits
< 0 || bits
!= (u_int
)keybits
) {
183 verbose("Warning: keysize mismatch for client_host_key: "
184 "actual %d, announced %d",
185 BN_num_bits(client_host_key
->rsa
->n
), bits
);
189 authenticated
= auth_rhosts_rsa(authctxt
, client_user
,
191 key_free(client_host_key
);
193 auth_info(authctxt
, "ruser %.100s", client_user
);
195 return (authenticated
);
200 auth1_process_tis_challenge(Authctxt
*authctxt
)
204 if ((challenge
= get_challenge(authctxt
)) == NULL
)
207 debug("sending challenge '%s'", challenge
);
208 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE
);
209 packet_put_cstring(challenge
);
219 auth1_process_tis_response(Authctxt
*authctxt
)
221 int authenticated
= 0;
225 response
= packet_get_string(&dlen
);
227 authenticated
= verify_response(authctxt
, response
);
228 explicit_bzero(response
, dlen
);
231 return (authenticated
);
235 * read packets, try to authenticate the user and
236 * return only if authentication is successful
239 do_authloop(Authctxt
*authctxt
)
241 int authenticated
= 0;
242 int prev
= 0, type
= 0;
243 const struct AuthMethod1
*meth
;
245 debug("Attempting authentication for %s%.100s.",
246 authctxt
->valid
? "" : "invalid user ", authctxt
->user
);
248 /* If the user has no password, accept authentication immediately. */
249 if (options
.permit_empty_passwd
&& options
.password_authentication
&&
251 (!options
.kerberos_authentication
|| options
.kerberos_or_local_passwd
) &&
253 PRIVSEP(auth_password(authctxt
, ""))) {
255 if (options
.use_pam
&& (PRIVSEP(do_pam_account())))
258 auth_log(authctxt
, 1, 0, "without authentication",
264 /* Indicate that authentication is needed. */
265 packet_start(SSH_SMSG_FAILURE
);
270 /* default to fail */
274 /* Get a packet from the client. */
276 type
= packet_read();
279 * If we started challenge-response authentication but the
280 * next packet is not a response to our challenge, release
281 * the resources allocated by get_challenge() (which would
282 * normally have been released by verify_response() had we
283 * received such a response)
285 if (prev
== SSH_CMSG_AUTH_TIS
&&
286 type
!= SSH_CMSG_AUTH_TIS_RESPONSE
)
287 abandon_challenge_response(authctxt
);
289 if (authctxt
->failures
>= options
.max_authtries
)
291 if ((meth
= lookup_authmethod1(type
)) == NULL
) {
292 logit("Unknown message during authentication: "
297 if (!*(meth
->enabled
)) {
298 verbose("%s authentication disabled.", meth
->name
);
302 authenticated
= meth
->method(authctxt
);
303 if (authenticated
== -1)
304 continue; /* "postponed" */
308 auth_close(authctxt
->as
);
312 if (!authctxt
->valid
&& authenticated
)
313 fatal("INTERNAL ERROR: authenticated invalid user %s",
317 if (authenticated
&& cray_access_denied(authctxt
->user
)) {
319 fatal("Access denied for user %s.",authctxt
->user
);
324 /* Special handling for root */
325 if (authenticated
&& authctxt
->pw
->pw_uid
== 0 &&
326 !auth_root_allowed(meth
->name
)) {
328 # ifdef SSH_AUDIT_EVENTS
329 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED
));
335 if (options
.use_pam
&& authenticated
&&
336 !PRIVSEP(do_pam_account())) {
340 error("Access denied for user %s by PAM account "
341 "configuration", authctxt
->user
);
342 len
= buffer_len(&loginmsg
);
343 buffer_append(&loginmsg
, "\0", 1);
344 msg
= buffer_ptr(&loginmsg
);
345 /* strip trailing newlines */
347 while (len
> 0 && msg
[--len
] == '\n')
350 msg
= "Access denied.";
351 packet_disconnect("%s", msg
);
356 /* Log before sending the reply */
357 auth_log(authctxt
, authenticated
, 0, get_authname(type
), NULL
);
365 if (++authctxt
->failures
>= options
.max_authtries
) {
366 #ifdef SSH_AUDIT_EVENTS
367 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES
));
369 auth_maxtries_exceeded(authctxt
);
372 packet_start(SSH_SMSG_FAILURE
);
379 * Performs authentication of an incoming connection. Session key has already
380 * been exchanged and encryption is enabled.
383 do_authentication(Authctxt
*authctxt
)
386 char *user
, *style
= NULL
;
388 /* Get the name of the user that we wish to log in as. */
389 packet_read_expect(SSH_CMSG_USER
);
391 /* Get the user name. */
392 user
= packet_get_cstring(&ulen
);
395 if ((style
= strchr(user
, ':')) != NULL
)
398 authctxt
->user
= user
;
399 authctxt
->style
= style
;
401 /* Verify that the user is a valid user. */
402 if ((authctxt
->pw
= PRIVSEP(getpwnamallow(user
))) != NULL
)
405 debug("do_authentication: invalid user %s", user
);
406 authctxt
->pw
= fakepw();
409 /* Configuration may have changed as a result of Match */
410 if (options
.num_auth_methods
!= 0)
411 fatal("AuthenticationMethods is not supported with SSH "
414 setproctitle("%s%s", authctxt
->valid
? user
: "unknown",
415 use_privsep
? " [net]" : "");
419 PRIVSEP(start_pam(authctxt
));
423 * If we are not running as root, the user must have the same uid as
427 if (!use_privsep
&& getuid() != 0 && authctxt
->pw
&&
428 authctxt
->pw
->pw_uid
!= getuid())
429 packet_disconnect("Cannot change user when server not running as root.");
433 * Loop until the user has been authenticated or the connection is
434 * closed, do_authloop() returns only if authentication is successful
436 do_authloop(authctxt
);
438 /* The user has been authenticated and accepted. */
439 packet_start(SSH_SMSG_SUCCESS
);
444 #endif /* WITH_SSH1 */