Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 2 | * Copyright (c) 1999 Dug Song. All rights reserved. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 3 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 23 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 24 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 25 | #include "includes.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 26 | #include "uuencode.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 27 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 28 | RCSID("$OpenBSD: radix.c,v 1.16 2001/06/23 15:12:19 itojun Exp $"); |
Damien Miller | bf7f466 | 2000-06-23 10:16:38 +1000 | [diff] [blame] | 29 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 30 | #ifdef AFS |
| 31 | #include <krb.h> |
| 32 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 33 | #include <radix.h> |
| 34 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 35 | typedef u_char my_u_char; |
| 36 | typedef u_int my_u_int32_t; |
| 37 | typedef u_short my_u_short; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 38 | |
| 39 | /* Nasty macros from BIND-4.9.2 */ |
| 40 | |
| 41 | #define GETSHORT(s, cp) { \ |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 42 | register my_u_char *t_cp = (my_u_char *)(cp); \ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 43 | (s) = (((my_u_short)t_cp[0]) << 8) \ |
| 44 | | (((my_u_short)t_cp[1])) \ |
| 45 | ; \ |
| 46 | (cp) += 2; \ |
| 47 | } |
| 48 | |
| 49 | #define GETLONG(l, cp) { \ |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 50 | register my_u_char *t_cp = (my_u_char *)(cp); \ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 51 | (l) = (((my_u_int32_t)t_cp[0]) << 24) \ |
| 52 | | (((my_u_int32_t)t_cp[1]) << 16) \ |
| 53 | | (((my_u_int32_t)t_cp[2]) << 8) \ |
| 54 | | (((my_u_int32_t)t_cp[3])) \ |
| 55 | ; \ |
| 56 | (cp) += 4; \ |
| 57 | } |
| 58 | |
| 59 | #define PUTSHORT(s, cp) { \ |
| 60 | register my_u_short t_s = (my_u_short)(s); \ |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 61 | register my_u_char *t_cp = (my_u_char *)(cp); \ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 62 | *t_cp++ = t_s >> 8; \ |
| 63 | *t_cp = t_s; \ |
| 64 | (cp) += 2; \ |
| 65 | } |
| 66 | |
| 67 | #define PUTLONG(l, cp) { \ |
| 68 | register my_u_int32_t t_l = (my_u_int32_t)(l); \ |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 69 | register my_u_char *t_cp = (my_u_char *)(cp); \ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 70 | *t_cp++ = t_l >> 24; \ |
| 71 | *t_cp++ = t_l >> 16; \ |
| 72 | *t_cp++ = t_l >> 8; \ |
| 73 | *t_cp = t_l; \ |
| 74 | (cp) += 4; \ |
| 75 | } |
| 76 | |
| 77 | #define GETSTRING(s, p, p_l) { \ |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 78 | register char *p_targ = (p) + p_l; \ |
| 79 | register char *s_c = (s); \ |
| 80 | register char *p_c = (p); \ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 81 | while (*p_c && (p_c < p_targ)) { \ |
| 82 | *s_c++ = *p_c++; \ |
| 83 | } \ |
| 84 | if (p_c == p_targ) { \ |
| 85 | return 1; \ |
| 86 | } \ |
| 87 | *s_c = *p_c++; \ |
| 88 | (p_l) = (p_l) - (p_c - (p)); \ |
| 89 | (p) = p_c; \ |
| 90 | } |
| 91 | |
| 92 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 93 | int |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 94 | creds_to_radix(CREDENTIALS *creds, u_char *buf, size_t buflen) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 95 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 96 | char *p, *s; |
| 97 | int len; |
| 98 | char temp[2048]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 99 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 100 | p = temp; |
| 101 | *p++ = 1; /* version */ |
| 102 | s = creds->service; |
| 103 | while (*s) |
| 104 | *p++ = *s++; |
| 105 | *p++ = *s; |
| 106 | s = creds->instance; |
| 107 | while (*s) |
| 108 | *p++ = *s++; |
| 109 | *p++ = *s; |
| 110 | s = creds->realm; |
| 111 | while (*s) |
| 112 | *p++ = *s++; |
| 113 | *p++ = *s; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 114 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 115 | s = creds->pname; |
| 116 | while (*s) |
| 117 | *p++ = *s++; |
| 118 | *p++ = *s; |
| 119 | s = creds->pinst; |
| 120 | while (*s) |
| 121 | *p++ = *s++; |
| 122 | *p++ = *s; |
| 123 | /* Null string to repeat the realm. */ |
| 124 | *p++ = '\0'; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 125 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 126 | PUTLONG(creds->issue_date, p); |
| 127 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 128 | u_int endTime; |
| 129 | endTime = (u_int) krb_life_to_time(creds->issue_date, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 130 | creds->lifetime); |
| 131 | PUTLONG(endTime, p); |
| 132 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 133 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 134 | memcpy(p, &creds->session, sizeof(creds->session)); |
| 135 | p += sizeof(creds->session); |
| 136 | |
| 137 | PUTSHORT(creds->kvno, p); |
| 138 | PUTLONG(creds->ticket_st.length, p); |
| 139 | |
| 140 | memcpy(p, creds->ticket_st.dat, creds->ticket_st.length); |
| 141 | p += creds->ticket_st.length; |
| 142 | len = p - temp; |
| 143 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 144 | return (uuencode((u_char *)temp, len, (char *)buf, buflen)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 145 | } |
| 146 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 147 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 148 | radix_to_creds(const char *buf, CREDENTIALS *creds) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 149 | { |
| 150 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 151 | char *p; |
| 152 | int len, tl; |
| 153 | char version; |
| 154 | char temp[2048]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 155 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 156 | len = uudecode(buf, (u_char *)temp, sizeof(temp)); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 157 | if (len < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 158 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 159 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 160 | p = temp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 161 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 162 | /* check version and length! */ |
| 163 | if (len < 1) |
| 164 | return 0; |
| 165 | version = *p; |
| 166 | p++; |
| 167 | len--; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 168 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 169 | GETSTRING(creds->service, p, len); |
| 170 | GETSTRING(creds->instance, p, len); |
| 171 | GETSTRING(creds->realm, p, len); |
| 172 | |
| 173 | GETSTRING(creds->pname, p, len); |
| 174 | GETSTRING(creds->pinst, p, len); |
| 175 | /* Ignore possibly different realm. */ |
| 176 | while (*p && len) |
| 177 | p++, len--; |
| 178 | if (len == 0) |
| 179 | return 0; |
| 180 | p++, len--; |
| 181 | |
| 182 | /* Enough space for remaining fixed-length parts? */ |
| 183 | if (len < (4 + 4 + sizeof(creds->session) + 2 + 4)) |
| 184 | return 0; |
| 185 | |
| 186 | GETLONG(creds->issue_date, p); |
| 187 | len -= 4; |
| 188 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 189 | u_int endTime; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 190 | GETLONG(endTime, p); |
| 191 | len -= 4; |
| 192 | creds->lifetime = krb_time_to_life(creds->issue_date, endTime); |
| 193 | } |
| 194 | |
| 195 | memcpy(&creds->session, p, sizeof(creds->session)); |
| 196 | p += sizeof(creds->session); |
| 197 | len -= sizeof(creds->session); |
| 198 | |
| 199 | GETSHORT(creds->kvno, p); |
| 200 | len -= 2; |
| 201 | GETLONG(creds->ticket_st.length, p); |
| 202 | len -= 4; |
| 203 | |
| 204 | tl = creds->ticket_st.length; |
| 205 | if (tl < 0 || tl > len || tl > sizeof(creds->ticket_st.dat)) |
| 206 | return 0; |
| 207 | |
| 208 | memcpy(creds->ticket_st.dat, p, tl); |
| 209 | p += tl; |
| 210 | len -= tl; |
| 211 | |
| 212 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 213 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 214 | #endif /* AFS */ |