[BACK]Return to auth-krb4.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Diff for /src/usr.bin/ssh/Attic/auth-krb4.c between version 1.19 and 1.19.2.6

version 1.19, 2000/10/03 18:03:02 version 1.19.2.6, 2002/03/08 17:04:41
Line 23 
Line 23 
  */   */
   
 #include "includes.h"  #include "includes.h"
   RCSID("$OpenBSD$");
   
   #include "ssh.h"
   #include "ssh1.h"
 #include "packet.h"  #include "packet.h"
 #include "xmalloc.h"  #include "xmalloc.h"
 #include "ssh.h"  #include "log.h"
 #include "servconf.h"  #include "servconf.h"
   #include "uidswap.h"
   #include "auth.h"
   
 RCSID("$OpenBSD$");  #ifdef AFS
   #include "radix.h"
   #endif
   
 #ifdef KRB4  #ifdef KRB4
 char *ticket = NULL;  
   
 extern ServerOptions options;  extern ServerOptions options;
   
   static int
   krb4_init(void *context)
   {
           static int cleanup_registered = 0;
           Authctxt *authctxt = (Authctxt *)context;
           const char *tkt_root = TKT_ROOT;
           struct stat st;
           int fd;
   
           if (!authctxt->krb4_ticket_file) {
                   /* Set unique ticket string manually since we're still root. */
                   authctxt->krb4_ticket_file = xmalloc(MAXPATHLEN);
   #ifdef AFS
                   if (lstat("/ticket", &st) != -1)
                           tkt_root = "/ticket/";
   #endif /* AFS */
                   snprintf(authctxt->krb4_ticket_file, MAXPATHLEN, "%s%u_%d",
                       tkt_root, authctxt->pw->pw_uid, getpid());
                   krb_set_tkt_string(authctxt->krb4_ticket_file);
           }
           /* Register ticket cleanup in case of fatal error. */
           if (!cleanup_registered) {
                   fatal_add_cleanup(krb4_cleanup_proc, authctxt);
                   cleanup_registered = 1;
           }
           /* Try to create our ticket file. */
           if ((fd = mkstemp(authctxt->krb4_ticket_file)) != -1) {
                   close(fd);
                   return (1);
           }
           /* Ticket file exists - make sure user owns it (just passed ticket). */
           if (lstat(authctxt->krb4_ticket_file, &st) != -1) {
                   if (st.st_mode == (S_IFREG | S_IRUSR | S_IWUSR) &&
                       st.st_uid == authctxt->pw->pw_uid)
                           return (1);
           }
           /* Failure - cancel cleanup function, leaving ticket for inspection. */
           log("WARNING: bad ticket file %s", authctxt->krb4_ticket_file);
   
           fatal_remove_cleanup(krb4_cleanup_proc, authctxt);
           cleanup_registered = 0;
   
           xfree(authctxt->krb4_ticket_file);
           authctxt->krb4_ticket_file = NULL;
   
           return (0);
   }
   
 /*  /*
  * try krb4 authentication,   * try krb4 authentication,
  * return 1 on success, 0 on failure, -1 if krb4 is not available   * return 1 on success, 0 on failure, -1 if krb4 is not available
  */   */
   
 int  int
 auth_krb4_password(struct passwd * pw, const char *password)  auth_krb4_password(Authctxt *authctxt, const char *password)
 {  {
         AUTH_DAT adata;          AUTH_DAT adata;
         KTEXT_ST tkt;          KTEXT_ST tkt;
         struct hostent *hp;          struct hostent *hp;
         unsigned long faddr;          struct passwd *pw;
         char localhost[MAXHOSTNAMELEN];          char localhost[MAXHOSTNAMELEN], phost[INST_SZ], realm[REALM_SZ];
         char phost[INST_SZ];          u_int32_t faddr;
         char realm[REALM_SZ];  
         int r;          int r;
   
           if ((pw = authctxt->pw) == NULL)
                   return (0);
   
         /*          /*
          * Try Kerberos password authentication only for non-root           * Try Kerberos password authentication only for non-root
          * users and only if Kerberos is installed.           * users and only if Kerberos is installed.
          */           */
         if (pw->pw_uid != 0 && krb_get_lrealm(realm, 1) == KSUCCESS) {          if (pw->pw_uid != 0 && krb_get_lrealm(realm, 1) == KSUCCESS) {
   
                 /* Set up our ticket file. */                  /* Set up our ticket file. */
                 if (!krb4_init(pw->pw_uid)) {                  if (!krb4_init(authctxt)) {
                         log("Couldn't initialize Kerberos ticket file for %s!",                          log("Couldn't initialize Kerberos ticket file for %s!",
                             pw->pw_name);                              pw->pw_name);
                         goto kerberos_auth_failure;                          goto failure;
                 }                  }
                 /* Try to get TGT using our password. */                  /* Try to get TGT using our password. */
                 r = krb_get_pw_in_tkt((char *) pw->pw_name, "",                  r = krb_get_pw_in_tkt((char *) pw->pw_name, "", realm,
                     realm, "krbtgt", realm,                      "krbtgt", realm, DEFAULT_TKT_LIFE, (char *)password);
                     DEFAULT_TKT_LIFE, (char *) password);  
                 if (r != INTK_OK) {                  if (r != INTK_OK) {
                         packet_send_debug("Kerberos V4 password "                          debug("Kerberos v4 password authentication for %s "
                             "authentication for %s failed: %s",                              "failed: %s", pw->pw_name, krb_err_txt[r]);
                             pw->pw_name, krb_err_txt[r]);                          goto failure;
                         goto kerberos_auth_failure;  
                 }                  }
                 /* Successful authentication. */                  /* Successful authentication. */
                 chown(tkt_string(), pw->pw_uid, pw->pw_gid);                  chown(tkt_string(), pw->pw_uid, pw->pw_gid);
Line 82 
Line 134 
                  * "rcmd" ticket to ensure that we are not talking                   * "rcmd" ticket to ensure that we are not talking
                  * to a bogus Kerberos server.                   * to a bogus Kerberos server.
                  */                   */
                 (void) gethostname(localhost, sizeof(localhost));                  gethostname(localhost, sizeof(localhost));
                 (void) strlcpy(phost, (char *) krb_get_phost(localhost),                  strlcpy(phost, (char *)krb_get_phost(localhost),
                     INST_SZ);                      sizeof(phost));
                 r = krb_mk_req(&tkt, KRB4_SERVICE_NAME, phost, realm, 33);                  r = krb_mk_req(&tkt, KRB4_SERVICE_NAME, phost, realm, 33);
   
                 if (r == KSUCCESS) {                  if (r == KSUCCESS) {
                         if (!(hp = gethostbyname(localhost))) {                          if ((hp = gethostbyname(localhost)) == NULL) {
                                 log("Couldn't get local host address!");                                  log("Couldn't get local host address!");
                                 goto kerberos_auth_failure;                                  goto failure;
                         }                          }
                         memmove((void *) &faddr, (void *) hp->h_addr,                          memmove((void *)&faddr, (void *)hp->h_addr,
                             sizeof(faddr));                              sizeof(faddr));
   
                         /* Verify our "rcmd" ticket. */                          /* Verify our "rcmd" ticket. */
Line 103 
Line 155 
                                  * Probably didn't have a srvtab on                                   * Probably didn't have a srvtab on
                                  * localhost. Disallow login.                                   * localhost. Disallow login.
                                  */                                   */
                                 log("Kerberos V4 TGT for %s unverifiable, "                                  log("Kerberos v4 TGT for %s unverifiable, "
                                     "no srvtab installed? krb_rd_req: %s",                                      "no srvtab installed? krb_rd_req: %s",
                                     pw->pw_name, krb_err_txt[r]);                                      pw->pw_name, krb_err_txt[r]);
                                 goto kerberos_auth_failure;                                  goto failure;
                         } else if (r != KSUCCESS) {                          } else if (r != KSUCCESS) {
                                 log("Kerberos V4 %s ticket unverifiable: %s",                                  log("Kerberos v4 %s ticket unverifiable: %s",
                                     KRB4_SERVICE_NAME, krb_err_txt[r]);                                      KRB4_SERVICE_NAME, krb_err_txt[r]);
                                 goto kerberos_auth_failure;                                  goto failure;
                         }                          }
                 } else if (r == KDC_PR_UNKNOWN) {                  } else if (r == KDC_PR_UNKNOWN) {
                         /*                          /*
                          * Disallow login if no rcmd service exists, and                           * Disallow login if no rcmd service exists, and
                          * log the error.                           * log the error.
                          */                           */
                         log("Kerberos V4 TGT for %s unverifiable: %s; %s.%s "                          log("Kerberos v4 TGT for %s unverifiable: %s; %s.%s "
                             "not registered, or srvtab is wrong?", pw->pw_name,                              "not registered, or srvtab is wrong?", pw->pw_name,
                         krb_err_txt[r], KRB4_SERVICE_NAME, phost);                              krb_err_txt[r], KRB4_SERVICE_NAME, phost);
                         goto kerberos_auth_failure;                          goto failure;
                 } else {                  } else {
                         /*                          /*
                          * TGT is bad, forget it. Possibly spoofed!                           * TGT is bad, forget it. Possibly spoofed!
                          */                           */
                         packet_send_debug("WARNING: Kerberos V4 TGT "                          debug("WARNING: Kerberos v4 TGT possibly spoofed "
                             "possibly spoofed for %s: %s",                              "for %s: %s", pw->pw_name, krb_err_txt[r]);
                             pw->pw_name, krb_err_txt[r]);                          goto failure;
                         goto kerberos_auth_failure;  
                 }                  }
   
                 /* Authentication succeeded. */                  /* Authentication succeeded. */
                 return 1;                  return (1);
           } else
                   /* Logging in as root or no local Kerberos realm. */
                   debug("Unable to authenticate to Kerberos.");
   
 kerberos_auth_failure:   failure:
                 krb4_cleanup_proc(NULL);          krb4_cleanup_proc(authctxt);
   
                 if (!options.kerberos_or_local_passwd)          if (!options.kerberos_or_local_passwd)
                         return 0;                  return (0);
         } else {  
                 /* Logging in as root or no local Kerberos realm. */  
                 packet_send_debug("Unable to authenticate to Kerberos.");  
         }  
         /* Fall back to ordinary passwd authentication. */          /* Fall back to ordinary passwd authentication. */
         return -1;          return (-1);
 }  }
   
 void  void
 krb4_cleanup_proc(void *ignore)  krb4_cleanup_proc(void *context)
 {  {
           Authctxt *authctxt = (Authctxt *)context;
         debug("krb4_cleanup_proc called");          debug("krb4_cleanup_proc called");
         if (ticket) {          if (authctxt->krb4_ticket_file) {
                 (void) dest_tkt();                  (void) dest_tkt();
                 xfree(ticket);                  xfree(authctxt->krb4_ticket_file);
                 ticket = NULL;                  authctxt->krb4_ticket_file = NULL;
         }          }
 }  }
   
 int  int
 krb4_init(uid_t uid)  auth_krb4(Authctxt *authctxt, KTEXT auth, char **client)
 {  {
         static int cleanup_registered = 0;  
         const char *tkt_root = TKT_ROOT;  
         struct stat st;  
         int fd;  
   
         if (!ticket) {  
                 /* Set unique ticket string manually since we're still root. */  
                 ticket = xmalloc(MAXPATHLEN);  
 #ifdef AFS  
                 if (lstat("/ticket", &st) != -1)  
                         tkt_root = "/ticket/";  
 #endif /* AFS */  
                 snprintf(ticket, MAXPATHLEN, "%s%u_%d", tkt_root, uid, getpid());  
                 (void) krb_set_tkt_string(ticket);  
         }  
         /* Register ticket cleanup in case of fatal error. */  
         if (!cleanup_registered) {  
                 fatal_add_cleanup(krb4_cleanup_proc, NULL);  
                 cleanup_registered = 1;  
         }  
         /* Try to create our ticket file. */  
         if ((fd = mkstemp(ticket)) != -1) {  
                 close(fd);  
                 return 1;  
         }  
         /* Ticket file exists - make sure user owns it (just passed ticket). */  
         if (lstat(ticket, &st) != -1) {  
                 if (st.st_mode == (S_IFREG | S_IRUSR | S_IWUSR) &&  
                     st.st_uid == uid)  
                         return 1;  
         }  
         /* Failure - cancel cleanup function, leaving bad ticket for inspection. */  
         log("WARNING: bad ticket file %s", ticket);  
         fatal_remove_cleanup(krb4_cleanup_proc, NULL);  
         cleanup_registered = 0;  
         xfree(ticket);  
         ticket = NULL;  
   
         return 0;  
 }  
   
 int  
 auth_krb4(const char *server_user, KTEXT auth, char **client)  
 {  
         AUTH_DAT adat = {0};          AUTH_DAT adat = {0};
         KTEXT_ST reply;          KTEXT_ST reply;
           Key_schedule schedule;
           struct sockaddr_in local, foreign;
         char instance[INST_SZ];          char instance[INST_SZ];
         int r, s;  
         socklen_t slen;          socklen_t slen;
         u_int cksum;          u_int cksum;
         Key_schedule schedule;          int r, s;
         struct sockaddr_in local, foreign;  
   
         s = packet_get_connection_in();          s = packet_get_connection_in();
   
Line 230 
Line 237 
         instance[1] = 0;          instance[1] = 0;
   
         /* Get the encrypted request, challenge, and session key. */          /* Get the encrypted request, challenge, and session key. */
         if ((r = krb_rd_req(auth, KRB4_SERVICE_NAME, instance, 0, &adat, ""))) {          if ((r = krb_rd_req(auth, KRB4_SERVICE_NAME, instance,
                 packet_send_debug("Kerberos V4 krb_rd_req: %.100s", krb_err_txt[r]);              0, &adat, ""))) {
                 return 0;                  debug("Kerberos v4 krb_rd_req: %.100s", krb_err_txt[r]);
                   return (0);
         }          }
         des_key_sched((des_cblock *) adat.session, schedule);          des_key_sched((des_cblock *) adat.session, schedule);
   
Line 241 
Line 249 
             *adat.pinst ? "." : "", adat.pinst, adat.prealm);              *adat.pinst ? "." : "", adat.pinst, adat.prealm);
   
         /* Check ~/.klogin authorization now. */          /* Check ~/.klogin authorization now. */
         if (kuserok(&adat, (char *) server_user) != KSUCCESS) {          if (kuserok(&adat, authctxt->user) != KSUCCESS) {
                 packet_send_debug("Kerberos V4 .klogin authorization failed!");                  log("Kerberos v4 .klogin authorization failed for %s to "
                 log("Kerberos V4 .klogin authorization failed for %s to account %s",                      "account %s", *client, authctxt->user);
                     *client, server_user);  
                 xfree(*client);                  xfree(*client);
                 return 0;                  return (0);
         }          }
         /* Increment the checksum, and return it encrypted with the          /* Increment the checksum, and return it encrypted with the
            session key. */             session key. */
Line 257 
Line 264 
            empty message, admitting our failure. */             empty message, admitting our failure. */
         if ((r = krb_mk_priv((u_char *) & cksum, reply.dat, sizeof(cksum) + 1,          if ((r = krb_mk_priv((u_char *) & cksum, reply.dat, sizeof(cksum) + 1,
             schedule, &adat.session, &local, &foreign)) < 0) {              schedule, &adat.session, &local, &foreign)) < 0) {
                 packet_send_debug("Kerberos V4 mk_priv: (%d) %s", r, krb_err_txt[r]);                  debug("Kerberos v4 mk_priv: (%d) %s", r, krb_err_txt[r]);
                 reply.dat[0] = 0;                  reply.dat[0] = 0;
                 reply.length = 0;                  reply.length = 0;
         } else          } else
Line 270 
Line 277 
         packet_put_string((char *) reply.dat, reply.length);          packet_put_string((char *) reply.dat, reply.length);
         packet_send();          packet_send();
         packet_write_wait();          packet_write_wait();
         return 1;          return (1);
 }  }
 #endif /* KRB4 */  #endif /* KRB4 */
   
 #ifdef AFS  #ifdef AFS
 int  int
 auth_kerberos_tgt(struct passwd *pw, const char *string)  auth_krb4_tgt(Authctxt *authctxt, const char *string)
 {  {
         CREDENTIALS creds;          CREDENTIALS creds;
           struct passwd *pw;
   
         if (pw == NULL)          if ((pw = authctxt->pw) == NULL)
                 goto auth_kerberos_tgt_failure;                  goto failure;
   
           temporarily_use_uid(pw);
   
         if (!radix_to_creds(string, &creds)) {          if (!radix_to_creds(string, &creds)) {
                 log("Protocol error decoding Kerberos V4 tgt");                  log("Protocol error decoding Kerberos v4 TGT");
                 packet_send_debug("Protocol error decoding Kerberos V4 tgt");                  goto failure;
                 goto auth_kerberos_tgt_failure;  
         }          }
         if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */          if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */
                 strlcpy(creds.service, "krbtgt", sizeof creds.service);                  strlcpy(creds.service, "krbtgt", sizeof creds.service);
   
         if (strcmp(creds.service, "krbtgt")) {          if (strcmp(creds.service, "krbtgt")) {
                 log("Kerberos V4 tgt (%s%s%s@%s) rejected for %s", creds.pname,                  log("Kerberos v4 TGT (%s%s%s@%s) rejected for %s",
                     creds.pinst[0] ? "." : "", creds.pinst, creds.realm,  
                     pw->pw_name);  
                 packet_send_debug("Kerberos V4 tgt (%s%s%s@%s) rejected for %s",  
                     creds.pname, creds.pinst[0] ? "." : "", creds.pinst,                      creds.pname, creds.pinst[0] ? "." : "", creds.pinst,
                     creds.realm, pw->pw_name);                      creds.realm, pw->pw_name);
                 goto auth_kerberos_tgt_failure;                  goto failure;
         }          }
         if (!krb4_init(pw->pw_uid))          if (!krb4_init(authctxt))
                 goto auth_kerberos_tgt_failure;                  goto failure;
   
         if (in_tkt(creds.pname, creds.pinst) != KSUCCESS)          if (in_tkt(creds.pname, creds.pinst) != KSUCCESS)
                 goto auth_kerberos_tgt_failure;                  goto failure;
   
         if (save_credentials(creds.service, creds.instance, creds.realm,          if (save_credentials(creds.service, creds.instance, creds.realm,
             creds.session, creds.lifetime, creds.kvno,              creds.session, creds.lifetime, creds.kvno, &creds.ticket_st,
             &creds.ticket_st, creds.issue_date) != KSUCCESS) {              creds.issue_date) != KSUCCESS) {
                 packet_send_debug("Kerberos V4 tgt refused: couldn't save credentials");                  debug("Kerberos v4 TGT refused: couldn't save credentials");
                 goto auth_kerberos_tgt_failure;                  goto failure;
         }          }
         /* Successful authentication, passed all checks. */          /* Successful authentication, passed all checks. */
         chown(tkt_string(), pw->pw_uid, pw->pw_gid);          chown(tkt_string(), pw->pw_uid, pw->pw_gid);
   
         packet_send_debug("Kerberos V4 tgt accepted (%s.%s@%s, %s%s%s@%s)",          debug("Kerberos v4 TGT accepted (%s%s%s@%s)",
             creds.service, creds.instance, creds.realm, creds.pname,              creds.pname, creds.pinst[0] ? "." : "", creds.pinst, creds.realm);
             creds.pinst[0] ? "." : "", creds.pinst, creds.realm);  
         memset(&creds, 0, sizeof(creds));          memset(&creds, 0, sizeof(creds));
         packet_start(SSH_SMSG_SUCCESS);  
         packet_send();  
         packet_write_wait();  
         return 1;  
   
 auth_kerberos_tgt_failure:          restore_uid();
         krb4_cleanup_proc(NULL);  
           return (1);
   
    failure:
           krb4_cleanup_proc(authctxt);
         memset(&creds, 0, sizeof(creds));          memset(&creds, 0, sizeof(creds));
         packet_start(SSH_SMSG_FAILURE);          restore_uid();
         packet_send();  
         packet_write_wait();          return (0);
         return 0;  
 }  }
   
 int  int
 auth_afs_token(struct passwd *pw, const char *token_string)  auth_afs_token(Authctxt *authctxt, const char *token_string)
 {  {
         CREDENTIALS creds;          CREDENTIALS creds;
           struct passwd *pw;
         uid_t uid;          uid_t uid;
   
         if (pw == NULL) {          if ((pw = authctxt->pw) == NULL)
                 /* XXX fake protocol error */                  return (0);
                 packet_send_debug("Protocol error decoding AFS token");  
                 packet_start(SSH_SMSG_FAILURE);  
                 packet_send();  
                 packet_write_wait();  
                 return 0;  
         }  
         if (!radix_to_creds(token_string, &creds)) {          if (!radix_to_creds(token_string, &creds)) {
                 log("Protocol error decoding AFS token");                  log("Protocol error decoding AFS token");
                 packet_send_debug("Protocol error decoding AFS token");                  return (0);
                 packet_start(SSH_SMSG_FAILURE);  
                 packet_send();  
                 packet_write_wait();  
                 return 0;  
         }          }
         if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */          if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */
                 strlcpy(creds.service, "afs", sizeof creds.service);                  strlcpy(creds.service, "afs", sizeof creds.service);
Line 363 
Line 360 
                 uid = pw->pw_uid;                  uid = pw->pw_uid;
   
         if (kafs_settoken(creds.realm, uid, &creds)) {          if (kafs_settoken(creds.realm, uid, &creds)) {
                 log("AFS token (%s@%s) rejected for %s", creds.pname, creds.realm,                  log("AFS token (%s@%s) rejected for %s",
                     pw->pw_name);                      creds.pname, creds.realm, pw->pw_name);
                 packet_send_debug("AFS token (%s@%s) rejected for %s", creds.pname,  
                     creds.realm, pw->pw_name);  
                 memset(&creds, 0, sizeof(creds));                  memset(&creds, 0, sizeof(creds));
                 packet_start(SSH_SMSG_FAILURE);                  return (0);
                 packet_send();  
                 packet_write_wait();  
                 return 0;  
         }          }
         packet_send_debug("AFS token accepted (%s@%s, %s@%s)", creds.service,          debug("AFS token accepted (%s@%s)", creds.pname, creds.realm);
             creds.realm, creds.pname, creds.realm);  
         memset(&creds, 0, sizeof(creds));          memset(&creds, 0, sizeof(creds));
         packet_start(SSH_SMSG_SUCCESS);  
         packet_send();          return (1);
         packet_write_wait();  
         return 1;  
 }  }
 #endif /* AFS */  #endif /* AFS */

Legend:
Removed from v.1.19  
changed lines
  Added in v.1.19.2.6