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.
25 /* Tries to authenticate the user using password. Returns true if
26 authentication succeeds. */
28 int auth_password(struct passwd *pw, const char *password)
30 extern ServerOptions options;
31 char *encrypted_password;
33 if (pw->pw_uid == 0 && options.permit_root_login == 2)
35 /*packet_send_debug("Server does not permit root login with password.");*/
39 if (*password == '\0' && options.permit_empty_passwd == 0)
41 /*packet_send_debug("Server does not permit empty password login.");*/
45 /* deny if no user. */
50 if (options.skey_authentication == 1) {
51 if (strncasecmp(password, "s/key", 5) == 0) {
52 char *skeyinfo = skey_keyinfo(pw->pw_name);
54 debug("generating fake skeyinfo for %.100s.", pw->pw_name);
55 skeyinfo = skey_fake_keyinfo(pw->pw_name);
58 packet_send_debug(skeyinfo);
62 else if (skey_haskey(pw->pw_name) == 0 &&
63 skey_passcheck(pw->pw_name, (char *)password) != -1) {
64 /* Authentication succeeded. */
67 /* Fall back to ordinary passwd authentication. */
72 /* Support for Kerberos v4 authentication - Dug Song <dugsong@UMICH.EDU> */
73 if (options.kerberos_authentication)
79 char localhost[MAXHOSTNAMELEN];
84 /* Try Kerberos password authentication only for non-root
85 users and only if Kerberos is installed. */
86 if (pw->pw_uid != 0 && krb_get_lrealm(realm, 1) == KSUCCESS) {
88 /* Set up our ticket file. */
89 if (!krb4_init(pw->pw_uid)) {
90 log("Couldn't initialize Kerberos ticket file for %s!", pw->pw_name);
91 goto kerberos_auth_failure;
93 /* Try to get TGT using our password. */
94 r = krb_get_pw_in_tkt((char *)pw->pw_name, "", realm, "krbtgt", realm,
95 DEFAULT_TKT_LIFE, (char *)password);
97 packet_send_debug("Kerberos V4 password authentication for %s "
98 "failed: %s", pw->pw_name, krb_err_txt[r]);
99 goto kerberos_auth_failure;
101 /* Successful authentication. */
102 chown(tkt_string(), pw->pw_uid, pw->pw_gid);
104 /* Now that we have a TGT, try to get a local "rcmd" ticket to
105 ensure that we are not talking to a bogus Kerberos server. */
106 (void) gethostname(localhost, sizeof(localhost));
107 (void) strlcpy(phost, (char *)krb_get_phost(localhost), INST_SZ);
108 r = krb_mk_req(&tkt, KRB4_SERVICE_NAME, phost, realm, 33);
111 if (!(hp = gethostbyname(localhost))) {
112 log("Couldn't get local host address!");
113 goto kerberos_auth_failure;
115 memmove((void *)&faddr, (void *)hp->h_addr, sizeof(faddr));
117 /* Verify our "rcmd" ticket. */
118 r = krb_rd_req(&tkt, KRB4_SERVICE_NAME, phost, faddr, &adata, "");
119 if (r == RD_AP_UNDEC) {
120 /* Probably didn't have a srvtab on localhost. Allow login. */
121 log("Kerberos V4 TGT for %s unverifiable, no srvtab installed? "
122 "krb_rd_req: %s", pw->pw_name, krb_err_txt[r]);
124 else if (r != KSUCCESS) {
125 log("Kerberos V4 %s ticket unverifiable: %s",
126 KRB4_SERVICE_NAME, krb_err_txt[r]);
127 goto kerberos_auth_failure;
130 else if (r == KDC_PR_UNKNOWN) {
131 /* Allow login if no rcmd service exists, but log the error. */
132 log("Kerberos V4 TGT for %s unverifiable: %s; %s.%s "
133 "not registered, or srvtab is wrong?", pw->pw_name,
134 krb_err_txt[r], KRB4_SERVICE_NAME, phost);
137 /* TGT is bad, forget it. Possibly spoofed! */
138 packet_send_debug("WARNING: Kerberos V4 TGT possibly spoofed for"
139 "%s: %s", pw->pw_name, krb_err_txt[r]);
140 goto kerberos_auth_failure;
143 /* Authentication succeeded. */
146 kerberos_auth_failure:
147 krb4_cleanup_proc(NULL);
149 if (!options.kerberos_or_local_passwd)
153 /* Logging in as root or no local Kerberos realm. */
154 packet_send_debug("Unable to authenticate to Kerberos.");
156 /* Fall back to ordinary passwd authentication. */
160 /* Check for users with no password. */
161 if (strcmp(password, "") == 0 && strcmp(pw->pw_passwd, "") == 0)
163 packet_send_debug("Login permitted without a password because the account has no password.");
164 return 1; /* The user has no password and an empty password was tried. */
167 /* Encrypt the candidate password using the proper salt. */
168 encrypted_password = crypt(password,
169 (pw->pw_passwd[0] && pw->pw_passwd[1]) ?
170 pw->pw_passwd : "xx");
172 /* Authentication is accepted if the encrypted passwords are identical. */
173 return (strcmp(encrypted_password, pw->pw_passwd) == 0);