* t_std_conf.c (test_get_krbhst): Use krb5_free_krbhst to
authorDanilo Almeida <dalmeida@mit.edu>
Thu, 5 Aug 1999 20:37:18 +0000 (20:37 +0000)
committerDanilo Almeida <dalmeida@mit.edu>
Thu, 5 Aug 1999 20:37:18 +0000 (20:37 +0000)
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
src/lib/krb5/os/an_to_ln.c
src/lib/krb5/os/changepw.c
src/lib/krb5/os/def_realm.c
src/lib/krb5/os/get_krbhst.c
src/lib/krb5/os/hst_realm.c
src/lib/krb5/os/init_os_ctx.c
src/lib/krb5/os/ktdefname.c
src/lib/krb5/os/locate_kdc.c
src/lib/krb5/os/realm_dom.c
src/lib/krb5/os/t_std_conf.c

index 9982029135b3fff2dd3c585f67c20ff21bcc0513..1cd8f2cbe3d0841e41fbab54409e18adb81488c4 100644 (file)
@@ -1,3 +1,43 @@
+1999-08-05  Danilo Almeida  <dalmeida@mit.edu>
+
+       * 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  <dalmeida@mit.edu>
+
+       * 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  <meeroh@mit.edu>
 
        * init_os_ctx.c (GetMacProfileFileSpec): removed bogus string conversion
index 39103c8dae971b9db2bd0f58554ed5835c2d09ea..39528d27288bf203780bc90433c50f2d545b6302 100644 (file)
@@ -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; i<nvalid-1; i++)
-                       krb5_xfree(mapping_values[i]);
-
+                   /* Just use the last one. */
                    /* Trim the value. */
                    cp = &mapping_values[nvalid-1]
                        [strlen(mapping_values[nvalid-1])];
@@ -706,8 +703,7 @@ krb5_aname_to_localname(context, aname, lnsize, lname)
                        kret = KRB5_CONFIG_NOTENUFSPACE;
 
                    /* Free residue */
-                   krb5_xfree(mapping_values[nvalid-1]);
-                   krb5_xfree(mapping_values);
+                   profile_free_list(mapping_values);
                }
                else {
                    /*
@@ -779,9 +775,7 @@ krb5_aname_to_localname(context, aname, lnsize, lname)
                        }
 
                        /* We're done, clean up the droppings. */
-                       for (i=0; mapping_values[i]; i++)
-                           krb5_xfree(mapping_values[i]);
-                       krb5_xfree(mapping_values);
+                       profile_free_list(mapping_values);
                    }
                    else {
                        /*
index e159f2090497e676894d4341cf88939da4241856..779fc89a8c23e1bd19414bea966aaeaac8f7775e 100644 (file)
@@ -180,9 +180,7 @@ krb5_change_password(context, creds, newpw, result_code,
        }
     }
 
-    for (i=0; hostlist[i]; i++)
-       free(hostlist[i]);
-    free ((char *)hostlist);
+    profile_free_list(hostlist);
 
     if (out == 0) {     /* Couldn't resolve any KDC names */
         free (addr_p);
index 690dcc633ed5b4f95dbb50caeec439ad01f0e26a..44a4e9381feb6fa174d059f302f0aed0f2eb6360 100644 (file)
@@ -57,7 +57,7 @@ krb5_get_default_realm(context, lrealm)
     krb5_context context;
     char FAR * FAR *lrealm;
 {
-    char *realm;
+    char *realm = 0;
     char *cp;
     krb5_error_code retval;
 
@@ -74,24 +74,21 @@ krb5_get_default_realm(context, lrealm)
             return KRB5_CONFIG_CANTOPEN;
         retval = profile_get_string(context->profile, "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
index 473a02a3bcf36880b0bc9861d489bce9e25fb19c..c929bbabaf7f704304fb10ad9277aad37e6dce5a 100644 (file)
@@ -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;
 }
index 3bd766ea58acd8f83b80bf9b4386a8653a72836f..bda5e3706f71219042600625cf8cf8de7ea143e8 100644 (file)
@@ -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
index 04d10bee1d3b70c7ae12278a28d606da4f48fa6a..a9c69b30b5bfb3eb64c42d1111cb789ea936946c 100644 (file)
@@ -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;
 
index 0493244f611056f7b51f77bf331112e5457096ab..d8f394697be991fd47509dc4d3bd2218f39611ea 100644 (file)
@@ -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)
        {
index ee81f6c086d43ac983098a1f62adcb6c1e77ee0f..2c99a2c6f281699a74961791326daf452fb26b77 100644 (file)
 #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);
index 8880c589d522da2f70c1d4e981580c621bb02e73..3d0b64ebdfd5ea807caa664fee96371a95bb6c48 100644 (file)
@@ -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;
 }
index c47072cb9328eb7ec6e342811d21caaf3fe6564a..0846b1c223ee1b3ba29e9dd208161101d520c6d1 100644 (file)
@@ -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");
 }