Adaugă codul complet al proiectului underchat-ircd

This commit is contained in:
mihaiitdata committed 2026-02-13 00:04:12 +02:00
1 parent caba5224bf
commit 5fdc1cc27c
462 files changed
+164488

No files matched your search

+60
View File
@@ -0,0 +1,60 @@
# Linesync Sidecar Container
# Provides git-based configuration synchronization for Nefarious IRCd
#
# Supports three modes:
# keygen - Generate SSH keypair for git authentication
# setup - Clone the linesync repository (initial setup)
# sync - Continuous sync loop (default)
#
# Automatically detects UID/GID from mounted directories for proper permissions.
FROM debian:12-slim
# Default UID/GID (will be overridden at runtime based on mounted directory ownership)
ENV LINESYNC_UID=1000
ENV LINESYNC_GID=1000
# Install required tools
RUN apt-get update && apt-get install -y --no-install-recommends \
bash \
git \
gawk \
openssh-client \
openssl \
ca-certificates \
curl \
gnupg \
gosu \
&& rm -rf /var/lib/apt/lists/*
# Install Docker CLI (for sending signals to nefarious container)
RUN install -m 0755 -d /etc/apt/keyrings \
&& curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc \
&& chmod a+r /etc/apt/keyrings/docker.asc \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian bookworm stable" > /etc/apt/sources.list.d/docker.list \
&& apt-get update \
&& apt-get install -y --no-install-recommends docker-ce-cli \
&& rm -rf /var/lib/apt/lists/*
# Create user (UID/GID will be adjusted at runtime)
RUN groupadd -g ${LINESYNC_GID} linesync \
&& useradd -u ${LINESYNC_UID} -g ${LINESYNC_GID} -m linesync
# Create directories
RUN mkdir -p /home/linesync/.ssh \
&& mkdir -p /home/linesync/ircd \
&& chown -R linesync:linesync /home/linesync
# Copy scripts
COPY gitsync.sh /home/linesync/gitsync.sh
COPY linesync-entrypoint.sh /home/linesync/entrypoint.sh
RUN chmod +x /home/linesync/gitsync.sh /home/linesync/entrypoint.sh
# SSH config template (will be copied to user's .ssh at runtime)
RUN echo "Host *\n StrictHostKeyChecking accept-new\n UserKnownHostsFile /home/linesync/.ssh/known_hosts" > /etc/ssh/ssh_config.d/linesync.conf
WORKDIR /home/linesync
# Run as root initially; entrypoint will drop privileges after detecting UID/GID
ENTRYPOINT ["/home/linesync/entrypoint.sh"]
CMD ["sync"]
+166
View File
@@ -0,0 +1,166 @@
# Linesync
Git-based configuration sync for Nefarious IRCd. Pulls config updates from a git repository and sends SIGHUP to reload.
## Using Docker Compose
Add linesync to your `docker-compose.yml`:
```yaml
services:
nefarious:
image: ghcr.io/evilnet/nefarious2:latest
container_name: nefarious
volumes:
- ./local.conf:/home/nefarious/ircd/local.conf
ports:
- "6667:6667"
linesync:
image: ghcr.io/evilnet/nefarious2-linesync:latest
depends_on:
- nefarious
volumes:
- ./linesync-ssh:/home/linesync/.ssh
- ./local.conf:/home/linesync/ircd/local.conf
- ./linesync:/home/linesync/ircd/linesync
- /var/run/docker.sock:/var/run/docker.sock
environment:
NEFARIOUS_CONTAINER: nefarious
SYNC_INTERVAL: 300
IRCD_CONF: /home/linesync/ircd/local.conf
```
### Setup
```bash
# Create directories
mkdir -p ./linesync-ssh ./linesync
touch ./local.conf
# Generate SSH key
docker compose run --rm linesync keygen
# Add the printed public key to your git repo's deploy keys
# Clone the linesync repo
docker compose run --rm -e GIT_REPOSITORY=git@github.com:yourorg/linesync-data.git linesync setup
# Start services
docker compose up -d
```
### Manual sync
```bash
docker compose run --rm linesync once
```
---
## Using Standalone Docker
### Setup
```bash
# Build
cd tools/linesync
docker build -t linesync .
# Create directories
mkdir -p ./linesync-ssh ./linesync
touch ./local.conf
# Generate SSH key
docker run --rm -v ./linesync-ssh:/home/linesync/.ssh linesync keygen
# Add the printed public key to your git repo's deploy keys
# Clone the linesync repo
docker run --rm \
-v ./linesync-ssh:/home/linesync/.ssh \
-v ./local.conf:/home/linesync/ircd/local.conf \
-v ./linesync:/home/linesync/ircd/linesync \
-e GIT_REPOSITORY=git@github.com:yourorg/linesync-data.git \
linesync setup
```
### Run continuous sync
```bash
docker run -d \
--name linesync \
-v ./linesync-ssh:/home/linesync/.ssh \
-v ./local.conf:/home/linesync/ircd/local.conf \
-v ./linesync:/home/linesync/ircd/linesync \
-v /var/run/docker.sock:/var/run/docker.sock \
-e NEFARIOUS_CONTAINER=nefarious \
-e SYNC_INTERVAL=300 \
-e IRCD_CONF=/home/linesync/ircd/local.conf \
linesync sync
```
### Run once
```bash
docker run --rm \
-v ./linesync-ssh:/home/linesync/.ssh \
-v ./local.conf:/home/linesync/ircd/local.conf \
-v ./linesync:/home/linesync/ircd/linesync \
-v /var/run/docker.sock:/var/run/docker.sock \
-e IRCD_CONF=/home/linesync/ircd/local.conf \
linesync once
```
### Debug shell
```bash
docker run --rm -it \
-v ./linesync-ssh:/home/linesync/.ssh \
-v ./local.conf:/home/linesync/ircd/local.conf \
-v ./linesync:/home/linesync/ircd/linesync \
linesync shell
```
---
## Environment Variables
| Variable | Default | Description |
|----------|---------|-------------|
| `GIT_REPOSITORY` | — | Git repo URL (required for setup) |
| `NEFARIOUS_CONTAINER` | `nefarious` | Container to send SIGHUP |
| `SYNC_INTERVAL` | `300` | Seconds between syncs |
| `IRCD_CONF` | `/home/linesync/ircd/ircd.conf` | Config file path |
| `CERT_TAG` | — | Git tag for SSL cert sync |
| `CERT_FILE` | `fullchain.pem` | Output path for synced certificate |
## Config File Format
Your git repository needs a `linesync.data` file with IRC config blocks. These get inserted into your config between markers:
```
# BEGIN LINESYNC
... (managed by linesync) ...
# END LINESYNC
```
Markers are added automatically on first sync.
## SSL Certificate Sync
Store certs in git tags:
```bash
git tag -f myserver-cert $(cat fullchain.pem | git hash-object -w --stdin)
git push origin :refs/tags/myserver-cert
git push --tags
```
Then set `CERT_TAG=myserver-cert` in your environment. To write to a specific file (e.g., `ircd.pem`), also set `CERT_FILE=/home/linesync/ircd/ircd.pem`.
## Notes
- UID/GID is auto-detected from bind mounts — files will be owned by your host user
- Docker socket access grants container control privileges
- Use read-only deploy keys
+334
View File
@@ -0,0 +1,334 @@
#!/bin/bash
# gitysnc.sh, Copyright (c) 2015 Rubin
# based on linesync.sh (c) 2002 Arjen Wolfs
#
# The code contained is in this file is licenced under the terms
# and conditions as specified in the GNU General Public License.
#
# This program should be run from crontab, i.e something like:
# 0 0 * * * /home/irc/bin/gitsync.sh -s /home/irc/.ssh/id_rsa /home/irc/lib/ircd.conf /home/irc/lib/ircd.pid
#
usage() {
echo "Help: "
echo " $0 [-h|-i repository] <-p ircd.pem|-s id_rsa> <ircd.conf> <ircd.pid>"
echo ""
echo " -h - this help"
echo " -p ircd.pem - convert this ircd.pem certificate to an ssh key and use that instead of the default ~/.ssh/id_rsa"
echo " -s id_rsa - Full path to your ssh private key to use for git access (defaults to ~/.ssh/id_rsa)"
echo " -i repository-url - Perform initial setup, needed only once to set up. Provide git URL as argument"
echo " -c tagname - Load a certificate from this git tagged object (eg, servername-cert)"
echo " -C certfile - Write the certificate to this file (default: fullchain.pem in ircd.conf directory)"
echo " <ircd.conf> - Full path to your ircd.conf file"
echo " <ircd.pid> - Full path to your ircd.pid file"
echo ""
}
#Handle argument parsing
while getopts "hi:p:s:c:C:" opt; do
case $opt in
h)
usage
exit
;;
i)
dosetup="yes"
repository="$OPTARG"
;;
p)
ircdkey="yes"
kpath="$OPTARG"
;;
s)
skey="$OPTARG"
;;
c)
certtag="$OPTARG"
;;
C)
certfile="$OPTARG"
;;
\?)
echo "Unknown option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
esac
done
shift $((OPTIND-1))
# This checks for the presence of an executable file in $PATH
locate_program() {
if [ ! -x "`which $1 2>&1`" ]; then
echo "You don't seem to have $1. Sorry."
exit 1
fi
}
# This checks for the presence of any file
check_file() {
if [ ! -f "$1" ]; then
echo "There doesn't appear to be a $1. Sorry."
exit 1
fi
}
# Try to find programs we will need
locate_program openssl
locate_program git
locate_program egrep
locate_program diff
locate_program chmod
locate_program readlink
# try to find GNU awk
awk_cmd=`which gawk`
if [ $? -ne 0 ]; then
awk_cmd=""
fi
if [ -z "$awk_cmd" ]; then
locate_program awk
is_gawk=`echo | awk --version | head -1 | egrep '^GNU.+$'`
if [ -z "$is_gawk" ]; then
echo "Your version of awk is not GNU awk. Sorry."
echo "Try: # sudo apt-get install gawk"
exit 1
fi
awk_cmd="awk"
fi
# Check for required command line parameters
if [ -z "$1" ] || [ -z "$2" ] ; then
echo "Error: No ircd.conf or ircd.pid specified"
usage
exit 1
fi
if [ -n "$skey" ] && [ -n "$ircdkey" ]; then
echo "Error: You cannot use -s and -p together. Pick one method to authenicate."
usage
exit;
fi
if [ -z "$skey" ] && [ -z "$ircdkey" ]; then
echo "Error: You must provide -s or -p. Pick one method to authenicate."
usage
exit;
fi
if [ -z "$skey" ]; then
skey="$HOME/.ssh/id_rsa"
fi
# check and set up stuff
diff_cmd="diff"
cpath="$1"
ppath="$2"
check_file "$cpath"
dpath=`dirname "$cpath"`
dpath=`readlink -f "$dpath"`
lpath="$dpath/linesync"
#check_file $lpath
save_dir="$PWD"; cd "$dpath"
tpath="$PWD"; cd "$save_dir"
tmp_path="$dpath/tmp"
ipath="$tmp_path/ssh.pem"
mkdir "$tmp_path" > /dev/null 2>&1
# Not all versions of date support %s, work around it
TS=`date +%Y%m%d%H%M%S`
TMPFILE="$tmp_path/linesync.$TS"
TMPCERT="$tmp_path/cert.$TS"
echo '#!/bin/bash' > "$tmp_path/git.sh"
#If they specified -p, we generate the git ssh key from ircd.pem
if [ -n "$ircdkey" ]; then
check_file "$kpath"
# first get the private key by just grabbing the lines that match...
awk '/BEGIN .*PRIVATE KEY/,/END .*PRIVATE KEY/' "$kpath" > "$ipath"
# Then we'll get the public key more properly..
if ! openssl x509 -in "$kpath" -pubkey -noout >> "$ipath"; then
echo "Error: I could not use $kpath as a key for some reason. Stopping!"
exit
fi
chmod 600 "$tmp_path/ssh.pem"
#Override git's ssh command so we can force our custom identity and no password
echo -e "ssh -oPasswordAuthentication=no -i \"$ipath\" \"\$1\" \"\$2\"\n" >> "$tmp_path/git.sh"
else
#Override git's ssh command so we can force our custom identity and no password
echo -e "ssh -oPasswordAuthentication=no -i \"$skey\" \"\$1\" \"\$2\"\n" >> "$tmp_path/git.sh"
fi
chmod a+x "$tmp_path/git.sh"
export GIT_SSH="$tmp_path/git.sh"
if [ "$dosetup" = "yes" ]; then
echo "Doing initial setup with repository $repository" >&2
# Check if destination already has a git repo
if [ -d "$lpath/.git" ]; then
echo "Doing setup.. but $lpath already contains a git repository."
echo "Remove it first if you want to re-clone: rm -rf $lpath/*"
exit 2
fi
# Check if directory exists and is not empty (allow empty dirs for bind mounts)
if [ -d "$lpath" ] && [ -n "$(ls -A "$lpath" 2>/dev/null)" ]; then
echo "Doing setup.. but destination directory $lpath already exists and is not empty."
echo "Move it out of the way and try again"
exit 2
fi
echo "Note: your public key (linesync admin will have added this to keydir):"
if [ -n "$ircdkey" ]; then
ssh-keygen -i -m PKCS8 -f "$tmp_path/ssh.pem"
#cat $ipath
else
cat "$skey".pub
fi
prevdir=`pwd`
cd "$dpath"
git clone "$repository" "$lpath"
if [ -d "$lpath"/.git ]; then
echo "Initial setup success"
exit 0
else
echo "Problem with initial setup. See above"
exit 5
fi
fi
#Check for the git repository
if [ ! -d "$lpath" ]; then
echo "Cannot find a git repository at $lpath."
echo "check ircd.conf path argument, or re-run with -i <repository> to perform initial setup"
usage
exit 6
fi
if [ ! -d "$lpath"/.git ]; then
echo "Error: $lpath is not a git repository. ?!"
usage
exit 10
fi
#update the repository from upstream
prevdir=`pwd`
cd "$lpath"
git reset -q --hard origin/master
git fetch --tags -f --quiet
git merge -q origin/master
if [ ! -z "$certtag" ]; then
#Store the cert in a temp file
git show "$certtag" > "$TMPCERT"
fi
cd "$prevdir"
#Copy the data to the temp file
cp "$lpath/linesync.data" "$TMPFILE"
if [ ! -s "$TMPFILE" ]; then
echo "Unable find retrieve $lpath/linesync.data, Sorry."
rm "$TMPFILE" > /dev/null 2>&1
exit 1
fi
# check our ircd.conf
ircd_setup=`egrep '^# (BEGIN|END) LINESYNC$' "$cpath"|wc -l`
if [ $ircd_setup != 2 ]; then
cp "$cpath" "$cpath.orig"
echo "Performing initial merge on $cpath, original file saved as $cpath.orig."
echo "# Do NOT remove the following line, linesync.sh depends on it!" >> $cpath
echo "# BEGIN LINESYNC" >> $cpath
echo "# END LINESYNC" >> $cpath
echo "# Do not remove the previous line, linesync.sh depends on it!" >> $cpath
# Do an initial merge to remove duplicates
inpath="$tmp_path/linesync.tmp.$TS"
$awk_cmd '
{
if (!loaded_template) {
command="cat " tempfile; tlines=0;
while ((command | getline avar) > 0) { template[tlines]=avar; tlines++ }
close(command)
loaded_template++
}
dup_line=0
for (i=0; i<tlines; i++) {
if (tolower($0)==tolower(template[i])) { dup_line++; break }
}
if (!dup_line) print $0
} ' "tempfile='$TMPFILE'" < "$cpath" > "$inpath"
else
inpath="$cpath"
fi
# Replace the marked block in ircd.conf with the new version
$awk_cmd '
$0=="# BEGIN LINESYNC" { chop++; print; next }
$0=="# END LINESYNC" {
command="cat " syncfile
while ((command | getline avar) > 0) { print avar }
close(command)
chop--
}
{ if (!chop) print $0 }
' "syncfile='$TMPFILE'" < "$inpath" > "$tmp_path/linesync.new.$TS"
# run a diff between current and new confs to see if we updated anything
# no point sending the ircd a -HUP if this is not needed, especially on a
# busy network, such as Undernet.
diff=`"$diff_cmd" "$cpath" "$tmp_path/linesync.new.$TS"`
if [ ! -z "$diff" ]; then
# Changes were detected
# Back up the current ircd.conf and replace it with the new one
cp "$cpath" "$dpath/ircd.conf.bk"
cp "$tmp_path/linesync.new.$TS" "$cpath"
# Rehash ircd (without caring wether or not it succeeds)
kill -HUP `cat "$ppath" 2>/dev/null` > /dev/null 2>&1
#todo: kill iauthd?
fi
if [ ! -z "$certtag" ]; then
# Use provided certfile path or default to fullchain.pem
: "${certfile:=$dpath/fullchain.pem}"
if [ ! -f "$certfile" ]; then
cdiff="yes"
else
cdiff=`"$diff_cmd" "$certfile" "$TMPCERT"`
fi
if [ ! -z "$cdiff" ]; then
#Changes detected
if [ -f "$certfile" ]; then
cp "$certfile" "${certfile}.backup"
fi
cp "$TMPCERT" "$certfile"
# Rehash pem files (without caring wether or not it succeeds)
kill -USR1 `cat "$ppath" 2>/dev/null` > /dev/null 2>&1
fi
fi
# (Try to) clean up
if [ -n "$tmp_path" ] && [ -d "$tmp_path" ]; then
rm -rf "$tmp_path" #> /dev/null 2>&1
fi
# That's it...
+33
View File
@@ -0,0 +1,33 @@
=== Some musings about a way to store lets-encrypt-generated ircd pem keys in git without flooding out the history ===
Save the certificate
git tag -f servername-cert $(command to generate cert | git hash-object -w --stdin)
Read the certificate
git show servername-cert
note about abusing remote tags:
https://stackoverflow.com/questions/19298600/tag-already-exists-in-the-remote-error-after-recreating-the-git-tag
Delete the tag on the server
git push origin :refs/tags/servername-cert
pushing and pulling include the tags
git pull --tags
git push --tags
So the sequence would be:
* Get new certificate from LE
* Put new certificate in git tag using the hash-object thing with -f to overwrite the old
* Delete the old tag on the server with the :refs/tags/ thing
* git push --tags to send the new tag(s) to the server
Then in gitsync.sh we need to do git pull --tags to get the latest (or just our needed tag?)
and save it to the ircd.pem file (key file will need to be seperated) and HUP the ircd if it changed.
+289
View File
@@ -0,0 +1,289 @@
#!/bin/bash
# Linesync Sidecar Entrypoint
# Supports multiple modes of operation for configuration synchronization
set -e
# =============================================================================
# UID/GID Auto-Detection and Privilege Dropping
# =============================================================================
# If running as root, detect the UID/GID from mounted directories and drop
# privileges to match. This allows bind mounts to work without manual -u flags.
if [ "$(id -u)" = "0" ]; then
# Detect UID/GID from mounted directories (in order of preference)
TARGET_UID=""
TARGET_GID=""
for detect_path in /home/linesync/ircd/ircd.conf ; do
if [ -d "$detect_path" ]; then
TARGET_UID=$(stat -c %u "$detect_path")
TARGET_GID=$(stat -c %g "$detect_path")
# Skip if owned by root (probably not a bind mount)
if [ "$TARGET_UID" != "0" ]; then
break
fi
fi
done
# Default to 1000:1000 if detection failed or got root
: "${TARGET_UID:=1000}"
: "${TARGET_GID:=1000}"
# Get current linesync UID/GID
CURRENT_UID=$(id -u linesync)
CURRENT_GID=$(id -g linesync)
# Modify user/group if needed
if [ "$TARGET_GID" != "$CURRENT_GID" ]; then
groupmod -g "$TARGET_GID" linesync 2>/dev/null || true
fi
if [ "$TARGET_UID" != "$CURRENT_UID" ]; then
usermod -u "$TARGET_UID" linesync 2>/dev/null || true
fi
# Fix ownership of home directory
chown -R linesync:linesync /home/linesync 2>/dev/null || true
# Ensure SSH config exists
if [ ! -f /home/linesync/.ssh/config ]; then
mkdir -p /home/linesync/.ssh
echo -e "Host *\n StrictHostKeyChecking accept-new\n UserKnownHostsFile /home/linesync/.ssh/known_hosts" > /home/linesync/.ssh/config
chmod 700 /home/linesync/.ssh
chmod 600 /home/linesync/.ssh/config
chown -R linesync:linesync /home/linesync/.ssh
fi
# Re-exec this script as linesync user
exec gosu linesync "$0" "$@"
fi
# =============================================================================
# Main Entrypoint (running as linesync user)
# =============================================================================
# Configuration with defaults
: "${SYNC_INTERVAL:=300}" # Sync interval in seconds (default: 5 minutes)
: "${IRCD_CONF:=/home/linesync/ircd/ircd.conf}"
: "${IRCD_PID:=/home/linesync/ircd/ircd.pid}"
: "${SSH_KEY:=/home/linesync/.ssh/id_ed25519}"
: "${NEFARIOUS_CONTAINER:=nefarious}"
: "${GIT_REPOSITORY:=}"
: "${CERT_TAG:=}"
: "${CERT_FILE:=}" # Output path for certificate (default: fullchain.pem in conf dir)
GITSYNC="/home/linesync/gitsync.sh"
show_help() {
echo "Linesync Sidecar Container"
echo ""
echo "Modes:"
echo " keygen Generate a new SSH keypair for git authentication"
echo " setup Clone the linesync git repository (initial setup)"
echo " sync Run continuous sync loop (default)"
echo " once Run sync once and exit"
echo " shell Start an interactive shell"
echo " help Show this help message"
echo ""
echo "Environment Variables:"
echo " SYNC_INTERVAL Seconds between sync attempts (default: 300)"
echo " IRCD_CONF Path to ircd.conf (default: /home/linesync/ircd/ircd.conf)"
echo " IRCD_PID Path to ircd.pid (default: /home/linesync/ircd/ircd.pid)"
echo " SSH_KEY Path to SSH private key (default: /home/linesync/.ssh/id_ed25519)"
echo " NEFARIOUS_CONTAINER Name of nefarious container to signal (default: nefarious)"
echo " GIT_REPOSITORY Git repository URL (required for setup mode)"
echo " CERT_TAG Git tag for SSL certificate (optional)"
echo " CERT_FILE Output path for certificate (default: fullchain.pem in conf dir)"
echo ""
echo "Volume Mounts:"
echo " /home/linesync/.ssh SSH keys directory"
echo " /home/linesync/ircd IRCd config directory (shared with nefarious)"
echo " /var/run/docker.sock Docker socket (for sending SIGHUP)"
echo ""
}
# Generate SSH keypair
do_keygen() {
local keytype="${1:-ed25519}"
local sshdir="/home/linesync/.ssh"
local keyfile="${sshdir}/id_${keytype}"
# Check if .ssh directory exists
if [ ! -d "$sshdir" ]; then
echo "Error: SSH directory $sshdir does not exist"
echo "Create the directory on the host first: mkdir -p ./linesync-ssh"
exit 1
fi
# Check if directory is writable
if [ ! -w "$sshdir" ]; then
echo "Error: SSH directory $sshdir is not writable"
echo "Check that the directory exists and has correct permissions"
exit 1
fi
# Ensure proper directory permissions
chmod 700 "$sshdir" 2>/dev/null || true
if [ -f "$keyfile" ]; then
echo "Key already exists at $keyfile"
echo "Public key:"
cat "${keyfile}.pub"
return 0
fi
echo "Generating ${keytype} keypair..."
ssh-keygen -t "$keytype" -f "$keyfile" -N "" -C "linesync@$(hostname)"
# Set proper permissions on the key files
chmod 600 "$keyfile"
chmod 644 "${keyfile}.pub"
echo ""
echo "Key generated successfully!"
echo ""
echo "Public key (add this to your git repository's deploy keys):"
echo "================================================================"
cat "${keyfile}.pub"
echo "================================================================"
}
# Initial repository setup
do_setup() {
if [ -z "$GIT_REPOSITORY" ]; then
echo "Error: GIT_REPOSITORY environment variable is required for setup"
exit 1
fi
if [ ! -f "$SSH_KEY" ]; then
echo "Error: SSH key not found at $SSH_KEY"
echo "Run 'keygen' mode first to create a keypair"
exit 1
fi
echo "Setting up linesync repository..."
echo "Repository: $GIT_REPOSITORY"
echo "SSH Key: $SSH_KEY"
echo "Config: $IRCD_CONF"
# Run gitsync.sh in setup mode
"$GITSYNC" -s "$SSH_KEY" -i "$GIT_REPOSITORY" "$IRCD_CONF" "$IRCD_PID"
}
# Signal nefarious container to reload config
signal_reload() {
if [ -S /var/run/docker.sock ]; then
echo "Sending SIGHUP to container: $NEFARIOUS_CONTAINER"
docker kill --signal=SIGHUP "$NEFARIOUS_CONTAINER" 2>/dev/null || \
echo "Warning: Could not signal container (may not be running)"
else
echo "Warning: Docker socket not available, cannot signal container"
echo "Config changes will take effect on next ircd restart"
fi
}
# Run a single sync
do_sync_once() {
local sync_args="-s $SSH_KEY"
if [ -n "$CERT_TAG" ]; then
sync_args="$sync_args -c $CERT_TAG"
if [ -n "$CERT_FILE" ]; then
sync_args="$sync_args -C $CERT_FILE"
fi
fi
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Running sync..."
# Create a wrapper that signals via docker instead of using PID file
# We'll run gitsync.sh but handle the signaling ourselves
local tmp_pid="/tmp/fake.pid"
echo "99999" > "$tmp_pid"
# Capture gitsync output to check if changes were made
local before_md5=""
local after_md5=""
if [ -f "$IRCD_CONF" ]; then
before_md5=$(md5sum "$IRCD_CONF" 2>/dev/null | cut -d' ' -f1)
fi
# Run gitsync (it will try to HUP pid 99999 which will fail silently)
"$GITSYNC" $sync_args "$IRCD_CONF" "$tmp_pid" 2>&1 || true
if [ -f "$IRCD_CONF" ]; then
after_md5=$(md5sum "$IRCD_CONF" 2>/dev/null | cut -d' ' -f1)
fi
# If config changed, signal the container
if [ "$before_md5" != "$after_md5" ] && [ -n "$after_md5" ]; then
echo "Configuration changed, signaling reload..."
signal_reload
fi
rm -f "$tmp_pid"
}
# Continuous sync loop
do_sync_loop() {
# Handle shutdown signals gracefully
trap 'echo "[$(date "+%Y-%m-%d %H:%M:%S")] Shutting down..."; exit 0' SIGTERM SIGINT
echo "Starting linesync daemon"
echo "Sync interval: ${SYNC_INTERVAL}s"
echo "Config: $IRCD_CONF"
echo "Container: $NEFARIOUS_CONTAINER"
echo ""
# Check prerequisites
if [ ! -f "$SSH_KEY" ]; then
echo "Error: SSH key not found at $SSH_KEY"
echo "Run 'keygen' mode first, or mount your key"
exit 1
fi
local linesync_dir
linesync_dir=$(dirname "$IRCD_CONF")/linesync
if [ ! -d "$linesync_dir" ]; then
echo "Error: Linesync repository not found at $linesync_dir"
echo "Run 'setup' mode first to clone the repository"
exit 1
fi
# Main loop
while true; do
do_sync_once
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Sleeping ${SYNC_INTERVAL}s..."
# Sleep in background so trap can interrupt
sleep "$SYNC_INTERVAL" &
wait $!
done
}
# Main entrypoint
case "${1:-sync}" in
keygen)
do_keygen "${2:-ed25519}"
;;
setup)
do_setup
;;
sync)
do_sync_loop
;;
once)
do_sync_once
;;
shell|bash|sh)
exec /bin/bash
;;
help|--help|-h)
show_help
;;
*)
echo "Unknown command: $1"
show_help
exit 1
;;
esac
+17
View File
@@ -0,0 +1,17 @@
# Configuration for linesync.sh
#
# - This file must be placed in the same directory as ircd.conf
# - Note that all URL's *MUST* have a trailing /
# - Since we are using wget, you could use ftp:// URL's as well if you want to
# Where do we get our ircd.conf update?
LINE_SERVER="http://some.domain/"
# Check servers, as many as you like, seperated by spaces
LINE_CHECK="http://host1.other.domain/lsync/ http://host-19.domain-x.net/ http://www.domain.what"
# What .conf lines are allowed in the downloaded updates?
# Pipe seperated - for Undernet use "Uworld|Jupe|Quarantine|Kill" (kKQU in .11 terms)
ALLOWED_LINES="Uworld|Jupe|Quarantine|Kill"
+241
View File
@@ -0,0 +1,241 @@
#!/bin/sh
# linesync.sh, Copyright (c) 2002 Arjen Wolfs
# 20020604, sengaia@undernet.org
# 20050417, daniel@undernet.org - modified for u2.10.12
# $Id: linesync.sh 1368 2005-04-17 16:59:49Z entrope $
#
# The code contained is in this file is licenced under the terms
# and conditions as specified in the GNU General Public License.
#
# linesync.sh - centralized ircd.conf updates.
# The purpose of this little shell script is to allow a section of an ircd.conf to be
# updated from a central location. Hence it is intended to facilitate the automated
# distribution of Kill, Jupe, Quarantine and Uworld lines; or any other .conf lines you
# may wish to keep synchronized accross all servers on a network.
#
# This script will download a file called linesync from a specified web server (see
# below for configuration), and calculate an md5sum from it. It will then download
# a file called linesync.sum from a configurable number of other web servers and
# compare the contents of these files against the checksum calculated. If any of the
# downloaded checksums mismatch, the program will abort. This provides security to
# the centralized update mechanism - in order for it to be compromised, multiple
# web servers would have to compromised.
#
# If all checksums match, the script inspects the .conf lines contained within the
# downloaded file. If any .conf lines are found that are not specifically allowed,
# the program will abort. This will prevent malicious/dangerous .conf lines (such as
# Operator or Connect lines) from being inserted into ircd.conf.
#
# If all the checks mentioned above are passed, the script checks ircd.conf for a section
# that begins with "# BEGIN LINESYNC", and ends with "# END LINESYNC". The section contained
# between these two comments is the section maintained by this program. If these lines are not
# found in the ircd.conf, they will be appended to it.
# Next, the script will build a new ircd.conf by removing all lines present between the two
# commented lines mentioned above, and replace them with the contents of the file downloaded.
#
# Once this has been completed, ircd.conf is backed up and replaced with the newly built version,
# and ircd will be rehashed.
#
# Configuration: This script requires two parameters - the full path to your ircd.conf, and the
# full path to your ircd.pid. It will look for a configuration file called linesync.conf in the
# same directory as ircd.conf. See the included sample linesync.conf for information on how to
# set it up. Obviously, you will need to have web server(s) to use for the distribution of your
# .conf update and checksums. This script requires the presence of wget and md5sum, and various
# other programs that should be present by default on any Unix system.
#
# This program should be run from crontab, i.e something like:
# 0 0 * * * /home/irc/bin/linesync.sh /home/irc/lib/ircd.conf /home/irc/lib/ircd.pid
#
# This program has been tested on and works on FreeBSD, Solaris, and Linux.
# md5sum is included in GNU textutils.
#
# Good Luck!
# Arjen Wolfs (sengaia@undernet.org), June 9 2002.
#
# This checks for the presence of an executable file in $PATH
locate_program() {
if [ ! -x "`which $1 2>&1`" ]; then
echo "You don't seem to have $1. Sorry."
exit 1
fi
}
# This checks for the presence of any file
check_file() {
if [ ! -f "$1" ]; then
echo "There doesn't appear to be a $1. Sorry."
exit 1
fi
}
# Try to find programs we will need
locate_program wget && locate_program egrep && locate_program diff
# try to find GNU awk
awk_cmd=`which gawk`
if [ $? -ne 0 ]; then
awk_cmd=""
fi
# try to find an appropriate md5 program
# BSD md5 capability courtesy of spale
md5_cmd=`which md5sum`
if [ -z "$md5_cmd" ]; then
md5_cmd=`which md5`
if [ -z "$md5_cmd" ]; then
echo "No MD5 capable programs found (I looked for md5sum and md5)."
exit
else
md5_cmd="$md5_cmd -q"
fi
fi
if [ -z "$awk_cmd" ]; then
locate_program awk
is_gawk=`echo | awk --version | head -1 | egrep '^GNU.+$'`
if [ -z "$is_gawk" ]; then
echo "Your version of awk is not GNU awk. Sorry."
exit 1
fi
awk_cmd="awk"
fi
# Check for required command line parameters
if [ -z "$1" -o -z "$2" ]; then
echo "Usage: $0 <conf_path> <pid_path>"
echo " <conf_path> Full path to ircd.conf (/home/irc/lib/ircd.conf)"
echo " <pid_path> Full path to ircd.pid (/home/irc/lib/ircd.pid)"
exit 1
fi
# check and set up stuff
diff_cmd="diff"
cpath=$1
ppath=$2
check_file $cpath
dpath=`dirname $cpath`
lpath="$dpath/linesync.conf"
check_file $lpath
save_dir=$PWD; cd $dpath
tpath=$PWD; cd $save_dir
tmp_path="$dpath/tmp"
mkdir $tmp_path > /dev/null 2>&1
# load and check configuration
. $lpath
if [ -z "$LINE_SERVER" -o -z "$LINE_CHECK" -o -z "$ALLOWED_LINES" ]; then
echo "Please setup $lpath correctly."
exit 1
fi
# Not all versions of date support %s, work around it
TS=`date +%Y%m%d%H%M%S`
TMPFILE="$tmp_path/linesync.$TS"
LSFILE="$LINE_SERVER""linesync"
# Attempt to download our .conf update
wget --cache=off --quiet --output-document=$TMPFILE $LSFILE > /dev/null 2>&1
if [ ! -s "$TMPFILE" ]; then
echo "Unable to retrieve $LSFILE. Sorry."
rm $TMPFILE > /dev/null 2>&1
exit 1
fi
# Check whether the file contains any disallowed .conf lines
bad_lines=`egrep '^[^'$ALLOWED_LINES'|#]+' $TMPFILE`
if [ ! -z "$bad_lines" ]; then
echo "The file downloaded in $TMPFILE contains the following disallowed line(s):"
echo $bad_lines
exit 1
fi
# Check whether somebody tried to sneak a second block onto some line
bad_lines=`egrep -i '}[ ]*;[ ]*[a-z]+[ ]*{' $TMPFILE`
if [ ! -z "$bad_lines" ] ; then
echo "The file downloaded in $TMPFILE contains the following multi-block line(s):"
echo $bad_lines
exit 1
fi
# check our ircd.conf
ircd_setup=`egrep '^# (BEGIN|END) LINESYNC$' $cpath|wc -l`
if [ $ircd_setup != 2 ]; then
cp $cpath $cpath.orig
echo "Performing initial merge on $cpath, original file saved as $cpath.orig."
echo "# Do NOT remove the following line, linesync.sh depends on it!" >> $cpath
echo "# BEGIN LINESYNC" >> $cpath
echo "# END LINESYNC" >> $cpath
echo "# Do not remove the previous line, linesync.sh depends on it!" >> $cpath
# Do an initial merge to remove duplicates
inpath="$tmp_path/linesync.tmp.$TS"
$awk_cmd '
{
if (!loaded_template) {
command="cat " tempfile; tlines=0;
while ((command | getline avar) > 0) { template[tlines]=avar; tlines++ }
close(command)
loaded_template++
}
dup_line=0
for (i=0; i<tlines; i++) {
if (tolower($0)==tolower(template[i])) { dup_line++; break }
}
if (!dup_line) print $0
} ' tempfile=$TMPFILE < $cpath > $inpath
else
inpath=$cpath
fi
# Get the checksum
CKSUM=`$md5_cmd $TMPFILE|cut -d' ' -f1`
check_file="$tmp_path/linesync.sum.$TS"
for ck_server in $LINE_CHECK; do
sumfile="$ck_server""linesync.sum"
wget --cache=off --quiet --output-document=$check_file $sumfile > /dev/null 2>&1
if [ ! -s "$check_file" ]; then
echo "Unable to retrieve checksum from $sumfile"
exit 1
fi
if [ "$CKSUM" != "`cat $check_file`" ]; then
echo "Checksum retrieved from $sumfile does not match!"
exit 1
fi
rm -f $check_file
done
# It all checks out, proceed...
# Replace the marked block in ircd.conf with the new version
$awk_cmd '
$0=="# BEGIN LINESYNC" { chop++; print; next }
$0=="# END LINESYNC" {
command="cat " syncfile
while ((command | getline avar) > 0) { print avar }
close(command)
chop--
}
{ if (!chop) print $0 }
' syncfile=$TMPFILE < $inpath > $tmp_path/linesync.new.$TS
# run a diff between current and new confs to see if we updated anything
# no point sending the ircd a -HUP if this is not needed, especially on a
# busy network, such as Undernet.
diff=`$diff_cmd $cpath $tmp_path/linesync.new.$TS`
if [ ! -z "$diff" ]; then
# Changes were detected
# Back up the current ircd.conf and replace it with the new one
cp $cpath $dpath/ircd.conf.bk
cp $tmp_path/linesync.new.$TS $cpath
# Rehash ircd (without caring wether or not it succeeds)
kill -HUP `cat $ppath 2>/dev/null` > /dev/null 2>&1
fi
# (Try to) clean up
rm -rf $tmp_path > /dev/null 2>&1
# That's it...