summaryrefslogtreecommitdiff
path: root/cloudinit/config/cc_set_passwords.py
diff options
context:
space:
mode:
Diffstat (limited to 'cloudinit/config/cc_set_passwords.py')
-rwxr-xr-xcloudinit/config/cc_set_passwords.py21
1 files changed, 16 insertions, 5 deletions
diff --git a/cloudinit/config/cc_set_passwords.py b/cloudinit/config/cc_set_passwords.py
index 1379428d..c3c5b0ff 100755
--- a/cloudinit/config/cc_set_passwords.py
+++ b/cloudinit/config/cc_set_passwords.py
@@ -179,20 +179,21 @@ def handle(_name, cfg, cloud, log, args):
for line in plist:
u, p = line.split(':', 1)
if prog.match(p) is not None and ":" not in p:
- hashed_plist_in.append("%s:%s" % (u, p))
+ hashed_plist_in.append(line)
hashed_users.append(u)
else:
+ # in this else branch, we potentially change the password
+ # hence, a deviation from .append(line)
if p == "R" or p == "RANDOM":
p = rand_user_password()
randlist.append("%s:%s" % (u, p))
plist_in.append("%s:%s" % (u, p))
users.append(u)
-
ch_in = '\n'.join(plist_in) + '\n'
if users:
try:
log.debug("Changing password for %s:", users)
- util.subp(['chpasswd'], ch_in)
+ chpasswd(cloud.distro, ch_in)
except Exception as e:
errors.append(e)
util.logexc(
@@ -202,7 +203,7 @@ def handle(_name, cfg, cloud, log, args):
if hashed_users:
try:
log.debug("Setting hashed password for %s:", hashed_users)
- util.subp(['chpasswd', '-e'], hashed_ch_in)
+ chpasswd(cloud.distro, hashed_ch_in, hashed=True)
except Exception as e:
errors.append(e)
util.logexc(
@@ -218,7 +219,7 @@ def handle(_name, cfg, cloud, log, args):
expired_users = []
for u in users:
try:
- util.subp(['passwd', '--expire', u])
+ cloud.distro.expire_passwd(u)
expired_users.append(u)
except Exception as e:
errors.append(e)
@@ -238,4 +239,14 @@ def handle(_name, cfg, cloud, log, args):
def rand_user_password(pwlen=9):
return util.rand_str(pwlen, select_from=PW_SET)
+
+def chpasswd(distro, plist_in, hashed=False):
+ if util.is_FreeBSD():
+ for pentry in plist_in.splitlines():
+ u, p = pentry.split(":")
+ distro.set_passwd(u, p, hashed=hashed)
+ else:
+ cmd = ['chpasswd'] + (['-e'] if hashed else [])
+ util.subp(cmd, plist_in)
+
# vi: ts=4 expandtab