home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Networking / nmap-5.00-setup.exe / nselib / imap.lua < prev    next >
Text File  |  2009-07-06  |  1KB  |  46 lines

  1. --- IMAP functions.
  2. -- @copyright Same as Nmap--See http://nmap.org/book/man-legal.html
  3.  
  4. module(... or "imap", package.seeall)
  5.  
  6. require 'stdnse'
  7.  
  8.  
  9. ---
  10. -- Asks an IMAP server for capabilities.
  11. --
  12. -- See RFC 3501.
  13. -- @param host Host to be queried.
  14. -- @param port Port to connect to.
  15. -- @return Table containing capabilities or nil on error.
  16. -- @return nil or String error message.
  17. function capabilities(host, port)
  18.    local socket = nmap.new_socket()
  19.    local capas = {}
  20.    socket:set_timeout(10000)
  21.    local proto = (port.version and port.version.service_tunnel == "ssl" and "ssl") or "tcp"
  22.    if not socket:connect(host.ip, port.number, proto) then return nil, "Could Not Connect" end
  23.  
  24.    status, line = socket:receive_lines(1)
  25.    if not string.match(line, "^[%*] OK") then return nil, "No Response" end
  26.    
  27.    socket:send("a001 CAPABILITY\r\n")
  28.    status, line = socket:receive_buf("\r\n", false)
  29.    if not status then 
  30.       capas.CAPABILITY = false
  31.    else 
  32.       while status do
  33.          if string.match(line, "^%*%s+CAPABILITY") then
  34.         line = string.gsub(line, "^%*%s+CAPABILITY", "")
  35.         for capability in string.gmatch(line, "[%w%+=-]+") do
  36.            capas[capability] = true
  37.             end
  38.             break
  39.          end
  40.          status, line = socket:receive_buf("\r\n", false)
  41.       end
  42.    end
  43.    socket:close()
  44.    return capas
  45. end
  46.