-
Notifications
You must be signed in to change notification settings - Fork 33
/
validators.py
403 lines (376 loc) · 7.52 KB
/
validators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import difflib
import pwd
import string
import sys
import cracklib
import ocflib.misc.mail
RESERVED_USERNAMES = frozenset((
# Misc
'Debian-exim',
'Debian-gdm',
'_graphite',
'about',
'account',
'accounts',
'admin',
'administrator',
'apphost',
'applicationhost',
'approve',
'apt-dater',
'archive',
'arpwatch',
'atool',
'audit',
'avahi',
'backup',
'bin',
'bind',
'bjb',
'board',
'boinc',
'bullseye',
'buster',
'buy',
'buysheet',
'cabinet',
'callinkapi',
'ceph',
'chronos',
'clamav',
'colord',
'contact',
'control',
'create',
'cricket',
'csgo',
'daemon',
'datathon',
'davfs2',
'dca',
'debian-security-support',
'debian-spamd',
'debian-transmission',
'debmirror',
'decalform',
'deforestation',
'directors',
'dns',
'dnsmasq',
'docs',
'donations',
'dovecot',
'dump',
'dumper',
'elasticsearch',
'email',
'epidemic',
'facebook',
'families',
'faq',
'ftp',
'games',
'geoclue',
'getinvolved',
'gettinginvolved',
'gh',
'github',
'gitlab-runner',
'gnats',
'groups',
'guest',
'hello',
'hiring',
'host',
'hosting',
'hours',
'hpc',
'hplip',
'https',
'icinga',
'info',
'infra',
'internal',
'internals',
'iodine',
'irc',
'jabber',
'jenkins-deploy',
'jenkins-slave',
'job',
'join',
'joinfamily',
'keyserver',
'lab',
'lets-encrypt',
'libuuid',
'libvirt-qemu',
'lightdm',
'list',
'logjam',
'lp',
'mail',
'mailinx',
'mailman',
'man',
'manager',
'marketing',
'mastodon',
'memcache',
'mesos',
'messagebus',
'minecraft',
'minutes',
'mis',
'mlk',
'mongodb',
'move',
'mumble-server',
'mysql',
'nagios',
'nessus',
'netsplit',
'news',
'nginx',
'nobody',
'noc',
'nogroup',
'nomail',
'nonexist',
'nonexistent',
'noreply',
'nslcd',
'ntp',
'oident',
'opencf',
'opencomp',
'openldap',
'opersquad',
'pagefault',
'paper',
'papercut',
'password',
'pgp',
'polw',
'postfix',
'postgres',
'postgrey',
'print',
'printer',
'printers',
'printerlog',
'printing',
'procmail',
'production',
'prometheus',
'prosody',
'proxy',
'pulse',
'rabbitmq',
'redis',
'register',
'requesttracker',
'reserve',
'rtkit',
'sales',
'saned',
'sdocs',
'secretary',
'senate-resolution',
'servers',
'sexy',
'shellinabox',
'shirts',
'shorturl',
'signin',
'sks',
'slack',
'snmp',
'socialrules',
'spamass-milter',
'spamd',
'srcds',
'ssh',
'sshd',
'ssl',
'ssladmin',
'ssladministrator',
'sslwebmaster',
'staffhours',
'startertasks',
'statd',
'stats',
'status',
'steam',
'stf-cost-breakdown',
'stretch',
'support',
'survey',
'sync',
'sys',
'sysadmin',
'systemd',
'systemd-bus-proxy',
'systemd-network',
'systemd-resolve',
'systemd-timesync',
'test',
'testsmcc',
'tftp',
'treasurer',
'tw',
'twitter',
'university',
'unscd',
'usbmux',
'usenet',
'user',
'uucp',
'uuidd',
'vde2-net',
'vhost',
'vnc',
'web',
'webhost',
'webhosting',
'webmaster',
'wiki',
'wordpress',
'www',
'www-data',
'xkcd',
'yacy',
'youtube',
'zabbix',
'zandronum',
'znc',
'zookeeper',
# RT queues
'bod',
'beauracracy',
'beaurocracy',
'bureacrucy',
'bureaucracy',
'devnull',
'help',
'hostmaster',
'lending',
'operations',
'projects',
'security',
'techtalks',
'todo',
# Mailing lists, aliases, and legacy mailing lists
'abuse',
'alums',
'alumnus',
'announce',
'decal',
'decal-announce',
'extcomm',
'gm',
'jenkins',
'joinstaff',
'mirrors',
'manager',
'managers',
'mon',
'munin',
'officers',
'opstaff',
'pimp',
'postmaster',
'projects',
'puppet',
'rancid',
'root',
'rt',
'rt-ops',
'sks',
'sm',
'staff',
'wheel',
# Usernames of some former staffers whose accounts were later lost
'adamj', # Adam Richter (former account, still has User_Info)
'anniem', # Ann Matsubara
'appel', # Shannon Appel
'blojo', # Jon Blow
'chamm',
'chaynges', # Cynthia Haynes
'cjain', # Chris Jain
'dpassage', # David Paschich
'euphrasi',
'evil',
'glass', # Adam Glass
'ianb',
'karat', # Eddy Karat
'kinshuk', # Kinshuk Govil
'kit',
'marko', # Mark Nolte
'moray',
'nweaver', # Nicholas Weaver
'pbrown',
'reiser', # Hans Reiser
'rgm', # Rob Menke
'shannona', # Shannon Appel (former account, still has User_Info)
'steveg',
'welch', # Sean Welch
'yukai',
))
def validate_username(username, check_exists=False):
"""Validate a username, raising a descriptive exception if problems are
encountered."""
if username_reserved(username):
raise ValueError('Username is reserved.')
if not 3 <= len(username) <= 16:
raise ValueError('Username must be between 3 and 16 characters.')
if not all(c.islower() for c in username):
raise ValueError('Username must be all lowercase letters.')
if check_exists and not user_exists(username):
raise ValueError('Username does not exist.')
def validate_password(username, password, strength_check=True):
"""Validate a password, raising a descriptive exception if problems are
encountered. Optionally checks password strength."""
if strength_check:
if len(password) < 8:
raise ValueError('Password must be at least 8 characters.')
s = difflib.SequenceMatcher()
s.set_seqs(password, username)
if s.ratio() > 0.6:
raise ValueError('Password is too similar to username.')
try:
cracklib.VeryFascistCheck(password)
except ValueError as e:
raise ValueError('Password problem: {}.'.format(e))
# sanity check; note we don't use string.whitespace since we don't want
# tabs or newlines
allowed = string.digits + string.ascii_letters + string.punctuation + ' '
if not all(c in allowed for c in password):
raise ValueError('Password contains forbidden characters.')
# TODO: we have two implementations of this (one here, one in search).
# one should be removed.
def user_exists(username):
try:
pwd.getpwnam(username)
except KeyError:
return False
else:
return True
def username_reserved(username):
if username.startswith('ocf'):
return True
if username in RESERVED_USERNAMES:
return True
# sanity check: make sure no local users share the username
with open('/etc/passwd') as f:
if any(line.startswith(username + ':') for line in f):
print(
'WARNING: Username {} rejected based on /etc/passwd!'
.format(username),
file=sys.stderr)
ocflib.misc.mail.send_problem_report(
"""Username {} rejected based on /etc/passwd. It should be \
added to RESERVED_USERNAMES for consistency across \
servers!""".format(username))
return True
return False