From aaa23c9c5fa73118ee82fbdb8a20625366d6347f Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Thu, 26 Jul 2007 13:21:36 +0000 Subject: [PATCH] - use first free uid in range for system user accounts - new function for getting the first free uid in a range in getenttools git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/gnuviech.info/gnuviechadmin/trunk@237 a67ec6bc-e5d5-0310-a910-815c51eb3124 --- gnuviechadmin/backend/sysuser.py | 7 ++----- gnuviechadmin/util/getenttools.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) 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)) +