2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7 RCSID("$OpenBSD: auth.c,v 1.1 2000/03/28 21:15:45 markus Exp $");
25 extern ServerOptions options;
26 extern char *forced_command;
29 * Check if the user is allowed to log in via ssh. If user is listed in
30 * DenyUsers or user's primary group is listed in DenyGroups, false will
31 * be returned. If AllowUsers isn't empty and user isn't listed there, or
32 * if AllowGroups isn't empty and user isn't listed there, false will be
34 * If the user's shell is not executable, false will be returned.
35 * Otherwise true is returned.
38 allowed_user(struct passwd * pw)
43 #ifdef WITH_AIXAUTHENTICATE
45 #endif /* WITH_AIXAUTHENTICATE */
47 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
51 /* deny if shell does not exists or is not executable */
52 if (stat(pw->pw_shell, &st) != 0)
54 if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP))))
57 /* Return false if user is listed in DenyUsers */
58 if (options.num_deny_users > 0) {
61 for (i = 0; i < options.num_deny_users; i++)
62 if (match_pattern(pw->pw_name, options.deny_users[i]))
65 /* Return false if AllowUsers isn't empty and user isn't listed there */
66 if (options.num_allow_users > 0) {
69 for (i = 0; i < options.num_allow_users; i++)
70 if (match_pattern(pw->pw_name, options.allow_users[i]))
72 /* i < options.num_allow_users iff we break for loop */
73 if (i >= options.num_allow_users)
76 /* Get the primary group name if we need it. Return false if it fails */
77 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
78 grp = getgrgid(pw->pw_gid);
82 /* Return false if user's group is listed in DenyGroups */
83 if (options.num_deny_groups > 0) {
86 for (i = 0; i < options.num_deny_groups; i++)
87 if (match_pattern(grp->gr_name, options.deny_groups[i]))
91 * Return false if AllowGroups isn't empty and user's group
94 if (options.num_allow_groups > 0) {
97 for (i = 0; i < options.num_allow_groups; i++)
98 if (match_pattern(grp->gr_name, options.allow_groups[i]))
100 /* i < options.num_allow_groups iff we break for
102 if (i >= options.num_allow_groups)
107 #ifdef WITH_AIXAUTHENTICATE
108 if (loginrestrictions(pw->pw_name,S_LOGIN,NULL,&loginmsg) != 0)
110 #endif /* WITH_AIXAUTHENTICATE */
112 /* We found no reason not to let this user try to log on... */
117 * convert ssh auth msg type into description
120 get_authname(int type)
122 static char buf[1024];
124 case SSH_CMSG_AUTH_PASSWORD:
126 case SSH_CMSG_AUTH_RSA:
128 case SSH_CMSG_AUTH_RHOSTS_RSA:
130 case SSH_CMSG_AUTH_RHOSTS:
133 case SSH_CMSG_AUTH_KERBEROS:
137 case SSH_CMSG_AUTH_TIS_RESPONSE:
141 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
145 #define AUTH_FAIL_MAX 6
146 #define AUTH_FAIL_LOG (AUTH_FAIL_MAX/2)
147 #define AUTH_FAIL_MSG "Too many authentication failures for %.100s"
150 * The user does not exist or access is denied,
151 * but fake indication that authentication is needed.
154 do_fake_authloop1(char *user)
158 log("Faking authloop for illegal user %.200s from %.200s port %d",
163 #ifdef WITH_AIXAUTHENTICATE
164 if (strncmp(get_authname(type),"password",
165 strlen(get_authname(type))) == 0)
166 loginfailed(pw->pw_name,get_canonical_hostname(),"ssh");
167 #endif /* WITH_AIXAUTHENTICATE */
169 /* Indicate that authentication is needed. */
170 packet_start(SSH_SMSG_FAILURE);
175 * Keep reading packets, and always respond with a failure. This is
176 * to avoid disclosing whether such a user really exists.
178 for (attempt = 1;; attempt++) {
179 /* Read a packet. This will not return if the client disconnects. */
182 (void)packet_read(&plen);
184 int type = packet_read(&plen);
186 char *password, *skeyinfo;
187 /* Try to send a fake s/key challenge. */
188 if (options.skey_authentication == 1 &&
189 (skeyinfo = skey_fake_keyinfo(user)) != NULL) {
191 if (type == SSH_CMSG_AUTH_TIS) {
192 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
193 packet_put_string(skeyinfo, strlen(skeyinfo));
197 } else if (type == SSH_CMSG_AUTH_PASSWORD &&
198 options.password_authentication &&
199 (password = packet_get_string(&dlen)) != NULL &&
201 strncasecmp(password, "s/key", 5) == 0 ) {
202 packet_send_debug(skeyinfo);
204 if (password != NULL)
208 if (attempt > AUTH_FAIL_MAX)
209 packet_disconnect(AUTH_FAIL_MSG, user);
212 * Send failure. This should be indistinguishable from a
213 * failed authentication.
215 packet_start(SSH_SMSG_FAILURE);
224 * read packets and try to authenticate local user *pw.
225 * return if authentication is successfull
228 do_authloop(struct passwd * pw)
232 RSA *client_host_key;
234 char *client_user = NULL, *password = NULL;
237 int plen, nlen, elen;
240 void (*authlog) (const char *fmt,...) = verbose;
242 /* Indicate that authentication is needed. */
243 packet_start(SSH_SMSG_FAILURE);
247 for (attempt = 1;; attempt++) {
248 int authenticated = 0;
249 strlcpy(user, "", sizeof user);
251 /* Get a packet from the client. */
252 type = packet_read(&plen);
254 /* Process the packet. */
257 case SSH_CMSG_HAVE_KERBEROS_TGT:
258 if (!options.kerberos_tgt_passing) {
259 /* packet_get_all(); */
260 verbose("Kerberos tgt passing disabled.");
263 /* Accept Kerberos tgt. */
264 char *tgt = packet_get_string(&dlen);
265 packet_integrity_check(plen, 4 + dlen, type);
266 if (!auth_kerberos_tgt(pw, tgt))
267 verbose("Kerberos tgt REFUSED for %s", pw->pw_name);
272 case SSH_CMSG_HAVE_AFS_TOKEN:
273 if (!options.afs_token_passing || !k_hasafs()) {
274 /* packet_get_all(); */
275 verbose("AFS token passing disabled.");
278 /* Accept AFS token. */
279 char *token_string = packet_get_string(&dlen);
280 packet_integrity_check(plen, 4 + dlen, type);
281 if (!auth_afs_token(pw, token_string))
282 verbose("AFS token REFUSED for %s", pw->pw_name);
288 case SSH_CMSG_AUTH_KERBEROS:
289 if (!options.kerberos_authentication) {
290 /* packet_get_all(); */
291 verbose("Kerberos authentication disabled.");
294 /* Try Kerberos v4 authentication. */
296 char *tkt_user = NULL;
297 char *kdata = packet_get_string((unsigned int *) &auth.length);
298 packet_integrity_check(plen, 4 + auth.length, type);
300 if (auth.length < MAX_KTXT_LEN)
301 memcpy(auth.dat, kdata, auth.length);
304 authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user);
307 snprintf(user, sizeof user, " tktuser %s", tkt_user);
314 case SSH_CMSG_AUTH_RHOSTS:
315 if (!options.rhosts_authentication) {
316 verbose("Rhosts authentication disabled.");
320 * Get client user name. Note that we just have to
321 * trust the client; this is one reason why rhosts
322 * authentication is insecure. (Another is
323 * IP-spoofing on a local network.)
325 client_user = packet_get_string(&ulen);
326 packet_integrity_check(plen, 4 + ulen, type);
328 /* Try to authenticate using /etc/hosts.equiv and
330 authenticated = auth_rhosts(pw, client_user);
332 snprintf(user, sizeof user, " ruser %s", client_user);
335 case SSH_CMSG_AUTH_RHOSTS_RSA:
336 if (!options.rhosts_rsa_authentication) {
337 verbose("Rhosts with RSA authentication disabled.");
341 * Get client user name. Note that we just have to
342 * trust the client; root on the client machine can
343 * claim to be any user.
345 client_user = packet_get_string(&ulen);
347 /* Get the client host key. */
348 client_host_key = RSA_new();
349 if (client_host_key == NULL)
350 fatal("RSA_new failed");
351 client_host_key->e = BN_new();
352 client_host_key->n = BN_new();
353 if (client_host_key->e == NULL || client_host_key->n == NULL)
354 fatal("BN_new failed");
355 bits = packet_get_int();
356 packet_get_bignum(client_host_key->e, &elen);
357 packet_get_bignum(client_host_key->n, &nlen);
359 if (bits != BN_num_bits(client_host_key->n))
360 error("Warning: keysize mismatch for client_host_key: "
361 "actual %d, announced %d", BN_num_bits(client_host_key->n), bits);
362 packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type);
364 authenticated = auth_rhosts_rsa(pw, client_user, client_host_key);
365 RSA_free(client_host_key);
367 snprintf(user, sizeof user, " ruser %s", client_user);
370 case SSH_CMSG_AUTH_RSA:
371 if (!options.rsa_authentication) {
372 verbose("RSA authentication disabled.");
375 /* RSA authentication requested. */
377 packet_get_bignum(n, &nlen);
378 packet_integrity_check(plen, nlen, type);
379 authenticated = auth_rsa(pw, n);
383 case SSH_CMSG_AUTH_PASSWORD:
384 if (!options.password_authentication) {
385 verbose("Password authentication disabled.");
389 * Read user password. It is in plain text, but was
390 * transmitted over the encrypted channel so it is
391 * not visible to an outside observer.
393 password = packet_get_string(&dlen);
394 packet_integrity_check(plen, 4 + dlen, type);
397 /* Do PAM auth with password */
398 authenticated = auth_pam_password(pw, password);
400 /* Try authentication with the password. */
401 authenticated = auth_password(pw, password);
403 memset(password, 0, strlen(password));
408 case SSH_CMSG_AUTH_TIS:
409 debug("rcvd SSH_CMSG_AUTH_TIS");
410 if (options.skey_authentication == 1) {
411 char *skeyinfo = skey_keyinfo(pw->pw_name);
412 if (skeyinfo == NULL) {
413 debug("generating fake skeyinfo for %.100s.", pw->pw_name);
414 skeyinfo = skey_fake_keyinfo(pw->pw_name);
416 if (skeyinfo != NULL) {
417 /* we send our s/key- in tis-challenge messages */
418 debug("sending challenge '%s'", skeyinfo);
419 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
420 packet_put_string(skeyinfo, strlen(skeyinfo));
427 case SSH_CMSG_AUTH_TIS_RESPONSE:
428 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
429 if (options.skey_authentication == 1) {
430 char *response = packet_get_string(&dlen);
431 debug("skey response == '%s'", response);
432 packet_integrity_check(plen, 4 + dlen, type);
433 authenticated = (skey_haskey(pw->pw_name) == 0 &&
434 skey_passcheck(pw->pw_name, response) != -1);
439 case SSH_CMSG_AUTH_TIS:
440 /* TIS Authentication is unsupported */
441 log("TIS authentication unsupported.");
447 * Any unknown messages will be ignored (and failure
448 * returned) during authentication.
450 log("Unknown message during authentication: type %d", type);
455 * Check if the user is logging in as root and root logins
457 * Note that root login is allowed for forced commands.
459 if (authenticated && pw->pw_uid == 0 && !options.permit_root_login) {
460 if (forced_command) {
461 log("Root login accepted for forced command.");
464 log("ROOT LOGIN REFUSED FROM %.200s",
465 get_canonical_hostname());
469 /* Raise logging level */
471 attempt == AUTH_FAIL_LOG ||
472 type == SSH_CMSG_AUTH_PASSWORD)
475 authlog("%s %s for %.200s from %.200s port %d%s",
476 authenticated ? "Accepted" : "Failed",
478 pw->pw_uid == 0 ? "ROOT" : pw->pw_name,
485 if (!do_pam_account(pw->pw_name, client_user)) {
486 if (client_user != NULL) {
490 do_fake_authloop1(pw->pw_name);
500 if (client_user != NULL) {
505 if (attempt > AUTH_FAIL_MAX)
506 packet_disconnect(AUTH_FAIL_MSG, pw->pw_name);
508 /* Send a message indicating that the authentication attempt failed. */
509 packet_start(SSH_SMSG_FAILURE);
516 * Performs authentication of an incoming connection. Session key has already
517 * been exchanged and encryption is enabled.
522 struct passwd *pw, pwcopy;
526 #ifdef WITH_AIXAUTHENTICATE
528 #endif /* WITH_AIXAUTHENTICATE */
530 /* Get the name of the user that we wish to log in as. */
531 packet_read_expect(&plen, SSH_CMSG_USER);
533 /* Get the user name. */
534 user = packet_get_string(&ulen);
535 packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER);
537 setproctitle("%s", user);
540 /* If machine has AFS, set process authentication group. */
547 /* Verify that the user is a valid user. */
549 if (!pw || !allowed_user(pw))
550 do_fake_authloop1(user);
553 /* Take a copy of the returned structure. */
554 memset(&pwcopy, 0, sizeof(pwcopy));
555 pwcopy.pw_name = xstrdup(pw->pw_name);
556 pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
557 pwcopy.pw_uid = pw->pw_uid;
558 pwcopy.pw_gid = pw->pw_gid;
559 pwcopy.pw_dir = xstrdup(pw->pw_dir);
560 pwcopy.pw_shell = xstrdup(pw->pw_shell);
568 * If we are not running as root, the user must have the same uid as
571 if (getuid() != 0 && pw->pw_uid != getuid())
572 packet_disconnect("Cannot change user when server not running as root.");
574 debug("Attempting authentication for %.100s.", pw->pw_name);
576 /* If the user has no password, accept authentication immediately. */
577 if (options.password_authentication &&
579 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
582 auth_pam_password(pw, "")) {
584 auth_password(pw, "")) {
586 /* Authentication with empty password succeeded. */
587 log("Login for user %s from %.100s, accepted without authentication.",
588 pw->pw_name, get_remote_ipaddr());
590 /* Loop until the user has been authenticated or the
591 connection is closed, do_authloop() returns only if
592 authentication is successfull */
596 /* The user has been authenticated and accepted. */
597 #ifdef WITH_AIXAUTHENTICATE
598 loginsuccess(user,get_canonical_hostname(),"ssh",&loginmsg);
599 #endif /* WITH_AIXAUTHENTICATE */
600 packet_start(SSH_SMSG_SUCCESS);
604 /* Perform session preparation. */
605 do_authenticated(pw);