Пример #1
0
int renameLogFile( const char *pLogPath, const char * pSrcSuffix,
                            const char * pDestSuffix )
{
    struct stat st;
    char achDest[1024];
    char achSrc[1024];
    safe_snprintf( achDest, sizeof( achDest ), "%s%s", pLogPath, pDestSuffix );
    if ( nio_stat( achDest, &st ) == 0 )
    {
        LOG_ERR(( "File already exists: [%s], Awstats updating"
                    " might be in progress.", achDest ));
        return -1;
    }
    safe_snprintf( achSrc, sizeof( achSrc ), "%s%s", pLogPath, pSrcSuffix );
    if ( nio_stat( achSrc, &st ) == -1 )
    {
        LOG_ERR(( "Log file does not exist: [%s], cannot update"
                    " Awstats statistics", achSrc ));
        return -1;
    }
    if ( st.st_size == 0 )
        return -1;
    if ( rename( achSrc, achDest ) == -1 )
    {
        LOG_ERR(( "Cannot rename file from [%s] to [%s]: %s",
                    achSrc, achDest, strerror( errno ) ));
        return -1;
    }
    return 0;
    
}
int StaticFileCacheData::readyGziped()
{
    time_t tm = time( NULL );
    if ( tm == getLastMod() )
        return -1;
    if ( tm != m_tmLastCheckGzip )
    {
        struct stat st;
        m_tmLastCheckGzip = tm;
        if ( !m_gzippedPath.c_str() || !*m_gzippedPath.c_str() )
        {
            if ( buildGzipPath() == -1 )
                return -1;
        }

        int ret = nio_stat( m_gzippedPath.c_str(), &st );
        if (( ret == -1 )||( st.st_mtime != getLastMod() )) 
        {
            if (( !m_pGziped )||( m_pGziped->getRef() == 0 ))
            {
                if ( ret != -1 )
                    unlink( m_gzippedPath.c_str() );
                ret = tryCreateGziped();
                if ( ret == -1 )
                {
                    if ( m_pGziped )
                    {
                        delete m_pGziped;
                        m_pGziped = NULL;
                    }
                    return -1;
                }
                else
                {
                    ret = nio_stat( m_gzippedPath.c_str(), &st );
                    if ( ret )
                        return -1;
                }
            }
            else
                return -1;
        }
        if (( !m_pGziped )||( m_pGziped->isDirty( st ) ))
            buildGzipCache( st );        
    }
    if ( m_pGziped )
    {
        if (( m_pGziped->isCached() ||
            ( m_pGziped->getfd() != -1 )))
            return 0;
        return m_pGziped->readyData( m_gzippedPath.c_str() );
    }
    return -1;
}
int StaticFileCacheData::buildGzipPath()
{
    unsigned char achHash[MD5_DIGEST_LENGTH];
    char achPath[4096];
    MD5_CTX md5_ctx;
    MD5_Init( &md5_ctx );
    MD5_Update( &md5_ctx, m_real.c_str(), m_real.len() );
    MD5_Final( achHash, &md5_ctx );
    struct stat st;
    int n = snprintf( achPath, 4096, "%s/%x/%x/", s_gzipCachePath, achHash[0]>>4, achHash[0]&0xf );
    if (( nio_stat( achPath, &st ) == -1 )&&( errno == ENOENT ))
    {
        achPath[n-3] = 0;
        mkdir( achPath, 0700 );
        achPath[n-3] = '/';
        if (( mkdir( achPath, 0700 ) == -1 )&&( errno != EEXIST ))
        {
            return -1;
        }
    }
    
    StringTool::hexEncode( (const char *)&achHash[1], MD5_DIGEST_LENGTH-1, &achPath[n] );
    n+= 30;
    char * pReal = m_gzippedPath.resizeBuf( n + 6 );
    if ( !pReal )
        return -1;
    strncpy( pReal, achPath, n );
    m_gzippedPath.setLen( n );
    memmove( pReal + n , ".lsz\0\0", 6 );
    return 0;
}
Пример #4
0
int LshttpdMain::testServerRoot( const char * pRoot )
{
    struct stat st;
    if ( nio_stat( pRoot, &st ) == -1 )
        return -1;
    if ( !S_ISDIR( st.st_mode ) )
        return -1;
    char achBuf[MAX_PATH_LEN] = {0};
    
    int confType = -1;
    if ( GPath::getAbsoluteFile(achBuf, MAX_PATH_LEN, pRoot,
                            DEFAULT_XML_CONFIG_FILE) == 0 )
    {
        if ( access( achBuf, R_OK ) == 0 )
        {
            m_pBuilder->setConfigFilePath( achBuf );
            confType = 0; //XML
        }
    }
    
    if ( confType == -1 )
    {
        if ( GPath::getAbsoluteFile(achBuf, MAX_PATH_LEN, pRoot,
                            DEFAULT_PLAIN_CONFIG_FILE) == 0 )
        {
            if ( access( achBuf, R_OK ) == 0 )
            {
                m_pBuilder->setPlainConfigFilePath( achBuf );
                confType = 1;
            }
        }
    }   
    
    if (confType == -1)
        return -1;
    
    int len = strlen( pRoot );
    if ( pRoot[len-1] == '/' )
        achBuf[len] = 0;
    else
        achBuf[++len] = 0;
    if ( GPath::checkSymLinks( achBuf, &achBuf[len],
                    &achBuf[MAX_PATH_LEN], achBuf, 1 ) == -1 )
        return -1;
    m_pServer->setServerRoot( achBuf );
    
    //load the config
    if (confType == 0)
        m_pBuilder->loadConfigFile();
    else
        m_pBuilder->loadPlainConfigFile();

    return 0;
}
Пример #5
0
int PidFile::testAndRemovePidFile( const char * pPidFile )
{
    struct stat st;
    if (( nio_stat( pPidFile, &st ) == 0 )&&
        ( st.st_mtime == m_stPid.st_mtime )&&
        ( st.st_size == m_stPid.st_size )&&
        ( st.st_ino == m_stPid.st_ino ))
    {
        unlink( pPidFile );
    }
    return 0;
}
Пример #6
0
int PidFile::testAndRelockPidFile( const char * pPidFile, int pid )
{
    struct stat st;
    if (( nio_stat( pPidFile, &st ) == -1 )||
        ( st.st_mtime != m_stPid.st_mtime )||
        ( st.st_size != m_stPid.st_size )||
        ( st.st_ino != m_stPid.st_ino ))
    {
        closePidFile();
        int ret = lockPidFile( pPidFile );
        if ( !ret )
            ret = writePid( pid );
        return ret;
    }
    return 0;
}
Пример #7
0
int Awstats::shouldBuildStatic( const char * pName )
{
    struct stat st;
    char achBuf[1024];
    if ( m_iMode == AWS_STATIC )
    {
        safe_snprintf( achBuf, 1024, "%s/html/awstats.%s.html",
                        m_sWorkingDir.c_str(), pName );
    }
    else
    {
        safe_snprintf( achBuf, 1024, "%s/conf/awstats.%s.conf",
                        m_sWorkingDir.c_str(), pName );
    }
    if ( nio_stat( achBuf, &st ) == -1 )
        return 1;
    return 0;
}
static int createLockFile( const char * pReal, char * p )
{
    *p = 'l';       // filename "*.lszl"
    struct stat st;
    int ret = nio_stat( pReal, &st );
    if ( ret != -1 )  //compression in progress
    {
        //LOG_INFO((
        if ( DateTime::s_curTime - st.st_mtime > 60 )
            unlink( pReal );
        else
        {
            *p = 0;
            return -1;
        }
    }
    ret = ::open( pReal, O_RDWR | O_CREAT | O_EXCL, 0600 );
    *p = 0;
    return ret;
}
Пример #9
0
int FcgiStarter::start( FcgiApp& app )
{
    int fd = app.getfd();
    FcgiAppConfig& config = app.getConfig();
    struct stat st;
//    if (( stat( config.getCommand(), &st ) == -1 )||
//        ( access(config.getCommand(), X_OK) == -1 ))
//    {
//        LOG_ERR(("Start FCGI [%s]: invalid path to executable - %s,"
//                 " not exist or not executable ",
//                config.getName(),config.getCommand() ));
//        return -1;
//    }
//    if ( st.st_mode & S_ISUID )
//    {
//        if ( D_ENABLED( DL_LESS ))
//            LOG_D(( "Fast CGI [%s]: Setuid bit is not allowed : %s\n",
//                config.getName(), config.getCommand() ));
//        return -1;
//    }
    if ( app.getfd() < 0 )
    {
        fd = ExtWorker::startServerSock( &config, config.getBackLog() );
        if ( fd != -1 )
        {
            app.setfd( fd );
            if ( config.getServerAddr().family() == PF_UNIX )
            {
                nio_stat( config.getServerAddr().getUnix(), &st );
                HttpGlobals::getServerInfo()->addUnixSocket(
                     config.getServerAddr().getUnix(), &st );
            }
        }
        else
            return -1;
    }
    int instances = config.getInstances();
    int cur_instances = app.getCurInstances();
    int new_instances = app.getConnPool().getTotalConns() + 2 - cur_instances;
    if ( new_instances <= 0 )
        new_instances = 1;
    if ( instances < new_instances + cur_instances )
    {
        new_instances = instances - cur_instances;
    }
    if ( new_instances <= 0 )
        return 0;
    int i;
    for( i = 0; i < new_instances; ++i )
    {
        int pid;
        pid = LocalWorker::workerExec( config, fd );
        if ( pid > 0 )
        {
            if ( D_ENABLED( DL_LESS ) )
                LOG_D(( "[%s] add child process pid: %d", app.getName(), pid ));
            PidRegistry::add( pid, &app, 0 );
        }
        else
        {
            LOG_ERR(("Start FCGI [%s]: failed to start the %d of %d instances.",
                config.getName(), i+1, instances ));
            break;
        }
    }
    return (i==0)?-1:0;
}
Пример #10
0
int RewriteEngine::processCond( const RewriteCond * pCond, HttpConnection *pConn )
{
    int len = REWRITE_BUF_SIZE - 1;
    char * pTest;
    if ( m_pLastCondStr && m_pLastCondStr->equal( * pCond->getTestStringFormat() ) )
    {
        pTest = m_pLastTestStr;
        len = m_lastTestStrLen;
    }
    else
    {
        pTest = buildString( pCond->getTestStringFormat(),
                                      pConn, m_pFreeBuf, len );
        m_pLastCondStr = pCond->getTestStringFormat();
        m_pLastTestStr = pTest;
        m_lastTestStrLen = len;
        m_noStat = 1;
    }
    int ret = 0;
    if ( !pTest )
        return -1;
    int condVec[ MAX_REWRITE_MATCH * 3 ];
    int condMatches = 0;
    //m_condMatches = 0;
    int code = pCond->getOpcode();
    if ( code == COND_OP_REGEX )
    {
        ret = pCond->getRegex()->exec( pTest, len, 0, 0, condVec,
                                       MAX_REWRITE_MATCH * 3);
        if ( m_logLevel > 2 )
            LOG_INFO(( pConn->getLogger(),
                "[%s] [REWRITE] Cond: Match '%s' with pattern '%s', result: %d",
                pConn->getLogId(), pTest, pCond->getPattern(), ret ));
        if ( ret == 0 )
            condMatches = 10;
        else
            condMatches = ret;
        ret = (ret < 0 );
    }
    else if (( code >= COND_OP_LESS)&&( code <= COND_OP_EQ ))
    {
        if ( pCond->getFlag() & COND_FLAG_NOCASE )
            ret = strcasecmp( pTest, pCond->getPattern() );
        else
            ret = strcmp( pTest, pCond->getPattern() );
        if ( m_logLevel > 2 )
            LOG_INFO(( pConn->getLogger(),
                "[%s] [REWRITE] Cond: Compare '%s' with pattern '%s', result: %d",
                pConn->getLogId(), pTest, pCond->getPattern(), ret ));
        switch( code )
        {
        case COND_OP_LESS:
            ret = ( ret >= 0 );
            break;
        case COND_OP_GREATER:
            ret = ( ret <= 0 );
            break;
        case COND_OP_EQ:
            ret = ( ret != 0 );
            break;
        }        
    }
    else if (( code >= COND_OP_DIR )&& (code <= COND_OP_FILE_ACC ))
    {
        if ( m_noStat == 1 )
            m_noStat = nio_stat( pTest, &m_st );
        if ( m_noStat == 0 )
        {
            switch( code )
            {
            case COND_OP_DIR:
                ret = !S_ISDIR( m_st.st_mode );
                break;
            case COND_OP_FILE:
                ret = !S_ISREG( m_st.st_mode );
                break;
            case COND_OP_SIZE:
                ret = (!S_ISREG( m_st.st_mode )||( m_st.st_size == 0 ));
                break;
            case COND_OP_SYM:
                ret = !S_ISLNK( m_st.st_mode );
                break;
            case COND_OP_FILE_ACC:
                ret = !S_ISREG( m_st.st_mode );
                break;
            default:
                ret = -1;
            }
            if ( m_logLevel > 2 )
                LOG_INFO(( pConn->getLogger(),
                    "[%s] [REWRITE] Cond: test '%s' with pattern '%s', result: %d",
                    pConn->getLogId(), pTest, pCond->getPattern(), ret ));
        }
        else
        {
            if ( m_logLevel > 2 )
                LOG_INFO(( pConn->getLogger(),
                "[%s] [REWRITE] stat( %s ) failed ",
                pConn->getLogId(), pTest ));
            ret = -1;
        }
        
    }
    else if ( code == COND_OP_URL_ACC )
    {
        //do nothing.
    }
    else
        assert( 0 );
    if (!(pCond->getFlag() & COND_FLAG_NOMATCH) )
    {
        if ( condMatches > 0 )
        {
            if ( pTest == m_pFreeBuf )
            {
                m_pFreeBuf = m_pCondBuf;
                m_pCondBuf = pTest;
            }
            m_condMatches = condMatches;
            memmove( m_condVec, condVec, condMatches * 3 * sizeof( int ) );
        }
        return ret;
    }
    else
        return !ret;
    
}
Пример #11
0
int SSIEngine::processFileAttr( HttpSession *pSession, SSIComponent * pComponent )
{
    SubstItem *pItem = (SubstItem *)pComponent->getFirstAttr();
    if ( !pItem )
        return 0;
    int len;
    int attr = pItem->getSubType();
    char achBuf[4096];
    char * p = achBuf;
    len = 4096;
    if (( attr != SSI_INC_FILE )&&( attr != SSI_INC_VIRTUAL ))
        return 0;
    SSIRuntime * pRuntime = pSession->getReq()->getSSIRuntime();
    RequestVars::appendSubst( pItem, pSession, p, len,
                0, pRuntime->getRegexResult() );
    {
        if ( achBuf[0] == 0 )
        {
            len = snprintf( achBuf, 4096, "[an error occurred while processing this directive]\n" );
            pSession->appendDynBody( achBuf, len );
            return 0;
        }
        HttpReq * pReq = pSession->getReq();
        const char * pURI ;
        const char * p1;
        if (( attr == SSI_INC_FILE )||( achBuf[0] != '/' ))
        {
            pURI = pReq->getRealPath()->c_str();
            p1 =  pURI + pReq->getRealPath()->len();
            while( (p1 > pReq->getRealPath()->c_str()) && p1[-1] != '/' )
                --p1;
        }
        else
        {
            pURI = pReq->getDocRoot()->c_str();
            p1 = pURI + pReq->getDocRoot()->len();
        }
        int prefix_len = p1 - pURI;
        memmove( &achBuf[prefix_len], achBuf, p - achBuf );
        memmove( achBuf, pURI, prefix_len );
        p += prefix_len;
    }
    *p = 0;
    p = achBuf;
    struct stat st;
    if ( nio_stat( achBuf, &st ) == -1 )
    {
        pSession->appendDynBody( "[error: stat() failed!\n", 23 );
        return 0;
    }
    if ( pComponent->getType() == SSIComponent::SSI_FSize )
    {
        long long size = st.st_size;
        len = snprintf( achBuf, 1024, "%lld", size );
    }
    else    //SSIComponent::SSI_Flastmod
    {
        struct tm * tm;
        tm = localtime( &st.st_mtime );
        len = strftime( achBuf, 1024, pRuntime
                    ->getConfig()->getTimeFmt()->c_str(), tm );
    }
    pSession->appendDynBody( achBuf, len );
    return 0;
}
Пример #12
0
int Awstats::prepareAwstatsEnv( const HttpVHost * pVHost )
{
    uid_t uid = HttpGlobals::s_uid;
    gid_t gid = HttpGlobals::s_gid;
    struct stat st;
    char achBuf[1024];
    
    if ( pVHost->getRootContext().getSetUidMode() == UID_DOCROOT )
    {
        uid = pVHost->getUid();
        gid = pVHost->getGid();
    }
    
    // create working directory if need
    if ( nio_stat( m_sWorkingDir.c_str(), &st ) == -1 )
    {
        if ( mkdir( m_sWorkingDir.c_str(), 0755 ) == -1 )
        {
            LOG_ERR(( "Can not create awstats working directory[%s]: %s",
                m_sWorkingDir.c_str(), strerror( errno ) ));
            return -1;
            
        }
    }
    chown( m_sWorkingDir.c_str(), uid, gid );
    if ( m_iMode == AWS_STATIC )
    {
        safe_snprintf( achBuf, 1024, "%s/html", m_sWorkingDir.c_str() );
        if ( nio_stat( achBuf, &st ) == -1 )
        {
            if ( mkdir( achBuf, 0755 ) == -1 )
            {
                LOG_ERR(( "Can not create awstats working directory[%s]: %s",
                    achBuf, strerror( errno ) ));
                return -1;
            }
        }
        chown( achBuf, uid, gid );
    }
    // create data directory if need
    safe_snprintf( achBuf, 1024, "%s/data", m_sWorkingDir.c_str() );
    mkdir( achBuf, 0755 );
    chown( achBuf, uid, gid );
    // create conf directory if need
    safe_snprintf( achBuf, 1024, "%s/conf", m_sWorkingDir.c_str() );
    mkdir( achBuf, 0755 );
    chown( achBuf, uid, gid );
    // copy awstats.model.conf to conf/ directory if does not exist
    safe_snprintf( achBuf, 1024, "%s/conf/awstats.model.conf", m_sWorkingDir.c_str() );
    if ( nio_stat( achBuf, &st ) == -1 )
    {
        char achTmp[1024];
        const char * pChroot = "";
        if (HttpGlobals::s_psChroot)
            pChroot = HttpGlobals::s_psChroot->c_str();
        
        safe_snprintf( achTmp, 1024, "%s%s/add-ons/awstats/wwwroot/cgi-bin/awstats.model.conf",
                  pChroot, HttpGlobals::s_pServerRoot);
        if ( copyFile( achTmp, achBuf ) )
        {
            return -1;
        }
        chown( achBuf, uid, gid );
    }

    if ( createConfigFile( achBuf, pVHost ) == -1 )
        return -1;
    chown( achBuf, uid, gid );
    return 0;        
}