Quick *nix shadow passwords with Ruby 3

Posted by Kenneth Kalmer on May 01, 2009

Just thought I’d share this one to boost the available online information. Using String#crypt and man crypt you’ll come up with something similar to the gist below (extract from a project I’m busy working on).

module Linux
  class User
    class << self
      # Generate an MD5 salt string
      def salt
        seeds = ('a'..'z').to_a
        seeds.concat( ('A'..'Z').to_a )
        seeds.concat( (0..9).to_a )
        seeds.concat ['/', '.']
        seeds.compact!
 
        salt_string = '$1$'
        8.times { salt_string << seeds[ rand(seeds.size) ].to_s }
 
        salt_string
      end
 
      # Crypt a password suitable for use in shadow files
      def crypt( string )
        string.crypt( self.salt )
      end
    end
  end
end

And the spec

require File.dirname(__FILE__) + '/../spec_helper'
 
describe Linux::User do
  describe "generating shadow passwords" do
    it "should generate a salt for crypt" do
      salt = Linux::User.salt
      salt.length.should be(11)
      salt.should match(/^\$1\$[a-zA-Z0-9\.\/]{8}$/)
    end
 
    it "should generate a shadow password" do
      pass = Linux::User.crypt( 'secret' )
      pass.should match(/^\$1\$[a-zA-Z0-9\.\/]{8}\$[a-zA-Z0-9\.\/]{22}$/)
      pass.length.should be(34)
    end
  end
end

HTH

Pluck out an old revision of a file with git show 3

Posted by Kenneth Kalmer on March 20, 2009

I was busy merging work back and forth between topic branches today and by accident miss-merged a 700-line spec. Everything else was fine (or so I hope) except this one file. I couldn’t use git revert, since that would mean I’ll have to redo all the merges I made.

So in panic I googled and read several man pages, coming to the following conclusion that worked quite well:

  1. Find the offending commit with git log or git log -p, not the revision number prior to the catastrophic change.
  2. View the file at that point in time with git show SHA-ID:path/to/file
  3. Replace current copy with git show SHA-ID:path/to/file > path/to/file
  4. Verify the damage has been undone with git diff
  5. Commit
  6. Breathe

Git is a double edged sword, it is extremely powerful and useful, but can be a real pain in the behind. I love git, and will not easily be convinced to move to something else.

Clear the log files of all your rails projects

Posted by Kenneth Kalmer on November 21, 2008

I hit a barrier last night where my virtual machine I use for Rails development ran out of space. I quickly checked around and saw my current Rails project had 800MB in log files (thanks autospec).

So I decided to quickly cook up this little bash script that I run from my top level working directory, and have it clear all the log files, in all my Rails projects.

Hope it helps.

Postfix log grep gist

Posted by Kenneth Kalmer on October 23, 2008

Just a quickie to tell you about my (first) latest gist: http://gist.github.com/19021

Basically search through the postfix maillog for a pattern, extract the postfix message id from each matched line, and then search through the log file for each corresponding line.

This rebuilds your original search with some more context, and makes it a snap to trace a message through its lifecycle…

  • Tags

    activerecord air amqp analytics audits bash bind capistrano cheat convert couchdb daemon-kit dlz dns elsewhere gentoo gist git hoptoad linux macros mercurial messaging mysql nginx olympics plugins postfix postini powerdns presentations projects quickies rails rake review ruby ruby19 ruote security shoes sitemap ssl svn webby
  • Recent Posts

  • Archives

  • Alltop. Seriously?! I got in?