/
_user.py
451 lines (393 loc) · 16.9 KB
/
_user.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# -*- coding: utf-8 -*-
# rdiffweb, A web interface to rdiff-backup repositories
# Copyright (C) 2012-2021 rdiffweb contributors
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
import logging
import os
import secrets
import string
import cherrypy
from sqlalchemy import Column, Integer, SmallInteger, String, and_, event, inspect, or_
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import deferred, relationship, validates
from zxcvbn import zxcvbn
import rdiffweb.tools.db # noqa
from rdiffweb.core import authorizedkeys
from rdiffweb.core.passwd import check_password, hash_password
from rdiffweb.tools.i18n import ugettext as _
from ._repo import RepoObject
from ._session import SessionObject
from ._sshkey import SshKey
from ._token import Token
logger = logging.getLogger(__name__)
Base = cherrypy.tools.db.get_base()
SEP = b'/'
class DuplicateSSHKeyError(Exception):
"""
Raised by add_authorizedkey when trying to add the same SSH Key twice.
"""
pass
class UserObject(Base):
__tablename__ = 'users'
__table_args__ = {'sqlite_autoincrement': True}
# Value for role.
ADMIN_ROLE = 0
MAINTAINER_ROLE = 5
USER_ROLE = 10
ROLES = {
'admin': ADMIN_ROLE,
'maintainer': MAINTAINER_ROLE,
'user': USER_ROLE,
}
# Value for mfa field
DISABLED_MFA = 0
ENABLED_MFA = 1
# Regex pattern to be used for validation.
PATTERN_EMAIL = r"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"
PATTERN_FULLNAME = r"""[^!"#$%&()*+,./:;<=>?@[\]_{|}~]+$"""
PATTERN_USERNAME = r"[a-zA-Z0-9_.\-]+$"
userid = Column('UserID', Integer, primary_key=True)
username = Column('Username', String, nullable=False, unique=True)
hash_password = Column('Password', String, nullable=False, default="")
user_root = Column('UserRoot', String, nullable=False, default="")
_is_admin = deferred(
Column(
'IsAdmin',
SmallInteger,
nullable=False,
server_default="0",
doc="DEPRECATED This column is replaced by 'role'",
)
)
email = Column('UserEmail', String, nullable=False, default="")
restore_format = deferred(
Column(
'RestoreFormat',
SmallInteger,
nullable=False,
server_default="1",
doc="DEPRECATED This column is not used anymore",
)
)
role = Column('role', SmallInteger, nullable=False, server_default=str(USER_ROLE), default=USER_ROLE)
fullname = Column('fullname', String, nullable=False, default="")
mfa = Column('mfa', SmallInteger, nullable=False, default=DISABLED_MFA)
repo_objs = relationship(
'RepoObject',
foreign_keys='UserObject.userid',
primaryjoin='UserObject.userid == RepoObject.userid',
uselist=True,
lazy=True,
order_by=lambda: RepoObject.repopath,
)
@classmethod
def get_user(cls, user):
"""Return a user object."""
return UserObject.query.filter(UserObject.username == user).first()
@classmethod
def create_admin_user(cls, default_username, default_password):
# Check if admin user exists. If not, created it.
userobj = UserObject.get_user(default_username)
if not userobj:
userobj = cls.add_user(default_username, role=UserObject.ADMIN_ROLE, user_root='/backups')
userobj.hash_password = hash_password('admin123')
# Also make sure to update the password with latest value from config file.
if default_password:
if default_password.startswith('{SSHA}') or default_password.startswith('$argon2'):
userobj.hash_password = default_password
else:
userobj.hash_password = hash_password(default_password)
userobj.add()
return userobj
@classmethod
def add_user(cls, username, password=None, role=USER_ROLE, **attrs):
"""
Used to add a new user with an optional password.
"""
assert password is None or isinstance(password, str)
# Check if user already exists.
if UserObject.get_user(username):
raise ValueError(_("User %s already exists." % (username,)))
# Find a database where to add the user
logger.info("adding new user [%s]", username)
userobj = UserObject(
username=username,
hash_password=hash_password(password) if password else '',
role=role,
**attrs,
).add()
# Return user object
return userobj
def add_authorizedkey(self, key, comment=None):
"""
Add the given key to the user. Adding the key to his `authorized_keys`
file if it exists and adding it to database.
"""
# Parse and validate ssh key
assert key
key = authorizedkeys.check_publickey(key)
# Remove option, replace comments.
key = authorizedkeys.AuthorizedKey(
options=None, keytype=key.keytype, key=key.key, comment=comment or key.comment
)
# If a filename exists, use it by default.
filename = os.path.join(self.user_root, '.ssh', 'authorized_keys')
if os.path.isfile(filename):
with open(filename, mode="r+", encoding='utf-8') as fh:
if authorizedkeys.exists(fh, key):
raise DuplicateSSHKeyError(_("SSH key already exists"))
logger.info("add key [%s] to [%s] authorized_keys", key, self.username)
authorizedkeys.add(fh, key)
else:
# Also look in database.
logger.info("add key [%s] to [%s] database", key, self.username)
try:
SshKey(userid=self.userid, fingerprint=key.fingerprint, key=key.getvalue()).add().flush()
except IntegrityError:
raise DuplicateSSHKeyError(
_("Duplicate key. This key already exists or is associated to another user.")
)
cherrypy.engine.publish('user_attr_changed', self, {'authorizedkeys': True})
cherrypy.engine.publish('authorizedkey_added', self, fingerprint=key.fingerprint, comment=comment)
def add_access_token(self, name, expiration_time=None, length=16):
"""
Create a new access token. Return the un-encrypted value of the token.
"""
assert name
assert length >= 8
# Generate a random token
token = ''.join(secrets.choice(string.ascii_lowercase) for i in range(length))
# Store hash token
try:
Token(
userid=self.userid, name=name, hash_token=hash_password(token), expiration_time=expiration_time
).add().flush()
except IntegrityError:
raise ValueError(_("Duplicate token name: %s") % name)
cherrypy.engine.publish('access_token_added', self, name)
return token
def valid_user_root(self):
"""
Check if the current user_root is valid and readable
"""
try:
return os.access(self.user_root, os.F_OK) and os.path.isdir(self.user_root)
except Exception:
return False
def delete(self, *args, **kwargs):
cfg = cherrypy.tree.apps[''].cfg
if self.username == cfg.admin_user:
raise ValueError(_("can't delete admin user"))
# FIXME This should be deleted by cascade
SshKey.query.filter(SshKey.userid == self.userid).delete()
RepoObject.query.filter(RepoObject.userid == self.userid).delete()
Token.query.filter(Token.userid == self.userid).delete()
# Delete ourself
return Base.delete(self)
def delete_authorizedkey(self, fingerprint):
"""
Remove the given key from the user. Remove the key from his
`authorized_keys` file if it exists and from database database.
"""
# If a filename exists, use it by default.
filename = os.path.join(self.user_root, '.ssh', 'authorized_keys')
if os.path.isfile(filename):
with open(filename, mode='r+', encoding='utf-8') as fh:
logger.info("removing key [%s] from [%s] authorized_keys", fingerprint, self.username)
authorizedkeys.remove(fh, fingerprint)
else:
# Also look in database.
logger.info("removing key [%s] from [%s] database", fingerprint, self.username)
SshKey.query.filter(and_(SshKey.userid == self.userid, SshKey.fingerprint == fingerprint)).delete()
cherrypy.engine.publish('user_attr_changed', self, {'authorizedkeys': True})
def delete_access_token(self, name):
assert name
if not Token.query.filter(Token.userid == self.userid, Token.name == name).delete():
raise ValueError(_("token name doesn't exists: %s") % name)
@property
def disk_usage(self):
# Skip if user_root is invalid.
if not self.user_root or not os.path.exists(self.user_root):
return 0
values = cherrypy.engine.publish('get_disk_usage', self)
# Return the first not None value
return next((v for v in values if v is not None), 0)
@property
def disk_quota(self):
# Skip if user_root is invalid.
if not self.user_root or not os.path.exists(self.user_root):
return 0
values = cherrypy.engine.publish('get_disk_quota', self)
# Return the first not None value
return next((v for v in values if v is not None), 0)
@disk_quota.setter
def disk_quota(self, value):
# Skip if user_root is invalid.
if not self.user_root or not os.path.exists(self.user_root):
return
cherrypy.engine.publish('set_disk_quota', self, value)
@property
def authorizedkeys(self):
"""
Return an iterator on the authorized key. Either from his
`authorized_keys` file if it exists or from database.
"""
# If a filename exists, use it by default.
filename = os.path.join(self.user_root, '.ssh', 'authorized_keys')
if os.path.isfile(filename):
for k in authorizedkeys.read(filename):
yield k
# Also look in database.
for record in SshKey.query.filter(SshKey.userid == self.userid).all():
yield authorizedkeys.check_publickey(record.key)
def refresh_repos(self, delete=False):
"""
Return list of repositories object to reflect the filesystem folders.
Return a RepoObject for each sub directories under `user_root` with `rdiff-backup-data`.
"""
# Update the repositories by walking in the directory tree.
def _onerror(unused):
logger.error('error updating user [%s] repos' % self.username, exc_info=1)
# Get application config
cfg = cherrypy.tree.apps[''].cfg
dirty = False
records = RepoObject.query.filter(RepoObject.userid == self.userid).order_by(RepoObject.repopath).all()
user_root = os.fsencode(self.user_root)
for root, dirs, unused_files in os.walk(user_root, _onerror):
for name in dirs.copy():
if name.startswith(b'.'):
dirs.remove(name)
if b'rdiff-backup-data' in dirs:
repopath = os.path.relpath(root, start=user_root)
del dirs[:]
# Handle special scenario when the repo is the
# user_root
repopath = b'' if repopath == b'.' else repopath
# Check if repo path exists.
record_match = next((record for record in records if record.repopath == os.fsdecode(repopath)), None)
if not record_match:
# Add repository to database.
RepoObject(user=self, repopath=os.fsdecode(repopath)).add()
dirty = True
else:
records.remove(record_match)
if root.count(SEP) - user_root.count(SEP) >= cfg.max_depth:
del dirs[:]
# If enabled, remove entried from database
if delete:
for record in records:
RepoObject.query.filter(RepoObject.repoid == record.repoid).delete()
return dirty
@hybrid_property
def is_admin(self):
return self.role is not None and self.role <= self.ADMIN_ROLE
@hybrid_property
def is_ldap(self):
return self.hash_password is None or self.hash_password == ''
@is_ldap.expression
def is_ldap(cls):
return or_(cls.hash_password.is_(None), cls.hash_password == '')
@hybrid_property
def is_maintainer(self):
return self.role is not None and self.role <= self.MAINTAINER_ROLE
def set_password(self, password):
"""
Change the user's password. Raise a ValueError if the username or
the password are invalid.
"""
assert isinstance(password, str)
if not password:
raise ValueError("password can't be empty")
cfg = cherrypy.tree.apps[''].cfg
# Cannot update admin-password if defined
if self.username == cfg.admin_user and cfg.admin_password:
raise ValueError(_("can't update admin-password defined in configuration file"))
# Check password length
if cfg.password_min_length > len(password) or len(password) > cfg.password_max_length:
raise ValueError(
_('Password must have between %(min)d and %(max)d characters.')
% {'min': cfg.password_min_length, 'max': cfg.password_max_length}
)
# Verify password score using zxcvbn
stats = zxcvbn(password)
if stats.get('score') < cfg.password_score:
msg = _('Password too weak.')
warning = stats.get('feedback', {}).get('warning')
suggestions = stats.get('feedback', {}).get('suggestions')
if warning:
msg += ' ' + warning
if suggestions:
msg += ' ' + ' '.join(suggestions)
raise ValueError(msg)
# Store password
logger.info("updating user password [%s] and revoke sessions", self.username)
self.hash_password = hash_password(password)
# Revoke other session to force re-login
session_id = cherrypy.serving.session.id if hasattr(cherrypy.serving, 'session') else None
SessionObject.query.filter(
SessionObject.username == self.username,
SessionObject.id != session_id,
).delete()
def __eq__(self, other):
return type(self) == type(other) and inspect(self).key == inspect(other).key
@validates('username')
def validates_username(self, key, value):
if self.username:
raise ValueError('Username cannot be modified.')
return value
def validate_access_token(self, token):
"""
Check if the given token matches.
"""
for access_token in Token.query.all():
if access_token.is_expired:
continue
if check_password(token, access_token.hash_token):
# When it matches, return the record.
return access_token
return False
def validate_password(self, password):
return check_password(password, self.hash_password)
@event.listens_for(UserObject.hash_password, "set")
def hash_password_set(target, value, oldvalue, initiator):
if value and value != oldvalue:
cherrypy.engine.publish('user_password_changed', target)
@event.listens_for(UserObject, 'before_insert')
def user_before_insert(mapper, connection, target):
"""
Publish event when user is added
"""
cherrypy.engine.publish('user_added', target)
@event.listens_for(UserObject, 'after_delete')
def user_after_delete(mapper, connection, target):
"""
Publish event when user is deleted.
"""
cherrypy.engine.publish('user_deleted', target.username)
@event.listens_for(UserObject, 'after_update')
def user_attr_changed(mapper, connection, target):
changes = {}
state = inspect(target)
for attr in state.attrs:
if attr.key in ['user_root', 'email', 'role', 'mfa']:
hist = attr.load_history()
if hist.has_changes():
changes[attr.key] = (
hist.deleted[0] if len(hist.deleted) >= 1 else None,
hist.added[0] if len(hist.added) >= 1 else None,
)
if changes:
cherrypy.engine.publish('user_attr_changed', target, changes)