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