/**
	Checks the Uri to be valid. 
	If there is no valid Host, Port, userinfo, Path, Query or Fragment then the 
	return value indicates an appropriate invalid Component. else returns zero, 
	which indicates given uri is valid.

	@return whether the Uri is Valid by returning zero or appropriate error value 
			for Invalid Uri.
 */
TInt TValidatorBase::Validate()
	{
	if (!IsValidHost())
		{
		return KUriUtilsErrInvalidHost;
		}
	
	if (!IsValidPort())
		{
		return KUriUtilsErrInvalidPort;
		}
		
	if (!IsValidUserInfo())
		{
		return KUriUtilsErrInvalidUserInfo;
		}
	
	if (!IsValidPath())
		{
		return KUriUtilsErrInvalidParam;
		}
	
	if (!IsValidQuery())
		{
		return KUriUtilsErrInvalidHeaders;
		}
	
	if (!IsValidFragment())
		{
		return KUriUtilsErrInvalidFragment;
		}
	
	return KErrNone;
	}
Exemple #2
0
	bool CheckPass(userrec* user)
	{
		if(IsValidHost(user->password))
		{
			user->Extend("cgiirc_realhost", new std::string(user->host));
			user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
			strlcpy(user->host, user->password, 64);
			strlcpy(user->dhost, user->password, 64);
			user->InvalidateCache();

			bool valid = false;
			user->RemoveCloneCounts();
#ifdef IPV6
			if (user->GetProtocolFamily() == AF_INET6)
				valid = (inet_pton(AF_INET6, user->password, &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
			else
				valid = (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr));
#else
			if (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr))
				valid = true;
#endif
			ServerInstance->AddLocalClone(user);
			ServerInstance->AddGlobalClone(user);
			user->CheckClass();

			if (valid)
			{
				/* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
				if(NotifyOpers)
					ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
			}
			else
			{
				/* We got as resolved hostname in the password. */
				try
				{

					bool cached;
					CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
					ServerInstance->AddResolver(r, cached);
				}
				catch (...)
				{
					if (NotifyOpers)
						ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
				}
			}
			
			*user->password = 0;

			/*if(NotifyOpers)
				ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/

			return true;
		}
		
		return false;
	}
Exemple #3
0
ECode CURI::ParseAuthority(
    /* [in] */ Boolean forceServer)
{
    if (mAuthority.IsNull()) {
        return NOERROR;
    }

    String tempUserInfo;
    String temp = mAuthority;
    Int32 index = temp.IndexOf('@');
    Int32 hostIndex = 0;
    if (index != -1) {
        // remove user info
        tempUserInfo = temp.Substring(0, index);
        FAIL_RETURN(ValidateUserInfo(mAuthority, tempUserInfo, 0));
        temp = temp.Substring(index + 1); // host[:port] is left
        hostIndex = index + 1;
    }

    index = temp.LastIndexOf(':');
    Int32 endIndex = temp.IndexOf(']');

    String tempHost;
    Int32 tempPort = -1;
    if (index != -1 && endIndex < index) {
        // determine port and host
        tempHost = temp.Substring(0, index);

        Char32 firstPortChar = temp.GetChar(index + 1);
        if (firstPortChar >= '0' && firstPortChar <= '9') {
            // allow only digits, no signs
            ECode ec = StringUtils::Parse(temp.Substring(index + 1), &tempPort);
            if (ec == (ECode)E_NUMBER_FORMAT_EXCEPTION) {
                if (forceServer) {
                    ALOGE("%s Invalid port number %d", mAuthority.string(), hostIndex + index + 1);
                    return E_URI_SYNTAX_EXCEPTION;
                }
                return NOERROR;
            }
        } else {
            if (forceServer) {
                ALOGE("%s Invalid port number %d", mAuthority.string(), hostIndex + index + 1);
                return E_URI_SYNTAX_EXCEPTION;
            }
            return NOERROR;
        }
    }
    else {
        tempHost = temp;
    }

    if (tempHost.IsEmpty()) {
        if (forceServer) {
            return E_URI_SYNTAX_EXCEPTION;
        }
        return NOERROR;
    }

    Boolean isValid = FALSE;
    FAIL_RETURN(IsValidHost(forceServer, tempHost, &isValid));
    if (!isValid) {
        return NOERROR;
    }

    // this is a server based uri,
    // fill in the userInfo, host and port fields
    mUserInfo = tempUserInfo;
    mHost = tempHost;
    mPort = tempPort;
    mServerAuthority = TRUE;

    return NOERROR;
}
Exemple #4
0
/****************************************************************************
 *  Function name: GetIpByHost
 *  Input argv:
 *  	-- host: host name
 *  Output argv:
 *  	--
 *  Return:
   	ip: sucess
   	NULL: fail
 *  Function Description: get the ip address by host name
 *  Be careful: release the memory by the client
****************************************************************************/
char * CUrl::GetIpByHost(const char *host)
{
	if( !host ){	// null pointer
		return NULL;
	}

	if( !IsValidHost(host) ){	// invalid host
		return NULL;
	}

	unsigned long inaddr = 0;
	char *result = NULL;
	int len = 0;


	inaddr = (unsigned long)inet_addr( host );
	//if ( (int)inaddr != -1){ 
	if ( inaddr != INADDR_NONE){ // host is just ip
		len = strlen(host);
		//pthread_mutex_lock(&mutexMemory);
		result = new char[len+1];
		//pthread_mutex_unlock(&mutexMemory);
		memset(result, 0, len+1);
		memcpy(result, host, len);

		return result;

        } else {
		//firt find from cache
		
		map<string,string>::iterator it  = mapCacheHostLookup.find(host);

		if( it != mapCacheHostLookup.end() ){	// find in host lookup cache
			const char * strHostIp;

			strHostIp = (*it).second.c_str();

			inaddr = (unsigned long)inet_addr( strHostIp );
			//if ( (int)inaddr != -1){ 
			if ( inaddr != INADDR_NONE ){ 
				len = strlen(strHostIp);
				//pthread_mutex_lock(&mutexMemory);
				result = new char[len+1];
				//pthread_mutex_unlock(&mutexMemory);
				memset( result, 0, len+1 );
				memcpy( result, strHostIp, len );

				//cout << ":)" ;
				
				return result;
        		}
		}
	}

	// if still not find, then try by DNS server
	struct hostent *hp;	/* Host entity */
	hp = gethostbyname(host);
	if(hp == NULL) { 
		//cout << "gethostbyname() error in GetIpByHost: " << host << endl;
		return NULL;
	}

	// cache host lookup
        struct  in_addr in;

	bcopy(*(hp->h_addr_list), (caddr_t)&in, hp->h_length);
		
	char    abuf[INET_ADDRSTRLEN];
        if( inet_ntop(AF_INET, (void *)&in,abuf, sizeof(abuf)) == NULL ){
		cout << "inet_ntop() return error in GetIpByHost" << endl;
		return NULL;

	} else {

		pthread_mutex_lock(&mutexCacheHost);
		//if( mapCacheHostLookup.count(host) == 0){
		if( mapCacheHostLookup.find(host) == mapCacheHostLookup.end() ){
		
			//cout << endl << host << " and " << abuf << endl;
			mapCacheHostLookup.insert( valTypeCHL ( host, abuf));
		}
		pthread_mutex_unlock(&mutexCacheHost);

	}

	// return result
	len = strlen(abuf);
	//pthread_mutex_lock(&mutexMemory);
	result = new char[len + 1];
	//pthread_mutex_unlock(&mutexMemory);
	memset( result, 0, len+1 );
	memcpy( result, abuf, len );

	return result;
}