1 /* $OpenBSD: auth1.c,v 1.70 2006/08/03 03:34:41 deraadt 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>
40 #include "monitor_wrap.h"
44 extern ServerOptions options
;
45 extern Buffer loginmsg
;
47 static int auth1_process_password(Authctxt
*, char *, size_t);
48 static int auth1_process_rsa(Authctxt
*, char *, size_t);
49 static int auth1_process_rhosts_rsa(Authctxt
*, char *, size_t);
50 static int auth1_process_tis_challenge(Authctxt
*, char *, size_t);
51 static int auth1_process_tis_response(Authctxt
*, char *, size_t);
53 static char *client_user
= NULL
; /* Used to fill in remote user for PAM */
59 int (*method
)(Authctxt
*, char *, size_t);
62 const struct AuthMethod1 auth1_methods
[] = {
64 SSH_CMSG_AUTH_PASSWORD
, "password",
65 &options
.password_authentication
, auth1_process_password
68 SSH_CMSG_AUTH_RSA
, "rsa",
69 &options
.rsa_authentication
, auth1_process_rsa
72 SSH_CMSG_AUTH_RHOSTS_RSA
, "rhosts-rsa",
73 &options
.rhosts_rsa_authentication
, auth1_process_rhosts_rsa
76 SSH_CMSG_AUTH_TIS
, "challenge-response",
77 &options
.challenge_response_authentication
,
78 auth1_process_tis_challenge
81 SSH_CMSG_AUTH_TIS_RESPONSE
, "challenge-response",
82 &options
.challenge_response_authentication
,
83 auth1_process_tis_response
85 { -1, NULL
, NULL
, NULL
}
88 static const struct AuthMethod1
89 *lookup_authmethod1(int type
)
93 for (i
= 0; auth1_methods
[i
].name
!= NULL
; i
++)
94 if (auth1_methods
[i
].type
== type
)
95 return (&(auth1_methods
[i
]));
101 get_authname(int type
)
103 const struct AuthMethod1
*a
;
106 if ((a
= lookup_authmethod1(type
)) != NULL
)
108 snprintf(buf
, sizeof(buf
), "bad-auth-msg-%d", type
);
114 auth1_process_password(Authctxt
*authctxt
, char *info
, size_t infolen
)
116 int authenticated
= 0;
121 * Read user password. It is in plain text, but was
122 * transmitted over the encrypted channel so it is
123 * not visible to an outside observer.
125 password
= packet_get_string(&dlen
);
128 /* Try authentication with the password. */
129 authenticated
= PRIVSEP(auth_password(authctxt
, password
));
131 memset(password
, 0, dlen
);
134 return (authenticated
);
139 auth1_process_rsa(Authctxt
*authctxt
, char *info
, size_t infolen
)
141 int authenticated
= 0;
144 /* RSA authentication requested. */
145 if ((n
= BN_new()) == NULL
)
146 fatal("do_authloop: BN_new failed");
147 packet_get_bignum(n
);
149 authenticated
= auth_rsa(authctxt
, n
);
152 return (authenticated
);
157 auth1_process_rhosts_rsa(Authctxt
*authctxt
, char *info
, size_t infolen
)
159 int keybits
, authenticated
= 0;
161 Key
*client_host_key
;
165 * Get client user name. Note that we just have to
166 * trust the client; root on the client machine can
167 * claim to be any user.
169 client_user
= packet_get_string(&ulen
);
171 /* Get the client host key. */
172 client_host_key
= key_new(KEY_RSA1
);
173 bits
= packet_get_int();
174 packet_get_bignum(client_host_key
->rsa
->e
);
175 packet_get_bignum(client_host_key
->rsa
->n
);
177 keybits
= BN_num_bits(client_host_key
->rsa
->n
);
178 if (keybits
< 0 || bits
!= (u_int
)keybits
) {
179 verbose("Warning: keysize mismatch for client_host_key: "
180 "actual %d, announced %d",
181 BN_num_bits(client_host_key
->rsa
->n
), bits
);
185 authenticated
= auth_rhosts_rsa(authctxt
, client_user
,
187 key_free(client_host_key
);
189 snprintf(info
, infolen
, " ruser %.100s", client_user
);
191 return (authenticated
);
196 auth1_process_tis_challenge(Authctxt
*authctxt
, char *info
, size_t infolen
)
200 if ((challenge
= get_challenge(authctxt
)) == NULL
)
203 debug("sending challenge '%s'", challenge
);
204 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE
);
205 packet_put_cstring(challenge
);
215 auth1_process_tis_response(Authctxt
*authctxt
, char *info
, size_t infolen
)
217 int authenticated
= 0;
221 response
= packet_get_string(&dlen
);
223 authenticated
= verify_response(authctxt
, response
);
224 memset(response
, 'r', dlen
);
227 return (authenticated
);
231 * read packets, try to authenticate the user and
232 * return only if authentication is successful
235 do_authloop(Authctxt
*authctxt
)
237 int authenticated
= 0;
239 int prev
= 0, type
= 0;
240 const struct AuthMethod1
*meth
;
242 debug("Attempting authentication for %s%.100s.",
243 authctxt
->valid
? "" : "invalid user ", authctxt
->user
);
245 /* If the user has no password, accept authentication immediately. */
246 if (options
.password_authentication
&&
248 (!options
.kerberos_authentication
|| options
.kerberos_or_local_passwd
) &&
250 PRIVSEP(auth_password(authctxt
, ""))) {
252 if (options
.use_pam
&& (PRIVSEP(do_pam_account())))
255 auth_log(authctxt
, 1, "without authentication", "");
260 /* Indicate that authentication is needed. */
261 packet_start(SSH_SMSG_FAILURE
);
266 /* default to fail */
271 /* Get a packet from the client. */
273 type
= packet_read();
276 * If we started challenge-response authentication but the
277 * next packet is not a response to our challenge, release
278 * the resources allocated by get_challenge() (which would
279 * normally have been released by verify_response() had we
280 * received such a response)
282 if (prev
== SSH_CMSG_AUTH_TIS
&&
283 type
!= SSH_CMSG_AUTH_TIS_RESPONSE
)
284 abandon_challenge_response(authctxt
);
286 if ((meth
= lookup_authmethod1(type
)) == NULL
) {
287 logit("Unknown message during authentication: "
292 if (!*(meth
->enabled
)) {
293 verbose("%s authentication disabled.", meth
->name
);
297 authenticated
= meth
->method(authctxt
, info
, sizeof(info
));
298 if (authenticated
== -1)
299 continue; /* "postponed" */
303 auth_close(authctxt
->as
);
307 if (!authctxt
->valid
&& authenticated
)
308 fatal("INTERNAL ERROR: authenticated invalid user %s",
312 if (authenticated
&& cray_access_denied(authctxt
->user
)) {
314 fatal("Access denied for user %s.",authctxt
->user
);
320 !check_nt_auth(type
== SSH_CMSG_AUTH_PASSWORD
,
322 packet_disconnect("Authentication rejected for uid %d.",
323 authctxt
->pw
== NULL
? -1 : authctxt
->pw
->pw_uid
);
327 /* Special handling for root */
328 if (authenticated
&& authctxt
->pw
->pw_uid
== 0 &&
329 !auth_root_allowed(meth
->name
)) {
331 # ifdef SSH_AUDIT_EVENTS
332 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED
));
338 if (options
.use_pam
&& authenticated
&&
339 !PRIVSEP(do_pam_account())) {
343 error("Access denied for user %s by PAM account "
344 "configuration", authctxt
->user
);
345 len
= buffer_len(&loginmsg
);
346 buffer_append(&loginmsg
, "\0", 1);
347 msg
= buffer_ptr(&loginmsg
);
348 /* strip trailing newlines */
350 while (len
> 0 && msg
[--len
] == '\n')
353 msg
= "Access denied.";
354 packet_disconnect(msg
);
359 /* Log before sending the reply */
360 auth_log(authctxt
, authenticated
, get_authname(type
), info
);
362 if (client_user
!= NULL
) {
370 if (authctxt
->failures
++ > options
.max_authtries
) {
371 #ifdef SSH_AUDIT_EVENTS
372 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES
));
374 packet_disconnect(AUTH_FAIL_MSG
, authctxt
->user
);
377 packet_start(SSH_SMSG_FAILURE
);
384 * Performs authentication of an incoming connection. Session key has already
385 * been exchanged and encryption is enabled.
388 do_authentication(Authctxt
*authctxt
)
391 char *user
, *style
= NULL
;
393 /* Get the name of the user that we wish to log in as. */
394 packet_read_expect(SSH_CMSG_USER
);
396 /* Get the user name. */
397 user
= packet_get_string(&ulen
);
400 if ((style
= strchr(user
, ':')) != NULL
)
403 authctxt
->user
= user
;
404 authctxt
->style
= style
;
406 /* Verify that the user is a valid user. */
407 if ((authctxt
->pw
= PRIVSEP(getpwnamallow(user
))) != NULL
)
410 debug("do_authentication: invalid user %s", user
);
411 authctxt
->pw
= fakepw();
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
);