-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommon_functions.py
94 lines (76 loc) · 1.88 KB
/
common_functions.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
import os
import sys
import pwd
import grp
################################################################################
# Common Functions
################################################################################
# VALIDATIONS
def user_exists(user):
"""Verify user is present on system(s)"""
try:
if pwd.getpwnam(user):
return True
except KeyError:
return(False)
def uid_exists(user):
"""Verify UID is present on system(s)"""
try:
if pwd.getpwuid(int(user)):
return True
except KeyError:
return(False)
def group_exists(group):
"""Verify group is present on system(s)"""
try:
if grp.getgrnam(group):
return True
except KeyError:
return(False)
def gid_exists(group):
"""Verify GID is present on system(s)"""
try:
if grp.getgrgid(group):
return True
except KeyError:
return(False)
# CONVERSIONS
def user_to_uid(user):
"""Translate a username to a uid."""
try:
username = pwd.getpwnam(user)[2]
return username
except KeyError:
return(False)
def uid_to_user(user):
"""Translate a uid to a username."""
try:
username = pwd.getpwuid(int(user))[0]
return username
except KeyError:
return(False)
def group_to_gid(group):
"""Translate a groupname to a gid."""
try:
groupname = grp.getgrnam(group)[2]
return groupname
except KeyError:
return(False)
def gid_to_group(group):
"""Translate a gid to a groupname."""
try:
groupname = grp.getgrgid(int(group))[0]
return groupname
except KeyError:
return(False)
# USER IN GROUP
def userInGrp(uid,gid):
"""Determine if a user is part of a group"""
if pwd.getpwuid(uid)[0] in grp.getgrgid(gid)[3]:
return(True)
else:
return(False)
# PERMISSION OCTETS
def getOctects(objPath):
"""Stat a file object and return the last 3 characters of st_mode"""
return(oct(os.stat(objPath).st_mode)[-3])