2017-02-19 5 views
0

プロジェクトでは、私はapacheレベルでHMAC認証が必要でした。だから私はmod_exampleはここまでhereを説明拡張:すべてのリクエストにカスタムApacheモジュールをフックしますか?

module AP_MODULE_DECLARE_DATA hmac_module = 
     { 
       STANDARD20_MODULE_STUFF, 
       NULL,   // Per-directory configuration handler 
       NULL,   // Merge handler for per-directory configurations 
       NULL,   // Per-server configuration handler 
       NULL,   // Merge handler for per-server configurations 
       NULL,   // Any directives we may have for httpd 
       register_hooks // Our hook registering function 
     }; 


/* register_hooks: Adds a hook to the httpd process */ 
static void register_hooks(apr_pool_t *pool) 
{ 

    /* Hook the request handler */ 
    ap_hook_handler(hmac_handler, NULL, NULL,APR_HOOK_REALLY_FIRST); 
} 

static int hmac_handler(request_rec *r) 
{ 
    // ... 
    // some variable definition 
    // ... 

    // Check that the "hmac-handler" handler is being called. 
    if (!r->handler || strcmp(r->handler, "hmac-handler")) return (DECLINED); 

    ap_args_to_table(r, &GET); 
    ap_parse_form_data(r, NULL, &POST, -1, 8192); 

    timestamp = apr_table_get(r->headers_in, "X-EPOCH"); 
    claimedHash = apr_table_get(r->headers_in, "X-HMAC"); 
    if (!timestamp){ 
     ap_log_rerror(APLOG_MARK,APLOG_ERR,HTTP_FORBIDDEN,r,"Timestamp does not exits in request"); 
     return HTTP_FORBIDDEN; 
    } 
    if(!claimedHash){ 
     ap_log_rerror(APLOG_MARK,APLOG_ERR,HTTP_FORBIDDEN,r,"There is no claimed hash in the request!"); 
     return HTTP_FORBIDDEN; 
    } 

    //... 
    // calculate timestamp's sha1 hash 
    //... 

    if(strcmp(claimedHash,encoded)){ 
     ap_log_rerror(APLOG_MARK,APLOG_ERR,HTTP_FORBIDDEN,r,"Claimed hash and digested values does not match,Claimed:%s , Target:%s",claimedHash,encoded); 
     return HTTP_FORBIDDEN; 
    } 

    // Let Apache know that we responded to this request. 
    return OK; 
} 

は今、私はそれがこの要求が認証するかしない確認するために、さらにそれを処理する前にApacheにこのモジュールをフックする必要があります。

私はap_hook_handlerのパラメータがAPR_HOOK_REALLY_FIRSTであることを知っています。他のハンドラの前にこのハンドラを実行する関数です。

しかし、特定のディレクトリ内で発生するANY要求の前にこのハンドラを実行する方法を知る必要があります。

答えて

0

私は最終的に自分自身を考え出しました。ハンドラ段階でフックを登録するのではなく、以前の段階で私のモジュールを登録しておくべきです:access_checker。

最後のコードは、誰でも興味があればgithubにあります。

関連する問題