#!/usr/bin/env awk
#
# Returns information about hosts sending authentication in cleartext.
#
# Dependencies:
#   - pwX
#
# Parameters:
#   - val
#   - num
#
# If val is omitted or equal to "Flows" (case insensitive),
# counts the number of flows.
#
# If num is omitted or 0, returns the full list, otherwise
# the top (num > 0) or bottom (num < 0) num results
#
# Examples:
#   - tawk -e 'passwords()' file.txt
#   - tawk -e 'passwords($numBytesSnt)' file.txt
#   - tawk -e 'passwords($numBytesSnt OFS $numPktsSnt)' file.txt
#   - tawk -e 'passwords("Flows", 5)' file.txt

@include "aggr"
@include "hdr"
@include "tuple2"
@include "_validate_col"

function passwords(val, num,        _proto, _atype) {
    _validate_col("pwxType")
    _validate_col("pwxUser")
    _validate_col("pwxPass")
    if (!_proto[1]) {
        _proto[1]  = "FTP"
        _proto[2]  = "POP3"
        _proto[3]  = "IMAP"
        _proto[4]  = "SMTP"
        _proto[5]  = "HTTP Basic"
        _proto[6]  = "HTTP Proxy"
        _proto[7]  = "HTTP GET"
        _proto[8]  = "HTTP POST"
        _proto[9]  = "IRC"
        _proto[10] = "Telnet"
        _proto[11] = "LDAP"
        _proto[12] = "PAP"
    }
    _atype = strtonum($pwxType)
    if (__PRIHDR__ && hdr()) {
        # print the header
        print "Client" OFS "Server" OFS "Proto" OFS "Username" OFS "Password" OFS (val ? val : "Flows")
    } else if (_atype) {
        aggr(tuple2() OFS _proto[_atype] OFS $pwxUser OFS $pwxPass, val, num)
    }
}
