bool UniqueMUCRoom::handleIqID( Stanza *stanza, int context )
  {
    switch( stanza->subtype() )
    {
      case StanzaIqResult:
        if( context == RequestUniqueName )
        {
          Tag *u = stanza->findChild( "unique", XMLNS_MUC_UNIQUE );
          if( u )
          {
            const std::string& name = u->cdata();
            if( !name.empty() )
              setName( name );
          }
        }
        break;
      case StanzaIqError:
        if( context == RequestUniqueName )
        {
          SHA s;
          s.feed( m_parent->jid().full() );
          s.feed( m_parent->getID() );
          setName( s.hex() );
        }
        break;
      default:
        break;
    }

    MUCRoom::join();

    return false;
  }
  bool SOCKS5BytestreamManager::requestSOCKS5Bytestream( const JID& to, S5BMode /*mode*/,
                                                         const std::string& sid )
  {
    if( !m_parent )
      return false;

    if( m_hosts.empty() )
    {
      m_parent->logInstance().log( LogLevelWarning, LogAreaClassS5BManager,
                                   "No stream hosts set, cannot request bytestream." );
      return false;
    }

    const std::string& msid = sid.empty() ? m_parent->getID() : sid;
    const std::string& id = m_parent->getID();
    Tag *iq = new Tag( "iq" );
    iq->addAttribute( "type", "set" );
    iq->addAttribute( "to", to.full() );
    iq->addAttribute( "id", id );
    Tag *q = new Tag( iq, "query", "xmlns", XMLNS_BYTESTREAMS );
    q->addAttribute( "sid", msid );
    q->addAttribute( "mode", /*( mode == S5BTCP ) ?*/ "tcp" /*: "udp"*/ );

    StreamHostList::const_iterator it = m_hosts.begin();
    for( ; it != m_hosts.end(); ++it )
    {
      Tag* s = new Tag( q, "streamhost", "jid", (*it).jid.full() );
      s->addAttribute( "host", (*it).host );
      s->addAttribute( "port", (*it).port );
    }

    if( m_server )
    {
      SHA sha;
      sha.feed( msid );
      sha.feed( m_parent->jid().full() );
      sha.feed( to.full() );
      m_server->registerHash( sha.hex() );
    }

    AsyncS5BItem asi;
    asi.sHosts = m_hosts;
    asi.id = id;
    asi.from = to;
    asi.incoming = false;
    m_asyncTrackMap[msid] = asi;

    m_trackMap[id] = msid;
    m_parent->trackID( this, id, S5BOpenStream );
    m_parent->send( iq );

    return true;
  }
示例#3
0
  void SOCKS5Bytestream::setConnectionImpl( ConnectionBase* connection )
  {
    if( m_socks5 )
      delete m_socks5; // deletes m_connection as well

    m_connection = connection;

    SHA sha;
    sha.feed( m_sid );
    sha.feed( m_initiator.full() );
    sha.feed( m_target.full() );
    m_socks5 = new ConnectionSOCKS5Proxy( this, connection, m_logInstance, sha.hex(), 0 );
  }
bool SOCKS5BytestreamManager::requestSOCKS5Bytestream( const JID& to, S5BMode mode,
        const std::string& sid,
        const JID& from )
{
    if( !m_parent )
    {
        m_parent->logInstance().warn( LogAreaClassS5BManager,
                                      "No parent (ClientBase) set, cannot request bytestream." );
        return false;
    }

    if( m_hosts.empty() )
    {
        m_parent->logInstance().warn( LogAreaClassS5BManager,
                                      "No stream hosts set, cannot request bytestream." );
        return false;
    }

    const std::string& msid = sid.empty() ? m_parent->getID() : sid;
    const std::string& id = m_parent->getID();
    IQ iq( IQ::Set, to, id );
    iq.addExtension( new Query( msid, mode, m_hosts ) );
    if( from )
        iq.setFrom( from );

    if( m_server )
    {
        SHA sha;
        sha.feed( msid );
        if( from )
            sha.feed( from.full() );
        else
            sha.feed( m_parent->jid().full() );
        sha.feed( to.full() );
        m_server->registerHash( sha.hex() );
    }

    AsyncS5BItem asi;
    asi.sHosts = m_hosts;
    asi.id = id;
    asi.from = to;
    asi.to = from ? from : m_parent->jid();
    asi.incoming = false;
    m_asyncTrackMap[msid] = asi;

    m_trackMap[id] = msid;
    m_parent->send( iq, this, S5BOpenStream );

    return true;
}
示例#5
0
  const std::string Capabilities::ver() const
  {
    if( !m_disco )
      return m_ver;

    SHA sha;
    sha.feed( generate( m_disco->identities(), m_disco->features( true ), m_disco->form() ) );
    const std::string& hash = Base64::encode64( sha.binary() );
    m_disco->removeNodeHandlers( const_cast<Capabilities*>( this ) );
    m_disco->registerNodeHandler( const_cast<Capabilities*>( this ), m_node + '#' + hash );
    return hash;
  }
示例#6
0
void jAccount::s_saveVCard(VCard *vcard)
{
	QString hex = "";
	const VCard::Photo &photo = vcard->photo();
	if(!photo.binval.empty())
	{
		QByteArray data(photo.binval.c_str(),photo.binval.length());
		SHA sha;
		sha.feed(photo.binval);
		sha.finalize();
		hex = jProtocol::fromStd(sha.hex());
	}
	m_jabber_protocol->updateAvatarPresence(hex);
	m_jabber_protocol->storeVCard(vcard);
}
示例#7
0
  NonSaslAuth::Query* NonSaslAuth::Query::newInstance( const std::string& user,
                                                       const std::string& sid,
                                                       const std::string& pwd,
                                                       const std::string& resource ) const
  {
    Query* q = new Query( user );
    if( m_digest && !sid.empty() )
    {
      SHA sha;
      sha.feed( sid );
      sha.feed( pwd );
      q->m_pwd = sha.hex();
    }
    else
      q->m_pwd = pwd;

    q->m_resource = resource;
    q->m_digest = m_digest;
    return q;
  }
void NonSaslAuthTest::send( const IQ& iq, IqHandler* ih, int ctx )
{
  if( m_test == 1 )
  {
    const NonSaslAuth::Query* q = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
    if( q )
    {
      m_result++;
      m_test = 2;
      IQ re( IQ::Result, iq.from(), iq.id() );
      Tag* d = new Tag( "query" );
      d->setXmlns( XMLNS_AUTH );
      new Tag( d, "username" );
      new Tag( d, "password" );
      new Tag( d, "resource" );
      re.addExtension( new NonSaslAuth::Query( d ) );
      ih->handleIqID( re, ctx );
      delete d;
    }
  }
  else if( m_test == 2 )
  {
    const NonSaslAuth::Query* q = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
    if( q )
    {
      Tag* d = q->tag();
      if( d->xml() == "<query xmlns='" + XMLNS_AUTH + "'>"
                      "<username>user</username>"
                      "<password>pwd</password>"
                      "<resource>resource</resource>"
                      "</query>" )
      {
        m_result++;
        IQ re( IQ::Result, iq.from(), iq.id() );
        ih->handleIqID( re, ctx );
      }
      delete d;
    }
  }
  else if( m_test == 3 )
  {
    const NonSaslAuth::Query* q = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
    if( q )
    {
      m_result++;
      m_test = 4;
      IQ re( IQ::Result, iq.from(), iq.id() );
      Tag* d = new Tag( "query" );
      d->setXmlns( XMLNS_AUTH );
      new Tag( d, "username" );
      new Tag( d, "digest" );
      new Tag( d, "password" );
      new Tag( d, "resource" );
      re.addExtension( new NonSaslAuth::Query( d ) );
      ih->handleIqID( re, ctx );
      delete d;
    }
  }
  else if( m_test == 4 )
  {
    const NonSaslAuth::Query* q = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
    if( q )
    {
      Tag* d = q->tag();
      SHA sha;
      sha.feed( "sid2" );
      sha.feed( "pwd" );
      if( d->xml() == "<query xmlns='" + XMLNS_AUTH + "'>"
          "<username>user</username>"
          "<digest>" + sha.hex() + "</digest>"
          "<resource>resource</resource>"
          "</query>" )
      {
        m_result += 2;
        IQ re( IQ::Result, iq.from(), iq.id() );
        ih->handleIqID( re, ctx );
      }
      delete d;
    }
  }
}
示例#9
0
int main( int /*argc*/, char** /*argv*/ )
{
  int fail = 0;
  std::string name;
  SHA sha;



  // -------
  name = "empty string";
  sha.feed( "" );
  sha.finalize();
  if( sha.hex() != "da39a3ee5e6b4b0d3255bfef95601890afd80709" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  sha.reset();

  // -------
  name = "The quick brown fox jumps over the lazy dog";
  sha.feed( name );
  sha.finalize();
  if( sha.hex() != "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), sha.hex().c_str() );
  }
  sha.reset();

  // -------
  name = "The quick brown fox jumps over the lazy cog";
  sha.feed( name );
  sha.finalize();
  if( sha.hex() != "de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), sha.hex().c_str() );
  }
  sha.reset();

  // -------
  name = "two-step";
  sha.feed( "The quick brown fox " );
  sha.feed( "jumps over the lazy dog" );
  sha.finalize();
  if( sha.hex() != "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), sha.hex().c_str() );
  }
  sha.reset();

  // -------
  name = "54byte string";
  sha.feed( std::string( 54, 'x' ) );
  sha.finalize();
  if( sha.hex() != "31045e7bb077ff8d188a776b196b980388735dbb" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), sha.hex().c_str() );
  }
  sha.reset();

  // -------
  name = "55byte string";
  sha.feed( std::string( 55, 'x' ) );
  sha.finalize();
  if( sha.hex() != "cef734ba81a024479e09eb5a75b6ddae62e6abf1" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), sha.hex().c_str() );
  }
  sha.reset();

  // -------
  name = "56byte string";
  sha.feed( std::string( 56, 'x' ) );
  sha.finalize();
  if( sha.hex() != "901305367c259952f4e7af8323f480d59f81335b" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), sha.hex().c_str() );
  }
  sha.reset();

  // -------
  name = "57byte string";
  sha.feed( std::string( 57, 'x' ) );
  sha.finalize();
  if( sha.hex() != "025ecbd5d70f8fb3c5457cd96bab13fda305dc59" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), sha.hex().c_str() );
  }
  sha.reset();

  // -------
  name = "many-step";
  sha.feed( "The" );
  sha.feed( " quick bro" );
  sha.feed( "" );
  sha.feed( "wn fox " );
  sha.feed( "jumps over the lazy dog" );
  sha.finalize();
  if( sha.hex() != "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), sha.hex().c_str() );
  }
  sha.reset();



  if( fail == 0 )
  {
    printf( "SHA: OK\n" );
    return 0;
  }
  else
  {
    fprintf( stderr, "SHA: %d test(s) failed\n", fail );
    return 1;
  }


}
void SOCKS5BytestreamManager::handleIqID( const IQ& iq, int context )
{
    StringMap::iterator it = m_trackMap.find( iq.id() );
    if( it == m_trackMap.end() )
        return;

    switch( context )
    {
    case S5BOpenStream:
    {
        switch( iq.subtype() )
        {
        case IQ::Result:
        {
            const Query* q = iq.findExtension<Query>( ExtS5BQuery );
            if( q && m_socks5BytestreamHandler )
            {
                const std::string& proxy = q->jid().full();
                const StreamHost* sh = findProxy( iq.from(), proxy, (*it).second );
                if( sh )
                {
                    SOCKS5Bytestream* s5b = 0;
                    bool selfProxy = ( proxy == m_parent->jid().full() && m_server );
                    if( selfProxy )
                    {
                        SHA sha;
                        sha.feed( (*it).second );
                        sha.feed( iq.to().full() );
                        sha.feed( iq.from().full() );
                        s5b = new SOCKS5Bytestream( this, m_server->getConnection( sha.hex() ),
                                                    m_parent->logInstance(),
                                                    iq.to(), iq.from(),
                                                    (*it).second );
                    }
                    else
                    {
                        s5b = new SOCKS5Bytestream( this, m_parent->connectionImpl()->newInstance(),
                                                    m_parent->logInstance(),
                                                    iq.to(), iq.from(),
                                                    (*it).second );
                        s5b->setStreamHosts( StreamHostList( 1, *sh ) );
                    }
                    m_s5bMap[(*it).second] = s5b;
                    m_socks5BytestreamHandler->handleOutgoingBytestream( s5b );
                    if( selfProxy )
                        s5b->activate();
                }
            }
            break;
        }
        case IQ::Error:
            m_socks5BytestreamHandler->handleBytestreamError( iq, (*it).second );
            break;
        default:
            break;
        }
        break;
    }
    case S5BActivateStream:
    {
        switch( iq.subtype() )
        {
        case IQ::Result:
        {
            S5BMap::const_iterator it5 = m_s5bMap.find( (*it).second );
            if( it5 != m_s5bMap.end() )
                (*it5).second->activate();
            break;
        }
        case IQ::Error:
            m_socks5BytestreamHandler->handleBytestreamError( iq, (*it).second );
            break;
        default:
            break;
        }
        break;
    }
    default:
        break;
    }
    m_trackMap.erase( it );
}
int main_test1( int /*argc*/, char** /*argv*/ )
{
  int fail = 0;
  std::string name;
  Tag* t = 0;


  // -------
  {
    name = "field request";
    NonSaslAuth::Query q( "user" );
    t = q.tag();
    if( !t || t->xml() != "<query xmlns='" + XMLNS_AUTH + "'>"
                          "<username>user</username></query>" )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed\n", name.c_str() );
    }
    delete t;
    t = 0;
  }

  // -------
  {
    name = "reply w/ pwd";
    Tag* q = new Tag( "query" );
    q->setXmlns( XMLNS_AUTH );
    new Tag( q, "username" );
    new Tag( q, "password" );
    new Tag( q, "resource" );
    NonSaslAuth::Query n( q );
    NonSaslAuth::Query* nq = n.newInstance( "user", "sid", "pwd", "resource" );
    t = nq->tag();
    if( !t || t->xml() != "<query xmlns='" + XMLNS_AUTH + "'>"
                          "<username>user</username>"
                          "<password>pwd</password>"
                          "<resource>resource</resource>"
                          "</query>" )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed\n", name.c_str() );
    }
    delete nq;
    delete q;
    delete t;
    t = 0;
  }

  // -------
  {
    name = "reply w/ digest";
    Tag* q = new Tag( "query" );
    q->setXmlns( XMLNS_AUTH );
    new Tag( q, "username" );
    new Tag( q, "password" );
    new Tag( q, "digest" );
    new Tag( q, "resource" );
    NonSaslAuth::Query n( q );
    NonSaslAuth::Query* nq = n.newInstance( "user", "sid", "pwd", "resource" );
    SHA sha;
    sha.feed( "sid" );
    sha.feed( "pwd" );
    t = nq->tag();
    if( !t || t->xml() != "<query xmlns='" + XMLNS_AUTH + "'>"
         "<username>user</username>"
         "<digest>" + sha.hex() + "</digest>"
         "<resource>resource</resource>"
         "</query>" )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
    }
    delete nq;
    delete q;
    delete t;
    t = 0;
  }

  // -------
  {
    StanzaExtensionFactory sef;
    sef.registerExtension( new NonSaslAuth::Query() );
    name = "NonSaslAuth::Query/SEFactory test";
    Tag* f = new Tag( "iq" );
    new Tag( f, "query", "xmlns", XMLNS_AUTH );
    IQ iq( IQ::Set, JID(), "" );
    sef.addExtensions( iq, f );
    const NonSaslAuth::Query* se = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
    if( se == 0 )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed\n", name.c_str() );
    }
    delete f;
  }


  if( fail == 0 )
  {
    printf( "NonSaslAuth::Query: OK\n" );
    return 0;
  }
  else
  {
    fprintf( stderr, "NonSaslAuth::Query: %d test(s) failed\n", fail );
    return 1;
  }

}
  bool SOCKS5BytestreamManager::handleIqID( Stanza *stanza, int context )
  {
    StringMap::iterator it = m_trackMap.find( stanza->id() );
    if( it == m_trackMap.end() )
      return false;

    switch( context )
    {
      case S5BOpenStream:
      {
        switch( stanza->subtype() )
        {
          case StanzaIqResult:
          {
            Tag* q = stanza->findChild( "query", "xmlns", XMLNS_BYTESTREAMS );
            if( !q || !m_socks5BytestreamHandler )
              return false;

            Tag* s = q->findChild( "streamhost-used" );
            if( !s || !s->hasAttribute( "jid" ) )
              return false;

            const std::string & proxy = s->findAttribute( "jid" );
            const StreamHost* sh = findProxy( stanza->from(), proxy, (*it).second );
            if( sh )
            {
              SOCKS5Bytestream* s5b = 0;
              bool selfProxy = ( proxy == m_parent->jid().full() && m_server );
              if( selfProxy )
              {
                SHA sha;
                sha.feed( (*it).second );
                sha.feed( m_parent->jid().full() );
                sha.feed( stanza->from().full() );
                s5b = new SOCKS5Bytestream( this, m_server->getConnection( sha.hex() ),
                                            m_parent->logInstance(),
                                            m_parent->jid(), stanza->from(),
                                            (*it).second );
              }
              else
              {
                s5b = new SOCKS5Bytestream( this, m_parent->connectionImpl()->newInstance(),
                                            m_parent->logInstance(),
                                            m_parent->jid(), stanza->from(),
                                            (*it).second );
                StreamHostList shl;
                shl.push_back( *sh );
                s5b->setStreamHosts( shl );
              }
              m_s5bMap[(*it).second] = s5b;
              m_socks5BytestreamHandler->handleOutgoingSOCKS5Bytestream( s5b );
              if( selfProxy )
                s5b->activate();
            }
            break;
          }
          case StanzaIqError:
            m_socks5BytestreamHandler->handleSOCKS5BytestreamError( stanza, (*it).second );
            break;
          default:
            break;
        }
        break;
      }
      case S5BActivateStream:
      {
        switch( stanza->subtype() )
        {
          case StanzaIqResult:
          {
            S5BMap::const_iterator it5 = m_s5bMap.find( (*it).second );
            if( it5 != m_s5bMap.end() )
              (*it5).second->activate();
            break;
          }
          case StanzaIqError:
            m_socks5BytestreamHandler->handleSOCKS5BytestreamError( stanza, (*it).second );
            break;
          default:
            break;
        }
        break;
      }
      default:
        break;
    }
    m_trackMap.erase( it );

    return false;
  }
示例#13
0
  bool NonSaslAuth::handleIqID( Stanza *stanza, int context )
  {
    switch( stanza->subtype() )
    {
      case StanzaIqError:
      {
        m_parent->setAuthed( false );
        m_parent->disconnect( ConnAuthenticationFailed );

        Tag *t = stanza->findChild( "error" );
        if( t )
        {
          if( t->hasChild( "conflict" ) || t->hasAttribute( "code", "409" ) )
            m_parent->setAuthFailure( NonSaslConflict );
          else if( t->hasChild( "not-acceptable" ) || t->hasAttribute( "code", "406" ) )
            m_parent->setAuthFailure( NonSaslNotAcceptable );
          else if( t->hasChild( "not-authorized" ) || t->hasAttribute( "code", "401" ) )
            m_parent->setAuthFailure( NonSaslNotAuthorized );
        }
        break;
      }
      case StanzaIqResult:
        switch( context )
        {
          case TRACK_REQUEST_AUTH_FIELDS:
          {
            const std::string& id = m_parent->getID();

            Tag *iq = new Tag( "iq" );
            iq->addAttribute( "id", id );
            iq->addAttribute( "type", "set" );
            Tag *query = new Tag( iq, "query" );
            query->addAttribute( "xmlns", XMLNS_AUTH );
            new Tag( query, "username", m_parent->jid().username() );
            new Tag( query, "resource", m_parent->jid().resource() );

            Tag *q = stanza->findChild( "query" );
            if( ( q->hasChild( "digest" ) ) && !m_sid.empty() )
            {
              SHA sha;
              sha.feed( m_sid );
              sha.feed( m_parent->password() );
              sha.finalize();
              new Tag( query, "digest", sha.hex() );
            }
            else
            {
              new Tag( query, "password", m_parent->password() );
            }

            m_parent->trackID( this, id, TRACK_SEND_AUTH );
            m_parent->send( iq );
            break;
          }
          case TRACK_SEND_AUTH:
            m_parent->setAuthed( true );
            m_parent->connected();
            break;
        }
        break;

      default:
        break;
    }
    return false;
  }