# Build the ism-1558 word-count PAM module.
#
# Build on a build host and ship the resulting .so; a hardened target should not
# need a compiler. `make install` is provided for convenience in test environments.

CC      ?= cc
CFLAGS  ?= -O2 -Wall -Wextra -Werror -fPIC
LDLIBS  := -lpam
# PAM looks for modules where libpam-modules installed its own. Debian's pam.pc
# defines no securedir, so pkg-config returns an empty value; ask dpkg instead.
SECDIR  ?= $(patsubst %/pam_unix.so,%,$(shell dpkg -L libpam-modules:$$(dpkg --print-architecture) 2>/dev/null | grep '/security/pam_unix\.so$$' | head -n 1))

MODULE  := pam_ism_wordcount.so

all: $(MODULE)

$(MODULE): pam_ism_wordcount.c
	$(CC) $(CFLAGS) -shared -o $@ $< $(LDLIBS)

install: $(MODULE)
	@test -n "$(SECDIR)" && test -f "$(SECDIR)/pam_unix.so" || \
		{ echo "error: PAM module directory not found; pass SECDIR=<dir>" >&2; exit 1; }
	install -m 0644 $(MODULE) $(SECDIR)/$(MODULE)

clean:
	rm -f $(MODULE)

.PHONY: all install clean
