[SSSD] [PATCH] toggle debug output of sssd_krb5_locator_plugin with an environment variable [was: [PATCH] declare hostip only in debug mode]

Sumit Bose sbose at redhat.com
Thu Sep 24 18:19:41 UTC 2009


On Thu, Sep 24, 2009 at 03:21:49PM +0200, Sumit Bose wrote:
> On Thu, Sep 24, 2009 at 09:09:04AM -0400, Simo Sorce wrote:
> > On Thu, 2009-09-24 at 14:39 +0200, Sumit Bose wrote:
> > > Hi,
> > > 
> > > this patch suppresses a compiler warning when KRB5_PLUGIN_DEBUG is not
> > > set, which is the common case.
> > 
> > Sumit,
> > would it be possible to use an env variable to control debug instead of
> > a compile time define ? That would solve it more neatly and also make it
> > simple to activate debugging at will.
> > 
> > Simo.
> > 
> 
> Thanks, very nice idea. I will set it automatically if the debug level
> of the Kerberos provider is 5 or higher and anyone is free to set it
> explicit when needed.
> 

ok, I have only implemented the explicit way so far, because the
other way might interfere with to debug_to_files scheme.

A man page for the locator plugin will be in another patch I will sent
soon.

bye,
Sumit
-------------- next part --------------
>From 1f4a55686ab0e3b9b30dccc266e422d0e6a538a9 Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose at redhat.com>
Date: Thu, 24 Sep 2009 14:28:33 +0200
Subject: [PATCH] toggle debug output of sssd_krb5_locator_plugin with an environment variable

---
 server/krb5_plugin/sssd_krb5_locator_plugin.c |   91 +++++++++++++++----------
 1 files changed, 55 insertions(+), 36 deletions(-)

diff --git a/server/krb5_plugin/sssd_krb5_locator_plugin.c b/server/krb5_plugin/sssd_krb5_locator_plugin.c
index 82ab8e9..7ccdb3f 100644
--- a/server/krb5_plugin/sssd_krb5_locator_plugin.c
+++ b/server/krb5_plugin/sssd_krb5_locator_plugin.c
@@ -17,7 +17,7 @@
     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
-
+#define _GNU_SOURCE
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -33,11 +33,40 @@
 
 #include "providers/krb5/krb5_auth.h"
 
+#define SSSD_KRB5_LOCATOR_DEBUG "SSSD_KRB5_LOCATOR_DEBUG"
+#define DEBUG_KEY "[sssd_krb5_locator] "
+#define DEBUG(body) do { \
+    if (ctx->debug) { \
+        debug_fn body; \
+    } \
+} while(0);
+
 struct sssd_ctx {
     char *sssd_realm;
     struct addrinfo *sssd_kdc_addrinfo;
+    bool debug;
 };
 
+void debug_fn(const char *format, ...)
+{
+    va_list ap;
+    char *s = NULL;
+    int ret;
+
+    va_start(ap, format);
+
+    ret = vasprintf(&s, format, ap);
+    if (ret < 0) {
+        /* ENOMEM */
+        return;
+    }
+
+    va_end(ap);
+
+    fprintf(stderr, DEBUG_KEY "%s", s);
+    free(s);
+}
+
 krb5_error_code sssd_krb5_locator_init(krb5_context context,
                                        void **private_data)
 {
@@ -45,14 +74,17 @@ krb5_error_code sssd_krb5_locator_init(krb5_context context,
     const char *dummy;
     int ret;
 
-
-#ifdef KRB5_PLUGIN_DEBUG
-    fprintf(stderr,"sssd_krb5_locator_init called\n");
-#endif
-
     ctx = calloc(1,sizeof(struct sssd_ctx));
     if (ctx == NULL) return ENOMEM;
 
+    dummy = getenv(SSSD_KRB5_LOCATOR_DEBUG);
+    if (dummy == NULL) {
+        ctx->debug = false;
+    } else {
+        ctx->debug = true;
+        DEBUG(("sssd_krb5_locator_init called\n"));
+    }
+
     dummy = getenv(SSSD_KRB5_REALM);
     if (dummy == NULL) goto failed;
     ctx->sssd_realm = strdup(dummy);
@@ -63,14 +95,10 @@ krb5_error_code sssd_krb5_locator_init(krb5_context context,
 
     ret = getaddrinfo(dummy, "kerberos", NULL, &ctx->sssd_kdc_addrinfo);
     if (ret != 0) {
-#ifdef KRB5_PLUGIN_DEBUG
-        fprintf(stderr,"getaddrinfo failed [%d][%s].\n", ret,
-                                                         gai_strerror(ret));
+        DEBUG(("getaddrinfo failed [%d][%s].\n", ret, gai_strerror(ret)));
         if (ret == EAI_SYSTEM) {
-            fprintf(stderr,"getaddrinfo failed [%d][%s].\n", errno,
-                                                             strerror(errno));
+            DEBUG(("getaddrinfo failed [%d][%s].\n", errno, strerror(errno)));
         }
-#endif
         goto failed;
     }
 
@@ -91,13 +119,11 @@ void sssd_krb5_locator_close(void *private_data)
 {
     struct sssd_ctx *ctx;
 
-#ifdef KRB5_PLUGIN_DEBUG
-    fprintf(stderr,"sssd_krb5_locator_close called\n");
-#endif
-
     if (private_data == NULL) return;
 
     ctx = (struct sssd_ctx *) private_data;
+    DEBUG(("sssd_krb5_locator_close called\n"));
+
     freeaddrinfo(ctx->sssd_kdc_addrinfo);
     free(ctx->sssd_realm);
     free(ctx);
@@ -122,11 +148,9 @@ krb5_error_code sssd_krb5_locator_lookup(void *private_data,
     if (private_data == NULL) return KRB5_PLUGIN_NO_HANDLE;
     ctx = (struct sssd_ctx *) private_data;
 
-#ifdef KRB5_PLUGIN_DEBUG
-    fprintf(stderr,"sssd_realm[%s] requested realm[%s] family[%d] "
-                   "socktype[%d] locate_service[%d]\n",
-                   ctx->sssd_realm, realm, family, socktype, svc);
-#endif
+    DEBUG(("sssd_realm[%s] requested realm[%s] family[%d] socktype[%d] "
+          "locate_service[%d]\n", ctx->sssd_realm, realm, family, socktype,
+          svc));
 
     switch (svc) {
         case locate_service_kdc:
@@ -161,33 +185,28 @@ krb5_error_code sssd_krb5_locator_lookup(void *private_data,
         return KRB5_PLUGIN_NO_HANDLE;
 
     for (ai = ctx->sssd_kdc_addrinfo; ai != NULL; ai = ai->ai_next) {
-#ifdef KRB5_PLUGIN_DEBUG
-        ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, hostip, NI_MAXHOST, NULL,
-                          0, NI_NUMERICHOST);
+        ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, hostip, NI_MAXHOST,
+                          NULL, 0, NI_NUMERICHOST);
         if (ret != 0) {
-            fprintf(stderr,"getnameinfo failed [%d][%s].\n",
-                           ret, gai_strerror(ret));
+            DEBUG(("getnameinfo failed [%d][%s].\n", ret, gai_strerror(ret)));
             if (ret == EAI_SYSTEM) {
-                fprintf(stderr,"getnameinfo failed [%d][%s].\n",
-                               errno, strerror(errno));
+                DEBUG(("getnameinfo failed [%d][%s].\n", errno, strerror(errno)));
             }
         }
-        fprintf(stderr,"addr[%s] family[%d] socktype[%d] - ",
-                hostip, ai->ai_family, ai->ai_socktype);
-#endif
+        DEBUG(("addr[%s] family[%d] socktype[%d] - ", hostip, ai->ai_family,
+                                                      ai->ai_socktype));
+
         if ((family == AF_UNSPEC || ai->ai_family == family) &&
             ai->ai_socktype == socktype) {
 
             ret = cbfunc(cbdata, socktype, ai->ai_addr);
-#ifdef KRB5_PLUGIN_DEBUG
             if (ret != 0) {
-                fprintf(stderr,"\ncbfunc failed\n");
+                DEBUG(("\ncbfunc failed\n"));
             } else {
-                fprintf(stderr,"used\n");
+                DEBUG(("used\n"));
             }
         } else {
-            fprintf(stderr," NOT used\n");
-#endif
+            DEBUG((" NOT used\n"));
         }
     }
 
-- 
1.6.2.5



More information about the sssd-devel mailing list