Example #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()
Example #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()
Example #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()
Example #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()
Example #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()
Example #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()
Example #7
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()
Example #8
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()
Example #9
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()
Example #10
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()
Example #11
0
 def setUpClass(cls):
     ManagerTests.setUpClass()
     cls.gm = GroupManager(cls.config, cls.dbs)