Coverage for pyanaconda/pwpolicy.py: 0%

# Coverage for pyanaconda/pwpolicy.py : 0%

## 87 statements 0 run 87 missing 0 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

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 | # # Brian C. Lane <bcl@redhat.com> # # Copyright 2015 Red Hat, Inc. # # This copyrighted material is made available to anyone wishing to use, modify, # copy, or redistribute it subject to the terms and conditions of the GNU # General Public License v.2. This program is distributed in the hope that it # will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the # implied warranties 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., 51 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat # trademarks that are incorporated in the source code or documentation are not # subject to the GNU General Public License and may only be used or replicated # with the express permission of Red Hat, Inc. # from pykickstart.base import BaseData, KickstartCommand from pykickstart.errors import KickstartParseError from pykickstart.options import KSOptionParser from pykickstart.version import F22 import warnings from pyanaconda.core.i18n import _ class F22_PwPolicyData(BaseData): """ Kickstart Data object to hold information about pwpolicy. """ removedKeywords = BaseData.removedKeywords removedAttrs = BaseData.removedAttrs def __init__(self, *args, **kwargs): BaseData.__init__(self, *args, **kwargs) self.name = kwargs.get("name", "") self.minlen = kwargs.get("minlen", 6) self.minquality = kwargs.get("minquality", 1) self.strict = kwargs.get("strict", False) self.changesok = kwargs.get("changesok", False) self.emptyok = kwargs.get("emptyok", True) # The defaults specified above are used only for password input via the UI # during a partial kickstart installations. # Fully interactive installs (no kickstart is specified by the user) # use the default set by the interactive defaults built-in kickstart file # (data/interactive-defaults.ks). # Automated kickstart installs simply ignore the password policy as the policy # only applies to the UI, not for passwords specified in kickstart. def __eq__(self, y): if not y: return False return self.name == y.name def __ne__(self, y): return not self == y def __str__(self): retval = BaseData.__str__(self) if self.name != "": retval += "pwpolicy" retval += self._getArgsAsStr() + "\n" return retval def _getArgsAsStr(self): retval = "" retval += " %s" % self.name retval += " --minlen=%d" % self.minlen retval += " --minquality=%d" % self.minquality if self.strict: retval += " --strict" else: retval += " --notstrict" if self.changesok: retval += " --changesok" else: retval += " --nochanges" if self.emptyok: retval += " --emptyok" else: retval += " --notempty" return retval class F22_PwPolicy(KickstartCommand): """ Kickstart command implementing password policy. """ removedKeywords = KickstartCommand.removedKeywords removedAttrs = KickstartCommand.removedAttrs def __init__(self, writePriority=0, *args, **kwargs): KickstartCommand.__init__(self, writePriority, *args, **kwargs) self.op = self._getParser() self.policyList = kwargs.get("policyList", []) def __str__(self): retval = "" for policy in self.policyList: retval += policy.__str__() return retval def _getParser(self): op = KSOptionParser(prog="pwpolicy", version=F22, description=""" Set the policy to use for the named password entry.""") op.add_argument("--minlen", type=int, version=F22, help=""" Name of the password entry, currently supported values are: root, user and luks""") op.add_argument("--minquality", type=int, version=F22, help=""" Minimum libpwquality to consider good. When using ``--strict`` it will not allow passwords with a quality lower than this.""") op.add_argument("--strict", action="store_true", version=F22, help=""" Strict password enforcement. Passwords not meeting the ``--minquality`` level will not be allowed.""") op.add_argument("--notstrict", dest="strict", action="store_false", version=F22, help=""" Passwords not meeting the ``--minquality`` level will be allowed after Done is clicked twice.""") op.add_argument("--changesok", action="store_true", version=F22, help="""Allow empty password.""") op.add_argument("--nochanges", dest="changesok", action="store_false", version=F22, help=""" Do not allow UI to be used to change the password/user if it has been set in the kickstart.""") op.add_argument("--emptyok", action="store_true", version=F22, help=""" Allow empty password.""") op.add_argument("--notempty", dest="emptyok", action="store_false", version=F22, help=""" Don't allow an empty password.""") return op def parse(self, args): (ns, extra) = self.op.parse_known_args(args=args, lineno=self.lineno) if len(extra) != 1: raise KickstartParseError(lineno=self.lineno, msg=_("policy name required for %s") % "pwpolicy") pd = self.handler.PwPolicyData() self.set_to_obj(ns, pd) pd.lineno = self.lineno pd.name = extra[0] # Check for duplicates in the data list. if pd in self.dataList(): warnings.warn(_("A %(command)s with the name %(policyName)s has already been defined.") % {"command": "pwpolicy", "policyName": pd.name}) return pd def dataList(self): return self.policyList def get_policy(self, name, fallback_to_default=False): """ Get the policy by name :param str name: Name of the policy to return. :param bool fallback_to_default: If true return default policy if policy with the given `name` doesn't exists. """ policy = [p for p in self.policyList if p.name == name] if policy: return policy[0] elif fallback_to_default: return self.handler.PwPolicyData() else: return None