Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

inetdtap.py 3.4KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- test-case-name: twisted.runner.test.test_inetdtap -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Twisted inetd TAP support
  6. The purpose of inetdtap is to provide an inetd-like server, to allow Twisted to
  7. invoke other programs to handle incoming sockets.
  8. This is a useful thing as a "networking swiss army knife" tool, like netcat.
  9. """
  10. import grp
  11. import pwd
  12. import socket
  13. from twisted.application import internet, service as appservice
  14. from twisted.internet.protocol import ServerFactory
  15. from twisted.python import log, usage
  16. from twisted.runner import inetd, inetdconf
  17. # Protocol map
  18. protocolDict = {"tcp": socket.IPPROTO_TCP, "udp": socket.IPPROTO_UDP}
  19. class Options(usage.Options):
  20. """
  21. To use it, create a file named `sample-inetd.conf` with:
  22. 8123 stream tcp wait some_user /bin/cat -
  23. You can then run it as in the following example and port 8123 became an
  24. echo server.
  25. twistd -n inetd -f sample-inetd.conf
  26. """
  27. optParameters = [
  28. ["rpc", "r", "/etc/rpc", "DEPRECATED. RPC procedure table file"],
  29. ["file", "f", "/etc/inetd.conf", "Service configuration file"],
  30. ]
  31. optFlags = [["nointernal", "i", "Don't run internal services"]]
  32. compData = usage.Completions(optActions={"file": usage.CompleteFiles("*.conf")})
  33. def makeService(config):
  34. s = appservice.MultiService()
  35. conf = inetdconf.InetdConf()
  36. with open(config["file"]) as f:
  37. conf.parseFile(f)
  38. for service in conf.services:
  39. protocol = service.protocol
  40. if service.protocol.startswith("rpc/"):
  41. log.msg("Skipping rpc service due to lack of rpc support")
  42. continue
  43. if (protocol, service.socketType) not in [("tcp", "stream"), ("udp", "dgram")]:
  44. log.msg(
  45. "Skipping unsupported type/protocol: %s/%s"
  46. % (service.socketType, service.protocol)
  47. )
  48. continue
  49. # Convert the username into a uid (if necessary)
  50. try:
  51. service.user = int(service.user)
  52. except ValueError:
  53. try:
  54. service.user = pwd.getpwnam(service.user)[2]
  55. except KeyError:
  56. log.msg("Unknown user: " + service.user)
  57. continue
  58. # Convert the group name into a gid (if necessary)
  59. if service.group is None:
  60. # If no group was specified, use the user's primary group
  61. service.group = pwd.getpwuid(service.user)[3]
  62. else:
  63. try:
  64. service.group = int(service.group)
  65. except ValueError:
  66. try:
  67. service.group = grp.getgrnam(service.group)[2]
  68. except KeyError:
  69. log.msg("Unknown group: " + service.group)
  70. continue
  71. if service.program == "internal":
  72. if config["nointernal"]:
  73. continue
  74. # Internal services can use a standard ServerFactory
  75. if service.name not in inetd.internalProtocols:
  76. log.msg("Unknown internal service: " + service.name)
  77. continue
  78. factory = ServerFactory()
  79. factory.protocol = inetd.internalProtocols[service.name]
  80. else:
  81. factory = inetd.InetdFactory(service)
  82. if protocol == "tcp":
  83. internet.TCPServer(service.port, factory).setServiceParent(s)
  84. elif protocol == "udp":
  85. raise RuntimeError("not supporting UDP")
  86. return s