2 /* $OpenBSD: auth1.c,v 1.73 2008/07/04 23:30:16 djm Exp $ */
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
15 __RCSID("$NetBSD: auth1.c,v 1.33 2009/02/16 20:53:54 christos Exp $");
16 #include <sys/types.h>
17 #include <sys/queue.h>
41 #include "monitor_wrap.h"
45 extern ServerOptions options
;
46 extern Buffer loginmsg
;
48 static int auth1_process_password(Authctxt
*, char *, size_t);
49 static int auth1_process_rsa(Authctxt
*, char *, size_t);
50 static int auth1_process_rhosts_rsa(Authctxt
*, char *, size_t);
51 static int auth1_process_tis_challenge(Authctxt
*, char *, size_t);
52 static int auth1_process_tis_response(Authctxt
*, char *, size_t);
53 #if defined(KRB4) || defined(KRB5)
54 static int auth1_process_kerberos(Authctxt
*, char *, size_t);
61 int (*method
)(Authctxt
*, char *, size_t);
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 #if defined(KRB4) || defined(KRB5)
89 SSH_CMSG_AUTH_KERBEROS
, "kerberos",
90 &options
.kerberos_authentication
,
91 auth1_process_kerberos
93 #endif /* KRB4 || KRB5 */
94 { -1, NULL
, NULL
, NULL
}
97 static const struct AuthMethod1
98 *lookup_authmethod1(int type
)
102 for (i
= 0; auth1_methods
[i
].name
!= NULL
; i
++)
103 if (auth1_methods
[i
].type
== type
)
104 return (&(auth1_methods
[i
]));
110 get_authname(int type
)
112 const struct AuthMethod1
*a
;
115 if ((a
= lookup_authmethod1(type
)) != NULL
)
117 snprintf(buf
, sizeof(buf
), "bad-auth-msg-%d", type
);
123 auth1_process_password(Authctxt
*authctxt
, char *info
, size_t infolen
)
125 int authenticated
= 0;
130 * Read user password. It is in plain text, but was
131 * transmitted over the encrypted channel so it is
132 * not visible to an outside observer.
134 password
= packet_get_string(&dlen
);
137 /* Try authentication with the password. */
138 authenticated
= PRIVSEP(auth_password(authctxt
, password
));
140 memset(password
, 0, dlen
);
143 return (authenticated
);
146 #if defined(KRB4) || defined(KRB5)
148 auth1_process_kerberos(Authctxt
*authctxt
, char *info
, size_t infolen
)
150 int authenticated
= 0;
153 char *kdata
= packet_get_string(&dlen
);
156 if (kdata
[0] == 4) { /* KRB_PROT_VERSION */
160 if (tkt
.length
< MAX_KTXT_LEN
)
161 memcpy(tkt
.dat
, kdata
, tkt
.length
);
163 if (PRIVSEP(auth_krb4(authctxt
, &tkt
, &client_user
, &reply
))) {
165 snprintf(info
, sizeof(info
), " tktuser %.100s",
168 packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE
);
169 packet_put_string((char *)
170 reply
.dat
, reply
.length
);
179 krb5_data tkt
, reply
;
183 if (PRIVSEP(auth_krb5(authctxt
, &tkt
, &client_user
, &reply
))) {
185 snprintf(info
, sizeof(info
), " tktuser %.100s",
188 /* Send response to client */
189 packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE
);
190 packet_put_string((char *)reply
.data
, reply
.length
);
201 return authenticated
;
203 #endif /* KRB4 || KRB5 */
207 auth1_process_rhosts_rsa(Authctxt
*authctxt
, char *info
, size_t infolen
)
209 int keybits
, authenticated
= 0;
212 Key
*client_host_key
;
216 * Get client user name. Note that we just have to
217 * trust the client; root on the client machine can
218 * claim to be any user.
220 client_user
= packet_get_string(&ulen
);
222 /* Get the client host key. */
223 client_host_key
= key_new(KEY_RSA1
);
224 bits
= packet_get_int();
225 packet_get_bignum(client_host_key
->rsa
->e
);
226 packet_get_bignum(client_host_key
->rsa
->n
);
228 keybits
= BN_num_bits(client_host_key
->rsa
->n
);
229 if (keybits
< 0 || bits
!= (u_int
)keybits
) {
230 verbose("Warning: keysize mismatch for client_host_key: "
231 "actual %d, announced %d",
232 BN_num_bits(client_host_key
->rsa
->n
), bits
);
236 authenticated
= auth_rhosts_rsa(authctxt
, client_user
,
238 key_free(client_host_key
);
240 snprintf(info
, infolen
, " ruser %.100s", client_user
);
243 return (authenticated
);
248 auth1_process_rsa(Authctxt
*authctxt
, char *info
, size_t infolen
)
250 int authenticated
= 0;
253 /* RSA authentication requested. */
254 if ((n
= BN_new()) == NULL
)
255 fatal("do_authloop: BN_new failed");
256 packet_get_bignum(n
);
258 authenticated
= auth_rsa(authctxt
, n
);
261 return (authenticated
);
266 auth1_process_tis_challenge(Authctxt
*authctxt
, char *info
, size_t infolen
)
270 if ((challenge
= get_challenge(authctxt
)) == NULL
)
273 debug("sending challenge '%s'", challenge
);
274 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE
);
275 packet_put_cstring(challenge
);
285 auth1_process_tis_response(Authctxt
*authctxt
, char *info
, size_t infolen
)
287 int authenticated
= 0;
291 response
= packet_get_string(&dlen
);
293 authenticated
= verify_response(authctxt
, response
);
294 memset(response
, 'r', dlen
);
297 return (authenticated
);
301 * read packets, try to authenticate the user and
302 * return only if authentication is successful
305 do_authloop(Authctxt
*authctxt
)
307 int authenticated
= 0;
310 const struct AuthMethod1
*meth
;
312 debug("Attempting authentication for %s%.100s.",
313 authctxt
->valid
? "" : "invalid user ", authctxt
->user
);
315 /* If the user has no password, accept authentication immediately. */
316 if (options
.password_authentication
&&
317 #if defined(KRB4) || defined(KRB5)
318 (!options
.kerberos_authentication
|| options
.kerberos_or_local_passwd
) &&
320 PRIVSEP(auth_password(authctxt
, ""))) {
322 if (options
.use_pam
&& PRIVSEP(do_pam_account()))
325 auth_log(authctxt
, 1, "without authentication", "");
331 /* Indicate that authentication is needed. */
332 packet_start(SSH_SMSG_FAILURE
);
337 /* default to fail */
342 /* Get a packet from the client. */
343 type
= packet_read();
344 if (authctxt
->failures
>= options
.max_authtries
)
346 if ((meth
= lookup_authmethod1(type
)) == NULL
) {
347 logit("Unknown message during authentication: "
352 if (!*(meth
->enabled
)) {
353 verbose("%s authentication disabled.", meth
->name
);
357 authenticated
= meth
->method(authctxt
, info
, sizeof(info
));
358 if (authenticated
== -1)
359 continue; /* "postponed" */
363 auth_close(authctxt
->as
);
367 if (!authctxt
->valid
&& authenticated
)
368 fatal("INTERNAL ERROR: authenticated invalid user %s",
371 /* Special handling for root */
372 if (authenticated
&& authctxt
->pw
->pw_uid
== 0 &&
373 !auth_root_allowed(meth
->name
))
377 if (options
.use_pam
&& authenticated
&&
378 !PRIVSEP(do_pam_account())) {
382 error("Access denied for user %s by PAM account "
383 "configuration", authctxt
->user
);
384 len
= buffer_len(&loginmsg
);
385 buffer_append(&loginmsg
, "\0", 1);
386 msg
= buffer_ptr(&loginmsg
);
387 /* strip trailing newlines */
389 while (len
> 0 && msg
[--len
] == '\n')
392 msg
= "Access denied.";
393 packet_disconnect(msg
);
398 /* Log before sending the reply */
399 auth_log(authctxt
, authenticated
, get_authname(type
), info
);
404 if (++authctxt
->failures
>= options
.max_authtries
)
405 packet_disconnect(AUTH_FAIL_MSG
, authctxt
->user
);
407 packet_start(SSH_SMSG_FAILURE
);
414 * Performs authentication of an incoming connection. Session key has already
415 * been exchanged and encryption is enabled.
418 do_authentication(Authctxt
*authctxt
)
421 char *user
, *style
= NULL
;
423 /* Get the name of the user that we wish to log in as. */
424 packet_read_expect(SSH_CMSG_USER
);
426 /* Get the user name. */
427 user
= packet_get_string(&ulen
);
430 if ((style
= strchr(user
, ':')) != NULL
)
434 /* XXX - SSH.com Kerberos v5 braindeath. */
435 if ((datafellows
& SSH_BUG_K5USER
) &&
436 options
.kerberos_authentication
) {
438 if ((p
= strchr(user
, '@')) != NULL
)
443 authctxt
->user
= user
;
444 authctxt
->style
= style
;
446 /* Verify that the user is a valid user. */
447 if ((authctxt
->pw
= PRIVSEP(getpwnamallow(user
))) != NULL
)
450 debug("do_authentication: invalid user %s", user
);
451 authctxt
->pw
= fakepw();
454 setproctitle("%s%s", authctxt
->valid
? user
: "unknown",
455 use_privsep
? " [net]" : "");
459 PRIVSEP(start_pam(authctxt
));
463 * If we are not running as root, the user must have the same uid as
466 if (!use_privsep
&& getuid() != 0 && authctxt
->pw
&&
467 authctxt
->pw
->pw_uid
!= getuid())
468 packet_disconnect("Cannot change user when server not running as root.");
471 * Loop until the user has been authenticated or the connection is
472 * closed, do_authloop() returns only if authentication is successful
474 do_authloop(authctxt
);
476 /* The user has been authenticated and accepted. */
477 packet_start(SSH_SMSG_SUCCESS
);