From e04d647d8e9f1450cdb461b3af4aa14f1d589e24 Mon Sep 17 00:00:00 2001
From: Ander Punnar <ander@kvlt.ee>
Date: Tue, 16 Apr 2019 18:09:47 +0300
Subject: [PATCH] __acl: fix always check first if path exists

---
 cdist/conf/type/__acl/explorer/acl_is      |  6 ++----
 cdist/conf/type/__acl/explorer/file_exists | 24 ++++++++++++++++++++++
 cdist/conf/type/__acl/explorer/is_dir      |  6 ++----
 cdist/conf/type/__acl/gencode-remote       |  6 +++---
 4 files changed, 31 insertions(+), 11 deletions(-)
 create mode 100755 cdist/conf/type/__acl/explorer/file_exists

diff --git a/cdist/conf/type/__acl/explorer/acl_is b/cdist/conf/type/__acl/explorer/acl_is
index f75f4003..89da89f1 100755
--- a/cdist/conf/type/__acl/explorer/acl_is
+++ b/cdist/conf/type/__acl/explorer/acl_is
@@ -18,9 +18,7 @@
 # along with cdist. If not, see <http://www.gnu.org/licenses/>.
 #
 
-acl_path="/$__object_id"
-
-[ ! -e "$acl_path" ] && exit 0
+[ ! -e "/$__object_id" ] && exit 0
 
 if ! command -v getfacl > /dev/null
 then
@@ -28,6 +26,6 @@ then
     exit 1
 fi
 
-getfacl "$acl_path" 2>/dev/null \
+getfacl "/$__object_id" 2>/dev/null \
     | grep -E '^((default:)?(user|group):[^:]|(default:)?mask::)' \
     || true
diff --git a/cdist/conf/type/__acl/explorer/file_exists b/cdist/conf/type/__acl/explorer/file_exists
new file mode 100755
index 00000000..998d407c
--- /dev/null
+++ b/cdist/conf/type/__acl/explorer/file_exists
@@ -0,0 +1,24 @@
+#!/bin/sh -e
+#
+# 2018 Ander Punnar (ander-at-kvlt-dot-ee)
+#
+# This file is part of cdist.
+#
+# cdist is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# cdist is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with cdist. If not, see <http://www.gnu.org/licenses/>.
+#
+
+if [ -e "/$__object_id" ]
+then echo 1
+else echo 0
+fi
diff --git a/cdist/conf/type/__acl/explorer/is_dir b/cdist/conf/type/__acl/explorer/is_dir
index d3080de6..7c4e2538 100755
--- a/cdist/conf/type/__acl/explorer/is_dir
+++ b/cdist/conf/type/__acl/explorer/is_dir
@@ -18,11 +18,9 @@
 # along with cdist. If not, see <http://www.gnu.org/licenses/>.
 #
 
-acl_path="/$__object_id"
+[ ! -e "/$__object_id" ] && exit 0
 
-[ ! -e "$acl_path" ] && exit 0
-
-if [ -d "$acl_path" ]
+if [ -d "/$__object_id" ]
 then echo 1
 else echo 0
 fi
diff --git a/cdist/conf/type/__acl/gencode-remote b/cdist/conf/type/__acl/gencode-remote
index 7003c26f..91fb7117 100755
--- a/cdist/conf/type/__acl/gencode-remote
+++ b/cdist/conf/type/__acl/gencode-remote
@@ -18,9 +18,7 @@
 # along with cdist. If not, see <http://www.gnu.org/licenses/>.
 #
 
-acl_path="/$__object_id"
-
-[ ! -e "$acl_path" ] && exit 0
+[ "$( cat "$__object/explorer/file_exists" )" = '0' ] && exit 0
 
 os="$( cat "$__global/explorer/os" )"
 
@@ -28,6 +26,8 @@ is_dir="$( cat "$__object/explorer/is_dir" )"
 
 acl_is="$( cat "$__object/explorer/acl_is" )"
 
+acl_path="/$__object_id"
+
 acl_should="$( for parameter in user group
 do
     if [ ! -f "$__object/parameter/$parameter" ]