|
| 1 | +use mlua::prelude::{Lua, LuaResult, LuaValue}; |
| 2 | +use bytes::Bytes; |
| 3 | +use rasn::der; |
| 4 | +use rasn_ldap::{LdapMessage, ProtocolOp}; |
| 5 | + |
| 6 | +fn bytes_to_string(b: Bytes) -> Result<String, std::string::FromUtf8Error> { |
| 7 | + return String::from_utf8(b.to_vec()); |
| 8 | +} |
| 9 | + |
| 10 | +pub fn decode<'lua>( |
| 11 | + lua: &'lua Lua, |
| 12 | + v: LuaValue<'lua>, |
| 13 | +) -> LuaResult<(LuaValue<'lua>, LuaValue<'lua>)> { |
| 14 | + let der = match v { |
| 15 | + LuaValue::String(v) => v, |
| 16 | + _ => { |
| 17 | + return Ok(( |
| 18 | + LuaValue::Nil, |
| 19 | + LuaValue::String(lua.create_string("wrong format on input data")?), |
| 20 | + )) |
| 21 | + } |
| 22 | + }; |
| 23 | + |
| 24 | + let lm = match der::decode::<LdapMessage>(der.as_bytes()) { |
| 25 | + Ok(lm) => lm, |
| 26 | + Err(err) => { |
| 27 | + let err_str = format!("{}", err.to_string()); |
| 28 | + |
| 29 | + return Ok(( |
| 30 | + LuaValue::Nil, |
| 31 | + LuaValue::String(lua.create_string(err_str.as_bytes())?), |
| 32 | + )); |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + let result = lua.create_table()?; |
| 37 | + match lm.protocol_op { |
| 38 | + ProtocolOp::BindResponse(resp) => { |
| 39 | + result.set("protocol_op", 1)?; |
| 40 | + result.set("result_code", resp.result_code as i64)?; |
| 41 | + result.set("matched_dn", bytes_to_string(resp.matched_dn).unwrap())?; |
| 42 | + result.set( |
| 43 | + "diagnostic_msg", |
| 44 | + bytes_to_string(resp.diagnostic_message).unwrap(), |
| 45 | + )?; |
| 46 | + return Ok((LuaValue::Table(result), LuaValue::Nil)); |
| 47 | + } |
| 48 | + ProtocolOp::SearchResEntry(entry) => { |
| 49 | + result.set("protocol_op", 4)?; |
| 50 | + result.set("entry_dn", bytes_to_string(entry.object_name).unwrap())?; |
| 51 | + |
| 52 | + let attributes = lua.create_table()?; |
| 53 | + for attribute in entry.attributes.into_iter() { |
| 54 | + let attribute_vals = lua.create_table()?; |
| 55 | + for val in attribute.vals.into_iter() { |
| 56 | + attribute_vals.push(bytes_to_string(val).unwrap())? |
| 57 | + } |
| 58 | + attributes.set(bytes_to_string(attribute.r#type).unwrap(), attribute_vals)?; |
| 59 | + } |
| 60 | + |
| 61 | + result.set("attributes", attributes)?; |
| 62 | + return Ok((LuaValue::Table(result), LuaValue::Nil)); |
| 63 | + } |
| 64 | + ProtocolOp::SearchResDone(done) => { |
| 65 | + let resp = done.0; |
| 66 | + result.set("protocol_op", 5)?; |
| 67 | + result.set("result_code", resp.result_code as i64)?; |
| 68 | + result.set("matched_dn", bytes_to_string(resp.matched_dn).unwrap())?; |
| 69 | + result.set( |
| 70 | + "diagnostic_msg", |
| 71 | + bytes_to_string(resp.diagnostic_message).unwrap(), |
| 72 | + )?; |
| 73 | + return Ok((LuaValue::Table(result), LuaValue::Nil)); |
| 74 | + } |
| 75 | + ProtocolOp::ModifyResponse(resp0) => { |
| 76 | + let resp = resp0.0; |
| 77 | + result.set("protocol_op", 7)?; |
| 78 | + result.set("result_code", resp.result_code as i64)?; |
| 79 | + result.set("matched_dn", bytes_to_string(resp.matched_dn).unwrap())?; |
| 80 | + result.set( |
| 81 | + "diagnostic_msg", |
| 82 | + bytes_to_string(resp.diagnostic_message).unwrap(), |
| 83 | + )?; |
| 84 | + return Ok((LuaValue::Table(result), LuaValue::Nil)); |
| 85 | + } |
| 86 | + ProtocolOp::SearchResRef(_) => { |
| 87 | + return Ok(( |
| 88 | + LuaValue::Nil, |
| 89 | + LuaValue::String( |
| 90 | + lua.create_string("decoder not yet implement: search result reference")?, |
| 91 | + ), |
| 92 | + )) |
| 93 | + } |
| 94 | + _ => { |
| 95 | + return Ok(( |
| 96 | + LuaValue::Nil, |
| 97 | + LuaValue::String(lua.create_string("decoder not yet implement")?), |
| 98 | + )) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments