61eac51
diff -up cups-2.1.4/cups/auth.c.resolv_reload cups-2.1.4/cups/auth.c
61eac51
--- cups-2.1.4/cups/auth.c.resolv_reload	2016-06-14 19:45:32.000000000 +0200
f8a15d6
+++ cups-2.1.4/cups/auth.c	2017-04-27 10:31:13.430082002 +0200
f8a15d6
@@ -522,6 +522,18 @@ cups_gss_getname(
61eac51
   DEBUG_printf(("7cups_gss_getname(http=%p, service_name=\"%s\")", http,
61eac51
                 service_name));
61eac51
 
61eac51
+#ifdef HAVE_RES_INIT
61eac51
+ /*
61eac51
+  * Check if /etc/resolv.conf is modified.
61eac51
+  * If so, reload resolver.
61eac51
+  */
61eac51
+
f8a15d6
+  dns_resolver_reload_rv retval;
61eac51
+
f8a15d6
+  retval = dnsReloadResolver();
f8a15d6
+  if (retval.status == DNS_RESOLVER_RELOAD_ERROR)
f8a15d6
+    DEBUG_printf(("1cups_gss_getname: dnsReloadResolver() failed - %s.", strerror(retval.errnum)));
61eac51
+#endif /* HAVE_RES_INIT */
61eac51
 
61eac51
  /*
61eac51
   * Get the hostname...
61eac51
diff -up cups-2.1.4/cups/http-addr.c.resolv_reload cups-2.1.4/cups/http-addr.c
f8a15d6
--- cups-2.1.4/cups/http-addr.c.resolv_reload	2017-04-27 10:31:13.415082133 +0200
f8a15d6
+++ cups-2.1.4/cups/http-addr.c	2017-04-27 10:31:13.431081993 +0200
f8a15d6
@@ -364,6 +364,20 @@ httpAddrLookup(
61eac51
 
61eac51
 #ifdef HAVE_RES_INIT
61eac51
  /*
61eac51
+  * Check if /etc/resolv.conf is modified.
61eac51
+  * If so, reload resolver and set need_res_init to 0.
61eac51
+  */
61eac51
+
f8a15d6
+  dns_resolver_reload_rv retval;
61eac51
+
f8a15d6
+  retval = dnsReloadResolver();
61eac51
+
f8a15d6
+  if (retval.status == DNS_RESOLVER_RELOAD_RELOADED && cg->need_res_init == 1)
61eac51
+    cg->need_res_init = 0;
f8a15d6
+
f8a15d6
+  if (retval.status == DNS_RESOLVER_RELOAD_ERROR)
f8a15d6
+    DEBUG_printf(("1httpAddrLookup: dnsReloadResolver() failed - %s.", strerror(retval.errnum)));
61eac51
+ /*
61eac51
   * STR #2920: Initialize resolver after failure in cups-polld
61eac51
   *
61eac51
   * If the previous lookup failed, re-initialize the resolver to prevent
61eac51
diff -up cups-2.1.4/cups/http-addrlist.c.resolv_reload cups-2.1.4/cups/http-addrlist.c
f8a15d6
--- cups-2.1.4/cups/http-addrlist.c.resolv_reload	2017-04-27 10:31:13.400082264 +0200
f8a15d6
+++ cups-2.1.4/cups/http-addrlist.c	2017-04-27 10:31:13.431081993 +0200
f8a15d6
@@ -414,6 +414,20 @@ httpAddrGetList(const char *hostname,	/*
61eac51
 
61eac51
 #ifdef HAVE_RES_INIT
61eac51
  /*
61eac51
+  * Check if /etc/resolv.conf is modified.
61eac51
+  * If so, reload resolver and set cg->need_res_init to 0
61eac51
+  */
61eac51
+
f8a15d6
+  dns_resolver_reload_rv retval;
61eac51
+
f8a15d6
+  retval = dnsReloadResolver();
61eac51
+
f8a15d6
+  if (retval.status == DNS_RESOLVER_RELOAD_RELOADED && cg->need_res_init == 1)
61eac51
+    cg->need_res_init = 0;
f8a15d6
+
f8a15d6
+  if (retval.status == DNS_RESOLVER_RELOAD_ERROR)
f8a15d6
+    DEBUG_printf(("1httpAddrGetList: dnsReloadResolver() failed - %s.", strerror(retval.errnum)));
61eac51
+ /*
61eac51
   * STR #2920: Initialize resolver after failure in cups-polld
61eac51
   *
61eac51
   * If the previous lookup failed, re-initialize the resolver to prevent
61eac51
diff -up cups-2.1.4/cups/http.c.resolv_reload cups-2.1.4/cups/http.c
f8a15d6
--- cups-2.1.4/cups/http.c.resolv_reload	2017-04-27 10:31:13.433081976 +0200
f8a15d6
+++ cups-2.1.4/cups/http.c	2017-04-27 10:32:39.450330261 +0200
f8a15d6
@@ -4849,6 +4849,110 @@ http_write_chunk(http_t     *http,	/* I
61eac51
 }
61eac51
 
f8a15d6
 
61eac51
+#ifdef HAVE_RES_INIT
61eac51
+/*
61eac51
+ * Function to check modification time of resolv.conf. 
2b16845
+ * If time is changed, it reloads resolver. 
f8a15d6
+ * If /etc/resolv.conf doesn't exist, it tries to reload resolver with local nameserver, if it wasn't reloaded before
f8a15d6
+ * If even reloading resolver with local nameserver doesn't work, it ends with error.
f8a15d6
+ * Global variable resolv_conf_modtime is set to -1 before the first call of this function - this is for prevention of resolver's reloading when cupsd is just started and resolv.conf exists - cupsd has current configuration in that case.
61eac51
+ */
61eac51
+
f8a15d6
+dns_resolver_reload_rv
f8a15d6
+dnsReloadResolver()
61eac51
+{
f8a15d6
+  dns_resolver_reload_rv retval, lstat_retval, stat_retval, res_init_retval;
f8a15d6
+  struct stat resolv_conf_stat, resolv_conf_lstat, resolv_conf_status;
f8a15d6
+  retval.status = DNS_RESOLVER_RELOAD_OK;
f8a15d6
+  retval.errnum = 0;
f8a15d6
+  stat_retval.errnum = 0;
f8a15d6
+  lstat_retval.errnum = 0;
f8a15d6
+  res_init_retval.errnum = 0;
61eac51
+
f8a15d6
+  /* Variable to store /etc/resolv.conf modification time - initialized by actual time*/
f8a15d6
+  static time_t resolv_conf_modtime = -1;
61eac51
+
f8a15d6
+  /* This part of code is to ensure we get modification time of symlink and original file - we will decide which is newer later */
f8a15d6
+  stat_retval.status = stat(DNS_RESOLV_CONF_PATH, &resolv_conf_stat);
f8a15d6
+  stat_retval.errnum = errno;
f8a15d6
+  lstat_retval.status = lstat(DNS_RESOLV_CONF_PATH, &resolv_conf_lstat);
f8a15d6
+  lstat_retval.errnum = errno;
f8a15d6
+
f8a15d6
+  /* symlink /etc/resolv.conf or original file couldn't be stated because it doesn't exist, try resolver on localhost 
f8a15d6
+   * Covers cases:
f8a15d6
+   * - resolv.conf and original file existed and it doesn't now - resolv_conf_modtime has different value than 0  - reload resolver with local nameserver
f8a15d6
+   * - resolv.conf and original file didn't exist and still doesn't exist - resolv_conf_modtime is set to 0 - do nothing
f8a15d6
+   */
f8a15d6
+  if (stat_retval.status == -1 && lstat_retval.status == -1 && stat_retval.errnum == ENOENT && lstat_retval.errnum == ENOENT)
f8a15d6
+  {
f8a15d6
+    /* if resolv_conf_modtime is set to 0, it means previous reload was for resolver with local nameserver - no need to reload again */
f8a15d6
+    if (resolv_conf_modtime != 0)
f8a15d6
+    {
f8a15d6
+      res_init_retval.status = res_init();
f8a15d6
+      res_init_retval.errnum = errno;
f8a15d6
+      if (res_init_retval.status == 0)
f8a15d6
+      {
f8a15d6
+        resolv_conf_modtime = 0;
f8a15d6
+        retval.status = DNS_RESOLVER_RELOAD_RELOADED;
f8a15d6
+      }
f8a15d6
+      else
f8a15d6
+        retval.status = res_init_retval.status;
f8a15d6
+
f8a15d6
+      retval.errnum = res_init_retval.errnum;
f8a15d6
+    }
f8a15d6
+    else
f8a15d6
+    {
f8a15d6
+      retval.status = DNS_RESOLVER_RELOAD_OK;
f8a15d6
+      retval.errnum = ENOENT;
f8a15d6
+    }
f8a15d6
+
f8a15d6
+    return (retval);
f8a15d6
+  }
f8a15d6
+
f8a15d6
+  /* If stat ends with different errno, return status - it should return both statuses and errnos, but for simplification it returns only stat */
f8a15d6
+  if (stat_retval.status == -1 && lstat_retval.status == -1)
2b16845
+  {
f8a15d6
+    retval.errnum = stat_retval.errnum;
f8a15d6
+    retval.status = stat_retval.status;
f8a15d6
+    return (retval);
2b16845
+  }
2b16845
+
f8a15d6
+  /* Here we compare modification times from lstat and stat to decide which is newer - if they are equal, lstat modification time is used. We are checking only stat() errno, because case with errors in both lstat() and stat() functions is checked before */
f8a15d6
+  if (stat_retval.errnum == 0)
f8a15d6
+    if (resolv_conf_stat.st_mtime > resolv_conf_lstat.st_mtime)
f8a15d6
+      resolv_conf_status = resolv_conf_stat;
f8a15d6
+    else
f8a15d6
+      resolv_conf_status = resolv_conf_lstat;
f8a15d6
+  else
f8a15d6
+    resolv_conf_status = resolv_conf_lstat;
61eac51
+
f8a15d6
+  /* If /etc/resolv.conf exists and modification times are different, reload resolver.
f8a15d6
+   * Covers cases:
f8a15d6
+   * - resolv.conf or original file exists and it was modified - resolv_conf_modtime has different value than resolv_conf_status.st_mtime - reload resolver with nameserver from /etc/resolv.conf 
f8a15d6
+   * - resolv.conf or original file didn't exist and it does now - resolv_conf_modtime is set to 0 and resolv_conf_status.st_mtime has value - reload resolver with nameserver form /etc/resolv.conf
f8a15d6
+   * - resolv.conf or original file exists and it wasn't modified - resolv_conf_modtime is equal to resolv_conf_status.st_mtime - do nothing
f8a15d6
+   */
f8a15d6
+  if (resolv_conf_status.st_mtime != resolv_conf_modtime)
61eac51
+  {
f8a15d6
+    res_init_retval.status = res_init();
f8a15d6
+    res_init_retval.errnum = errno;
f8a15d6
+    if (res_init_retval.status == 0)
f8a15d6
+    {
f8a15d6
+      retval.status = DNS_RESOLVER_RELOAD_RELOADED;
f8a15d6
+    }
f8a15d6
+    else
f8a15d6
+      retval.status = res_init_retval.status;
61eac51
+
f8a15d6
+    retval.errnum = res_init_retval.errnum;
61eac51
+  }
61eac51
+
61eac51
+  resolv_conf_modtime = resolv_conf_status.st_mtime;
61eac51
+
f8a15d6
+  return (retval);
61eac51
+}
61eac51
+#endif /* HAVE_RES_INIT */
f8a15d6
+
f8a15d6
+
61eac51
 /*
61eac51
  * End of "$Id: http.c 12970 2015-11-13 20:02:51Z msweet $".
f8a15d6
  */
61eac51
diff -up cups-2.1.4/cups/http.h.resolv_reload cups-2.1.4/cups/http.h
61eac51
--- cups-2.1.4/cups/http.h.resolv_reload	2016-06-14 19:45:32.000000000 +0200
f8a15d6
+++ cups-2.1.4/cups/http.h	2017-04-27 10:35:32.279819884 +0200
61eac51
@@ -57,6 +57,12 @@ typedef off_t ssize_t;			/* @private@ */
61eac51
 #      define SO_PEERCRED LOCAL_PEERCRED
61eac51
 #    endif /* LOCAL_PEERCRED && !SO_PEERCRED */
61eac51
 #  endif /* WIN32 */
61eac51
+#  ifdef HAVE_RES_INIT
61eac51
+#    include <sys/stat.h>
61eac51
+#    include <unistd.h>
61eac51
+#    include <arpa/nameser.h>
61eac51
+#    include <resolv.h>
61eac51
+#  endif /* HAVE_RES_INIT */
61eac51
 
61eac51
 
61eac51
 /*
61eac51
@@ -97,6 +103,13 @@ extern "C" {
61eac51
 #endif /* AF_INET6 && !s6_addr32 */
61eac51
 
61eac51
 
61eac51
+#ifdef HAVE_RES_INIT
61eac51
+/*
61eac51
+ * Global variable for storing old modification time of resolv.conf 
61eac51
+ */
61eac51
+  extern time_t resolv_conf_modtime;
61eac51
+#endif /* HAVE_RES_INIT */
61eac51
+
61eac51
 /*
61eac51
  * Limits...
61eac51
  */
61eac51
@@ -105,6 +118,9 @@ extern "C" {
61eac51
 #  define HTTP_MAX_HOST		256	/* Max length of hostname string */
61eac51
 #  define HTTP_MAX_BUFFER	2048	/* Max length of data buffer */
61eac51
 #  define HTTP_MAX_VALUE	256	/* Max header field value length */
61eac51
+#  ifdef HAVE_RES_INIT
f8a15d6
+#    define DNS_RESOLV_CONF_PATH	"/etc/resolv.conf" /* Path to resolv.conf */
61eac51
+#  endif /* HAVE_RES_INIT */
61eac51
 
61eac51
 
61eac51
 /*
61eac51
@@ -408,6 +424,15 @@ typedef enum http_version_e		/**** HTTP
61eac51
 #  endif /* !_CUPS_NO_DEPRECATED */
61eac51
 } http_version_t;
61eac51
 
61eac51
+#ifdef HAVE_RES_INIT
f8a15d6
+typedef enum dns_resolver_reload_e
61eac51
+{
f8a15d6
+  DNS_RESOLVER_RELOAD_ERROR = -1,
f8a15d6
+  DNS_RESOLVER_RELOAD_OK = 0,
f8a15d6
+  DNS_RESOLVER_RELOAD_RELOADED = 1
f8a15d6
+} dns_resolver_reload_t;
61eac51
+#endif /* HAVE_RES_INIT */
61eac51
+
61eac51
 typedef union _http_addr_u		/**** Socket address union, which
61eac51
 					 **** makes using IPv6 and other
61eac51
 					 **** address types easier and
f8a15d6
@@ -446,6 +471,13 @@ typedef struct http_credential_s	/**** H
f8a15d6
 typedef int (*http_timeout_cb_t)(http_t *http, void *user_data);
f8a15d6
 					/**** HTTP timeout callback @since CUPS 1.5/OS X 10.7@ ****/
f8a15d6
 
f8a15d6
+#ifdef HAVE_RES_INIT
f8a15d6
+typedef struct dns_resolver_reload_retval
f8a15d6
+{
f8a15d6
+  dns_resolver_reload_t status;
f8a15d6
+  int errnum;
f8a15d6
+} dns_resolver_reload_rv;
f8a15d6
+#endif /* HAVE_RES_INIT */
f8a15d6
 
f8a15d6
 
f8a15d6
 /*
f8a15d6
@@ -592,6 +624,11 @@ extern http_version_t	httpGetVersion(htt
f8a15d6
 extern int		httpReconnect2(http_t *http, int msec, int *cancel)
f8a15d6
 			               _CUPS_API_1_6;
61eac51
 
61eac51
+/**** Prototype of function to check modification time of /etc/resolv.conf ****/
61eac51
+#ifdef HAVE_RES_INIT
f8a15d6
+extern dns_resolver_reload_rv	dnsReloadResolver();
61eac51
+#endif /* HAVE_RES_INIT */
61eac51
+
f8a15d6
 
f8a15d6
 /**** New in CUPS 1.7/OS X 10.9 ****/
f8a15d6
 extern http_t		*httpAcceptConnection(int fd, int blocking)
61eac51
diff -up cups-2.1.4/cups/http-support.c.resolv_reload cups-2.1.4/cups/http-support.c
f8a15d6
--- cups-2.1.4/cups/http-support.c.resolv_reload	2017-04-27 10:31:13.411082168 +0200
f8a15d6
+++ cups-2.1.4/cups/http-support.c	2017-04-27 10:31:13.434081967 +0200
f8a15d6
@@ -2266,6 +2266,18 @@ http_resolve_cb(
61eac51
     http_addrlist_t	*addrlist,	/* List of addresses */
61eac51
 			*addr;		/* Current address */
61eac51
 
61eac51
+#ifdef HAVE_RES_INIT
61eac51
+   /*
f8a15d6
+    * Check if resolv.conf is modified, if so, reload resolver
f8a15d6
+    */
61eac51
+
f8a15d6
+    dns_resolver_reload_rv retval;
61eac51
+
f8a15d6
+    retval = dnsReloadResolver();
f8a15d6
+    if (retval.status == DNS_RESOLVER_RELOAD_ERROR)
f8a15d6
+      DEBUG_printf(("1http_resolve_cb: dnsReloadResolver() failed - %s.", strerror(retval.errnum)));
61eac51
+#endif /* HAVE_RES_INIT */
61eac51
+
61eac51
     DEBUG_printf(("8http_resolve_cb: Looking up \"%s\".", hostTarget));
61eac51
 
61eac51
     snprintf(fqdn, sizeof(fqdn), "%d", ntohs(port));
61eac51
diff -up cups-2.1.4/scheduler/conf.c.resolv_reload cups-2.1.4/scheduler/conf.c
f8a15d6
--- cups-2.1.4/scheduler/conf.c.resolv_reload	2017-04-27 10:31:13.429082011 +0200
f8a15d6
+++ cups-2.1.4/scheduler/conf.c	2017-04-27 10:31:13.435081958 +0200
f8a15d6
@@ -939,6 +939,15 @@ cupsdReadConfiguration(void)
61eac51
   if (!RemotePort)
61eac51
     BrowseLocalProtocols = 0;		/* Disable sharing - no remote access */
61eac51
 
61eac51
+#ifdef HAVE_RES_INIT
f8a15d6
+  dns_resolver_reload_rv retval;	/* Return status of dnsReloadResolver() */
61eac51
+
f8a15d6
+  retval = dnsReloadResolver();
f8a15d6
+
f8a15d6
+  if (retval.status == DNS_RESOLVER_RELOAD_ERROR)
f8a15d6
+    syslog(LOG_LPR, "1cupsdReadConfiguration: dnsReloadResolver() failed - %s.", strerror(retval.errnum));
61eac51
+#endif /* HAVE_RES_INIT */
61eac51
+
61eac51
  /*
61eac51
   * See if the ServerName is an IP address...
61eac51
   */
61eac51
diff -up cups-2.1.4/scheduler/main.c.resolv_reload cups-2.1.4/scheduler/main.c
f8a15d6
--- cups-2.1.4/scheduler/main.c.resolv_reload	2017-04-27 10:31:13.421082081 +0200
f8a15d6
+++ cups-2.1.4/scheduler/main.c	2017-04-27 10:31:13.435081958 +0200
f8a15d6
@@ -140,6 +140,14 @@ main(int  argc,				/* I - Number of comm
f8a15d6
   long			tmo_delay;	/* Time before it must be called */
61eac51
 #endif /* HAVE_AVAHI */
61eac51
 
61eac51
+#ifdef HAVE_RES_INIT
f8a15d6
+  dns_resolver_reload_rv retval;	/* Return status from dnsReloadResolver() */
61eac51
+
f8a15d6
+  retval = dnsReloadResolver();
f8a15d6
+  if (retval.status == DNS_RESOLVER_RELOAD_ERROR)
f8a15d6
+    fprintf(stderr, "cupsd: Cannot reload a resolver - %s , using old configuration now.\n", strerror(retval.errnum));
61eac51
+#endif /* HAVE_RES_INIT */
61eac51
+
f8a15d6
 
61eac51
 #ifdef HAVE_GETEUID
61eac51
  /*