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