2 * Copyright (c) 1999 Dug Song. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 RCSID("$OpenBSD: radix.c,v 1.13 2000/09/07 20:27:52 deraadt Exp $");
33 typedef unsigned char my_u_char;
34 typedef unsigned int my_u_int32_t;
35 typedef unsigned short my_u_short;
37 /* Nasty macros from BIND-4.9.2 */
39 #define GETSHORT(s, cp) { \
40 register my_u_char *t_cp = (my_u_char*)(cp); \
41 (s) = (((my_u_short)t_cp[0]) << 8) \
42 | (((my_u_short)t_cp[1])) \
47 #define GETLONG(l, cp) { \
48 register my_u_char *t_cp = (my_u_char*)(cp); \
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])) \
57 #define PUTSHORT(s, cp) { \
58 register my_u_short t_s = (my_u_short)(s); \
59 register my_u_char *t_cp = (my_u_char*)(cp); \
65 #define PUTLONG(l, cp) { \
66 register my_u_int32_t t_l = (my_u_int32_t)(l); \
67 register my_u_char *t_cp = (my_u_char*)(cp); \
68 *t_cp++ = t_l >> 24; \
69 *t_cp++ = t_l >> 16; \
75 #define GETSTRING(s, p, p_l) { \
76 register char* p_targ = (p) + p_l; \
77 register char* s_c = (s); \
78 register char* p_c = (p); \
79 while (*p_c && (p_c < p_targ)) { \
82 if (p_c == p_targ) { \
86 (p_l) = (p_l) - (p_c - (p)); \
92 creds_to_radix(CREDENTIALS *creds, unsigned char *buf, size_t buflen)
99 *p++ = 1; /* version */
121 /* Null string to repeat the realm. */
124 PUTLONG(creds->issue_date, p);
126 unsigned int endTime;
127 endTime = (unsigned int) krb_life_to_time(creds->issue_date,
132 memcpy(p, &creds->session, sizeof(creds->session));
133 p += sizeof(creds->session);
135 PUTSHORT(creds->kvno, p);
136 PUTLONG(creds->ticket_st.length, p);
138 memcpy(p, creds->ticket_st.dat, creds->ticket_st.length);
139 p += creds->ticket_st.length;
142 return (uuencode((unsigned char *)temp, len, (char *)buf, buflen));
146 radix_to_creds(const char *buf, CREDENTIALS *creds)
154 len = uudecode(buf, (unsigned char *)temp, sizeof(temp));
160 /* check version and length! */
167 GETSTRING(creds->service, p, len);
168 GETSTRING(creds->instance, p, len);
169 GETSTRING(creds->realm, p, len);
171 GETSTRING(creds->pname, p, len);
172 GETSTRING(creds->pinst, p, len);
173 /* Ignore possibly different realm. */
180 /* Enough space for remaining fixed-length parts? */
181 if (len < (4 + 4 + sizeof(creds->session) + 2 + 4))
184 GETLONG(creds->issue_date, p);
187 unsigned int endTime;
190 creds->lifetime = krb_time_to_life(creds->issue_date, endTime);
193 memcpy(&creds->session, p, sizeof(creds->session));
194 p += sizeof(creds->session);
195 len -= sizeof(creds->session);
197 GETSHORT(creds->kvno, p);
199 GETLONG(creds->ticket_st.length, p);
202 tl = creds->ticket_st.length;
203 if (tl < 0 || tl > len || tl > sizeof(creds->ticket_st.dat))
206 memcpy(creds->ticket_st.dat, p, tl);