]> git.proxmox.com Git - proxmox-backup.git/blame - src/config.rs
tools: get_hardware_address: better error handling
[proxmox-backup.git] / src / config.rs
CommitLineData
a8f268af
DM
1//! Proxmox Backup Server Configuration library
2//!
3//! This library contains helper to read, parse and write the
4//! configuration files.
5
f7d4e4b5 6use anyhow::{bail, format_err, Error};
f8fd5095
DM
7use std::path::PathBuf;
8use nix::sys::stat::Mode;
9use openssl::rsa::{Rsa};
10use openssl::x509::{X509Builder};
11use openssl::pkey::PKey;
a8f268af 12
f8fd5095 13use proxmox::tools::fs::{CreateOptions, replace_file};
9ea4bce4 14use proxmox::try_block;
e18a6c9e 15
a8f268af
DM
16use crate::buildcfg;
17
5c6cdf98 18pub mod acl;
423e6561 19pub mod cached_user_info;
90d515c9 20pub mod datastore;
e6263c26 21pub mod jobstate;
f34d4401 22pub mod network;
90d515c9 23pub mod remote;
6f652b1b 24pub mod sync;
90d515c9 25pub mod user;
9b2bad7a 26pub mod verify;
5c20e2da 27
a8f268af
DM
28/// Check configuration directory permissions
29///
30/// For security reasons, we want to make sure they are set correctly:
31/// * owned by 'backup' user/group
32/// * nobody else can read (mode 0700)
8fdef1a8 33pub fn check_configdir_permissions() -> Result<(), Error> {
a8f268af 34 let cfgdir = buildcfg::CONFIGDIR;
f74a03da
DM
35
36 let backup_user = crate::backup::backup_user()?;
37 let backup_uid = backup_user.uid.as_raw();
38 let backup_gid = backup_user.gid.as_raw();
a8f268af
DM
39
40 try_block!({
41 let stat = nix::sys::stat::stat(cfgdir)?;
42
43 if stat.st_uid != backup_uid {
5c20e2da 44 bail!("wrong user ({} != {})", stat.st_uid, backup_uid);
a8f268af
DM
45 }
46 if stat.st_gid != backup_gid {
5c20e2da 47 bail!("wrong group ({} != {})", stat.st_gid, backup_gid);
a8f268af
DM
48 }
49
50 let perm = stat.st_mode & 0o777;
51 if perm != 0o700 {
5c20e2da 52 bail!("wrong permission ({:o} != {:o})", perm, 0o700);
a8f268af
DM
53 }
54 Ok(())
5c20e2da
WB
55 })
56 .map_err(|err| {
57 format_err!(
58 "configuration directory '{}' permission problem - {}",
59 cfgdir,
60 err
61 )
62 })
a8f268af
DM
63}
64
65pub fn create_configdir() -> Result<(), Error> {
a8f268af 66 let cfgdir = buildcfg::CONFIGDIR;
a8f268af
DM
67
68 match nix::unistd::mkdir(cfgdir, Mode::from_bits_truncate(0o700)) {
5c20e2da 69 Ok(()) => {}
a8f268af 70 Err(nix::Error::Sys(nix::errno::Errno::EEXIST)) => {
8fdef1a8 71 check_configdir_permissions()?;
a8f268af 72 return Ok(());
5c20e2da
WB
73 }
74 Err(err) => bail!(
75 "unable to create configuration directory '{}' - {}",
76 cfgdir,
77 err
78 ),
a8f268af
DM
79 }
80
f74a03da 81 let backup_user = crate::backup::backup_user()?;
a8f268af 82
f74a03da
DM
83 nix::unistd::chown(cfgdir, Some(backup_user.uid), Some(backup_user.gid))
84 .map_err(|err| {
85 format_err!(
86 "unable to set configuration directory '{}' permissions - {}",
87 cfgdir,
88 err
89 )
90 })
a8f268af 91}
f8fd5095
DM
92
93/// Update self signed node certificate.
94pub fn update_self_signed_cert(force: bool) -> Result<(), Error> {
95
96 let backup_user = crate::backup::backup_user()?;
97
98 create_configdir()?;
99
100 let key_path = PathBuf::from(configdir!("/proxy.key"));
101 let cert_path = PathBuf::from(configdir!("/proxy.pem"));
102
103 if key_path.exists() && cert_path.exists() && !force { return Ok(()); }
104
105 let rsa = Rsa::generate(4096).unwrap();
106
107 let priv_pem = rsa.private_key_to_pem()?;
108
109 replace_file(
110 &key_path,
111 &priv_pem,
112 CreateOptions::new()
113 .perm(Mode::from_bits_truncate(0o0640))
114 .owner(nix::unistd::ROOT)
115 .group(backup_user.gid),
116 )?;
117
118 let mut x509 = X509Builder::new()?;
119
120 x509.set_version(2)?;
121
122 let today = openssl::asn1::Asn1Time::days_from_now(0)?;
123 x509.set_not_before(&today)?;
124 let expire = openssl::asn1::Asn1Time::days_from_now(365*1000)?;
125 x509.set_not_after(&expire)?;
126
127 let nodename = proxmox::tools::nodename();
128 let mut fqdn = nodename.to_owned();
129
130 let resolv_conf = crate::api2::node::dns::read_etc_resolv_conf()?;
131 if let Some(search) = resolv_conf["search"].as_str() {
132 fqdn.push('.');
133 fqdn.push_str(search);
134 }
135
136 // we try to generate an unique 'subject' to avoid browser problems
137 //(reused serial numbers, ..)
138 let uuid = proxmox::tools::uuid::Uuid::generate();
139
140 let mut subject_name = openssl::x509::X509NameBuilder::new()?;
141 subject_name.append_entry_by_text("O", "Proxmox Backup Server")?;
142 subject_name.append_entry_by_text("OU", &format!("{:X}", uuid))?;
143 subject_name.append_entry_by_text("CN", &fqdn)?;
144 let subject_name = subject_name.build();
145
146 x509.set_subject_name(&subject_name)?;
147 x509.set_issuer_name(&subject_name)?;
148
149 let bc = openssl::x509::extension::BasicConstraints::new(); // CA = false
150 let bc = bc.build()?;
151 x509.append_extension(bc)?;
152
153 let usage = openssl::x509::extension::ExtendedKeyUsage::new()
154 .server_auth()
155 .build()?;
156 x509.append_extension(usage)?;
157
158 let context = x509.x509v3_context(None, None);
159
160 let mut alt_names = openssl::x509::extension::SubjectAlternativeName::new();
161
162 alt_names.ip("127.0.0.1");
163 alt_names.ip("::1");
164
165 alt_names.dns("localhost");
166
167 if nodename != "localhost" { alt_names.dns(nodename); }
168 if nodename != fqdn { alt_names.dns(&fqdn); }
169
170 let alt_names = alt_names.build(&context)?;
171
172 x509.append_extension(alt_names)?;
173
174 let pub_pem = rsa.public_key_to_pem()?;
175 let pubkey = PKey::public_key_from_pem(&pub_pem)?;
176
177 x509.set_pubkey(&pubkey)?;
178
179 let context = x509.x509v3_context(None, None);
180 let ext = openssl::x509::extension::SubjectKeyIdentifier::new().build(&context)?;
181 x509.append_extension(ext)?;
182
183 let context = x509.x509v3_context(None, None);
184 let ext = openssl::x509::extension::AuthorityKeyIdentifier::new()
185 .keyid(true)
186 .build(&context)?;
187 x509.append_extension(ext)?;
188
189 let privkey = PKey::from_rsa(rsa)?;
190
191 x509.sign(&privkey, openssl::hash::MessageDigest::sha256())?;
192
193 let x509 = x509.build();
194 let cert_pem = x509.to_pem()?;
195
196 replace_file(
197 &cert_path,
198 &cert_pem,
199 CreateOptions::new()
200 .perm(Mode::from_bits_truncate(0o0640))
201 .owner(nix::unistd::ROOT)
202 .group(backup_user.gid),
203 )?;
204
205 Ok(())
206}