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