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