Debian Codegraph @

ipapython/ipavalidate.py

raw
1 # Authors: Rob Crittenden <[email protected]>
2 #
3 # Copyright (C) 2007 Red Hat
4 # see file 'COPYING' for use and warranty information
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 import re
21
22 def Email(mail, notEmpty=True):
23 """Do some basic validation of an e-mail address.
24 Return True if ok
25 Return False if not
26
27 If notEmpty is True the this will return an error if the field
28 is "" or None.
29 """
30 usernameRE = re.compile(r"^[^ \t\n\r@<>()]+$", re.I)
31 domainRE = re.compile(r"^[a-z0-9][a-z0-9\.\-_]*\.[a-z]+$", re.I)
32
33 if not mail or mail is None:
34 if notEmpty is True:
35 return False
36 else:
37 return True
38
39 mail = mail.strip()
40 s = mail.split('@', 1)
41 try:
42 username, domain=s
43 except ValueError:
44 return False
45 if not usernameRE.search(username):
46 return False
47 if not domainRE.search(domain):
48 return False
49
50 return True
51
52 def Plain(text, notEmpty=False, allowSpaces=True):
53 """Do some basic validation of a plain text field
54 Return True if ok
55 Return False if not
56
57 If notEmpty is True the this will return an error if the field
58 is "" or None.
59 """
60 if (text is None) or (not text.strip()):
61 if notEmpty is True:
62 return False
63 else:
64 return True
65
66 if allowSpaces:
67 textRE = re.compile(r"^[a-zA-Z_\-0-9\'\ ]*$")
68 else:
69 textRE = re.compile(r"^[a-zA-Z_\-0-9\']*$")
70 if not textRE.search(text):
71 return False
72
73 return True
74
75 def String(text, notEmpty=False):
76 """A string type. This is much looser in what it allows than plain"""
77
78 if text is None or not text.strip():
79 if notEmpty is True:
80 return False
81 else:
82 return True
83
84 return True
85
86 def Path(text, notEmpty=False):
87 """Do some basic validation of a path
88 Return True if ok
89 Return False if not
90
91 If notEmpty is True the this will return an error if the field
92 is "" or None.
93 """
94 textRE = re.compile(r"^[a-zA-Z_\-0-9\\ \.\/\\:]*$")
95
96 if not text and notEmpty is True:
97 return False
98
99 if text is None:
100 if notEmpty is True:
101 return False
102 else:
103 return True
104
105 if not textRE.search(text):
106 return False
107
108 return True
109
110 def GoodName(text, notEmpty=False):
111 """From shadow-utils:
112
113 User/group names must match gnu e-regex:
114 [a-zA-Z0-9_.][a-zA-Z0-9_.-]{0,30}[a-zA-Z0-9_.$-]?
115
116 as a non-POSIX, extension, allow "$" as the last char for
117 sake of Samba 3.x "add machine script"
118
119 Return True if ok
120 Return False if not
121 """
122 textRE = re.compile(r"^[a-zA-Z0-9_.][a-zA-Z0-9_.-]{0,30}[a-zA-Z0-9_.$-]?$")
123
124 if not text and notEmpty is True:
125 return False
126
127 if text is None:
128 if notEmpty is True:
129 return False
130 else:
131 return True
132
133 m = textRE.match(text)
134 if not m or text != m.group(0):
135 return False
136
137 return True