2017-02-19 3 views
0

프로젝트의 경우 아파치 레벨에서 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; 
} 

지금, 나는 아파치에서이 모듈을 연결해야합니다.

ap_hook_handler에있는 APR_HOOK_REALLY_FIRST 매개 변수는 다른 처리기보다 먼저이 처리기를 실행한다는 것을 알고 있습니다.

하지만이 핸들러를 특정 디렉토리 내에서 발생하는 모든 요청 전에 실행하는 방법을 알아야합니다.

답변

0

나는 마침내 그것을 알아 냈다. 핸들러 단계에서 후크를 등록하는 대신, 초기 단계에서 모듈을 등록해야합니다 : access_checker.

누구나 관심이있는 경우 최종 코드는 github에 있습니다.