Merge lp:~mgorven/ibid/port-numbers into lp:~ibid-core/ibid/old-trunk-1.6

Proposed by Michael Gorven
Status: Merged
Approved by: Stefano Rivera
Approved revision: not available
Merged at revision: not available
Proposed branch: lp:~mgorven/ibid/port-numbers
Merge into: lp:~ibid-core/ibid/old-trunk-1.6
Diff against target: 105 lines (+72/-2)
2 files modified
ibid/plugins/network.py (+71/-1)
ibid/plugins/sysadmin.py (+1/-1)
To merge this branch: bzr merge lp:~mgorven/ibid/port-numbers
Reviewer Review Type Date Requested Status
Stefano Rivera Approve
Keegan Carruthers-Smith Approve
marcog (community) Approve
Ibid Core Team Pending
Max Rabkin Pending
Review via email: mp+19260@code.launchpad.net

This proposal supersedes a proposal from 2010-02-09.

To post a comment you must log in.
Revision history for this message
Michael Gorven (mgorven) wrote : Posted in a previous version of this proposal

Simple port/protocol lookup using /etc/services.

Revision history for this message
Keegan Carruthers-Smith (keegan-csmith) wrote : Posted in a previous version of this proposal

Looks good

review: Approve
Revision history for this message
Max Rabkin (max-rabkin) wrote : Posted in a previous version of this proposal

Are comments in /etc/services guaranteed to be at the start of the
line or be preceded by whitespace? On Debian, /etc/services points to
/usr/share/nmap/nmap-services for a more complete list. Perhaps we
should use that if it's available? Unfortunately, it's in a slightly
different format from /etc/services (it has an open probability where
/etc/services has aliases).

If the answer to my questions are yes and no, then it looks good.

Review: Approve

Revision history for this message
Keegan Carruthers-Smith (keegan-csmith) wrote : Posted in a previous version of this proposal

> Are comments in /etc/services guaranteed to be at the start of the
> line or be preceded by whitespace?
No, they also occur after an entry but the code correctly deals with that.

Revision history for this message
Max Rabkin (max-rabkin) wrote : Posted in a previous version of this proposal

Also, specifying a transport should be optional (bot should reply to
"port 25" with "smtp on TCP", or something like that.

Revision history for this message
Michael Gorven (mgorven) wrote : Posted in a previous version of this proposal

* Use Nmap's services file if it exists
* Make TCP/UDP optional when asking about port numbers
* Handle SCTP as well

Revision history for this message
Max Rabkin (max-rabkin) : Posted in a previous version of this proposal
review: Approve
Revision history for this message
Michael Gorven (mgorven) wrote : Posted in a previous version of this proposal

Moved to network plugin and tweaked portfor regex.

Revision history for this message
Stefano Rivera (stefanor) wrote : Posted in a previous version of this proposal

> from collections import defaultdict

Get that from ibid.compat

More review coming when I look at it properly

Revision history for this message
Stefano Rivera (stefanor) wrote : Posted in a previous version of this proposal

I'm not convinced that this needs to be cached from load time. Generating the dict on use would save memory. But that's a more general issue, not specific to this plugin.

ICMP ports?

Query: usage ports
Response: I don't know about that protocol
Query: help ports
Response: I don't know about that protocol

review: Needs Fixing
Revision history for this message
Michael Gorven (mgorven) wrote : Posted in a previous version of this proposal

> ICMP ports?

The services files doesn't have that information, and it would be used
extremely rarely.

Revision history for this message
Michael Gorven (mgorven) wrote :

Fix masking of help plugin, and only load services file during first request.

Revision history for this message
marcog (marco-gallotta) wrote :

Query: port for http
Response: 80/tcp and 80/udp
Query: port 80
Response: http (TCP) and http (UDP)

Perhaps group them if the protocol is the same?

Query: port for http
Response: 80 (tcp and udp)
Query: port 80
Response: http (TCP and UDP)

The one response uses / and the other uses (), perhaps they should match. The case should also match.

Query: X11:1 port
Response: I don't know about that protocol
Query: port 6001
Response: X11:1 (TCP)

?

review: Needs Fixing
lp:~mgorven/ibid/port-numbers updated
893. By Michael Gorven

Fix case sensitivity in protocol name lookup.

894. By Michael Gorven

Display transport consistently.

Revision history for this message
Michael Gorven (mgorven) wrote :

> The one response uses / and the other uses (), perhaps they should match.
> The case should also match.

r894

> Query: X11:1 port
> Response: I don't know about that protocol

r893

Revision history for this message
Michael Gorven (mgorven) wrote :

> Perhaps group them if the protocol is the same?

Not really worth the effort IMHO.

Revision history for this message
marcog (marco-gallotta) :
review: Approve
Revision history for this message
Keegan Carruthers-Smith (keegan-csmith) :
review: Approve
Revision history for this message
Stefano Rivera (stefanor) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'ibid/plugins/network.py'
--- ibid/plugins/network.py 2010-01-29 23:33:18 +0000
+++ ibid/plugins/network.py 2010-02-13 09:40:30 +0000
@@ -3,11 +3,13 @@
33
4import re4import re
5import socket5import socket
6from ibid.compat import defaultdict
6from httplib import HTTPConnection, HTTPSConnection7from httplib import HTTPConnection, HTTPSConnection
8from os.path import exists
7from subprocess import Popen, PIPE9from subprocess import Popen, PIPE
10from sys import version_info
8from urllib import getproxies_environment11from urllib import getproxies_environment
9from urlparse import urlparse12from urlparse import urlparse
10from sys import version_info
1113
12try:14try:
13 from dns.resolver import Resolver, NoAnswer, NXDOMAIN15 from dns.resolver import Resolver, NoAnswer, NXDOMAIN
@@ -431,4 +433,72 @@
431433
432 event.addresponse(u"ISO doesn't know about any TLD for %s", location)434 event.addresponse(u"ISO doesn't know about any TLD for %s", location)
433435
436help['ports'] = u'Looks up port numbers for protocols'
437class Ports(Processor):
438 u"""port for <protocol>
439 (tcp|udp) port <number>"""
440 feature = 'ports'
441 priority = 10
442
443 services = Option('services', 'Path to services file', '/etc/services')
444 nmapservices = Option('nmap-services', "Path to Nmap's services file", '/usr/share/nmap/nmap-services')
445 protocols = {}
446 ports = {}
447
448 def setup(self):
449 self.filename = None
450 if exists(self.nmapservices):
451 self.filename = self.nmapservices
452 self.nmap = True
453 elif exists(self.services):
454 self.filename = self.services
455 self.nmap = False
456
457 if not self.filename:
458 raise Exception(u"Services file doesn't exist")
459
460 def _load_services(self):
461 if self.protocols:
462 return
463
464 self.protocols = defaultdict(list)
465 self.ports = defaultdict(list)
466 f = open(self.filename)
467 for line in f.readlines():
468 parts = line.split()
469 if parts and not parts[0].startswith('#') and parts[0] != 'unknown':
470 number, transport = parts[1].split('/')
471 port = '%s (%s)' % (number, transport.upper())
472 self.protocols[parts[0].lower()].append(port)
473 self.ports[parts[1]].append(parts[0])
474 if not self.nmap:
475 for proto in parts[2:]:
476 if proto.startswith('#'):
477 break
478 self.protocols[proto.lower()].append(port)
479
480 @match(r'^(?:(.+)\s+)?ports?(?:\s+numbers?)?(?(1)|\s+for\s+(.+))$')
481 def portfor(self, event, proto1, proto2):
482 self._load_services()
483 protocol = (proto1 or proto2).lower()
484 if protocol in self.protocols:
485 event.addresponse(human_join(self.protocols[protocol]))
486 else:
487 event.addresponse(u"I don't know about that protocol")
488
489 @match(r'^(?:(udp|tcp|sctp)\s+)?port\s+(\d+)$')
490 def port(self, event, transport, number):
491 self._load_services()
492 results = []
493 if transport:
494 results.extend(self.ports.get('%s/%s' % (number, transport.lower()), []))
495 else:
496 for transport in ('tcp', 'udp', 'sctp'):
497 results.extend('%s (%s)' % (protocol, transport.upper()) for protocol in self.ports.get('%s/%s' % (number, transport.lower()), []))
498
499 if results:
500 event.addresponse(human_join(results))
501 else:
502 event.addresponse(u"I don't know about any protocols using that port")
503
434# vi: set et sta sw=4 ts=4:504# vi: set et sta sw=4 ts=4:
435505
=== modified file 'ibid/plugins/sysadmin.py'
--- ibid/plugins/sysadmin.py 2010-01-18 23:20:33 +0000
+++ ibid/plugins/sysadmin.py 2010-02-13 09:40:30 +0000
@@ -1,8 +1,8 @@
1# Copyright (c) 2009-2010, Michael Gorven, Stefano Rivera1# Copyright (c) 2009-2010, Michael Gorven, Stefano Rivera
2# Released under terms of the MIT/X/Expat Licence. See COPYING for details.2# Released under terms of the MIT/X/Expat Licence. See COPYING for details.
33
4import os
4from subprocess import Popen, PIPE5from subprocess import Popen, PIPE
5import os
66
7from ibid.plugins import Processor, match7from ibid.plugins import Processor, match
8from ibid.config import Option8from ibid.config import Option

Subscribers

People subscribed via source and target branches