Merge pull request #20421 from mayflower/refactor/clamav-service

clamav service: refactor
This commit is contained in:
Franz Pletz 2016-11-17 19:37:53 +01:00 committed by GitHub
commit 6a5ae21c47
3 changed files with 73 additions and 35 deletions

View File

@ -30,6 +30,8 @@ with lib;
(mkRenamedOptionModule [ "services" "gitlab" "stateDir" ] [ "services" "gitlab" "statePath" ]) (mkRenamedOptionModule [ "services" "gitlab" "stateDir" ] [ "services" "gitlab" "statePath" ])
(mkRemovedOptionModule [ "services" "gitlab" "satelliteDir" ] "") (mkRemovedOptionModule [ "services" "gitlab" "satelliteDir" ] "")
(mkRenamedOptionModule [ "services" "clamav" "updater" "config" ] [ "services" "clamav" "updater" "extraConfig" ])
# Old Grub-related options. # Old Grub-related options.
(mkRenamedOptionModule [ "boot" "initrd" "extraKernelModules" ] [ "boot" "initrd" "kernelModules" ]) (mkRenamedOptionModule [ "boot" "initrd" "extraKernelModules" ] [ "boot" "initrd" "kernelModules" ])
(mkRenamedOptionModule [ "boot" "extraKernelParams" ] [ "boot" "kernelParams" ]) (mkRenamedOptionModule [ "boot" "extraKernelParams" ] [ "boot" "kernelParams" ])

View File

@ -3,26 +3,37 @@ with lib;
let let
clamavUser = "clamav"; clamavUser = "clamav";
stateDir = "/var/lib/clamav"; stateDir = "/var/lib/clamav";
runDir = "/var/run/clamav"; runDir = "/run/clamav";
logDir = "/var/log/clamav";
clamavGroup = clamavUser; clamavGroup = clamavUser;
cfg = config.services.clamav; cfg = config.services.clamav;
pkg = pkgs.clamav;
clamdConfigFile = pkgs.writeText "clamd.conf" '' clamdConfigFile = pkgs.writeText "clamd.conf" ''
DatabaseDirectory ${stateDir} DatabaseDirectory ${stateDir}
LocalSocket ${runDir}/clamd.ctl LocalSocket ${runDir}/clamd.ctl
LogFile ${logDir}/clamav.log
PidFile ${runDir}/clamd.pid PidFile ${runDir}/clamd.pid
TemporaryDirectory /tmp
User clamav User clamav
Foreground yes
${cfg.daemon.extraConfig} ${cfg.daemon.extraConfig}
''; '';
pkg = pkgs.clamav;
freshclamConfigFile = pkgs.writeText "freshclam.conf" ''
DatabaseDirectory ${stateDir}
Foreground yes
Checks ${toString cfg.updater.frequency}
${cfg.updater.extraConfig}
DatabaseMirror database.clamav.net
'';
in in
{ {
options = { options = {
services.clamav = { services.clamav = {
daemon = { daemon = {
enable = mkEnableOption "clamd daemon"; enable = mkEnableOption "ClamAV clamd daemon";
extraConfig = mkOption { extraConfig = mkOption {
type = types.lines; type = types.lines;
@ -34,16 +45,27 @@ in
}; };
}; };
updater = { updater = {
enable = mkEnableOption "freshclam updater"; enable = mkEnableOption "ClamAV freshclam updater";
frequency = mkOption { frequency = mkOption {
type = types.int;
default = 12; default = 12;
description = '' description = ''
Number of database checks per day. Number of database checks per day.
''; '';
}; };
config = mkOption { interval = mkOption {
type = types.str;
default = "hourly";
description = ''
How often freshclam is invoked. See systemd.time(7) for more
information about the format.
'';
};
extraConfig = mkOption {
type = types.lines;
default = ""; default = "";
description = '' description = ''
Extra configuration for freshclam. Contents will be added verbatim to the Extra configuration for freshclam. Contents will be added verbatim to the
@ -68,50 +90,53 @@ in
gid = config.ids.gids.clamav; gid = config.ids.gids.clamav;
}; };
services.clamav.updater.config = mkIf cfg.updater.enable '' environment.etc."clamav/freshclam.conf".source = freshclamConfigFile;
DatabaseDirectory ${stateDir} environment.etc."clamav/clamd.conf".source = clamdConfigFile;
Foreground yes
Checks ${toString cfg.updater.frequency}
DatabaseMirror database.clamav.net
'';
systemd.services.clamd = mkIf cfg.daemon.enable { systemd.services.clamav-daemon = mkIf cfg.daemon.enable {
description = "ClamAV daemon (clamd)"; description = "ClamAV daemon (clamd)";
path = [ pkg ]; after = mkIf cfg.updater.enable [ "clamav-freshclam.service" ];
after = [ "network.target" "freshclam.service" ]; requires = mkIf cfg.updater.enable [ "clamav-freshclam.service" ];
requires = [ "freshclam.service" ];
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
restartTriggers = [ clamdConfigFile ];
preStart = '' preStart = ''
mkdir -m 0755 -p ${logDir}
mkdir -m 0755 -p ${runDir} mkdir -m 0755 -p ${runDir}
chown ${clamavUser}:${clamavGroup} ${logDir}
chown ${clamavUser}:${clamavGroup} ${runDir} chown ${clamavUser}:${clamavGroup} ${runDir}
''; '';
serviceConfig = { serviceConfig = {
ExecStart = "${pkg}/bin/clamd --config-file=${clamdConfigFile}"; ExecStart = "${pkg}/bin/clamd";
Type = "forking"; ExecReload = "${pkgs.coreutils}/bin/kill -USR2 $MAINPID";
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID"; PrivateTmp = "yes";
Restart = "on-failure"; PrivateDevices = "yes";
RestartSec = "10s"; PrivateNetwork = "yes";
StartLimitInterval = "1min";
}; };
}; };
systemd.services.freshclam = mkIf cfg.updater.enable { systemd.timers.clamav-freshclam = mkIf cfg.updater.enable {
description = "ClamAV updater (freshclam)"; description = "Timer for ClamAV virus database updater (freshclam)";
after = [ "network.target" ]; wantedBy = [ "timers.target" ];
wantedBy = [ "multi-user.target" ]; timerConfig = {
path = [ pkg ]; OnCalendar = cfg.updater.interval;
Unit = "clamav-freshclam.service";
};
};
systemd.services.clamav-freshclam = mkIf cfg.updater.enable {
description = "ClamAV virus database updater (freshclam)";
restartTriggers = [ freshclamConfigFile ];
preStart = '' preStart = ''
mkdir -m 0755 -p ${stateDir} mkdir -m 0755 -p ${stateDir}
chown ${clamavUser}:${clamavGroup} ${stateDir} chown ${clamavUser}:${clamavGroup} ${stateDir}
''; '';
serviceConfig = { serviceConfig = {
ExecStart = "${pkg}/bin/freshclam --daemon --config-file=${pkgs.writeText "freshclam.conf" cfg.updater.config}"; Type = "oneshot";
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID"; ExecStart = "${pkg}/bin/freshclam";
Restart = "on-failure"; PrivateTmp = "yes";
RestartSec = "10s"; PrivateDevices = "yes";
StartLimitInterval = "1min";
}; };
}; };
}; };

View File

@ -10,11 +10,17 @@ stdenv.mkDerivation rec {
sha256 = "0yh2q318bnmf2152g2h1yvzgqbswn0wvbzb8p4kf7v057shxcyqn"; sha256 = "0yh2q318bnmf2152g2h1yvzgqbswn0wvbzb8p4kf7v057shxcyqn";
}; };
# don't install sample config files into the absolute sysconfdir folder
postPatch = ''
substituteInPlace Makefile.in --replace ' etc ' ' '
'';
buildInputs = [ buildInputs = [
zlib bzip2 libxml2 openssl ncurses curl libiconv libmilter pcre zlib bzip2 libxml2 openssl ncurses curl libiconv libmilter pcre
]; ];
configureFlags = [ configureFlags = [
"--sysconfdir=/etc/clamav"
"--with-zlib=${zlib.dev}" "--with-zlib=${zlib.dev}"
"--with-libbz2-prefix=${bzip2.dev}" "--with-libbz2-prefix=${bzip2.dev}"
"--with-iconv-dir=${libiconv}" "--with-iconv-dir=${libiconv}"
@ -26,6 +32,11 @@ stdenv.mkDerivation rec {
"--enable-milter" "--enable-milter"
]; ];
postInstall = ''
mkdir $out/etc
cp etc/*.sample $out/etc
'';
meta = with stdenv.lib; { meta = with stdenv.lib; {
homepage = http://www.clamav.net; homepage = http://www.clamav.net;
description = "Antivirus engine designed for detecting Trojans, viruses, malware and other malicious threats"; description = "Antivirus engine designed for detecting Trojans, viruses, malware and other malicious threats";