2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7 RCSID("$OpenBSD: auth1.c,v 1.2 2000/04/29 18:11:52 markus Exp $");
27 extern ServerOptions options;
28 extern char *forced_command;
31 * convert ssh auth msg type into description
34 get_authname(int type)
36 static char buf[1024];
38 case SSH_CMSG_AUTH_PASSWORD:
40 case SSH_CMSG_AUTH_RSA:
42 case SSH_CMSG_AUTH_RHOSTS_RSA:
44 case SSH_CMSG_AUTH_RHOSTS:
47 case SSH_CMSG_AUTH_KERBEROS:
51 case SSH_CMSG_AUTH_TIS_RESPONSE:
55 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
60 * The user does not exist or access is denied,
61 * but fake indication that authentication is needed.
64 do_fake_authloop1(char *user)
68 log("Faking authloop for illegal user %.200s from %.200s port %d",
73 #ifdef WITH_AIXAUTHENTICATE
74 loginfailed(user,get_canonical_hostname(),"ssh");
75 #endif /* WITH_AIXAUTHENTICATE */
77 /* Indicate that authentication is needed. */
78 packet_start(SSH_SMSG_FAILURE);
83 * Keep reading packets, and always respond with a failure. This is
84 * to avoid disclosing whether such a user really exists.
86 for (attempt = 1;; attempt++) {
87 /* Read a packet. This will not return if the client disconnects. */
90 (void)packet_read(&plen);
92 int type = packet_read(&plen);
94 char *password, *skeyinfo;
96 /* Try to send a fake s/key challenge. */
97 if (options.skey_authentication == 1 &&
98 (skeyinfo = skey_fake_keyinfo(user)) != NULL) {
99 if (type == SSH_CMSG_AUTH_TIS) {
100 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
101 packet_put_string(skeyinfo, strlen(skeyinfo));
105 } else if (type == SSH_CMSG_AUTH_PASSWORD &&
106 options.password_authentication &&
107 (password = packet_get_string(&dlen)) != NULL &&
109 strncasecmp(password, "s/key", 5) == 0 ) {
110 packet_send_debug(skeyinfo);
113 if (password != NULL)
116 if (attempt > AUTH_FAIL_MAX)
117 packet_disconnect(AUTH_FAIL_MSG, user);
120 * Send failure. This should be indistinguishable from a
121 * failed authentication.
123 packet_start(SSH_SMSG_FAILURE);
132 * read packets and try to authenticate local user *pw.
133 * return if authentication is successfull
136 do_authloop(struct passwd * pw)
140 RSA *client_host_key;
142 char *client_user = NULL, *password = NULL;
145 int plen, nlen, elen;
148 void (*authlog) (const char *fmt,...) = verbose;
150 extern int saved_argc;
151 extern char **saved_argv;
152 #endif /* HAVE_OSF_SIA */
154 /* Indicate that authentication is needed. */
155 packet_start(SSH_SMSG_FAILURE);
159 for (attempt = 1;; attempt++) {
160 int authenticated = 0;
161 strlcpy(user, "", sizeof user);
163 /* Get a packet from the client. */
164 type = packet_read(&plen);
166 /* Process the packet. */
169 case SSH_CMSG_HAVE_KERBEROS_TGT:
170 if (!options.kerberos_tgt_passing) {
171 /* packet_get_all(); */
172 verbose("Kerberos tgt passing disabled.");
175 /* Accept Kerberos tgt. */
176 char *tgt = packet_get_string(&dlen);
177 packet_integrity_check(plen, 4 + dlen, type);
178 if (!auth_kerberos_tgt(pw, tgt))
179 verbose("Kerberos tgt REFUSED for %s", pw->pw_name);
184 case SSH_CMSG_HAVE_AFS_TOKEN:
185 if (!options.afs_token_passing || !k_hasafs()) {
186 /* packet_get_all(); */
187 verbose("AFS token passing disabled.");
190 /* Accept AFS token. */
191 char *token_string = packet_get_string(&dlen);
192 packet_integrity_check(plen, 4 + dlen, type);
193 if (!auth_afs_token(pw, token_string))
194 verbose("AFS token REFUSED for %s", pw->pw_name);
200 case SSH_CMSG_AUTH_KERBEROS:
201 if (!options.kerberos_authentication) {
202 /* packet_get_all(); */
203 verbose("Kerberos authentication disabled.");
206 /* Try Kerberos v4 authentication. */
208 char *tkt_user = NULL;
209 char *kdata = packet_get_string((unsigned int *) &auth.length);
210 packet_integrity_check(plen, 4 + auth.length, type);
212 if (auth.length < MAX_KTXT_LEN)
213 memcpy(auth.dat, kdata, auth.length);
216 authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user);
219 snprintf(user, sizeof user, " tktuser %s", tkt_user);
226 case SSH_CMSG_AUTH_RHOSTS:
227 if (!options.rhosts_authentication) {
228 verbose("Rhosts authentication disabled.");
232 * Get client user name. Note that we just have to
233 * trust the client; this is one reason why rhosts
234 * authentication is insecure. (Another is
235 * IP-spoofing on a local network.)
237 client_user = packet_get_string(&ulen);
238 packet_integrity_check(plen, 4 + ulen, type);
240 /* Try to authenticate using /etc/hosts.equiv and
242 authenticated = auth_rhosts(pw, client_user);
244 snprintf(user, sizeof user, " ruser %s", client_user);
247 case SSH_CMSG_AUTH_RHOSTS_RSA:
248 if (!options.rhosts_rsa_authentication) {
249 verbose("Rhosts with RSA authentication disabled.");
253 * Get client user name. Note that we just have to
254 * trust the client; root on the client machine can
255 * claim to be any user.
257 client_user = packet_get_string(&ulen);
259 /* Get the client host key. */
260 client_host_key = RSA_new();
261 if (client_host_key == NULL)
262 fatal("RSA_new failed");
263 client_host_key->e = BN_new();
264 client_host_key->n = BN_new();
265 if (client_host_key->e == NULL || client_host_key->n == NULL)
266 fatal("BN_new failed");
267 bits = packet_get_int();
268 packet_get_bignum(client_host_key->e, &elen);
269 packet_get_bignum(client_host_key->n, &nlen);
271 if (bits != BN_num_bits(client_host_key->n))
272 log("Warning: keysize mismatch for client_host_key: "
273 "actual %d, announced %d", BN_num_bits(client_host_key->n), bits);
274 packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type);
276 authenticated = auth_rhosts_rsa(pw, client_user, client_host_key);
277 RSA_free(client_host_key);
279 snprintf(user, sizeof user, " ruser %s", client_user);
282 case SSH_CMSG_AUTH_RSA:
283 if (!options.rsa_authentication) {
284 verbose("RSA authentication disabled.");
287 /* RSA authentication requested. */
289 packet_get_bignum(n, &nlen);
290 packet_integrity_check(plen, nlen, type);
291 authenticated = auth_rsa(pw, n);
295 case SSH_CMSG_AUTH_PASSWORD:
296 if (!options.password_authentication) {
297 verbose("Password authentication disabled.");
301 * Read user password. It is in plain text, but was
302 * transmitted over the encrypted channel so it is
303 * not visible to an outside observer.
305 password = packet_get_string(&dlen);
306 packet_integrity_check(plen, 4 + dlen, type);
309 /* Do PAM auth with password */
310 authenticated = auth_pam_password(pw, password);
311 #elif defined(HAVE_OSF_SIA)
312 /* Do SIA auth with password */
313 if (sia_validate_user(NULL, saved_argc, saved_argv,
314 get_canonical_hostname(), pw->pw_name, NULL, 0,
315 NULL, password) == SIASUCCESS) {
318 #else /* !USE_PAM && !HAVE_OSF_SIA */
319 /* Try authentication with the password. */
320 authenticated = auth_password(pw, password);
323 memset(password, 0, strlen(password));
328 case SSH_CMSG_AUTH_TIS:
329 debug("rcvd SSH_CMSG_AUTH_TIS");
330 if (options.skey_authentication == 1) {
331 char *skeyinfo = skey_keyinfo(pw->pw_name);
332 if (skeyinfo == NULL) {
333 debug("generating fake skeyinfo for %.100s.", pw->pw_name);
334 skeyinfo = skey_fake_keyinfo(pw->pw_name);
336 if (skeyinfo != NULL) {
337 /* we send our s/key- in tis-challenge messages */
338 debug("sending challenge '%s'", skeyinfo);
339 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
340 packet_put_string(skeyinfo, strlen(skeyinfo));
347 case SSH_CMSG_AUTH_TIS_RESPONSE:
348 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
349 if (options.skey_authentication == 1) {
350 char *response = packet_get_string(&dlen);
351 debug("skey response == '%s'", response);
352 packet_integrity_check(plen, 4 + dlen, type);
353 authenticated = (skey_haskey(pw->pw_name) == 0 &&
354 skey_passcheck(pw->pw_name, response) != -1);
359 case SSH_CMSG_AUTH_TIS:
360 /* TIS Authentication is unsupported */
361 log("TIS authentication unsupported.");
367 * Any unknown messages will be ignored (and failure
368 * returned) during authentication.
370 log("Unknown message during authentication: type %d", type);
375 * Check if the user is logging in as root and root logins
377 * Note that root login is allowed for forced commands.
379 if (authenticated && pw->pw_uid == 0 && !options.permit_root_login) {
380 if (forced_command) {
381 log("Root login accepted for forced command.");
384 log("ROOT LOGIN REFUSED FROM %.200s",
385 get_canonical_hostname());
389 /* Raise logging level */
391 attempt == AUTH_FAIL_LOG ||
392 type == SSH_CMSG_AUTH_PASSWORD)
395 authlog("%s %s for %.200s from %.200s port %d%s",
396 authenticated ? "Accepted" : "Failed",
398 pw->pw_uid == 0 ? "ROOT" : pw->pw_name,
405 if (!do_pam_account(pw->pw_name, client_user)) {
406 if (client_user != NULL) {
410 do_fake_authloop1(pw->pw_name);
420 if (client_user != NULL) {
425 if (attempt > AUTH_FAIL_MAX) {
426 #ifdef WITH_AIXAUTHENTICATE
427 loginfailed(pw->pw_name,get_canonical_hostname(),"ssh");
428 #endif /* WITH_AIXAUTHENTICATE */
429 packet_disconnect(AUTH_FAIL_MSG, pw->pw_name);
432 /* Send a message indicating that the authentication attempt failed. */
433 packet_start(SSH_SMSG_FAILURE);
440 * Performs authentication of an incoming connection. Session key has already
441 * been exchanged and encryption is enabled.
446 struct passwd *pw, pwcopy;
450 #ifdef WITH_AIXAUTHENTICATE
451 extern char *aixloginmsg;
452 #endif /* WITH_AIXAUTHENTICATE */
454 /* Get the name of the user that we wish to log in as. */
455 packet_read_expect(&plen, SSH_CMSG_USER);
457 /* Get the user name. */
458 user = packet_get_string(&ulen);
459 packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER);
461 setproctitle("%s", user);
464 /* If machine has AFS, set process authentication group. */
471 /* Verify that the user is a valid user. */
473 if (!pw || !allowed_user(pw))
474 do_fake_authloop1(user);
477 /* Take a copy of the returned structure. */
478 memset(&pwcopy, 0, sizeof(pwcopy));
479 pwcopy.pw_name = xstrdup(pw->pw_name);
480 pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
481 pwcopy.pw_uid = pw->pw_uid;
482 pwcopy.pw_gid = pw->pw_gid;
483 pwcopy.pw_dir = xstrdup(pw->pw_dir);
484 pwcopy.pw_shell = xstrdup(pw->pw_shell);
492 * If we are not running as root, the user must have the same uid as
495 if (getuid() != 0 && pw->pw_uid != getuid())
496 packet_disconnect("Cannot change user when server not running as root.");
498 debug("Attempting authentication for %.100s.", pw->pw_name);
500 /* If the user has no password, accept authentication immediately. */
501 if (options.password_authentication &&
503 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
506 auth_pam_password(pw, "")) {
507 #elif defined(HAVE_OSF_SIA)
508 (sia_validate_user(NULL, saved_argc, saved_argv,
509 get_canonical_hostname(), pw->pw_name, NULL, 0, NULL,
510 "") == SIASUCCESS)) {
511 #else /* !HAVE_OSF_SIA && !USE_PAM */
512 auth_password(pw, "")) {
514 /* Authentication with empty password succeeded. */
515 log("Login for user %s from %.100s, accepted without authentication.",
516 pw->pw_name, get_remote_ipaddr());
518 /* Loop until the user has been authenticated or the
519 connection is closed, do_authloop() returns only if
520 authentication is successfull */
524 /* The user has been authenticated and accepted. */
525 #ifdef WITH_AIXAUTHENTICATE
526 /* We don't have a pty yet, so just label the line as "ssh" */
527 if (loginsuccess(user,get_canonical_hostname(),"ssh",&aixloginmsg) < 0)
529 #endif /* WITH_AIXAUTHENTICATE */
530 packet_start(SSH_SMSG_SUCCESS);
534 /* Perform session preparation. */
535 do_authenticated(pw);