FROM ubuntu:22.04

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
       openssh-server bash gcc libc6-dev \
    && rm -rf /var/lib/apt/lists/*

RUN useradd -ms /bin/bash steve \
    && echo "steve:bookworm42" | chpasswd \
    && useradd -ms /bin/bash alex \
    && echo "alex:bookworm42" | chpasswd

RUN mkdir -p /opt/village_storage \
    && chmod 1777 /opt/village_storage \
    && touch /opt/village_storage/blocker \
    && chown alex:alex /opt/village_storage/blocker

RUN echo "Note from Alex: I think you have already found my account and password" > /opt/village_storage/notes.txt

RUN chown -R alex:alex /opt/village_storage/ \
    && chmod -R 1777 /opt/village_storage/

RUN echo 'vsosh{st1cky_0wn3r_d3l3t3}' > /root/flag \
    && chmod 600 /root/flag

COPY get_flag.c /tmp/get_flag.c
RUN gcc -o /opt/village_storage/get_flag /tmp/get_flag.c \
    && chown root:root /opt/village_storage/get_flag \
    && chmod 4755 /opt/village_storage/get_flag \
    && rm /tmp/get_flag.c

RUN sed -ri 's/^#?PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config \
    && sed -ri 's/^#?PasswordAuthentication.*/PasswordAuthentication yes/' /etc/ssh/sshd_config \
    && sed -ri 's|^#?PrintMotd.*|PrintMotd no|' /etc/ssh/sshd_config \
    && sed -ri 's|^#?UsePAM.*|UsePAM yes|' /etc/ssh/sshd_config \
    && sed -ri 's|^#?PrintLastLog.*|PrintLastLog no|' /etc/ssh/sshd_config || true \
    && sed -ri 's/^(session\s+optional\s+pam_motd\.so.*)$/# \1/' /etc/pam.d/sshd

RUN chmod -x /etc/update-motd.d/* 2>/dev/null || true

COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod 755 /usr/local/bin/entrypoint.sh

RUN mkdir -p /run/sshd

EXPOSE 22

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
