Skip to content

Commit 417d14e

Browse files
committed
Cleanup, deal with VPS that don't expose kernel config in /boot
Remove restriction that spaces in options must be underscores; let them be single quoted instead. (Keep underscore translation for compatibility, but don't document.) Make shellcheck happy (ier). Allow CONFIG_HZ to be specified for systems that don't provide the kernel config. Provide an estimation function that can determine CONFIG_HZ. Requires installation of 'bc' to overcome bash arithmetic limitations. For some reason, the high nibble of timestamps seems to be non-zero, which makes resolving timestamps impossible. Mask those bits (though systems with very long uptimes may legitimately use them...) Try to diagnose failures installing the iptables rule. Add -n to suppress hostname lookups for -L (speed) If both /etc/default and /etc/sysconfig have .conf files, only use /etc/default Document how to avoid self-lockout. Switch to auto-versioning. Separate release from version. Add a Makefile for install, man page, uninstall
1 parent 91a389a commit 417d14e

File tree

7 files changed

+723
-95
lines changed

7 files changed

+723
-95
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.git* export-ignore
2+
ipblock export-subst ident

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
*~
2+
*#
3+
.#*
4+
*.swp
25
*.bak
36
*.orig
7+
*.sig
8+
ipblock-*
9+
*.tar *.tar.gz *.tar.xz *tar.lzop *.tar.lzma *.tar.Z *.tar.zst *.tar.bz *.tar.bz2

Makefile

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# Copyright (C) 2023 Timothe Litt litt at acm ddot org
2+
3+
# Install targets - can override on command line
4+
5+
# Note that DESTDIR is supported for staging environments
6+
7+
prefix := /usr/local
8+
datarootdir := $(prefix)/share
9+
mandir := $(datarootdir)/man
10+
man1dir := $(mandir)/man1
11+
manext := .1
12+
man1ext := .1
13+
exec_prefix := $(prefix)
14+
bindir := $(exec_prefix)/bin
15+
confdir := $(shell if [ -f "/etc/default/ipblock.conf" ]; then echo "/etc/default"; \
16+
elif [ -f "/etc/sysconfig/ipblock.conf" ] || ! [ -d "/etc/default" ]; \
17+
then echo "/etc/sysconfig"; \
18+
else echo "/etc/default"; fi)
19+
INSTALL := install
20+
INSTALL_PROGRAM := $(INSTALL)
21+
INSTALL_DATA := $(INSTALL) -m 644
22+
23+
# Specify key="deadbeef" or key="deadbeef beeffeed" on command line (else default)
24+
GPG := gpg
25+
26+
SHELL := bash
27+
SED := sed
28+
29+
# https://kristaps.bsd.lv/lowdown/ - used to build man page.
30+
LOWDOWN := lowdown
31+
32+
# Usage:
33+
# See INSTALL
34+
35+
# Extract version from ipblock source
36+
37+
kitversion := $(shell $(SED) -nEe"s/^RELEASE='([^']+)'.*$$/\1/p" ipblock)
38+
kitname := ipblock-$(kitversion)
39+
kitowner := 0:0
40+
41+
# location if command is installed
42+
43+
IPBLOCK := $(strip $(shell command -v ipblock))
44+
45+
# If in a Git working directory and the git command is available,
46+
# get the last tag in case making a distribution.
47+
48+
ifneq "$(strip $(shell [ -d '.git' ] && echo 'true' ))" ""
49+
gitcmd := $(shell command -v git)
50+
ifneq "$(strip $(gitcmd))" ""
51+
gittag := $(shell git tag | sort -t. -k 1.2,1n -k 2,2n -k 3,3n | tail -n1)
52+
endif
53+
endif
54+
55+
# file types from which tar can infer compression, if tool is installed
56+
57+
# kittypes = gz xz lzop lz lzma Z zst bz bz2
58+
59+
# kittypes to build
60+
61+
kittypes := gz xz
62+
63+
# Files to package
64+
65+
#kitfiles := INSTALL README.md LICENSE Makefile ipblock config/ipblock.conf ipblock$(manext)
66+
kitfiles := README.md LICENSE Makefile ipblock config/ipblock.conf ipblock$(manext)
67+
68+
.PHONY : all
69+
70+
all : ipblock$(man1ext)
71+
72+
# Compilations: man page from README and help
73+
74+
ipblock$(man1ext) : README.md ipblock Makefile
75+
$(SED) -e's,^`ipblock -h` for complete help$$,./ipblock -h,e' $< | \
76+
$(LOWDOWN) -s -t man --parse-codeindent -M "title=ipblock" -M "date=$$(date -r ipblock +%d-%b-%Y)" -Msection=8 -o $@ -
77+
78+
# Make tarball kits - various compressions
79+
80+
.PHONY : dist unsigned-dist signed-dist
81+
82+
dist : signed-dist
83+
84+
signed-dist : unsigned-dist $(foreach type,$(kittypes),$(kitname).tar.$(type).sig)
85+
86+
unsigned-dist : $(foreach type,$(kittypes),$(kitname).tar.$(type))
87+
88+
# Tarball build directory
89+
90+
$(kitname)/% : %
91+
@mkdir -p $(dir $@)
92+
@-chown $(kitowner) $(dir $@)
93+
cp -p $< $@
94+
@-chown $(kitowner) $@
95+
96+
# Clean up after builds
97+
98+
.PHONY : clean
99+
100+
clean:
101+
rm -rf $(kitname) $(foreach type,$(kittypes),$(kitname).tar.$(type){,.sig})
102+
103+
# Install program and doc
104+
105+
.PHONY : install
106+
107+
install_dirs := $(DESTDIR)$(bindir) $(DESTDIR)$(man1dir) $(DESTDIR)$(confdir)
108+
109+
install : ipblock ipblock$(man1ext) config/ipblock.conf installdirs
110+
$(INSTALL_PROGRAM) ipblock $(DESTDIR)$(bindir)/ipblock
111+
$(INSTALL_DATA) ipblock$(man1ext) $(DESTDIR)$(man1dir)/ipblock$(man1ext)
112+
-if [ -f "$(confdir)/ipblock.conf" ]; then true ; else $(INSTALL_DATA) config/ipblock.conf $(DESTDIR)$(confdir)/ipblock.conf; fi
113+
@echo ""
114+
@echo "Please read 'man 1 ipblock' before using the command'"
115+
116+
# un-install
117+
118+
.PHONY : uninstall
119+
120+
# uninstall should have the command in $(bindir)...
121+
# uninstall may encounter no IPvX rule, no chain. So flush and disable are best effort.
122+
uninstall :
123+
@-if ! [ -x "$(DESTDIR)$(bindir)/ipblock" ]; then \
124+
echo "The ipblock command is not in '$(DESTDIR)$(bindir)'" >&2 ; \
125+
echo "This uninstall will not find it and may not do what you expect." >&2 ; \
126+
if [ -n "$(IPBLOCK)" ] && [ "$(IPBLOCK)" != "$(DESTDIR)$(bindir)/ipblock" ]; then \
127+
echo "Did you forget to set 'prefix=$(dir $(IPBLOCK))'?" >&2 ; \
128+
fi ; \
129+
fi
130+
-$(DESTDIR)$(bindir)/ipblock -4F >/dev/null 2>&1 || true
131+
-$(DESTDIR)$(bindir)/ipblock -4X >/dev/null 2>&1 || true
132+
-$(DESTDIR)$(bindir)/ipblock -6F >/dev/null 2>&1 || true
133+
-$(DESTDIR)$(bindir)/ipblock -6X >/dev/null 2>&1 || true
134+
-rm -f "$(DESTDIR)$(bindir)/ipblock"
135+
-rm -f "$(DESTDIR)$(man1dir)/ipblock$(man1ext)"
136+
@-[ -f "$(DESTDIR)$(confdir)/ipblock.conf" ] && echo "Not deleting $(DESTDIR)$(confdir)/ipblock.conf in case you want to reinstall later"
137+
138+
# create install directory tree (especially when staging)
139+
140+
installdirs : $(install_dirs)
141+
$(INSTALL) -d $(install_dirs)
142+
143+
# rules for making tarballs - $1 is file type that implies compression
144+
145+
define make_tar =
146+
147+
%.tar.$(1) : $$(foreach f,$$(kitfiles), %/$$(f))
148+
tar -caf $$@ $$^
149+
@-chown $(kitowner) $$@
150+
ifneq ($(strip $(gitcmd)),)
151+
@if git ls-files --others --exclude-standard --directory --no-empty-directory --error-unmatch -- ':/*' >/dev/null 2>/dev/null || \
152+
! git diff-index --quiet HEAD || [ -n "$$$$(git diff --stat)" ]; then \
153+
echo " *** Not tagging V$(kitversion) because working directory is dirty"; echo ""; \
154+
elif [ "$(strip $(gittag))" == "V$(kitversion)" ]; then \
155+
echo " *** Not tagging because V$(kitversion) already exists"; \
156+
echo ""; \
157+
else \
158+
git tag -f V$(kitversion) || true; \
159+
fi
160+
endif
161+
162+
endef
163+
164+
$(foreach type,$(kittypes),$(eval $(call make_tar,$(type))))
165+
166+
# create a detached signature for a file
167+
168+
%.sig : % Makefile
169+
@-rm -f $<.sig
170+
$(GPG) --output $@ --detach-sig $(foreach k,$(key), --local-user "$(k)") $(basename $@)
171+
@-chown $(kitowner) $@

README.md

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Quickly (and temporarily) block an IP address
44

55
Copyright (C) 2017, 2018, 2019, 2020 Timothe Litt
66

7-
When your machine is under attack from an unexpected source, the last thing that you want to do is remember the `iptables` syntax for adding an immediate blocking rule.
7+
When your machine is under attack from an unexpected source, the last thing that you want to
8+
do is remember the `iptables` syntax for adding an immediate blocking rule.
89

910
`ipblock` addresses this issue. Simply say
1011

@@ -14,19 +15,20 @@ All packets from that address will be dropped.
1415

1516
`ipblock` only adds a single rule to your `iptables` and/or `ip6tables` rulesets, no
1617
matter how many addresses (up to the ipt_recent limit) are blocked. This rule is
17-
inserted at the top of the chain, thus taking precedence over any other exceptions.
18+
inserted at the top of the specified chain, thus taking precedence over any other exceptions.
1819

1920
The rule is only added the first time that `ipblock` is run, so your `iptables` rules are not reloaded.
2021

2122
Additional command options allow you to:
22-
- Remove an address from the block list
23-
- Remove all addresses from the block list
24-
- List currently blocked addresses and last seen time
25-
- Save the current blocked address list as a script
26-
- Disable the block list (removing the extra `iptables` rule
27-
- Customize the table name and/or chain used
2823

29-
Options may be specified in an initialization file.
24+
- Remove an address from the block list
25+
- Remove all addresses from the block list
26+
- List currently blocked addresses and last seen time
27+
- Save the current blocked address list as a script
28+
- Disable the block list (removing the extra `iptables` rule
29+
- Customize the table name and/or chain used
30+
31+
Options, including the desired chain, should be specified in the configuration file.
3032

3133
`ipblock -h` for complete help
3234

@@ -43,18 +45,59 @@ This will create a subdirectory named ipblock-&lt;version&gt;.
4345

4446
`cd` to that directory.
4547

46-
Copy files to your preferred local software directories, e.g.:
47-
cp -p ipblock /usr/local/bin
48-
cp -p config/ipblock.conf /etc/sysconfig/ipblock.conf
48+
`make install`
49+
50+
This will install ipblock in `/usr/local/bin`, which should be in your `PATH`
51+
52+
A `man` page will be installed in `/usr/local/share/man`.
53+
54+
You can install elsewhere by specifying a prefix, as in:
55+
56+
`make prefix=/opt install`
57+
58+
See `Makefile` for other options.
59+
60+
Select an `iptables` chain and specify it in `ipblock.conf`, which will be in `/etc/default` or `/etc/sysconfig`
61+
62+
To avoid locking yourself out, specify a chain that INPUT calls AFTER guard rules
63+
that protect your local network. E.g. in your standard rules, start with:
64+
65+
> iptables -N BLOCKED
66+
> iptables -A -i lo -j ACCEPT
67+
> iptables -A INPUT -s _mylan_,_trustedpublic_ -j ACCEPT
68+
> iptables -A INPUT -j BLOCKED
69+
70+
and in `ipblock.conf`
4971

50-
Make sure that the directory containing `ipblock` is in your **PATH**
72+
> OPTIONS="-C BLOCKED"
5173
5274
Read the disclaimer before running the `ipblock` command.
5375

76+
## De-installation
77+
78+
If you didn't save the unpacked tarball directory, re-create it following the
79+
directions for Installation.
80+
81+
Then
82+
83+
`cd` to that directory.
84+
85+
`make uninstall`
86+
87+
If you selected a different installation directory, include the prefix, e.g.:
88+
89+
`make prefix=/opt uninstall`
90+
91+
If you are uninstalling due to a defect or concern, feel free to create a
92+
bug report.
93+
5494
## License and Disclaimer
55-
Copyright (c) 2017, 2018, 2019, 2020, 2021 Timothe Litt
95+
Copyright (c) 2017, 2018, 2019, 2020, 2021, 2023 Timothe Litt
96+
97+
This is free software; the author disclaims all responsibility for its use, reliability and consequences.
5698

57-
This is free software; the author disclaims all responsibility for its use, reliability and consequences. The name of the author may not be used to endorse any product, but must be retained in the documentation and code. Any modifications must be clearly documented and attributed, and are the responsibility of their author.
99+
The name of the author may not be used to endorse any product, but must be retained in the documentation and code.
100+
Any modifications must be clearly documented and attributed, and are the responsibility of their author.
58101

59102
This notice and the copyright statements must be retained in all copies (complete or partial) of this software and documentation. See LICENSE for details.
60103

config/ipblock.conf

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
# Default options for ipblock
22

3-
# Restriction: option values (e.g. to -D) can't contain a space.
4-
# For -D, use '_' instead
3+
# Restriction: option values (e.g. to -D) that contain a space
4+
# must be in single quotes.
55

6-
#OPTIONS="-D %d-%b-%Y_%T -L"
6+
# Note: only OPTIONS= statements will be used. All will be evaluated.
7+
#
8+
# To avoid lockout, select a suitable chain. E.g.
9+
# OPTIONS="-C mychain"
10+
#
11+
# iptables -A INPUT -p tcp -s mylan/masklen --dport 22 -J ACCEPT
12+
# iptables -A INPUT -j mychain
13+
# ...
14+
# -N mychain
15+
# (automatic RETURN to INPUT from the end)
16+
#
17+
# Since ipblock will add to the front of "mychain", this
18+
# ensures that you will at least have ssh access in the event
19+
# that you block your own access to your system.
20+
#
21+
# If you leave -C at the default, "INPUT", the ipblock rule will
22+
# supersede any protection that you hav established, so don't.
23+
#
24+
# See ipblock -h for more information.
25+
26+
#OPTIONS="-C mychain"
27+
#OPTIONS="$OPTIONS -D '%d-%b-%Y %T'"

0 commit comments

Comments
 (0)