From b3fe263e5dc8b362aec2ee8c35d5528a04cb8fe2 Mon Sep 17 00:00:00 2001 From: John Carr Date: Fri, 21 Aug 1992 02:57:41 +0000 Subject: [PATCH] Principal type changes git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@2347 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/krb5/krb/bld_pr_ext.c | 60 +++++++++++++------------- src/lib/krb5/krb/bld_princ.c | 80 +++++++++++++++++------------------ src/lib/krb5/krb/pr_to_salt.c | 40 +++++++++++------- src/lib/krb5/krb/princ_comp.c | 28 ++++++------ src/lib/krb5/krb/rd_req_sim.c | 3 +- src/lib/krb5/krb/recvauth.c | 9 ++-- src/lib/krb5/krb/unparse.c | 31 ++++++++------ 7 files changed, 133 insertions(+), 118 deletions(-) diff --git a/src/lib/krb5/krb/bld_pr_ext.c b/src/lib/krb5/krb/bld_pr_ext.c index 68cdeee64..ca2697335 100644 --- a/src/lib/krb5/krb/bld_pr_ext.c +++ b/src/lib/krb5/krb/bld_pr_ext.c @@ -54,6 +54,8 @@ va_dcl va_list ap; register int i, count = 0, size; register char *next; + char *tmpdata; + krb5_data *princ_data; krb5_principal princ_ret; #if __STDC__ || defined(STDARG_PROTOTYPES) @@ -63,29 +65,33 @@ va_dcl #endif /* count up */ while (va_arg(ap, int) != 0) { - va_arg(ap, char *); /* pass one up */ + (void)va_arg(ap, char *); /* pass one up */ count++; } va_end(ap); /* we do a 2-pass to avoid the need to guess on allocation needs cf. bld_princ.c */ - /* get space for array and realm, and insert realm */ - princ_ret = (krb5_principal) malloc(sizeof(*princ_ret) * (count + 2)); - if (!princ_ret) + /* get space for array */ + princ_data = (krb5_data *) malloc(sizeof(krb5_data) * count); + if (!princ_data) return ENOMEM; - if (!(princ_ret[0] = (krb5_data *) malloc(sizeof(*princ_ret[0])))) { - xfree(princ_ret); + princ_ret = (krb5_principal) malloc(sizeof(krb5_principal_data)); + if (!princ_ret) { + xfree(princ_data); return ENOMEM; } - princ_ret[0]->length = rlen; - princ_ret[0]->data = malloc(rlen); - if (!princ_ret[0]->data) { - xfree(princ_ret[0]); + princ_ret->data = princ_data; + princ_ret->length = count; + tmpdata = malloc(rlen); + if (!tmpdata) { + xfree(princ_data); xfree(princ_ret); return ENOMEM; } - memcpy(princ_ret[0]->data, realm, rlen); + krb5_princ_set_realm_length(princ_ret, rlen); + krb5_princ_set_realm_data(princ_ret, tmpdata); + memcpy(tmpdata, realm, rlen); /* process rest of components */ #if __STDC__ || defined(STDARG_PROTOTYPES) @@ -93,30 +99,24 @@ va_dcl #else va_start(ap); #endif - for (i = 1; i <= count; i++) { - if (!(princ_ret[i] = - (krb5_data *) malloc(sizeof(*princ_ret[i])))) { - free_out: - for (i--; i >= 0; i--) { - xfree(princ_ret[i]->data); - xfree(princ_ret[i]); - } - xfree(princ_ret); - va_end(ap); - return (ENOMEM); - } + for (i = 0; i < count; i++) { size = va_arg(ap, int); next = va_arg(ap, char *); - princ_ret[i]->length = size; - princ_ret[i]->data = malloc(size); - if (!princ_ret[i]->data) { - xfree(princ_ret[i]); + princ_data[i].length = size; + princ_data[i].data = malloc(size); + if (!princ_data[i].data) goto free_out; - } - memcpy(princ_ret[i]->data, next, size); + memcpy(princ_data[i].data, next, size); } - princ_ret[count+1] = 0; va_end(ap); *princ = princ_ret; return 0; + +free_out: + while (i-- >= 0) + xfree(princ_data[i].data); + xfree(princ_data); + xfree(princ_ret); + va_end(ap); + return ENOMEM; } diff --git a/src/lib/krb5/krb/bld_princ.c b/src/lib/krb5/krb/bld_princ.c index b1be2a99a..36352accc 100644 --- a/src/lib/krb5/krb/bld_princ.c +++ b/src/lib/krb5/krb/bld_princ.c @@ -43,68 +43,62 @@ static char rcsid_bld_princ_c [] = krb5_error_code krb5_build_principal_va(princ, rlen, realm, ap) -krb5_principal *princ; +krb5_principal princ; int rlen; const char *realm; va_list ap; { register int i, count = 0; register char *next; - krb5_principal princ_ret; + char *tmpdata; + krb5_data *data; /* guess at an initial sufficent count of 2 pieces */ - count = 2 + 2; /* plus 2 for realm & null terminator */ + count = 2; /* get space for array and realm, and insert realm */ - princ_ret = (krb5_principal) malloc(sizeof(*princ_ret) * (count)); - if (!princ_ret) - return ENOMEM; - if (!(princ_ret[0] = (krb5_data *) malloc(sizeof(*princ_ret[0])))) { - xfree(princ_ret); + data = (krb5_data *) malloc(sizeof(krb5_data) * count); + if (data == 0) return ENOMEM; - } - princ_ret[0]->length = rlen; - princ_ret[0]->data = malloc(rlen); - if (!princ_ret[0]->data) { - xfree(princ_ret[0]); - xfree(princ_ret); + krb5_princ_set_realm_length(princ, rlen); + tmpdata = malloc(rlen); + if (!tmpdata) { + free (data); return ENOMEM; } - memcpy(princ_ret[0]->data, realm, rlen); + krb5_princ_set_realm_data(princ, tmpdata); + memcpy(tmpdata, realm, rlen); /* process rest of components */ - for (i = 1, next = va_arg(ap, char *); + for (i = 0, next = va_arg(ap, char *); next; next = va_arg(ap, char *), i++) { - if (i == count-1) { + if (i == count) { /* not big enough. realloc the array */ - krb5_principal p_tmp; - p_tmp = (krb5_principal) realloc((char *)princ_ret, sizeof(*princ_ret)*(count*2)); - if (!p_tmp) - goto free_out; - princ_ret = p_tmp; - count *= 2; - } - if (!(princ_ret[i] = - (krb5_data *) malloc(sizeof(*princ_ret[i])))) { - free_out: - for (i--; i >= 0; i--) { - xfree(princ_ret[i]->data); - xfree(princ_ret[i]); + krb5_data *p_tmp; + p_tmp = (krb5_data *) realloc((char *)data, + sizeof(krb5_data)*(count*2)); + if (!p_tmp) { + free_out: + while (i-- >= 0) + xfree(data[i].data); + xfree(data); + xfree(tmpdata); + return (ENOMEM); } - xfree(princ_ret); - return (ENOMEM); + count *= 2; + data = p_tmp; } - princ_ret[i]->length = strlen(next); - princ_ret[i]->data = strdup(next); - if (!princ_ret[i]->data) { - xfree(princ_ret[i]); + + data[i].length = strlen(next); + data[i].data = strdup(next); + if (!data[i].data) goto free_out; - } } - princ_ret[i] = 0; /* put a null as the last entry */ - *princ = princ_ret; + princ->data = data; + princ->length = i; + /* Set princ->type */ return 0; } @@ -121,13 +115,19 @@ va_dcl { va_list ap; krb5_error_code retval; + krb5_principal pr_ret = (krb5_principal)malloc(sizeof(krb5_principal_data)); + + if (!pr_ret) + return ENOMEM; #if __STDC__ || defined(STDARG_PROTOTYPES) va_start(ap, realm); #else va_start(ap); #endif - retval = krb5_build_principal_va(princ, rlen, realm, ap); + retval = krb5_build_principal_va(pr_ret, rlen, realm, ap); va_end(ap); + if (retval == 0) + *princ = pr_ret; return retval; } diff --git a/src/lib/krb5/krb/pr_to_salt.c b/src/lib/krb5/krb/pr_to_salt.c index 71f5abab8..9fe68f76d 100644 --- a/src/lib/krb5/krb/pr_to_salt.c +++ b/src/lib/krb5/krb/pr_to_salt.c @@ -40,29 +40,37 @@ static char rcsid_pr_to_salt_c[] = krb5_error_code krb5_principal2salt(pr, ret) -krb5_const_principal pr; +register krb5_const_principal pr; krb5_data *ret; { int size, offset; - krb5_data * const * prp; - + int nelem; + register int i; if (pr == 0) { - ret->length = 0; - ret->data = 0; - } else { - for (size = 0, prp = pr; *prp; prp++) - size += (*prp)->length; + ret->length = 0; + ret->data = 0; + return 0; + } + + nelem = krb5_princ_size(pr); + + size = krb5_princ_realm(pr)->length; + + for (i = 0; i < nelem; i++) + size += krb5_princ_component(pr, i)->length; + + ret->length = size; + if (!(ret->data = malloc (size))) + return ENOMEM; - ret->length = size; - if (!(ret->data = malloc (size+1))) - return ENOMEM; + offset = krb5_princ_realm(pr)->length; + memcpy(ret->data, krb5_princ_realm(pr)->data, offset); - for (offset=0, prp=pr; *prp; prp++) - { - memcpy(&ret->data[offset],(*prp)->data, (*prp)->length); - offset += (*prp)->length; - } + for (i = 0; i < nelem; i++) { + memcpy(&ret->data[offset], krb5_princ_component(pr, i)->data, + krb5_princ_component(pr, i)->length); + offset += krb5_princ_component(pr, i)->length; } return 0; } diff --git a/src/lib/krb5/krb/princ_comp.c b/src/lib/krb5/krb/princ_comp.c index 4fa3f9169..4e8c9f531 100644 --- a/src/lib/krb5/krb/princ_comp.c +++ b/src/lib/krb5/krb/princ_comp.c @@ -34,26 +34,28 @@ static char rcsid_princ_comp_c[] = #include #include -#ifndef min -#define min(a,b) ((a) < (b) ? (a) : (b)) -#endif /* min */ - krb5_boolean krb5_principal_compare(princ1, princ2) krb5_const_principal princ1; krb5_const_principal princ2; { - register krb5_data * const *p1, * const *p2; + register int i, nelem; - for (p1 = princ1, p2 = princ2; *p1 && *p2; p1++, p2++) { - if ((*p1)->length != (*p2)->length) - return FALSE; - if (memcmp((*p1)->data, (*p2)->data, (*p1)->length)) - + nelem = krb5_princ_size(princ1); + if (nelem != krb5_princ_size(princ2)) + return FALSE; + + if (krb5_princ_realm(princ1)->length != krb5_princ_realm(princ2)->length || + memcmp (krb5_princ_realm(princ1)->data, krb5_princ_realm(princ2)->data, + krb5_princ_realm(princ2)->length)) + return FALSE; + + for (i = 0; i < nelem; i++) { + register const krb5_data *p1 = krb5_princ_component(princ1, i); + register const krb5_data *p2 = krb5_princ_component(princ2, i); + if (p1->length != p2->length || + memcmp(p1->data, p2->data, p1->length)) return FALSE; } - if (*p1 || *p2) /* didn't both run out of components - at once */ - return FALSE; return TRUE; } diff --git a/src/lib/krb5/krb/rd_req_sim.c b/src/lib/krb5/krb/rd_req_sim.c index ef379c783..26f8ae6f7 100644 --- a/src/lib/krb5/krb/rd_req_sim.c +++ b/src/lib/krb5/krb/rd_req_sim.c @@ -74,7 +74,8 @@ krb5_tkt_authent **authdat; return(retval); } } - if (!(retval = krb5_get_server_rcache(server[1], &rcache))) { + if (!(retval = krb5_get_server_rcache(krb5_princ_component(server, 1), + &rcache))) { retval = krb5_rd_req_decoded(request, server, sender_addr, 0, 0, 0, rcache, authdat); diff --git a/src/lib/krb5/krb/recvauth.c b/src/lib/krb5/krb/recvauth.c index 025d2f440..0eeb5be06 100644 --- a/src/lib/krb5/krb/recvauth.c +++ b/src/lib/krb5/krb/recvauth.c @@ -74,6 +74,7 @@ krb5_recvauth(/* IN */ krb5_data outbuf; krb5_rcache rcache; krb5_octet response; + krb5_data *server_name; char *cachename; extern krb5_deltat krb5_clockskew; static char *rc_base = "rc_"; @@ -153,13 +154,13 @@ krb5_recvauth(/* IN */ problem = krb5_rc_resolve_type(&rcache, rc_type ? rc_type : "dfl"); cachename = NULL; - if (!problem && !(cachename = malloc(server[1]->length+1+ - strlen(rc_base)))) + server_name = krb5_princ_component(server, 0); + if (!problem && !(cachename = malloc(server_name->length+1+strlen(rc_base)))) problem = ENOMEM; if (!problem) { strcpy(cachename, rc_base ? rc_base : "rc_"); - strncat(cachename, server[1]->data, server[1]->length); - cachename[server[1]->length+strlen(rc_base)] = '\0'; + strncat(cachename, server_name->data, server_name->length); + cachename[server_name->length+strlen(rc_base)] = '\0'; problem = krb5_rc_resolve(rcache, cachename); } if (!problem) { diff --git a/src/lib/krb5/krb/unparse.c b/src/lib/krb5/krb/unparse.c index 1c8563081..a2ff154b7 100644 --- a/src/lib/krb5/krb/unparse.c +++ b/src/lib/krb5/krb/unparse.c @@ -68,19 +68,22 @@ int *size; { register char *cp, *q; register int i,j; - register int totalsize = 0; - int length; + int length, nelem; + register int totalsize = krb5_princ_realm(principal)->length + 1; - if (!principal[0] || !principal[1]) - return KRB5_PARSE_MALFORMED; - for (i = 0; principal[i]; i++) { - cp = principal[i]->data; - length = principal[i]->length; + for (cp = krb5_princ_realm(principal)->data; *cp; cp++) + if (*cp == REALM_SEP || *cp == COMPONENT_SEP || + *cp == '\\' || *cp == '\t') + totalsize++; + + nelem = krb5_princ_size(principal); + for (i = 0; i < nelem; i++) { + cp = krb5_princ_component(principal, i)->data; + length = krb5_princ_component(principal, i)->length; + totalsize += length; for (j=0; j < length; j++,cp++) if (*cp == REALM_SEP || *cp == COMPONENT_SEP || *cp == '\0' || *cp == '\\' || *cp == '\t') - totalsize += 2; - else totalsize++; totalsize++; /* This is for the separator */ } @@ -105,9 +108,9 @@ int *size; q = *name; - for (i = 1; principal[i]; i++) { - cp = principal[i]->data; - length = principal[i]->length; + for (i = 0; i < nelem; i++) { + cp = krb5_princ_component(principal, i)->data; + length = krb5_princ_component(principal, i)->length; for (j=0; j < length; j++,cp++) { switch (*cp) { case COMPONENT_SEP: @@ -131,8 +134,8 @@ int *size; q--; /* Back up last component separator */ *q++ = REALM_SEP; - cp = principal[0]->data; - length = principal[0]->length; + cp = krb5_princ_realm(principal)->data; + length = krb5_princ_realm(principal)->length; for (j=0; j < length; j++,cp++) { switch (*cp) { case COMPONENT_SEP: -- 2.26.2