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

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