-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathuseradd
executable file
·41 lines (32 loc) · 1.55 KB
/
useradd
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
#! /usr/bin/env python
from manageldap import *
from subprocess import *
import argparse, os, pwd, re
def performLocal(name):
user = pwd.getpwnam(name)
uid = user.pw_uid
gid = user.pw_gid
home = user.pw_dir
if not os.path.isdir(home):
os.mkdir(home)
os.chown(home, uid, gid)
local = "/usr/local/sbin/adduser.local"
if os.path.exists(local):
adduser = call([local, name, str(uid), str(gid), home])
parser = argparse.ArgumentParser(description="Add users to LDAP Directory")
parser.add_argument('-g','--groups',type=str,help='A comma separated list of group names',default="")
parser.add_argument('-u','--uid',type=int,help='The uid of the new user.',default=0)
parser.add_argument('-n','--name',type=str,help='The name of the user. This is placed in the comment field.',default='')
parser.add_argument('-s','--shell',type=str,help='The login shell of the user. The default shell is /bin/bash.',default="/bin/bash")
parser.add_argument('-c','--gecos',type=str,help='This is a description of the user.',default="")
parser.add_argument("login",type=str)
args = parser.parse_args()
if re.match("^[a-z][a-z0-9-]*$", args.login):
groups = args.groups.split(",") if args.groups != "" else []
newuser = useradd(args.login,groups=groups,uid=args.uid,name=args.name,shell=args.shell,gecos=args.gecos)
update(newuser)
# Do local things (create home, run adduser scripts, etc.)
performLocal(args.login)
else:
print("Username must start with a lowercase letter and contain only letters, numbers and dashes.")
exit(1)