Blame SOURCES/cups-1.6.3-resolv_reload.patch

3858b4
diff -up cups-1.6.3/cups/auth.c.resolv_reload cups-1.6.3/cups/auth.c
3858b4
--- cups-1.6.3/cups/auth.c.resolv_reload	2013-06-07 03:12:52.000000000 +0200
3858b4
+++ cups-1.6.3/cups/auth.c	2017-04-07 13:34:55.224510840 +0200
3858b4
@@ -527,6 +527,18 @@ cups_gss_getname(
3858b4
   DEBUG_printf(("7cups_gss_getname(http=%p, service_name=\"%s\")", http,
3858b4
                 service_name));
3858b4
 
3858b4
+#ifdef HAVE_RES_INIT
3858b4
+ /*
3858b4
+  * Check if /etc/resolv.conf is modified.
3858b4
+  * If so, reload resolver.
3858b4
+  */
3858b4
+
3858b4
+  dns_resolver_reload_rv retval;
3858b4
+
3858b4
+  retval = dnsReloadResolver();
3858b4
+  if (retval.status == DNS_RESOLVER_RELOAD_ERROR)
3858b4
+    DEBUG_printf(("1cups_gss_getname: dnsReloadResolver() failed - %s.", strerror(retval.errnum)));
3858b4
+#endif /* HAVE_RES_INIT */
3858b4
 
3858b4
  /*
3858b4
   * Get the hostname...
3858b4
diff -up cups-1.6.3/cups/http-addr.c.resolv_reload cups-1.6.3/cups/http-addr.c
3858b4
--- cups-1.6.3/cups/http-addr.c.resolv_reload	2017-04-07 13:34:55.112511816 +0200
3858b4
+++ cups-1.6.3/cups/http-addr.c	2017-04-07 13:34:55.224510840 +0200
3858b4
@@ -220,6 +220,20 @@ httpAddrLookup(
3858b4
 
3858b4
 #ifdef HAVE_RES_INIT
3858b4
  /*
3858b4
+  * Check if /etc/resolv.conf is modified.
3858b4
+  * If so, reload resolver and set need_res_init to 0.
3858b4
+  */
3858b4
+
3858b4
+  dns_resolver_reload_rv retval;
3858b4
+
3858b4
+  retval = dnsReloadResolver();
3858b4
+
3858b4
+  if (retval.status == DNS_RESOLVER_RELOAD_RELOADED && cg->need_res_init == 1)
3858b4
+    cg->need_res_init = 0;
3858b4
+
3858b4
+  if (retval.status == DNS_RESOLVER_RELOAD_ERROR)
3858b4
+    DEBUG_printf(("1httpAddrLookup: dnsReloadResolver() failed - %s.", strerror(retval.errnum)));
3858b4
+ /*
3858b4
   * STR #2920: Initialize resolver after failure in cups-polld
3858b4
   *
3858b4
   * If the previous lookup failed, re-initialize the resolver to prevent
3858b4
diff -up cups-1.6.3/cups/http-addrlist.c.resolv_reload cups-1.6.3/cups/http-addrlist.c
3858b4
--- cups-1.6.3/cups/http-addrlist.c.resolv_reload	2017-04-07 13:34:55.112511816 +0200
3858b4
+++ cups-1.6.3/cups/http-addrlist.c	2017-04-07 13:34:55.224510840 +0200
3858b4
@@ -389,6 +389,20 @@ httpAddrGetList(const char *hostname,	/*
3858b4
 
3858b4
 #ifdef HAVE_RES_INIT
3858b4
  /*
3858b4
+  * Check if /etc/resolv.conf is modified.
3858b4
+  * If so, reload resolver and set cg->need_res_init to 0
3858b4
+  */
3858b4
+
3858b4
+  dns_resolver_reload_rv retval;
3858b4
+
3858b4
+  retval = dnsReloadResolver();
3858b4
+
3858b4
+  if (retval.status == DNS_RESOLVER_RELOAD_RELOADED && cg->need_res_init == 1)
3858b4
+    cg->need_res_init = 0;
3858b4
+
3858b4
+  if (retval.status == DNS_RESOLVER_RELOAD_ERROR)
3858b4
+    DEBUG_printf(("1httpAddrGetList: dnsReloadResolver() failed - %s.", strerror(retval.errnum)));
3858b4
+ /*
3858b4
   * STR #2920: Initialize resolver after failure in cups-polld
3858b4
   *
3858b4
   * If the previous lookup failed, re-initialize the resolver to prevent
3858b4
diff -up cups-1.6.3/cups/http.c.resolv_reload cups-1.6.3/cups/http.c
3858b4
--- cups-1.6.3/cups/http.c.resolv_reload	2017-04-07 13:34:55.207510988 +0200
3858b4
+++ cups-1.6.3/cups/http.c	2017-04-10 21:21:41.528795395 +0200
3858b4
@@ -4795,6 +4795,108 @@ _httpTLSSetOptions(int options)
3858b4
   tls_options = options;
3858b4
 }
3858b4
 
3858b4
+#ifdef HAVE_RES_INIT
3858b4
+/*
3858b4
+ * Function to check modification time of resolv.conf. 
3858b4
+ * If time is changed, it reloads resolver. 
3858b4
+ * If /etc/resolv.conf doesn't exist, it tries to reload resolver with local nameserver, if it wasn't reloaded before
3858b4
+ * If even reloading resolver with local nameserver doesn't work, it ends with error.
3858b4
+ * 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.
3858b4
+ */
3858b4
+
3858b4
+dns_resolver_reload_rv
3858b4
+dnsReloadResolver()
3858b4
+{
3858b4
+  dns_resolver_reload_rv retval, lstat_retval, stat_retval, res_init_retval;
3858b4
+  struct stat resolv_conf_stat, resolv_conf_lstat, resolv_conf_status;
3858b4
+  retval.status = DNS_RESOLVER_RELOAD_OK;
3858b4
+  retval.errnum = 0;
3858b4
+  stat_retval.errnum = 0;
3858b4
+  lstat_retval.errnum = 0;
3858b4
+  res_init_retval.errnum = 0;
3858b4
+
3858b4
+  /* Variable to store /etc/resolv.conf modification time - initialized by actual time*/
3858b4
+  static time_t resolv_conf_modtime = -1;
3858b4
+
3858b4
+  /* This part of code is to ensure we get modification time of symlink and original file - we will decide which is newer later */
3858b4
+  stat_retval.status = stat(DNS_RESOLV_CONF_PATH, &resolv_conf_stat);
3858b4
+  stat_retval.errnum = errno;
3858b4
+  lstat_retval.status = lstat(DNS_RESOLV_CONF_PATH, &resolv_conf_lstat);
3858b4
+  lstat_retval.errnum = errno;
3858b4
+
3858b4
+  /* symlink /etc/resolv.conf or original file couldn't be stated because it doesn't exist, try resolver on localhost 
3858b4
+   * Covers cases:
3858b4
+   * - resolv.conf and original file existed and it doesn't now - resolv_conf_modtime has different value than 0  - reload resolver with local nameserver
3858b4
+   * - resolv.conf and original file didn't exist and still doesn't exist - resolv_conf_modtime is set to 0 - do nothing
3858b4
+   */
3858b4
+  if (stat_retval.status == -1 && lstat_retval.status == -1 && stat_retval.errnum == ENOENT && lstat_retval.errnum == ENOENT)
3858b4
+  {
3858b4
+    /* if resolv_conf_modtime is set to 0, it means previous reload was for resolver with local nameserver - no need to reload again */
3858b4
+    if (resolv_conf_modtime != 0)
3858b4
+    {
3858b4
+      res_init_retval.status = res_init();
3858b4
+      res_init_retval.errnum = errno;
3858b4
+      if (res_init_retval.status == 0)
3858b4
+      {
3858b4
+        resolv_conf_modtime = 0;
3858b4
+        retval.status = DNS_RESOLVER_RELOAD_RELOADED;
3858b4
+      }
3858b4
+      else
3858b4
+        retval.status = res_init_retval.status;
3858b4
+
3858b4
+      retval.errnum = res_init_retval.errnum;
3858b4
+    }
3858b4
+    else
3858b4
+    {
3858b4
+      retval.status = DNS_RESOLVER_RELOAD_OK;
3858b4
+      retval.errnum = ENOENT;
3858b4
+    }
3858b4
+
3858b4
+    return (retval);
3858b4
+  }
3858b4
+
3858b4
+  /* If stat ends with different errno, return status - it should return both statuses and errnos, but for simplification it returns only stat */
3858b4
+  if (stat_retval.status == -1 && lstat_retval.status == -1)
3858b4
+  {
3858b4
+    retval.errnum = stat_retval.errnum;
3858b4
+    retval.status = stat_retval.status;
3858b4
+    return (retval);
3858b4
+  }
3858b4
+
3858b4
+  /* 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 */
3858b4
+  if (stat_retval.errnum == 0)
3858b4
+    if (resolv_conf_stat.st_mtime > resolv_conf_lstat.st_mtime)
3858b4
+      resolv_conf_status = resolv_conf_stat;
3858b4
+    else
3858b4
+      resolv_conf_status = resolv_conf_lstat;
3858b4
+  else
3858b4
+    resolv_conf_status = resolv_conf_lstat;
3858b4
+
3858b4
+  /* If /etc/resolv.conf exists and modification times are different, reload resolver.
3858b4
+   * Covers cases:
3858b4
+   * - 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 
3858b4
+   * - 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
3858b4
+   * - resolv.conf or original file exists and it wasn't modified - resolv_conf_modtime is equal to resolv_conf_status.st_mtime - do nothing
3858b4
+   */
3858b4
+  if (resolv_conf_status.st_mtime != resolv_conf_modtime)
3858b4
+  {
3858b4
+    res_init_retval.status = res_init();
3858b4
+    res_init_retval.errnum = errno;
3858b4
+    if (res_init_retval.status == 0)
3858b4
+    {
3858b4
+      retval.status = DNS_RESOLVER_RELOAD_RELOADED;
3858b4
+    }
3858b4
+    else
3858b4
+      retval.status = res_init_retval.status;
3858b4
+
3858b4
+    retval.errnum = res_init_retval.errnum;
3858b4
+  }
3858b4
+
3858b4
+  resolv_conf_modtime = resolv_conf_status.st_mtime;
3858b4
+
3858b4
+  return (retval);
3858b4
+}
3858b4
+#endif /* HAVE_RES_INIT */
3858b4
 
3858b4
 /*
3858b4
  * End of "$Id: http.c 7850 2008-08-20 00:07:25Z mike $".
3858b4
diff -up cups-1.6.3/cups/http.h.resolv_reload cups-1.6.3/cups/http.h
3858b4
--- cups-1.6.3/cups/http.h.resolv_reload	2013-06-07 03:12:52.000000000 +0200
3858b4
+++ cups-1.6.3/cups/http.h	2017-04-07 13:34:55.225510832 +0200
3858b4
@@ -60,6 +60,12 @@ typedef off_t ssize_t;			/* @private@ */
3858b4
 #      define SO_PEERCRED LOCAL_PEERCRED
3858b4
 #    endif /* LOCAL_PEERCRED && !SO_PEERCRED */
3858b4
 #  endif /* WIN32 */
3858b4
+#  ifdef HAVE_RES_INIT
3858b4
+#    include <sys/stat.h>
3858b4
+#    include <unistd.h>
3858b4
+#    include <arpa/nameser.h>
3858b4
+#    include <resolv.h>
3858b4
+#  endif /* HAVE_RES_INIT */
3858b4
 
3858b4
 
3858b4
 /*
3858b4
@@ -102,6 +108,13 @@ extern "C" {
3858b4
 #endif /* AF_INET6 && !s6_addr32 */
3858b4
 
3858b4
 
3858b4
+#ifdef HAVE_RES_INIT
3858b4
+/*
3858b4
+ * Global variable for storing old modification time of resolv.conf 
3858b4
+ */
3858b4
+  extern time_t resolv_conf_modtime;
3858b4
+#endif /* HAVE_RES_INIT */
3858b4
+
3858b4
 /*
3858b4
  * Limits...
3858b4
  */
3858b4
@@ -110,6 +123,9 @@ extern "C" {
3858b4
 #  define HTTP_MAX_HOST		256	/* Max length of hostname string */
3858b4
 #  define HTTP_MAX_BUFFER	2048	/* Max length of data buffer */
3858b4
 #  define HTTP_MAX_VALUE	256	/* Max header field value length */
3858b4
+#  ifdef HAVE_RES_INIT
3858b4
+#    define DNS_RESOLV_CONF_PATH	"/etc/resolv.conf" /* Path to resolv.conf */
3858b4
+#  endif /* HAVE_RES_INIT */
3858b4
 
3858b4
 
3858b4
 /*
3858b4
@@ -290,6 +306,15 @@ typedef enum http_version_e		/**** HTTP
3858b4
   HTTP_1_1 = 101			/* HTTP/1.1 */
3858b4
 } http_version_t;
3858b4
 
3858b4
+#ifdef HAVE_RES_INIT
3858b4
+typedef enum dns_resolver_reload_e
3858b4
+{
3858b4
+  DNS_RESOLVER_RELOAD_ERROR = -1,
3858b4
+  DNS_RESOLVER_RELOAD_OK = 0,
3858b4
+  DNS_RESOLVER_RELOAD_RELOADED = 1
3858b4
+} dns_resolver_reload_t;
3858b4
+#endif /* HAVE_RES_INIT */
3858b4
+
3858b4
 typedef union _http_addr_u		/**** Socket address union, which
3858b4
 					 **** makes using IPv6 and other
3858b4
 					 **** address types easier and
3858b4
@@ -328,6 +353,13 @@ typedef struct http_credential_s	/**** H
3858b4
 typedef int (*http_timeout_cb_t)(http_t *http, void *user_data);
3858b4
 					/**** HTTP timeout callback @since CUPS 1.5/OS X 10.7@ ****/
3858b4
 
3858b4
+#ifdef HAVE_RES_INIT
3858b4
+typedef struct dns_resolver_reload_retval
3858b4
+{
3858b4
+  dns_resolver_reload_t status;
3858b4
+  int errnum;
3858b4
+} dns_resolver_reload_rv;
3858b4
+#endif /* HAVE_RES_INIT */
3858b4
 
3858b4
 
3858b4
 /*
3858b4
@@ -472,6 +504,11 @@ extern http_version_t	httpGetVersion(htt
3858b4
 extern int		httpReconnect2(http_t *http, int msec, int *cancel)
3858b4
 			               _CUPS_API_1_6;
3858b4
 
3858b4
+/**** Prototype of function to check modification time of /etc/resolv.conf ****/
3858b4
+#ifdef HAVE_RES_INIT
3858b4
+extern dns_resolver_reload_rv	dnsReloadResolver();
3858b4
+#endif /* HAVE_RES_INIT */
3858b4
+
3858b4
 
3858b4
 /*
3858b4
  * C++ magic...
3858b4
diff -up cups-1.6.3/cups/http-support.c.resolv_reload cups-1.6.3/cups/http-support.c
3858b4
--- cups-1.6.3/cups/http-support.c.resolv_reload	2017-04-07 13:34:55.130511659 +0200
3858b4
+++ cups-1.6.3/cups/http-support.c	2017-04-07 13:34:55.225510832 +0200
3858b4
@@ -2095,6 +2095,18 @@ http_resolve_cb(
3858b4
     http_addrlist_t	*addrlist,	/* List of addresses */
3858b4
 			*addr;		/* Current address */
3858b4
 
3858b4
+#ifdef HAVE_RES_INIT
3858b4
+   /*
3858b4
+    * Check if resolv.conf is modified, if so, reload resolver
3858b4
+    */
3858b4
+
3858b4
+    dns_resolver_reload_rv retval;
3858b4
+
3858b4
+    retval = dnsReloadResolver();
3858b4
+    if (retval.status == DNS_RESOLVER_RELOAD_ERROR)
3858b4
+      DEBUG_printf(("1http_resolve_cb: dnsReloadResolver() failed - %s.", strerror(retval.errnum)));
3858b4
+#endif /* HAVE_RES_INIT */
3858b4
+
3858b4
     DEBUG_printf(("8http_resolve_cb: Looking up \"%s\".", hostTarget));
3858b4
 
3858b4
     snprintf(fqdn, sizeof(fqdn), "%d", ntohs(port));
3858b4
diff -up cups-1.6.3/scheduler/conf.c.resolv_reload cups-1.6.3/scheduler/conf.c
3858b4
--- cups-1.6.3/scheduler/conf.c.resolv_reload	2017-04-07 13:34:55.213510936 +0200
3858b4
+++ cups-1.6.3/scheduler/conf.c	2017-04-07 13:34:55.226510823 +0200
3858b4
@@ -859,6 +859,15 @@ cupsdReadConfiguration(void)
3858b4
   if (!RemotePort)
3858b4
     BrowseLocalProtocols = 0;		/* Disable sharing - no remote access */
3858b4
 
3858b4
+#ifdef HAVE_RES_INIT
3858b4
+  dns_resolver_reload_rv retval;	/* Return status of dnsReloadResolver() */
3858b4
+
3858b4
+  retval = dnsReloadResolver();
3858b4
+
3858b4
+  if (retval.status == DNS_RESOLVER_RELOAD_ERROR)
3858b4
+    syslog(LOG_LPR, "1cupsdReadConfiguration: dnsReloadResolver() failed - %s.", strerror(retval.errnum));
3858b4
+#endif /* HAVE_RES_INIT */
3858b4
+
3858b4
  /*
3858b4
   * See if the ServerName is an IP address...
3858b4
   */
3858b4
diff -up cups-1.6.3/scheduler/main.c.resolv_reload cups-1.6.3/scheduler/main.c
3858b4
--- cups-1.6.3/scheduler/main.c.resolv_reload	2017-04-07 13:34:55.149511494 +0200
3858b4
+++ cups-1.6.3/scheduler/main.c	2017-04-07 13:34:55.226510823 +0200
3858b4
@@ -164,6 +164,14 @@ main(int  argc,				/* I - Number of comm
3858b4
   long			tmo_delay;	/* Time before it must be called */
3858b4
 #endif /* HAVE_AVAHI */
3858b4
 
3858b4
+#ifdef HAVE_RES_INIT
3858b4
+  dns_resolver_reload_rv retval;	/* Return status from dnsReloadResolver() */
3858b4
+
3858b4
+  retval = dnsReloadResolver();
3858b4
+  if (retval.status == DNS_RESOLVER_RELOAD_ERROR)
3858b4
+    fprintf(stderr, "cupsd: Cannot reload a resolver - %s , using old configuration now.\n", strerror(retval.errnum));
3858b4
+#endif /* HAVE_RES_INIT */
3858b4
+
3858b4
 
3858b4
 #ifdef HAVE_GETEUID
3858b4
  /*