Skip to main content

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