2017-05-10 00:13:54 +02:00
|
|
|
use xml;
|
2017-02-18 22:16:20 +01:00
|
|
|
use jid::Jid;
|
2017-02-19 15:25:18 +01:00
|
|
|
use transport::{Transport, SslTransport};
|
2017-02-19 00:01:39 +01:00
|
|
|
use error::Error;
|
2017-02-19 15:25:18 +01:00
|
|
|
use ns;
|
2017-05-10 00:13:54 +02:00
|
|
|
use plugin::{Plugin, PluginInit, PluginProxyBinding};
|
2017-02-20 17:41:09 +01:00
|
|
|
use connection::{Connection, C2S};
|
2017-05-04 20:21:14 +01:00
|
|
|
use sasl::client::Mechanism as SaslMechanism;
|
|
|
|
|
use sasl::client::mechanisms::{Plain, Scram};
|
|
|
|
|
use sasl::common::{Credentials as SaslCredentials, Identity, Secret, ChannelBinding};
|
|
|
|
|
use sasl::common::scram::{Sha1, Sha256};
|
2017-03-07 18:19:09 +01:00
|
|
|
use components::sasl_error::SaslError;
|
|
|
|
|
use util::FromElement;
|
2017-05-10 00:13:54 +02:00
|
|
|
use event::{Dispatcher, Propagation, SendElement, ReceiveElement, Priority};
|
2017-02-20 16:28:51 +01:00
|
|
|
|
|
|
|
|
use base64;
|
|
|
|
|
|
|
|
|
|
use minidom::Element;
|
2017-02-19 15:25:18 +01:00
|
|
|
|
2017-02-19 18:08:35 +01:00
|
|
|
use xml::reader::XmlEvent as ReaderEvent;
|
2017-02-18 22:16:20 +01:00
|
|
|
|
2017-05-10 00:13:54 +02:00
|
|
|
use std::sync::{Mutex, Arc};
|
2017-02-20 16:28:51 +01:00
|
|
|
|
2017-05-10 00:13:54 +02:00
|
|
|
use std::collections::{HashSet, HashMap};
|
|
|
|
|
|
|
|
|
|
use std::any::TypeId;
|
2017-02-25 06:49:13 +01:00
|
|
|
|
2017-02-24 17:44:11 +01:00
|
|
|
/// Struct that should be moved somewhere else and cleaned up.
|
2017-02-24 23:42:08 +01:00
|
|
|
#[derive(Debug)]
|
2017-02-24 17:44:11 +01:00
|
|
|
pub struct StreamFeatures {
|
2017-02-25 06:49:13 +01:00
|
|
|
pub sasl_mechanisms: Option<HashSet<String>>,
|
2017-02-24 17:44:11 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-21 13:55:20 +01:00
|
|
|
/// A builder for `Client`s.
|
2017-02-19 00:01:39 +01:00
|
|
|
pub struct ClientBuilder {
|
|
|
|
|
jid: Jid,
|
2017-03-07 18:19:09 +01:00
|
|
|
credentials: SaslCredentials,
|
2017-02-19 00:01:39 +01:00
|
|
|
host: Option<String>,
|
|
|
|
|
port: u16,
|
2017-02-18 22:16:20 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-19 00:01:39 +01:00
|
|
|
impl ClientBuilder {
|
2017-02-21 14:22:05 +01:00
|
|
|
/// Creates a new builder for an XMPP client that will connect to `jid` with default parameters.
|
2017-02-19 00:01:39 +01:00
|
|
|
pub fn new(jid: Jid) -> ClientBuilder {
|
|
|
|
|
ClientBuilder {
|
|
|
|
|
jid: jid,
|
2017-03-07 18:19:09 +01:00
|
|
|
credentials: SaslCredentials::default(),
|
2017-02-19 00:01:39 +01:00
|
|
|
host: None,
|
|
|
|
|
port: 5222,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 14:22:05 +01:00
|
|
|
/// Sets the host to connect to.
|
2017-02-19 00:01:39 +01:00
|
|
|
pub fn host(mut self, host: String) -> ClientBuilder {
|
|
|
|
|
self.host = Some(host);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 14:22:05 +01:00
|
|
|
/// Sets the port to connect to.
|
2017-02-19 00:01:39 +01:00
|
|
|
pub fn port(mut self, port: u16) -> ClientBuilder {
|
|
|
|
|
self.port = port;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-25 06:49:13 +01:00
|
|
|
/// Sets the password to use.
|
|
|
|
|
pub fn password<P: Into<String>>(mut self, password: P) -> ClientBuilder {
|
2017-03-07 18:19:09 +01:00
|
|
|
self.credentials = SaslCredentials {
|
2017-05-04 20:21:14 +01:00
|
|
|
identity: Identity::Username(self.jid.node.clone().expect("JID has no node")),
|
|
|
|
|
secret: Secret::password_plain(password),
|
2017-03-07 18:19:09 +01:00
|
|
|
channel_binding: ChannelBinding::None,
|
|
|
|
|
};
|
2017-02-25 06:49:13 +01:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 13:55:20 +01:00
|
|
|
/// Connects to the server and returns a `Client` when succesful.
|
2017-02-19 00:01:39 +01:00
|
|
|
pub fn connect(self) -> Result<Client, Error> {
|
|
|
|
|
let host = &self.host.unwrap_or(self.jid.domain.clone());
|
2017-02-19 15:25:18 +01:00
|
|
|
let mut transport = SslTransport::connect(host, self.port)?;
|
2017-02-20 17:41:09 +01:00
|
|
|
C2S::init(&mut transport, &self.jid.domain, "before_sasl")?;
|
2017-05-10 00:13:54 +02:00
|
|
|
let dispatcher = Arc::new(Mutex::new(Dispatcher::new()));
|
2017-03-07 18:19:09 +01:00
|
|
|
let mut credentials = self.credentials;
|
2017-02-25 06:58:42 +01:00
|
|
|
credentials.channel_binding = transport.channel_bind();
|
2017-05-10 00:13:54 +02:00
|
|
|
let transport = Arc::new(Mutex::new(transport));
|
2017-02-25 06:49:13 +01:00
|
|
|
let mut client = Client {
|
2017-02-19 00:01:39 +01:00
|
|
|
jid: self.jid,
|
2017-05-10 00:13:54 +02:00
|
|
|
transport: transport.clone(),
|
|
|
|
|
plugins: HashMap::new(),
|
|
|
|
|
binding: PluginProxyBinding::new(dispatcher.clone()),
|
|
|
|
|
dispatcher: dispatcher,
|
2017-02-25 06:49:13 +01:00
|
|
|
};
|
2017-05-10 00:13:54 +02:00
|
|
|
client.dispatcher.lock().unwrap().register(Priority::Default, Box::new(move |evt: &SendElement| {
|
|
|
|
|
let mut t = transport.lock().unwrap();
|
|
|
|
|
t.write_element(&evt.0).unwrap();
|
|
|
|
|
Propagation::Continue
|
|
|
|
|
}));
|
2017-02-25 06:58:42 +01:00
|
|
|
client.connect(credentials)?;
|
2017-02-25 06:49:13 +01:00
|
|
|
client.bind()?;
|
|
|
|
|
Ok(client)
|
2017-02-18 22:16:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
2017-02-19 00:01:39 +01:00
|
|
|
|
2017-02-21 13:58:24 +01:00
|
|
|
/// An XMPP client.
|
2017-02-19 00:01:39 +01:00
|
|
|
pub struct Client {
|
|
|
|
|
jid: Jid,
|
2017-05-10 00:13:54 +02:00
|
|
|
transport: Arc<Mutex<SslTransport>>,
|
|
|
|
|
plugins: HashMap<TypeId, Arc<Box<Plugin>>>,
|
2017-02-20 16:28:51 +01:00
|
|
|
binding: PluginProxyBinding,
|
2017-05-10 00:13:54 +02:00
|
|
|
dispatcher: Arc<Mutex<Dispatcher>>,
|
2017-02-19 00:01:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Client {
|
2017-02-21 14:22:05 +01:00
|
|
|
/// Returns a reference to the `Jid` associated with this `Client`.
|
2017-02-19 15:06:14 +01:00
|
|
|
pub fn jid(&self) -> &Jid {
|
|
|
|
|
&self.jid
|
|
|
|
|
}
|
2017-02-20 16:28:51 +01:00
|
|
|
|
2017-02-21 14:22:05 +01:00
|
|
|
/// Registers a plugin.
|
2017-05-10 00:13:54 +02:00
|
|
|
pub fn register_plugin<P: Plugin + PluginInit + 'static>(&mut self, mut plugin: P) {
|
|
|
|
|
let binding = self.binding.clone();
|
|
|
|
|
plugin.bind(binding);
|
|
|
|
|
let p = Arc::new(Box::new(plugin) as Box<Plugin>);
|
|
|
|
|
{
|
|
|
|
|
let mut disp = self.dispatcher.lock().unwrap();
|
|
|
|
|
P::init(&mut disp, p.clone());
|
|
|
|
|
}
|
|
|
|
|
if self.plugins.insert(TypeId::of::<P>(), p).is_some() {
|
|
|
|
|
panic!("registering a plugin that's already registered");
|
|
|
|
|
}
|
2017-02-20 16:28:51 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-21 14:22:05 +01:00
|
|
|
/// Returns the plugin given by the type parameter, if it exists, else panics.
|
2017-02-20 16:28:51 +01:00
|
|
|
pub fn plugin<P: Plugin>(&self) -> &P {
|
2017-05-10 00:13:54 +02:00
|
|
|
self.plugins.get(&TypeId::of::<P>())
|
|
|
|
|
.expect("the requested plugin was not registered")
|
|
|
|
|
.as_any()
|
|
|
|
|
.downcast_ref::<P>()
|
|
|
|
|
.expect("plugin downcast failure (should not happen!!)")
|
2017-02-20 16:28:51 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-21 14:22:05 +01:00
|
|
|
/// Returns the next event and flush the send queue.
|
2017-05-10 00:13:54 +02:00
|
|
|
pub fn main(&mut self) -> Result<(), Error> {
|
|
|
|
|
self.dispatcher.lock().unwrap().flush_all();
|
2017-02-20 16:28:51 +01:00
|
|
|
loop {
|
2017-05-10 00:13:54 +02:00
|
|
|
let elem = self.read_element()?;
|
|
|
|
|
{
|
|
|
|
|
let mut disp = self.dispatcher.lock().unwrap();
|
|
|
|
|
disp.dispatch(ReceiveElement(elem));
|
|
|
|
|
disp.flush_all();
|
2017-02-20 16:28:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-10 00:13:54 +02:00
|
|
|
fn reset_stream(&self) {
|
|
|
|
|
self.transport.lock().unwrap().reset_stream()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read_element(&self) -> Result<Element, Error> {
|
|
|
|
|
self.transport.lock().unwrap().read_element()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn write_element(&self, elem: &Element) -> Result<(), Error> {
|
|
|
|
|
self.transport.lock().unwrap().write_element(elem)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read_event(&self) -> Result<xml::reader::XmlEvent, Error> {
|
|
|
|
|
self.transport.lock().unwrap().read_event()
|
2017-02-20 16:28:51 +01:00
|
|
|
}
|
|
|
|
|
|
2017-03-07 18:19:09 +01:00
|
|
|
fn connect(&mut self, mut credentials: SaslCredentials) -> Result<(), Error> {
|
2017-02-25 06:49:13 +01:00
|
|
|
let features = self.wait_for_features()?;
|
|
|
|
|
let ms = &features.sasl_mechanisms.ok_or(Error::SaslError(Some("no SASL mechanisms".to_owned())))?;
|
|
|
|
|
fn wrap_err(err: String) -> Error { Error::SaslError(Some(err)) }
|
|
|
|
|
// TODO: better way for selecting these, enabling anonymous auth
|
2017-03-07 18:38:21 +01:00
|
|
|
let mut mechanism: Box<SaslMechanism> = if ms.contains("SCRAM-SHA-256-PLUS") && credentials.channel_binding != ChannelBinding::None {
|
2017-03-07 18:19:09 +01:00
|
|
|
Box::new(Scram::<Sha256>::from_credentials(credentials).map_err(wrap_err)?)
|
|
|
|
|
}
|
2017-03-07 18:38:21 +01:00
|
|
|
else if ms.contains("SCRAM-SHA-1-PLUS") && credentials.channel_binding != ChannelBinding::None {
|
2017-03-07 18:19:09 +01:00
|
|
|
Box::new(Scram::<Sha1>::from_credentials(credentials).map_err(wrap_err)?)
|
|
|
|
|
}
|
|
|
|
|
else if ms.contains("SCRAM-SHA-256") {
|
2017-03-07 18:38:21 +01:00
|
|
|
if credentials.channel_binding != ChannelBinding::None {
|
|
|
|
|
credentials.channel_binding = ChannelBinding::Unsupported;
|
|
|
|
|
}
|
2017-02-25 06:49:13 +01:00
|
|
|
Box::new(Scram::<Sha256>::from_credentials(credentials).map_err(wrap_err)?)
|
|
|
|
|
}
|
|
|
|
|
else if ms.contains("SCRAM-SHA-1") {
|
2017-03-07 18:38:21 +01:00
|
|
|
if credentials.channel_binding != ChannelBinding::None {
|
|
|
|
|
credentials.channel_binding = ChannelBinding::Unsupported;
|
|
|
|
|
}
|
2017-02-25 06:49:13 +01:00
|
|
|
Box::new(Scram::<Sha1>::from_credentials(credentials).map_err(wrap_err)?)
|
|
|
|
|
}
|
|
|
|
|
else if ms.contains("PLAIN") {
|
|
|
|
|
Box::new(Plain::from_credentials(credentials).map_err(wrap_err)?)
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return Err(Error::SaslError(Some("can't find a SASL mechanism to use".to_owned())));
|
|
|
|
|
};
|
2017-02-24 23:42:08 +01:00
|
|
|
let auth = mechanism.initial().map_err(|x| Error::SaslError(Some(x)))?;
|
2017-02-24 17:36:17 +01:00
|
|
|
let mut elem = Element::builder("auth")
|
|
|
|
|
.ns(ns::SASL)
|
2017-02-25 03:43:11 +01:00
|
|
|
.attr("mechanism", mechanism.name())
|
2017-02-24 17:36:17 +01:00
|
|
|
.build();
|
|
|
|
|
if !auth.is_empty() {
|
|
|
|
|
elem.append_text_node(base64::encode(&auth));
|
2017-02-20 16:28:51 +01:00
|
|
|
}
|
2017-05-10 00:13:54 +02:00
|
|
|
self.write_element(&elem)?;
|
2017-02-20 16:28:51 +01:00
|
|
|
loop {
|
2017-05-10 00:13:54 +02:00
|
|
|
let n = self.read_element()?;
|
2017-02-24 17:36:17 +01:00
|
|
|
if n.is("challenge", ns::SASL) {
|
2017-02-24 17:16:51 +01:00
|
|
|
let text = n.text();
|
|
|
|
|
let challenge = if text == "" {
|
|
|
|
|
Vec::new()
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
base64::decode(&text)?
|
|
|
|
|
};
|
2017-02-24 23:42:08 +01:00
|
|
|
let response = mechanism.response(&challenge).map_err(|x| Error::SaslError(Some(x)))?;
|
2017-02-24 17:16:51 +01:00
|
|
|
let mut elem = Element::builder("response")
|
2017-02-21 00:07:10 +01:00
|
|
|
.ns(ns::SASL)
|
|
|
|
|
.build();
|
2017-02-24 17:16:51 +01:00
|
|
|
if !response.is_empty() {
|
|
|
|
|
elem.append_text_node(base64::encode(&response));
|
2017-02-20 16:28:51 +01:00
|
|
|
}
|
2017-05-10 00:13:54 +02:00
|
|
|
self.write_element(&elem)?;
|
2017-02-20 16:28:51 +01:00
|
|
|
}
|
|
|
|
|
else if n.is("success", ns::SASL) {
|
2017-02-24 23:42:08 +01:00
|
|
|
let text = n.text();
|
|
|
|
|
let data = if text == "" {
|
|
|
|
|
Vec::new()
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
base64::decode(&text)?
|
|
|
|
|
};
|
|
|
|
|
mechanism.success(&data).map_err(|x| Error::SaslError(Some(x)))?;
|
2017-05-10 00:13:54 +02:00
|
|
|
self.reset_stream();
|
|
|
|
|
{
|
|
|
|
|
let mut g = self.transport.lock().unwrap();
|
|
|
|
|
C2S::init(&mut *g, &self.jid.domain, "after_sasl")?;
|
|
|
|
|
}
|
2017-02-24 23:53:54 +01:00
|
|
|
self.wait_for_features()?;
|
|
|
|
|
return Ok(());
|
2017-02-20 16:28:51 +01:00
|
|
|
}
|
2017-02-24 17:20:00 +01:00
|
|
|
else if n.is("failure", ns::SASL) {
|
2017-03-07 18:19:09 +01:00
|
|
|
let inner = SaslError::from_element(&n).map_err(|_| Error::SaslError(None))?;
|
|
|
|
|
return Err(Error::XmppSaslError(inner));
|
2017-02-24 17:20:00 +01:00
|
|
|
}
|
2017-02-24 17:36:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-25 06:49:13 +01:00
|
|
|
fn bind(&mut self) -> Result<(), Error> {
|
2017-02-24 17:36:17 +01:00
|
|
|
let mut elem = Element::builder("iq")
|
|
|
|
|
.attr("id", "bind")
|
|
|
|
|
.attr("type", "set")
|
|
|
|
|
.build();
|
2017-02-24 23:53:54 +01:00
|
|
|
let mut bind = Element::builder("bind")
|
|
|
|
|
.ns(ns::BIND)
|
|
|
|
|
.build();
|
|
|
|
|
if let Some(ref resource) = self.jid.resource {
|
|
|
|
|
let res = Element::builder("resource")
|
|
|
|
|
.ns(ns::BIND)
|
2017-04-30 17:44:07 +01:00
|
|
|
.append(resource.to_owned())
|
2017-02-24 23:53:54 +01:00
|
|
|
.build();
|
|
|
|
|
bind.append_child(res);
|
|
|
|
|
}
|
2017-02-24 17:36:17 +01:00
|
|
|
elem.append_child(bind);
|
2017-05-10 00:13:54 +02:00
|
|
|
self.write_element(&elem)?;
|
2017-02-24 17:36:17 +01:00
|
|
|
loop {
|
2017-05-10 00:13:54 +02:00
|
|
|
let n = self.read_element()?;
|
2017-02-24 17:36:17 +01:00
|
|
|
if n.is("iq", ns::CLIENT) && n.has_child("bind", ns::BIND) {
|
2017-02-20 16:28:51 +01:00
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-24 17:36:17 +01:00
|
|
|
|
2017-02-24 17:44:11 +01:00
|
|
|
fn wait_for_features(&mut self) -> Result<StreamFeatures, Error> {
|
2017-02-24 17:36:17 +01:00
|
|
|
// TODO: this is very ugly
|
|
|
|
|
loop {
|
2017-05-10 00:13:54 +02:00
|
|
|
let e = self.read_event()?;
|
2017-02-24 17:36:17 +01:00
|
|
|
match e {
|
|
|
|
|
ReaderEvent::StartElement { .. } => {
|
|
|
|
|
break;
|
|
|
|
|
},
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
loop {
|
2017-05-10 00:13:54 +02:00
|
|
|
let n = self.read_element()?;
|
2017-02-24 17:36:17 +01:00
|
|
|
if n.is("features", ns::STREAM) {
|
2017-02-24 17:44:11 +01:00
|
|
|
let mut features = StreamFeatures {
|
|
|
|
|
sasl_mechanisms: None,
|
|
|
|
|
};
|
|
|
|
|
if let Some(ms) = n.get_child("mechanisms", ns::SASL) {
|
2017-02-25 06:49:13 +01:00
|
|
|
let mut res = HashSet::new();
|
2017-02-24 17:44:11 +01:00
|
|
|
for cld in ms.children() {
|
2017-02-25 06:49:13 +01:00
|
|
|
res.insert(cld.text());
|
2017-02-24 17:44:11 +01:00
|
|
|
}
|
|
|
|
|
features.sasl_mechanisms = Some(res);
|
|
|
|
|
}
|
|
|
|
|
return Ok(features);
|
2017-02-24 17:36:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-19 00:01:39 +01:00
|
|
|
}
|