From: Ken Raeburn Date: Thu, 5 Feb 2009 18:42:10 +0000 (+0000) Subject: Create and use (in several places) a variant of X-Git-Tag: krb5-1.8-alpha1~711 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=5726fcb8ee77cebb03087159dfd18f0dfd5733bb;p=krb5.git Create and use (in several places) a variant of krb5int_copy_data_contents that adds a trailing '\0' so the result can be used as a C string. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@21893 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/include/k5-int.h b/src/include/k5-int.h index c0540f38a..11d19e469 100644 --- a/src/include/k5-int.h +++ b/src/include/k5-int.h @@ -1148,6 +1148,9 @@ krb5int_gic_opt_to_opte(krb5_context context, krb5_error_code krb5int_copy_data_contents (krb5_context, const krb5_data *, krb5_data *); +krb5_error_code +krb5int_copy_data_contents_add0 (krb5_context, const krb5_data *, krb5_data *); + krb5_error_code krb5int_copy_creds_contents (krb5_context, const krb5_creds *, krb5_creds *); diff --git a/src/lib/kdb/kdb_cpw.c b/src/lib/kdb/kdb_cpw.c index 9f1f2b14e..55e8199d2 100644 --- a/src/lib/kdb/kdb_cpw.c +++ b/src/lib/kdb/kdb_cpw.c @@ -1,7 +1,7 @@ /* * lib/kdb/kdb_cpw.c * - * Copyright 1995 by the Massachusetts Institute of Technology. + * Copyright 1995, 2009 by the Massachusetts Institute of Technology. * All Rights Reserved. * * Export of this software from the United States of America may @@ -431,32 +431,17 @@ add_key_pwd(context, master_key, ks_tuple, ks_tuple_count, passwd, key_salt.data.length = 0; key_salt.data.data = 0; break; - case KRB5_KDB_SALTTYPE_AFS3: { -#if 0 - krb5_data * saltdata; - if (retval = krb5_copy_data(context, krb5_princ_realm(context, - db_entry->princ), &saltdata)) - return(retval); - - key_salt.data = *saltdata; + case KRB5_KDB_SALTTYPE_AFS3: + /* The afs_mit_string_to_key needs to use strlen, and the + realm field is not (necessarily) NULL terminated. */ + retval = krb5int_copy_data_contents_add0(context, + krb5_princ_realm(context, + db_entry->princ), + &key_salt.data); + if (retval) + return retval; key_salt.data.length = SALT_TYPE_AFS_LENGTH; /*length actually used below...*/ - free(saltdata); -#else - /* Why do we do this? Well, the afs_mit_string_to_key needs to - use strlen, and the realm is not NULL terminated.... */ - unsigned int slen = - (*krb5_princ_realm(context,db_entry->princ)).length; - if(!(key_salt.data.data = (char *) malloc(slen+1))) - return ENOMEM; - key_salt.data.data[slen] = 0; - memcpy(key_salt.data.data, - (*krb5_princ_realm(context,db_entry->princ)).data, - slen); - key_salt.data.length = SALT_TYPE_AFS_LENGTH; /*length actually used below...*/ -#endif - - } - break; + break; default: return(KRB5_KDB_BAD_SALTTYPE); } diff --git a/src/lib/krb5/krb/bld_pr_ext.c b/src/lib/krb5/krb/bld_pr_ext.c index befa0eee4..1a288c896 100644 --- a/src/lib/krb5/krb/bld_pr_ext.c +++ b/src/lib/krb5/krb/bld_pr_ext.c @@ -1,7 +1,7 @@ /* * lib/krb5/krb/bld_pr_ext.c * - * Copyright 1991, 2008 by the Massachusetts Institute of Technology. + * Copyright 1991, 2008, 2009 by the Massachusetts Institute of Technology. * All Rights Reserved. * * Export of this software from the United States of America may @@ -36,12 +36,10 @@ krb5_build_principal_ext(krb5_context context, krb5_principal * princ, unsigned int rlen, const char * realm, ...) { va_list ap; - register int i, count = 0; - register unsigned int size; - register char *next; - char *tmpdata = 0; + int i, count = 0; krb5_data *princ_data; krb5_principal princ_ret; + krb5_data tmpdata; va_start(ap, realm); /* count up */ @@ -64,28 +62,22 @@ krb5_build_principal_ext(krb5_context context, krb5_principal * princ, } princ_ret->data = princ_data; princ_ret->length = count; - tmpdata = malloc(rlen+1); - if (!tmpdata) { + tmpdata.length = rlen; + tmpdata.data = (char *) realm; + if (krb5int_copy_data_contents_add0(context, &tmpdata, &princ_ret->realm) != 0) { free(princ_data); free(princ_ret); return ENOMEM; } - krb5_princ_set_realm_length(context, princ_ret, rlen); - krb5_princ_set_realm_data(context, princ_ret, tmpdata); - memcpy(tmpdata, realm, rlen); - tmpdata[rlen] = 0; /* process rest of components */ va_start(ap, realm); for (i = 0; i < count; i++) { - size = va_arg(ap, unsigned int); - next = va_arg(ap, char *); - princ_data[i].length = size; - princ_data[i].data = malloc(size+1); - if (!princ_data[i].data) + tmpdata.length = va_arg(ap, unsigned int); + tmpdata.data = va_arg(ap, char *); + if (krb5int_copy_data_contents_add0(context, &tmpdata, + &princ_data[i]) != 0) goto free_out; - memcpy(princ_data[i].data, next, size); - princ_data[i].data[size] = 0; } va_end(ap); *princ = princ_ret; @@ -96,8 +88,8 @@ free_out: while (--i >= 0) free(princ_data[i].data); free(princ_data); + free(princ_ret->realm.data); free(princ_ret); - free(tmpdata); va_end(ap); return ENOMEM; } diff --git a/src/lib/krb5/krb/copy_data.c b/src/lib/krb5/krb/copy_data.c index 41032cca5..4896e8804 100644 --- a/src/lib/krb5/krb/copy_data.c +++ b/src/lib/krb5/krb/copy_data.c @@ -1,7 +1,7 @@ /* * lib/krb5/krb/copy_data.c * - * Copyright 1990,1991 by the Massachusetts Institute of Technology. + * Copyright 1990,1991,2009 by the Massachusetts Institute of Technology. * All Rights Reserved. * * Export of this software from the United States of America may @@ -75,3 +75,22 @@ krb5int_copy_data_contents(krb5_context context, const krb5_data *indata, krb5_d return 0; } + +/* As above, but add an (uncounted) extra byte at the end to + null-terminate the data so it can be used as a standard C + string. */ +krb5_error_code +krb5int_copy_data_contents_add0(krb5_context context, const krb5_data *indata, krb5_data *outdata) +{ + if (!indata) + return EINVAL; + outdata->length = indata->length; + if (!(outdata->data = malloc(outdata->length + 1))) + return ENOMEM; + if (outdata->length) + memcpy(outdata->data, indata->data, outdata->length); + outdata->data[outdata->length] = 0; + outdata->magic = KV5M_DATA; + + return 0; +} diff --git a/src/lib/krb5/krb/copy_princ.c b/src/lib/krb5/krb/copy_princ.c index 14af7b5f3..4e168b002 100644 --- a/src/lib/krb5/krb/copy_princ.c +++ b/src/lib/krb5/krb/copy_princ.c @@ -1,7 +1,7 @@ /* * lib/krb5/krb/copy_princ.c * - * Copyright 1990 by the Massachusetts Institute of Technology. + * Copyright 1990, 2009 by the Massachusetts Institute of Technology. * All Rights Reserved. * * Export of this software from the United States of America may @@ -54,35 +54,25 @@ krb5_copy_principal(krb5_context context, krb5_const_principal inprinc, krb5_pri } for (i = 0; i < nelems; i++) { - unsigned int len = krb5_princ_component(context, inprinc, i)->length; - krb5_princ_component(context, tempprinc, i)->length = len; - if (len) { - if (((krb5_princ_component(context, tempprinc, i)->data = - malloc(len)) == 0)) { - while (--i >= 0) - free(krb5_princ_component(context, tempprinc, i)->data); - free (tempprinc->data); - free (tempprinc); - return ENOMEM; - } - memcpy(krb5_princ_component(context, tempprinc, i)->data, - krb5_princ_component(context, inprinc, i)->data, len); - } else - krb5_princ_component(context, tempprinc, i)->data = 0; + if (krb5int_copy_data_contents(context, + krb5_princ_component(context, inprinc, i), + krb5_princ_component(context, tempprinc, i)) != 0) { + while (--i >= 0) + free(krb5_princ_component(context, tempprinc, i)->data); + free (tempprinc->data); + free (tempprinc); + return ENOMEM; + } } - tempprinc->realm.data = - malloc((tempprinc->realm.length = inprinc->realm.length) + 1); - if (!tempprinc->realm.data) { + if (krb5int_copy_data_contents_add0(context, &inprinc->realm, + &tempprinc->realm) != 0) { for (i = 0; i < nelems; i++) free(krb5_princ_component(context, tempprinc, i)->data); free(tempprinc->data); free(tempprinc); return ENOMEM; } - memcpy(tempprinc->realm.data, inprinc->realm.data, - inprinc->realm.length); - tempprinc->realm.data[tempprinc->realm.length] = 0; *outprinc = tempprinc; return 0; diff --git a/src/lib/krb5/libkrb5.exports b/src/lib/krb5/libkrb5.exports index 9651f30bc..0d1842dec 100644 --- a/src/lib/krb5/libkrb5.exports +++ b/src/lib/krb5/libkrb5.exports @@ -506,6 +506,7 @@ krb5int_accessor krb5int_cc_default krb5int_cleanup_library krb5int_cm_call_select +krb5int_copy_data_contents_add0 krb5int_foreach_localaddr krb5int_free_addrlist krb5int_init_context_kdc