]> git.saurik.com Git - apple/configd.git/blobdiff - dnsinfo/dnsinfo_flatfile.c
configd-1109.101.1.tar.gz
[apple/configd.git] / dnsinfo / dnsinfo_flatfile.c
index bd4e4495aa6a372848e2ee1226afd37aa1e09a9b..900f9ec4150c1b1e8bf0434425b67a890aec390f 100644 (file)
@@ -1,15 +1,15 @@
 /*
- * Copyright (c) 2009, 2011, 2012, 2014, 2015 Apple Inc. All rights reserved.
+ * Copyright (c) 2009, 2011, 2012, 2014, 2015, 2017-2020 Apple Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
- * 
+ *
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * compliance with the License. Please obtain a copy of the License at
  * http://www.opensource.apple.com/apsl/ and read it before using this
  * file.
- * 
+ *
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
@@ -17,7 +17,7 @@
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  * Please see the License for the specific language governing rights and
  * limitations under the License.
- * 
+ *
  * @APPLE_LICENSE_HEADER_END@
  */
 
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <unistd.h>
+#include <SystemConfiguration/SCPrivate.h>     // for SC_log
+
+#ifdef SC_LOG_HANDLE
+#include <os/log.h>
+os_log_t       SC_LOG_HANDLE(void);
+#endif //SC_LOG_HANDLE
 
 #include "dnsinfo.h"
 #include "dnsinfo_private.h"
@@ -293,7 +299,6 @@ _dnsinfo_flatfile_create_resolver(const char *dir, const char *path)
        if (f == NULL) return NULL;
 
        while ((buf = fgetln(f, &len)) != NULL) {
-               int     i;
                char    *lineptr;
                int     max_count;
                int     token;
@@ -305,8 +310,7 @@ _dnsinfo_flatfile_create_resolver(const char *dir, const char *path)
                line = reallocf(line, len+1);
                if (line == NULL) continue;
 
-               strncpy(line, buf, len);
-               line[len] = '\0';
+               strlcpy(line, buf, len+1);
 
                // parse the first word of the line (the config token)
                lineptr = line;
@@ -322,7 +326,7 @@ _dnsinfo_flatfile_create_resolver(const char *dir, const char *path)
 
                // translate config token to enumerated value
                token = -1;
-               for (i = 0; i < sizeof(tokens) / sizeof(tokens[0]); i++) {
+               for (size_t i = 0; i < sizeof(tokens) / sizeof(tokens[0]); i++) {
                        if (strcasecmp(word, tokens[i].name) == 0) {
                                token     = tokens[i].token;
                                max_count = tokens[i].max_count;
@@ -331,6 +335,10 @@ _dnsinfo_flatfile_create_resolver(const char *dir, const char *path)
                }
                if (token == -1) {
                        // if not a recognized token
+                       SC_log(LOG_NOTICE,
+                              "Unrecognized token (%s) found in: %s",
+                              word,
+                              filename);
                        continue;
                }
 
@@ -390,7 +398,7 @@ _dnsinfo_flatfile_create_resolver(const char *dir, const char *path)
 
                                if_index = if_nametoindex(word);
                                if (if_index > 0) {
-                                       _dns_resolver_set_if_index(&res, if_index);
+                                       _dns_resolver_set_if_index(&res, if_index, word);
                                }
                                break;
                        }
@@ -464,7 +472,7 @@ _dnsinfo_flatfile_create_resolver(const char *dir, const char *path)
                                long    number  = -1;
 
                                number = strtol(word, NULL, 0);
-                               if (number < 0 || number > UINT32_MAX) break;
+                               if (number < 0 || number > (long)UINT32_MAX) break;
                                _dns_resolver_set_order(&res, (uint32_t)number);
                                break;
                        }
@@ -488,13 +496,12 @@ _dnsinfo_flatfile_create_resolver(const char *dir, const char *path)
                                long    number  = -1;
 
                                number = strtol(word, NULL, 0);
-                               if (number < 0 || number > UINT32_MAX) break;
+                               if (number < 0 || number > (long)UINT32_MAX) break;
                                _dns_resolver_set_timeout(&res, (uint32_t)number);
                                break;
                        }
                }
        }
-       if (line != NULL) free(line);
 
        // set the domain to the basename of the path if not specified
        if ((res != NULL) && (token_count[TOKEN_DOMAIN] == 0)) {
@@ -520,6 +527,7 @@ _dnsinfo_flatfile_create_resolver(const char *dir, const char *path)
 
     done :
 
+       if (line != NULL) free(line);
        fclose(f);
        return res;
 }
@@ -563,31 +571,44 @@ _dnsinfo_flatfile_add_resolvers(dns_create_config_t *config)
 #ifdef MAIN
 #undef MAIN
 
+#include "dnsinfo_logging.h"
 #include "dnsinfo_copy.c"
 
 int
 main(int argc, char **argv)
 {
-       uint8_t                 *buf;
-       dns_config_t            *config;
-       dns_create_config_t     create_config;
-       _dns_config_buf_t       *config_buf;
-       uint32_t                n_config;
-       uint32_t                n_padding;
-       dns_create_resolver_t   resolver;
-
-       resolver = _dnsinfo_flatfile_create_resolver(NULL, _PATH_RESCONF);
-
-       create_config = _dns_configuration_create();
-       _dnsinfo_flatfile_add_resolvers(&create_config);
-
-       config_buf = (_dns_config_buf_t *)create_config;
-       n_config  = sizeof(_dns_config_buf_t) + ntohl(config_buf->n_attribute);
-       n_padding = ntohl(config_buf->n_padding);
-       buf = malloc(n_config + n_padding);
-       bcopy((void *)config_buf, buf, n_config);
-       bzero(&buf[n_config], n_padding);
-       config = _dns_configuration_expand_config((_dns_config_buf_t *)buf);
+       dns_config_t            *dns_config     = NULL;
+       _dns_config_buf_t       *dns_config_buf = NULL;
+       dns_create_config_t     dns_create_config;
+       dns_create_resolver_t   dns_create_resolver;
+
+       dns_create_resolver = _dnsinfo_flatfile_create_resolver(NULL, _PATH_RESCONF);
+       _dns_resolver_free(&dns_create_resolver);
+
+       dns_create_config = _dns_configuration_create();
+       if (dns_create_config != NULL) {
+               size_t          n;
+
+               _dnsinfo_flatfile_add_resolvers(&dns_create_config);
+
+               n = sizeof(_dns_config_buf_t);
+               n += ntohl(((_dns_config_buf_t *)dns_create_config)->n_attribute);
+               dns_config_buf = _dns_configuration_buffer_create((void *)dns_create_config, n);
+               _dns_configuration_free(&dns_create_config);
+       }
+
+       if (dns_config_buf != NULL) {
+               dns_config = _dns_configuration_buffer_expand(dns_config_buf);
+               if (dns_config == NULL) {
+                       // if we were unable to expand the configuration
+                       _dns_configuration_buffer_free(&dns_config_buf);
+               }
+       }
+
+       if (dns_config != NULL) {
+               _dns_configuration_log(dns_config, TRUE, NULL);
+               free(dns_config);
+       }
 
        return 0;
 }