Mkdir() permission denied C/linux programming - c

when I execute this code, I get the error Couldn't create backup sub-directory: Permission denied but I can't understand why since I give full permisions and I'm using a admin account on ubuntu.
umask(0777);
int folder_date_status = mkdir(filepath_W, 0777);
if(folder_date_status == -1){
perror("Couldn't create backup sub-directory");
return -1;
}

An admin account doesn't run with full privileges by default. This is so that programs you run don't unexpectedly act as privileged users (ie. you must explicitly give permission).
To give permission to the program to create a sub-directory in a directory which requires privileged access, try using sudo.
If the program name is called myprogram, try running:
sudo ./myprogram
Then type your password if it is requested.
Note that super-user access should only be required if it is trying to make a subdirectory in a write-restricted directory (eg. restricted directory owned by root, or another user). Also ensure that the parent directory exists (otherwise it could also throw an error).

Did you verify your filepath_w? Do you have permissio to read, write and execute on it? I suggest you to use a absolute pathname and point it to the tmp dir, something like this:
filepath_w = "/tmp/directory"

Related

rename a file using C function rename()

I'm using a Mac. I need to rename a file in the /Library/Application Support/AppName/filename.aiff
This is the system library and not /User/username/Library...
I'm using the rename(old name, new name) function. This function doesn't work (even though it doesn't return an error) if I place the file in the /Library/Application Support/AppName directory but it works properly if I place the file, for example, in /User/username/Documents/filename.aiff.
I don't know what the problem is. Any help would be appreciated. Thanks!
You don't own the directory you're trying to move files into:
/Users/Username/... is a user owned directory, so you're allowed to manipulate files there.
/Library is not a user owned directory.
In order to manipulate files in a non-user owned directory you would need elevated permissions. Instead of using /Library you should be using ~/Library, which is the user owned directory. ~/Library is the shorthand name for /Users/Username/Library.
If the rename is working fine in other path means the problem should be related to permission to access file or with the path searched for file.

Permissions to protect file from being deleted or modified

I want to chmod a directory to prevent any files inside that directory from being deleted or modified without my permission.
How can I achieve that, my directory is set to 777 now which I think is a issue. Could other users access and delete my file without permission if I set the permissions to 777? What permission should I set so that?
I want to be the only one who can write to my own directory, others should only be able to read my file.
Yes, with 777, anybody could delete files from the directory.
You should run chmod 0755 yourdir or chmod og-w yourdir.
You can see the contents (read), add or remove files (write) and "pass through" the folder (execute)
Members of the owning group and other users can see the contents of the folder (read) and "pass through" to child folders (execute). They cannot add or remote files (write).
This guide is a good discussion of *nix directory permissions.
Rather than thinking about it in terms of numeric codes, perhaps it's easier to use the symbolic names for permissions. For example, to remove the ability for "others" to "write" your files:
chmod o-w FILE...
You may also want g-w if you do not want members of your Unix group to write your files.
The removal of write (w) permissions is the same as "clearing bit 2" in the mode, so 7 becomes 5, but this is hard for most normal people to remember, and you don't entirely need to.

BAT script - how to automatically insert additional arguments?

I want to write a simple bat script but I cannot deal with one thing.
I run a command and it gives few messages and it's waiting for my login. I want to write a script which will do it.
For example, I run a command:
myCommand.bat
and it gives:
msg1...
msg2...
...
type your login:_
Now, I have to put my login.
How can I do it automatically?
BAT script is the best way for me :)
You probably can solve this by either
placing your username and/or password directly in myCommand.bat
placing your username and/or password in a file "myPassword.txt" and using redirection: <myPassowrd.txt myCommand.bat
But, you probably should not do either of those for security reasons.
Addendum:
If your batch script is calling an executable that is prompting for the login credentials, you may have 2 options
The executable may have a command line option(s) that allows you to specify username
and/or password. You would have to research your executable to find out what they are.
If the executable is not expecting any other input, you might be able to pipe the username and/or password into the program. For example, if the program asks for username and then asks for password, something like this might work:
(echo yourName&echo yourPassword)|yourProgram.exe
Security concerns still apply

file permissions are changed through joomla

I have a default installation of joomla under ubuntu 10.04. I have also changed the permissions of index.php of template(apache user with read write permissions), so that I can change the index.php from administrator screen. Everything works fine till here.
But now when I change the file from admin screen and save the file, its changing the file permission and removing the write permission from the file. It does saves the changes though.
Now as the write permissions are removed, when I try to install the template it gives me error. So again I have to manually set the write permission for the apache user on index.php and install the template again, then it runs good.
Does anyone know if joomla is changing the file permissions, and any idea how I can tackle this problem.
Thanks,
Tanmay
You're possibly affected by a umask setting. Put umask(0133); in your configuration file (or anywhere you want, just make sure it comes before making files).
Background information:
r (read) has a value of 4 (binary value: 100)
w (write) has a value of 2 (binary value: 010)
x (execute) has a value of 1 (binary value: 001)
These permissions can be granted to the owner, group and everyone else ('other').
When a file is created in PHP, it adds the 666 to it by default. From this number, the umask value is removed using bitwise AND. It looks like you've a umask of 0333. The best way to show what happens is by demonstrating it:
PHP adds: rw-rw-rw- 0666
umask : -wx-wx-wx 0333
result : r--r--r-- 0444
And if you're using umask 0133:
PHP adds: rw-rw-rw- 0666
umask : --x-wx-wx 0133
result : rw-r--r-- 0644

FreeBsd: Allow users of the same group have automatic read/write access to files and directories created by them

In our FreeBSD-environment where we have one server that acts as a file-server, we have a problem that our system administrator says can not be fixed.
All our files resides in a directory and we all have access to that directory, its sub-directories and files. The problem is that once a user in our group creates a file or directory, we have to chmod that directory or file to change the rights so that others in our group can access, read, write and delete. These are not files or sub-directories inside our home-directories, but in a directory where we are supposed to work with them on a daily basis.
Finding it difficult to believe that there is no good solution, I would request that someone assist me with a solution.
I think what you want is a setgid bit on the directories and umask. Then newly created there files and directories will have proper group and proper permissions to let others read abd write them.
find /your-files-are-rooted-here -type d -print0 | xargs -0 chmod ug+rw,g+s
and set umask to 002 (or whatever is appropriate). And, of course, you may want to fix permissions for existing files (the command above only takes care of directories).
One place to but the umask setting is "/etc/bashrc". Find "umask". Change "umask = 022" to "umask = 002". After doing this, when a new file created, every one in the same group with the file owner can write in this new file.
Note that this only works for files created from the shell, specifically bash.

Resources