From: Geoffrey King Date: Wed, 18 Aug 1999 00:38:13 +0000 (+0000) Subject: * ktutil_funcs.c (ktutil_add): Cleanup newly allocated keytab X-Git-Tag: krb5-1.2-beta1~290 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=47baa389b6e665f218f85683c0f46d13152f50cb;p=krb5.git * ktutil_funcs.c (ktutil_add): Cleanup newly allocated keytab entry and tail of linked list when an error occurs. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@11676 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/kadmin/ktutil/ChangeLog b/src/kadmin/ktutil/ChangeLog index f9aa8c022..6a9d1a5c5 100644 --- a/src/kadmin/ktutil/ChangeLog +++ b/src/kadmin/ktutil/ChangeLog @@ -1,3 +1,8 @@ +1999-08-17 Geoffrey King + + * ktutil_funcs.c (ktutil_add): Cleanup newly allocated keytab + entry and tail of linked list when an error occurs. + 1999-08-16 Geoffrey King * ktutil.c (ktutil_add_entry): diff --git a/src/kadmin/ktutil/ktutil_funcs.c b/src/kadmin/ktutil/ktutil_funcs.c index bacb0065c..9aead83ec 100644 --- a/src/kadmin/ktutil/ktutil_funcs.c +++ b/src/kadmin/ktutil/ktutil_funcs.c @@ -95,7 +95,7 @@ krb5_error_code ktutil_add(context, list, princ_str, kvno, int use_pass; { krb5_keytab_entry *entry; - krb5_kt_list lp = NULL, tail = NULL, back = NULL; + krb5_kt_list lp = NULL, prev = NULL; krb5_principal princ; krb5_enctype enctype; krb5_timestamp now; @@ -108,9 +108,6 @@ krb5_error_code ktutil_add(context, list, princ_str, kvno, char *cp; int i, tmp, pwsize = BUFSIZ; - retval = krb5_timeofday(context, &now); - if (retval) - return retval; retval = krb5_parse_name(context, princ_str, &princ); if (retval) return retval; @@ -122,13 +119,14 @@ krb5_error_code ktutil_add(context, list, princ_str, kvno, retval = krb5_string_to_enctype(enctype_str, &enctype); if (retval) return KRB5_BAD_ENCTYPE; + retval = krb5_timeofday(context, &now); + if (retval) + return retval; if (*list) { /* point lp at the tail of the list */ for (lp = *list; lp->next; lp = lp->next); - back = lp; } - entry = (krb5_keytab_entry *) malloc(sizeof(krb5_keytab_entry)); if (!entry) { return ENOMEM; @@ -145,25 +143,32 @@ krb5_error_code ktutil_add(context, list, princ_str, kvno, if (!lp->next) { return ENOMEM; } + prev = lp; lp = lp->next; } - - if (!tail) - tail = lp; lp->next = NULL; lp->entry = entry; if (use_pass) { password.length = pwsize; password.data = (char *) malloc(pwsize); - if (!password.data) - return ENOMEM; + if (!password.data) { + retval = ENOMEM; + goto cleanup; + } sprintf(promptstr, "Password for %.1000s: ", princ_str); - krb5_read_password(context, promptstr, NULL, password.data, - &password.length); - krb5_principal2salt(context, princ, &salt); - krb5_c_string_to_key(context, enctype, &password, &salt, &key); + retval = krb5_read_password(context, promptstr, NULL, password.data, + &password.length); + if (retval) + goto cleanup; + retval = krb5_principal2salt(context, princ, &salt); + if (retval) + goto cleanup; + retval = krb5_c_string_to_key(context, enctype, &password, + &salt, &key); + if (retval) + goto cleanup; memset(password.data, 0, password.length); password.length = 0; memcpy(&lp->entry->key, &key, sizeof(krb5_keyblock)); @@ -181,19 +186,23 @@ krb5_error_code ktutil_add(context, list, princ_str, kvno, buf[strlen(buf) - 1] = strlen(buf) % 2 ? '\0' : '0'; if (strlen(buf) == 0) { fprintf(stderr, "addent: Error reading key.\n"); - return 0; + retval = 0; + goto cleanup; } lp->entry->key.enctype = enctype; lp->entry->key.contents = (krb5_octet *) malloc((strlen(buf) + 1) / 2); - if (!lp->entry->key.contents) - return ENOMEM; + if (!lp->entry->key.contents) { + retval = ENOMEM; + goto cleanup; + } i = 0; for (cp = buf; *cp; cp += 2) { if (!isxdigit(cp[0]) || !isxdigit(cp[1])) { fprintf(stderr, "addent: Illegal character in key.\n"); - return 0; + retval = 0; + goto cleanup; } sscanf(cp, "%02x", &tmp); lp->entry->key.contents[i++] = (krb5_octet) tmp; @@ -205,9 +214,15 @@ krb5_error_code ktutil_add(context, list, princ_str, kvno, lp->entry->timestamp = now; if (!*list) - *list = tail; + *list = lp; return 0; + + cleanup: + if (prev) + prev->next = NULL; + ktutil_free_kt_list(context, lp); + return retval; } /*