• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

击试验,如果目录是可写把定的UID

用户头像
it1352
帮助1

问题说明

我们可以测试出目录是否由当前进程的UID可写的:

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

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

但是,任何人都可以提出一个方法来测试一个目录有写入一些的其他的UID?

我的情况是,我管理MySQL服务器的实例,我要临时更改慢查询日志文件的位置。我可以通过执行MySQL命令 SET GLOBAL slow_query_log_file ='$ new_log_filename做到这一点,然后禁用和放大器;启用查询日志记录,以的mysqld 开始使用该文件。

My scenario is that I am administering a MySQL Server instance, and I want to change the location of the slow-query log file temporarily. I can do this by executing a MySQL command SET GLOBAL slow_query_log_file='$new_log_filename' and then disable & enable query logging to make mysqld start using that file.

不过,我想我的脚本来检查的mysqld 进程的UID有权限创建新的日志文件。所以我想要做类似的信息(伪code):

But I'd like my script to check that the uid of the mysqld process has permissions to create that new log file. So I'd like to do something like (pseudocode):

$ if [ -w-as-mysql-uid `basename $new_log_filename` ] ; then echo 'Eureka!' ; fi

不过,当然,这是一个虚构的测试predicate的。

But of course that's an imaginary test predicate.

澄清:的我想是不依赖于苏的解决方案,因为我不能假设用户的我的脚本有苏特权。

Clarification: I would like a solution that doesn't rely on su because I can't assume the user of my script has su privilege.

正确答案

#1

下面是检查的一个长期的,迂回的方式。

Here's a long, roundabout way of checking.

USER=johndoe
DIR=/path/to/somewhere

# Use -L to get information about the target of a symlink,
# not the link itself, as pointed out in the comments
INFO=( $(stat -cL "%a %G %U" $DIR) )
PERM=${INFO[0]}
GROUP=${INFO[1]}
OWNER=${INFO[2]}

ACCESS=no
if [[ $PERM & 0002 != 0 ]]; then
    # Everyone has write access
    ACCESS=yes
elif [[ $PERM & 0020 != 0 ]]; then
    # Some group has write access.
    # Is user in that group?
    gs=( $(groups $USER) )
    for g in "${gs[@]}"; do
        if [[ $GROUP == $g ]]; then
            ACCESS=yes
            break
        fi
    done
elif [[ $PERM & 0200 != 0 ]]; then 
    # The owner has write access.
    # Does the user own the file?
    [[ $USER == $OWNER ]] && ACCESS=yes
fi

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfjakg
系列文章
更多 icon
同类精品
更多 icon
继续加载