rusthound_ce/objects/
inssuancepolicie.rs

1use serde_json::value::Value;
2use serde::{Deserialize, Serialize};
3use ldap3::SearchEntry;
4use log::{debug, trace};
5use std::collections::HashMap;
6use std::error::Error;
7
8use crate::enums::{decode_guid_le, parse_ntsecuritydescriptor};
9use crate::utils::date::string_to_epoch;
10use crate::objects::common::{LdapObject, AceTemplate, SPNTarget, Link, Member};
11
12/// IssuancePolicie structure
13#[derive(Debug, Clone, Deserialize, Serialize, Default)]
14pub struct IssuancePolicie {
15    #[serde(rename = "Properties")]
16    properties: IssuancePolicieProperties,
17    #[serde(rename = "GroupLink")]
18    group_link: GroupLink,
19    #[serde(rename = "Aces")]
20    aces: Vec<AceTemplate>,
21    #[serde(rename = "ObjectIdentifier")]
22    object_identifier: String,
23    #[serde(rename = "IsDeleted")]
24    is_deleted: bool,
25    #[serde(rename = "IsACLProtected")]
26    is_acl_protected: bool,
27    #[serde(rename = "ContainedBy")]
28    contained_by: Option<Member>,
29}
30
31impl IssuancePolicie {
32    // New IssuancePolicie
33    pub fn new() -> Self { 
34        Self {
35            ..Default::default() 
36        } 
37    }
38
39    /// Function to parse and replace value in json template for IssuancePolicie object.
40    pub fn parse(
41         &mut self,
42        result: SearchEntry,
43        domain: &str,
44        dn_sid: &mut HashMap<String, String>,
45        sid_type: &mut HashMap<String, String>,
46        domain_sid: &str
47    ) -> Result<(), Box<dyn Error>> {
48        let result_dn: String = result.dn.to_uppercase();
49        let result_attrs: HashMap<String, Vec<String>> = result.attrs;
50        let result_bin: HashMap<String, Vec<Vec<u8>>> = result.bin_attrs;
51
52        // Debug for current object
53        debug!("Parse IssuancePolicie: {result_dn}");
54
55        // Trace all result attributes
56        for (key, value) in &result_attrs {
57            trace!("  {key:?}:{value:?}");
58        }
59        // Trace all bin result attributes
60        for (key, value) in &result_bin {
61            trace!("  {key:?}:{value:?}");
62        }
63
64        // Change all values...
65        self.properties.domain = domain.to_uppercase();
66        self.properties.distinguishedname = result_dn;    
67        self.properties.domainsid = domain_sid.to_string();
68
69        // With a check
70        for (key, value) in &result_attrs {
71            match key.as_str() {
72                "description" => {
73                    self.properties.description = Some(value[0].to_owned());
74                }
75                "whenCreated" => {
76                    let epoch = string_to_epoch(&value[0])?;
77                    if epoch.is_positive() {
78                        self.properties.whencreated = epoch;
79                    }
80                }
81                "IsDeleted" => {
82                    self.is_deleted = true;
83                }
84                "displayName" => {
85                    self.properties.name = format!("{}@{}",&value[0],domain).to_uppercase();
86                    self.properties.displayname = value[0].to_owned();
87                }
88                "msPKI-Cert-Template-OID" => {
89                    self.properties.certtemplateoid = value[0].to_owned();
90                }
91                _ => {}
92            }
93        }
94
95        // For all, bins attributs
96        for (key, value) in &result_bin {
97            match key.as_str() {
98                "objectGUID" => {
99                    // objectGUID raw to string
100                    let guid = decode_guid_le(&value[0]);
101                    self.object_identifier = guid.to_owned();
102                }
103                "nTSecurityDescriptor" => {
104                    // nTSecurityDescriptor raw to string
105                    let relations_ace = parse_ntsecuritydescriptor(
106                        self,
107                         &value[0],
108                        "IssuancePolicie",
109                         &result_attrs,
110                         &result_bin,
111                         domain,
112                    );
113                    self.aces = relations_ace;
114                }
115                _ => {}
116            }
117        }
118
119        // Push DN and SID in HashMap
120        if self.object_identifier != "SID" {
121            dn_sid.insert(
122                self.properties.distinguishedname.to_owned(),
123                self.object_identifier.to_owned()
124            );
125            // Push DN and Type
126            sid_type.insert(
127                self.object_identifier.to_owned(),
128                "IssuancePolicie".to_string()
129            );
130        }
131
132        // Trace and return IssuancePolicie struct
133        // trace!("JSON OUTPUT: {:?}",serde_json::to_string(&self).unwrap());
134        Ok(())
135    }
136}
137
138impl LdapObject for IssuancePolicie {
139    // To JSON
140    fn to_json(&self) -> Value {
141        serde_json::to_value(self).unwrap()
142    }
143
144    // Get values
145    fn get_object_identifier(&self) -> &String {
146         &self.object_identifier
147    }
148    fn get_is_acl_protected(&self) -> &bool {
149         &self.is_acl_protected
150    }
151    fn get_aces(&self) -> &Vec<AceTemplate> {
152         &self.aces
153    }
154    fn get_spntargets(&self) -> &Vec<SPNTarget> {
155        panic!("Not used by current object.");
156    }
157    fn get_allowed_to_delegate(&self) -> &Vec<Member> {
158        panic!("Not used by current object.");
159    }
160    fn get_links(&self) -> &Vec<Link> {
161        panic!("Not used by current object.");
162    }
163    fn get_contained_by(&self) -> &Option<Member> {
164         &self.contained_by
165    }
166    fn get_child_objects(&self) -> &Vec<Member> {
167        panic!("Not used by current object.");
168    }
169    fn get_haslaps(&self) -> &bool {
170         &false
171    }
172    
173    // Get mutable values
174    fn get_aces_mut(&mut self) -> &mut Vec<AceTemplate> {
175         &mut self.aces
176    }
177    fn get_spntargets_mut(&mut self) -> &mut Vec<SPNTarget> {
178        panic!("Not used by current object.");
179    }
180    fn get_allowed_to_delegate_mut(&mut self) -> &mut Vec<Member> {
181        panic!("Not used by current object.");
182    }
183    
184    // Edit values
185    fn set_is_acl_protected(&mut self, is_acl_protected: bool) {
186        self.is_acl_protected = is_acl_protected;
187        self.properties.isaclprotected = is_acl_protected;
188    }
189    fn set_aces(&mut self, aces: Vec<AceTemplate>) {
190        self.aces = aces;
191    }
192    fn set_spntargets(&mut self, _spn_targets: Vec<SPNTarget>) {
193        // Not used by current object.
194    }
195    fn set_allowed_to_delegate(&mut self, _allowed_to_delegate: Vec<Member>) {
196        // Not used by current object.
197    }
198    fn set_links(&mut self, _links: Vec<Link>) {
199        // Not used by current object.
200    }
201    fn set_contained_by(&mut self, contained_by: Option<Member>) {
202        self.contained_by = contained_by;
203    }
204    fn set_child_objects(&mut self, _child_objects: Vec<Member>) {
205        // Not used by current object.
206    }
207}
208
209
210// IssuancePolicie properties structure
211#[derive(Debug, Clone, Deserialize, Serialize)]
212pub struct IssuancePolicieProperties {
213    domain: String,
214    name: String,
215    distinguishedname: String,
216    domainsid: String,
217    isaclprotected: bool,
218    description: Option<String>,
219    whencreated: i64,
220    displayname: String,
221    certtemplateoid: String,
222}
223
224impl Default for IssuancePolicieProperties {
225    fn default() -> IssuancePolicieProperties {
226        IssuancePolicieProperties {
227            domain: String::from(""),
228            name: String::from(""),
229            distinguishedname: String::from(""),
230            domainsid: String::from(""),
231            isaclprotected: false,
232            description: None,
233            whencreated: -1,
234            displayname: String::from(""),
235            certtemplateoid: String::from(""),
236        }
237    }
238}
239/// GroupLink structure
240#[derive(Debug, Clone, Deserialize, Serialize)]
241pub struct GroupLink {
242    #[serde(rename = "ObjectIdentifier")]
243    object_identifier: Option<String>,
244    #[serde(rename = "ObjectType")]
245    object_type: String,
246}
247
248impl GroupLink {
249    // New object.
250    pub fn new(object_identifier: Option<String>, object_type: String) -> Self { Self { object_identifier, object_type } }
251
252    // Immutable access.
253    pub fn object_identifier(&self) -> &Option<String> {
254        &self.object_identifier
255    }
256    pub fn object_type(&self) -> &String {
257        &self.object_type
258    }
259 
260    // Mutable access.
261    pub fn object_identifier_mut(&mut self) -> &mut Option<String> {
262        &mut self.object_identifier
263    }
264    pub fn object_type_mut(&mut self) -> &mut String {
265        &mut self.object_type
266    }
267}
268
269// Implement Default trait for GroupLink
270impl Default for GroupLink {
271    fn default() -> Self {
272        Self {
273            object_identifier: None,
274            object_type: "Base".to_string(),
275        }
276    }
277}