Blame SOURCES/0001-Don-t-try-to-stat-not-on-disk-ccache-residuals.patch

5af5b2
From 7e0bcb958eb5861cb30a190dcac1e6422d65299e Mon Sep 17 00:00:00 2001
5af5b2
From: Nalin Dahyabhai <nalin@redhat.com>
5af5b2
Date: Fri, 1 Nov 2013 09:48:13 -0400
5af5b2
Subject: [PATCH 1/6] Don't try to stat() not-on-disk ccache residuals
5af5b2
5af5b2
Don't assume that ccache residual names are filenames which we can
5af5b2
stat() usefully.  Instead, use helper functions to call the library
5af5b2
routines to try to read the default principal name from caches.
5af5b2
---
5af5b2
 src/clients/ksu/ccache.c | 88 +++++++++++++++++++++++++++++-------------------
5af5b2
 src/clients/ksu/ksu.h    |  6 ++++
5af5b2
 src/clients/ksu/main.c   | 17 +++++-----
5af5b2
 3 files changed, 68 insertions(+), 43 deletions(-)
5af5b2
5af5b2
diff --git a/src/clients/ksu/ccache.c b/src/clients/ksu/ccache.c
5af5b2
index 9916c75..7917af2 100644
5af5b2
--- a/src/clients/ksu/ccache.c
5af5b2
+++ b/src/clients/ksu/ccache.c
5af5b2
@@ -60,12 +60,10 @@ krb5_error_code krb5_ccache_copy (context, cc_def, cc_other_tag,
5af5b2
 {
5af5b2
     int i=0;
5af5b2
     krb5_ccache  * cc_other;
5af5b2
-    const char * cc_def_name;
5af5b2
-    const char * cc_other_name;
5af5b2
+    const char * cc_other_type;
5af5b2
     krb5_error_code retval=0;
5af5b2
     krb5_creds ** cc_def_creds_arr = NULL;
5af5b2
     krb5_creds ** cc_other_creds_arr = NULL;
5af5b2
-    struct stat st_temp;
5af5b2
 
5af5b2
     cc_other = (krb5_ccache *)  xcalloc(1, sizeof (krb5_ccache));
5af5b2
 
5af5b2
@@ -74,10 +72,9 @@ krb5_error_code krb5_ccache_copy (context, cc_def, cc_other_tag,
5af5b2
         return retval;
5af5b2
     }
5af5b2
 
5af5b2
-    cc_def_name = krb5_cc_get_name(context, cc_def);
5af5b2
-    cc_other_name = krb5_cc_get_name(context, *cc_other);
5af5b2
+    cc_other_type = krb5_cc_get_type(context, *cc_other);
5af5b2
 
5af5b2
-    if ( ! stat(cc_def_name, &st_temp)){
5af5b2
+    if (krb5_ccache_is_initialized(context, cc_def)) {
5af5b2
         if((retval = krb5_get_nonexp_tkts(context,cc_def,&cc_def_creds_arr))){
5af5b2
             return retval;
5af5b2
         }
5af5b2
@@ -86,7 +83,8 @@ krb5_error_code krb5_ccache_copy (context, cc_def, cc_other_tag,
5af5b2
     *stored = krb5_find_princ_in_cred_list(context, cc_def_creds_arr,
5af5b2
                                            primary_principal);
5af5b2
 
5af5b2
-    if (!lstat( cc_other_name, &st_temp))
5af5b2
+    if (!krb5_cc_support_switch(context, cc_other_type) &&
5af5b2
+        krb5_ccache_name_is_initialized(context, cc_other_tag))
5af5b2
         return EINVAL;
5af5b2
 
5af5b2
     if (krb5_seteuid(0)||krb5_seteuid(target_uid)) {
5af5b2
@@ -533,24 +531,18 @@ krb5_error_code krb5_ccache_overwrite(context, ccs, cct, primary_principal)
5af5b2
     krb5_ccache cct;
5af5b2
     krb5_principal primary_principal;
5af5b2
 {
5af5b2
-    const char * cct_name;
5af5b2
-    const char * ccs_name;
5af5b2
     krb5_error_code retval=0;
5af5b2
     krb5_principal temp_principal;
5af5b2
     krb5_creds ** ccs_creds_arr = NULL;
5af5b2
     int i=0;
5af5b2
-    struct stat st_temp;
5af5b2
 
5af5b2
-    ccs_name = krb5_cc_get_name(context, ccs);
5af5b2
-    cct_name = krb5_cc_get_name(context, cct);
5af5b2
-
5af5b2
-    if ( ! stat(ccs_name, &st_temp)){
5af5b2
+    if (krb5_ccache_is_initialized(context, ccs)) {
5af5b2
         if ((retval = krb5_get_nonexp_tkts(context,  ccs, &ccs_creds_arr))){
5af5b2
             return retval;
5af5b2
         }
5af5b2
     }
5af5b2
 
5af5b2
-    if ( ! stat(cct_name, &st_temp)){
5af5b2
+    if (krb5_ccache_is_initialized(context, cct)) {
5af5b2
         if ((retval = krb5_cc_get_principal(context, cct, &temp_principal))){
5af5b2
             return retval;
5af5b2
         }
5af5b2
@@ -649,12 +641,10 @@ krb5_error_code krb5_ccache_copy_restricted (context, cc_def, cc_other_tag,
5af5b2
 
5af5b2
     int i=0;
5af5b2
     krb5_ccache  * cc_other;
5af5b2
-    const char * cc_def_name;
5af5b2
-    const char * cc_other_name;
5af5b2
+    const char * cc_other_type;
5af5b2
     krb5_error_code retval=0;
5af5b2
     krb5_creds ** cc_def_creds_arr = NULL;
5af5b2
     krb5_creds ** cc_other_creds_arr = NULL;
5af5b2
-    struct stat st_temp;
5af5b2
 
5af5b2
     cc_other = (krb5_ccache *)  xcalloc(1, sizeof (krb5_ccache));
5af5b2
 
5af5b2
@@ -663,19 +653,17 @@ krb5_error_code krb5_ccache_copy_restricted (context, cc_def, cc_other_tag,
5af5b2
         return retval;
5af5b2
     }
5af5b2
 
5af5b2
-    cc_def_name = krb5_cc_get_name(context, cc_def);
5af5b2
-    cc_other_name = krb5_cc_get_name(context, *cc_other);
5af5b2
+    cc_other_type = krb5_cc_get_type(context, *cc_other);
5af5b2
 
5af5b2
-    if ( ! stat(cc_def_name, &st_temp)){
5af5b2
-        if((retval = krb5_get_nonexp_tkts(context,cc_def,&cc_def_creds_arr))){
5af5b2
+    if (krb5_ccache_is_initialized(context, cc_def)) {
5af5b2
+        retval = krb5_get_nonexp_tkts(context, cc_def, &cc_def_creds_arr);
5af5b2
+        if (retval)
5af5b2
             return retval;
5af5b2
-        }
5af5b2
-
5af5b2
     }
5af5b2
 
5af5b2
-    if (!lstat( cc_other_name, &st_temp)) {
5af5b2
+    if (!krb5_cc_support_switch(context, cc_other_type) &&
5af5b2
+        krb5_ccache_name_is_initialized(context, cc_other_tag))
5af5b2
         return EINVAL;
5af5b2
-    }
5af5b2
 
5af5b2
     if (krb5_seteuid(0)||krb5_seteuid(target_uid)) {
5af5b2
         return errno;
5af5b2
@@ -723,12 +711,10 @@ krb5_error_code krb5_ccache_filter (context, cc, prst)
5af5b2
     krb5_creds ** cc_creds_arr = NULL;
5af5b2
     const char * cc_name;
5af5b2
     krb5_boolean stored;
5af5b2
-    struct stat st_temp;
5af5b2
 
5af5b2
     cc_name = krb5_cc_get_name(context, cc);
5af5b2
 
5af5b2
-    if ( ! stat(cc_name, &st_temp)){
5af5b2
-
5af5b2
+    if (krb5_ccache_is_initialized(context, cc)) {
5af5b2
         if (auth_debug) {
5af5b2
             fprintf(stderr,"putting cache %s through a filter for -z option\n",                     cc_name);
5af5b2
         }
5af5b2
@@ -793,12 +779,8 @@ krb5_error_code  krb5_find_princ_in_cache (context, cc, princ, found)
5af5b2
 {
5af5b2
     krb5_error_code retval;
5af5b2
     krb5_creds ** creds_list = NULL;
5af5b2
-    const char * cc_name;
5af5b2
-    struct stat st_temp;
5af5b2
 
5af5b2
-    cc_name = krb5_cc_get_name(context, cc);
5af5b2
-
5af5b2
-    if ( ! stat(cc_name, &st_temp)){
5af5b2
+    if (krb5_ccache_is_initialized(context, cc)) {
5af5b2
         if ((retval = krb5_get_nonexp_tkts(context, cc, &creds_list))){
5af5b2
             return retval;
5af5b2
         }
5af5b2
@@ -807,3 +789,41 @@ krb5_error_code  krb5_find_princ_in_cache (context, cc, princ, found)
5af5b2
     *found = krb5_find_princ_in_cred_list(context, creds_list, princ);
5af5b2
     return 0;
5af5b2
 }
5af5b2
+
5af5b2
+extern krb5_boolean
5af5b2
+krb5_ccache_name_is_initialized(krb5_context context, const char *cctag)
5af5b2
+{
5af5b2
+    krb5_error_code retval = 0;
5af5b2
+    krb5_ccache cc;
5af5b2
+    krb5_principal princ;
5af5b2
+
5af5b2
+    retval = krb5_cc_resolve(context, cctag, &cc);
5af5b2
+    if (retval)
5af5b2
+        return FALSE;
5af5b2
+
5af5b2
+    retval = krb5_cc_get_principal(context, cc, &princ);
5af5b2
+    if (retval == 0)
5af5b2
+        krb5_free_principal(context, princ);
5af5b2
+    krb5_cc_close(context, cc);
5af5b2
+
5af5b2
+    return retval == 0;
5af5b2
+}
5af5b2
+
5af5b2
+extern krb5_boolean
5af5b2
+krb5_ccache_is_initialized(krb5_context context, krb5_ccache def_cc)
5af5b2
+{
5af5b2
+    krb5_error_code retval = 0;
5af5b2
+    krb5_boolean result;
5af5b2
+    char *def_cc_name;
5af5b2
+
5af5b2
+    if (def_cc == NULL)
5af5b2
+        return FALSE;
5af5b2
+
5af5b2
+    retval = krb5_cc_get_full_name(context, def_cc, &def_cc_name);
5af5b2
+    if (retval)
5af5b2
+        return FALSE;
5af5b2
+
5af5b2
+    result = krb5_ccache_name_is_initialized(context, def_cc_name);
5af5b2
+    krb5_free_string(context, def_cc_name);
5af5b2
+    return result;
5af5b2
+}
5af5b2
diff --git a/src/clients/ksu/ksu.h b/src/clients/ksu/ksu.h
5af5b2
index f2c0811..2a63c21 100644
5af5b2
--- a/src/clients/ksu/ksu.h
5af5b2
+++ b/src/clients/ksu/ksu.h
5af5b2
@@ -141,6 +141,12 @@ extern krb5_error_code krb5_store_some_creds
5af5b2
 (krb5_context, krb5_ccache, krb5_creds **, krb5_creds **,
5af5b2
  krb5_principal, krb5_boolean *);
5af5b2
 
5af5b2
+extern krb5_boolean krb5_ccache_name_is_initialized
5af5b2
+(krb5_context, const char *);
5af5b2
+
5af5b2
+extern krb5_boolean krb5_ccache_is_initialized
5af5b2
+(krb5_context, krb5_ccache);
5af5b2
+
5af5b2
 extern krb5_error_code krb5_ccache_copy_restricted
5af5b2
 (krb5_context, krb5_ccache, char *, krb5_principal,
5af5b2
  krb5_ccache *, krb5_boolean *, uid_t);
5af5b2
diff --git a/src/clients/ksu/main.c b/src/clients/ksu/main.c
5af5b2
index 233eb52..e2ca06a 100644
5af5b2
--- a/src/clients/ksu/main.c
5af5b2
+++ b/src/clients/ksu/main.c
5af5b2
@@ -112,7 +112,6 @@ main (argc, argv)
5af5b2
     extern char * getpass(), *crypt();
5af5b2
     int pargc;
5af5b2
     char ** pargv;
5af5b2
-    struct stat  st_temp;
5af5b2
     krb5_boolean stored = FALSE;
5af5b2
     krb5_principal  kdc_server;
5af5b2
     krb5_boolean zero_password;
5af5b2
@@ -265,9 +264,10 @@ main (argc, argv)
5af5b2
                 if ( strchr(cc_source_tag, ':')){
5af5b2
                     cc_source_tag_tmp = strchr(cc_source_tag, ':') + 1;
5af5b2
 
5af5b2
-                    if( stat( cc_source_tag_tmp, &st_temp)){
5af5b2
+                    if (!krb5_ccache_name_is_initialized(ksu_context,
5af5b2
+                                                         cc_source_tag)) {
5af5b2
                         com_err(prog_name, errno,
5af5b2
-                                _("while looking for credentials file %s"),
5af5b2
+                                _("while looking for credentials cache %s"),
5af5b2
                                 cc_source_tag_tmp);
5af5b2
                         exit (1);
5af5b2
                     }
5af5b2
@@ -432,7 +432,8 @@ main (argc, argv)
5af5b2
                      (long) target_uid, gen_sym());
5af5b2
             cc_target_tag_tmp = strchr(cc_target_tag, ':') + 1;
5af5b2
 
5af5b2
-        }while ( !stat ( cc_target_tag_tmp, &st_temp));
5af5b2
+        } while (krb5_ccache_name_is_initialized(ksu_context,
5af5b2
+                                                 cc_target_tag));
5af5b2
     }
5af5b2
 
5af5b2
 
5af5b2
@@ -884,8 +885,6 @@ static void sweep_up(context, cc)
5af5b2
     krb5_ccache cc;
5af5b2
 {
5af5b2
     krb5_error_code retval;
5af5b2
-    const char * cc_name;
5af5b2
-    struct stat  st_temp;
5af5b2
 
5af5b2
     krb5_seteuid(0);
5af5b2
     if (krb5_seteuid(target_uid) < 0) {
5af5b2
@@ -894,9 +893,9 @@ static void sweep_up(context, cc)
5af5b2
         exit(1);
5af5b2
     }
5af5b2
 
5af5b2
-    cc_name = krb5_cc_get_name(context, cc);
5af5b2
-    if ( ! stat(cc_name, &st_temp)){
5af5b2
-        if ((retval = krb5_cc_destroy(context, cc)))
5af5b2
+    if (krb5_ccache_is_initialized(context, cc)) {
5af5b2
+        retval = krb5_cc_destroy(context, cc);
5af5b2
+        if (retval)
5af5b2
             com_err(prog_name, retval, _("while destroying cache"));
5af5b2
     }
5af5b2
 }
5af5b2
-- 
5af5b2
1.8.4.2
5af5b2