transited.c revision 57416
117359Swosch/* 217359Swosch * Copyright (c) 1997 - 2000 Kungliga Tekniska H�gskolan 317359Swosch * (Royal Institute of Technology, Stockholm, Sweden). 417359Swosch * All rights reserved. 517359Swosch * 617359Swosch * Redistribution and use in source and binary forms, with or without 717359Swosch * modification, are permitted provided that the following conditions 817359Swosch * are met: 917359Swosch * 1017359Swosch * 1. Redistributions of source code must retain the above copyright 1117359Swosch * notice, this list of conditions and the following disclaimer. 1217359Swosch * 1317359Swosch * 2. Redistributions in binary form must reproduce the above copyright 1417359Swosch * notice, this list of conditions and the following disclaimer in the 1517359Swosch * documentation and/or other materials provided with the distribution. 1617359Swosch * 1717359Swosch * 3. Neither the name of the Institute nor the names of its contributors 1817359Swosch * may be used to endorse or promote products derived from this software 1917359Swosch * without specific prior written permission. 2017359Swosch * 2117359Swosch * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 2217359Swosch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2317359Swosch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2417359Swosch * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 2517359Swosch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2617359Swosch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2717359Swosch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2817359Swosch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2917359Swosch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3017359Swosch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3117359Swosch * SUCH DAMAGE. 3217359Swosch */ 3317359Swosch 3417359Swosch#include "krb5_locl.h" 3517359Swosch 3617359SwoschRCSID("$Id: transited.c,v 1.6 2000/02/07 03:19:43 assar Exp $"); 3717359Swosch 3817359Swosch/* this is an attempt at one of the most horrible `compression' 3917359Swosch schemes that has ever been invented; it's so amazingly brain-dead 4017359Swosch that words can not describe it, and all this just to save a few 4117359Swosch silly bytes */ 4217359Swosch 4317359Swoschstruct tr_realm { 4417359Swosch char *realm; 4517359Swosch unsigned leading_space:1; 4617359Swosch unsigned leading_slash:1; 4717359Swosch unsigned trailing_dot:1; 4817359Swosch struct tr_realm *next; 4917359Swosch}; 5017359Swosch 5117359Swoschstatic void 5217359Swoschfree_realms(struct tr_realm *r) 5317359Swosch{ 5417359Swosch struct tr_realm *p; 5517359Swosch while(r){ 5617359Swosch p = r; 5717359Swosch r = r->next; 5817359Swosch free(p->realm); 5917359Swosch free(p); 6017359Swosch } 6117359Swosch} 6217359Swosch 6317359Swoschstatic int 6417359Swoschmake_path(struct tr_realm *r, const char *from, const char *to) 6517359Swosch{ 6617359Swosch const char *p; 6717359Swosch struct tr_realm *path = r->next; 6817359Swosch struct tr_realm *tmp; 6917359Swosch 7017359Swosch if(strlen(from) < strlen(to)){ 7117359Swosch const char *tmp; 7217359Swosch tmp = from; 7317359Swosch from = to; 7417359Swosch to = tmp; 7517359Swosch } 7617359Swosch 7717359Swosch if(strcmp(from + strlen(from) - strlen(to), to) == 0){ 7817359Swosch p = from; 7917359Swosch while(1){ 8017359Swosch p = strchr(p, '.'); 8117359Swosch if(p == NULL) 8217359Swosch return KRB5KDC_ERR_POLICY; 8317359Swosch p++; 8417359Swosch if(strcmp(p, to) == 0) 8517359Swosch break; 8617359Swosch tmp = calloc(1, sizeof(*tmp)); 8717359Swosch tmp->next = path; 8817359Swosch path = tmp; 8917359Swosch path->realm = strdup(p); 9017359Swosch if(path->realm == NULL){ 9117359Swosch r->next = path; /* XXX */ 9217359Swosch return ENOMEM;; 9317359Swosch } 9418194Ssos } 9517359Swosch }else if(strncmp(from, to, strlen(to)) == 0){ 9617359Swosch p = from + strlen(from); 9717359Swosch while(1){ 9817359Swosch while(p >= from && *p != '/') p--; 9917359Swosch if(p == from) 10017359Swosch return KRB5KDC_ERR_POLICY; 10117359Swosch if(strncmp(to, from, p - from) == 0) 10217359Swosch break; 10317359Swosch tmp = calloc(1, sizeof(*tmp)); 10417359Swosch tmp->next = path; 10517359Swosch path = tmp; 10617359Swosch path->realm = malloc(p - from + 1); 10717359Swosch if(path->realm == NULL){ 10817359Swosch r->next = path; /* XXX */ 10917359Swosch return ENOMEM; 11017359Swosch } 11117359Swosch memcpy(path->realm, from, p - from); 11217359Swosch path->realm[p - from] = '\0'; 11317359Swosch p--; 11417359Swosch } 11517359Swosch }else 11617359Swosch return KRB5KDC_ERR_POLICY; 11717359Swosch r->next = path; 11817359Swosch 11917359Swosch return 0; 12017359Swosch} 12117359Swosch 12217359Swoschstatic int 12317359Swoschmake_paths(struct tr_realm *realms, const char *client_realm, 12417359Swosch const char *server_realm) 12517359Swosch{ 12617359Swosch struct tr_realm *r; 12717359Swosch int ret; 12817359Swosch const char *prev_realm = client_realm; 12917359Swosch const char *next_realm = NULL; 13017359Swosch for(r = realms; r; r = r->next){ 13117359Swosch /* it *might* be that you can have more than one empty 132 component in a row, at least that's how I interpret the 133 "," exception in 1510 */ 134 if(r->realm[0] == '\0'){ 135 while(r->next && r->next->realm[0] == '\0') 136 r = r->next; 137 if(r->next) 138 next_realm = r->next->realm; 139 else 140 next_realm = server_realm; 141 ret = make_path(r, prev_realm, next_realm); 142 if(ret){ 143 free_realms(realms); 144 return ret; 145 } 146 } 147 prev_realm = r->realm; 148 } 149 return 0; 150} 151 152static int 153expand_realms(struct tr_realm *realms, const char *client_realm) 154{ 155 struct tr_realm *r; 156 const char *prev_realm = NULL; 157 for(r = realms; r; r = r->next){ 158 if(r->trailing_dot){ 159 char *tmp; 160 if(prev_realm == NULL) 161 prev_realm = client_realm; 162 tmp = realloc(r->realm, strlen(r->realm) + strlen(prev_realm) + 1); 163 if(tmp == NULL){ 164 free_realms(realms); 165 return ENOMEM; 166 } 167 r->realm = tmp; 168 strcat(r->realm, prev_realm); 169 }else if(r->leading_slash && !r->leading_space && prev_realm){ 170 /* yet another exception: if you use x500-names, the 171 leading realm doesn't have to be "quoted" with a space */ 172 char *tmp; 173 tmp = malloc(strlen(r->realm) + strlen(prev_realm) + 1); 174 if(tmp == NULL){ 175 free_realms(realms); 176 return ENOMEM; 177 } 178 strcpy(tmp, prev_realm); 179 strcat(tmp, r->realm); 180 free(r->realm); 181 r->realm = tmp; 182 } 183 prev_realm = r->realm; 184 } 185 return 0; 186} 187 188static struct tr_realm * 189make_realm(char *realm) 190{ 191 struct tr_realm *r; 192 char *p, *q; 193 int quote = 0; 194 r = calloc(1, sizeof(*r)); 195 if(r == NULL){ 196 free(realm); 197 return NULL; 198 } 199 r->realm = realm; 200 for(p = q = r->realm; *p; p++){ 201 if(p == r->realm && *p == ' '){ 202 r->leading_space = 1; 203 continue; 204 } 205 if(q == r->realm && *p == '/') 206 r->leading_slash = 1; 207 if(quote){ 208 *q++ = *p; 209 quote = 0; 210 continue; 211 } 212 if(*p == '\\'){ 213 quote = 1; 214 continue; 215 } 216 if(p[0] == '.' && p[1] == '\0') 217 r->trailing_dot = 1; 218 *q++ = *p; 219 } 220 *q = '\0'; 221 return r; 222} 223 224static struct tr_realm* 225append_realm(struct tr_realm *head, struct tr_realm *r) 226{ 227 struct tr_realm *p; 228 if(head == NULL){ 229 r->next = NULL; 230 return r; 231 } 232 p = head; 233 while(p->next) p = p->next; 234 p->next = r; 235 return head; 236} 237 238static int 239decode_realms(const char *tr, int length, struct tr_realm **realms) 240{ 241 struct tr_realm *r = NULL; 242 243 char *tmp; 244 int quote = 0; 245 const char *start = tr; 246 int i; 247 248 for(i = 0; i < length; i++){ 249 if(quote){ 250 quote = 0; 251 continue; 252 } 253 if(tr[i] == '\\'){ 254 quote = 1; 255 continue; 256 } 257 if(tr[i] == ','){ 258 tmp = malloc(tr + i - start + 1); 259 memcpy(tmp, start, tr + i - start); 260 tmp[tr + i - start] = '\0'; 261 r = make_realm(tmp); 262 if(r == NULL){ 263 free_realms(*realms); 264 return ENOMEM; 265 } 266 *realms = append_realm(*realms, r); 267 start = tr + i + 1; 268 } 269 } 270 tmp = malloc(tr + i - start + 1); 271 memcpy(tmp, start, tr + i - start); 272 tmp[tr + i - start] = '\0'; 273 r = make_realm(tmp); 274 if(r == NULL){ 275 free_realms(*realms); 276 return ENOMEM; 277 } 278 *realms = append_realm(*realms, r); 279 280 return 0; 281} 282 283 284krb5_error_code 285krb5_domain_x500_decode(krb5_data tr, char ***realms, int *num_realms, 286 const char *client_realm, const char *server_realm) 287{ 288 struct tr_realm *r = NULL; 289 struct tr_realm *p, **q; 290 int ret; 291 292 /* split string in components */ 293 ret = decode_realms(tr.data, tr.length, &r); 294 if(ret) 295 return ret; 296 297 /* apply prefix rule */ 298 ret = expand_realms(r, client_realm); 299 if(ret) 300 return ret; 301 302 ret = make_paths(r, client_realm, server_realm); 303 if(ret) 304 return ret; 305 306 /* remove empty components */ 307 q = &r; 308 for(p = r; p; ){ 309 if(p->realm[0] == '\0'){ 310 free(p->realm); 311 *q = p->next; 312 free(p); 313 p = *q; 314 }else{ 315 q = &p->next; 316 p = p->next; 317 } 318 } 319 { 320 char **R; 321 *realms = NULL; 322 *num_realms = 0; 323 while(r){ 324 R = realloc(*realms, (*num_realms + 1) * sizeof(**realms)); 325 if(R == NULL) { 326 free(*realms); 327 return ENOMEM; 328 } 329 R[*num_realms] = r->realm; 330 (*num_realms)++; 331 *realms = R; 332 p = r->next; 333 free(r); 334 r = p; 335 } 336 } 337 return 0; 338} 339 340krb5_error_code 341krb5_domain_x500_encode(char **realms, int num_realms, krb5_data *encoding) 342{ 343 char *s = NULL; 344 int len = 0; 345 int i; 346 for(i = 0; i < num_realms; i++){ 347 len += strlen(realms[i]); 348 if(realms[i][0] == '/') 349 len++; 350 } 351 len += num_realms - 1; 352 s = malloc(len + 1); 353 *s = '\0'; 354 for(i = 0; i < num_realms; i++){ 355 if(i && i < num_realms - 1) 356 strcat(s, ","); 357 if(realms[i][0] == '/') 358 strcat(s, " "); 359 strcat(s, realms[i]); 360 } 361 encoding->data = s; 362 encoding->length = strlen(s); 363 return 0; 364} 365 366#if 0 367int 368main(int argc, char **argv) 369{ 370 krb5_data x; 371 char **r; 372 int num, i; 373 x.data = argv[1]; 374 x.length = strlen(x.data); 375 if(domain_expand(x, &r, &num, argv[2], argv[3])) 376 exit(1); 377 for(i = 0; i < num; i++) 378 printf("%s\n", r[i]); 379 return 0; 380} 381#endif 382 383