1 /* $OpenBSD: auth1.c,v 1.66 2006/03/25 13:17:01 djm 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".
27 #include "monitor_wrap.h"
31 extern ServerOptions options
;
32 extern Buffer loginmsg
;
34 static int auth1_process_password(Authctxt
*, char *, size_t);
35 static int auth1_process_rsa(Authctxt
*, char *, size_t);
36 static int auth1_process_rhosts_rsa(Authctxt
*, char *, size_t);
37 static int auth1_process_tis_challenge(Authctxt
*, char *, size_t);
38 static int auth1_process_tis_response(Authctxt
*, char *, size_t);
40 static char *client_user
= NULL
; /* Used to fill in remote user for PAM */
46 int (*method
)(Authctxt
*, char *, size_t);
49 const struct AuthMethod1 auth1_methods
[] = {
51 SSH_CMSG_AUTH_PASSWORD
, "password",
52 &options
.password_authentication
, auth1_process_password
55 SSH_CMSG_AUTH_RSA
, "rsa",
56 &options
.rsa_authentication
, auth1_process_rsa
59 SSH_CMSG_AUTH_RHOSTS_RSA
, "rhosts-rsa",
60 &options
.rhosts_rsa_authentication
, auth1_process_rhosts_rsa
63 SSH_CMSG_AUTH_TIS
, "challenge-response",
64 &options
.challenge_response_authentication
,
65 auth1_process_tis_challenge
68 SSH_CMSG_AUTH_TIS_RESPONSE
, "challenge-response",
69 &options
.challenge_response_authentication
,
70 auth1_process_tis_response
72 { -1, NULL
, NULL
, NULL
}
75 static const struct AuthMethod1
76 *lookup_authmethod1(int type
)
80 for (i
= 0; auth1_methods
[i
].name
!= NULL
; i
++)
81 if (auth1_methods
[i
].type
== type
)
82 return (&(auth1_methods
[i
]));
88 get_authname(int type
)
90 const struct AuthMethod1
*a
;
93 if ((a
= lookup_authmethod1(type
)) != NULL
)
95 snprintf(buf
, sizeof(buf
), "bad-auth-msg-%d", type
);
101 auth1_process_password(Authctxt
*authctxt
, char *info
, size_t infolen
)
103 int authenticated
= 0;
108 * Read user password. It is in plain text, but was
109 * transmitted over the encrypted channel so it is
110 * not visible to an outside observer.
112 password
= packet_get_string(&dlen
);
115 /* Try authentication with the password. */
116 authenticated
= PRIVSEP(auth_password(authctxt
, password
));
118 memset(password
, 0, dlen
);
121 return (authenticated
);
126 auth1_process_rsa(Authctxt
*authctxt
, char *info
, size_t infolen
)
128 int authenticated
= 0;
131 /* RSA authentication requested. */
132 if ((n
= BN_new()) == NULL
)
133 fatal("do_authloop: BN_new failed");
134 packet_get_bignum(n
);
136 authenticated
= auth_rsa(authctxt
, n
);
139 return (authenticated
);
144 auth1_process_rhosts_rsa(Authctxt
*authctxt
, char *info
, size_t infolen
)
146 int keybits
, authenticated
= 0;
148 Key
*client_host_key
;
152 * Get client user name. Note that we just have to
153 * trust the client; root on the client machine can
154 * claim to be any user.
156 client_user
= packet_get_string(&ulen
);
158 /* Get the client host key. */
159 client_host_key
= key_new(KEY_RSA1
);
160 bits
= packet_get_int();
161 packet_get_bignum(client_host_key
->rsa
->e
);
162 packet_get_bignum(client_host_key
->rsa
->n
);
164 keybits
= BN_num_bits(client_host_key
->rsa
->n
);
165 if (keybits
< 0 || bits
!= (u_int
)keybits
) {
166 verbose("Warning: keysize mismatch for client_host_key: "
167 "actual %d, announced %d",
168 BN_num_bits(client_host_key
->rsa
->n
), bits
);
172 authenticated
= auth_rhosts_rsa(authctxt
, client_user
,
174 key_free(client_host_key
);
176 snprintf(info
, infolen
, " ruser %.100s", client_user
);
178 return (authenticated
);
183 auth1_process_tis_challenge(Authctxt
*authctxt
, char *info
, size_t infolen
)
187 if ((challenge
= get_challenge(authctxt
)) == NULL
)
190 debug("sending challenge '%s'", challenge
);
191 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE
);
192 packet_put_cstring(challenge
);
202 auth1_process_tis_response(Authctxt
*authctxt
, char *info
, size_t infolen
)
204 int authenticated
= 0;
208 response
= packet_get_string(&dlen
);
210 authenticated
= verify_response(authctxt
, response
);
211 memset(response
, 'r', dlen
);
214 return (authenticated
);
218 * read packets, try to authenticate the user and
219 * return only if authentication is successful
222 do_authloop(Authctxt
*authctxt
)
224 int authenticated
= 0;
226 int prev
= 0, type
= 0;
227 const struct AuthMethod1
*meth
;
229 debug("Attempting authentication for %s%.100s.",
230 authctxt
->valid
? "" : "invalid user ", authctxt
->user
);
232 /* If the user has no password, accept authentication immediately. */
233 if (options
.password_authentication
&&
235 (!options
.kerberos_authentication
|| options
.kerberos_or_local_passwd
) &&
237 PRIVSEP(auth_password(authctxt
, ""))) {
239 if (options
.use_pam
&& (PRIVSEP(do_pam_account())))
242 auth_log(authctxt
, 1, "without authentication", "");
247 /* Indicate that authentication is needed. */
248 packet_start(SSH_SMSG_FAILURE
);
253 /* default to fail */
258 /* Get a packet from the client. */
260 type
= packet_read();
263 * If we started challenge-response authentication but the
264 * next packet is not a response to our challenge, release
265 * the resources allocated by get_challenge() (which would
266 * normally have been released by verify_response() had we
267 * received such a response)
269 if (prev
== SSH_CMSG_AUTH_TIS
&&
270 type
!= SSH_CMSG_AUTH_TIS_RESPONSE
)
271 abandon_challenge_response(authctxt
);
273 if ((meth
= lookup_authmethod1(type
)) == NULL
) {
274 logit("Unknown message during authentication: "
279 if (!*(meth
->enabled
)) {
280 verbose("%s authentication disabled.", meth
->name
);
284 authenticated
= meth
->method(authctxt
, info
, sizeof(info
));
285 if (authenticated
== -1)
286 continue; /* "postponed" */
290 auth_close(authctxt
->as
);
294 if (!authctxt
->valid
&& authenticated
)
295 fatal("INTERNAL ERROR: authenticated invalid user %s",
299 if (authenticated
&& cray_access_denied(authctxt
->user
)) {
301 fatal("Access denied for user %s.",authctxt
->user
);
307 !check_nt_auth(type
== SSH_CMSG_AUTH_PASSWORD
,
309 packet_disconnect("Authentication rejected for uid %d.",
310 authctxt
->pw
== NULL
? -1 : authctxt
->pw
->pw_uid
);
314 /* Special handling for root */
315 if (authenticated
&& authctxt
->pw
->pw_uid
== 0 &&
316 !auth_root_allowed(meth
->name
)) {
318 # ifdef SSH_AUDIT_EVENTS
319 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED
));
325 if (options
.use_pam
&& authenticated
&&
326 !PRIVSEP(do_pam_account())) {
330 error("Access denied for user %s by PAM account "
331 "configuration", authctxt
->user
);
332 len
= buffer_len(&loginmsg
);
333 buffer_append(&loginmsg
, "\0", 1);
334 msg
= buffer_ptr(&loginmsg
);
335 /* strip trailing newlines */
337 while (len
> 0 && msg
[--len
] == '\n')
340 msg
= "Access denied.";
341 packet_disconnect(msg
);
346 /* Log before sending the reply */
347 auth_log(authctxt
, authenticated
, get_authname(type
), info
);
349 if (client_user
!= NULL
) {
357 if (authctxt
->failures
++ > options
.max_authtries
) {
358 #ifdef SSH_AUDIT_EVENTS
359 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES
));
361 packet_disconnect(AUTH_FAIL_MSG
, authctxt
->user
);
364 packet_start(SSH_SMSG_FAILURE
);
371 * Performs authentication of an incoming connection. Session key has already
372 * been exchanged and encryption is enabled.
375 do_authentication(Authctxt
*authctxt
)
378 char *user
, *style
= NULL
;
380 /* Get the name of the user that we wish to log in as. */
381 packet_read_expect(SSH_CMSG_USER
);
383 /* Get the user name. */
384 user
= packet_get_string(&ulen
);
387 if ((style
= strchr(user
, ':')) != NULL
)
390 authctxt
->user
= user
;
391 authctxt
->style
= style
;
393 /* Verify that the user is a valid user. */
394 if ((authctxt
->pw
= PRIVSEP(getpwnamallow(user
))) != NULL
)
397 debug("do_authentication: invalid user %s", user
);
398 authctxt
->pw
= fakepw();
401 setproctitle("%s%s", authctxt
->valid
? user
: "unknown",
402 use_privsep
? " [net]" : "");
406 PRIVSEP(start_pam(authctxt
));
410 * If we are not running as root, the user must have the same uid as
414 if (!use_privsep
&& getuid() != 0 && authctxt
->pw
&&
415 authctxt
->pw
->pw_uid
!= getuid())
416 packet_disconnect("Cannot change user when server not running as root.");
420 * Loop until the user has been authenticated or the connection is
421 * closed, do_authloop() returns only if authentication is successful
423 do_authloop(authctxt
);
425 /* The user has been authenticated and accepted. */
426 packet_start(SSH_SMSG_SUCCESS
);