# normalize MAC addresses
mac-addr-regexp = '([0-9a-f]{2})[^0-9a-f]?([0-9a-f]{2})[^0-9a-f]?([0-9a-f]{2})[^0-9a-f]?([0-9a-f]{2})[^0-9a-f]?([0-9a-f]{2})[^0-9a-f]?([0-9a-f]{2})'
# packetfence-local-auth
# Try to get the Cleartext-Password from any of the pfguest, pfsponsor or pfsms
# databases.
# If this fails, the mschap module will try to authenticate using ntlm_auth.

packetfence-local-auth { 
    # Disable ntlm_auth
    update control {
        &MS-CHAP-Use-NTLM-Auth := No
    }
    # Check password table for local user
    pflocal
    if (fail || notfound || noop) {
        # Check password table with email and password for a sponsor registration
        pfguest
        if (fail || notfound || noop) {
            # Check password table with email and password for a guest registration
            pfsponsor
            if (fail || notfound || noop) {
                # Check activation table with phone number and PIN code 
                pfsms
                if (fail || notfound || noop) {
                    update control {
                       &MS-CHAP-Use-NTLM-Auth := Yes
                    }
                }
            }
        }
    }

}
### END Local SQL authentication
packetfence-switch-access { 
    if ( \
        ( &Service-Type ==  "NAS-Prompt-User") && \
        ( &NAS-Port-Type == "Virtual" || &NAS-Port-Type == "Async") ) {
            rest-switch-access  
    }
    
}

request-timing {
    if (control:PacketFence-Request-Time != 0) {
        update control {
            &PacketFence-Request-Time := "%{expr: %{control:PacketFence-Request-Time} - %{control:Tmp-Integer-0}}"
        }
    }
}


packetfence-eap-mac-policy {
    if ( &EAP-Type ) {

        if (&Calling-Station-Id && (&Calling-Station-Id =~ /^${policy.mac-addr-regexp}$/i)) {
            update {
                &request:Tmp-String-1 := "%{tolower:%{1}%{2}%{3}%{4}%{5}%{6}}"
            }
            if  ( &Tmp-String-1 == "%{tolower:%{User-Name}}" ) {
                update {
                    &control:Cleartext-Password := &request:User-Name
                }
                updated
            }
        }
    }
    noop
}

packetfence-audit-log-accept {
    if(&User-Name != "dummy") {
        request-timing
        -sql
    }
}

packetfence-audit-log-reject {
    if(&User-Name != "dummy") {
        request-timing
        -sql_reject
    }
}

packetfence-mschap-authenticate {
    if(PacketFence-Domain) {
      if ( "%{User-Name}" =~ /^host\/.*/) {
        chrooted_mschap_machine
      }
      else {
        chrooted_mschap
      }
    }
    else {
      if ( "%{User-Name}" =~ /^host\/.*/) {
        mschap_machine
      }
      else {
        mschap
      }
    }
}
