Viewing file: userGroupCheck.py (6.25 KB) -rwxr-xr-x Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
## userGroupCheck.py - code to make sure that the user/group input is valid ## Copyright (C) 2001-2005 Red Hat, Inc. ## Copyright (C) 2001-2003 Brent Fox <bfox@redhat.com> ## Copyright (C) 2004-2005 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)
maxusernamelength = libuser.UT_NAMESIZE - 1 maxgroupnamelength = libuser.UT_NAMESIZE - 1
MSG_EMPTY = 0 MSG_TOOLONG = 1 MSG_WHITESPACE = 2 MSG_DOLLARSIGN = 3 MSG_ILLEGAL_CHARS = 4 MSG_NUMBERS = 5
user_msgs = { MSG_EMPTY: _("Please enter a user name."), MSG_TOOLONG: _("The user name must not exceed %d characters."), MSG_WHITESPACE: _("The user name '%s' contains whitespace. Please do not include whitespace in the user name."), MSG_DOLLARSIGN: _("The user name '%s' contains a dollar sign which is not at the end. Please use dollar signs only at the end of user names to indicate Samba machine accounts."), MSG_ILLEGAL_CHARS: _("The user name '%s' contains an invalid character at position %d."), MSG_NUMBERS: _("Using all numbers as the user name can cause confusion about whether the user name or numerical user id is meant. Do you really want to use a numerical-only user name?") }
group_msgs = { MSG_EMPTY: _("Please enter a group name."), MSG_TOOLONG: _("The group name must not exceed %d characters."), MSG_WHITESPACE: _("The group name '%s' contains whitespace. Please do not include whitespace in the group name."), MSG_ILLEGAL_CHARS: _("The group name '%s' contains an invalid character at position %d."), MSG_NUMBERS: _("Using all numbers as the group name can cause confusion about whether the group name or numerical group id is meant. Do you really want to use a numerical-only group name?") }
def isUserGroupNameOk (type, name, widget): if type == 'user': msgs = user_msgs maxnamelength = maxusernamelength if type == 'group': msgs = group_msgs maxnamelength = maxgroupnamelength
if len (string.strip (name)) == 0: messageDialog.show_message_dialog (msgs[MSG_EMPTY]) widget.set_text ("") widget.grab_focus () return False if len(name) > maxnamelength: messageDialog.show_message_dialog (msgs[MSG_TOOLONG] % (maxusernamelength)) widget.set_text ("") widget.grab_focus () return False
alldigits = True for i, j in map (lambda x: (name[x], x), range (len (name))): if i == "_" or (i == "-" and j != 0) or (type == "user" and i == "$" and j != 0 and j == len (name) - 1): #specifically allow "-" (except at the beginning), "$" at the end and "_" alldigits = False continue
if i == "$": messageDialog.show_message_dialog (msgs[MSG_DOLLARSIGN] % (name)) widget.set_text ("") widget.grab_focus () return False
if i not in string.ascii_letters and i not in string.digits and i != '.': messageDialog.show_message_dialog (msgs[MSG_ILLEGAL_CHARS] % (name, j+1)) widget.set_text ("") widget.grab_focus () return False
if i not in string.digits: alldigits = False
if alldigits: yesno = messageDialog.show_message_dialog (msgs[MSG_NUMBERS]) widget.set_text ("") widget.grab_focus () if yesno == gtk.RESPONSE_YES: return True else: return False
return True
def isUsernameOk (str, widget): return isUserGroupNameOk ('user', str, widget)
def isGroupnameOk(str, widget): return isUserGroupNameOk ('group', str, widget)
def isPasswordOk(str, widget): for i in str: if i not in string.ascii_letters and i not in string.digits and i not in string.punctuation and i not in string.whitespace: messageDialog.show_message_dialog(_("The password contains invalid characters. " "Please use only ASCII characters.")) widget.set_text("") widget.grab_focus() return 0 return 1
def isNameOk(str, widget): try: dummy = str.decode ('utf-8') except UnicodeDecodeError: #have to check for whitespace for gecos, since whitespace is ok messageDialog.show_message_dialog(_("The name '%s' contains invalid characters. " "Please use only UTF-8 characters.") % str) widget.set_text("") widget.grab_focus() return 0
if string.find(str, ":") > 0: #have to check for colons since /etc/passwd is a colon delimited file messageDialog.show_message_dialog(_("The name '%s' contains a colon. " "Please do not use colons in the name.") % str) widget.set_text("") widget.grab_focus() return 0 return 1
def isHomedirOk(str, widget): if len(string.strip(str)) == 0: messageDialog.show_message_dialog(_("Please enter a home directory.")) widget.set_text("") widget.grab_focus() return 0
if string.find(str, ":") > 0: #have to check for colons since /etc/passwd is a colon delimited file messageDialog.show_message_dialog(_("The directory name '%s' contains a colon. " "Please do not use colons in the directory name.") % str) widget.set_text("") widget.grab_focus() return 0 return 1
|