summaryrefslogtreecommitdiffstats
path: root/ipa-python
diff options
context:
space:
mode:
authorrcritten@redhat.com <rcritten@redhat.com>2007-09-21 10:24:36 -0400
committerrcritten@redhat.com <rcritten@redhat.com>2007-09-21 10:24:36 -0400
commit7b969737112c7a26711c3d4a9713ef1ca30f1be8 (patch)
tree27c9475aaa3951de88eabedfe2e1a125225fe2fe /ipa-python
parent919d037189cd3134d3eb4ba07b5ce131f018936f (diff)
downloadfreeipa-7b969737112c7a26711c3d4a9713ef1ca30f1be8.tar.gz
freeipa-7b969737112c7a26711c3d4a9713ef1ca30f1be8.tar.xz
freeipa-7b969737112c7a26711c3d4a9713ef1ca30f1be8.zip
Give ipa-adduser, ipa-addgroup and ipa-usermod an interactive mode
Add ipa-passwd tool Add simple field validation package This patch adds a package requirement, python-krbV. This is needed to determine the current user based on their kerberos ticket.
Diffstat (limited to 'ipa-python')
-rwxr-xr-xipa-python/freeipa-python.spec2
-rw-r--r--ipa-python/ipavalidate.py99
-rw-r--r--ipa-python/test/test_ipavalidate.py70
3 files changed, 170 insertions, 1 deletions
diff --git a/ipa-python/freeipa-python.spec b/ipa-python/freeipa-python.spec
index f2403582b..d7185d077 100755
--- a/ipa-python/freeipa-python.spec
+++ b/ipa-python/freeipa-python.spec
@@ -10,7 +10,7 @@ Source0: %{name}-%{version}.tgz
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
BuildArch: noarch
-Requires: python PyKerberos
+Requires: python PyKerberos python-krbV
%{!?python_sitelib: %define python_sitelib %(%{__python} -c "from distutils.sysconfig import get_python_lib; print get_python_lib()")}
diff --git a/ipa-python/ipavalidate.py b/ipa-python/ipavalidate.py
new file mode 100644
index 000000000..aa09f5836
--- /dev/null
+++ b/ipa-python/ipavalidate.py
@@ -0,0 +1,99 @@
+#! /usr/bin/python -E
+# Authors: Rob Crittenden <rcritten@redhat.com>
+#
+# Copyright (C) 2007 Red Hat
+# see file 'COPYING' for use and warranty information
+#
+# 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; version 2 or later
+#
+# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+
+import re
+
+def email(mail, notEmpty=True):
+ """Do some basic validation of an e-mail address.
+ Return 0 if ok
+ Return 1 if not
+
+ If notEmpty is True the this will return an error if the field
+ is "" or None.
+ """
+ usernameRE = re.compile(r"^[^ \t\n\r@<>()]+$", re.I)
+ domainRE = re.compile(r"^[a-z0-9][a-z0-9\.\-_]*\.[a-z]+$", re.I)
+
+ if not mail or mail is None:
+ if notEmpty is True:
+ return 1
+ else:
+ return 0
+
+ mail = mail.strip()
+ s = mail.split('@', 1)
+ try:
+ username, domain=s
+ except ValueError:
+ return 1
+ if not usernameRE.search(username):
+ return 1
+ if not domainRE.search(domain):
+ return 1
+
+ return 0
+
+def plain(text, notEmpty=False):
+ """Do some basic validation of a plain text field
+ Return 0 if ok
+ Return 1 if not
+
+ If notEmpty is True the this will return an error if the field
+ is "" or None.
+ """
+ textRE = re.compile(r"^[a-zA-Z_\-0-9\'\ ]*$")
+
+ if not text and notEmpty is True:
+ return 1
+
+ if text is None:
+ if notEmpty is True:
+ return 1
+ else:
+ return 0
+
+ if not textRE.search(text):
+ return 1
+
+ return 0
+
+def path(text, notEmpty=False):
+ """Do some basic validation of a path
+ Return 0 if ok
+ Return 1 if not
+
+ If notEmpty is True the this will return an error if the field
+ is "" or None.
+ """
+ textRE = re.compile(r"^[a-zA-Z_\-0-9\\ \.\/\\:]*$")
+
+ if not text and notEmpty is True:
+ return 1
+
+ if text is None:
+ if notEmpty is True:
+ return 1
+ else:
+ return 0
+
+ if not textRE.search(text):
+ return 1
+
+ return 0
diff --git a/ipa-python/test/test_ipavalidate.py b/ipa-python/test/test_ipavalidate.py
new file mode 100644
index 000000000..507f7e022
--- /dev/null
+++ b/ipa-python/test/test_ipavalidate.py
@@ -0,0 +1,70 @@
+#! /usr/bin/python -E
+#
+# Copyright (C) 2007 Red Hat
+# see file 'COPYING' for use and warranty information
+#
+# 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; version 2 or later
+#
+# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+
+import sys
+sys.path.insert(0, ".")
+
+import unittest
+
+import ipavalidate
+
+class TestValidate(unittest.TestCase):
+ def setUp(self):
+ pass
+
+ def tearDown(self):
+ pass
+
+ def test_validemail(self):
+ self.assertEqual(0, ipavalidate.email("test@freeipa.org"))
+ self.assertEqual(0, ipavalidate.email("", notEmpty=False))
+
+ def test_invalidemail(self):
+ self.assertEqual(1, ipavalidate.email("test"))
+ self.assertEqual(1, ipavalidate.email("test@freeipa"))
+ self.assertEqual(1, ipavalidate.email("test@.com"))
+ self.assertEqual(1, ipavalidate.email(""))
+ self.assertEqual(1, ipavalidate.email(None))
+
+ def test_validplain(self):
+ self.assertEqual(0, ipavalidate.plain("Joe User"))
+ self.assertEqual(0, ipavalidate.plain("Joe O'Malley"))
+ self.assertEqual(0, ipavalidate.plain("", notEmpty=False))
+ self.assertEqual(0, ipavalidate.plain(None, notEmpty=False))
+
+ def test_invalidplain(self):
+ self.assertEqual(1, ipavalidate.plain("Joe (User)"))
+ self.assertEqual(1, ipavalidate.plain("", notEmpty=True))
+ self.assertEqual(1, ipavalidate.plain(None, notEmpty=True))
+
+ def test_validpath(self):
+ self.assertEqual(0, ipavalidate.path("/"))
+ self.assertEqual(0, ipavalidate.path("/home/user"))
+ self.assertEqual(0, ipavalidate.path("../home/user"))
+ self.assertEqual(0, ipavalidate.path("", notEmpty=False))
+ self.assertEqual(0, ipavalidate.path(None, notEmpty=False))
+
+ def test_invalidpath(self):
+ self.assertEqual(1, ipavalidate.path("(foo)"))
+ self.assertEqual(1, ipavalidate.path("", notEmpty=True))
+ self.assertEqual(1, ipavalidate.path(None, notEmpty=True))
+
+if __name__ == '__main__':
+ unittest.main()
+