Viewing file: userGroupFind.py (3.3 KB) -rwxr-xr-x Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
## userGroupIds.py - allocate new user and group ids according to prefs ## Copyright (C) 2006 Red Hat, Inc. ## Copyright (C) 2006 Nils Philippsen <nphilipp@redhat.com>
## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version.
## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
import string import libuser #import messageDialog
## ## I18N ## from rhpl.translate import _, N_ import rhpl.translate as translate domain = "system-config-users" translate.textdomain (domain)
high_uid_ignore = ['nfsnobody'] high_gid_ignore = ['nfsnobody']
class DuplicateUidNumberError (Exception): def __init__ (self, uidNumber): self.uidNumber = uidNumber
class DuplicateGidNumberError (Exception): def __init__ (self, gidNumber): self.gidNumber = gidNumber
def find_uid_gid (lu_admin, preferences, uidNumber = None, gidNumber = None): """ Try to find a free uid/gid number pair based on preferences, eventually with a given uid number. """ uidNumbers = map (lambda x: x[libuser.UIDNUMBER][0], filter (lambda x: x[libuser.USERNAME][0] not in high_uid_ignore, lu_admin.enumerateUsersFull ()))
if uidNumber == None: uidNumber = 500 if preferences['ASSIGN_HIGHEST_UID']: for uidNum in uidNumbers: if uidNum > uidNumber: uidNumber = uidNum + 1 while uidNumber in uidNumbers: uidNumber += 1 while gidNumber == None or uidNumber in uidNumbers: if uidNumber not in uidNumbers: try: gidNumber = find_gid (lu_admin, preferences, preferences['PREFER_SAME_UID_GID'] and uidNumber or None) except DuplicateGidNumberError: uidNumber += 1 else: uidNumber += 1 else: if uidNumber in uidNumbers: raise DuplicateUidNumberError (uidNumber) gidNumber = find_gid (lu_admin, preferences, gidNumber != None and gidNumber or (preferences['PREFER_SAME_UID_GID'] and uidNumber or None))
return (uidNumber, gidNumber)
def find_gid (lu_admin, preferences, gidNumber = None): """ Try to find a free gid number based on preferences, eventually with a given gid number. """ gidNumbers = map (lambda x: x[libuser.GIDNUMBER][0], filter (lambda x: x[libuser.GROUPNAME][0] not in high_gid_ignore, lu_admin.enumerateGroupsFull ())) if gidNumber == None: gidNumber = 500 if preferences['ASSIGN_HIGHEST_GID']: for gidNum in gidNumbers: if gidNum > gidNumber: gidNumber = gidNum + 1 while gidNumber in gidNumbers: gidNumber += 1 else: if gidNumber in gidNumbers: raise DuplicateGidNumberError (gidNumber)
return gidNumber
|