[comp.protocols.kerberos] lifetime>0x7f bug fixes & enhancements

jm36+@ANDREW.CMU.EDU (John Gardiner Myers) (01/31/91)

The following patches fix several sign-extension bugs with the
lifetime field and implement the "extended lifetime" table agreed upon
at the "meeting at Apollo in Boston in November 88".

Thanks to Ted Anderson of Transarc for supplying me with the lifetime
table.

These fixes concentrate mostly on the client libraries.  I made the
obvious fixes to the Kerberos server, but since we don't run an MIT
kerberos server here, I haven't been able to test them.

diff -cr ./kuser/klist.c /afs/andrew.cmu.edu/system/src/local/kerberos/kuser/klist.c
*** ./kuser/klist.c	Mon Oct  2 16:37:54 1989
--- /afs/andrew.cmu.edu/system/src/local/kerberos/kuser/klist.c	Wed Jan 30 13:16:10 1991
***************
*** 29,34 ****
--- 29,35 ----
  char   *getenv();
  
  extern char *krb_err_txt[];
+ extern unsigned long krb_life_to_time();
  
  /* ARGSUSED */
  main(argc, argv)
***************
*** 163,169 ****
  	    header = 0;
  	}
  	if (tgt_test) {
! 	    c.issue_date += ((unsigned char) c.lifetime) * 5 * 60;
  	    if (!strcmp(c.service, TICKET_GRANTING_TICKET) &&
  		!strcmp(c.instance, prealm)) {
  		if (time(0) < c.issue_date)
--- 164,170 ----
  	    header = 0;
  	}
  	if (tgt_test) {
! 	    c.issue_date = krb_life_to_time(c.issue_date, c.lifetime);
  	    if (!strcmp(c.service, TICKET_GRANTING_TICKET) &&
  		!strcmp(c.instance, prealm)) {
  		if (time(0) < c.issue_date)
***************
*** 175,181 ****
  	}
  	if (long_form) {
  	    (void) strcpy(buf1, short_date(&c.issue_date));
! 	    c.issue_date += ((unsigned char) c.lifetime) * 5 * 60;
  	    (void) strcpy(buf2, short_date(&c.issue_date));
  	    printf("%s  %s  ", buf1, buf2);
  	}
--- 176,182 ----
  	}
  	if (long_form) {
  	    (void) strcpy(buf1, short_date(&c.issue_date));
! 	    c.issue_date = krb_life_to_time(c.issue_date, c.lifetime);
  	    (void) strcpy(buf2, short_date(&c.issue_date));
  	    printf("%s  %s  ", buf1, buf2);
  	}
***************
*** 192,201 ****
  
  char   *
  short_date(dp)
!     long   *dp;
  {
      register char *cp;
      extern char *ctime();
      cp = ctime(dp) + 4;
      cp[15] = '\0';
      return (cp);
--- 193,203 ----
  
  char   *
  short_date(dp)
!     unsigned long   *dp;
  {
      register char *cp;
      extern char *ctime();
+     if (*dp == (unsigned long)(-1L)) return "Never         ";
      cp = ctime(dp) + 4;
      cp[15] = '\0';
      return (cp);
diff -cr ./lib/krb/Imakefile /afs/andrew.cmu.edu/system/src/local/kerberos/lib/krb/Imakefile
*** ./lib/krb/Imakefile	Thu Jan  4 11:56:54 1990
--- /afs/andrew.cmu.edu/system/src/local/kerberos/lib/krb/Imakefile	Fri Jan 25 17:56:58 1991
***************
*** 78,83 ****
--- 78,84 ----
  	krb_err_txt.c \
  	krb_get_in_tkt.c \
  	kuserok.c \
+ 	lifetime.c \
  	log.c \
  	mk_req.c \
  	mk_err.c \
***************
*** 138,143 ****
--- 139,145 ----
  	krb_err_txt.o \
  	krb_get_in_tkt.o \
  	kuserok.o \
+ 	lifetime.o \
  	log.o \
  	mk_req.o \
  	mk_err.o \
diff -cr ./lib/krb/extract_ticket.c /afs/andrew.cmu.edu/system/src/local/kerberos/lib/krb/extract_ticket.c
*** ./lib/krb/extract_ticket.c	Fri Oct  7 06:08:36 1988
--- /afs/andrew.cmu.edu/system/src/local/kerberos/lib/krb/extract_ticket.c	Fri Jan 25 17:34:21 1991
***************
*** 48,54 ****
  	ptr = ptr + 11 + strlen(ptr+10) + (int) *(cipher->dat+i);
      bcopy(ptr, (char *) session, 8); /* Save the session key */
      ptr += 8;
!     *lifetime = *(ptr++);	/* Save the life of the ticket */
      *kvno = *(ptr++);		/* Save the kvno */
      (void) strcpy(realm,ptr);	/* instance */
      ptr += strlen(realm) + 1;
--- 48,54 ----
  	ptr = ptr + 11 + strlen(ptr+10) + (int) *(cipher->dat+i);
      bcopy(ptr, (char *) session, 8); /* Save the session key */
      ptr += 8;
!     *lifetime = (unsigned char) *(ptr++); /* Save the life of the ticket */
      *kvno = *(ptr++);		/* Save the kvno */
      (void) strcpy(realm,ptr);	/* instance */
      ptr += strlen(realm) + 1;
diff -cr ./lib/krb/get_ad_tkt.c /afs/andrew.cmu.edu/system/src/local/kerberos/lib/krb/get_ad_tkt.c
*** ./lib/krb/get_ad_tkt.c	Mon Oct  2 15:41:28 1989
--- /afs/andrew.cmu.edu/system/src/local/kerberos/lib/krb/get_ad_tkt.c	Fri Jan 25 17:35:20 1991
***************
*** 208,214 ****
      (void) strcpy(rlm,ptr);
      ptr += strlen(rlm) + 1;
  
!     lifetime = (unsigned long) ptr[0];
      kvno = (unsigned long) ptr[1];
      tkt->length = (int) ptr[2];
      ptr += 3;
--- 208,214 ----
      (void) strcpy(rlm,ptr);
      ptr += strlen(rlm) + 1;
  
!     lifetime = (unsigned char) ptr[0];
      kvno = (unsigned long) ptr[1];
      tkt->length = (int) ptr[2];
      ptr += 3;
diff -cr lib/krb/lifetime.c /afs/andrew.cmu.edu/system/src/local/kerberos/lib/krb/lifetime.c
*** lib/krb/lifetime.c	Wed Jan 30 13:51:00 1991
--- /afs/andrew.cmu.edu/system/src/local/kerberos/lib/krb/lifetime.c	Wed Jan 30 13:22:15 1991
***************
*** 0 ****
--- 1,142 ----
+ /*
+  * Ticket lifetime.  This defines the table used to lookup lifetime
+  * for the fixed part of rande of the one byte lifetime field.  Values
+  * less than 0x80 are intrpreted as the number of 5 minute intervals.
+  * Values from 0x80 to 0xBF should be looked up in this table.  The
+  * value of 0x80 is the same using both methods: 10 and two-thirds
+  * hours .  The lifetime of 0xBF is 30 days.  The intervening values
+  * of have a fixed ratio of roughly 1.06914.  The value 0xFF is
+  * defined to mean a ticket has no expiration time.  This should be
+  * used advisedly since individual servers may impose defacto
+  * upperbounds on ticket lifetimes.
+  */
+ 
+ #define TKTLIFENUMFIXED 64
+ #define TKTLIFEMINFIXED 0x80
+ #define TKTLIFEMAXFIXED 0xBF
+ #define TKTLIFENOEXPIRE 0xFF
+ #define MAXTKTLIFETIME	(30*24*3600)	/* 30 days */
+ #ifndef NEVERDATE
+ #define NEVERDATE ((unsigned long)-1L)
+ #endif
+ 
+ static int tkt_lifetimes[TKTLIFENUMFIXED] = {
+     38400,				/* 10.67 hours, 0.44 days */ 
+     41055,				/* 11.40 hours, 0.48 days */ 
+     43894,				/* 12.19 hours, 0.51 days */ 
+     46929,				/* 13.04 hours, 0.54 days */ 
+     50174,				/* 13.94 hours, 0.58 days */ 
+     53643,				/* 14.90 hours, 0.62 days */ 
+     57352,				/* 15.93 hours, 0.66 days */ 
+     61318,				/* 17.03 hours, 0.71 days */ 
+     65558,				/* 18.21 hours, 0.76 days */ 
+     70091,				/* 19.47 hours, 0.81 days */ 
+     74937,				/* 20.82 hours, 0.87 days */ 
+     80119,				/* 22.26 hours, 0.93 days */ 
+     85658,				/* 23.79 hours, 0.99 days */ 
+     91581,				/* 25.44 hours, 1.06 days */ 
+     97914,				/* 27.20 hours, 1.13 days */ 
+     104684,				/* 29.08 hours, 1.21 days */ 
+     111922,				/* 31.09 hours, 1.30 days */ 
+     119661,				/* 33.24 hours, 1.38 days */ 
+     127935,				/* 35.54 hours, 1.48 days */ 
+     136781,				/* 37.99 hours, 1.58 days */ 
+     146239,				/* 40.62 hours, 1.69 days */ 
+     156350,				/* 43.43 hours, 1.81 days */ 
+     167161,				/* 46.43 hours, 1.93 days */ 
+     178720,				/* 49.64 hours, 2.07 days */ 
+     191077,				/* 53.08 hours, 2.21 days */ 
+     204289,				/* 56.75 hours, 2.36 days */ 
+     218415,				/* 60.67 hours, 2.53 days */ 
+     233517,				/* 64.87 hours, 2.70 days */ 
+     249664,				/* 69.35 hours, 2.89 days */ 
+     266926,				/* 74.15 hours, 3.09 days */ 
+     285383,				/* 79.27 hours, 3.30 days */ 
+     305116,				/* 84.75 hours, 3.53 days */ 
+     326213,				/* 90.61 hours, 3.78 days */ 
+     348769,				/* 96.88 hours, 4.04 days */ 
+     372885,				/* 103.58 hours, 4.32 days */ 
+     398668,				/* 110.74 hours, 4.61 days */ 
+     426234,				/* 118.40 hours, 4.93 days */ 
+     455705,				/* 126.58 hours, 5.27 days */ 
+     487215,				/* 135.34 hours, 5.64 days */ 
+     520904,				/* 144.70 hours, 6.03 days */ 
+     556921,				/* 154.70 hours, 6.45 days */ 
+     595430,				/* 165.40 hours, 6.89 days */ 
+     636601,				/* 176.83 hours, 7.37 days */ 
+     680618,				/* 189.06 hours, 7.88 days */ 
+     727680,				/* 202.13 hours, 8.42 days */ 
+     777995,				/* 216.11 hours, 9.00 days */ 
+     831789,				/* 231.05 hours, 9.63 days */ 
+     889303,				/* 247.03 hours, 10.29 days */ 
+     950794,				/* 264.11 hours, 11.00 days */ 
+     1016537,				/* 282.37 hours, 11.77 days */ 
+     1086825,				/* 301.90 hours, 12.58 days */ 
+     1161973,				/* 322.77 hours, 13.45 days */ 
+     1242318,				/* 345.09 hours, 14.38 days */ 
+     1328218,				/* 368.95 hours, 15.37 days */ 
+     1420057,				/* 394.46 hours, 16.44 days */ 
+     1518247,				/* 421.74 hours, 17.57 days */ 
+     1623226,				/* 450.90 hours, 18.79 days */ 
+     1735464,				/* 482.07 hours, 20.09 days */ 
+     1855462,				/* 515.41 hours, 21.48 days */ 
+     1983758,				/* 551.04 hours, 22.96 days */ 
+     2120925,				/* 589.15 hours, 24.55 days */ 
+     2267576,				/* 629.88 hours, 26.25 days */ 
+     2424367,				/* 673.44 hours, 28.06 days */ 
+     2592000};				/* 720.00 hours, 30.00 days */ 
+ 
+ /*
+  * krb_life_to_time - takes a start time and a Kerberos standard
+  * lifetime char and returns the corresponding end time.  There are
+  * four simple cases to be handled.  The first is a life of 0xff,
+  * meaning no expiration, and results in an end time of 0xffffffff.
+  * The second is when life is less than the values covered by the
+  * table.  In this case, the end time is the start time plus the
+  * number of 5 minute intervals specified by life.  The third case
+  * returns start plus the MAXTKTLIFETIME if life is greater than
+  * TKTLIFEMAXFIXED.  The last case, uses the life value (minus
+  * TKTLIFEMINFIXED) as an index into the table to extract the lifetime
+  * in seconds, which is added to start to produce the end time.
+  */
+ unsigned long krb_life_to_time(start, life)
+ unsigned long start;
+ int life;
+ {
+     life = (unsigned char) life;
+     if (life == TKTLIFENOEXPIRE) return NEVERDATE;
+     if (life < TKTLIFEMINFIXED) return start + life*5*60;
+     if (life > TKTLIFEMAXFIXED) return start + MAXTKTLIFETIME;
+     return start + tkt_lifetimes[life - TKTLIFEMINFIXED];
+ }
+ 
+ /*
+  * krb_time_to_life - takes start and end times for the ticket and
+  * returns a Kerberos standard lifetime char, possibily using the
+  * tkt_lifetimes table for lifetimes above 127*5 minutes.  First, the
+  * special case of (end == NEVERDATE) is handled to mean no
+  * expiration.  Then negative lifetimes and those greater than the
+  * maximum ticket lifetime are rejected.  Then lifetimes less than the
+  * first table entry are handled by rounding the requested lifetime
+  * *up* to the next 5 minute interval.  The final step is to search
+  * the table for the smallest entry *greater than or equal* to the
+  * requested entry.
+  */
+ int krb_time_to_life(start, end)
+ unsigned long start;
+ unsigned long end;
+ {
+     long lifetime;
+     int i;
+ 
+     if (end == NEVERDATE) return TKTLIFENOEXPIRE;
+     lifetime = end - start;
+     if (lifetime > MAXTKTLIFETIME || lifetime <= 0) return 0;
+     if (lifetime < tkt_lifetimes[0]) return (lifetime + 5*60 - 1)/(5*60);
+     for (i=0; i<TKTLIFENUMFIXED; i++) {
+ 	if (lifetime <= tkt_lifetimes[i]) {
+ 	    return i+TKTLIFEMINFIXED;
+ 	}
+     }
+     return 0;
+ }
diff -cr ./lib/krb/rd_req.c /afs/andrew.cmu.edu/system/src/local/kerberos/lib/krb/rd_req.c
*** ./lib/krb/rd_req.c	Fri Jun  2 13:24:01 1989
--- /afs/andrew.cmu.edu/system/src/local/kerberos/lib/krb/rd_req.c	Wed Jan 30 13:11:15 1991
***************
*** 22,27 ****
--- 22,28 ----
  #include <strings.h>
  
  extern int krb_ap_req_debug;
+ extern unsigned long krb_life_to_time();
  
  static struct timeval t_local = { 0, 0 };
  
***************
*** 325,331 ****
          if ((ad->time_sec - t_local.tv_sec) > CLOCK_SKEW)
              return(RD_AP_NYV);
      }
!     else if ((t_local.tv_sec - ad->time_sec) > 5 * 60 * ad->life)
          return(RD_AP_EXP);
  
      /* All seems OK */
--- 326,332 ----
          if ((ad->time_sec - t_local.tv_sec) > CLOCK_SKEW)
              return(RD_AP_NYV);
      }
!     else if (t_local.tv_sec > krb_life_to_time(ad->time_sec, ad->life))
          return(RD_AP_EXP);
  
      /* All seems OK */
diff -cr ./server/kerberos.c /afs/andrew.cmu.edu/system/src/local/kerberos/server/kerberos.c
*** ./server/kerberos.c	Thu Jan  4 11:56:33 1990
--- /afs/andrew.cmu.edu/system/src/local/kerberos/server/kerberos.c	Wed Jan 30 13:40:03 1991
***************
*** 401,407 ****
  	    }
  	    ptr = (char *) pkt_time_ws(pkt) + 4;
  
! 	    req_life = (u_long) (*ptr++);
  
  	    service = ptr;
  	    instance = ptr + strlen(service) + 1;
--- 401,407 ----
  	    }
  	    ptr = (char *) pkt_time_ws(pkt) + 4;
  
! 	    req_life = (unsigned char) (*ptr++);
  
  	    service = ptr;
  	    instance = ptr + strlen(service) + 1;
***************
*** 524,530 ****
  	    bcopy(ptr, &time_ws, 4);
  	    ptr += 4;
  
! 	    req_life = (u_long) (*ptr++);
  
  	    service = ptr;
  	    instance = ptr + strlen(service) + 1;
--- 524,530 ----
  	    bcopy(ptr, &time_ws, 4);
  	    ptr += 4;
  
! 	    req_life = (unsigned char) (*ptr++);
  
  	    service = ptr;
  	    instance = ptr + strlen(service) + 1;

-- 
_.John G. Myers		Internet: John.G.Myers@andrew.cmu.edu
(412) 268-2984		LoseNet:  ...!seismo!ihnp4!wiscvm.wisc.edu!give!up