1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use crate::common::OpType;
use crate::{handle_client_error, DomainOpt};
use anyhow::{Context, Error};
use kanidm_proto::internal::ImageValue;
use std::fs::read;

impl DomainOpt {
    pub fn debug(&self) -> bool {
        match self {
            DomainOpt::SetDisplayname(copt) => copt.copt.debug,
            DomainOpt::SetLdapBasedn { copt, .. }
            | DomainOpt::SetImage { copt, .. }
            | DomainOpt::RemoveImage { copt }
            | DomainOpt::SetLdapAllowUnixPasswordBind { copt, .. }
            | DomainOpt::RevokeKey { copt, .. }
            | DomainOpt::Show(copt) => copt.debug,
        }
    }

    pub async fn exec(&self) {
        match self {
            DomainOpt::SetDisplayname(opt) => {
                eprintln!(
                    "Attempting to set the domain's display name to: {:?}",
                    opt.new_display_name
                );
                let client = opt.copt.to_client(OpType::Write).await;
                match client
                    .idm_domain_set_display_name(&opt.new_display_name)
                    .await
                {
                    Ok(_) => println!("Success"),
                    Err(e) => handle_client_error(e, opt.copt.output_mode),
                }
            }
            DomainOpt::SetLdapBasedn { copt, new_basedn } => {
                eprintln!(
                    "Attempting to set the domain's ldap basedn to: {:?}",
                    new_basedn
                );
                let client = copt.to_client(OpType::Write).await;
                match client.idm_domain_set_ldap_basedn(new_basedn).await {
                    Ok(_) => println!("Success"),
                    Err(e) => handle_client_error(e, copt.output_mode),
                }
            }
            DomainOpt::SetLdapAllowUnixPasswordBind { copt, enable } => {
                let client = copt.to_client(OpType::Write).await;
                match client.idm_set_ldap_allow_unix_password_bind(*enable).await {
                    Ok(_) => println!("Success"),
                    Err(e) => handle_client_error(e, copt.output_mode),
                }
            }
            DomainOpt::Show(copt) => {
                let client = copt.to_client(OpType::Read).await;
                match client.idm_domain_get().await {
                    Ok(e) => println!("{}", e),
                    Err(e) => handle_client_error(e, copt.output_mode),
                }
            }
            DomainOpt::RevokeKey { copt, key_id } => {
                let client = copt.to_client(OpType::Write).await;
                match client.idm_domain_revoke_key(key_id).await {
                    Ok(_) => println!("Success"),
                    Err(e) => handle_client_error(e, copt.output_mode),
                }
            }
            DomainOpt::SetImage {
                copt,
                path,
                image_type,
            } => {
                let client = copt.to_client(OpType::Write).await;
                let img_res: Result<ImageValue, Error> = (move || {
                    let file_name = path
                        .file_name()
                        .context("Please pass a file")?
                        .to_str()
                        .context("Path contains non utf-8")?
                        .to_string();

                    let image_type = match image_type {
                        Some(val) => val.clone(),
                        None => {
                        path
                            .extension().context("Path has no extension so we can't infer the imageType, or you could pass the optional imageType argument yourself.")?
                            .to_str().context("Path contains invalid utf-8")?
                            .try_into()
                            .map_err(Error::msg)?
                        }
                    };

                    let read_res = read(path);
                    match read_res {
                        Ok(data) => Ok(ImageValue::new(file_name, image_type, data)),
                        Err(err) => {
                            if copt.debug {
                                eprintln!(
                                    "{}",
                                    kanidm_lib_file_permissions::diagnose_path(path.as_ref())
                                );
                            }
                            Err(err).context(format!("Failed to read file at '{}'", path.display()))
                        }
                    }
                })();

                let img = match img_res {
                    Ok(img) => img,
                    Err(err) => {
                        eprintln!("{err}");
                        return;
                    }
                };

                match client.idm_domain_update_image(img).await {
                    Ok(_) => println!("Success"),
                    Err(e) => handle_client_error(e, copt.output_mode),
                }
            }
            DomainOpt::RemoveImage { copt } => {
                let client = copt.to_client(OpType::Write).await;

                match client.idm_domain_delete_image().await {
                    Ok(_) => println!("Success"),
                    Err(e) => handle_client_error(e, copt.output_mode),
                }
            }
        }
    }
}