27 Jul 2011 Matteo Mattei c/c++ linux python
If you have root access but you need to run some applications/scripts with some other user credentials you can do it with
su - username -c "command to execute"
But if you need to do it within a C/C++ program you need to write something like this:
This is how to compile and execute the above code:
[root@barracuda ~]# gcc mysu.c -o mysu
[root@barracuda ~]# id
uid=0(root) gid=0(root) gruppi=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),19(log)
[root@barracuda ~]# ./mysu matteo /bin/bash
[matteo@barracuda /root]$ id
uid=1000(matteo) gid=100(users) groups=100(users),3(sys),10(wheel),14(uucp),91(video),92(audio),93(optical),95(storage),96(scanner),97(camera),98(power),108(vboxusers)
The same result could be obtained also in Python with a very little effort:
20 Jun 2011 Matteo Mattei relax hard disk jokes security
Since a REALLY secure Hard Disk erase procedure does not exist, the only way to clean every private data from a storage device is to use a powerful tool. Today we experimented the secure data cleaning on a 2.5” HD with an hammer. Yes, you have understood, an HAMMER!!! What’s more powerful than an hammer?!
This is the result of our experiment:
Any further feedback is really appreciated!
10 Jun 2011 Matteo Mattei linux vmware
Few days ago I spent some times trying to shrink my Ubuntu 11.04 appliance with root partition formatted with EXT4 filesystem.
The main problem is that the current VMware tools (8.4.6, build-385536) does not support the ext4 shrink. If you run
sudo vmware-toolbox, your root partition is formatted in ext4 and you try to execute the shrink, an error message like the following could appear.
Anyway there is a trick to streamline the final vmdk size. Run this command within a shell into the guest system:
sudo dd if=/dev/zero of=/zero.raw bs=20480
rm -f /zero.raw
Then, shutdown the virtual image and download the vdiskmanager tool from VMware website.
Now run the vmware-vdiskmanager with the -k parameter:
vmware-diskmanager -k /path/to/image.vmdk
This operation will take a while, but at the end you will get a considerable smaller vmdk image file.
06 Feb 2011 Matteo Mattei mysql php
For my client I needed to create a PHP script that can export a full MySQL database schema in another database. This script also need to keep and set constraints.
You only need to configure
$DB_DST_* variables to fit your environment.
Here below you can find the code I created for this purpose:
Update 2016-10-14: The code below has been rewritten using PHP mysqli driver (thanks to Richard Maurer).
04 Feb 2011 Matteo Mattei linux ftp virtual vsftp
I am usually to configure vsftp on web servers to allow FTP access based on domains. Few days ago my client asked me to create multiple FTP users for a single domain every one with a different root folder into that domain.
This is my usual configuration of my /etc/vsftpd.conf
ftpd_banner=Welcome to my private FTP service.
and this is my /etc/pam.d/vsftpd
auth required pam_pwdfile.so pwdfile /etc/ftpd.passwd
account required pam_permit.so
The first time I have created the file /etc/ftpd.passwd in this way:
htpasswd -c -d -b /etc/ftpd.passwd domain1.com <password>
For the next users simply avoid the “-c” parameter:
htpasswd -d -b /etc/ftpd.passwd domain2.com <password>
With this simple configuration all users have these credentials:
- host: domain1.com
- username: domain1.com
- password: password
- port: 21
- Root folder: /var/www/domain1/
Now the point is: how can we create multiple users for a single domain each one with a different root folder? The answer is pretty simple, follow me!
Create the folder /var/www/users and add the following line at the end of /etc/vsftpd.conf
Inside the folder /var/www/users create a file for each virtual user (for example the user user1.domain1.com) containing a line with the root directory for that user:
echo "local_root=/var/www/domain1.com/pub/user1" > /var/www/users/user1.domain1.com
Now add the new user/password in /etc/ftpd.passwd as usual:
htpasswd -d -b /etc/ftpd.passwd user1.domain1.com <password>
Restart vsftpd server and test your new configuration!