Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get reason for permission denied due to traversed directory not executable

I have a file /a/b that is readable by a user A. But /a does not provide executable permission by A, and thus the path /a/b cannot traverse through /a. For an arbitrarily long path, how would I determine the cause for not being able to access a given path due to an intermediate path not being accessible by the user?

like image 641
Matt Joiner Avatar asked Feb 07 '23 08:02

Matt Joiner


1 Answers

Something along like this:

#!/bin/bash
PAR=${1}
PAR=${PAR:="."}
if ! [[ "${PAR:0:1}" == / || "${PAR:0:2}" == ~[/a-z] ]]
then
  TMP=`pwd`
  PAR=$(dirname ${TMP}/${PAR})
fi

cd $PAR 2> /dev/null
if [ $? -eq 1 ]; then
  while [ ! -z "$PAR" ]; do
    PREV=$(readlink -f ${PAR})
    TMP=$(echo ${PAR}|awk -F\/ '{$NF=""}'1|tr ' ' \/)
    PAR=${TMP%/}
    cd ${PAR} 2>/dev/null
    if [ $? -eq 0 ]; then
      if [ -e ${PREV} ]; then
        ls -ld ${PREV}
      fi
      exit
    fi
  done
fi

Ugly but it would get the job done ..

So the idea is basicly that taking a parameter $1, if its not absolute directory, expand it to such and then drop the last element of the path and try to cd into it, if it fails, rince and repeat .. If it works, PREV would hold the last directory where user couldn't cd into, so print it out ..

like image 179
rasjani Avatar answered Feb 11 '23 00:02

rasjani