Test if a directory is writable by a given UID?

后端 未结 8 1576
一生所求
一生所求 2020-12-14 14:31

We can test if a directory is writable by the uid of the current process:

if [ -w $directory ] ; then echo \'Eureka!\' ; fi

But can anyone

8条回答
  •  失恋的感觉
    2020-12-14 15:24

    One funny possibility (but it's not bash anymore) is to make a C program with the suid flag, owned by mysql.

    Step 1.

    Create this wonderful C source file, and call it caniwrite.c (sorry, I've always sucked at choosing names):

    #define _GNU_SOURCE
    #include 
    #include 
    #include 
    
    int main(int argc,char* argv[]) {
       int i;
       for(i=1;i

    Step 2.

    Compile:

    gcc -Wall -ocaniwrite caniwrite.c
    

    Step 3.

    Move it in whatever folder you like, /usr/local/bin/ being a good choice, change it's ownership and set the suid flag: (do this as root)

    # mv -nv caniwrite /usr/local/bin
    # chown mysql:mysql /usr/local/bin/caniwrite
    # chmod +s /usr/local/bin/caniwrite
    

    Done!

    Just call it as:

    if caniwrite folder1; then
        echo "folder1 is writable"
    else
        echo "folder1 is not writable"
    fi
    

    In fact, you can call caniwrite with as many arguments as you wish. If all the directories (or files) are writable, then the return code is true, otherwise the return code is false.

提交回复
热议问题