diff --git a/qubes/vm/qubesvm.py b/qubes/vm/qubesvm.py index bae91936..8a5ae3f9 100644 --- a/qubes/vm/qubesvm.py +++ b/qubes/vm/qubesvm.py @@ -75,20 +75,35 @@ def _setter_qid(self, prop, value): return value -def _setter_name(self, prop, value): - ''' Helper for setting the domain name ''' +def validate_name(holder, prop, value): + ''' Check if value is syntactically correct VM name ''' if not isinstance(value, str): - raise TypeError('{} value must be string, {!r} found'.format( - prop.__name__, type(value).__name__)) + raise TypeError('VM name must be string, {!r} found'.format( + type(value).__name__)) if len(value) > 31: - raise ValueError('{} value must be shorter than 32 characters'.format( - prop.__name__)) + if holder is not None and prop is not None: + raise qubes.exc.QubesPropertyValueError(holder, prop, value, + '{} value must be shorter than 32 characters'.format( + prop.__name__)) + else: + raise qubes.exc.QubesValueError( + 'VM name must be shorter than 32 characters') # this regexp does not contain '+'; if it had it, we should specifically # disallow 'lost+found' #1440 if re.match(r"^[a-zA-Z][a-zA-Z0-9_-]*$", value) is None: - raise ValueError('{} value contains illegal characters'.format( - prop.__name__)) + if holder is not None and prop is not None: + raise qubes.exc.QubesPropertyValueError(holder, prop, value, + '{} value contains illegal characters'.format(prop.__name__)) + else: + raise qubes.exc.QubesValueError( + 'VM name contains illegal characters') + + +def _setter_name(self, prop, value): + ''' Helper for setting the domain name ''' + validate_name(self, prop, value) + if self.is_running(): raise qubes.exc.QubesVMNotHaltedError( self, 'Cannot change name of running VM') @@ -101,7 +116,7 @@ def _setter_name(self, prop, value): pass if value in self.app.domains: - raise qubes.exc.QubesValueError( + raise qubes.exc.QubesPropertyValueError(self, prop, value, 'VM named {} alread exists'.format(value)) return value