-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwoSystem.py
More file actions
131 lines (110 loc) · 2.68 KB
/
woSystem.py
File metadata and controls
131 lines (110 loc) · 2.68 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
# TODO geteuid not supported on Windows
import re
from platform import system
#########################################################
# Linux
if system() == 'Linux':
from woCommon import getCmdOutput
from os import getenv, geteuid
from pwd import getpwnam, getpwall
def getCurrentUsers():
"""
@return: list of currently logged in users
"""
u = getCmdOutput('users')
u = u.split()
u = set(u)
return list(u)
def getCurrentUser():
"""
@return: LOGNAME
"""
return getenv("LOGNAME")
def isAdmin():
"""
@return: True if root
"""
return geteuid() == 0
def isNormalUser(username):
"""Check if it is a regular user, with userid within UID_MIN and UID_MAX."""
if username == '+':
return False
#FIXME: Hide active user - bug #286529
if getenv('SUDO_USER') and username == getenv('SUDO_USER'):
return False
try:
userid = int(getpwnam(username)[2])
except:
userid = -1
logindefs = open('/etc/login.defs')
uidminmax = re.compile('^UID_(?:MIN|MAX)\s+(\d+)', re.M).findall(logindefs.read())
if uidminmax[0] < uidminmax[1]:
uidmin = int(uidminmax[0])
uidmax = int(uidminmax[1])
else:
uidmin = int(uidminmax[1])
uidmax = int(uidminmax[0])
if uidmin <= userid <= uidmax:
return True
else:
return False
def getAllNormalUsers():
"""
@return: real users without system accounts
"""
ulist = []
for u in getAllUsers():
if isNormalUser(u):
ulist.append(u)
return ulist
def getAllUsers():
"""
@return: all users including system ones
"""
pwlist = getpwall()
ulist = []
for p in pwlist:
ulist.append(p[0])
ulist = list(set(ulist)) # make it unique
ulist.sort()
return ulist
#######################################################################
# W I N D O W S
elif system() == 'Windows':
from getpass import getuser
def getCurrentUsers():
"""
@return: list of currently logged in users
"""
# TODO returns only the current user
ul = [getuser()]
return ul
def getCurrentUser():
"""
@return: LOGNAME
"""
return getuser()
def isAdmin():
"""
@return: True if root
"""
# TODO
return None
def isNormalUser(username):
"""Check if it is a regular user, with userid within UID_MIN and UID_MAX."""
# TODO
return True
def getAllNormalUsers():
"""
@return: real users without system accounts
"""
# TODO
return [getuser()]
def getAllUsers():
"""
@return: all users including system ones
"""
# TODO
return getAllNormalUsers()
else:
print("%s not supported." % system())