- AUTH_DAT adat = { 0 };
- KTEXT_ST reply;
- char instance[INST_SZ];
- int r, s;
- u_int cksum;
- Key_schedule schedule;
- struct sockaddr_in local, foreign;
-
- s = packet_get_connection_in();
-
- r = sizeof(local);
- memset(&local, 0, sizeof(local));
- if (getsockname(s, (struct sockaddr *) &local, &r) < 0)
- debug("getsockname failed: %.100s", strerror(errno));
- r = sizeof(foreign);
- memset(&foreign, 0, sizeof(foreign));
- if (getpeername(s, (struct sockaddr *)&foreign, &r) < 0)
- debug("getpeername failed: %.100s", strerror(errno));
-
- instance[0] = '*'; instance[1] = 0;
-
- /* Get the encrypted request, challenge, and session key. */
- if ((r = krb_rd_req(auth, KRB4_SERVICE_NAME, instance, 0, &adat, ""))) {
- packet_send_debug("Kerberos V4 krb_rd_req: %.100s", krb_err_txt[r]);
- return 0;
- }
- des_key_sched((des_cblock *)adat.session, schedule);
-
- *client = xmalloc(MAX_K_NAME_SZ);
- (void) snprintf(*client, MAX_K_NAME_SZ, "%s%s%s@%s", adat.pname,
- *adat.pinst ? "." : "", adat.pinst, adat.prealm);
-
- /* Check ~/.klogin authorization now. */
- if (kuserok(&adat, (char *)server_user) != KSUCCESS) {
- packet_send_debug("Kerberos V4 .klogin authorization failed!");
- log("Kerberos V4 .klogin authorization failed for %s to account %s",
- *client, server_user);
- return 0;
- }
- /* Increment the checksum, and return it encrypted with the session key. */
- cksum = adat.checksum + 1;
- cksum = htonl(cksum);
-
- /* If we can't successfully encrypt the checksum, we send back an empty
- message, admitting our failure. */
- if ((r = krb_mk_priv((u_char *)&cksum, reply.dat, sizeof(cksum)+1,
- schedule, &adat.session, &local, &foreign)) < 0) {
- packet_send_debug("Kerberos V4 mk_priv: (%d) %s", r, krb_err_txt[r]);
- reply.dat[0] = 0;
- reply.length = 0;
- }
- else
- reply.length = r;
-
- /* Clear session key. */
- memset(&adat.session, 0, sizeof(&adat.session));
-
- packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE);
- packet_put_string((char *) reply.dat, reply.length);
- packet_send();
- packet_write_wait();
- return 1;
+ AUTH_DAT adata;
+ KTEXT_ST tkt;
+ struct hostent *hp;
+ struct passwd *pw;
+ char localhost[MAXHOSTNAMELEN], phost[INST_SZ], realm[REALM_SZ];
+ u_int32_t faddr;
+ int r;
+
+ if ((pw = authctxt->pw) == NULL)
+ return (0);
+
+ /*
+ * Try Kerberos password authentication only for non-root
+ * users and only if Kerberos is installed.
+ */
+ if (pw->pw_uid != 0 && krb_get_lrealm(realm, 1) == KSUCCESS) {
+ /* Set up our ticket file. */
+ if (!krb4_init(authctxt)) {
+ log("Couldn't initialize Kerberos ticket file for %s!",
+ pw->pw_name);
+ goto failure;
+ }
+ /* Try to get TGT using our password. */
+ r = krb_get_pw_in_tkt((char *) pw->pw_name, "", realm,
+ "krbtgt", realm, DEFAULT_TKT_LIFE, (char *)password);
+ if (r != INTK_OK) {
+ debug("Kerberos v4 password authentication for %s "
+ "failed: %s", pw->pw_name, krb_err_txt[r]);
+ goto failure;
+ }
+ /* Successful authentication. */
+ chown(tkt_string(), pw->pw_uid, pw->pw_gid);
+
+ /*
+ * Now that we have a TGT, try to get a local
+ * "rcmd" ticket to ensure that we are not talking
+ * to a bogus Kerberos server.
+ */
+ gethostname(localhost, sizeof(localhost));
+ strlcpy(phost, (char *)krb_get_phost(localhost),
+ sizeof(phost));
+ r = krb_mk_req(&tkt, KRB4_SERVICE_NAME, phost, realm, 33);
+
+ if (r == KSUCCESS) {
+ if ((hp = gethostbyname(localhost)) == NULL) {
+ log("Couldn't get local host address!");
+ goto failure;
+ }
+ memmove((void *)&faddr, (void *)hp->h_addr,
+ sizeof(faddr));
+
+ /* Verify our "rcmd" ticket. */
+ r = krb_rd_req(&tkt, KRB4_SERVICE_NAME, phost,
+ faddr, &adata, "");
+ if (r == RD_AP_UNDEC) {
+ /*
+ * Probably didn't have a srvtab on
+ * localhost. Disallow login.
+ */
+ log("Kerberos v4 TGT for %s unverifiable, "
+ "no srvtab installed? krb_rd_req: %s",
+ pw->pw_name, krb_err_txt[r]);
+ goto failure;
+ } else if (r != KSUCCESS) {
+ log("Kerberos v4 %s ticket unverifiable: %s",
+ KRB4_SERVICE_NAME, krb_err_txt[r]);
+ goto failure;
+ }
+ } else if (r == KDC_PR_UNKNOWN) {
+ /*
+ * Disallow login if no rcmd service exists, and
+ * log the error.
+ */
+ log("Kerberos v4 TGT for %s unverifiable: %s; %s.%s "
+ "not registered, or srvtab is wrong?", pw->pw_name,
+ krb_err_txt[r], KRB4_SERVICE_NAME, phost);
+ goto failure;
+ } else {
+ /*
+ * TGT is bad, forget it. Possibly spoofed!
+ */
+ debug("WARNING: Kerberos v4 TGT possibly spoofed "
+ "for %s: %s", pw->pw_name, krb_err_txt[r]);
+ goto failure;
+ }
+ /* Authentication succeeded. */
+ return (1);
+ } else
+ /* Logging in as root or no local Kerberos realm. */
+ debug("Unable to authenticate to Kerberos.");
+
+ failure:
+ krb4_cleanup_proc(authctxt);
+
+ if (!options.kerberos_or_local_passwd)
+ return (0);
+
+ /* Fall back to ordinary passwd authentication. */
+ return (-1);
+}
+
+void
+krb4_cleanup_proc(void *context)
+{
+ Authctxt *authctxt = (Authctxt *)context;
+ debug("krb4_cleanup_proc called");
+ if (authctxt->krb4_ticket_file) {
+ (void) dest_tkt();
+ xfree(authctxt->krb4_ticket_file);
+ authctxt->krb4_ticket_file = NULL;
+ }
+}
+
+int
+auth_krb4(Authctxt *authctxt, KTEXT auth, char **client)
+{
+ AUTH_DAT adat = {0};
+ KTEXT_ST reply;
+ Key_schedule schedule;
+ struct sockaddr_in local, foreign;
+ char instance[INST_SZ];
+ socklen_t slen;
+ u_int cksum;
+ int r, s;
+
+ s = packet_get_connection_in();
+
+ slen = sizeof(local);
+ memset(&local, 0, sizeof(local));
+ if (getsockname(s, (struct sockaddr *) & local, &slen) < 0)
+ debug("getsockname failed: %.100s", strerror(errno));
+ slen = sizeof(foreign);
+ memset(&foreign, 0, sizeof(foreign));
+ if (getpeername(s, (struct sockaddr *) & foreign, &slen) < 0) {
+ debug("getpeername failed: %.100s", strerror(errno));
+ fatal_cleanup();
+ }
+ instance[0] = '*';
+ instance[1] = 0;
+
+ /* Get the encrypted request, challenge, and session key. */
+ if ((r = krb_rd_req(auth, KRB4_SERVICE_NAME, instance,
+ 0, &adat, ""))) {
+ debug("Kerberos v4 krb_rd_req: %.100s", krb_err_txt[r]);
+ return (0);
+ }
+ des_key_sched((des_cblock *) adat.session, schedule);
+
+ *client = xmalloc(MAX_K_NAME_SZ);
+ (void) snprintf(*client, MAX_K_NAME_SZ, "%s%s%s@%s", adat.pname,
+ *adat.pinst ? "." : "", adat.pinst, adat.prealm);
+
+ /* Check ~/.klogin authorization now. */
+ if (kuserok(&adat, authctxt->user) != KSUCCESS) {
+ log("Kerberos v4 .klogin authorization failed for %s to "
+ "account %s", *client, authctxt->user);
+ xfree(*client);
+ return (0);
+ }
+ /* Increment the checksum, and return it encrypted with the
+ session key. */
+ cksum = adat.checksum + 1;
+ cksum = htonl(cksum);
+
+ /* If we can't successfully encrypt the checksum, we send back an
+ empty message, admitting our failure. */
+ if ((r = krb_mk_priv((u_char *) & cksum, reply.dat, sizeof(cksum) + 1,
+ schedule, &adat.session, &local, &foreign)) < 0) {
+ debug("Kerberos v4 mk_priv: (%d) %s", r, krb_err_txt[r]);
+ reply.dat[0] = 0;
+ reply.length = 0;
+ } else
+ reply.length = r;
+
+ /* Clear session key. */
+ memset(&adat.session, 0, sizeof(&adat.session));
+
+ packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE);
+ packet_put_string((char *) reply.dat, reply.length);
+ packet_send();
+ packet_write_wait();
+ return (1);