From 300ef32ed9238d8728b427a0379cfc685f247e7f Mon Sep 17 00:00:00 2001 From: Danilo Almeida Date: Thu, 5 Aug 1999 20:37:18 +0000 Subject: [PATCH] * t_std_conf.c (test_get_krbhst): Use krb5_free_krbhst to free buffers allocated by krb5_get_krbhst. * locate_kdc.c (_krb5_use_dns): Add _krb_use_dns to abstract away looking up of whether we use DNS or not in the profile. * ktdefname.c (krb5_kt_default_name): Use profile_release_string instead of free to free string allocated by profile_get_string. * get_krbhst.c (krb5_get_krbhst): Copy results from profile_get_values into malloc'ed buffers so we can safely free them later. Also call profile_free_list on the original values. * locate_kdc.c (krb5_locate_kdc): * hst_realm.c (krb5_get_host_realm): * def_realm.c (krb5_get_default_realm): Use _krb5_use_dns to figure out whether to use DNS or not instead of directly reading the profile in this routine. * realm_dom.c (krb5_get_realm_domain): * hst_realm.c (krb5_get_host_realm): * def_realm.c (krb5_get_default_realm): Copy results of profile_get_string into malloc'ed buffer so it can safely be free'd later. * locate_kdc.c (krb5_locate_srv_conf): * def_realm.c (krb5_get_default_realm): * changepw.c (krb5_change_password): * an_to_ln.c (krb5_aname_to_localname): Use profile_free_list to free values allocated by profile_get_values. * init_os_ctx.c (os_init_paths): Wrap use of ctx->profile_in_memory with KRB5_DNS_LOOKUP. Use pointer value to determine whether to free files by checking the files value rather than depending on the return value. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@11626 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/krb5/os/ChangeLog | 40 +++++++++++++++++++++ src/lib/krb5/os/an_to_ln.c | 12 ++----- src/lib/krb5/os/changepw.c | 4 +-- src/lib/krb5/os/def_realm.c | 31 ++++++++-------- src/lib/krb5/os/get_krbhst.c | 28 +++++++++++++-- src/lib/krb5/os/hst_realm.c | 32 ++++++++--------- src/lib/krb5/os/init_os_ctx.c | 17 +++++---- src/lib/krb5/os/ktdefname.c | 2 +- src/lib/krb5/os/locate_kdc.c | 66 +++++++++++++++++++---------------- src/lib/krb5/os/realm_dom.c | 13 ++++++- src/lib/krb5/os/t_std_conf.c | 5 ++- 11 files changed, 158 insertions(+), 92 deletions(-) diff --git a/src/lib/krb5/os/ChangeLog b/src/lib/krb5/os/ChangeLog index 998202913..1cd8f2cbe 100644 --- a/src/lib/krb5/os/ChangeLog +++ b/src/lib/krb5/os/ChangeLog @@ -1,3 +1,43 @@ +1999-08-05 Danilo Almeida + + * t_std_conf.c (test_get_krbhst): Use krb5_free_krbhst to + free buffers allocated by krb5_get_krbhst. + + * locate_kdc.c (_krb5_use_dns): Add _krb_use_dns to abstract + away looking up of whether we use DNS or not in the profile. + + * ktdefname.c (krb5_kt_default_name): Use profile_release_string + instead of free to free string allocated by profile_get_string. + + * get_krbhst.c (krb5_get_krbhst): Copy results from profile_get_values + into malloc'ed buffers so we can safely free them later. Also call + profile_free_list on the original values. + + * locate_kdc.c (krb5_locate_kdc): + * hst_realm.c (krb5_get_host_realm): + * def_realm.c (krb5_get_default_realm): Use _krb5_use_dns to figure + out whether to use DNS or not instead of directly reading the + profile in this routine. + + * realm_dom.c (krb5_get_realm_domain): + * hst_realm.c (krb5_get_host_realm): + * def_realm.c (krb5_get_default_realm): Copy results of + profile_get_string into malloc'ed buffer so it can safely be free'd + later. + + * locate_kdc.c (krb5_locate_srv_conf): + * def_realm.c (krb5_get_default_realm): + * changepw.c (krb5_change_password): + * an_to_ln.c (krb5_aname_to_localname): Use profile_free_list + to free values allocated by profile_get_values. + +1999-08-04 Danilo Almeida + + * init_os_ctx.c (os_init_paths): Wrap use of ctx->profile_in_memory + with KRB5_DNS_LOOKUP. Use pointer value to determine whether to free + files by checking the files value rather than depending on the + return value. + 1999-07-27 Miro Jurisic * init_os_ctx.c (GetMacProfileFileSpec): removed bogus string conversion diff --git a/src/lib/krb5/os/an_to_ln.c b/src/lib/krb5/os/an_to_ln.c index 39103c8da..39528d272 100644 --- a/src/lib/krb5/os/an_to_ln.c +++ b/src/lib/krb5/os/an_to_ln.c @@ -688,10 +688,7 @@ krb5_aname_to_localname(context, aname, lnsize, lname) /* We found one or more explicit mappings. */ for (nvalid=0; mapping_values[nvalid]; nvalid++); - /* Free the other ones, just use the last one */ - for (i=0; iprofile, "libdefaults", "default_realm", 0, 0, - &context->default_realm); + &realm); + + if (!retval) { + context->default_realm = malloc(strlen(realm) + 1); + if (!context->default_realm) { + profile_release_string(realm); + return ENOMEM; + } + strcpy(context->default_realm, realm); + profile_release_string(realm); + } + #ifdef KRB5_DNS_LOOKUP if (context->default_realm == 0) { - int use_dns=0; - char * string=NULL; - krb5_error_code retval2; - - retval2 = profile_get_string(context->profile, "libdefaults", - "dns_fallback", 0, - context->profile_in_memory?"1":"0", - &string); - if ( retval2 ) - return(retval2); - - if ( string ) { - use_dns = krb5_conf_boolean(string); - free(string); - } + int use_dns = _krb5_use_dns(context); if ( use_dns ) { /* * Since this didn't appear in our config file, try looking diff --git a/src/lib/krb5/os/get_krbhst.c b/src/lib/krb5/os/get_krbhst.c index 473a02a3b..c929bbaba 100644 --- a/src/lib/krb5/os/get_krbhst.c +++ b/src/lib/krb5/os/get_krbhst.c @@ -63,6 +63,9 @@ krb5_get_krbhst(context, realm, hostlist) char **values, **cpp, *cp; const char *realm_kdc_names[4]; krb5_error_code retval; + int i, count; + + *hostlist = 0; realm_kdc_names[0] = "realms"; realm_kdc_names[1] = realm->data; @@ -95,7 +98,26 @@ krb5_get_krbhst(context, realm, hostlist) if (cp) *cp = 0; } - - *hostlist = values; - return 0; + count = cpp - values; + *hostlist = malloc(sizeof(char *) * (count + 1)); + if (!*hostlist) { + retval = ENOMEM; + goto cleanup; + } + for (i = 0; i < count; i++) { + *hostlist[i] = malloc(strlen(values[i]) + 1); + if (!*hostlist[i]) { + retval = ENOMEM; + goto cleanup; + } + } + *hostlist[count] = 0; + cleanup: + if (retval && *hostlist) { + for (cpp = *hostlist; *cpp; cpp++) + free(*cpp); + free(*hostlist); + } + profile_free_list(values); + return retval; } diff --git a/src/lib/krb5/os/hst_realm.c b/src/lib/krb5/os/hst_realm.c index 3bd766ea5..bda5e3706 100644 --- a/src/lib/krb5/os/hst_realm.c +++ b/src/lib/krb5/os/hst_realm.c @@ -230,7 +230,7 @@ krb5_get_host_realm(context, host, realmsp) char FAR * FAR * FAR *realmsp; { char **retrealms; - char *default_realm, *realm, *cp; + char *default_realm, *realm, *cp, *temp_realm; krb5_error_code retval; int l; char local_host[MAX_DNS_NAMELEN+1]; @@ -265,12 +265,13 @@ krb5_get_host_realm(context, host, realmsp) cp = local_host; realm = default_realm = (char *)NULL; + temp_realm = 0; while (cp) { retval = profile_get_string(context->profile, "domain_realm", cp, - 0, (char *)NULL, &realm); + 0, (char *)NULL, &temp_realm); if (retval) return retval; - if (realm != (char *)NULL) + if (temp_realm != (char *)NULL) break; /* Match found */ /* Setup for another test */ @@ -284,24 +285,19 @@ krb5_get_host_realm(context, host, realmsp) cp = strchr(cp, '.'); } } + if (temp_realm) { + realm = malloc(strlen(temp_realm) + 1); + if (!realm) { + profile_release_string(temp_realm); + return ENOMEM; + } + strcpy(realm, temp_realm); + profile_release_string(temp_realm); + } #ifdef KRB5_DNS_LOOKUP if (realm == (char *)NULL) { - int use_dns=0; - char * string=NULL; - krb5_error_code retval2; - - retval2 = profile_get_string(context->profile, "libdefaults", - "dns_fallback", 0, - context->profile_in_memory?"1":"0", - &string); - if ( retval2 ) - return(retval2); - - if ( string ) { - use_dns = krb5_conf_boolean(string); - free(string); - } + int use_dns = _krb5_use_dns(context); if ( use_dns ) { /* * Since this didn't appear in our config file, try looking diff --git a/src/lib/krb5/os/init_os_ctx.c b/src/lib/krb5/os/init_os_ctx.c index 04d10bee1..a9c69b30b 100644 --- a/src/lib/krb5/os/init_os_ctx.c +++ b/src/lib/krb5/os/init_os_ctx.c @@ -321,24 +321,27 @@ os_init_paths(ctx, secure) profile_filespec_t *files = 0; ctx->profile_secure = secure; +#ifdef KRB5_DNS_LOOKUP ctx->profile_in_memory = 0; +#endif /* KRB5_DNS_LOOKUP */ retval = os_get_default_config_files(&files, secure); if (!retval) { retval = profile_init(files, &ctx->profile); - +#ifdef KRB5_DNS_LOOKUP /* if none of the filenames can be opened use an empty profile */ - if ( retval == ENOENT ) { - retval = profile_init(NULL,&ctx->profile); - if ( !retval ) + if (retval == ENOENT) { + retval = profile_init(NULL, &ctx->profile); + if (!retval) ctx->profile_in_memory = 1; } - - if (files) - free_filespecs(files); +#endif /* KRB5_DNS_LOOKUP */ } + if (files) + free_filespecs(files); + if (retval) ctx->profile = 0; diff --git a/src/lib/krb5/os/ktdefname.c b/src/lib/krb5/os/ktdefname.c index 0493244f6..d8f394697 100644 --- a/src/lib/krb5/os/ktdefname.c +++ b/src/lib/krb5/os/ktdefname.c @@ -60,7 +60,7 @@ krb5_kt_default_name(context, name, namesize) if ((size_t) namesize < (strlen(retval)+1)) return KRB5_CONFIG_NOTENUFSPACE; strcpy(name, retval); - free(retval); + profile_release_string(retval); } else { #if defined (_MSDOS) || defined(_WIN32) { diff --git a/src/lib/krb5/os/locate_kdc.c b/src/lib/krb5/os/locate_kdc.c index ee81f6c08..2c99a2c6f 100644 --- a/src/lib/krb5/os/locate_kdc.c +++ b/src/lib/krb5/os/locate_kdc.c @@ -46,6 +46,30 @@ #endif #define MAX_DNS_NAMELEN (15*(MAXHOSTNAMELEN + 1)+1) + +int +_krb5_use_dns(context) + krb5_context context; +{ + krb5_error_code code; + char * value = NULL; + int use_dns = 0; + + code = profile_get_string(context->profile, "libdefaults", + "dns_fallback", 0, + context->profile_in_memory?"1":"0", + &value); + if (code) + return(code); + + if (value) { + use_dns = _krb5_conf_boolean(value); + profile_release_string(value); + } + + return use_dns; +} + #endif /* KRB5_DNS_LOOKUP */ /* @@ -114,6 +138,7 @@ krb5_locate_srv_conf(context, realm, name, addr_pp, naddrs, master_index, nmaste count++; if (count == 0) { + profile_free_list(hostlist); krb5_xfree(host); *naddrs = 0; return 0; @@ -165,8 +190,11 @@ krb5_locate_srv_conf(context, realm, name, addr_pp, naddrs, master_index, nmaste #endif addr_p = (struct sockaddr *)malloc (sizeof (struct sockaddr) * count); - if (addr_p == NULL) + if (addr_p == NULL) { + profile_free_list(hostlist); + profile_free_list(masterlist); return ENOMEM; + } for (i=0, out=0; hostlist[i]; i++) { host = hostlist[i]; @@ -186,8 +214,6 @@ krb5_locate_srv_conf(context, realm, name, addr_pp, naddrs, master_index, nmaste } if ((hp = gethostbyname(hostlist[i])) == 0) { - free(hostlist[i]); - hostlist[i] = 0; continue; } @@ -218,8 +244,11 @@ krb5_locate_srv_conf(context, realm, name, addr_pp, naddrs, master_index, nmaste addr_p = (struct sockaddr *) realloc ((char *)addr_p, sizeof(struct sockaddr) * count); - if (addr_p == NULL) + if (addr_p == NULL) { + profile_free_list(hostlist); + profile_free_list(masterlist); return ENOMEM; + } } if (sec_udpport && !port) { addr_p[out] = addr_p[out-1]; @@ -234,19 +263,10 @@ krb5_locate_srv_conf(context, realm, name, addr_pp, naddrs, master_index, nmaste } if (ismaster) *nmasters = out - *master_index; - - /* Free the hostlist entry we are looping over. */ - free(hostlist[i]); - hostlist[i] = 0; } - if (masterlist) { - for (i=0; masterlist[i]; i++) - free(masterlist[i]); - free(masterlist); - } - - free ((char *)hostlist); + profile_free_list(hostlist); + profile_free_list(masterlist); if (out == 0) { /* Couldn't resolve any KDC names */ free (addr_p); @@ -550,21 +570,7 @@ krb5_locate_kdc(context, realm, addr_pp, naddrs, master_index, nmasters) #ifdef KRB5_DNS_LOOKUP if (code) { - int use_dns=0; - char * string=NULL; - krb5_error_code code2; /* preserve error code from krb5_locate_srv_conf */ - - code2 = profile_get_string(context->profile, "libdefaults", - "dns_fallback", 0, - context->profile_in_memory?"1":"0", - &string); - if ( code2 ) - return(code2); - - if ( string ) { - use_dns = krb5_conf_boolean(string); - free(string); - } + int use_dns = _krb5_use_dns(context); if ( use_dns ) { code = krb5_locate_srv_dns(realm, "_kerberos", "_udp", addr_pp, naddrs); diff --git a/src/lib/krb5/os/realm_dom.c b/src/lib/krb5/os/realm_dom.c index 8880c589d..3d0b64ebd 100644 --- a/src/lib/krb5/os/realm_dom.c +++ b/src/lib/krb5/os/realm_dom.c @@ -49,8 +49,19 @@ krb5_get_realm_domain(context, realm, domain) char **domain; { krb5_error_code retval; + char *temp_domain = 0; retval = profile_get_string(context->profile, "realms", realm, - "default_domain", realm, domain); + "default_domain", realm, &temp_domain); + if (!retval && temp_domain) + { + *domain = malloc(strlen(temp_domain) + 1); + if (!*domain) { + retval = ENOMEM; + } else { + strcpy(*domain, temp_domain); + } + profile_release_string(temp_domain); + } return retval; } diff --git a/src/lib/krb5/os/t_std_conf.c b/src/lib/krb5/os/t_std_conf.c index c47072cb9..0846b1c22 100644 --- a/src/lib/krb5/os/t_std_conf.c +++ b/src/lib/krb5/os/t_std_conf.c @@ -93,14 +93,13 @@ void test_get_krbhst(ctx, realm) } if (hostlist[0] == 0) { printf(" (none)\n"); - free(hostlist); + krb5_free_krbhst(ctx, hostlist); return; } for (cpp = hostlist; *cpp; cpp++) { printf(" '%s'", *cpp); - free(*cpp); } - free(hostlist); + krb5_free_krbhst(ctx, hostlist); printf("\n"); } -- 2.26.2