diff --git a/gnuviechadmin/backend/sysuser.py b/gnuviechadmin/backend/sysuser.py index 6c1c4e4..a5f3579 100644 --- a/gnuviechadmin/backend/sysuser.py +++ b/gnuviechadmin/backend/sysuser.py @@ -74,11 +74,8 @@ class SysuserEntity(BackendEntity): return config.get('sysuser', 'shellno') def _get_next_sysuid(self): - uid = int(config.get('sysuser', 'minuid')) - muid = getenttools.get_max_uid(int(config.get('sysuser', 'maxuid'))) - if muid >= uid: - uid = muid + 1 - return uid + return getenttools.get_next_uid(int(config.get('sysuser', 'minuid')), + int(config.get('sysuser', 'maxuid'))) def _populate_home(self): templatedir = get_template_dir(config.get('sysuser', 'hometemplate')) diff --git a/gnuviechadmin/util/getenttools.py b/gnuviechadmin/util/getenttools.py index ffa068e..56b63e1 100644 --- a/gnuviechadmin/util/getenttools.py +++ b/gnuviechadmin/util/getenttools.py @@ -77,6 +77,16 @@ def get_group_by_id(gid): return groups[0] return None +def get_next_uid(lowerboundary = 10000, upperboundary = 65536): + """Gets the first available user id in the range between lowerboundary and + upper boundary.""" + for uid in range(lowerboundary, upperboundary): + try: + user = pwd.getpwuid(uid) + except KeyError: + return uid + raise Exception("no free uid found in range %d to %d", lowerboundary, upperboundary) + def get_max_uid(boundary = 65536): """Gets the highest uid value.""" return max([user.uid for user in parse_users() if user.uid <= boundary]) @@ -91,3 +101,5 @@ if __name__ == "__main__": print "Max GID is %d" % (get_max_gid(40000)) print "User with max UID is %s" % (get_user_by_id(get_max_uid(40000))) print "Group with max GID is %s" % (get_group_by_id(get_max_gid(40000))) + print "First free UID is %s" % (get_next_uid(10000, 40000)) +