程序删除其他用户的文件 - 权限被拒绝

时间:2021-08-05 21:52:48

I have two Users. User1 is running a program which tries to delete a file from user2. But my program always return me "permission denied".

我有两个用户。 User1正在运行尝试从user2删除文件的程序。但是我的程序总是给我“拒绝许可”。

When I try to delete the file myself as user1 with the rm command there is no problem. The permission of the files are 775 and my user1 is in the group of user2. This group is also the owner of the files. The permission of the directory in which the files are is 775 too.

当我尝试使用rm命令将我自己的文件删除为user1时没有问题。文件的权限是775,我的user1在user2的组中。该组也是文件的所有者。文件所在目录的权限也是775。

For removing the file the program I have written uses the "remove" function from c/c++. Does anyone have a solution or idea ?

为了删除文件,我编写的程序使用c / c ++中的“remove”函数。有没有人有解决方案或想法?

I have asked this question on unix.stackexchange.com before. They have sent me here.

我之前在unix.stackexchange.com上问了这个问题。他们把我送到了这里。

Here is my code:

这是我的代码:

    void deleteFile()
{

    if(0 != remove("File1.txt"))
        cout<<"Error deleting File: "<<strerror(errno)<<endl;
    if(0 != remove("File2.txt"))
        cout<<"Error deleting File: "<<strerror(errno)<<endl;
}

i have renamed the files but i know the original paths are correct. i have already tested this

我已重命名文件,但我知道原始路径是正确的。我已经测试了这个

more information: ok i have runnned the program as user2 and the files have been deleted without any problems.

更多信息:确定我已经将程序作为user2运行并且文件已被删除而没有任何问题。

groups user1 users user2

组user1用户user2

groups user2 user2 adm www-data plugdev users ftp vsftpd

groups user2 user2 adm www-data plugdev users ftp vsftpd

ls -lah drwxrwxr-x 7 user2 user2 4.0K Nov 27 14:13 . drwxrw-r-x 4 user2 user2 4.0K Nov 11 12:34 .. -rwxrwxr-x 1 user2 user2 50 Nov 12 15:12 File1.txt -rwxrwxr-x 1 user2 user2 826 Nov 27 14:13 File2.txt

ls -lah drwxrwxr-x 7 user2 user2 4.0K 11月27日14:13。 drwxrw-r-x 4 user2 user2 4.0K Nov 11 12:34 .. -rwxrwxr-x 1 user2 user2 50 Nov 12 15:12 File1.txt -rwxrwxr-x 1 user2 user2 826 Nov 27 14:13 File2.txt

2 个解决方案

#1


0  

I did a test, below, which demonstrates that to delete a file, a user needs to have write permission for the directory which contains the file. According to your output from "ls -l" your user2 should be permitted to delete (remove) the file.

我在下面做了一个测试,它表明要删除文件,用户需要拥有包含该文件的目录的写权限。根据“ls -l”的输出,您的user2应该被允许删除(删除)该文件。

Your code example does not compile. Can you provide a complete code example that demonstrates your problem? The simple program below behaves as expected. You might try your test with this simpler example:

您的代码示例无法编译。您能提供一个完整的代码示例来演示您的问题吗?下面的简单程序表现如预期。您可以尝试使用这个更简单的示例进行测试:

#include <stdio.h>

    int main()
{

    printf("result: %i\n", remove("/home/user1/delete_me"));
}

I think your problem is not related to file permissions.

我认为您的问题与文件权限无关。

newf:~# groups user1
user1 : user1
newf:~# groups user2
user2 : user2 user1
newf:~# su - user1
$ date > delete_me
$ chmod g+w delete_me
$ exit
newf:~# su - user2
$ ls -la ~user1
total 12
drwxr-xr-x 2 user1 user1 4096 Nov 28 13:27 .
drwxr-xr-x 8 root  root  4096 Nov 28 13:19 ..
-rw-rw-r-- 1 user1 user1   29 Nov 28 13:28 delete_me
$ date >> ~user1/delete_me
$ cat ~user1/delete_me
Fri Nov 28 13:28:44 EST 2014
Fri Nov 28 13:31:39 EST 2014
$ rm ~user1/delete_me
rm: cannot remove ‘/home/user1/delete_me’: Permission denied
$ exit
newf:~# su - user1
$ chmod g+w .
$ exit
newf:~# su - user2
$ ls -la ~user1 
total 12
drwxrwxr-x 2 user1 user1 4096 Nov 28 13:27 .
drwxr-xr-x 8 root  root  4096 Nov 28 13:19 ..
-rw-rw-r-- 1 user1 user1   58 Nov 28 13:31 delete_me
$ rm ~user1/delete_me
$ ls -la ~user1
total 8
drwxrwxr-x 2 user1 user1 4096 Nov 28 13:32 .
drwxr-xr-x 8 root  root  4096 Nov 28 13:19 ..

#2


-3  

You are not using remove correctly. It is simply:

您没有正确使用删除。它很简单:

remove("File1.txt")

instead of

remove(("File1.txt").c_str())

which when compiling results in the error:

在编译结果时出错:

error: request for member ‘c_str’ in ‘("File1.txt")’, which is of non-class type ‘const char [10]’

For error checking:

用于错误检查:

if(remove("File1.txt") == -1)
     error occurred

#1


0  

I did a test, below, which demonstrates that to delete a file, a user needs to have write permission for the directory which contains the file. According to your output from "ls -l" your user2 should be permitted to delete (remove) the file.

我在下面做了一个测试,它表明要删除文件,用户需要拥有包含该文件的目录的写权限。根据“ls -l”的输出,您的user2应该被允许删除(删除)该文件。

Your code example does not compile. Can you provide a complete code example that demonstrates your problem? The simple program below behaves as expected. You might try your test with this simpler example:

您的代码示例无法编译。您能提供一个完整的代码示例来演示您的问题吗?下面的简单程序表现如预期。您可以尝试使用这个更简单的示例进行测试:

#include <stdio.h>

    int main()
{

    printf("result: %i\n", remove("/home/user1/delete_me"));
}

I think your problem is not related to file permissions.

我认为您的问题与文件权限无关。

newf:~# groups user1
user1 : user1
newf:~# groups user2
user2 : user2 user1
newf:~# su - user1
$ date > delete_me
$ chmod g+w delete_me
$ exit
newf:~# su - user2
$ ls -la ~user1
total 12
drwxr-xr-x 2 user1 user1 4096 Nov 28 13:27 .
drwxr-xr-x 8 root  root  4096 Nov 28 13:19 ..
-rw-rw-r-- 1 user1 user1   29 Nov 28 13:28 delete_me
$ date >> ~user1/delete_me
$ cat ~user1/delete_me
Fri Nov 28 13:28:44 EST 2014
Fri Nov 28 13:31:39 EST 2014
$ rm ~user1/delete_me
rm: cannot remove ‘/home/user1/delete_me’: Permission denied
$ exit
newf:~# su - user1
$ chmod g+w .
$ exit
newf:~# su - user2
$ ls -la ~user1 
total 12
drwxrwxr-x 2 user1 user1 4096 Nov 28 13:27 .
drwxr-xr-x 8 root  root  4096 Nov 28 13:19 ..
-rw-rw-r-- 1 user1 user1   58 Nov 28 13:31 delete_me
$ rm ~user1/delete_me
$ ls -la ~user1
total 8
drwxrwxr-x 2 user1 user1 4096 Nov 28 13:32 .
drwxr-xr-x 8 root  root  4096 Nov 28 13:19 ..

#2


-3  

You are not using remove correctly. It is simply:

您没有正确使用删除。它很简单:

remove("File1.txt")

instead of

remove(("File1.txt").c_str())

which when compiling results in the error:

在编译结果时出错:

error: request for member ‘c_str’ in ‘("File1.txt")’, which is of non-class type ‘const char [10]’

For error checking:

用于错误检查:

if(remove("File1.txt") == -1)
     error occurred