Guest User

Untitled

a guest
Jan 4th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. import string, sys, traceback
  2.  
  3. # Read a single line of input from standard input
  4. input=sys.stdin.readline()
  5. input=string.strip(input)
  6.  
  7. # The input looks like 1234,789 where 1234 and 789 are the port
  8. # numbers of the sockets on the foreign and the local machine. The
  9. # ident daemon can then look up the user who owns that socket and
  10. # return the user's ID. (These days, when anyone can be running their
  11. # own Linux box, it's not much of a protection, but many MUDs and IRC
  12. # servers still use it.)
  13.  
  14. # Enclose everything in a try...except block, since this server must
  15. # produce some output no matter what.
  16.  
  17. try:
  18. # Write the input to a log file, so I can track what requests
  19. # have been made. (/scratch is a directory that gets erased on
  20. # every bootup.)
  21. f=open('/scratch/identd.calls', 'a')
  22. f.write(input+'\n')
  23. f.close()
  24.  
  25. # Break the input line apart to get the server and client ports,
  26. # and remove any whitespace.
  27. L=string.split(input, ',')
  28. L=map(string.strip, L)
  29. server_port, client_port = string.atoi(L[0]), string.atoi(L[1])
  30.  
  31. # Open /proc/net/tcp, and scan each line. Lines in /proc/net/tcp
  32. # look like:
  33. # 79: 00000000:004F 00000000:0000 0A 00000000:00000000 00:FFBE3990 00000000 0 0 1287
  34. # ^==local port ^==remote port ^=numeric user ID
  35.  
  36. f=open('/proc/net/tcp', 'r')
  37. f.readline() # The first line is just headers
  38.  
  39. while (1):
  40. # Read a single line
  41. L=f.readline()
  42.  
  43. # If we reach the end of the file, exit this loop
  44. if L=="": break
  45.  
  46. # Break the line into a list, splitting at the whitespace.
  47. L=string.split(L)
  48.  
  49. # Break the local and remote addresses into the IP address
  50. # and the port number.
  51. [localaddr, localport] = string.split(L[1], ':')
  52. [remoteaddr, remoteport] = string.split(L[2], ':')
  53.  
  54. # Convert the hexadecimal port numbers into integers.
  55. localport=string.atoi(localport, 16)
  56. remoteport=string.atoi(remoteport, 16)
  57.  
  58. # If they match the requested port numbers, get the
  59. # corresponding user name.
  60. if (localport==server_port and
  61. remoteport==client_port):
  62. import pwd
  63. uid=string.atoi(L[7])
  64. userid=pwd.getpwuid(uid)[0]
  65. # Output a string containing the port numbers and the user ID.
  66. sys.stdout.write('%s,%s:USERID:UNIX %s\r\n' %
  67. (server_port, client_port, userid) )
  68. sys.exit(0)
  69.  
  70. # No match for the local and remote port numbers was found, so
  71. # we'll output an error
  72. sys.stdout.write(input+':ERROR:NO-USER\r\n')
  73.  
  74. # The sys.exit(0) call raises the SystemExit exception. We don't want
  75. # to do anything when sys.exit() is called, so its handler is simply a
  76. # "pass" statement (which does nothing).
  77. except SystemExit: pass
  78.  
  79. # All other exceptions will be caught by this handler. An error
  80. # response has to be returned to the client.
  81. except:
  82. sys.stdout.write(input+':ERROR:UNKNOWN-ERROR'+ CRLF)
Add Comment
Please, Sign In to add comment