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

Wednesday, August 17, 2016

How To Write Codes In Perl For Accomodating To Simple Testings

In order to write scripts in perl which are testable, it is good to follow the following proposed method (assuming this code is inside file a.pl):-


#!/usr/bin/env perl                                                                                   

sub main
{                                                                                                  
    ### Your main code goes here           
    # ... ... ...                          
    1;                                                         
}


sub is_five
{
    my $num = shift(@_);
    if ($num == 5)
    {
        return 1;
    }
    else:
    {
        return 0;
    }
} # is_five


############################
# This loop will only be entered if this script is called explicitly, ie:-
#   $./a.pl
#
# This loop will not be entered If this file is required, ie:-
#   require 'a.pl'
############################
unless (caller)
{
    main();
}
Now, to write a test that tests the is_five() function, we can create a file call test_a.py, and write the test like this:-
#!/usr/bin/env perl

use Test::Simple tests => 2;
require "a.pl";

sub test_is_five___pass
{
    return is_five(5)
}
sub test_is_five___fail
{
    return is_five(3)
}

unless (caller)
{
    ok(test_is_five___pass());
    ok(! test_is_five___fail());
}

Thursday, August 4, 2016

The Untold Story Behind The Christmas Tree

Christmas Tree are a must during ....well......Christmas. (duh)

Especially when it comes to malls and huge shopping complexes, or theme parks.

People love gathering around a well decorated Christmas tree for photo shots.

Well, there was this mall which decided to make it different. They brought in a 1000 pound 100 feet Christmas tree.

2 groups of people are put in charged of making it happen
- one is responsible for decorating it with bells and whistles
- the other is responsible for making it stable so it doesn't collapse and is safe for people to stick around.

Well, I'm lazy to type. You know most of the drill. Along the process , debates on whether to emphasize more on the decorative or to put more budget and resource on safety is a never ending process.

To cut the story short, here is how it goes.....

If the tree stays put for the rest of Christmas,  no customer or even anyone is going to even notice the existence of the group that puts in effort to make the Christmas tree safe.

But If the tree falls, they are the ones that will get all the heat first.

Well, as for the group of people that focus on adding bells and whistles?

Regardless of whether the tree falls or not, they will be the first to get all the credits during the first day of Christmas when all the people pill over and stick around the Christmas tree.

What if the tree falls on the next day? Well, these folks have already claimed their credits, and are already on their way planning on decorating their next Christmas tree for the next year.

That is the hard truthful fact of life.

Wednesday, April 13, 2016

Ocean's 5th jab

Brought Ocean to vet again as he seems to show signs of painfulness in his mouth and has not been eating well for the past few days.

We decided to seek advice from the vet to see if it is possible to switch to medication instead of jabs, if that is a better option.

Vet gave a jab that will last for 2 days, and gave us a week's medication. Dr. Amelia told us to start giving Ocean the medication starting this Friday, and we should bring Ocean in again for check out a week later.

We plan to crush the pill and mix it with can food, which is what Ocean seems to like lately.
Hopefully with this change, he does not need to endure so much suffering and can have a better quality life moving forward.

Friday, April 8, 2016

Tokyo day 8 - 08 April 2016

7am woke up
8am leave house
9am reach Tsukiji Fish Market.



We didn't plan to visit the auction as it was too early.

We walked along and bought some dried seafood stuff. And also some seaweed from a 150 year old shop , which they claim that the people from the imperial palace are also eating their seaweed.

We found out that most of the food serving restaurant is not crowded except all those that are serving fresh seafood.

We settled for this one, which was recommended by some TV show and blogs...



The wait was around an hour.

But it was all worth it.






And then we went to tokyo Skytree.









Then queue for the tickets to go up...



And from here onwards.... we are up. ...





Of everything up there, I personally find this most interesting. 
It's a glass on the floor, which allows you to see thru from above alllllllll the way down.....


After that, we did some shopping....



... and decided to buy some dessert back to our house as a light lunch and take a rest before we headed out again....

This shop is selling mochi which contains a full strawberry inside.



This is a cream cheese cake, with which strawberries. It's only selling from 1-31april as its seasonal. And it's also damnnnnn expensive.



This is the mochi with strawberry inside...










Headed out again for the evening.
We are going to have a hot spring ...
It's at Odaiba. 

It's somewhere rather far away from the place we stay.... I presume somewhere near haneda airport ??





The main entrance...





And
.....
The inside of the Onsen. ..







Having some hot spring foot bath/spa.....


And some fish service...
This one need to pay extra ....


And the bill....


Since it's the last night, we celebrated and bought some beer and appetizer to go along with it, fresh octopus tentacles.
All gotten from 7-11.





And for the record, the is the place that we stay for the past week...




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