fail2ban/fail2ban — pull request #399 by grooverdan (2013-10-15 01:11:30+00:00)
Title: Socket create directory if absent + error reporting improvements

create socket directory and enhance error message. Base very loosely on #168. I'm thinking its quite lasy to depend on initscripts or the user manually to do things that the program can doo.

BF/ENH: remove race conditions by trying to remove force remove socket. Report errors if cannot remove
socket

Tests:

Creates directory and socket and communicates

$ rm -rf /tmp/nonexist && ./fail2ban-client -s /tmp/nonexist/s.sock -c config/ start
2013-10-15 10:53:17,453 fail2ban.server : INFO Starting Fail2ban v0.8.10.dev
2013-10-15 10:53:17,453 fail2ban.server : INFO Starting in daemon mode
$ ./fail2ban-client -s /tmp/nonexist/s.sock stop && rm -rf /tmp/nonexist
Shutdown successful

Reports error if can't create a directory

$ ./fail2ban-client -s /etc/cant_write/s.sock -c config/ start
ERROR While attempting to create directory /etc/cant_write: [Errno 13] Permission denied: '/etc/cant_write'
$ ./fail2ban-client -s /tmp/s.sock -c config/ start
2013-10-15 10:54:14,101 fail2ban.server : INFO Starting Fail2ban v0.8.10.dev
2013-10-15 10:54:14,102 fail2ban.server : INFO Starting in daemon mode

Starting a second instance fails when it could ping the remote

$ ./fail2ban-client -s /tmp/s.sock -c config/ start
ERROR Server already running

The force option had no effect above because the server responded to a ping. Correct behaviour?

$ ./fail2ban-client -s /tmp/s.sock -x -c config/ start
ERROR Server already running

Starting second instance when fist instance killed off still fails

$ ./fail2ban-client -s /tmp/s.sock -c config/ start
2013-10-15 11:57:28,998 fail2ban.server : INFO Starting Fail2ban v0.8.10.dev
2013-10-15 11:57:28,998 fail2ban.server : INFO Starting in daemon mode
$ killall -9 fail2ban-server
$ ./fail2ban-client -s /tmp/s.sock -c config/ start
2013-10-15 11:57:43,699 fail2ban.server : INFO Starting Fail2ban v0.8.10.dev
2013-10-15 11:57:43,700 fail2ban.server : INFO Starting in daemon mode
ERROR Could not start server. Maybe an old socket file is still present. Try to remove /tmp/s.sock. If you used fail2ban-client to start the server, adding the -x option will do itlast error: Unable to contact server - [Errno 111] Connection refused. Is fail2ban running? check the fail2ban.log

$ rm /tmp/s.sock

Remove permission after started and try.

$ ./fail2ban-client -s /tmp/nonexist/s.sock -c config/ start
2013-10-15 12:02:26,370 fail2ban.server : INFO Starting Fail2ban v0.8.10.dev
2013-10-15 12:02:26,371 fail2ban.server : INFO Starting in daemon mode
$ chmod 000 /tmp/nonexist
$ ./fail2ban-client -s /tmp/nonexist/s.sock -c config/ start
2013-10-15 12:03:03,869 fail2ban.server : INFO Starting Fail2ban v0.8.10.dev
2013-10-15 12:03:03,870 fail2ban.server : INFO Starting in daemon mode
ERROR Could not start server. Maybe an old socket file is still present. Try to remove /tmp/nonexist/s.sock. If you used fail2ban-client to start the server, adding the -x option will do itlast error: Unable to contact server - [Errno 13] Permission denied. Is fail2ban running? check the fail2ban.log

Even forcing doesn't get beyond permission denied

$ ./fail2ban-client -s /tmp/nonexist/s.sock -x -c config/ start
2013-10-15 12:03:43,957 fail2ban.server : INFO Starting Fail2ban v0.8.10.dev
2013-10-15 12:03:43,957 fail2ban.server : INFO Starting in daemon mode
ERROR Could not start server. Maybe an old socket file is still present. Try to remove /tmp/nonexist/s.sock. If you used fail2ban-client to start the server, adding the -x option will do itlast error: Unable to contact server - [Errno 13] Permission denied. Is fail2ban running? check the fail2ban.log