1use std::fmt::Debug;
2use std::str::FromStr;
3
4use url::Url;
5
6use crate::kdc::detect_kdc_url;
7use crate::kerberos::ServerProperties;
8use crate::negotiate::{NegotiatedProtocol, ProtocolConfig};
9use crate::{Kerberos, Result};
10
11#[derive(Clone, Debug)]
13pub struct KerberosConfig {
14 pub kdc_url: Option<Url>,
27 pub client_computer_name: String,
31}
32
33impl ProtocolConfig for KerberosConfig {
34 fn new_instance(&self) -> Result<NegotiatedProtocol> {
35 Ok(NegotiatedProtocol::Kerberos(Kerberos::new_client_from_config(
36 self.clone(),
37 )?))
38 }
39
40 fn box_clone(&self) -> Box<dyn ProtocolConfig> {
41 Box::new(self.clone())
42 }
43}
44
45pub fn parse_kdc_url(kdc_url: &str) -> Option<Url> {
46 if !kdc_url.contains("://") {
47 Url::from_str(&format!("tcp://{kdc_url}")).ok()
48 } else {
49 Url::from_str(kdc_url).ok()
50 }
51}
52
53impl KerberosConfig {
54 pub fn new(kdc_url: &str, client_computer_name: String) -> Self {
55 let kdc_url = parse_kdc_url(kdc_url);
56
57 Self {
58 kdc_url,
59 client_computer_name,
60 }
61 }
62
63 pub fn get_kdc_url(self, domain: &str) -> Option<Url> {
64 if let Some(kdc_url) = self.kdc_url {
65 Some(kdc_url)
66 } else {
67 detect_kdc_url(domain)
68 }
69 }
70}
71
72#[derive(Clone, Debug)]
74pub struct KerberosServerConfig {
75 pub kerberos_config: KerberosConfig,
77 pub server_properties: ServerProperties,
79}
80
81impl ProtocolConfig for KerberosServerConfig {
82 fn new_instance(&self) -> Result<NegotiatedProtocol> {
83 Ok(NegotiatedProtocol::Kerberos(Kerberos::new_server_from_config(
84 self.kerberos_config.clone(),
85 self.server_properties.clone(),
86 )?))
87 }
88
89 fn box_clone(&self) -> Box<dyn ProtocolConfig> {
90 Box::new(self.clone())
91 }
92}