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.
33 #ifdef HAVE_MD5_PASSWORDS
37 /* Don't need anything from here if we are using PAM */
39 /* Tries to authenticate the user using password. Returns true if
40 authentication succeeds. */
42 int auth_password(struct passwd *pw, const char *password)
44 extern ServerOptions options;
45 char *encrypted_password;
50 if (pw->pw_uid == 0 && options.permit_root_login == 2)
52 /*packet_send_debug("Server does not permit root login with password.");*/
56 if (*password == '\0' && options.permit_empty_passwd == 0)
58 /*packet_send_debug("Server does not permit empty password login.");*/
62 /* deny if no user. */
67 if (options.skey_authentication == 1) {
68 if (strncasecmp(password, "s/key", 5) == 0) {
69 char *skeyinfo = skey_keyinfo(pw->pw_name);
71 debug("generating fake skeyinfo for %.100s.", pw->pw_name);
72 skeyinfo = skey_fake_keyinfo(pw->pw_name);
75 packet_send_debug(skeyinfo);
79 else if (skey_haskey(pw->pw_name) == 0 &&
80 skey_passcheck(pw->pw_name, (char *)password) != -1) {
81 /* Authentication succeeded. */
84 /* Fall back to ordinary passwd authentication. */
89 /* Support for Kerberos v4 authentication - Dug Song <dugsong@UMICH.EDU> */
90 if (options.kerberos_authentication)
96 char localhost[MAXHOSTNAMELEN];
101 /* Try Kerberos password authentication only for non-root
102 users and only if Kerberos is installed. */
103 if (pw->pw_uid != 0 && krb_get_lrealm(realm, 1) == KSUCCESS) {
105 /* Set up our ticket file. */
106 if (!krb4_init(pw->pw_uid)) {
107 log("Couldn't initialize Kerberos ticket file for %s!", pw->pw_name);
108 goto kerberos_auth_failure;
110 /* Try to get TGT using our password. */
111 r = krb_get_pw_in_tkt((char *)pw->pw_name, "", realm, "krbtgt", realm,
112 DEFAULT_TKT_LIFE, (char *)password);
114 packet_send_debug("Kerberos V4 password authentication for %s "
115 "failed: %s", pw->pw_name, krb_err_txt[r]);
116 goto kerberos_auth_failure;
118 /* Successful authentication. */
119 chown(tkt_string(), pw->pw_uid, pw->pw_gid);
121 /* Now that we have a TGT, try to get a local "rcmd" ticket to
122 ensure that we are not talking to a bogus Kerberos server. */
123 (void) gethostname(localhost, sizeof(localhost));
124 (void) strlcpy(phost, (char *)krb_get_phost(localhost), INST_SZ);
125 r = krb_mk_req(&tkt, KRB4_SERVICE_NAME, phost, realm, 33);
128 if (!(hp = gethostbyname(localhost))) {
129 log("Couldn't get local host address!");
130 goto kerberos_auth_failure;
132 memmove((void *)&faddr, (void *)hp->h_addr, sizeof(faddr));
134 /* Verify our "rcmd" ticket. */
135 r = krb_rd_req(&tkt, KRB4_SERVICE_NAME, phost, faddr, &adata, "");
136 if (r == RD_AP_UNDEC) {
137 /* Probably didn't have a srvtab on localhost. Allow login. */
138 log("Kerberos V4 TGT for %s unverifiable, no srvtab installed? "
139 "krb_rd_req: %s", pw->pw_name, krb_err_txt[r]);
141 else if (r != KSUCCESS) {
142 log("Kerberos V4 %s ticket unverifiable: %s",
143 KRB4_SERVICE_NAME, krb_err_txt[r]);
144 goto kerberos_auth_failure;
147 else if (r == KDC_PR_UNKNOWN) {
148 /* Allow login if no rcmd service exists, but log the error. */
149 log("Kerberos V4 TGT for %s unverifiable: %s; %s.%s "
150 "not registered, or srvtab is wrong?", pw->pw_name,
151 krb_err_txt[r], KRB4_SERVICE_NAME, phost);
154 /* TGT is bad, forget it. Possibly spoofed! */
155 packet_send_debug("WARNING: Kerberos V4 TGT possibly spoofed for"
156 "%s: %s", pw->pw_name, krb_err_txt[r]);
157 goto kerberos_auth_failure;
160 /* Authentication succeeded. */
163 kerberos_auth_failure:
164 krb4_cleanup_proc(NULL);
166 if (!options.kerberos_or_local_passwd)
170 /* Logging in as root or no local Kerberos realm. */
171 packet_send_debug("Unable to authenticate to Kerberos.");
173 /* Fall back to ordinary passwd authentication. */
177 /* Check for users with no password. */
178 if (strcmp(password, "") == 0 && strcmp(pw->pw_passwd, "") == 0)
180 packet_send_debug("Login permitted without a password because the account has no password.");
181 return 1; /* The user has no password and an empty password was tried. */
185 spw = getspnam(pw->pw_name);
189 if ((spw->sp_namp == NULL) || (strcmp(pw->pw_name, spw->sp_namp) != 0))
190 fatal("Shadow lookup returned garbage.");
192 if (strlen(spw->sp_pwdp) < 3)
195 /* Encrypt the candidate password using the proper salt. */
196 #ifdef HAVE_MD5_PASSWORDS
197 if (is_md5_salt(spw->sp_pwdp))
198 encrypted_password = md5_crypt(password, spw->sp_pwdp);
200 encrypted_password = crypt(password, spw->sp_pwdp);
201 #else /* HAVE_MD5_PASSWORDS */
202 encrypted_password = crypt(password, spw->sp_pwdp);
203 #endif /* HAVE_MD5_PASSWORDS */
205 /* Authentication is accepted if the encrypted passwords are identical. */
206 return (strcmp(encrypted_password, spw->sp_pwdp) == 0);
207 #else /* !HAVE_SHADOW_H */
209 /* Encrypt the candidate password using the proper salt. */
210 encrypted_password = crypt(password,
211 (pw->pw_passwd[0] && pw->pw_passwd[1]) ?
212 pw->pw_passwd : "xx");
213 /* Authentication is accepted if the encrypted passwords are identical. */
214 return (strcmp(encrypted_password, pw->pw_passwd) == 0);
215 #endif /* !HAVE_SHADOW_H */
218 #endif /* !HAVE_PAM */