1 /* $OpenBSD: auth1.c,v 1.69 2006/08/01 23:22:47 stevesk 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>
33 #include "monitor_wrap.h"
37 extern ServerOptions options
;
38 extern Buffer loginmsg
;
40 static int auth1_process_password(Authctxt
*, char *, size_t);
41 static int auth1_process_rsa(Authctxt
*, char *, size_t);
42 static int auth1_process_rhosts_rsa(Authctxt
*, char *, size_t);
43 static int auth1_process_tis_challenge(Authctxt
*, char *, size_t);
44 static int auth1_process_tis_response(Authctxt
*, char *, size_t);
46 static char *client_user
= NULL
; /* Used to fill in remote user for PAM */
52 int (*method
)(Authctxt
*, char *, size_t);
55 const struct AuthMethod1 auth1_methods
[] = {
57 SSH_CMSG_AUTH_PASSWORD
, "password",
58 &options
.password_authentication
, auth1_process_password
61 SSH_CMSG_AUTH_RSA
, "rsa",
62 &options
.rsa_authentication
, auth1_process_rsa
65 SSH_CMSG_AUTH_RHOSTS_RSA
, "rhosts-rsa",
66 &options
.rhosts_rsa_authentication
, auth1_process_rhosts_rsa
69 SSH_CMSG_AUTH_TIS
, "challenge-response",
70 &options
.challenge_response_authentication
,
71 auth1_process_tis_challenge
74 SSH_CMSG_AUTH_TIS_RESPONSE
, "challenge-response",
75 &options
.challenge_response_authentication
,
76 auth1_process_tis_response
78 { -1, NULL
, NULL
, NULL
}
81 static const struct AuthMethod1
82 *lookup_authmethod1(int type
)
86 for (i
= 0; auth1_methods
[i
].name
!= NULL
; i
++)
87 if (auth1_methods
[i
].type
== type
)
88 return (&(auth1_methods
[i
]));
94 get_authname(int type
)
96 const struct AuthMethod1
*a
;
99 if ((a
= lookup_authmethod1(type
)) != NULL
)
101 snprintf(buf
, sizeof(buf
), "bad-auth-msg-%d", type
);
107 auth1_process_password(Authctxt
*authctxt
, char *info
, size_t infolen
)
109 int authenticated
= 0;
114 * Read user password. It is in plain text, but was
115 * transmitted over the encrypted channel so it is
116 * not visible to an outside observer.
118 password
= packet_get_string(&dlen
);
121 /* Try authentication with the password. */
122 authenticated
= PRIVSEP(auth_password(authctxt
, password
));
124 memset(password
, 0, dlen
);
127 return (authenticated
);
132 auth1_process_rsa(Authctxt
*authctxt
, char *info
, size_t infolen
)
134 int authenticated
= 0;
137 /* RSA authentication requested. */
138 if ((n
= BN_new()) == NULL
)
139 fatal("do_authloop: BN_new failed");
140 packet_get_bignum(n
);
142 authenticated
= auth_rsa(authctxt
, n
);
145 return (authenticated
);
150 auth1_process_rhosts_rsa(Authctxt
*authctxt
, char *info
, size_t infolen
)
152 int keybits
, authenticated
= 0;
154 Key
*client_host_key
;
158 * Get client user name. Note that we just have to
159 * trust the client; root on the client machine can
160 * claim to be any user.
162 client_user
= packet_get_string(&ulen
);
164 /* Get the client host key. */
165 client_host_key
= key_new(KEY_RSA1
);
166 bits
= packet_get_int();
167 packet_get_bignum(client_host_key
->rsa
->e
);
168 packet_get_bignum(client_host_key
->rsa
->n
);
170 keybits
= BN_num_bits(client_host_key
->rsa
->n
);
171 if (keybits
< 0 || bits
!= (u_int
)keybits
) {
172 verbose("Warning: keysize mismatch for client_host_key: "
173 "actual %d, announced %d",
174 BN_num_bits(client_host_key
->rsa
->n
), bits
);
178 authenticated
= auth_rhosts_rsa(authctxt
, client_user
,
180 key_free(client_host_key
);
182 snprintf(info
, infolen
, " ruser %.100s", client_user
);
184 return (authenticated
);
189 auth1_process_tis_challenge(Authctxt
*authctxt
, char *info
, size_t infolen
)
193 if ((challenge
= get_challenge(authctxt
)) == NULL
)
196 debug("sending challenge '%s'", challenge
);
197 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE
);
198 packet_put_cstring(challenge
);
208 auth1_process_tis_response(Authctxt
*authctxt
, char *info
, size_t infolen
)
210 int authenticated
= 0;
214 response
= packet_get_string(&dlen
);
216 authenticated
= verify_response(authctxt
, response
);
217 memset(response
, 'r', dlen
);
220 return (authenticated
);
224 * read packets, try to authenticate the user and
225 * return only if authentication is successful
228 do_authloop(Authctxt
*authctxt
)
230 int authenticated
= 0;
232 int prev
= 0, type
= 0;
233 const struct AuthMethod1
*meth
;
235 debug("Attempting authentication for %s%.100s.",
236 authctxt
->valid
? "" : "invalid user ", authctxt
->user
);
238 /* If the user has no password, accept authentication immediately. */
239 if (options
.password_authentication
&&
241 (!options
.kerberos_authentication
|| options
.kerberos_or_local_passwd
) &&
243 PRIVSEP(auth_password(authctxt
, ""))) {
245 if (options
.use_pam
&& (PRIVSEP(do_pam_account())))
248 auth_log(authctxt
, 1, "without authentication", "");
253 /* Indicate that authentication is needed. */
254 packet_start(SSH_SMSG_FAILURE
);
259 /* default to fail */
264 /* Get a packet from the client. */
266 type
= packet_read();
269 * If we started challenge-response authentication but the
270 * next packet is not a response to our challenge, release
271 * the resources allocated by get_challenge() (which would
272 * normally have been released by verify_response() had we
273 * received such a response)
275 if (prev
== SSH_CMSG_AUTH_TIS
&&
276 type
!= SSH_CMSG_AUTH_TIS_RESPONSE
)
277 abandon_challenge_response(authctxt
);
279 if ((meth
= lookup_authmethod1(type
)) == NULL
) {
280 logit("Unknown message during authentication: "
285 if (!*(meth
->enabled
)) {
286 verbose("%s authentication disabled.", meth
->name
);
290 authenticated
= meth
->method(authctxt
, info
, sizeof(info
));
291 if (authenticated
== -1)
292 continue; /* "postponed" */
296 auth_close(authctxt
->as
);
300 if (!authctxt
->valid
&& authenticated
)
301 fatal("INTERNAL ERROR: authenticated invalid user %s",
305 if (authenticated
&& cray_access_denied(authctxt
->user
)) {
307 fatal("Access denied for user %s.",authctxt
->user
);
313 !check_nt_auth(type
== SSH_CMSG_AUTH_PASSWORD
,
315 packet_disconnect("Authentication rejected for uid %d.",
316 authctxt
->pw
== NULL
? -1 : authctxt
->pw
->pw_uid
);
320 /* Special handling for root */
321 if (authenticated
&& authctxt
->pw
->pw_uid
== 0 &&
322 !auth_root_allowed(meth
->name
)) {
324 # ifdef SSH_AUDIT_EVENTS
325 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED
));
331 if (options
.use_pam
&& authenticated
&&
332 !PRIVSEP(do_pam_account())) {
336 error("Access denied for user %s by PAM account "
337 "configuration", authctxt
->user
);
338 len
= buffer_len(&loginmsg
);
339 buffer_append(&loginmsg
, "\0", 1);
340 msg
= buffer_ptr(&loginmsg
);
341 /* strip trailing newlines */
343 while (len
> 0 && msg
[--len
] == '\n')
346 msg
= "Access denied.";
347 packet_disconnect(msg
);
352 /* Log before sending the reply */
353 auth_log(authctxt
, authenticated
, get_authname(type
), info
);
355 if (client_user
!= NULL
) {
363 if (authctxt
->failures
++ > options
.max_authtries
) {
364 #ifdef SSH_AUDIT_EVENTS
365 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES
));
367 packet_disconnect(AUTH_FAIL_MSG
, authctxt
->user
);
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_string(&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 setproctitle("%s%s", authctxt
->valid
? user
: "unknown",
408 use_privsep
? " [net]" : "");
412 PRIVSEP(start_pam(authctxt
));
416 * If we are not running as root, the user must have the same uid as
420 if (!use_privsep
&& getuid() != 0 && authctxt
->pw
&&
421 authctxt
->pw
->pw_uid
!= getuid())
422 packet_disconnect("Cannot change user when server not running as root.");
426 * Loop until the user has been authenticated or the connection is
427 * closed, do_authloop() returns only if authentication is successful
429 do_authloop(authctxt
);
431 /* The user has been authenticated and accepted. */
432 packet_start(SSH_SMSG_SUCCESS
);