zoukankan      html  css  js  c++  java
  • 验证用户程序是否具备指定文件的读权限

    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc,char *argv[])
    {
        if(argc != 2)
        {    
            printf("parameter error");
            exit(1);
        }
        if(access(argv[1],R_OK) < 0)
        {    
            printf("access error for %s ",argv[1]);
        }else
            printf("access ok ");
        
        if(open(argv[1],O_RDONLY) < 0)
        {    
            printf("open error for %s ",argv[1]);
        }else
            printf("open for reading ok ");
        exit(0);    
    }

    ./access /etc/shadow
    测试程序是否具备读指定文件的权限
    正常情况下用户不具备打开该文件的权限
    su 成为超级用户
    chown root access  将文件用户id改为root
    chmod u+s access 使文件在执行阶段具有文件所有者的权限(具备root权限)
    exit 退出超级用户
    再次执行发现不能验证读但是可以打开文件了,因为验证是以实际用户去验证的

  • 相关阅读:
    PHP技巧通过COM使用ADODB
    PHP开发环境安装配置全攻略
    散列表
    poj Antiprime Sequences
    HDU 2011 菜鸟杯
    UVA The ? 1 ? 2 ? ... ? n = k problem
    poj 3126 Prime Path
    uva 699 The Falling Leaves
    UVA Steps
    poj 1426 Find The Multiple
  • 原文地址:https://www.cnblogs.com/xieweiwei/p/3245977.html
Copyright © 2011-2022 走看看