use crate::brew::Brew;
use crate::state::Server;
use anyhow::{bail, Context, Result};
use std::collections::BTreeSet;
use std::path::PathBuf;
pub const BASE_MODULES: &[(&str, &str)] = &[
("mpm_event_module", "mod_mpm_event.so"),
("authz_core_module", "mod_authz_core.so"),
("unixd_module", "mod_unixd.so"),
("log_config_module", "mod_log_config.so"),
("mime_module", "mod_mime.so"),
("dir_module", "mod_dir.so"),
("proxy_module", "mod_proxy.so"),
("proxy_fcgi_module", "mod_proxy_fcgi.so"),
("rewrite_module", "mod_rewrite.so"),
("headers_module", "mod_headers.so"),
("setenvif_module", "mod_setenvif.so"),
("env_module", "mod_env.so"),
("alias_module", "mod_alias.so"),
("autoindex_module", "mod_autoindex.so"),
];
pub const SSL_MODULES: &[(&str, &str)] = &[
("ssl_module", "mod_ssl.so"),
("socache_shmcb_module", "mod_socache_shmcb.so"),
];
const MPM_PREFIX: &str = "mpm_";
const PREREQS: &[(&str, &[&str])] = &[
("proxy_ajp_module", &["proxy_module"]),
(
"proxy_balancer_module",
&["proxy_module", "slotmem_shm_module"],
),
("proxy_connect_module", &["proxy_module"]),
("proxy_express_module", &["proxy_module"]),
("proxy_fdpass_module", &["proxy_module"]),
("proxy_ftp_module", &["proxy_module"]),
("proxy_hcheck_module", &["proxy_module", "watchdog_module"]),
("proxy_html_module", &["proxy_module", "xml2enc_module"]),
("proxy_http_module", &["proxy_module"]),
("proxy_scgi_module", &["proxy_module"]),
("proxy_uwsgi_module", &["proxy_module"]),
("proxy_wstunnel_module", &["proxy_module"]),
("lbmethod_bybusyness_module", &["proxy_balancer_module"]),
("lbmethod_byrequests_module", &["proxy_balancer_module"]),
("lbmethod_bytraffic_module", &["proxy_balancer_module"]),
(
"lbmethod_heartbeat_module",
&["proxy_balancer_module", "slotmem_shm_module"],
),
("dav_fs_module", &["dav_module"]),
("dav_lock_module", &["dav_module"]),
("cache_disk_module", &["cache_module"]),
(
"cache_socache_module",
&["cache_module", "socache_shmcb_module"],
),
("session_cookie_module", &["session_module"]),
("session_crypto_module", &["session_module"]),
("session_dbd_module", &["session_module", "dbd_module"]),
("authn_anon_module", &["authn_core_module"]),
("authn_dbd_module", &["authn_core_module", "dbd_module"]),
("authn_dbm_module", &["authn_core_module"]),
("authn_file_module", &["authn_core_module"]),
(
"authn_socache_module",
&["authn_core_module", "socache_shmcb_module"],
),
("authz_dbd_module", &["dbd_module"]),
(
"auth_basic_module",
&[
"authn_core_module",
"authn_file_module",
"authz_user_module",
],
),
(
"auth_digest_module",
&[
"authn_core_module",
"authn_file_module",
"authz_user_module",
],
),
(
"auth_form_module",
&[
"authn_core_module",
"authn_file_module",
"authz_user_module",
"session_module",
"session_cookie_module",
],
),
("brotli_module", &["filter_module"]),
("deflate_module", &["filter_module"]),
("ext_filter_module", &["filter_module"]),
("substitute_module", &["filter_module"]),
("heartbeat_module", &["watchdog_module"]),
(
"heartmonitor_module",
&["watchdog_module", "slotmem_shm_module"],
),
("info_module", &["authz_host_module"]),
("status_module", &["authz_host_module"]),
("ssl_module", &["socache_shmcb_module"]),
];
pub fn modules_dir(brew: &Brew) -> PathBuf {
brew.opt("httpd").join("lib/httpd/modules")
}
pub fn name_for_file(file: &str) -> Option<String> {
let short = file.strip_suffix(".so")?.strip_prefix("mod_")?;
if short.is_empty() {
return None;
}
Some(format!("{short}_module"))
}
pub fn file_for_name(name: &str) -> String {
format!("mod_{}.so", name.strip_suffix("_module").unwrap_or(name))
}
pub fn canonical(input: &str) -> String {
let lower = input.trim().to_lowercase();
let s = lower.strip_suffix(".so").unwrap_or(&lower);
let s = s.strip_prefix("mod_").unwrap_or(s);
if s.ends_with("_module") {
s.to_string()
} else {
format!("{s}_module")
}
}
pub fn is_base(name: &str) -> bool {
BASE_MODULES.iter().any(|(n, _)| *n == name)
}
pub fn is_ssl(name: &str) -> bool {
SSL_MODULES.iter().any(|(n, _)| *n == name)
}
pub fn with_prereqs(names: &[String]) -> Vec<String> {
let mut seen: BTreeSet<String> = BTreeSet::new();
let mut queue: Vec<String> = names.iter().map(|n| canonical(n)).collect();
while let Some(n) = queue.pop() {
if !seen.insert(n.clone()) {
continue;
}
if let Some((_, deps)) = PREREQS.iter().find(|(k, _)| *k == n) {
queue.extend(deps.iter().map(|d| (*d).to_string()));
}
}
seen.into_iter().collect()
}
pub fn dependents_of(name: &str, enabled: &[String]) -> Vec<String> {
let name = canonical(name);
enabled
.iter()
.map(|e| canonical(e))
.filter(|e| *e != name && with_prereqs(std::slice::from_ref(e)).contains(&name))
.collect()
}
pub fn snippet(name: &str) -> Option<&'static str> {
match name {
"status_module" => Some(concat!(
"<IfModule status_module>\n",
" ExtendedStatus On\n",
" <Location /server-status>\n",
" SetHandler server-status\n",
" Require local\n",
" </Location>\n",
"</IfModule>\n\n",
)),
"info_module" => Some(concat!(
"<IfModule info_module>\n",
" <Location /server-info>\n",
" SetHandler server-info\n",
" Require local\n",
" </Location>\n",
"</IfModule>\n\n",
)),
_ => None,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Origin {
Base,
Ssl,
Optional,
}
#[derive(Debug, Clone)]
pub struct Module {
pub name: String,
pub origin: Origin,
pub enabled: bool,
pub implied: bool,
}
pub fn installed(brew: &Brew) -> Result<Vec<String>> {
let dir = modules_dir(brew);
let rd = std::fs::read_dir(&dir).with_context(|| {
format!(
"No Apache modules at {} — is httpd installed?",
dir.display()
)
})?;
let mut names: Vec<String> = rd
.flatten()
.filter_map(|e| e.file_name().into_string().ok())
.filter_map(|f| name_for_file(&f))
.filter(|n| !n.starts_with(MPM_PREFIX))
.collect();
names.sort();
names.dedup();
Ok(names)
}
pub fn catalog(brew: &Brew, server: &Server) -> Result<Vec<Module>> {
let chosen: BTreeSet<String> = server.modules.iter().map(|m| canonical(m)).collect();
let effective: BTreeSet<String> = with_prereqs(&server.modules).into_iter().collect();
let mut names: BTreeSet<String> = installed(brew)?.into_iter().collect();
let dir = modules_dir(brew);
for (name, file) in BASE_MODULES.iter().chain(SSL_MODULES) {
if dir.join(file).exists() {
names.insert((*name).to_string());
}
}
Ok(names
.into_iter()
.map(|name| {
let origin = if is_base(&name) {
Origin::Base
} else if is_ssl(&name) {
Origin::Ssl
} else {
Origin::Optional
};
Module {
enabled: chosen.contains(&name),
implied: !chosen.contains(&name) && effective.contains(&name),
origin,
name,
}
})
.collect())
}
pub fn resolve_choice(brew: &Brew, input: &str) -> Result<String> {
let name = canonical(input);
if name.starts_with(MPM_PREFIX) {
bail!(
"'{name}' is an MPM — only one may be loaded and reeve pins mpm_event. \
Loading a second is a hard startup failure."
);
}
if is_base(&name) {
bail!("'{name}' is always loaded by reeve; nothing to enable.");
}
let installed = installed(brew)?;
if installed.contains(&name) {
return Ok(name);
}
let stem = name.trim_end_matches("_module");
let mut near: Vec<(usize, &String)> = installed
.iter()
.filter_map(|n| {
let other = n.trim_end_matches("_module");
let d = edit_distance(stem, other);
let close = d <= 3 || other.contains(stem) || stem.contains(other);
close.then_some((d, n))
})
.collect();
near.sort_by_key(|(d, n)| (*d, (*n).clone()));
near.truncate(5);
if near.is_empty() {
bail!(
"No module '{}' in {}",
file_for_name(&name),
modules_dir(brew).display()
);
}
bail!(
"No module '{}'. Did you mean: {}?",
file_for_name(&name),
near.iter()
.map(|(_, s)| s.as_str())
.collect::<Vec<_>>()
.join(", ")
);
}
fn edit_distance(a: &str, b: &str) -> usize {
let b: Vec<char> = b.chars().collect();
let mut prev: Vec<usize> = (0..=b.len()).collect();
let mut cur = vec![0usize; b.len() + 1];
for (i, ca) in a.chars().enumerate() {
cur[0] = i + 1;
for (j, cb) in b.iter().enumerate() {
let sub = prev[j] + usize::from(ca != *cb);
cur[j + 1] = sub.min(prev[j + 1] + 1).min(cur[j] + 1);
}
std::mem::swap(&mut prev, &mut cur);
}
prev[b.len()]
}
pub fn load_list(brew: &Brew, server: &Server, needs_ssl: bool) -> Vec<(String, PathBuf)> {
let dir = modules_dir(brew);
let mut emitted: BTreeSet<String> = BTreeSet::new();
let mut out = Vec::new();
let mut push = |name: String, out: &mut Vec<(String, PathBuf)>| {
if !emitted.insert(name.clone()) {
return;
}
let path = dir.join(file_for_name(&name));
if path.exists() {
out.push((name, path));
}
};
for (name, _) in BASE_MODULES {
push((*name).to_string(), &mut out);
}
if needs_ssl {
for (name, _) in SSL_MODULES {
push((*name).to_string(), &mut out);
}
}
for name in with_prereqs(&server.modules) {
push(name, &mut out);
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn names_round_trip_with_the_file_convention() {
assert_eq!(name_for_file("mod_rewrite.so").unwrap(), "rewrite_module");
assert_eq!(
name_for_file("mod_lbmethod_bybusyness.so").unwrap(),
"lbmethod_bybusyness_module"
);
assert_eq!(file_for_name("rewrite_module"), "mod_rewrite.so");
assert_eq!(
file_for_name("socache_shmcb_module"),
"mod_socache_shmcb.so"
);
assert!(name_for_file("httpd.exp").is_none());
assert!(name_for_file("mod_.so").is_none());
}
#[test]
fn edit_distance_measures_typos() {
assert_eq!(edit_distance("deflate", "deflate"), 0);
assert_eq!(edit_distance("defalte", "deflate"), 2);
assert_eq!(edit_distance("proxy_wstunel", "proxy_wstunnel"), 1);
assert_eq!(edit_distance("", "abc"), 3);
assert_eq!(edit_distance("abc", ""), 3);
}
#[test]
fn canonical_accepts_every_spelling() {
for input in [
"rewrite",
"mod_rewrite",
"mod_rewrite.so",
"rewrite_module",
" MOD_Rewrite.SO ",
] {
assert_eq!(canonical(input), "rewrite_module", "input was {input:?}");
}
}
#[test]
fn prereqs_expand_transitively() {
let got = with_prereqs(&["lbmethod_bybusyness".to_string()]);
assert!(got.contains(&"lbmethod_bybusyness_module".to_string()));
assert!(got.contains(&"proxy_balancer_module".to_string()));
assert!(got.contains(&"proxy_module".to_string()));
assert!(got.contains(&"slotmem_shm_module".to_string()));
}
#[test]
fn prereqs_terminate_and_dedupe() {
let got = with_prereqs(&["deflate".to_string(), "brotli".to_string()]);
assert_eq!(
got.iter().filter(|n| *n == "filter_module").count(),
1,
"shared prerequisite should appear once: {got:?}"
);
}
#[test]
fn dependents_block_a_removal_that_would_break_config() {
let enabled = vec!["proxy_balancer".to_string(), "deflate".to_string()];
let deps = dependents_of("slotmem_shm", &enabled);
assert_eq!(deps, vec!["proxy_balancer_module".to_string()]);
assert_eq!(
dependents_of("filter", &enabled),
vec!["deflate_module".to_string()]
);
assert!(dependents_of("deflate", &enabled).is_empty());
}
#[test]
fn every_prereq_key_is_unique() {
let mut keys: Vec<&str> = PREREQS.iter().map(|(k, _)| *k).collect();
let before = keys.len();
keys.sort();
keys.dedup();
assert_eq!(before, keys.len(), "duplicate key in PREREQS");
}
#[test]
fn prereq_graph_has_no_cycles_and_names_are_canonical() {
for (k, deps) in PREREQS {
assert_eq!(canonical(k), *k, "{k} is not a canonical name");
for d in *deps {
assert_eq!(canonical(d), *d, "{d} is not a canonical name");
}
let closure = with_prereqs(&[(*k).to_string()]);
assert!(closure.contains(&(*k).to_string()));
}
}
#[test]
fn base_modules_need_no_prereqs_beyond_themselves() {
for (name, _) in BASE_MODULES {
for dep in with_prereqs(&[(*name).to_string()]) {
assert!(
is_base(&dep) || is_ssl(&dep),
"base module {name} needs {dep}, which reeve doesn't always load"
);
}
}
}
#[test]
fn snippets_only_exist_for_modules_that_declare_authz_host() {
for name in ["status_module", "info_module"] {
assert!(snippet(name).is_some());
assert!(
with_prereqs(&[name.to_string()]).contains(&"authz_host_module".to_string()),
"{name}'s snippet uses `Require local` but doesn't require authz_host"
);
}
assert!(snippet("rewrite_module").is_none());
}
}