Program deleting file of other user - permission denied - c++

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".
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.
For removing the file the program I have written uses the "remove" function from c/c++.
Does anyone have a solution or idea ?
I have asked this question on unix.stackexchange.com before. They have sent me here.
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.
groups user1
users user2
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

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.
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 ..

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

Related

When attemping to copy a dir from repo to container in workflow, it's as if COPY command did nothing

My repo:
/
dbt-action/
action.yml
Dockerfile
entrypoint.sh
dbt/
profiles.yml
My workflow step:
- name: Run DBT
uses: ./dbt-action
My Dockerfile:
FROM ghcr.io/dbt-labs/dbt-redshift:1.3.latest
COPY dbt .dbt
COPY entrypoint.sh /entrypoint.sh
My entrypoint:
!/bin/bash
pwd
ls -la
Outputs the following:
drwxr-xr-x 6 1001 123 4096 Jan 7 13:06 .
drwxr-xr-x 6 root root 4096 Jan 7 13:06 ..
drwxr-xr-x 8 1001 123 4096 Jan 7 13:06 .git
drwxr-xr-x 3 1001 123 4096 Jan 7 13:06 .github
drwxr-xr-x 3 1001 123 4096 Jan 7 13:06 blah
-rw-r--r-- 1 1001 123 1744 Jan 7 13:06 README.md
drwxr-xr-x 3 1001 123 4096 Jan 7 13:06 dbt-action
Expected output:
Same as above but with additional directory .dbt coming from COPY dbt .dbt in my Dockerfile.
Why don't I see dir .dbt when I ls -la in my entrypoint?
Seems like you are executing your ‘Docker build’ from the wrong working directory, since the ‘dbt-action’ folder is present, but not it contents. Can you double check the PWD before you build?

Privileges problem UMASK Nautilus. CentOS 7.7

I have a problem with the privileges of [files/folders] created by Nautilus. As a test I set the UMASK of my specific user to 0000:
[simone#MYPC:~] >cat /mnt/home/simone/.bashrc | grep mask
# User Umask Override
umask 0000
[simone#MYPC:~] >cat /mnt/home/simone/.bashrc
# $HOME/.bashrc
umask 0000
[simone#MYPC:~] >umask
000
When I write a file or folder passing from terminal I can do it with the required privileges:
[simone#MYPC:~/Desktop] >touch file1.txt
[simone#MYPC:~/Desktop] >mkdir Folder1
[simone#MYPC:~/Desktop] >ls -la
-rw-rw-rw-. 1 simone home 0 Mar 21 2022 file1.txt
drwxrwxrwx. 2 simone home 4096 Mar 21 2022 Folder1
But when I create a file (TextEditor) or folder through Nautilus, I get different privileges:
[simone#MYPC:~] >ls -lart /tmp
drwxr-xr-x. 1 simone home 0 Mar 18 10:04 FolderByNautilus
[simone#MYPC:~] > ls -la /tmp/fileNautilus.txt
-rw-r--r--. 1 simone home 5 Mar 11 2022 /tmp/fileNautilus.txt
I would like Nautilus or text editor to write with the mask 0000 leaving the same privileges with which my terminal writes

Invalid value for "path" parameter: no file exists at

As mentioned at Terraform Resource: Connection Error while executing apply
I changed my code to the the below
provisioner "remote-exec" {
connection {
type = "ssh"
host = aws_eip.nat-eip.public_ip
user = "ubuntu"
private_key = file("/id_rsa.pem")
}
inline = [
"chmod +x /tmp/start_node.sh",
"sudo sed -i -e 's/\r$//' /tmp/start_node.sh", # Remove the spurious CR characters.
"sudo /tmp/start_node.sh",
]
}
But I still get the same error
Error: Invalid function argument
on explorer.tf line 60, in resource "aws_instance" "explorer":
60: private_key = file("/id_rsa.pem")
Invalid value for "path" parameter: no file exists at /id_rsa.pem;
this function works only with files that are distributed as part of the
configuration source code, so if this file will be created by a resource in
this configuration you must instead obtain this result from an attribute of
that resource.
ls -la ooutput
total 156
drwxr-xr-x 10 CORP\mayuresh CORP\domain users 4096 Jan 12 14:29 .
drwxr-xr-x 16 CORP\mayuresh CORP\domain users 4096 Jan 10 13:10 ..
drwxr-xr-x 12 CORP\mayuresh CORP\domain users 4096 Jan 12 09:49 byoc-terraform
drwxr-xr-x 2 CORP\mayuresh CORP\domain users 4096 Jan 11 11:57 controllers
-rw-r--r-- 1 CORP\mayuresh CORP\domain users 188 Jan 10 13:27 .env
-rw-r--r-- 1 CORP\mayuresh CORP\domain users 1582 Jan 10 17:12 fetchUserData.js
drwxr-xr-x 9 CORP\mayuresh CORP\domain users 4096 Jan 12 13:14 .git
-rw-r--r-- 1 CORP\mayuresh CORP\domain users 629 Jan 10 13:27 .gitignore
-rw-r--r-- 1 CORP\mayuresh CORP\domain users 107 Dec 30 06:49 .gitmodules
-rw-r--r-- 1 CORP\mayuresh CORP\domain users 1765 Jan 12 13:21 id_rsa.pem
-rw-r--r-- 1 CORP\mayuresh CORP\domain users 1488 Jan 10 13:27 index.js
drwxr-xr-x 3 CORP\mayuresh CORP\domain users 4096 Jan 10 13:27 models
drwxr-xr-x 221 CORP\mayuresh CORP\domain users 12288 Jan 10 13:30 node_modules
-rw-r--r-- 1 CORP\mayuresh CORP\domain users 1058 Jan 10 13:27 package.json
-rw-r--r-- 1 CORP\mayuresh CORP\domain users 78791 Jan 10 13:27 package-lock.json
drwxr-xr-x 2 CORP\mayuresh CORP\domain users 4096 Jan 10 13:27 routes
drwxr-xr-x 2 CORP\mayuresh CORP\domain users 4096 Jan 10 17:01 utils
drwxr-xr-x 2 CORP\mayuresh CORP\domain users 4096 Jan 10 13:27 VMCreationFiles```
Have you tried using the full path? Especially beneficial if you are using modules.
I.E:
private_key = file("${path.module}/id_rsa.pem")
Or I think even this will work
private_key = file("./id_rsa.pem")
I believe your existing code is looking for the file at the root of your filesystem.
Your path to the .pem is wrong. It looks like the file exists in your $HOME directory.
You can provide the absolute path of the id_rsa.pem file if that file is outside of path.module, path.root, path.cwd
To provide the absolute path
Fetch the full path of the file How to get full path of a file?
Paste the path in:
provisioner "remote-exec" {
connection {
type = "ssh"
host = aws_eip.nat-eip.public_ip
user = "ubuntu"
private_key = file("<Absolute path to .pem file e.g /home/ubuntu/id_rsa.pem>")
}

pip install ansible directory owner error

When trying to sudo pip install ansible on mac OS 10.12 I get the error:
The directory '/Users/myuser/Library/Caches/pip/http' or its
parent directory is not owned by the current user and the cache has
been disabled. Please check the permissions and owner of that
directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/myuser/Library/Caches/pip' or its parent
directory is not owned by the current user and caching wheels has been
disabled. check the permissions and owner of that directory. If
executing pip with sudo, you may want sudo's -H flag.
I checked the owner of the current and listed directories using ls -l and they are all the same. (The first ls is the parent directory of website where I am trying to run the install)
My-Mac:Artivest myuser$ ls -l
total 0
drwxr-xr-x 9 myuser staff 306 Jul 23 11:12 developer_tools
drwxr-xr-x 2 myuser staff 68 Jul 23 11:32 modules
drwxr-xr-x 3 myuser staff 102 Jul 23 11:29 virtualbox-images
drwxr-xr-x 54 myuser staff 1836 Jul 23 11:48 website
My-Mac:Artivest myuser$ cd /Users/myuser/Library/Caches/pip
My-Mac:pip myuser$ ls -l
total 8
drwx------ 8 myuser staff 272 Jul 23 12:20 http
-rw-r--r-- 1 myuser staff 113 Jul 23 11:00 selfcheck.json
My-Mac:pip myuser$ cd /Users/myuser/Library/Caches/pip/http
My-Mac:http myuser$ ls -l
total 0
drwx------ 3 myuser staff 102 Jul 23 12:20 1
drwx------ 3 myuser staff 102 Jul 23 11:00 9
drwx------ 5 myuser staff 170 Jul 23 12:20 a
drwx------ 3 myuser staff 102 Jul 23 11:00 b
drwx------ 3 myuser staff 102 Jul 23 12:20 e
drwx------ 3 myuser staff 102 Jul 23 11:00 f
My-Mac:http myuser$
How can I get past this error?
First I would suggest installing python using brew, then you could try:
$ pip install --upgrade --user ansible
This will install ansible within your $HOME if using python 3 it will be in /Library/Python/3.7/bin or if using python 2 in ~/Library/Python/2.7/bin

Change file extensions in gradle

This seems so simple, yet I cannot find a single source that provides a working solution.
I have a project like this:
src
-main
-resources
-bpmns
-nnn.bpmn
and I want gradle to rename all files that end with bpmn to bpmn20.xml, leaving all files that already have bpmn20.xml extensions untouched.
This is my approach:
task renameBPMN(type: Copy) {
outputs.upToDateWhen { false } //to pevent gradle from not executing this task
from('src/main/resources/bpmn/')
into('src/main/resources/bpmn/')
rename ('/^.*\\.(bpmn)$/', '$1.bpmn20.xml')
}
According to some regex testers online, the regex should match nnn.bpmn, gradle however does nothing at all.
Any ideas anyone?
This is what he needs or actually this script would do it.
Here it'll change a file: filenamebpmn or filename.bpmn to filenamebpmn20.xml or filename.bpmn20.xml respectively. It'll ignore all other files whether it's a filenamebpmn.xml or filename.bpmn.xml or any other file.
$ cat build.gradle
task renameBPMN() << {
fileTree("bpmnfolder").matching { include "*bpmn" }.each { fileName ->
println "Will be changed to new name --- " + fileName
file("${fileName}").renameTo("${fileName}20.xml")
}
}
$ ls -l bpmnfolder
total 3
-rw-r--r-- 1 c400093 Domain Users 3 Aug 6 15:14 1bpmn
-rw-r--r-- 1 c400093 Domain Users 3 Aug 6 14:50 2bpmn
-rw-r--r-- 1 c400093 Domain Users 6 Aug 6 14:50 3bpmn.xml
$ /cygdrive/c/gradle-2.0/bin/gradle renameBPMN
:renameBPMN
Will be changed to new name --- C:\cygwin\home\c400093\giga\goga\giga\bpmnfolder\1bpmn
Will be changed to new name --- C:\cygwin\home\c400093\giga\goga\giga\bpmnfolder\2bpmn
BUILD SUCCESSFUL
Total time: 3.153 secs
$ ls -l bpmnfolder
total 3
-rw-r--r-- 1 c400093 Domain Users 3 Aug 6 15:14 1bpmn20.xml
-rw-r--r-- 1 c400093 Domain Users 3 Aug 6 14:50 2bpmn20.xml
-rw-r--r-- 1 c400093 Domain Users 6 Aug 6 14:50 3bpmn.xml
$
AND
If you want to include filenames which end with bpmn as extension i.e. filename.bpmn or filenamebpmn, then use the following code.
$ ls -l bpmnfolder
total 5
-rw-r--r-- 1 c400093 Domain Users 3 Aug 6 15:39 1bpmn
-rw-r--r-- 1 c400093 Domain Users 3 Aug 6 15:39 2bpmn
-rw-r--r-- 1 c400093 Domain Users 6 Aug 6 15:39 3bpmn.xml
-rw-r--r-- 1 c400093 Domain Users 8 Aug 6 15:39 4.bpmn
-rw-r--r-- 1 c400093 Domain Users 3 Aug 6 15:39 5.bpmn.xml
$ cat build.gradle
task renameBPMN() << {
fileTree("bpmnfolder").matching { include "*bpmn" include "*.bpmn" }.each { fileName ->
println "Will be changed to new name --- " + fileName
file("${fileName}").renameTo("${fileName}20.xml")
}
}
$ /cygdrive/c/gradle-2.0/bin/gradle renameBPMN
:renameBPMN
Will be changed to new name --- C:\cygwin\home\c400093\giga\goga\giga\bpmnfolder\1bpmn
Will be changed to new name --- C:\cygwin\home\c400093\giga\goga\giga\bpmnfolder\2bpmn
Will be changed to new name --- C:\cygwin\home\c400093\giga\goga\giga\bpmnfolder\4.bpmn
BUILD SUCCESSFUL
Total time: 4.271 secs
$ ls -l bpmnfolder
total 5
-rw-r--r-- 1 c400093 Domain Users 3 Aug 6 15:39 1bpmn20.xml
-rw-r--r-- 1 c400093 Domain Users 3 Aug 6 15:39 2bpmn20.xml
-rw-r--r-- 1 c400093 Domain Users 6 Aug 6 15:39 3bpmn.xml
-rw-r--r-- 1 c400093 Domain Users 8 Aug 6 15:39 4.bpmn20.xml
-rw-r--r-- 1 c400093 Domain Users 3 Aug 6 15:39 5.bpmn.xml
$
Modify your task to:
task rename(type: Copy) {
outputs.upToDateWhen { false } //to pevent gradle from not executing this task
from('src/main/resources/bpmn/')
into('src/main/resources/bpmn/')
exclude { resource -> !resource.file.name.endsWith('.bpmn')}
rename '(.*).bpmn', '$1.bpmn20.xml'
}
According to the gradle documentation, AbstractCopyTask::rename()'s regular expression does not need delimiters:
rename '(.*)_OEM_BLUE_(.*)', '$1$2'
Try replacing:
rename ('/^.*\\.(bpmn)$/', '$1.bpmn20.xml')
With:
rename ('^.*\\.(bpmn)$', '$1.bpmn20.xml')