docker-samba/add-user-group.py

47 lines
1.6 KiB
Python
Raw Normal View History

2021-02-19 11:54:25 +00:00
#!/usr/bin/python3
import os
import sys
import subprocess
2021-02-19 23:26:08 +00:00
import shlex
2021-02-19 11:54:25 +00:00
def main():
if (len(sys.argv) < 3):
print("No users/groups to configure.")
return
groups = sys.argv[1].split(';')
users = sys.argv[2].split(';')
# group,groupid
for group in groups:
elements = group.split(',')
if (len(elements) != 2):
print("Skipping invalid group config string \"" + group + "\"")
continue
2021-02-19 23:41:39 +00:00
gid = elements[1]
gname = elements[0]
subprocess.check_call("addgroup -g" + shlex.quote(gid) + " " + shlex.quote(gname), shell=True)
print("Added group " + gname + " with gid " + gid)
2021-02-19 11:54:25 +00:00
# username,uid,password,[group]
for user in users:
elements = user.split(',')
if (len(elements) != 3 and len(elements) != 4):
print("Skipping invalid user config string \"" + user + "\"")
continue
2021-02-19 23:41:39 +00:00
uname = elements[0]
uid = elements[1]
passwd = elements[2]
2021-02-19 23:43:08 +00:00
subprocess.check_call("adduser -D -H -u " + shlex.quote(uid) + " " + shlex.quote(uname), shell=True)
2021-02-19 23:41:39 +00:00
print("Added user " + uname + " with uid " + uid)
2021-02-19 11:54:25 +00:00
if (len(elements) == 4):
2021-02-19 23:41:39 +00:00
gname = elements[3]
subprocess.check_call("addgroup " + shlex.quote(uname) + " " + shlex.quote(gname), shell=True)
print("Added user " + uname + " to group " + gname)
2021-02-19 11:54:25 +00:00
# set passwd
2021-02-19 23:43:08 +00:00
subprocess.check_call("echo -ne " + shlex.quote(passwd + "\n" + passwd + "\n") + " | smbpasswd -a -U " + shlex.quote(uname), shell=True)
2021-02-19 23:41:39 +00:00
print("Set user " + uname + " password to " + passwd)
2021-02-19 11:54:25 +00:00
main()