Skip to content

Commit

Permalink
OpenBSD, inherite from NetBSD
Browse files Browse the repository at this point in the history
  • Loading branch information
goneri committed Mar 24, 2020
1 parent ea67897 commit 62de1e6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 88 deletions.
5 changes: 4 additions & 1 deletion cloudinit/distros/netbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
LOG = logging.getLogger(__name__)


class Distro(cloudinit.distros.bsd.BSD):
class NetBSD(cloudinit.distros.bsd.BSD):
ci_sudoers_fn = '/usr/pkg/etc/sudoers.d/90-cloud-init-users'

group_add_cmd_prefix = ["groupadd"]
Expand Down Expand Up @@ -128,4 +128,7 @@ def update_package_sources(self):
pass


class Distro(NetBSD):
pass

# vi: ts=4 expandtab
89 changes: 2 additions & 87 deletions cloudinit/distros/openbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@
import os
import platform

import cloudinit.distros.bsd
import cloudinit.distros.netbsd
from cloudinit import log as logging
from cloudinit import util

LOG = logging.getLogger(__name__)


class Distro(cloudinit.distros.bsd.BSD):
class Distro(cloudinit.distros.netbsd.NetBSD):
hostname_conf_fn = '/etc/myname'
ci_sudoers_fn = '/usr/pkg/etc/sudoers.d/90-cloud-init-users'
group_add_cmd_prefix = ["groupadd"]
pkg_cmd_install_prefix = ["pkg_add", "-U"]
pkg_cmd_remove_prefix = ['pkg_delete']

def _read_hostname(self, filename, default=None):
with open(self.hostname_conf_fn, 'r') as fd:
Expand All @@ -31,84 +27,6 @@ def _write_hostname(self, hostname, filename):
def _get_add_member_to_group_cmd(self, member_name, group_name):
return ['usermod', '-G', group_name, member_name]

def add_user(self, name, **kwargs):
if util.is_user(name):
LOG.info("User %s already exists, skipping.", name)
return False

adduser_cmd = ['useradd']
log_adduser_cmd = ['useradd']

adduser_opts = {
"homedir": '-d',
"gecos": '-c',
"primary_group": '-g',
"groups": '-G',
"shell": '-s',
}
adduser_flags = {
"no_user_group": '--no-user-group',
"system": '--system',
"no_log_init": '--no-log-init',
}

for key, val in kwargs.items():
if (key in adduser_opts and val and
isinstance(val, str)):
adduser_cmd.extend([adduser_opts[key], val])

elif key in adduser_flags and val:
adduser_cmd.append(adduser_flags[key])
log_adduser_cmd.append(adduser_flags[key])

if 'no_create_home' not in kwargs or 'system' not in kwargs:
adduser_cmd += ['-m']
log_adduser_cmd += ['-m']

adduser_cmd += [name]
log_adduser_cmd += [name]

# Run the command
LOG.info("Adding user %s", name)
try:
util.subp(adduser_cmd, logstring=log_adduser_cmd)
except Exception:
util.logexc(LOG, "Failed to create user %s", name)
raise
# Set the password if it is provided
# For security consideration, only hashed passwd is assumed
passwd_val = kwargs.get('passwd', None)
if passwd_val is not None:
self.set_passwd(name, passwd_val, hashed=True)

def set_passwd(self, user, passwd, hashed=False):
if hashed:
hashed_pw = passwd
elif not hasattr(crypt, 'METHOD_BLOWFISH'):
# crypt.METHOD_BLOWFISH comes with Python 3.7
LOG.error((
'Cannot set non-encrypted password for user %s. '
'Python >= 3.7 is required.'), user)
return
else:
method = crypt.METHOD_BLOWFISH # pylint: disable=E1101
hashed_pw = crypt.crypt(
passwd,
crypt.mksalt(method))

try:
util.subp(['usermod', '-C', 'no', '-p', hashed_pw, user])
except Exception:
util.logexc(LOG, "Failed to set password for %s", user)
raise

def force_passwd_change(self, user):
try:
util.subp(['usermod', '-F', user])
except Exception:
util.logexc(LOG, "Failed to set pw expiration for %s", user)
raise

def lock_passwd(self, name):
try:
util.subp(['usermod', '-p', '*', name])
Expand All @@ -127,8 +45,5 @@ def _get_pkg_cmd_environ(self):
os_arch=os_arch, os_release=os_release)
return e

def update_package_sources(self):
pass


# vi: ts=4 expandtab

0 comments on commit 62de1e6

Please sign in to comment.