If condition to check file is rewritable as different user - powershell

Is there a way to check if a file has chmod -R 777 set to that particular file with shell as a different user? i have a script which will dynamically run as a different user when it gets deployed to different environment
Static shell script which will be used as it gets deployed to higher environment e.g
chmod -R 777 /home/job/test.json
When it first gets deployed to dev environment . its working as expected. no error. but as soon its deployed as dev2 user using the same files but only as a different user,
it will throw chmod: changing permissions ... operation not permitted
For an example, files are initially deployed as user 'Test1'
-rwxrwxrwx | test.json
this are working as expected. Now we switch user to trigger some script as dev2. since test.json is originally created by dev1 . it will throw operation not permitted error eventhough it has chmod 777 . Reason being is because its trying to rerun chmod 777 as a different users
Is there a function we can use to determine if the file is rewrittable ? for an example
if [ -f /test1.json ]
then echo "check if this is -rwxrwxrwx" && echo " ignore condition "
else echo "do nothing"
fi

Related

Files created by manage.py commands not chown'd from root user

I'm using cookiecutter-django project template which includes nice docker-compose integration. However when I run a manage.py command that creates a file via docker-compose e.g:
sudo docker-compose -f dev.yml run django python manage.py dumpdata --output ./myapp/fixtures/data.json
the file is owned by root:root so I do not initially have write permissions on my host filesystem. I'm still learning about docker-compose and am struggling to find the tidiest way to chown any created files back to my local user.
I did try setting the user: flag in my django service in dev.yml like so..
django:
user: $USER
but this didn't take.
I also read about using
django:
user: ${UID}
but this also fails with a warning
WARNING: The UID variable is not set. Defaulting to a blank string.
despite echo $UID returning the correct value. So for some reason, env variables are not being passed through properly, though I don't know how to go about debugging that. possibly related: https://github.com/docker/compose/issues/2613
edit: more updates.
so running docker-compose -f dev.yml run django env shows that $UID isn't present. I tried assigning it in entrypoint.sh but as that file is run by root, the $UID will be 0 at runtime. I'm out of ideas now as to how to pass the user ID in.

Perl script can't unzip to mounted Windows share - permission denied

I have a simple script that is unzipping a file to a mounted Windows share. It is normally launched as a Postfix alias when a new email arrives, but even when I run it as root I get a permission denied error.
The script is doing this:
if ( </tmp/*.zip> ){
unzip '</tmp/*.zip>' => '</mnt/win/#1.txt>'
or die "unzip failed: $UnzipError\n";
}
As root I can copy files to /mnt/win without a problem, but when this script is launched by Postfix it fails with permission denied.
/etc/fstab looks like:
//srvr1/tmp /mnt/win cifs credentials=/root/.smbcredentials,iocharset=iso8859-1,sec=ntlm 0 0
ls -ld looks like:
drwxr-xr-x 2 root root 196608 Jul 15 17:24 /mnt/win
Files produced by the script are nobody/nogroup, but I'm not sure how I change the permissions of /mnt/win to allow nobody/nogroup to read/write.
cmhod -R ugo+RW /mnt/win
Also gives permission denied. Is there a way to change permissions in /etc/fstab to allow nobody/nogroup?
You need to use this command to change the permissions of folder & files inside it.
sudo chmod -R R+X /mnt/win
If you want to change the user of the file as well which is nobody/nogroup rightnow, you can use this commands :
sudo chown -R username.username /mnt/win/
Here you can choose any user's name e.g. mine is mukesh
sudo chown -R mueksh.mukesh /mnt/win/

First deploy fails in Capistrano 3 with a /var/www/XXX permission error

I am running the first deploy to a machine via cap staging deploy. As of Cap3, deploy:setup is no longer needed. Yet, strangely, I get a mkdir permissions error:
INFO[cb348f12] Running /usr/bin/env mkdir -pv /var/www/myapp/shared /var/www/myapp/releases on ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
DEBUG[cb348f12] Command: /usr/bin/env mkdir -pv /var/www/myapp/shared /var/www/myapp/releases
DEBUG[cb348f12] mkdir:
DEBUG[cb348f12] cannot create directory ‘/var/www’
DEBUG[cb348f12] : Permission denied
DEBUG[cb348f12]
DEBUG[cb348f12] mkdir:
DEBUG[cb348f12] cannot create directory ‘/var/www’
DEBUG[cb348f12] : Permission denied
DEBUG[cb348f12]
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing on host ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com: mkdir exit status: 1
Of course, I could go an create and chmod that directory myself, but that's not the point. Cap3 is supposed to take care of that itself. I'm confused why it doesn't do that. I should also note that I had this same setup succeed before with another machine (though that was "production" environment).
Here are some basic settings in my deploy.rb:
set :application, "myapp"
set :user, 'ec2-user'
Instead of creating the following directory structure: /var/www/myapp/shared/var/www/myapp/releases
try: /var/www/myapp/releases
EDIT: Re-reading your question, I found that you already know how to create folder on server and set permission.
Cap3 is supposed to take care of that itself. I'm confused why it
doesn't do that
Nope. Cap3 cannot create a folder on server automatically, if the user provided to Cap3 does not have previledges to create the folder. There is no magic here just unix permissions.
In your case the user your have provided ec2-user does not have permission to create folder in /var which causes the error.
Original answer
Check whether /var/www folder exists on your server. If it does not, then create that folder and set its ownership to ec2-user so that Capistrano can create any other folders as necessary during the deployment.
sudo mkdir -p /var/www
sudo chown ec2-user:ec2-user /var/www
With the above commands run, try deploying again with capistrano. Permission error should now be gone.

Upstart / init script not working

I'm trying to create a service / script to automatically start and controll my nodejs server, but it doesnt seem to work at all.
First of all, I used this source as main reference http://kvz.io/blog/2009/12/15/run-nodejs-as-a-service-on-ubuntu-karmic/
After testing around, I minimzed the content of the actual file to avoid any kind of error, resulting in this (the bare minimum, but it doesnt work)
description "server"
author "blah"
start on started mountall
stop on shutdown
respawn
respawn limit 99 5
script
export HOME="/var/www"
exec nodejs /var/www/server/server.js >> /var/log/node.log 2>&1
end script
The file is saved in /etc/init/server.conf
when trying to start the script (as root, or normal user), I get:
root#iof304:/etc/init# start server
start: Job failed to start
Then, I tried to check my syntax with init-checkconf, resulting in:
$ init-checkconf /etc/init/server.conf
File /etc/init/server.conf: syntax ok
I tried different other things, like initctl reload-configuration with no result.
What can I do? How can I get this to work? It can't be that hard, right?
This is what our typical startup script looks like. As you can see we're running our node processes as user nodejs. We're also using the pre-start script to make sure all of the log file directories and .tmp directories are created with the right permissions.
#!upstart
description "grabagadget node.js server"
author "Jeffrey Van Alstine"
start on started mysql
stop on shutdown
respawn
script
export HOME="/home/nodejs"
exec start-stop-daemon --start --chuid nodejs --make-pidfile --pidfile /var/run/nodejs/grabagadget.pid --startas /usr/bin/node -- /var/nodejs/grabagadget/app.js --environment production >> /var/log/nodejs/grabagadget.log 2>&1
end script
pre-start script
mkdir -p /var/log/nodejs
chown nodejs:root /var/log/nodejs
mkdir -p /var/run/nodejs
mkdir -p /var/nodejs/grabagadget/.tmp
# Git likes to reset permissions on this file, but it really needs to be writable on server start
chown nodejs:root /var/nodejs/grabagadget/views/layout.ejs
chown -R nodejs:root /var/nodejs/grabagadget/.tmp
# Date format same as (new Date()).toISOString() for consistency
sudo -u nodejs echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/nodejs/grabagadget.log
end script
pre-stop script
rm /var/run/nodejs/grabagadget.pid
sudo -u nodejs echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/nodejs/grabgadget.log
end script
As of Ubuntu 15, upstart is no longer being used, see systemd.

Appending content with different user

My script is creating a gt.txt file with abc user and writing some content into it. I have another script which is appending the content to the same file gt.txt but since it is being run as a different user, it is throwing an error.
What shall I do? Any possible solutions? Both are shell scripts but are run as different users.
I assume the error you are getting is 'permission denied'.
One option is to create a group, which both users can belong to, and assign group permissions to the file which allow group members to write to the file. Like this:
groupadd gtgroup
usermod -a -G gtgroup abcuser
usermod -a -G gtgroup defuser
chgrp gtgroup gt.txt
chmod g+rw gt.txt
You will need to issue most of those commands as root or via sudo. Also, groupadd and usermod commands may vary from one unix-like system to another.
If the file is being created from within the perl script, then you might have to perform the chgrp and chmod within the perl script. Use Perl chown and chmod for that.