2018-06-26 23:30:06 -07:00
|
|
|
"""
|
|
|
|
|
User management for tljh.
|
|
|
|
|
|
2018-06-27 02:07:49 -07:00
|
|
|
Supports minimal user & group management
|
2018-06-26 23:30:06 -07:00
|
|
|
"""
|
|
|
|
|
import grp
|
2019-10-24 15:52:37 +02:00
|
|
|
import pwd
|
2018-06-26 23:30:06 -07:00
|
|
|
import subprocess
|
2018-08-29 11:04:07 -07:00
|
|
|
from os.path import expanduser
|
2018-06-26 23:30:06 -07:00
|
|
|
|
2019-10-24 15:52:37 +02:00
|
|
|
# Set up plugin infrastructure
|
2019-10-28 08:48:52 +01:00
|
|
|
from tljh.utils import get_plugin_manager
|
2019-10-24 15:52:37 +02:00
|
|
|
|
2018-06-26 23:30:06 -07:00
|
|
|
|
|
|
|
|
def ensure_user(username):
|
|
|
|
|
"""
|
|
|
|
|
Make sure a given user exists
|
|
|
|
|
"""
|
|
|
|
|
# Check if user exists
|
|
|
|
|
try:
|
|
|
|
|
pwd.getpwnam(username)
|
|
|
|
|
# User exists, nothing to do!
|
|
|
|
|
return
|
|
|
|
|
except KeyError:
|
|
|
|
|
# User doesn't exist, time to create!
|
|
|
|
|
pass
|
|
|
|
|
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.check_call(["useradd", "--create-home", username])
|
2021-11-01 09:42:45 +01:00
|
|
|
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.check_call(["chmod", "o-rwx", expanduser(f"~{username}")])
|
2018-08-29 11:04:07 -07:00
|
|
|
|
2019-12-02 15:58:01 +01:00
|
|
|
pm = get_plugin_manager()
|
2019-10-28 08:43:09 +01:00
|
|
|
pm.hook.tljh_new_user_create(username=username)
|
|
|
|
|
|
2018-06-26 23:30:06 -07:00
|
|
|
|
|
|
|
|
def remove_user(username):
|
|
|
|
|
"""
|
|
|
|
|
Remove user from system if exists
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
pwd.getpwnam(username)
|
|
|
|
|
except KeyError:
|
|
|
|
|
# User doesn't exist, nothing to do
|
|
|
|
|
return
|
|
|
|
|
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.check_call(["deluser", "--quiet", username])
|
2018-06-26 23:30:06 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def ensure_group(groupname):
|
|
|
|
|
"""
|
|
|
|
|
Ensure given group exists
|
|
|
|
|
"""
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.check_call(["groupadd", "--force", groupname])
|
2018-06-26 23:30:06 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_group(groupname):
|
|
|
|
|
"""
|
2018-07-03 17:34:57 -07:00
|
|
|
Remove group from system if exists
|
2018-06-26 23:30:06 -07:00
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
grp.getgrnam(groupname)
|
|
|
|
|
except KeyError:
|
|
|
|
|
# Group doesn't exist, nothing to do
|
|
|
|
|
return
|
|
|
|
|
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.check_call(["delgroup", "--quiet", groupname])
|
2018-06-26 23:30:06 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def ensure_user_group(username, groupname):
|
|
|
|
|
"""
|
|
|
|
|
Ensure given user is member of given group
|
|
|
|
|
|
|
|
|
|
Group and User must already exist.
|
|
|
|
|
"""
|
|
|
|
|
group = grp.getgrnam(groupname)
|
|
|
|
|
if username in group.gr_mem:
|
|
|
|
|
return
|
|
|
|
|
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.check_call(["gpasswd", "--add", username, groupname])
|
2018-06-26 23:30:06 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_user_group(username, groupname):
|
|
|
|
|
"""
|
|
|
|
|
Ensure given user is *not* a member of given group
|
|
|
|
|
"""
|
|
|
|
|
group = grp.getgrnam(groupname)
|
|
|
|
|
if username not in group.gr_mem:
|
|
|
|
|
return
|
|
|
|
|
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.check_call(["gpasswd", "--delete", username, groupname])
|