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.4 2000/09/07 20:27:49 deraadt Exp $");
34 #define is_winnt (GetVersion() < 0x80000000)
38 extern ServerOptions options;
39 extern char *forced_command;
41 extern int saved_argc;
42 extern char **saved_argv;
43 #endif /* HAVE_OSF_SIA */
46 * convert ssh auth msg type into description
49 get_authname(int type)
51 static char buf[1024];
53 case SSH_CMSG_AUTH_PASSWORD:
55 case SSH_CMSG_AUTH_RSA:
57 case SSH_CMSG_AUTH_RHOSTS_RSA:
59 case SSH_CMSG_AUTH_RHOSTS:
62 case SSH_CMSG_AUTH_KERBEROS:
66 case SSH_CMSG_AUTH_TIS_RESPONSE:
70 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
75 * The user does not exist or access is denied,
76 * but fake indication that authentication is needed.
79 do_fake_authloop1(char *user)
83 log("Faking authloop for illegal user %.200s from %.200s port %d",
88 #ifdef WITH_AIXAUTHENTICATE
89 loginfailed(user,get_canonical_hostname(),"ssh");
90 #endif /* WITH_AIXAUTHENTICATE */
92 /* Indicate that authentication is needed. */
93 packet_start(SSH_SMSG_FAILURE);
98 * Keep reading packets, and always respond with a failure. This is
99 * to avoid disclosing whether such a user really exists.
101 for (attempt = 1;; attempt++) {
102 /* Read a packet. This will not return if the client disconnects. */
105 (void)packet_read(&plen);
107 int type = packet_read(&plen);
109 char *password, *skeyinfo;
111 /* Try to send a fake s/key challenge. */
112 if (options.skey_authentication == 1 &&
113 (skeyinfo = skey_fake_keyinfo(user)) != NULL) {
114 if (type == SSH_CMSG_AUTH_TIS) {
115 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
116 packet_put_string(skeyinfo, strlen(skeyinfo));
120 } else if (type == SSH_CMSG_AUTH_PASSWORD &&
121 options.password_authentication &&
122 (password = packet_get_string(&dlen)) != NULL &&
124 strncasecmp(password, "s/key", 5) == 0 ) {
125 packet_send_debug(skeyinfo);
128 if (password != NULL)
131 if (attempt > AUTH_FAIL_MAX)
132 packet_disconnect(AUTH_FAIL_MSG, user);
135 * Send failure. This should be indistinguishable from a
136 * failed authentication.
138 packet_start(SSH_SMSG_FAILURE);
147 * read packets and try to authenticate local user *pw.
148 * return if authentication is successfull
151 do_authloop(struct passwd * pw)
155 RSA *client_host_key;
157 char *client_user = NULL, *password = NULL;
160 int plen, nlen, elen;
163 void (*authlog) (const char *fmt,...) = verbose;
165 /* Indicate that authentication is needed. */
166 packet_start(SSH_SMSG_FAILURE);
170 for (attempt = 1;; attempt++) {
171 int authenticated = 0;
172 strlcpy(user, "", sizeof user);
174 /* Get a packet from the client. */
175 type = packet_read(&plen);
177 /* Process the packet. */
180 case SSH_CMSG_HAVE_KERBEROS_TGT:
181 if (!options.kerberos_tgt_passing) {
182 /* packet_get_all(); */
183 verbose("Kerberos tgt passing disabled.");
186 /* Accept Kerberos tgt. */
187 char *tgt = packet_get_string(&dlen);
188 packet_integrity_check(plen, 4 + dlen, type);
189 if (!auth_kerberos_tgt(pw, tgt))
190 verbose("Kerberos tgt REFUSED for %s", pw->pw_name);
195 case SSH_CMSG_HAVE_AFS_TOKEN:
196 if (!options.afs_token_passing || !k_hasafs()) {
197 /* packet_get_all(); */
198 verbose("AFS token passing disabled.");
201 /* Accept AFS token. */
202 char *token_string = packet_get_string(&dlen);
203 packet_integrity_check(plen, 4 + dlen, type);
204 if (!auth_afs_token(pw, token_string))
205 verbose("AFS token REFUSED for %s", pw->pw_name);
211 case SSH_CMSG_AUTH_KERBEROS:
212 if (!options.kerberos_authentication) {
213 /* packet_get_all(); */
214 verbose("Kerberos authentication disabled.");
217 /* Try Kerberos v4 authentication. */
219 char *tkt_user = NULL;
220 char *kdata = packet_get_string((unsigned int *) &auth.length);
221 packet_integrity_check(plen, 4 + auth.length, type);
223 if (auth.length < MAX_KTXT_LEN)
224 memcpy(auth.dat, kdata, auth.length);
227 authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user);
230 snprintf(user, sizeof user, " tktuser %s", tkt_user);
237 case SSH_CMSG_AUTH_RHOSTS:
238 if (!options.rhosts_authentication) {
239 verbose("Rhosts authentication disabled.");
243 * Get client user name. Note that we just have to
244 * trust the client; this is one reason why rhosts
245 * authentication is insecure. (Another is
246 * IP-spoofing on a local network.)
248 client_user = packet_get_string(&ulen);
249 packet_integrity_check(plen, 4 + ulen, type);
251 /* Try to authenticate using /etc/hosts.equiv and
253 authenticated = auth_rhosts(pw, client_user);
255 snprintf(user, sizeof user, " ruser %s", client_user);
258 case SSH_CMSG_AUTH_RHOSTS_RSA:
259 if (!options.rhosts_rsa_authentication) {
260 verbose("Rhosts with RSA authentication disabled.");
264 * Get client user name. Note that we just have to
265 * trust the client; root on the client machine can
266 * claim to be any user.
268 client_user = packet_get_string(&ulen);
270 /* Get the client host key. */
271 client_host_key = RSA_new();
272 if (client_host_key == NULL)
273 fatal("RSA_new failed");
274 client_host_key->e = BN_new();
275 client_host_key->n = BN_new();
276 if (client_host_key->e == NULL || client_host_key->n == NULL)
277 fatal("BN_new failed");
278 bits = packet_get_int();
279 packet_get_bignum(client_host_key->e, &elen);
280 packet_get_bignum(client_host_key->n, &nlen);
282 if (bits != BN_num_bits(client_host_key->n))
283 log("Warning: keysize mismatch for client_host_key: "
284 "actual %d, announced %d", BN_num_bits(client_host_key->n), bits);
285 packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type);
287 authenticated = auth_rhosts_rsa(pw, client_user, client_host_key);
288 RSA_free(client_host_key);
290 snprintf(user, sizeof user, " ruser %s", client_user);
293 case SSH_CMSG_AUTH_RSA:
294 if (!options.rsa_authentication) {
295 verbose("RSA authentication disabled.");
298 /* RSA authentication requested. */
300 packet_get_bignum(n, &nlen);
301 packet_integrity_check(plen, nlen, type);
302 authenticated = auth_rsa(pw, n);
306 case SSH_CMSG_AUTH_PASSWORD:
307 if (!options.password_authentication) {
308 verbose("Password authentication disabled.");
312 * Read user password. It is in plain text, but was
313 * transmitted over the encrypted channel so it is
314 * not visible to an outside observer.
316 password = packet_get_string(&dlen);
317 packet_integrity_check(plen, 4 + dlen, type);
320 /* Do PAM auth with password */
321 authenticated = auth_pam_password(pw, password);
322 #elif defined(HAVE_OSF_SIA)
323 /* Do SIA auth with password */
324 if (sia_validate_user(NULL, saved_argc, saved_argv,
325 get_canonical_hostname(), pw->pw_name, NULL, 0,
326 NULL, password) == SIASUCCESS) {
329 #else /* !USE_PAM && !HAVE_OSF_SIA */
330 /* Try authentication with the password. */
331 authenticated = auth_password(pw, password);
334 memset(password, 0, strlen(password));
339 case SSH_CMSG_AUTH_TIS:
340 debug("rcvd SSH_CMSG_AUTH_TIS");
341 if (options.skey_authentication == 1) {
342 char *skeyinfo = skey_keyinfo(pw->pw_name);
343 if (skeyinfo == NULL) {
344 debug("generating fake skeyinfo for %.100s.", pw->pw_name);
345 skeyinfo = skey_fake_keyinfo(pw->pw_name);
347 if (skeyinfo != NULL) {
348 /* we send our s/key- in tis-challenge messages */
349 debug("sending challenge '%s'", skeyinfo);
350 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
351 packet_put_string(skeyinfo, strlen(skeyinfo));
358 case SSH_CMSG_AUTH_TIS_RESPONSE:
359 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
360 if (options.skey_authentication == 1) {
361 char *response = packet_get_string(&dlen);
362 debug("skey response == '%s'", response);
363 packet_integrity_check(plen, 4 + dlen, type);
364 authenticated = (skey_haskey(pw->pw_name) == 0 &&
365 skey_passcheck(pw->pw_name, response) != -1);
370 case SSH_CMSG_AUTH_TIS:
371 /* TIS Authentication is unsupported */
372 log("TIS authentication unsupported.");
378 * Any unknown messages will be ignored (and failure
379 * returned) during authentication.
381 log("Unknown message during authentication: type %d", type);
387 * The only authentication which is able to change the user
388 * context on NT systems is the password authentication. So
389 * we deny all requsts for changing the user context if another
390 * authentication method is used.
391 * This may change in future when a special openssh
392 * subauthentication package is available.
394 if (is_winnt && type != SSH_CMSG_AUTH_PASSWORD &&
395 authenticated && geteuid() != pw->pw_uid) {
396 packet_disconnect("Authentication rejected for uid %d.",
403 * Check if the user is logging in as root and root logins
405 * Note that root login is allowed for forced commands.
407 if (authenticated && pw->pw_uid == 0 && !options.permit_root_login) {
408 if (forced_command) {
409 log("Root login accepted for forced command.");
412 log("ROOT LOGIN REFUSED FROM %.200s",
413 get_canonical_hostname());
417 /* Raise logging level */
419 attempt == AUTH_FAIL_LOG ||
420 type == SSH_CMSG_AUTH_PASSWORD)
423 authlog("%s %s for %.200s from %.200s port %d%s",
424 authenticated ? "Accepted" : "Failed",
426 pw->pw_uid == 0 ? "ROOT" : pw->pw_name,
433 if (!do_pam_account(pw->pw_name, client_user)) {
434 if (client_user != NULL) {
438 do_fake_authloop1(pw->pw_name);
448 if (client_user != NULL) {
453 if (attempt > AUTH_FAIL_MAX) {
454 #ifdef WITH_AIXAUTHENTICATE
455 loginfailed(pw->pw_name,get_canonical_hostname(),"ssh");
456 #endif /* WITH_AIXAUTHENTICATE */
457 packet_disconnect(AUTH_FAIL_MSG, pw->pw_name);
460 /* Send a message indicating that the authentication attempt failed. */
461 packet_start(SSH_SMSG_FAILURE);
468 * Performs authentication of an incoming connection. Session key has already
469 * been exchanged and encryption is enabled.
474 struct passwd *pw, pwcopy;
478 #ifdef WITH_AIXAUTHENTICATE
479 extern char *aixloginmsg;
480 #endif /* WITH_AIXAUTHENTICATE */
482 /* Get the name of the user that we wish to log in as. */
483 packet_read_expect(&plen, SSH_CMSG_USER);
485 /* Get the user name. */
486 user = packet_get_string(&ulen);
487 packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER);
489 setproctitle("%s", user);
492 /* If machine has AFS, set process authentication group. */
499 /* Verify that the user is a valid user. */
501 if (!pw || !allowed_user(pw))
502 do_fake_authloop1(user);
505 /* Take a copy of the returned structure. */
506 memset(&pwcopy, 0, sizeof(pwcopy));
507 pwcopy.pw_name = xstrdup(pw->pw_name);
508 pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
509 pwcopy.pw_uid = pw->pw_uid;
510 pwcopy.pw_gid = pw->pw_gid;
511 #ifdef HAVE_PW_CLASS_IN_PASSWD
512 pwcopy.pw_class = xstrdup(pw->pw_class);
514 pwcopy.pw_dir = xstrdup(pw->pw_dir);
515 pwcopy.pw_shell = xstrdup(pw->pw_shell);
524 * If we are not running as root, the user must have the same uid as
526 * Rule not valid on Windows systems.
528 if (getuid() != 0 && pw->pw_uid != getuid())
529 packet_disconnect("Cannot change user when server not running as root.");
532 debug("Attempting authentication for %.100s.", pw->pw_name);
534 /* If the user has no password, accept authentication immediately. */
535 if (options.password_authentication &&
537 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
540 auth_pam_password(pw, "")) {
541 #elif defined(HAVE_OSF_SIA)
542 (sia_validate_user(NULL, saved_argc, saved_argv,
543 get_canonical_hostname(), pw->pw_name, NULL, 0, NULL,
544 "") == SIASUCCESS)) {
545 #else /* !HAVE_OSF_SIA && !USE_PAM */
546 auth_password(pw, "")) {
548 /* Authentication with empty password succeeded. */
549 log("Login for user %s from %.100s, accepted without authentication.",
550 pw->pw_name, get_remote_ipaddr());
552 /* Loop until the user has been authenticated or the
553 connection is closed, do_authloop() returns only if
554 authentication is successfull */
558 /* The user has been authenticated and accepted. */
559 #ifdef WITH_AIXAUTHENTICATE
560 /* We don't have a pty yet, so just label the line as "ssh" */
561 if (loginsuccess(user,get_canonical_hostname(),"ssh",&aixloginmsg) < 0)
563 #endif /* WITH_AIXAUTHENTICATE */
564 packet_start(SSH_SMSG_SUCCESS);
568 /* Perform session preparation. */
569 do_authenticated(pw);