2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * As far as I am concerned, the code I have written for this software
6 * can be used freely for any purpose. Any derived versions of this
7 * software must be clearly marked as such, and if the derived work is
8 * incompatible with the protocol description in the RFC file, it must be
9 * called by a name other than "ssh" or "Secure Shell".
13 RCSID("$OpenBSD: auth1.c,v 1.59 2004/07/28 09:40:29 markus Exp $");
27 #include "monitor_wrap.h"
31 extern ServerOptions options
;
32 extern Buffer loginmsg
;
35 * convert ssh auth msg type into description
38 get_authname(int type
)
40 static char buf
[1024];
42 case SSH_CMSG_AUTH_PASSWORD
:
44 case SSH_CMSG_AUTH_RSA
:
46 case SSH_CMSG_AUTH_RHOSTS_RSA
:
48 case SSH_CMSG_AUTH_RHOSTS
:
50 case SSH_CMSG_AUTH_TIS
:
51 case SSH_CMSG_AUTH_TIS_RESPONSE
:
52 return "challenge-response";
54 snprintf(buf
, sizeof buf
, "bad-auth-msg-%d", type
);
59 * read packets, try to authenticate the user and
60 * return only if authentication is successful
63 do_authloop(Authctxt
*authctxt
)
65 int authenticated
= 0;
69 char *client_user
, *password
;
75 debug("Attempting authentication for %s%.100s.",
76 authctxt
->valid
? "" : "invalid user ", authctxt
->user
);
78 /* If the user has no password, accept authentication immediately. */
79 if (options
.password_authentication
&&
81 (!options
.kerberos_authentication
|| options
.kerberos_or_local_passwd
) &&
83 PRIVSEP(auth_password(authctxt
, ""))) {
85 if (options
.use_pam
&& (PRIVSEP(do_pam_account())))
88 auth_log(authctxt
, 1, "without authentication", "");
93 /* Indicate that authentication is needed. */
94 packet_start(SSH_SMSG_FAILURE
);
101 /* default to fail */
106 /* Get a packet from the client. */
108 type
= packet_read();
111 * If we started challenge-response authentication but the
112 * next packet is not a response to our challenge, release
113 * the resources allocated by get_challenge() (which would
114 * normally have been released by verify_response() had we
115 * received such a response)
117 if (prev
== SSH_CMSG_AUTH_TIS
&&
118 type
!= SSH_CMSG_AUTH_TIS_RESPONSE
)
119 abandon_challenge_response(authctxt
);
121 /* Process the packet. */
123 case SSH_CMSG_AUTH_RHOSTS_RSA
:
124 if (!options
.rhosts_rsa_authentication
) {
125 verbose("Rhosts with RSA authentication disabled.");
129 * Get client user name. Note that we just have to
130 * trust the client; root on the client machine can
131 * claim to be any user.
133 client_user
= packet_get_string(&ulen
);
135 /* Get the client host key. */
136 client_host_key
= key_new(KEY_RSA1
);
137 bits
= packet_get_int();
138 packet_get_bignum(client_host_key
->rsa
->e
);
139 packet_get_bignum(client_host_key
->rsa
->n
);
141 if (bits
!= BN_num_bits(client_host_key
->rsa
->n
))
142 verbose("Warning: keysize mismatch for client_host_key: "
143 "actual %d, announced %d",
144 BN_num_bits(client_host_key
->rsa
->n
), bits
);
147 authenticated
= auth_rhosts_rsa(authctxt
, client_user
,
149 key_free(client_host_key
);
151 snprintf(info
, sizeof info
, " ruser %.100s", client_user
);
154 case SSH_CMSG_AUTH_RSA
:
155 if (!options
.rsa_authentication
) {
156 verbose("RSA authentication disabled.");
159 /* RSA authentication requested. */
160 if ((n
= BN_new()) == NULL
)
161 fatal("do_authloop: BN_new failed");
162 packet_get_bignum(n
);
164 authenticated
= auth_rsa(authctxt
, n
);
168 case SSH_CMSG_AUTH_PASSWORD
:
169 if (!options
.password_authentication
) {
170 verbose("Password authentication disabled.");
174 * Read user password. It is in plain text, but was
175 * transmitted over the encrypted channel so it is
176 * not visible to an outside observer.
178 password
= packet_get_string(&dlen
);
181 /* Try authentication with the password. */
182 authenticated
= PRIVSEP(auth_password(authctxt
, password
));
184 memset(password
, 0, strlen(password
));
188 case SSH_CMSG_AUTH_TIS
:
189 debug("rcvd SSH_CMSG_AUTH_TIS");
190 if (options
.challenge_response_authentication
== 1) {
191 char *challenge
= get_challenge(authctxt
);
192 if (challenge
!= NULL
) {
193 debug("sending challenge '%s'", challenge
);
194 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE
);
195 packet_put_cstring(challenge
);
203 case SSH_CMSG_AUTH_TIS_RESPONSE
:
204 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
205 if (options
.challenge_response_authentication
== 1) {
206 char *response
= packet_get_string(&dlen
);
208 authenticated
= verify_response(authctxt
, response
);
209 memset(response
, 'r', dlen
);
216 * Any unknown messages will be ignored (and failure
217 * returned) during authentication.
219 logit("Unknown message during authentication: type %d", type
);
224 auth_close(authctxt
->as
);
228 if (!authctxt
->valid
&& authenticated
)
229 fatal("INTERNAL ERROR: authenticated invalid user %s",
233 if (authenticated
&& cray_access_denied(authctxt
->user
)) {
235 fatal("Access denied for user %s.",authctxt
->user
);
241 !check_nt_auth(type
== SSH_CMSG_AUTH_PASSWORD
,
243 packet_disconnect("Authentication rejected for uid %d.",
244 authctxt
->pw
== NULL
? -1 : authctxt
->pw
->pw_uid
);
248 /* Special handling for root */
249 if (authenticated
&& authctxt
->pw
->pw_uid
== 0 &&
250 !auth_root_allowed(get_authname(type
))) {
252 # ifdef SSH_AUDIT_EVENTS
253 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED
));
259 if (options
.use_pam
&& authenticated
&&
260 !PRIVSEP(do_pam_account())) {
264 error("Access denied for user %s by PAM account "
265 "configuration", authctxt
->user
);
266 len
= buffer_len(&loginmsg
);
267 buffer_append(&loginmsg
, "\0", 1);
268 msg
= buffer_ptr(&loginmsg
);
269 /* strip trailing newlines */
271 while (len
> 0 && msg
[--len
] == '\n')
274 msg
= "Access denied.";
275 packet_disconnect(msg
);
279 /* Log before sending the reply */
280 auth_log(authctxt
, authenticated
, get_authname(type
), info
);
282 if (client_user
!= NULL
) {
290 if (authctxt
->failures
++ > options
.max_authtries
) {
291 #ifdef SSH_AUDIT_EVENTS
292 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES
));
294 packet_disconnect(AUTH_FAIL_MSG
, authctxt
->user
);
297 packet_start(SSH_SMSG_FAILURE
);
304 * Performs authentication of an incoming connection. Session key has already
305 * been exchanged and encryption is enabled.
308 do_authentication(Authctxt
*authctxt
)
311 char *user
, *style
= NULL
;
313 /* Get the name of the user that we wish to log in as. */
314 packet_read_expect(SSH_CMSG_USER
);
316 /* Get the user name. */
317 user
= packet_get_string(&ulen
);
320 if ((style
= strchr(user
, ':')) != NULL
)
323 authctxt
->user
= user
;
324 authctxt
->style
= style
;
326 /* Verify that the user is a valid user. */
327 if ((authctxt
->pw
= PRIVSEP(getpwnamallow(user
))) != NULL
)
330 debug("do_authentication: invalid user %s", user
);
331 authctxt
->pw
= fakepw();
334 setproctitle("%s%s", authctxt
->valid
? user
: "unknown",
335 use_privsep
? " [net]" : "");
339 PRIVSEP(start_pam(authctxt
));
343 * If we are not running as root, the user must have the same uid as
344 * the server. (Unless you are running Windows)
347 if (!use_privsep
&& getuid() != 0 && authctxt
->pw
&&
348 authctxt
->pw
->pw_uid
!= getuid())
349 packet_disconnect("Cannot change user when server not running as root.");
353 * Loop until the user has been authenticated or the connection is
354 * closed, do_authloop() returns only if authentication is successful
356 do_authloop(authctxt
);
358 /* The user has been authenticated and accepted. */
359 packet_start(SSH_SMSG_SUCCESS
);