Esempio n. 1
0
def newuser(argv):
    """Create a new user
    newuser [-Mm] [<longopts>] <username>

    Options:
        -M  Do NOT make home directory.
        -m  Only make home directory for existing user with no home directory.

    Where long options are:
        --first_name=<firstname>
        --last_name=<lastname>
        --password=<newpass>
        --shell=<shell>
        --home=<home>
    You will be prompted for missing information.

    This function should be run as root user.
    """
    makehome = True
    onlyhome = False
    try:
        opts, longopts, args = getopt.getopt(argv[1:], "h?Mm")
    except getopt.GetoptError:
        print(newuser.__doc__)
        return
    for opt, optarg in opts:
        if opt in ("-?", "-h"):
            print(newuser.__doc__)
            return
        elif opt == "-M":
            makehome = False
        elif opt == "-m":
            onlyhome = True

    try:
        username = args[0]
    except IndexError:
        username = cliutils.get_input("Account name? ")

    try:
        pwent = passwd.getpwnam(username)
    except KeyError:
        pass
    else:
        if onlyhome:
            make_homedir(pwent.home, pwent.uid, pwent.gid)
            return
        else:
            print("User already exists, exiting.", file=sys.stderr)
            return

    password = longopts.get("password")
    if not password:
        password = ask_password()
        if not password:
            print("Passwords do not match, exiting.", file=sys.stderr)
            return

    # Get maximum UID value from passwd and database, not including system ones.
    uidl = [pwe.uid for pwe in passwd.getpwall() if pwe.uid < 10000]
    dblist = [u.uid for u in models.dbsession.query(models.User).all()]
    uidl.extend(dblist)
    uid = max(uidl) + 1

    first_name = longopts.get("first_name")
    if not first_name:
        first_name = cliutils.get_input("First Name? ")
    last_name = longopts.get("last_name")
    if not last_name:
        last_name = cliutils.get_input("Last Name? ")

    gecos = "{}, {}".format(last_name, first_name)

    shell = longopts.get("shell")
    if not shell:
        shell = cliutils.get_input("Shell? ", default="/bin/sh")

    home = longopts.get("home")
    if not home:
        home = cliutils.get_input("Homedir? ", default="/home/{}".format(username))

    email = longopts.get("email")
    if not email:
        email = cliutils.get_input("Email? ")

    primary_grp = models.dbsession.query(models.Group).filter(models.Group.groupname == "users").one()
    glist = models.dbsession.query(models.Group).all()
    glist.remove(primary_grp)
    sup_groups = cliutils.choose_multiple(glist, prompt="Extra groups?")

    superuser = cliutils.yes_no("Is admin?")

    user = models.create(
        models.User,
        username=username,
        uid=uid,
        gid=primary_grp.gid,
        gecos=gecos,
        first_name=first_name,
        last_name=last_name,
        shell=shell,
        homedir=home,
        email=email,
        is_superuser=superuser,
        is_active=True,
    )
    user.password = password
    models.dbsession.add(user)
    models.dbsession.commit()
    user.groups = sup_groups
    models.dbsession.commit()
    new_shadow(username, password)
    if makehome:
        make_homedir(home, user.uid, user.gid)
    return user
Esempio n. 2
0
File: UI.py Progetto: kdart/pycopia3
 def choose_multiple(self, somelist, chosen=None, prompt=None):
     return cliutils.choose_multiple(somelist,
                                     chosen,
                                     self._get_prompt("PS3", prompt),
                                     input=self._io.raw_input,
                                     error=self.error)
Esempio n. 3
0
 def choose_multiple(self, somelist, chosen=None, prompt=None):
     return cliutils.choose_multiple(somelist,
                                     chosen,
                                     self._get_prompt("PS3", prompt),
                                     input=self._io.raw_input,
                                     error=self.error)