Example #1
0
def userdel(force, remove, config, login):
    user = None
    try:
        user = pwd.getpwnam(login)
    except KeyError:
        print(_("Error: User not found"))
        exit(1)

    conf = get_config(config)
    dbs = connect_db(conf)
    pm = UserManager(config=conf, dbs=dbs)

    try:
        pm.deluser(username=login)
    except KeyError:
        print(_("Error: User not in database"))
        exit(1)

    if remove:
        shutil.rmtree(str(user.pw_dir), ignore_errors=force)

    glm = GroupListManager(conf, dbs)
    glm.delallgroupuser(login)

    dbs.commit()

    gr = None
    try:
        gr = grp.getgrgid(user.pw_gid)
        if gr.gr_mem:
            exit(0)
    except KeyError:
        dbs.commit()
        dbs.close()
        exit(0)

    gm = GroupManager(config=conf, dbs=dbs)

    try:
        gm.delgroup(gid=str(gr.gr_gid))
    except ValueError:
        print(
            _('Warning: Primary group "{group}" of user is empty but not in Database. Try "groupdel {group}"').format(
                group=gr.gr_gid
            )
        )
        exit(1)

    dbs.commit()
    dbs.close()
Example #2
0
def userdel(force, remove, config, login):
    user = None
    try:
        user = pwd.getpwnam(login)
    except KeyError:
        print(_("Error: User not found"))
        exit(1)

    conf = get_config(config)
    dbs = connect_db(conf)
    pm = UserManager(config=conf, dbs=dbs)

    try:
        pm.deluser(username=login)
    except KeyError:
        print(_("Error: User not in database"))
        exit(1)

    if remove:
        shutil.rmtree(str(user.pw_dir), ignore_errors=force)

    glm = GroupListManager(conf, dbs)
    glm.delallgroupuser(login)

    dbs.commit()

    gr = None
    try:
        gr = grp.getgrgid(user.pw_gid)
        if gr.gr_mem:
            exit(0)
    except KeyError:
        dbs.commit()
        dbs.close()
        exit(0)

    gm = GroupManager(config=conf, dbs=dbs)

    try:
        gm.delgroup(gid=str(gr.gr_gid))
    except ValueError:
        print(
            _('Warning: Primary group "{group}" of user is empty but not in Database. Try "groupdel {group}"'
              ).format(group=gr.gr_gid))
        exit(1)

    dbs.commit()
    dbs.close()
Example #3
0
def usermod(comment, home_dir, expiredate, inactive, gid, groups, append,
            login_new, lock, move_home, non_unique, password, shell, uid,
            unlock, config, login):
    conf = get_config(config)
    user = None
    try:
        user = pwd.getpwnam(login)
    except KeyError:
        print("Error: User not found")
        exit(1)

    if uid:
        try:
            if not non_unique and pwd.getpwuid(uid):
                print("Error: UID already taken")
                exit(1)
        except KeyError:
            pass

    if expiredate:
        expiredate = (expiredate - REFDATE).days
    if gid:
        gid = get_gid(gid)

    dbs = connect_db(conf)
    pm = UserManager(conf, dbs)

    if lock:
        if not config.has_section('fields'):
            section = config[config.default_section]
        else:
            section = config['fields']

        pw = pm.getuserbyuid(get_uid(login))[section.get(
            'password', 'password')]

        if pw[0] != '!':
            password = '******' + pw

    if unlock:
        if not config.has_section('fields'):
            section = config[config.default_section]
        else:
            section = config['fields']

        pw = pm.getuserbyuid(get_uid(login))[section.get(
            'password', 'password')]

        if pw[0] == '!':
            password = pw[1:]

    lastchg = None
    if password:
        lastchg = (datetime.date.today() - REFDATE).days

    pm.moduser(username_old=login,
               username=login_new,
               gid=gid,
               uid=uid,
               gecos=comment,
               homedir=home_dir,
               shell=shell,
               lstchg=lastchg,
               expire=expiredate,
               inact=inactive,
               password=password)

    if login_new:
        glm = GroupListManager(conf, dbs)
        glm.modallgroupuser(login, login_new)

    if groups:
        if login_new:
            login = login_new
        glm = GroupListManager(conf, dbs)
        if not append:
            glm.delallgroupuser(login)
            for group in groups:
                try:
                    glm.addgroupuser(login, get_gid(group))
                except KeyError:
                    print(
                        _("Warning: Can't find group {group}").format(
                            group=group))
        else:
            db_groups = glm.getgroupsforusername(login)
            for group in groups:
                gid = get_gid(group)
                if gid not in db_groups:
                    glm.addgroupuser(login, gid)

    if home_dir and move_home:
        try:
            shutil.move(str(user.pw_dir), home_dir)
        except PermissionError:
            print(_("Error: Insufficient permissions to move home dir."))
            dbs.rollback()
            dbs.close()
            exit(1)
    dbs.commit()
    dbs.close()
Example #4
0
def usermod(
    comment,
    home_dir,
    expiredate,
    inactive,
    gid,
    groups,
    append,
    login_new,
    lock,
    move_home,
    non_unique,
    password,
    shell,
    uid,
    unlock,
    config,
    login,
):
    conf = get_config(config)
    user = None
    try:
        user = pwd.getpwnam(login)
    except KeyError:
        print("Error: User not found")
        exit(1)

    if uid:
        try:
            if not non_unique and pwd.getpwuid(uid):
                print("Error: UID already taken")
                exit(1)
        except KeyError:
            pass

    if expiredate:
        expiredate = (expiredate - REFDATE).days
    if gid:
        gid = get_gid(gid)

    dbs = connect_db(conf)
    pm = UserManager(conf, dbs)

    if lock:
        if not config.has_section("fields"):
            section = config[config.default_section]
        else:
            section = config["fields"]

        pw = pm.getuserbyuid(get_uid(login))[section.get("password", "password")]

        if pw[0] != "!":
            password = "******" + pw

    if unlock:
        if not config.has_section("fields"):
            section = config[config.default_section]
        else:
            section = config["fields"]

        pw = pm.getuserbyuid(get_uid(login))[section.get("password", "password")]

        if pw[0] == "!":
            password = pw[1:]

    lastchg = None
    if password:
        lastchg = (datetime.date.today() - REFDATE).days

    pm.moduser(
        username_old=login,
        username=login_new,
        gid=gid,
        uid=uid,
        gecos=comment,
        homedir=home_dir,
        shell=shell,
        lstchg=lastchg,
        expire=expiredate,
        inact=inactive,
        password=password,
    )

    if login_new:
        glm = GroupListManager(conf, dbs)
        glm.modallgroupuser(login, login_new)

    if groups:
        if login_new:
            login = login_new
        glm = GroupListManager(conf, dbs)
        if not append:
            glm.delallgroupuser(login)
            for group in groups:
                try:
                    glm.addgroupuser(login, get_gid(group))
                except KeyError:
                    print(_("Warning: Can't find group {group}").format(group=group))
        else:
            db_groups = glm.getgroupsforusername(login)
            for group in groups:
                gid = get_gid(group)
                if gid not in db_groups:
                    glm.addgroupuser(login, gid)

    if home_dir and move_home:
        try:
            shutil.move(str(user.pw_dir), home_dir)
        except PermissionError:
            print(_("Error: Insufficient permissions to move home dir."))
            dbs.rollback()
            dbs.close()
            exit(1)
    dbs.commit()
    dbs.close()