1#[cfg(any(feature = "nethsm", feature = "yubihsm2"))]
4use std::collections::HashMap;
5use std::{
6 fs::{File, Permissions, create_dir_all, read_dir, set_permissions, write},
7 io::Write,
8 os::{linux::fs::MetadataExt, unix::fs::PermissionsExt},
9 path::{Path, PathBuf},
10 process::{Child, Command},
11 str::FromStr,
12 thread,
13 time,
14};
15
16use change_user_run::{create_users, get_command};
17use log::debug;
18#[cfg(feature = "nethsm")]
19use nethsm::{FullCredentials, UserId};
20use signstar_common::{backend::BackendType, system_user::get_home_base_dir_path};
21#[cfg(feature = "nethsm")]
22use signstar_crypto::AdministrativeSecretHandling;
23#[cfg(any(feature = "nethsm", feature = "yubihsm2"))]
24use signstar_crypto::passphrase::Passphrase;
25use tempfile::NamedTempFile;
26
27use crate::config::{Config, ConfigSystemUserIds, MappingAuthorizedKeyEntry};
28#[cfg(feature = "nethsm")]
29use crate::{admin_credentials::AdminCredentials, nethsm::NetHsmAdminCredentials};
30#[cfg(any(feature = "nethsm", feature = "yubihsm2"))]
32pub mod impl_any {
33 use super::*;
34 use crate::config::UserBackendConnectionFilter;
35
36 impl SystemUserConfig {
37 pub fn new(settings: SystemUserConfigSettings) -> Self {
39 Self {
40 backend_credentials: None,
41 create_secrets: settings.create_secrets,
42 create_ssh_authorized_keys: settings.create_ssh_authorized_keys,
43 }
44 }
45
46 pub fn apply(&mut self, config: &Config) -> Result<(), crate::Error> {
52 if self.create_secrets {
53 let user_backend_connections =
54 config.user_backend_connections(&[UserBackendConnectionFilter::NonAdmin]);
55
56 let mut creds = HashMap::new();
57 for user_backend_connection in user_backend_connections {
58 let Some(user_creds) =
59 user_backend_connection.create_non_admin_backend_user_secrets()?
60 else {
61 continue;
62 };
63
64 for user_cred in user_creds.iter() {
65 creds.insert(user_cred.user(), user_cred.passphrase().clone());
66 }
67 }
68
69 self.backend_credentials = Some(creds);
70 }
71
72 if self.create_ssh_authorized_keys {
73 let user_backend_connections =
74 config.user_backend_connections(&[UserBackendConnectionFilter::NonAdmin]);
75 for user_backend_connection in user_backend_connections {
76 user_backend_connection.write_authorized_key_entry()?;
77 }
78
79 for mapping in config.system().mappings() {
80 mapping.write_authorized_key_entry()?;
81 }
82 }
83
84 Ok(())
85 }
86
87 pub fn backend_credentials(&self) -> Option<&HashMap<String, Passphrase>> {
93 self.backend_credentials.as_ref()
94 }
95 }
96
97 impl SystemPrepareConfig {
98 pub fn apply(&mut self) -> Result<Option<BackgroundProcess>, crate::Error> {
114 if self.machine_id {
115 write_machine_id()?;
116 }
117
118 let background_process = if self.credentials_socket {
119 Some(start_credentials_socket()?)
120 } else {
121 None
122 };
123
124 if let Some(config_file_location) = self.signstar_config.location {
125 create_config(config_file_location, self.signstar_config.variant)?;
126
127 if let Some(mut system_user_config) = self.signstar_config.system_user_config.take()
128 {
129 let config = Config::from_str(&String::from_utf8_lossy(
130 self.signstar_config.variant.as_config_bytes(),
131 ))?;
132 create_unix_users_and_homes(&config)?;
133 system_user_config.apply(&config)?;
134 self.signstar_config.system_user_config = Some(system_user_config.clone())
135 }
136 }
137
138 Ok(background_process)
139 }
140 }
141}
142
143#[cfg(not(any(feature = "nethsm", feature = "yubihsm2")))]
145mod impl_none {
146 use super::*;
147
148 impl SystemUserConfig {
149 pub fn new(settings: SystemUserConfigSettings) -> Self {
151 Self {
152 create_ssh_authorized_keys: settings.create_ssh_authorized_keys,
153 }
154 }
155
156 pub fn apply(&self, config: &Config) -> Result<(), crate::Error> {
166 if self.create_ssh_authorized_keys {
167 for mapping in config.system().mappings() {
168 mapping.write_authorized_key_entry()?;
169 }
170 }
171
172 Ok(())
173 }
174 }
175
176 impl SystemPrepareConfig {
177 pub fn apply(&self) -> Result<Option<BackgroundProcess>, crate::Error> {
193 if self.machine_id {
194 write_machine_id()?;
195 }
196
197 let background_process = if self.credentials_socket {
198 Some(start_credentials_socket()?)
199 } else {
200 None
201 };
202
203 if let Some(config_file_location) = self.signstar_config.location {
204 create_config(config_file_location, self.signstar_config.variant)?;
205
206 if let Some(system_user_config) = self.signstar_config.system_user_config.as_ref() {
207 let config = Config::from_str(&String::from_utf8_lossy(
208 self.signstar_config.variant.as_config_bytes(),
209 ))?;
210 create_unix_users_and_homes(&config)?;
211 system_user_config.apply(&config)?;
212 }
213 }
214
215 Ok(background_process)
216 }
217 }
218}
219
220const NO_BACKEND_ADMIN_PLAINTEXT_NON_ADMIN_PLAINTEXT: &[u8] =
225 include_bytes!("../../fixtures/config/no_backend/admin-plaintext-non-admin-plaintext.yaml");
226
227const NO_BACKEND_ADMIN_PLAINTEXT_NON_ADMIN_SYSTEMD_CREDS: &[u8] =
232 include_bytes!("../../fixtures/config/no_backend/admin-plaintext-non-admin-systemd-creds.yaml");
233
234const NO_BACKEND_ADMIN_SYSTEMD_CREDS_NON_ADMIN_PLAINTEXT: &[u8] =
239 include_bytes!("../../fixtures/config/no_backend/admin-systemd-creds-non-admin-plaintext.yaml");
240
241const NO_BACKEND_ADMIN_SYSTEMD_CREDS_NON_ADMIN_SYSTEMD_CREDS: &[u8] = include_bytes!(
246 "../../fixtures/config/no_backend/admin-systemd-creds-non-admin-systemd-creds.yaml"
247);
248
249const NO_BACKEND_ADMIN_SSS_NON_ADMIN_PLAINTEXT: &[u8] =
254 include_bytes!("../../fixtures/config/no_backend/admin-sss-non-admin-plaintext.yaml");
255
256const NO_BACKEND_ADMIN_SSS_NON_ADMIN_SYSTEMD_CREDS: &[u8] =
261 include_bytes!("../../fixtures/config/no_backend/admin-sss-non-admin-systemd-creds.yaml");
262
263const ONLY_NETHSM_ADMIN_PLAINTEXT_NON_ADMIN_PLAINTEXT: &[u8] =
268 include_bytes!("../../fixtures/config/nethsm_backend/admin-plaintext-non-admin-plaintext.yaml");
269
270const ONLY_NETHSM_ADMIN_PLAINTEXT_NON_ADMIN_SYSTEMD_CREDS: &[u8] = include_bytes!(
275 "../../fixtures/config/nethsm_backend/admin-plaintext-non-admin-systemd-creds.yaml"
276);
277
278const ONLY_NETHSM_ADMIN_PLAINTEXT_NON_ADMIN_SYSTEMD_CREDS_SINGLE_CONNECTION: &[u8] = include_bytes!(
283 "../../fixtures/config/nethsm_backend/admin-plaintext-non-admin-systemd-creds-single-connection.yaml"
284);
285
286const ONLY_NETHSM_ADMIN_SYSTEMD_CREDS_NON_ADMIN_PLAINTEXT: &[u8] = include_bytes!(
291 "../../fixtures/config/nethsm_backend/admin-systemd-creds-non-admin-plaintext.yaml"
292);
293
294const ONLY_NETHSM_ADMIN_SYSTEMD_CREDS_NON_ADMIN_SYSTEMD_CREDS: &[u8] = include_bytes!(
299 "../../fixtures/config/nethsm_backend/admin-systemd-creds-non-admin-systemd-creds.yaml"
300);
301
302const ONLY_NETHSM_ADMIN_SSS_NON_ADMIN_PLAINTEXT: &[u8] =
307 include_bytes!("../../fixtures/config/nethsm_backend/admin-sss-non-admin-plaintext.yaml");
308
309const ONLY_NETHSM_ADMIN_SSS_NON_ADMIN_SYSTEMD_CREDS: &[u8] =
314 include_bytes!("../../fixtures/config/nethsm_backend/admin-sss-non-admin-systemd-creds.yaml");
315
316const ONLY_YUBIHSM2_ADMIN_PLAINTEXT_NON_ADMIN_PLAINTEXT: &[u8] = include_bytes!(
321 "../../fixtures/config/yubihsm2_backend/admin-plaintext-non-admin-plaintext.yaml"
322);
323
324const ONLY_YUBIHSM2_ADMIN_PLAINTEXT_NON_ADMIN_SYSTEMD_CREDS: &[u8] = include_bytes!(
329 "../../fixtures/config/yubihsm2_backend/admin-plaintext-non-admin-systemd-creds.yaml"
330);
331
332const ONLY_YUBIHSM2_ADMIN_SYSTEMD_CREDS_NON_ADMIN_PLAINTEXT: &[u8] = include_bytes!(
337 "../../fixtures/config/yubihsm2_backend/admin-systemd-creds-non-admin-plaintext.yaml"
338);
339
340const ONLY_YUBIHSM2_ADMIN_SYSTEMD_CREDS_NON_ADMIN_SYSTEMD_CREDS: &[u8] = include_bytes!(
345 "../../fixtures/config/yubihsm2_backend/admin-systemd-creds-non-admin-systemd-creds.yaml"
346);
347
348const ONLY_YUBIHSM2_ADMIN_SSS_NON_ADMIN_PLAINTEXT: &[u8] =
353 include_bytes!("../../fixtures/config/yubihsm2_backend/admin-sss-non-admin-plaintext.yaml");
354
355const ONLY_YUBIHSM2_ADMIN_SSS_NON_ADMIN_SYSTEMD_CREDS: &[u8] =
360 include_bytes!("../../fixtures/config/yubihsm2_backend/admin-sss-non-admin-systemd-creds.yaml");
361
362const ONLY_YUBIHSM2_HTTP_ADMIN_PLAINTEXT_NON_ADMIN_PLAINTEXT: &[u8] = include_bytes!(
367 "../../fixtures/config/yubihsm2_backend/admin-plaintext-non-admin-plaintext-http.yaml"
368);
369
370const ONLY_YUBIHSM2_MOCKHSM_ADMIN_PLAINTEXT_NON_ADMIN_PLAINTEXT: &[u8] = include_bytes!(
375 "../../fixtures/config/yubihsm2_mockhsm_backend/admin-plaintext-non-admin-plaintext.yaml"
376);
377
378const ONLY_YUBIHSM2_MOCKHSM_ADMIN_PLAINTEXT_NON_ADMIN_SYSTEMD_CREDS: &[u8] = include_bytes!(
383 "../../fixtures/config/yubihsm2_mockhsm_backend/admin-plaintext-non-admin-systemd-creds.yaml"
384);
385
386const ONLY_YUBIHSM2_MOCKHSM_ADMIN_SYSTEMD_CREDS_NON_ADMIN_PLAINTEXT: &[u8] = include_bytes!(
391 "../../fixtures/config/yubihsm2_mockhsm_backend/admin-systemd-creds-non-admin-plaintext.yaml"
392);
393
394const ONLY_YUBIHSM2_MOCKHSM_ADMIN_SYSTEMD_CREDS_NON_ADMIN_SYSTEMD_CREDS: &[u8] = include_bytes!(
399 "../../fixtures/config/yubihsm2_mockhsm_backend/admin-systemd-creds-non-admin-systemd-creds.yaml"
400);
401
402const ONLY_YUBIHSM2_MOCKHSM_ADMIN_SSS_NON_ADMIN_PLAINTEXT: &[u8] = include_bytes!(
407 "../../fixtures/config/yubihsm2_mockhsm_backend/admin-sss-non-admin-plaintext.yaml"
408);
409
410const ONLY_YUBIHSM2_MOCKHSM_ADMIN_SSS_NON_ADMIN_SYSTEMD_CREDS: &[u8] = include_bytes!(
415 "../../fixtures/config/yubihsm2_mockhsm_backend/admin-sss-non-admin-systemd-creds.yaml"
416);
417
418const ALL_BACKENDS_ADMIN_PLAINTEXT_NON_ADMIN_PLAINTEXT: &[u8] =
423 include_bytes!("../../fixtures/config/all_backends/admin-plaintext-non-admin-plaintext.yaml");
424
425const ALL_BACKENDS_ADMIN_PLAINTEXT_NON_ADMIN_SYSTEMD_CREDS: &[u8] = include_bytes!(
430 "../../fixtures/config/all_backends/admin-plaintext-non-admin-systemd-creds.yaml"
431);
432
433const ALL_BACKENDS_ADMIN_SYSTEMD_CREDS_NON_ADMIN_PLAINTEXT: &[u8] = include_bytes!(
438 "../../fixtures/config/all_backends/admin-systemd-creds-non-admin-plaintext.yaml"
439);
440
441const ALL_BACKENDS_ADMIN_SYSTEMD_CREDS_NON_ADMIN_SYSTEMD_CREDS: &[u8] = include_bytes!(
446 "../../fixtures/config/all_backends/admin-systemd-creds-non-admin-systemd-creds.yaml"
447);
448
449const ALL_BACKENDS_ADMIN_SSS_NON_ADMIN_PLAINTEXT: &[u8] =
454 include_bytes!("../../fixtures/config/all_backends/admin-sss-non-admin-plaintext.yaml");
455
456const ALL_BACKENDS_ADMIN_SSS_NON_ADMIN_SYSTEMD_CREDS: &[u8] =
461 include_bytes!("../../fixtures/config/all_backends/admin-sss-non-admin-systemd-creds.yaml");
462
463#[derive(Debug, thiserror::Error)]
465pub enum Error {
466 #[error("Timeout of {timeout}ms reached while {context}")]
468 Timeout {
469 timeout: u64,
471
472 context: String,
474 },
475}
476
477#[derive(Clone, Copy, Debug, Default)]
479pub enum ConfigFileLocation {
480 Run,
482
483 Etc,
485
486 #[default]
488 UsrShare,
489}
490
491impl ConfigFileLocation {
492 pub fn to_parent_dir_path(&self) -> PathBuf {
494 match self {
495 ConfigFileLocation::Run => PathBuf::from(Config::RUN_OVERRIDE_CONFIG_DIR),
496 ConfigFileLocation::Etc => PathBuf::from(Config::ETC_OVERRIDE_CONFIG_DIR),
497 ConfigFileLocation::UsrShare => PathBuf::from(Config::DEFAULT_CONFIG_DIR),
498 }
499 }
500}
501
502impl From<ConfigFileLocation> for PathBuf {
503 fn from(value: ConfigFileLocation) -> Self {
504 value
505 .to_parent_dir_path()
506 .join(format!("{}.yaml", Config::CONFIG_NAME))
507 }
508}
509
510#[derive(Clone, Copy, Debug, Default)]
512pub enum ConfigFileVariant {
513 NoBackendAdminPlaintextNonAdminPlaintext,
518
519 NoBackendAdminPlaintextNonAdminSystemdCreds,
524
525 NoBackendAdminSystemdCredsNonAdminPlaintext,
530
531 NoBackendAdminSystemdCredsNonAdminSystemdCreds,
536
537 NoBackendAdminSssNonAdminPlaintext,
542
543 NoBackendAdminSssNonAdminSystemdCreds,
548
549 OnlyNetHsmBackendAdminPlaintextNonAdminPlaintext,
554
555 OnlyNetHsmBackendAdminPlaintextNonAdminSystemdCreds,
560
561 OnlyNetHsmBackendAdminPlaintextNonAdminSystemdCredsSingleConnection,
566
567 OnlyNetHsmBackendAdminSystemdCredsNonAdminPlaintext,
572
573 OnlyNetHsmBackendAdminSystemdCredsNonAdminSystemdCreds,
578
579 OnlyNetHsmBackendAdminSssNonAdminPlaintext,
584
585 OnlyNetHsmBackendAdminSssNonAdminSystemdCreds,
590
591 OnlyYubiHsm2BackendAdminPlaintextNonAdminPlaintext,
596
597 OnlyYubiHsm2BackendAdminPlaintextNonAdminSystemdCreds,
602
603 OnlyYubiHsm2BackendAdminSystemdCredsNonAdminPlaintext,
608
609 OnlyYubiHsm2BackendAdminSystemdCredsNonAdminSystemdCreds,
614
615 OnlyYubiHsm2BackendAdminSssNonAdminPlaintext,
620
621 OnlyYubiHsm2BackendAdminSssNonAdminSystemdCreds,
626
627 OnlyYubiHsm2HttpBackendAdminPlaintextNonAdminPlaintext,
632
633 OnlyYubiHsm2MockHsmBackendAdminPlaintextNonAdminPlaintext,
638
639 OnlyYubiHsm2MockHsmBackendAdminPlaintextNonAdminSystemdCreds,
644
645 OnlyYubiHsm2MockHsmBackendAdminSystemdCredsNonAdminPlaintext,
650
651 OnlyYubiHsm2MockHsmBackendAdminSystemdCredsNonAdminSystemdCreds,
656
657 OnlyYubiHsm2MockHsmBackendAdminSssNonAdminPlaintext,
662
663 OnlyYubiHsm2MockHsmBackendAdminSssNonAdminSystemdCreds,
668
669 AllBackendsAdminPlaintextNonAdminPlaintext,
674
675 AllBackendsAdminPlaintextNonAdminSystemdCreds,
680
681 AllBackendsAdminSystemdCredsNonAdminPlaintext,
686
687 AllBackendsAdminSystemdCredsNonAdminSystemdCreds,
692
693 AllBackendsAdminSssNonAdminPlaintext,
698
699 #[default]
704 AllBackendsAdminSssNonAdminSystemdCreds,
705}
706
707impl ConfigFileVariant {
708 pub fn as_config_bytes(&self) -> &[u8] {
710 match self {
711 ConfigFileVariant::NoBackendAdminPlaintextNonAdminPlaintext => {
712 NO_BACKEND_ADMIN_PLAINTEXT_NON_ADMIN_PLAINTEXT
713 }
714 ConfigFileVariant::NoBackendAdminPlaintextNonAdminSystemdCreds => {
715 NO_BACKEND_ADMIN_PLAINTEXT_NON_ADMIN_SYSTEMD_CREDS
716 }
717 ConfigFileVariant::NoBackendAdminSystemdCredsNonAdminPlaintext => {
718 NO_BACKEND_ADMIN_SYSTEMD_CREDS_NON_ADMIN_PLAINTEXT
719 }
720 ConfigFileVariant::NoBackendAdminSystemdCredsNonAdminSystemdCreds => {
721 NO_BACKEND_ADMIN_SYSTEMD_CREDS_NON_ADMIN_SYSTEMD_CREDS
722 }
723 ConfigFileVariant::NoBackendAdminSssNonAdminPlaintext => {
724 NO_BACKEND_ADMIN_SSS_NON_ADMIN_PLAINTEXT
725 }
726 ConfigFileVariant::NoBackendAdminSssNonAdminSystemdCreds => {
727 NO_BACKEND_ADMIN_SSS_NON_ADMIN_SYSTEMD_CREDS
728 }
729 ConfigFileVariant::OnlyNetHsmBackendAdminPlaintextNonAdminPlaintext => {
730 ONLY_NETHSM_ADMIN_PLAINTEXT_NON_ADMIN_PLAINTEXT
731 }
732 ConfigFileVariant::OnlyNetHsmBackendAdminPlaintextNonAdminSystemdCreds => {
733 ONLY_NETHSM_ADMIN_PLAINTEXT_NON_ADMIN_SYSTEMD_CREDS
734 }
735 ConfigFileVariant::OnlyNetHsmBackendAdminPlaintextNonAdminSystemdCredsSingleConnection => {
736 ONLY_NETHSM_ADMIN_PLAINTEXT_NON_ADMIN_SYSTEMD_CREDS_SINGLE_CONNECTION
737 }
738 ConfigFileVariant::OnlyNetHsmBackendAdminSystemdCredsNonAdminPlaintext => {
739 ONLY_NETHSM_ADMIN_SYSTEMD_CREDS_NON_ADMIN_PLAINTEXT
740 }
741 ConfigFileVariant::OnlyNetHsmBackendAdminSystemdCredsNonAdminSystemdCreds => {
742 ONLY_NETHSM_ADMIN_SYSTEMD_CREDS_NON_ADMIN_SYSTEMD_CREDS
743 }
744 ConfigFileVariant::OnlyNetHsmBackendAdminSssNonAdminPlaintext => {
745 ONLY_NETHSM_ADMIN_SSS_NON_ADMIN_PLAINTEXT
746 }
747 ConfigFileVariant::OnlyNetHsmBackendAdminSssNonAdminSystemdCreds => {
748 ONLY_NETHSM_ADMIN_SSS_NON_ADMIN_SYSTEMD_CREDS
749 }
750 ConfigFileVariant::OnlyYubiHsm2BackendAdminPlaintextNonAdminPlaintext => {
751 ONLY_YUBIHSM2_ADMIN_PLAINTEXT_NON_ADMIN_PLAINTEXT
752 }
753 ConfigFileVariant::OnlyYubiHsm2BackendAdminPlaintextNonAdminSystemdCreds => {
754 ONLY_YUBIHSM2_ADMIN_PLAINTEXT_NON_ADMIN_SYSTEMD_CREDS
755 }
756 ConfigFileVariant::OnlyYubiHsm2BackendAdminSystemdCredsNonAdminPlaintext => {
757 ONLY_YUBIHSM2_ADMIN_SYSTEMD_CREDS_NON_ADMIN_PLAINTEXT
758 }
759 ConfigFileVariant::OnlyYubiHsm2BackendAdminSystemdCredsNonAdminSystemdCreds => {
760 ONLY_YUBIHSM2_ADMIN_SYSTEMD_CREDS_NON_ADMIN_SYSTEMD_CREDS
761 }
762 ConfigFileVariant::OnlyYubiHsm2BackendAdminSssNonAdminPlaintext => {
763 ONLY_YUBIHSM2_ADMIN_SSS_NON_ADMIN_PLAINTEXT
764 }
765 ConfigFileVariant::OnlyYubiHsm2BackendAdminSssNonAdminSystemdCreds => {
766 ONLY_YUBIHSM2_ADMIN_SSS_NON_ADMIN_SYSTEMD_CREDS
767 }
768 ConfigFileVariant::OnlyYubiHsm2HttpBackendAdminPlaintextNonAdminPlaintext => {
769 ONLY_YUBIHSM2_HTTP_ADMIN_PLAINTEXT_NON_ADMIN_PLAINTEXT
770 }
771 ConfigFileVariant::OnlyYubiHsm2MockHsmBackendAdminPlaintextNonAdminPlaintext => {
772 ONLY_YUBIHSM2_MOCKHSM_ADMIN_PLAINTEXT_NON_ADMIN_PLAINTEXT
773 }
774 ConfigFileVariant::OnlyYubiHsm2MockHsmBackendAdminPlaintextNonAdminSystemdCreds => {
775 ONLY_YUBIHSM2_MOCKHSM_ADMIN_PLAINTEXT_NON_ADMIN_SYSTEMD_CREDS
776 }
777 ConfigFileVariant::OnlyYubiHsm2MockHsmBackendAdminSystemdCredsNonAdminPlaintext => {
778 ONLY_YUBIHSM2_MOCKHSM_ADMIN_SYSTEMD_CREDS_NON_ADMIN_PLAINTEXT
779 }
780 ConfigFileVariant::OnlyYubiHsm2MockHsmBackendAdminSystemdCredsNonAdminSystemdCreds => {
781 ONLY_YUBIHSM2_MOCKHSM_ADMIN_SYSTEMD_CREDS_NON_ADMIN_SYSTEMD_CREDS
782 }
783 ConfigFileVariant::OnlyYubiHsm2MockHsmBackendAdminSssNonAdminPlaintext => {
784 ONLY_YUBIHSM2_MOCKHSM_ADMIN_SSS_NON_ADMIN_PLAINTEXT
785 }
786 ConfigFileVariant::OnlyYubiHsm2MockHsmBackendAdminSssNonAdminSystemdCreds => {
787 ONLY_YUBIHSM2_MOCKHSM_ADMIN_SSS_NON_ADMIN_SYSTEMD_CREDS
788 }
789 ConfigFileVariant::AllBackendsAdminPlaintextNonAdminPlaintext => {
790 ALL_BACKENDS_ADMIN_PLAINTEXT_NON_ADMIN_PLAINTEXT
791 }
792 ConfigFileVariant::AllBackendsAdminPlaintextNonAdminSystemdCreds => {
793 ALL_BACKENDS_ADMIN_PLAINTEXT_NON_ADMIN_SYSTEMD_CREDS
794 }
795 ConfigFileVariant::AllBackendsAdminSystemdCredsNonAdminPlaintext => {
796 ALL_BACKENDS_ADMIN_SYSTEMD_CREDS_NON_ADMIN_PLAINTEXT
797 }
798 ConfigFileVariant::AllBackendsAdminSystemdCredsNonAdminSystemdCreds => {
799 ALL_BACKENDS_ADMIN_SYSTEMD_CREDS_NON_ADMIN_SYSTEMD_CREDS
800 }
801 ConfigFileVariant::AllBackendsAdminSssNonAdminPlaintext => {
802 ALL_BACKENDS_ADMIN_SSS_NON_ADMIN_PLAINTEXT
803 }
804 ConfigFileVariant::AllBackendsAdminSssNonAdminSystemdCreds => {
805 ALL_BACKENDS_ADMIN_SSS_NON_ADMIN_SYSTEMD_CREDS
806 }
807 }
808 }
809
810 pub fn to_config(&self) -> Result<Config, crate::Error> {
819 Config::from_str(
820 &String::from_utf8(self.as_config_bytes().to_vec()).map_err(|source| {
821 crate::Error::Utf8String {
822 path: PathBuf::from("/dev/null"),
823 context: "creating a Signstar config object from config fixture bytes"
824 .to_string(),
825 source,
826 }
827 })?,
828 )
829 }
830
831 pub fn contains_backend(&self, backend_type: BackendType) -> bool {
833 match backend_type {
834 BackendType::NetHsm => matches!(
835 self,
836 Self::OnlyNetHsmBackendAdminPlaintextNonAdminPlaintext
837 | Self::OnlyNetHsmBackendAdminPlaintextNonAdminSystemdCreds
838 | Self::OnlyNetHsmBackendAdminPlaintextNonAdminSystemdCredsSingleConnection
839 | Self::OnlyNetHsmBackendAdminSystemdCredsNonAdminPlaintext
840 | Self::OnlyNetHsmBackendAdminSystemdCredsNonAdminSystemdCreds
841 | Self::OnlyNetHsmBackendAdminSssNonAdminPlaintext
842 | Self::OnlyNetHsmBackendAdminSssNonAdminSystemdCreds
843 | Self::AllBackendsAdminPlaintextNonAdminPlaintext
844 | Self::AllBackendsAdminPlaintextNonAdminSystemdCreds
845 | Self::AllBackendsAdminSystemdCredsNonAdminPlaintext
846 | Self::AllBackendsAdminSystemdCredsNonAdminSystemdCreds
847 | Self::AllBackendsAdminSssNonAdminPlaintext
848 | Self::AllBackendsAdminSssNonAdminSystemdCreds
849 ),
850 BackendType::YubiHsm2 => matches!(
851 self,
852 Self::OnlyYubiHsm2BackendAdminPlaintextNonAdminPlaintext
853 | Self::OnlyYubiHsm2BackendAdminPlaintextNonAdminSystemdCreds
854 | Self::OnlyYubiHsm2BackendAdminSystemdCredsNonAdminPlaintext
855 | Self::OnlyYubiHsm2BackendAdminSystemdCredsNonAdminSystemdCreds
856 | Self::OnlyYubiHsm2BackendAdminSssNonAdminPlaintext
857 | Self::OnlyYubiHsm2BackendAdminSssNonAdminSystemdCreds
858 | Self::OnlyYubiHsm2HttpBackendAdminPlaintextNonAdminPlaintext
859 | Self::OnlyYubiHsm2MockHsmBackendAdminPlaintextNonAdminPlaintext
860 | Self::OnlyYubiHsm2MockHsmBackendAdminPlaintextNonAdminSystemdCreds
861 | Self::OnlyYubiHsm2MockHsmBackendAdminSystemdCredsNonAdminPlaintext
862 | Self::OnlyYubiHsm2MockHsmBackendAdminSystemdCredsNonAdminSystemdCreds
863 | Self::OnlyYubiHsm2MockHsmBackendAdminSssNonAdminPlaintext
864 | Self::OnlyYubiHsm2MockHsmBackendAdminSssNonAdminSystemdCreds
865 | Self::AllBackendsAdminPlaintextNonAdminPlaintext
866 | Self::AllBackendsAdminPlaintextNonAdminSystemdCreds
867 | Self::AllBackendsAdminSystemdCredsNonAdminPlaintext
868 | Self::AllBackendsAdminSystemdCredsNonAdminSystemdCreds
869 | Self::AllBackendsAdminSssNonAdminPlaintext
870 | Self::AllBackendsAdminSssNonAdminSystemdCreds
871 ),
872 }
873 }
874}
875
876#[derive(Clone, Copy, Debug, Default)]
878pub struct SystemUserConfigSettings {
879 #[cfg(any(feature = "nethsm", feature = "yubihsm2"))]
881 pub create_secrets: bool,
882
883 pub create_ssh_authorized_keys: bool,
885}
886
887#[derive(Clone, Debug, Default)]
889pub struct SystemUserConfig {
890 #[cfg(any(feature = "nethsm", feature = "yubihsm2"))]
892 backend_credentials: Option<HashMap<String, Passphrase>>,
893
894 #[cfg(any(feature = "nethsm", feature = "yubihsm2"))]
896 create_secrets: bool,
897
898 create_ssh_authorized_keys: bool,
900}
901
902#[derive(Clone, Debug, Default)]
904pub struct ConfigFileConfig {
905 pub location: Option<ConfigFileLocation>,
909
910 pub variant: ConfigFileVariant,
912
913 pub system_user_config: Option<SystemUserConfig>,
919}
920
921fn create_config(
934 location: ConfigFileLocation,
935 variant: ConfigFileVariant,
936) -> Result<(), crate::Error> {
937 create_dir_all(location.to_parent_dir_path()).map_err(|source| crate::Error::IoPath {
938 path: location.to_parent_dir_path(),
939 context: "creating the parent directory for the Signstar config",
940 source,
941 })?;
942 let path = PathBuf::from(location);
943
944 let mut file = File::create(&path).map_err(|source| crate::Error::IoPath {
945 path: path.clone(),
946 context: "creating a Signstar configuration file",
947 source,
948 })?;
949 let config_bytes = variant.as_config_bytes();
950 file.write_all(config_bytes)
951 .map_err(|source| crate::Error::IoPath {
952 path,
953 context: "writing data to a Signstar configuration file",
954 source,
955 })?;
956
957 Ok(())
958}
959
960fn create_unix_users_and_homes(config: &Config) -> Result<(), crate::Error> {
966 let users = config
967 .system_user_ids()
968 .iter()
969 .cloned()
970 .map(|id| id.as_ref())
971 .collect::<Vec<_>>();
972 Ok(create_users(&users, Some(&get_home_base_dir_path()), None)?)
973}
974
975#[derive(Clone, Debug)]
977pub struct SystemPrepareConfig {
978 pub machine_id: bool,
980
981 pub credentials_socket: bool,
983
984 pub signstar_config: ConfigFileConfig,
986}
987
988impl Default for SystemPrepareConfig {
989 fn default() -> Self {
990 Self {
991 machine_id: true,
992 credentials_socket: true,
993 signstar_config: ConfigFileConfig::default(),
994 }
995 }
996}
997
998pub fn list_files_in_dir(path: impl AsRef<Path>) -> Result<(), crate::Error> {
1000 let path = path.as_ref();
1001 let entries = read_dir(path).map_err(|source| crate::Error::IoPath {
1002 path: path.to_path_buf(),
1003 context: "reading its children",
1004 source,
1005 })?;
1006
1007 for entry in entries {
1008 let entry = entry.map_err(|source| crate::Error::IoPath {
1009 path: path.to_path_buf(),
1010 context: "getting an entry below it",
1011 source,
1012 })?;
1013 let meta = entry.metadata().map_err(|source| crate::Error::IoPath {
1014 path: path.to_path_buf(),
1015 context: "getting metadata",
1016 source,
1017 })?;
1018
1019 debug!(
1020 "{} {}/{} {entry:?}",
1021 meta.permissions().mode(),
1022 meta.st_uid(),
1023 meta.st_gid()
1024 );
1025
1026 if meta.is_dir() {
1027 list_files_in_dir(entry.path())?;
1028 }
1029 }
1030
1031 Ok(())
1032}
1033
1034pub fn get_tmp_config(data: &[u8]) -> Result<NamedTempFile, crate::Error> {
1036 let tmp_config = NamedTempFile::new().map_err(|source| crate::Error::Io {
1037 context: "creating a temporary configuration file".to_string(),
1038 source,
1039 })?;
1040 write(&tmp_config, data).map_err(|source| crate::Error::IoPath {
1041 path: tmp_config.path().to_path_buf(),
1042 context: "writing full signstar configuration to temporary file",
1043 source,
1044 })?;
1045 Ok(tmp_config)
1046}
1047
1048pub fn write_machine_id() -> Result<(), crate::Error> {
1057 debug!("Write dummy /etc/machine-id, required for systemd-creds");
1058 let machine_id = PathBuf::from("/etc/machine-id");
1059 std::fs::write(&machine_id, "d3b07384d113edec49eaa6238ad5ff00").map_err(|source| {
1060 crate::Error::IoPath {
1061 path: machine_id.to_path_buf(),
1062 context: "writing machine-id",
1063 source,
1064 }
1065 })?;
1066
1067 let metadata = machine_id
1068 .metadata()
1069 .map_err(|source| crate::Error::IoPath {
1070 path: machine_id,
1071 context: "getting metadata of file",
1072 source,
1073 })?;
1074 debug!(
1075 "/etc/machine-id\nmode: {}\nuid: {}\ngid: {}",
1076 metadata.permissions().mode(),
1077 metadata.st_uid(),
1078 metadata.st_gid()
1079 );
1080 Ok(())
1081}
1082
1083#[derive(Debug)]
1088pub struct BackgroundProcess {
1089 child: Child,
1090 command: String,
1091}
1092
1093impl BackgroundProcess {
1094 pub fn kill(&mut self) -> Result<(), crate::Error> {
1100 self.child.kill().map_err(|source| crate::Error::Io {
1101 context: format!("killing process of command \"{}\"", self.command),
1102 source,
1103 })
1104 }
1105}
1106
1107impl Drop for BackgroundProcess {
1108 fn drop(&mut self) {
1110 if let Err(error) = self.child.kill() {
1111 log::debug!(
1112 "Unable to kill background process of command {}:\n{error}",
1113 self.command
1114 )
1115 }
1116 }
1117}
1118
1119pub fn start_credentials_socket() -> Result<BackgroundProcess, crate::Error> {
1132 let systemd_run_path = PathBuf::from("/run/systemd");
1133 let socket_path = PathBuf::from("/run/systemd/io.systemd.Credentials");
1134 create_dir_all(&systemd_run_path).map_err(|source| crate::Error::IoPath {
1135 path: systemd_run_path.clone(),
1136 context: "creating the directory",
1137 source,
1138 })?;
1139
1140 let mut command = Command::new(get_command("systemd-socket-activate")?);
1142 let command = command.args([
1143 "--listen",
1144 "/run/systemd/io.systemd.Credentials",
1145 "--accept",
1146 "--fdname=varlink",
1147 "systemd-creds",
1148 ]);
1149 let child = command.spawn().map_err(|source| crate::Error::IoPath {
1150 path: PathBuf::from("/run/systemd/io.systemd.Credentials"),
1151 context: "creating a socket using systemd-socket-activate",
1152 source,
1153 })?;
1154
1155 let timeout = 10000;
1157 let step = 100;
1158 let mut elapsed = 0;
1159 let mut permissions_set = false;
1160 while elapsed < timeout {
1161 if socket_path.exists() {
1162 debug!("Found {socket_path:?}");
1163 set_permissions(socket_path.as_path(), Permissions::from_mode(0o666)).map_err(
1164 |source| crate::Error::IoPath {
1165 path: socket_path.to_path_buf(),
1166 context: "applying permissions",
1167 source,
1168 },
1169 )?;
1170 permissions_set = true;
1171 break;
1172 } else {
1173 thread::sleep(time::Duration::from_millis(step));
1174 elapsed += step;
1175 }
1176 }
1177 if !permissions_set {
1178 return Err(Error::Timeout {
1179 timeout,
1180 context: format!("waiting for {socket_path:?}"),
1181 }
1182 .into());
1183 }
1184
1185 Ok(BackgroundProcess {
1186 child,
1187 command: format!("{command:?}"),
1188 })
1189}
1190
1191#[cfg(feature = "nethsm")]
1202pub fn nethsm_admin_credentials(
1203 config_data: &[u8],
1204) -> Result<NetHsmAdminCredentials, crate::Error> {
1205 let config_file = get_tmp_config(config_data)?;
1206 NetHsmAdminCredentials::load_from_file(
1207 config_file.path(),
1208 AdministrativeSecretHandling::Plaintext,
1209 )
1210}
1211
1212#[cfg(feature = "nethsm")]
1217pub fn create_full_credentials(users: &[UserId]) -> Vec<FullCredentials> {
1218 users
1219 .iter()
1220 .map(|user| FullCredentials::new(user.clone(), Passphrase::generate(Some(30))))
1221 .collect()
1222}