adduser.py 3.73 KB
Newer Older
Suleyman Poyraz's avatar
Suleyman Poyraz committed
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# This is a script to easily add a user to system with scom
#

import os
import sys
import dbus
import time
from optparse import OptionParser


# uid = -1 means next available uid
# first group in the groups list is the main group

user = {"uid": -1,
        "username": "kaplan",
        "realname": "Pisi",
        "home": "/home/pisi",
        "shell": "/bin/bash",
        "password": "pisi",
        "defaultgroup": "users",
        "groups": [],
        "admingroups": ["wheel"],
        "grants": [],
        "blocks": []
}

defaultGroups = "users,cdrom,plugdev,floppy,disk,audio,video,power,dialout,lp,lpadmin"

def fail(_message):
    print(_message)
    sys.exit(1)

def connectToDBus():
    global bus
    bus = None

    try:
        bus = dbus.SystemBus()
    except dbus.DBusException:
        return False

    if bus:
        return True

def addUser():
    obj = bus.get_object("tr.org.sulin.scom", "/package/baselayout")
    try:
        obj.addUser(user["uid"], user["username"], user["realname"],
                    user["home"], user["shell"], user["password"],
                    user["groups"], user["grants"], user["blocks"],
                    dbus_interface="tr.org.sulin.scom.User.Manager")
    except dbus.DBusException as e:
        fail("Error: %s" % e)


if __name__ == "__main__":
    usage = "usage: %prog [options] username"
    parser = OptionParser(usage=usage)

    parser.add_option("-c", "--comment", dest="realname", type="string", default="Pisi",
            help="comment for the user, usually used for Full Name")

    parser.add_option("-u", "--uid", dest="uid", type="int", default=-1,
            help="user id number, default is -1 meaning next available")

    parser.add_option("-g", "--gid", dest="defaultgroup", type="string", default=user["defaultgroup"],
            help="default group name for user")

    parser.add_option("-G", "--groups", dest="groups", type="string", default=defaultGroups,
            help="groups that user is member of")

    parser.add_option("-p", "--password", dest="password", type="string", default="pisi",
            help="password")

    parser.add_option("-d", "--home", dest="home", type="string", default="",
            help="password")

    parser.add_option("-s", "--shell", dest="shell", type="string", default="/bin/bash",
            help="default login shell")

    parser.add_option("--admin", action="store_true", dest="isadmin", default=False,
            help="give user admin rights, adding to wheel group")

    parser.add_option("--dry-run", action="store_true", dest="dryrun", default=False,
            help="do not add user, only show what will be done")


    (opts, args) = parser.parse_args()

    if len(args) != 1:
        fail("Please provide a username.")

    user["username"] = args[0]
    groups = opts.groups.split(",")


    if opts.home == "":
        user["home"] = "/home/%s" % user["username"]
    else:
        user["home"] = opts.home


    if opts.defaultgroup in groups:
        groups.remove(user["defaultgroup"])
    user["defaultgroup"] = opts.defaultgroup
    user["groups"].append(user["defaultgroup"])

    if opts.isadmin:
        for i in user["admingroups"]:
            if i not in groups:
                groups.append(i)


    user["groups"].extend(groups)
    user["shell"] = opts.shell
    user["realname"] = opts.realname
    user["password"] = opts.password
    user["uid"] = opts.uid

    if opts.dryrun:
        for i in list(user.keys()):
            print("%s\t%s" % (i, user[i]))
    else:
        if os.getuid() != 0:
            fail("You must have root permissions to add a user")

        if not connectToDBus():
            fail("Could not connect to D-Bus, please check your system settings")
        addUser()