2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7 RCSID("$OpenBSD: auth1.c,v 1.3 2000/08/20 18:42:40 millert Exp $");
27 extern ServerOptions options;
28 extern char *forced_command;
30 extern int saved_argc;
31 extern char **saved_argv;
32 #endif /* HAVE_OSF_SIA */
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:
51 case SSH_CMSG_AUTH_KERBEROS:
55 case SSH_CMSG_AUTH_TIS_RESPONSE:
59 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
64 * The user does not exist or access is denied,
65 * but fake indication that authentication is needed.
68 do_fake_authloop1(char *user)
72 log("Faking authloop for illegal user %.200s from %.200s port %d",
77 #ifdef WITH_AIXAUTHENTICATE
78 loginfailed(user,get_canonical_hostname(),"ssh");
79 #endif /* WITH_AIXAUTHENTICATE */
81 /* Indicate that authentication is needed. */
82 packet_start(SSH_SMSG_FAILURE);
87 * Keep reading packets, and always respond with a failure. This is
88 * to avoid disclosing whether such a user really exists.
90 for (attempt = 1;; attempt++) {
91 /* Read a packet. This will not return if the client disconnects. */
94 (void)packet_read(&plen);
96 int type = packet_read(&plen);
98 char *password, *skeyinfo;
100 /* Try to send a fake s/key challenge. */
101 if (options.skey_authentication == 1 &&
102 (skeyinfo = skey_fake_keyinfo(user)) != NULL) {
103 if (type == SSH_CMSG_AUTH_TIS) {
104 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
105 packet_put_string(skeyinfo, strlen(skeyinfo));
109 } else if (type == SSH_CMSG_AUTH_PASSWORD &&
110 options.password_authentication &&
111 (password = packet_get_string(&dlen)) != NULL &&
113 strncasecmp(password, "s/key", 5) == 0 ) {
114 packet_send_debug(skeyinfo);
117 if (password != NULL)
120 if (attempt > AUTH_FAIL_MAX)
121 packet_disconnect(AUTH_FAIL_MSG, user);
124 * Send failure. This should be indistinguishable from a
125 * failed authentication.
127 packet_start(SSH_SMSG_FAILURE);
136 * read packets and try to authenticate local user *pw.
137 * return if authentication is successfull
140 do_authloop(struct passwd * pw)
144 RSA *client_host_key;
146 char *client_user = NULL, *password = NULL;
149 int plen, nlen, elen;
152 void (*authlog) (const char *fmt,...) = verbose;
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 #ifdef HAVE_PW_CLASS_IN_PASSWD
484 pwcopy.pw_class = xstrdup(pw->pw_class);
486 pwcopy.pw_dir = xstrdup(pw->pw_dir);
487 pwcopy.pw_shell = xstrdup(pw->pw_shell);
495 * If we are not running as root, the user must have the same uid as
498 if (getuid() != 0 && pw->pw_uid != getuid())
499 packet_disconnect("Cannot change user when server not running as root.");
501 debug("Attempting authentication for %.100s.", pw->pw_name);
503 /* If the user has no password, accept authentication immediately. */
504 if (options.password_authentication &&
506 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
509 auth_pam_password(pw, "")) {
510 #elif defined(HAVE_OSF_SIA)
511 (sia_validate_user(NULL, saved_argc, saved_argv,
512 get_canonical_hostname(), pw->pw_name, NULL, 0, NULL,
513 "") == SIASUCCESS)) {
514 #else /* !HAVE_OSF_SIA && !USE_PAM */
515 auth_password(pw, "")) {
517 /* Authentication with empty password succeeded. */
518 log("Login for user %s from %.100s, accepted without authentication.",
519 pw->pw_name, get_remote_ipaddr());
521 /* Loop until the user has been authenticated or the
522 connection is closed, do_authloop() returns only if
523 authentication is successfull */
527 /* The user has been authenticated and accepted. */
528 #ifdef WITH_AIXAUTHENTICATE
529 /* We don't have a pty yet, so just label the line as "ssh" */
530 if (loginsuccess(user,get_canonical_hostname(),"ssh",&aixloginmsg) < 0)
532 #endif /* WITH_AIXAUTHENTICATE */
533 packet_start(SSH_SMSG_SUCCESS);
537 /* Perform session preparation. */
538 do_authenticated(pw);