Blame SOURCES/0001-cups-browsed.c-Make-NotifLeaseDuration-configurable-.patch

e5b3e5
From d90a4fc57b00b4a1f6c196bcb96025251b555dd9 Mon Sep 17 00:00:00 2001
e5b3e5
From: zdohnal <zdohnal@redhat.com>
e5b3e5
Date: Fri, 18 Jun 2021 12:27:53 +0200
e5b3e5
Subject: [PATCH] cups-browsed.c: Make NotifLeaseDuration configurable and
e5b3e5
 renew after half the lease duration not 60 sec before end
e5b3e5
e5b3e5
1) NotifLeaseDuration directive for cups-browsed.conf - it will make
e5b3e5
   lease duration for notifications configurable by users. IMO it is not
e5b3e5
   useful for regular users, but it is helpful during sanity testing
e5b3e5
   (for verifying that we actually renew the subscription when time
e5b3e5
   comes). The current hardcoded 1 day is unusuable for that :( .
e5b3e5
   I implemented the lowest threshold to 300s to prevent a possible DoS.
e5b3e5
2) Subscription renewal is set to happen in the middle of NotifLeaseDuration,
e5b3e5
   not one minute before lease expiration. This was a problem on busy servers,
e5b3e5
   where cups-browsed was busy and wasn't able to renew the subscription
e5b3e5
   before cupsd removed it. Then if some jobs had come before the subscription
e5b3e5
   was created again, the queue got disabled. The proposed approach is based
e5b3e5
   on behavior of DHCP.
e5b3e5
---
e5b3e5
 utils/cups-browsed.c       | 17 +++++++++++++----
e5b3e5
 utils/cups-browsed.conf.5  | 11 +++++++++++
e5b3e5
 utils/cups-browsed.conf.in |  8 ++++++++
e5b3e5
 3 files changed, 32 insertions(+), 4 deletions(-)
e5b3e5
e5b3e5
diff --git a/utils/cups-browsed.c b/utils/cups-browsed.c
e5b3e5
index 61d6c551..2d367c59 100644
e5b3e5
--- a/utils/cups-browsed.c
e5b3e5
+++ b/utils/cups-browsed.c
e5b3e5
@@ -142,7 +142,6 @@ static int  ldap_rebind_proc(LDAP *RebindLDAPHandle,
e5b3e5
 #define TIMEOUT_REMOVE      -1
e5b3e5
 #define TIMEOUT_CHECK_LIST   2
e5b3e5
 
e5b3e5
-#define NOTIFY_LEASE_DURATION (24 * 60 * 60)
e5b3e5
 #define CUPS_DBUS_NAME "org.cups.cupsd.Notifier"
e5b3e5
 #define CUPS_DBUS_PATH "/org/cups/cupsd/Notifier"
e5b3e5
 #define CUPS_DBUS_INTERFACE "org.cups.cupsd.Notifier"
e5b3e5
@@ -508,6 +507,7 @@ static int autoshutdown_timeout = 30;
e5b3e5
 static autoshutdown_inactivity_type_t autoshutdown_on = NO_QUEUES;
e5b3e5
 static guint autoshutdown_exec_id = 0;
e5b3e5
 static const char *default_printer = NULL;
e5b3e5
+static unsigned int notify_lease_duration = 86400;
e5b3e5
 
e5b3e5
 static int debug_stderr = 0;
e5b3e5
 static int debug_logfile = 0;
e5b3e5
@@ -5017,7 +5017,7 @@ create_subscription ()
e5b3e5
   ippAddString (req, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
e5b3e5
 		"notify-recipient-uri", NULL, "dbus://");
e5b3e5
   ippAddInteger (req, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
e5b3e5
-		 "notify-lease-duration", NOTIFY_LEASE_DURATION);
e5b3e5
+		 "notify-lease-duration", notify_lease_duration);
e5b3e5
 
e5b3e5
   resp = cupsDoRequest (conn, req, "/");
e5b3e5
   if (!resp || cupsLastError() != IPP_STATUS_OK) {
e5b3e5
@@ -5060,7 +5060,7 @@ renew_subscription (int id)
e5b3e5
   ippAddString (req, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
e5b3e5
 		"notify-recipient-uri", NULL, "dbus://");
e5b3e5
   ippAddInteger (req, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
e5b3e5
-		 "notify-lease-duration", NOTIFY_LEASE_DURATION);
e5b3e5
+		 "notify-lease-duration", notify_lease_duration);
e5b3e5
 
e5b3e5
   resp = cupsDoRequest (conn, req, "/");
e5b3e5
   if (!resp || cupsLastError() != IPP_STATUS_OK) {
e5b3e5
@@ -11857,6 +11857,15 @@ read_configuration (const char *filename)
e5b3e5
       } else
e5b3e5
 	debug_printf("Invalid %s value: %d\n",
e5b3e5
 		     line, t);
e5b3e5
+    } else if (!strcasecmp(line, "NotifLeaseDuration") && value) {
e5b3e5
+      int t = atoi(value);
e5b3e5
+      if (t >= 300) {
e5b3e5
+	  notify_lease_duration = t;
e5b3e5
+	debug_printf("Set %s to %d sec.\n",
e5b3e5
+		     line, t);
e5b3e5
+      } else
e5b3e5
+	debug_printf("Invalid %s value: %d\n",
e5b3e5
+		     line, t);
e5b3e5
     } else if (!strcasecmp(line, "HttpMaxRetries") && value) {
e5b3e5
       int t = atoi(value);
e5b3e5
       if (t > 0) {
e5b3e5
@@ -12728,7 +12737,7 @@ int main(int argc, char*argv[]) {
e5b3e5
   /* Subscribe to CUPS' D-Bus notifications and create a proxy to receive
e5b3e5
      the notifications */
e5b3e5
   subscription_id = create_subscription ();
e5b3e5
-  g_timeout_add_seconds (NOTIFY_LEASE_DURATION - 60,
e5b3e5
+  g_timeout_add_seconds (notify_lease_duration / 2,
e5b3e5
 			 renew_subscription_timeout,
e5b3e5
 			 &subscription_id);
e5b3e5
   cups_notifier = cups_notifier_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
e5b3e5
diff --git a/utils/cups-browsed.conf.5 b/utils/cups-browsed.conf.5
e5b3e5
index c47f8e8d..263f0843 100644
e5b3e5
--- a/utils/cups-browsed.conf.5
e5b3e5
+++ b/utils/cups-browsed.conf.5
e5b3e5
@@ -966,6 +966,17 @@ shutdown.
e5b3e5
 
e5b3e5
 .fam T
e5b3e5
 .fi
e5b3e5
+NotifLeaseDuration defines how long the D-BUS subscription created by cups-browsed
e5b3e5
+in cupsd will last before cupsd cancels it. The default value is 1 day
e5b3e5
+in seconds - 86400. The subscription renewal is set to happen after half of
e5b3e5
+NotifLeaseDuration passed. The D-BUS notifications are used for watching over queues
e5b3e5
+and doing specific actions when a D-BUS notification comes.
e5b3e5
+.PP
e5b3e5
+.nf
e5b3e5
+.fam C
e5b3e5
+        NotifLeaseDuration 86400
e5b3e5
+.fam T
e5b3e5
+.fi
e5b3e5
 .SH SEE ALSO
e5b3e5
 
e5b3e5
 \fBcups-browsed\fP(8)
e5b3e5
diff --git a/utils/cups-browsed.conf.in b/utils/cups-browsed.conf.in
e5b3e5
index 3cc4ebb1..ffdf83d6 100644
e5b3e5
--- a/utils/cups-browsed.conf.in
e5b3e5
+++ b/utils/cups-browsed.conf.in
e5b3e5
@@ -741,3 +741,11 @@ BrowseRemoteProtocols @BROWSEREMOTEPROTOCOLS@
e5b3e5
 # on the size of the file.
e5b3e5
 
e5b3e5
 # DebugLogFileSize 300
e5b3e5
+
e5b3e5
+# NotifLeaseDuration defines how long the D-BUS subscription created by cups-browsed
e5b3e5
+# in cupsd will last before cupsd cancels it. The default value is 1 day
e5b3e5
+# in seconds - 86400. The subscription renewal is set to happen after half of
e5b3e5
+# NotifLeaseDuration passed. The D-BUS notifications are used for watching over queues
e5b3e5
+# and doing specific actions when a D-BUS notification comes.
e5b3e5
+
e5b3e5
+# NotifLeaseDuration 86400
e5b3e5
-- 
e5b3e5
2.31.1
e5b3e5