Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Tuesday, December 15, 2020

How To Create A Wrapper Script With Customized Environment

When working in a team, everyone will have to conform and work in a same environment. But there are times that, a certain script of yours needs a different environment in order to run. An example would be, everyone in the team has their python version set to 2.7.13. But for some reason, you have a python script that will only work with python 3. Having a wrapper script that customize the environment before running your script would solve this issue. Here's how the wrapper script will look like
1
2
3
4
5
6
7
#!/bin/tcsh -f

setenv BNR_PATH /some/special/path/
setenv BNR_BIN /another/special/path/bin
setenv BNR_ROOT /special/root/path

/path/to/your/script.py $argv:q

Tuesday, October 11, 2016

How To Run A Script As The Owner Of The Script


For the explanation below, just keep in mind that these assumption is true:-
- owner of file == yltan(uid=742)
- user that executes the script == icetools(uid=48102)

How to run a python script as the owner of the script.

1. Create any simple python script, eg a.py:-
#!/usr/bin/env python
import os

print "Before swap::" + str(os.getresuid())
os.system("whoami")

os.setreuid(os.geteuid(),os.getuid())

print "After Swap:" + str(os.getresuid())
os.system("whoami")
2. This is the magic wrapper script, setuid_swap.c. :-
/* This binary is intended to be a setuid script wrapper for dbRsync.pl.
  
   Example compilation on an hp machine:  gcc dbRsync.c -o dbRsync
   This compilation creates an executable called dbRsync. After compiling,
   chmod +s dbRsync (or whatever the executable is) to set the sticky bit or
   it won't work.
*/

int main(int ac, char **av) {
    execv( "/path/to/your/a.py", av );
}
3. Compile the setuid_wrap.c to wrapper_a :-
gcc setuid_swap.c -o wrapper_a
4. Change the setuid bit:-
chmod 4755 wrapper_a
5. Run wrapper_a as someone else other than the owner of the file:-
sudo su - icetools

./wrapper_a
6. ... and this is the output that you get (Running as icetools):-
Before swap::(48102, 742, 742)
icetools
After Swap:(742, 48102, 48102)
yltan






For a detail explanation of how setuid work,

https://drive.google.com/open?id=0B_HHt58thGk_MTVwQkhleWNmZE0



Alternatively, you could swap the users in the compiled code itself by doing this:-
/* This binary is intended to be a setuid script wrapper for dbRsync.pl.
  
   Example compilation on an hp machine:  gcc dbRsync.c -o dbRsync
   This compilation creates an executable called dbRsync. After compiling,
   chmod +s dbRsync (or whatever the executable is) to set the sticky bit or
   it won't work.
*/

int main(int ac, char **av) {
    int uid;
    uid = geteuid();
    setreuid(uid, uid);
    execv( "/path/to/your/a.py", av );
}

Sunday, September 18, 2016

How To Diff A Directory Efficiently

diff -qr -x '*~' -x '*.swp' -x '*.pyc'  -I '$Revision:'  -I '$File:'  -I '$Header:' -I '$Change:' -I '$DateTime:' -I '$Id:' -I '$Date:' -I '$Change:' -I '$Author:' directory1 directory2

Friday, November 27, 2015

Debugging Flow For A Process

I've been stucked with a job which took a lot longer than what it is suppose to take to complete. A friend of mine shared his gems of tricks on how he troubleshoot it, and with a little bit of tweak to my taste, I've came up with a flow(well, almost all of it still from him :p) which I'm logging it here so that I won't forget it.


#1 Find the offending process id (_pid_)

ps -aux | grep job_name


#2 Look at the entire hierarchy of the pid and look at where it stops

pstree -pulna _pid_


#3 Look at the trace of the running program.

strace -t -s 22222 -p _pid_


#4 Look at the read/write IO activities 

cat /proc/_pid_/io







Useful Links

http://www.linux-tutorial.info/modules.php?name=MContent&pageid=84
http://chadfowler.com/blog/2014/01/26/the-magic-of-strace/

Friday, November 20, 2015

Quotes Within Quotes In Linux

I keep forgetting this every time even though I have stumbled across this so many times.
I'm now gonna stick this here so that I can refer it back and hopefully remember this for good.




Basically, the idea is to ....
always just replace each embedded single quote with the sequence: '\'' (that is: quote backslash quote quote) or '"'"' , which closes the string, appends an escaped single quote and reopens the string. 
 https://stackoverflow.com/a/1315213/335181


Putting the above into a perl script works wonder:
#!/usr/bin/perl -pl
s/'/'\\''/g;    ### or s/'/'"'"'/g;
$_ = qq['$_'];



How To Bypass Kerberos(kinit) Authentication

Whenever you try to setuid and impersonate as someone else to run something, it is very likely that you will run into kerberos/kinit issues....