Wednesday, March 17, 2010

SSH Without Password

SSH login (and sftp) is a secure and convenient method of accessing data and services on remote computers. Sometimes we want to automate the secure log in. Since normally a SSH log in requires a password that corresponds to the user account on the remote computer, using SSH from a script presents a problem. Public/private certificates to the rescue!

Suppose we have a server that we want to access from scripts on two different computers. These scripts are executed periodically by cron jobs. We want to enable a no-password log in from those two computers while requiring a password SSH log in for all others. To do this, we must create a public/private key pair for each of the computers we wish to grant no-password log in privileges. The public key from each of these computers is then shared with the server we want to access. This cryptographically links the two local computers to the remote server.

The storage of these keys on both the local and remote computers is specific to user accounts. For example, suppose we have a local user account named 'getsoh' (get state of health). Every five minutes a cron job triggers a script belonging to the getsoh account that retrieves (using sftp or scp) a log on the remote server that is owned by the user 'createsoh'. We want to create the public/private key pair as user getsoh on the local computer and share the public key with the user createsoh on the remote computer. This is easy to do.
  • On the local machine as user 'getsoh', ensure that the ~/.ssh folder exists. If not create it (type: mkdir ~/.ssh). Ensure that ~/.ssh permissions are 0700.
  • Type: ssh-keygen -t rsa (accept all default settings, including an empty passphrase)
  • Two files (id_rsa and id_rsa.pub) should now be in your ~/.ssh folder.
  • On the remote machine as user 'createsoh', ensure that the folder ~/.ssh exists.
  • Copy the public key to the remote server (ex: remote.com) and append to the ~/.ssh/authorized_keys folder of user 'createsoh'. Type: cat ~/.ssh/id_rsa.pub | ssh createsoh@remote.com  'cat >> ~/.ssh/authorized_keys'
  • Remember to append to rather than overwrite the authorized_keys file in case other keys already are stored.
Remember that creating keys with an empty pass-phrase is a security risk. If someone were to retrieve your private key, they could use that to log in to the remote computers. The use of ssh-agent partially solves that risk by allowing you to use a non-empty pass-phrase and enter it only once, after which it is then cached for the duration of the session. This approach, however, cannot be used when ssh access is needed from a script run periodically by cron.

If the private key is stolen from the client, an unauthorized user will be able to login to the server hosting the public key. To minimize the risk of this occuring, an additional check can be placed in the public key on the server. At the very front of the key in the authorized_keys file, insert: from="client.net", where client.net is the client computer hostname or IP address. Wildcards are supported (i.e. *.clients.net). Multiple hosts/addresses can be used if separated by a comma. Make sure you put double quotes around the host/ip addresses.

If the only purpose of the ssh log in is to automatically run a script, additional restrictions can be defined that will increase security. Let's say that periodically, the client remotely accesses the server through ssh to copy a log file to a shared folder. Expanding on the "from" option mentioned previously, a "command" option can also be used to restrict what can be done when the automatic ssh log in occurs. Assuming that the "from" option is inserted at the front of the key in the server authorized_keys file, insert a comma and then the following:

command="~/getlog.sh",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty

This option will run the ~/.getlog.sh command immediately after a SSH login with the key occurs and then exit.

Troubleshooting:
  •  Try the command: ssh -o PreferredAuthentications=publickey user@server.net
  • Ensure that all ~/.ssh folders have permissions of 0700
  • Ensure that the server ~/.ssh/authorized_keys file has permissions of 0600
  • Use ssh -v[vv] to generate verbose debug information. The the more 'v's the more verbose.

      Labels: , ,

      Wednesday, October 14, 2009

      Mounting SMB Shares on Ubuntu

      I have a 12+ year old Dell computer that's been faithfully chugging away in my basement as a file server running CentOS 4. Each user in the family has their own data directory on the server that is mounted on their Windows XP "My Documents" directory. Works great.

      I've started thinking of switching over to Ubuntu as the primary OS for our family's use and so am experimenting using VirtualBox and a Ubuntu virtual machine. By the way, VirtualBox's seamless mode has a big "WOW" factor. It allows you to run a guest operating system, in this case, Ubuntu, and the Windows XP host system using, essentially, the same desktop. It's very cool and very useful. But I diverge.

      As I mentioned, I have SAMBA shares sitting on the Dell file server and I want to mount each user's share onto their Ubuntu ~/Documents folder so they have exactly the same access as on the Window computers. I don't want to mess with NFS because then I have to worry about both NFS and SAMBA shares and I would prefer to have to deal with only one file sharing service.

      This should work using SAMBA shares from any computer running any operating system. First, ensure you have the correct packages on your Ubuntu system. From a Ubuntu terminal, type:
      sudo apt-get install smbfs smbclient
      Before trying to mount the remote SMB share, securely store your Ubuntu login credentials so that the share will automount at boot. This is done by using your favorite text editor to edit the file /root/.credentials. Enter the following lines in the file, substituting the correct SMB username and SMB password (this example uses a SMB username of barney, a SMB password of betty.
      username=barney
      password=betty
      
      Now make the credentials file root read-only by typing:
      chmod 0600 /root/.credentials
      Assuming the SAMBA server IP is 192.168.1.200, the remote SAMBA share is named barney, and barney's Ubuntu user id is 1000, enter the SMB share and mount point information in your /etc/fstab file as follows:
      //192.168.1.200/barney /home/barney/Documents smbfs \
              auto,credentials=/root/.credentials,uid=1000,umask=000,user 0 0
      

      To test, you can type:
      sudo mount -a
      or simply reboot.

      Labels: , ,

      Tuesday, September 29, 2009

      Command Line TrueCrypt Volume Creation Using ext3 filesystem

      The current Linux version of TrueCrypt (version 6.2a) seems to have a bug that does not allow the creation of an ext3 TrueCrypt volume directly from the TrueCrypt command line. The following script provides a workaround, as well as providing a nice user interface (as text-based interfaces go!)

      #!/bin/sh
      
      #
      # this script creates a truecrypt file container. the script requires
      # five parameters:
      #
      #   - size in GBytes. A 4 GByte container is specified by '4'.
      #   - entropy source. This will usually be /dev/random or /dev/urandom
      #   - filename. Path and filename of container (ex: /data/secure1.tc)
      #   - mount point. Where the container will be mounted (ex: /mnt/tc)
      #   - keyfile. Path and filename of the keyfile to be used (ex: /root/key)
      #
      # note that /dev/random is a blocking device - i.e. it will wait to
      # generate random numbers until there is sufficient entropy in the
      # system to ensure security. if a blocking device is used, the user
      # will probably need to generate entropy by typing random characters
      # in a file until /dev/random receives sufficient data.
      #
      # initially developed and tested on Fedora 10
      #
      
      if [ $(id -u) -ne 0 ]; then
      echo
      echo "This script must be run as root. Exiting..."
      echo
      exit 1
      fi
      
      if [ $# -ne 5 ]; then
      echo
      echo "usage: $0 size(GB) entropy-src filename mnt-point keyfile"
      echo
      echo "For example, to create a 4GB truecrypt file container using"
      echo "the /dev/random RNG at /data/secure.tc and mount it on"
      echo "/mnt/tc with the keyfile /root/thekey, do the following:"
      echo
      echo "$0 4 /dev/random /data/secure.tc /mnt/tc /root/thekey"
      echo
      exit 1
      fi
      
      GBSIZE=${1}
      ENTROPY=${2}
      VOL=${3}
      MNT=${4}
      KEY=${5}
      
      # generate the size of the container in bytes
      
      SIZE=$(echo "${GBSIZE}*(2^30)" | bc)
      
      # unmount anything that is on the designated mount point
      
      truecrypt -t -d $MNT 2> /dev/null
      
      # create a truecrypt file container using the designated key, size,
      # and volume location. Create a FAT volume but we'll overwrite it
      # later with an ext3 fileysystem (have to do this because truecrypt
      # won't allow the direct creation of an ext3 container from the
      # command line).
      
      if [ "$ENTROPY" == "/dev/random" ]; then
      echo
      echo "You have selected a blocking entropy source. This means that"
      echo "the creation of the truecrypt file container will wait until"
      echo "there is enough randomness in the system to secure the"
      echo "encryption keys. If the file container creation process does"
      echo "not start immediately, open up a file (e.g. /tmp/barney) and"
      echo "begin to type random characters until the progress indicator"
      echo "appears."
      echo
      read -p "Hit the  key to continue..."
      echo
      fi
      
      truecrypt -t \
      --create \
      --keyfiles=$KEY \
      --password="" \
      --volume-type=normal \
      --size=${SIZE} \
      --encryption=AES \
      --hash=SHA-512 \
      --filesystem=FAT \
      --random-source=${ENTROPY} \
      $VOL
      
      if [ "$?" != "0" ]; then
      echo
      echo "Truecrypt container creation failed."
      echo
      exit 1
      fi
      
      # mount the newly created truecrypt container
      
      truecrypt -t -k $KEY -p "" --protect-hidden=no $VOL $MNT
      if [ "$?" != "0" ]; then
      echo
      echo "Initial mount of newly created truecrypt container failed. Exiting..."
      echo
      exit 1
      fi
      
      # create a ext3 filesystem on the /dev/mapper device
      
      mapper=$(truecrypt -t -l | cut -d" " --fields=3)
      umount $MNT
      mkfs.ext3 $mapper
      
      # unmount and then remount to use the new filesystem
      
      truecrypt -t -d $VOL
      truecrypt -t -k $KEY -p "" --protect-hidden=no $VOL $MNT
      if [ "$?" != "0" ]; then
      echo
      echo "Cannot mount ext3 truecrypt container. Exiting..."
      echo
      exit 1
      fi
      
      echo
      echo "The truecrypt container was successfully created and mounted."
      echo
      exit 0
      
      

      Labels: ,

      Wednesday, September 23, 2009

      Hide Users on a Fedora 10 Login Screen

      You may want to hide the login ids of users on systems for privacy or security reasons. Although this is possible, the command is very unwieldy. Here's how you do it for Fedora 10.

      gconftool-2 --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults --direct --type bool --set /apps/gdm/simple-greeter/disable_user_list true

      Ugly, but it works.

      Labels: ,

      Saturday, September 19, 2009

      NTP Server & Client Configs on Fedora 10

      On the server, ensure that there is the following line in the /etc/hosts file:
      127.0.0.1 localhost
      Also ensure that port 123/UDP is open.

      Then edit the /etc/ntp.conf file to look like this:
      # For more information about this file, see the man pages
      # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).


      # Permit time synchronization with our time source, but do not

      # permit the source to query or modify the service on this system.

      restrict default kod nomodify notrap nopeer noquery

      restrict -6 default kod nomodify notrap nopeer noquery


      # Permit all access over the loopback interface. This could

      # be tightened as well, but to do so would effect some of

      # the administrative functions.
      restrict 127.0.0.1

      restrict -6 ::1


      # Use public servers from the pool.ntp.org project.

      # Please consider joining the pool (http://www.pool.ntp.org/join.html).

      #server 0.fedora.pool.ntp.org dynamic

      #server 1.fedora.pool.ntp.org dynamic

      #server 2.fedora.pool.ntp.org dynamic

      driftfile /var/lib/ntp/drift


      # Undisciplined Local Clock. This is a fake driver intended for backup

      # and when no outside source of synchronized time is available.
      server 127.127.1.0

      # local clock
      fudge
      127.127.1.0 stratum 10



      # Key file containing the keys and key identifiers used when operating
      # with symmetric key cryptography.
      keys /etc/ntp/keys
      On the server, enter:
      chkconfig ntpd on
      service ntpd on
      Then, set the date correctly on the server (date MMDDhhmm).

      On the client,
      ensure that there is the following line in the /etc/hosts file:
      127.0.0.1 localhost


      Edit the /etc/ntp.conf file to look like this, assuming that the local NTP server we just set up is named barney with an IP address of 10.8.0.1:


      # For more information about this file, see the man pages
      # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

      # Permit time synchronization with our time source, but do not
      # permit the source to query or modify the service on this system.
      restrict default kod nomodify notrap nopeer noquery
      restrict -6 default kod nomodify notrap nopeer noquery

      # Permit all access over the loopback interface. This could
      # be tightened as well, but to do so would effect some of
      # the administrative functions.
      restrict 127.0.0.1
      restrict -6 ::1

      driftfile /var/lib/ntp/drift

      # Hosts on local network are less restricted.
      restrict barney mask 255.255.255.0 nomodify notrap
      server barney

      # Key file containing the keys and key identifiers used when operating
      # with symmetric key cryptography.
      keys /etc/ntp/keys

      On the client, enter:
      chkconfig ntpd on
      service ntpd on
      If there is a large difference in times, you can quickly bring the client into close sync with the time server by typing:
      ntpdate barney
      You can see if the client is being updated by using the command:
      watch ntpq -p


      Labels: ,

      Friday, September 18, 2009

      RPM & YUM tricks



      So you're building a complex system that requires installing many software packages in RPM format and you keep having problems with dependencies, conflicts, and who knows what else. Here are some handy RPM commands that might come in useful.

      # extracts the contents of an RPM to disk
      rpm2cpio < packagename.rpm | cpio - ivd

      # list the contents of an RPM file
      rpm -qpi packagename.rpm

      # install all the RPMs in the current folder
      rpm -U *.rpm

      # don't install, but print out what would happen
      rpm -i --test list of RPM packages

      # replace existing files in another package even if there is a conflict
      rpm -i --replacefiles packagename.rpm

      # Removes the last 'n' RPMs. The example below uses last 15 RPMs.
      rpm -qa --last | head -15 | cut -d" " -f 1 | xargs rpm -e

      # test a group of RPMs for missing dependencies (or other problems)
      # definitely need to resolve "is needed by" errors
      mkdir /tmp/testdb
      rpm --initdb --dbpath /tmp/testdb
      cd packages folder
      rpm --test --dbpath /tmp/testdb -Uvh *.rpm
      rpm -rf /tmp/testdb



      # save the RPMs that are downloaded and installed by yum

      Edit the /etc/yum.conf file and change (or insert) keepcache=1


      # download only (don't install) RPMs using yum
      yum install yum-downloadonly
      yum update somepackage --downloadonly
      yum update somepackage --downloadonly --downloaddir=/tmp/newpkgs

      Labels: , ,

      Root is read-only?

      When you're doing serious work on Linux, sometimes things get broken and need to be fixed. If that something involves disks, you might be greeted with a screen at boot that says that there is a problem and that you should "Give root password for maintenance (or type Control-D for normal startup)". But when you type your root password and get the shell prompt, you can't do anything because the root is read-only. For instance, you might need to edit /etc/fstab or another configuration file to remove or comment out an offending line. But you can't save your changes because root is read-only. The command to fix this is simple:

      mount -n -o remount -t ext3 /dev/sda3 /

      Remember to enter the correct filesystem type (ext3 in this example) and the correct disk partition (/dev/sda3 in this example) and your problem should be solved.

      Labels: ,

      Friday, September 4, 2009

      Compiling & Installing Truecrypt on Fedora 10

      Truecrypt is a powerful, multi-platform encryption tool. However, only OpenSuse and Ubuntu packages are available for download at the Truecrypt web site. Those with other distributions must compile it themselves. This tutorial covers compiling and installing Truecrypt on Fedora 10 and assumes starting from a base OS install.
      1. Download the Truecrypt source code from http://www.truecrypt.org/downloads2 (Mac OS X / Linux tar.gz file).
      2. Copy the downloaded file to a folder of your choice and extract it: tar xzf truecrypt-file.tar.gz
      3. cd into the new source code directory
      4. Download the crypto header files by doing the following:
        wget ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-11/v2-20/pkcs11.h
        wget ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-11/v2-20/pkcs11f.h
        wget ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-11/v2-20/pkcs11t.h
      5. Make sure the necessary packages (and their dependencies) are installed:
        yum install gcc-c++ fuse-devel wxGTK-devel
      6. make
      7. If successful, the truecrypt executable should be in the Main folder. Move the executable to /usr/bin: mv Main/truecrypt /usr/bin (must be root)
      You may want to install truecrypt executable on other computers without having to compile it on each computer. There may be occasions where you don't have web access to download the packages upon which truecrypt depends. Fortunately, the number of software packages that truecrypt is dependent upon to run is much smaller than the number necessary to compile it.

      In addition to the newly truecrypt executable, you will need to include three software packages in the installation package:
      • SDL
      • wxGTK
      • wxBase
      Normally, the RPM files are not saved when installed by yum. This behavior can be changed by editing the /etc/yum.conf file and adding (or changing) the line "keepcache=1". Then, when installing (yum install SDL wxGTK wxBase), the RPMs are saved in the yum cache. The new RPMs can be in one of two cache locations:
      • /var/cache/yum/fedora/packages
      • /var/cache/yum/updates/packages
      Once you have a compiled truecrypt executable and three RPMs, you can pop them on a thumb drive and install them on any other Fedora 10 computer by:
      • Copying the truecrypt executable to /usr/bin
      • yum install SDL...rpm wxBase...rpm wxGTK...rpm
      Where the complete filenames for the SDL wxBase and wxGTK RPM files are listed.

      Labels: , ,

      Friday, April 17, 2009

      Custom Syslinux Splash Screens

      While rolling a custom CentOS 5.2 distribution, I wanted to create a custom splash screen rather than use the vanilla CentOS jpg file. Since the existing splash screen was a JPEG file with a size of 640x480, I created my own JPEG file with the same dimensions. It didn't work. Just showed a blank background.

      There seems to be little documentation on this problem, at least that I could find. Those with a problem who posted to a forum on the web never seemed to get a good response from the community. Hints were few. Saw a few about a special file format (lss, I believe it was). There were some comments about 8-bit color. There were some posts that said that syslinux couldn't display PNG files.

      Finally, I created a 8-bit PNG file and just tried it and it worked! At least for syslinux 3.36. I believe the latest version as of this writing is 3.75, but I can't use that because of dependencies I don't want to resolve in the distro.

      SOLUTION: 8-big PNG, 640x480.

      Labels: ,

      Tuesday, April 7, 2009

      Removable Devices & udev

      The Linux udev capability provides naming support for dynamic devices such as USB drives, cameras, etc. udev rules follow a simple syntax to match a dynamic device when it is first "plugged in" and subsequently name it based on parameters in the matching rule. For the following example, I used CentOS 5.2 and a SanDisk compact flash reader connected via USB.

      The first step is to plug the device into your computer and query its characteristics. In my case, when I plugged in the CF reader, it was auto-mounted as /dev/sdb1. Use the udevinfo command to identify the characteristics of the device: udevinfo -a -p $(udevinfo -q path -n /dev/sdb1). This prints a long list of characteristics for the device itself, as well as the "parent" devices. The key is to use these characteristics to generate a rule that matches only the CF reader.

      I identified a section from the output of the udevinfo command that seemed to have characteristics that would be unique to the reader. These were the idVendor value of "0781" and the idProduct value of "b4b5". I then went to the udev rules folder (/etc/udev/rules.d) and created a 10-local.rules file. Inside the rules file, I added the following rule: BUS=="usb", SYSFS{idVendor}=="0781", SYSFS{idProduct}="b4b5", NAME+="cf%n". This rule matches on a dynamic device on the usb bus with a Vendor ID of 0781 and a Product ID of b4b5. The output of this rule is defined by the NAME parameter, which simply states to create a device named /dev/cf1, where 1 could vary if other /dev/cf? devices are already in use.

      It sometimes takes a few tries to properly define the rule so that it matches the desired devices and performs the desired naming. Two commands are very useful to help you debug and test. The first is the 'udevtest dev-path' command. udevtest takes the device path of the device in question and outputs the actions that would be performed. I used the command 'udevtest /block/sdb/sdb1' for my particular device. If you need to add, delete, or modify a rule, make the change in the file (10-local.rules) and then run 'udevcontrol reload_rules'.

      To also define the mount point, I created a mount point in the /media folder named "cfboot" and entered the following line in the /etc/fstab file: /dev/cf1 /media/cfboot ext3 defaults,user 0 0. Note that the 'user' option is required if you wish any user other than root to be able to mount this device. The combination of the udev rule and the fstab line, when the CF reader is plugged in, creates a device node named /dev/cf1 and mounts the device on /media/cfboot.

      Resources which I found useful are:
      • http://linux.die.net/man/8/udev
      • http://reactivated.net/writing_udev_rules.html
      • http://ubuntuforums.org/showthread.php?t=168221
      • http://www.linuxjournal.com/article/7316

      Labels: , ,