-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin.authd.py
119 lines (84 loc) · 2.6 KB
/
in.authd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/python
# Add \r to all prints because original ircd seems to expect it.
import sys, os, re
def main():
if len(sys.argv) > 1:
query = ' '.join(sys.argv[1:])
else:
query = sys.stdin.readline()
query = query.strip().split(',')
if len(query) == 2:
query_local_port = query[0]
query_remote_port = query[1]
# Accept only int
try:
query_local_port = int(query_local_port)
query_remote_port = int(query_remote_port)
except:
print "%s , %s : ERROR : INVALID-PORT\r" % (str(query_local_port), str(query_remote_port))
sys.exit(1)
uid = get_connection_uid(query_local_port, query_remote_port)
if uid:
username = get_username(uid)
if username:
print "%s , %s : USERID : UNIX : %s\r" % (str(query_local_port), str(query_remote_port), str(username))
else:
print "%s , %s : ERROR : NO-USER\r" % (str(query_local_port), str(query_remote_port))
else:
print "%s , %s : ERROR : NO-USER\r" % (str(query_local_port), str(query_remote_port))
else:
print "0 , 0 : ERROR :INVALID-PORT\r"
#
# Get connection user id
#
# Parameters: query_local_port, query_remote_port
# Returns: uid if possible, else -1
#
#
def get_connection_uid(query_local_port, query_remote_port):
uid = False
# Try to open connection details.
ports = []
try:
if os.path.isfile("/proc/net/tcp"):
ports.extend(open("/proc/net/tcp", "r").readlines())
if os.path.isfile("/proc/net/tcp6"):
ports.extend(open("/proc/net/tcp6", "r").readlines())
except:
print "%s , %s : ERROR : UNKNOWN-ERROR\r" % (str(query_local_port), str(query_remote_port))
sys.exit(1)
for line in ports:
# Tidy up line
line = re.sub(r'([a-zA-Z0-9])\s+', r'\1 ', line.strip()).split(' ')
if len(line) > 11:
if len(line[1].split(":")) > 1:
tcp_local_port = line[1].split(':')[1]
tcp_remote_port = line[2].split(':')[1]
try:
tcp_local_port = int(tcp_local_port, 16)
tcp_remote_port = int(tcp_remote_port, 16)
except:
pass
if tcp_local_port == query_local_port and tcp_remote_port == query_remote_port:
uid = line[7]
return uid
#
# Get username by uid
#
# Paramers: uid
# Returns: username if possible, else -1
#
def get_username(uid):
username = False
try:
fp = open("/etc/passwd", "r")
except:
sys.exit(1)
for line in fp:
line = line.split(':')
if line[2] == uid:
username = line[0]
break
fp.close()
return username
main()