Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * radix.c |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 3 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Dug Song <dugsong@UMICH.EDU> |
| 5 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 6 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 7 | #include "includes.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 8 | #include "uuencode.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 9 | |
Damien Miller | bf7f466 | 2000-06-23 10:16:38 +1000 | [diff] [blame] | 10 | RCSID("$OpenBSD: radix.c,v 1.12 2000/06/22 23:55:00 djm Exp $"); |
| 11 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 12 | #ifdef AFS |
| 13 | #include <krb.h> |
| 14 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 15 | typedef unsigned char my_u_char; |
| 16 | typedef unsigned int my_u_int32_t; |
| 17 | typedef unsigned short my_u_short; |
| 18 | |
| 19 | /* Nasty macros from BIND-4.9.2 */ |
| 20 | |
| 21 | #define GETSHORT(s, cp) { \ |
| 22 | register my_u_char *t_cp = (my_u_char*)(cp); \ |
| 23 | (s) = (((my_u_short)t_cp[0]) << 8) \ |
| 24 | | (((my_u_short)t_cp[1])) \ |
| 25 | ; \ |
| 26 | (cp) += 2; \ |
| 27 | } |
| 28 | |
| 29 | #define GETLONG(l, cp) { \ |
| 30 | register my_u_char *t_cp = (my_u_char*)(cp); \ |
| 31 | (l) = (((my_u_int32_t)t_cp[0]) << 24) \ |
| 32 | | (((my_u_int32_t)t_cp[1]) << 16) \ |
| 33 | | (((my_u_int32_t)t_cp[2]) << 8) \ |
| 34 | | (((my_u_int32_t)t_cp[3])) \ |
| 35 | ; \ |
| 36 | (cp) += 4; \ |
| 37 | } |
| 38 | |
| 39 | #define PUTSHORT(s, cp) { \ |
| 40 | register my_u_short t_s = (my_u_short)(s); \ |
| 41 | register my_u_char *t_cp = (my_u_char*)(cp); \ |
| 42 | *t_cp++ = t_s >> 8; \ |
| 43 | *t_cp = t_s; \ |
| 44 | (cp) += 2; \ |
| 45 | } |
| 46 | |
| 47 | #define PUTLONG(l, cp) { \ |
| 48 | register my_u_int32_t t_l = (my_u_int32_t)(l); \ |
| 49 | register my_u_char *t_cp = (my_u_char*)(cp); \ |
| 50 | *t_cp++ = t_l >> 24; \ |
| 51 | *t_cp++ = t_l >> 16; \ |
| 52 | *t_cp++ = t_l >> 8; \ |
| 53 | *t_cp = t_l; \ |
| 54 | (cp) += 4; \ |
| 55 | } |
| 56 | |
| 57 | #define GETSTRING(s, p, p_l) { \ |
| 58 | register char* p_targ = (p) + p_l; \ |
| 59 | register char* s_c = (s); \ |
| 60 | register char* p_c = (p); \ |
| 61 | while (*p_c && (p_c < p_targ)) { \ |
| 62 | *s_c++ = *p_c++; \ |
| 63 | } \ |
| 64 | if (p_c == p_targ) { \ |
| 65 | return 1; \ |
| 66 | } \ |
| 67 | *s_c = *p_c++; \ |
| 68 | (p_l) = (p_l) - (p_c - (p)); \ |
| 69 | (p) = p_c; \ |
| 70 | } |
| 71 | |
| 72 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 73 | int |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 74 | creds_to_radix(CREDENTIALS *creds, unsigned char *buf, size_t buflen) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 75 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 76 | char *p, *s; |
| 77 | int len; |
| 78 | char temp[2048]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 79 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 80 | p = temp; |
| 81 | *p++ = 1; /* version */ |
| 82 | s = creds->service; |
| 83 | while (*s) |
| 84 | *p++ = *s++; |
| 85 | *p++ = *s; |
| 86 | s = creds->instance; |
| 87 | while (*s) |
| 88 | *p++ = *s++; |
| 89 | *p++ = *s; |
| 90 | s = creds->realm; |
| 91 | while (*s) |
| 92 | *p++ = *s++; |
| 93 | *p++ = *s; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 94 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 95 | s = creds->pname; |
| 96 | while (*s) |
| 97 | *p++ = *s++; |
| 98 | *p++ = *s; |
| 99 | s = creds->pinst; |
| 100 | while (*s) |
| 101 | *p++ = *s++; |
| 102 | *p++ = *s; |
| 103 | /* Null string to repeat the realm. */ |
| 104 | *p++ = '\0'; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 105 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 106 | PUTLONG(creds->issue_date, p); |
| 107 | { |
| 108 | unsigned int endTime; |
| 109 | endTime = (unsigned int) krb_life_to_time(creds->issue_date, |
| 110 | creds->lifetime); |
| 111 | PUTLONG(endTime, p); |
| 112 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 113 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 114 | memcpy(p, &creds->session, sizeof(creds->session)); |
| 115 | p += sizeof(creds->session); |
| 116 | |
| 117 | PUTSHORT(creds->kvno, p); |
| 118 | PUTLONG(creds->ticket_st.length, p); |
| 119 | |
| 120 | memcpy(p, creds->ticket_st.dat, creds->ticket_st.length); |
| 121 | p += creds->ticket_st.length; |
| 122 | len = p - temp; |
| 123 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 124 | return (uuencode((unsigned char *)temp, len, (char *)buf, buflen)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 125 | } |
| 126 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 127 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 128 | radix_to_creds(const char *buf, CREDENTIALS *creds) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 129 | { |
| 130 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 131 | char *p; |
| 132 | int len, tl; |
| 133 | char version; |
| 134 | char temp[2048]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 135 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 136 | len = uudecode(buf, (unsigned char *)temp, sizeof(temp)); |
| 137 | if (len < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 138 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 139 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 140 | p = temp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 141 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 142 | /* check version and length! */ |
| 143 | if (len < 1) |
| 144 | return 0; |
| 145 | version = *p; |
| 146 | p++; |
| 147 | len--; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 148 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 149 | GETSTRING(creds->service, p, len); |
| 150 | GETSTRING(creds->instance, p, len); |
| 151 | GETSTRING(creds->realm, p, len); |
| 152 | |
| 153 | GETSTRING(creds->pname, p, len); |
| 154 | GETSTRING(creds->pinst, p, len); |
| 155 | /* Ignore possibly different realm. */ |
| 156 | while (*p && len) |
| 157 | p++, len--; |
| 158 | if (len == 0) |
| 159 | return 0; |
| 160 | p++, len--; |
| 161 | |
| 162 | /* Enough space for remaining fixed-length parts? */ |
| 163 | if (len < (4 + 4 + sizeof(creds->session) + 2 + 4)) |
| 164 | return 0; |
| 165 | |
| 166 | GETLONG(creds->issue_date, p); |
| 167 | len -= 4; |
| 168 | { |
| 169 | unsigned int endTime; |
| 170 | GETLONG(endTime, p); |
| 171 | len -= 4; |
| 172 | creds->lifetime = krb_time_to_life(creds->issue_date, endTime); |
| 173 | } |
| 174 | |
| 175 | memcpy(&creds->session, p, sizeof(creds->session)); |
| 176 | p += sizeof(creds->session); |
| 177 | len -= sizeof(creds->session); |
| 178 | |
| 179 | GETSHORT(creds->kvno, p); |
| 180 | len -= 2; |
| 181 | GETLONG(creds->ticket_st.length, p); |
| 182 | len -= 4; |
| 183 | |
| 184 | tl = creds->ticket_st.length; |
| 185 | if (tl < 0 || tl > len || tl > sizeof(creds->ticket_st.dat)) |
| 186 | return 0; |
| 187 | |
| 188 | memcpy(creds->ticket_st.dat, p, tl); |
| 189 | p += tl; |
| 190 | len -= tl; |
| 191 | |
| 192 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 193 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 194 | #endif /* AFS */ |