5 Author: Tatu Ylonen <ylo@cs.hut.fi>
7 Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 Created: Sat Mar 18 05:11:38 1995 ylo
12 Password authentication. This file contains the functions to check whether
13 the password is valid for the user.
29 /* Tries to authenticate the user using password. Returns true if
30 authentication succeeds. */
32 int auth_password(struct passwd *pw, const char *password)
34 extern ServerOptions options;
35 char *encrypted_password;
37 if (pw->pw_uid == 0 && options.permit_root_login == 2)
39 /*packet_send_debug("Server does not permit root login with password.");*/
43 if (*password == '\0' && options.permit_empty_passwd == 0)
45 /*packet_send_debug("Server does not permit empty password login.");*/
49 /* deny if no user. */
54 if (options.skey_authentication == 1) {
55 if (strncasecmp(password, "s/key", 5) == 0) {
56 char *skeyinfo = skey_keyinfo(pw->pw_name);
58 debug("generating fake skeyinfo for %.100s.", pw->pw_name);
59 skeyinfo = skey_fake_keyinfo(pw->pw_name);
62 packet_send_debug(skeyinfo);
66 else if (skey_haskey(pw->pw_name) == 0 &&
67 skey_passcheck(pw->pw_name, (char *)password) != -1) {
68 /* Authentication succeeded. */
71 /* Fall back to ordinary passwd authentication. */
76 /* Support for Kerberos v4 authentication - Dug Song <dugsong@UMICH.EDU> */
77 if (options.kerberos_authentication)
83 char localhost[MAXHOSTNAMELEN]; /* local host name */
84 char phost[INST_SZ]; /* host instance */
85 char realm[REALM_SZ]; /* local Kerberos realm */
88 /* Try Kerberos password authentication only for non-root
89 users and only if Kerberos is installed. */
90 if (pw->pw_uid != 0 && krb_get_lrealm(realm, 1) == KSUCCESS) {
92 /* Set up our ticket file. */
93 if (!ssh_tf_init(pw->pw_uid)) {
94 log("Couldn't initialize Kerberos ticket file for %s!",
96 goto kerberos_auth_failure;
98 /* Try to get TGT using our password. */
99 r = krb_get_pw_in_tkt((char *)pw->pw_name, "", realm, "krbtgt", realm,
100 DEFAULT_TKT_LIFE, (char *)password);
102 packet_send_debug("Kerberos V4 password authentication for %s "
103 "failed: %s", pw->pw_name, krb_err_txt[r]);
104 goto kerberos_auth_failure;
106 /* Successful authentication. */
107 chown(ticket, pw->pw_uid, pw->pw_gid);
109 (void) gethostname(localhost, sizeof(localhost));
110 (void) strlcpy(phost, (char *)krb_get_phost(localhost), INST_SZ);
112 /* Now that we have a TGT, try to get a local "rcmd" ticket to
113 ensure that we are not talking to a bogus Kerberos server. */
114 r = krb_mk_req(&tkt, KRB4_SERVICE_NAME, phost, realm, 33);
117 if (!(hp = gethostbyname(localhost))) {
118 log("Couldn't get local host address!");
119 goto kerberos_auth_failure;
121 memmove((void *)&faddr, (void *)hp->h_addr, sizeof(faddr));
123 /* Verify our "rcmd" ticket. */
124 r = krb_rd_req(&tkt, KRB4_SERVICE_NAME, phost, faddr, &adata, "");
125 if (r == RD_AP_UNDEC) {
126 /* Probably didn't have a srvtab on localhost. Allow login. */
127 log("Kerberos V4 TGT for %s unverifiable, no srvtab installed? "
128 "krb_rd_req: %s", pw->pw_name, krb_err_txt[r]);
130 else if (r != KSUCCESS) {
131 log("Kerberos V4 %s ticket unverifiable: %s",
132 KRB4_SERVICE_NAME, krb_err_txt[r]);
133 goto kerberos_auth_failure;
136 else if (r == KDC_PR_UNKNOWN) {
137 /* Allow login if no rcmd service exists, but log the error. */
138 log("Kerberos V4 TGT for %s unverifiable: %s; %s.%s "
139 "not registered, or srvtab is wrong?", pw->pw_name,
140 krb_err_txt[r], KRB4_SERVICE_NAME, phost);
143 /* TGT is bad, forget it. Possibly spoofed! */
144 packet_send_debug("WARNING: Kerberos V4 TGT possibly spoofed for"
145 "%s: %s", pw->pw_name, krb_err_txt[r]);
146 goto kerberos_auth_failure;
149 /* Authentication succeeded. */
152 kerberos_auth_failure:
156 if (!options.kerberos_or_local_passwd ) return 0;
159 /* Logging in as root or no local Kerberos realm. */
160 packet_send_debug("Unable to authenticate to Kerberos.");
162 /* Fall back to ordinary passwd authentication. */
166 /* Check for users with no password. */
167 if (strcmp(password, "") == 0 && strcmp(pw->pw_passwd, "") == 0)
169 packet_send_debug("Login permitted without a password because the account has no password.");
170 return 1; /* The user has no password and an empty password was tried. */
173 /* Encrypt the candidate password using the proper salt. */
174 encrypted_password = crypt(password,
175 (pw->pw_passwd[0] && pw->pw_passwd[1]) ?
176 pw->pw_passwd : "xx");
178 /* Authentication is accepted if the encrypted passwords are identical. */
179 return (strcmp(encrypted_password, pw->pw_passwd) == 0);