docker-samba/add-user-group.py

52 lines
1.8 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-20 00:27:46 +00:00
gid = elements[1].strip()
gname = elements[0].strip()
2021-02-19 23:49:47 +00:00
cmd = "addgroup -g" + shlex.quote(gid) + " " + shlex.quote(gname)
print(cmd)
2021-02-19 23:54:29 +00:00
subprocess.check_call("addgroup -g" + shlex.quote(gid) + " " + shlex.quote(gname), shell=True, stdout=subprocess.DEVNULL)
2021-02-19 11:54:25 +00:00
# username,uid,password,[group]
for user in users:
elements = user.split(',')
2021-02-20 00:27:46 +00:00
if (len(elements) < 3):
2021-02-19 11:54:25 +00:00
print("Skipping invalid user config string \"" + user + "\"")
continue
2021-02-20 00:27:46 +00:00
uname = elements[0].strip()
uid = elements[1].strip()
passwd = elements[2].strip()
2021-02-19 23:49:47 +00:00
cmd = "adduser -D -H -u " + shlex.quote(uid) + " " + shlex.quote(uname)
print(cmd)
2021-02-19 23:54:29 +00:00
subprocess.check_call(cmd, shell=True, stdout=subprocess.DEVNULL)
2021-02-20 00:27:46 +00:00
if (len(elements) > 3):
for i in range(3, len(elements)):
gname = elements[i].strip()
cmd = "addgroup " + shlex.quote(uname) + " " + shlex.quote(gname)
print(cmd)
subprocess.check_call(cmd, shell=True, stdout=subprocess.DEVNULL)
2021-02-19 11:54:25 +00:00
# set passwd
2021-02-19 23:52:24 +00:00
cmd = "echo -ne \"" + shlex.quote(passwd) + "\\n" + shlex.quote(passwd) + "\\n\" | smbpasswd -a -U " + shlex.quote(uname)
2021-02-19 23:49:47 +00:00
print(cmd)
2021-02-19 23:54:29 +00:00
subprocess.check_call(cmd, shell=True, stdout=subprocess.DEVNULL)
2021-02-19 11:54:25 +00:00
main()