blob: 7e668ea656682fabd73f56ad7accece5d95a1ab9 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * radix.c
Damien Miller4af51302000-04-16 11:18:38 +10003 *
Damien Miller95def091999-11-25 00:26:21 +11004 * Dug Song <dugsong@UMICH.EDU>
5 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10006
Damien Millerd4a8b7e1999-10-27 13:42:43 +10007#include "includes.h"
Damien Millereba71ba2000-04-29 23:57:08 +10008#include "uuencode.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +10009
Damien Millerbf7f4662000-06-23 10:16:38 +100010RCSID("$OpenBSD: radix.c,v 1.12 2000/06/22 23:55:00 djm Exp $");
11
Damien Millerd4a8b7e1999-10-27 13:42:43 +100012#ifdef AFS
13#include <krb.h>
14
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015typedef unsigned char my_u_char;
16typedef unsigned int my_u_int32_t;
17typedef 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 Miller4af51302000-04-16 11:18:38 +100073int
Damien Millere247cc42000-05-07 12:03:14 +100074creds_to_radix(CREDENTIALS *creds, unsigned char *buf, size_t buflen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075{
Damien Miller95def091999-11-25 00:26:21 +110076 char *p, *s;
77 int len;
78 char temp[2048];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Miller95def091999-11-25 00:26:21 +110080 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 Millerd4a8b7e1999-10-27 13:42:43 +100094
Damien Miller95def091999-11-25 00:26:21 +110095 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 Millerd4a8b7e1999-10-27 13:42:43 +1000105
Damien Miller95def091999-11-25 00:26:21 +1100106 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 Millerd4a8b7e1999-10-27 13:42:43 +1000113
Damien Miller95def091999-11-25 00:26:21 +1100114 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 Millere247cc42000-05-07 12:03:14 +1000124 return (uuencode((unsigned char *)temp, len, (char *)buf, buflen));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125}
126
Damien Miller4af51302000-04-16 11:18:38 +1000127int
Damien Miller95def091999-11-25 00:26:21 +1100128radix_to_creds(const char *buf, CREDENTIALS *creds)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129{
130
Damien Miller95def091999-11-25 00:26:21 +1100131 char *p;
132 int len, tl;
133 char version;
134 char temp[2048];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135
Damien Millere247cc42000-05-07 12:03:14 +1000136 len = uudecode(buf, (unsigned char *)temp, sizeof(temp));
137 if (len < 0)
Damien Miller95def091999-11-25 00:26:21 +1100138 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Damien Miller95def091999-11-25 00:26:21 +1100140 p = temp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Miller95def091999-11-25 00:26:21 +1100142 /* check version and length! */
143 if (len < 1)
144 return 0;
145 version = *p;
146 p++;
147 len--;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148
Damien Miller95def091999-11-25 00:26:21 +1100149 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 Millerd4a8b7e1999-10-27 13:42:43 +1000193}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194#endif /* AFS */