Пример #1
0
def groupadd(force, gid, key, non_unique, password, system, config, group):
    if not gid or force:
        gid = find_new_gid(sysuser=system)
    else:
        try:
            if not non_unique and grp.getgrgid(gid):
                print("Error: GID already taken")
                exit(1)
        except KeyError:
            pass

    try:
        if grp.getgrnam(group):
            if force:
                exit(0)
            print("Error: Group name already taken")
            exit(1)
    except KeyError:
        pass

    conf = get_config(config)
    defs = get_defs()

    for k, v in key:
        defs[k] = v
    dbs = connect_db(conf)

    gm = GroupManager(conf, dbs)
    gm.addgroup(group, gid, password)

    dbs.commit()
    dbs.close()
Пример #2
0
def groupmod(gid, config, new_name, non_unique, password, group):
    try:
        gr = grp.getgrnam(group)
    except KeyError:
        print(_("Error: Group not found"))
        exit(1)
        return

    conf = get_config(config)
    dbs = connect_db(conf)

    if gid:
        try:
            if not non_unique and grp.getgrgid(gid):
                print("Error: GID already taken")
                exit(1)
        except KeyError:
            pass
        old_gid = int(gr.gr_gid)

        glm = GroupListManager(conf, dbs)
        glm.modallgroupgid(old_gid, gid)

        um = UserManager(conf, dbs)
        um.modallgid(old_gid, gid)

    gm = GroupManager(conf, dbs)
    gm.modgroup(name_old=group, name=new_name, gid=gid, password=password)

    dbs.commit()
    dbs.close()
Пример #3
0
def groupmod(gid, config, new_name, non_unique, password, group):
    try:
        gr = grp.getgrnam(group)
    except KeyError:
        print(_("Error: Group not found"))
        exit(1)
        return

    conf = get_config(config)
    dbs = connect_db(conf)

    if gid:
        try:
            if not non_unique and grp.getgrgid(gid):
                print("Error: GID already taken")
                exit(1)
        except KeyError:
            pass
        old_gid = int(gr.gr_gid)

        glm = GroupListManager(conf, dbs)
        glm.modallgroupgid(old_gid, gid)

        um = UserManager(conf, dbs)
        um.modallgid(old_gid, gid)

    gm = GroupManager(conf, dbs)
    gm.modgroup(name_old=group, name=new_name, gid=gid, password=password)

    dbs.commit()
    dbs.close()
Пример #4
0
def groupadd(force, gid, key, non_unique, password, system, config, group):
    if not gid or force:
        gid = find_new_gid(sysuser=system)
    else:
        try:
            if not non_unique and grp.getgrgid(gid):
                print("Error: GID already taken")
                exit(1)
        except KeyError:
            pass

    try:
        if grp.getgrnam(group):
            if force:
                exit(0)
            print("Error: Group name already taken")
            exit(1)
    except KeyError:
        pass

    conf = get_config(config)
    defs = get_defs()

    for k, v in key:
        defs[k] = v
    dbs = connect_db(conf)

    gm = GroupManager(conf, dbs)
    gm.addgroup(group, gid, password)

    dbs.commit()
    dbs.close()
Пример #5
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()
Пример #6
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()
Пример #7
0
def importusers(ignore_password, config, lower, upper):
    conf = get_config(config)
    users = {}

    with open("/etc/passwd") as passwd:
        for line in passwd:
            line = line.strip()
            u = line.split(":")
            if lower <= int(u[2]) <= upper:
                for i in range(len(u)):
                    if not u[i].strip():
                        u[i] = None
                users[u[0]] = u

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

    with open("/etc/shadow") as shadow:
        for line in shadow:
            line.strip()
            s = line.split(":")

            if s[0] in users.keys():
                for i in range(len(s)):
                    if not s[i].strip():
                        s[i] = None
                u = users[s[0]]
                if ignore_password:
                    s[1] = "!"

                um.adduser(
                    u[0],
                    uid=u[2],
                    gid=u[3],
                    gecos=u[4],
                    homedir=u[5],
                    shell=u[6],
                    password=s[1],
                    lstchg=s[2],
                    mini=s[3],
                    maxi=s[4],
                    warn=s[5],
                    inact=s[6],
                    expire=s[7],
                    flag=s[8],
                )
    dbs.commit()
    dbs.close()
Пример #8
0
def importusers(ignore_password, config, lower, upper):
    conf = get_config(config)
    users = {}

    with open('/etc/passwd') as passwd:
        for line in passwd:
            line = line.strip()
            u = line.split(':')
            if lower <= int(u[2]) <= upper:
                for i in range(len(u)):
                    if not u[i].strip():
                        u[i] = None
                users[u[0]] = u

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

    with open('/etc/shadow') as shadow:
        for line in shadow:
            line.strip()
            s = line.split(':')

            if s[0] in list(users.keys()):
                for i in range(len(s)):
                    if not s[i].strip():
                        s[i] = None
                u = users[s[0]]
                if ignore_password:
                    s[1] = '!'

                um.adduser(u[0],
                           uid=u[2],
                           gid=u[3],
                           gecos=u[4],
                           homedir=u[5],
                           shell=u[6],
                           password=s[1],
                           lstchg=s[2],
                           mini=s[3],
                           maxi=s[4],
                           warn=s[5],
                           inact=s[6],
                           expire=s[7],
                           flag=s[8])
    dbs.commit()
    dbs.close()
Пример #9
0
def groupdel(config, group):
    try:
        gr = grp.getgrnam(group)
    except KeyError:
        print("Error: Group not found")
        exit(1)
        return

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

    try:
        gm.delgroup(gid=str(gr.gr_gid))
    except KeyError as e:
        print("Error: %s" % e)
        exit(1)

    dbs.commit()
    dbs.close()
Пример #10
0
def groupdel(config, group):
    try:
        gr = grp.getgrnam(group)
    except KeyError:
        print("Error: Group not found")
        exit(1)
        return

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

    try:
        gm.delgroup(gid=str(gr.gr_gid))
    except KeyError as e:
        print("Error: %s" % e)
        exit(1)

    dbs.commit()
    dbs.close()
Пример #11
0
def importgroups(ignore_password, config, lower, upper):
    conf = get_config(config)
    groups = {}

    with open('/etc/group') as group:
        for line in group:
            line = line.strip()
            g = line.split(':')
            if lower <= int(g[2]) <= upper:
                for i in range(len(g)):
                    if not g[i].strip():
                        g[i] = None
                groups[g[0]] = g

    dbs = connect_db(conf)
    gm = GroupManager(conf, dbs)
    glm = GroupListManager(conf, dbs)

    with open('/etc/gshadow') as gshadow:
        for line in gshadow:
            line.strip()
            gs = line.split(':')

            if gs[0] in list(groups.keys()):
                for i in range(len(gs)):
                    if not gs[i].strip():
                        gs[i] = None
                g = groups[gs[0]]
                if ignore_password:
                    gs[1] = '!'
                gm.addgroup(g[0], gid=g[2], password=gs[1])
                if g[3]:
                    for user in g[3].split(','):
                        glm.addgroupuser(username=user, gid=g[2])
    dbs.commit()
    dbs.close()
Пример #12
0
def importgroups(ignore_password, config, lower, upper):
    conf = get_config(config)
    groups = {}

    with open("/etc/group") as group:
        for line in group:
            line = line.strip()
            g = line.split(":")
            if lower <= int(g[2]) <= upper:
                for i in range(len(g)):
                    if not g[i].strip():
                        g[i] = None
                groups[g[0]] = g

    dbs = connect_db(conf)
    gm = GroupManager(conf, dbs)
    glm = GroupListManager(conf, dbs)

    with open("/etc/gshadow") as gshadow:
        for line in gshadow:
            line.strip()
            gs = line.split(":")

            if gs[0] in groups.keys():
                for i in range(len(gs)):
                    if not gs[i].strip():
                        gs[i] = None
                g = groups[gs[0]]
                if ignore_password:
                    gs[1] = "!"
                gm.addgroup(g[0], gid=g[2], password=gs[1])
                if g[3]:
                    for user in g[3].split(","):
                        glm.addgroupuser(username=user, gid=g[2])
    dbs.commit()
    dbs.close()
Пример #13
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()
Пример #14
0
def useradd(ctx, basedir, comment, home_dir, expiredate, inactive, gid, groups,
            skel, key, no_create_home, no_user_group, non_unique, password,
            system, shell, uid, config, login):
    conf = get_config(config)
    defs = get_defs()
    useradd_conf = get_useradd_conf()

    for k, v in key:
        defs[k] = v

    if not uid:
        uid = find_new_uid(sysuser=system)
    else:
        try:
            if not non_unique and pwd.getpwuid(uid):
                print(_("Error: UID already taken"))
                exit(1)
        except KeyError:
            pass

    try:
        if not non_unique and pwd.getpwnam(login):
            print(_("Error: Login name already taken"))
            exit(1)
    except KeyError:
        pass

    if not shell:
        shell = useradd_conf.get('SHELL', '')

    if not basedir:
        basedir = useradd_conf.get('HOME', '/home')

    if not home_dir:
        home_dir = os.path.join(basedir, login)

    if not gid:
        try:
            gr = grp.getgrnam(login)
            if gr:
                gid = int(gr.gr_gid)
                no_user_group = True

        except KeyError:
            gid = find_new_gid(sysuser=system, preferred_gid=uid)
    else:
        gid = get_gid(gid)

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

    if not no_create_home:
        if not skel:
            skel = useradd_conf.get('SKEL', '/etc/skel')
        try:
            create_home(home_dir, skel, uid, gid)
        except PermissionError:
            print(_("Error: Insufficient permissions to create home dir"))
            exit(1)
        except FileExistsError:
            print(_('Error: Directory "%s" already exists') % home_dir)
            exit(1)

    lastchg = datetime.date.today() - REFDATE

    dbs = connect_db(conf)

    pm = UserManager(conf, dbs)
    pm.adduser(username=login,
               gid=gid,
               uid=uid,
               gecos=comment,
               homedir=home_dir,
               shell=shell,
               lstchg=lastchg.days,
               mini=defs.get('PASS_MIN_DAYS', 0),
               maxi=defs.get('PASS_MAX_DAYS', 99999),
               warn=defs.get('PASS_WARN_DAYS', 7),
               expire=expiredate,
               inact=inactive,
               password=password)

    if groups:
        glm = GroupListManager(conf, dbs)
        for g in groups:
            try:
                glm.addgroupuser(login, get_gid(g))
            except KeyError:
                print(_("Warning: Can't find group {group}").format(group=g))

    dbs.commit()
    dbs.close()

    if not no_user_group:
        ctx.invoke(groupadd,
                   group=login,
                   gid=gid,
                   system=system,
                   config=config,
                   non_unique=non_unique)
Пример #15
0
def useradd(
    ctx,
    basedir,
    comment,
    home_dir,
    expiredate,
    inactive,
    gid,
    groups,
    skel,
    key,
    no_create_home,
    no_user_group,
    non_unique,
    password,
    system,
    shell,
    uid,
    config,
    login,
):
    conf = get_config(config)
    defs = get_defs()
    useradd_conf = get_useradd_conf()

    for k, v in key:
        defs[k] = v

    if not uid:
        uid = find_new_uid(sysuser=system)
    else:
        try:
            if not non_unique and pwd.getpwuid(uid):
                print(_("Error: UID already taken"))
                exit(1)
        except KeyError:
            pass

    try:
        if not non_unique and pwd.getpwnam(login):
            print(_("Error: Login name already taken"))
            exit(1)
    except KeyError:
        pass

    if not shell:
        shell = useradd_conf.get("SHELL", "")

    if not basedir:
        basedir = useradd_conf.get("HOME", "/home")

    if not home_dir:
        home_dir = os.path.join(basedir, login)

    if not gid:
        try:
            gr = grp.getgrnam(login)
            if gr:
                gid = int(gr.gr_gid)
                no_user_group = True

        except KeyError:
            gid = find_new_gid(sysuser=system, preferred_gid=uid)
    else:
        gid = get_gid(gid)

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

    if not no_create_home:
        if not skel:
            skel = useradd_conf.get("SKEL", "/etc/skel")
        try:
            create_home(home_dir, skel, uid, gid)
        except PermissionError:
            print(_("Error: Insufficient permissions to create home dir"))
            exit(1)
        except FileExistsError:
            print(_('Error: Directory "%s" already exists') % home_dir)
            exit(1)

    lastchg = datetime.date.today() - REFDATE

    dbs = connect_db(conf)

    pm = UserManager(conf, dbs)
    pm.adduser(
        username=login,
        gid=gid,
        uid=uid,
        gecos=comment,
        homedir=home_dir,
        shell=shell,
        lstchg=lastchg.days,
        mini=defs.get("PASS_MIN_DAYS", 0),
        maxi=defs.get("PASS_MAX_DAYS", 99999),
        warn=defs.get("PASS_WARN_DAYS", 7),
        expire=expiredate,
        inact=inactive,
        password=password,
    )

    if groups:
        glm = GroupListManager(conf, dbs)
        for g in groups:
            try:
                glm.addgroupuser(login, get_gid(g))
            except KeyError:
                print(_("Warning: Can't find group {group}").format(group=g))

    dbs.commit()
    dbs.close()

    if not no_user_group:
        ctx.invoke(groupadd, group=login, gid=gid, system=system, config=config, non_unique=non_unique)
Пример #16
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()