| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2022, James Livulpi |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: homectl |
| 11 |
author: |
| 12 |
- "James Livulpi (@jameslivulpi)" |
| 13 |
short_description: Manage user accounts with systemd-homed |
| 14 |
version_added: 4.4.0 |
| 15 |
description: |
| 16 |
- Manages a user's home directory managed by systemd-homed. |
| 17 |
notes: |
| 18 |
- This module uses L(passlib, https://pypi.org/project/passlib/) for password hashing when available, |
| 19 |
falling back to the Python C(crypt) module or L(legacycrypt, https://pypi.org/project/legacycrypt/). |
| 20 |
requirements: |
| 21 |
- passlib (Python library, recommended), or legacycrypt on Python 3.13 or newer |
| 22 |
- It requires no dependency on Python 3.12 and earlier, but then it relies on the deprecated standard library C(crypt). |
| 23 |
extends_documentation_fragment: |
| 24 |
- community.general._attributes |
| 25 |
attributes: |
| 26 |
check_mode: |
| 27 |
support: full |
| 28 |
diff_mode: |
| 29 |
support: none |
| 30 |
options: |
| 31 |
name: |
| 32 |
description: |
| 33 |
- The user name to create, remove, or update. |
| 34 |
required: true |
| 35 |
aliases: ['user', 'username'] |
| 36 |
type: str |
| 37 |
password: |
| 38 |
description: |
| 39 |
- Set the user's password to this. |
| 40 |
- Homed requires this value to be in cleartext on user creation and updating a user. |
| 41 |
- The module takes the password and generates a password hash in SHA-512 with 10000 rounds of salt generation using |
| 42 |
crypt. |
| 43 |
- See U(https://systemd.io/USER_RECORD/). |
| 44 |
- This is required for O(state=present). When an existing user is updated this is checked against the stored hash in |
| 45 |
homed. |
| 46 |
type: str |
| 47 |
state: |
| 48 |
description: |
| 49 |
- The operation to take on the user. |
| 50 |
choices: ['absent', 'present'] |
| 51 |
default: present |
| 52 |
type: str |
| 53 |
storage: |
| 54 |
description: |
| 55 |
- Indicates the storage mechanism for the user's home directory. |
| 56 |
- If the storage type is not specified, C(homed.conf(5\)) defines which default storage to use. |
| 57 |
- Only used when a user is first created. |
| 58 |
choices: ['classic', 'luks', 'directory', 'subvolume', 'fscrypt', 'cifs'] |
| 59 |
type: str |
| 60 |
disksize: |
| 61 |
description: |
| 62 |
- The intended home directory disk space. |
| 63 |
- Human readable value such as V(10G), V(10M), or V(10B). |
| 64 |
type: str |
| 65 |
resize: |
| 66 |
description: |
| 67 |
- When used with O(disksize) this attempts to resize the home directory immediately. |
| 68 |
default: false |
| 69 |
type: bool |
| 70 |
realname: |
| 71 |
description: |
| 72 |
- The user's real ('human') name. |
| 73 |
- This can also be used to add a comment to maintain compatibility with C(useradd). |
| 74 |
aliases: ['comment'] |
| 75 |
type: str |
| 76 |
realm: |
| 77 |
description: |
| 78 |
- The 'realm' a user is defined in. |
| 79 |
type: str |
| 80 |
email: |
| 81 |
description: |
| 82 |
- The email address of the user. |
| 83 |
type: str |
| 84 |
location: |
| 85 |
description: |
| 86 |
- A free-form location string describing the location of the user. |
| 87 |
type: str |
| 88 |
iconname: |
| 89 |
description: |
| 90 |
- The name of an icon picked by the user, for example for the purpose of an avatar. |
| 91 |
- Should follow the semantics defined in the Icon Naming Specification. |
| 92 |
- See U(https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html) for specifics. |
| 93 |
type: str |
| 94 |
homedir: |
| 95 |
description: |
| 96 |
- Path to use as home directory for the user. |
| 97 |
- This is the directory the user's home directory is mounted to while the user is logged in. |
| 98 |
- This is not where the user's data is actually stored, see O(imagepath) for that. |
| 99 |
- Only used when a user is first created. |
| 100 |
type: path |
| 101 |
imagepath: |
| 102 |
description: |
| 103 |
- Path to place the user's home directory. |
| 104 |
- See U(https://www.freedesktop.org/software/systemd/man/homectl.html#--image-path=PATH) for more information. |
| 105 |
- Only used when a user is first created. |
| 106 |
type: path |
| 107 |
uid: |
| 108 |
description: |
| 109 |
- Sets the UID of the user. |
| 110 |
- If using O(gid) homed requires the value to be the same. |
| 111 |
- Only used when a user is first created. |
| 112 |
type: int |
| 113 |
gid: |
| 114 |
description: |
| 115 |
- Sets the gid of the user. |
| 116 |
- If using O(uid) homed requires the value to be the same. |
| 117 |
- Only used when a user is first created. |
| 118 |
type: int |
| 119 |
mountopts: |
| 120 |
description: |
| 121 |
- String separated by comma each indicating mount options for a users home directory. |
| 122 |
- Valid options are V(nosuid), V(nodev) or V(noexec). |
| 123 |
- Homed by default uses V(nodev) and V(nosuid) while V(noexec) is off. |
| 124 |
type: str |
| 125 |
umask: |
| 126 |
description: |
| 127 |
- Sets the umask for the user's login sessions. |
| 128 |
- Value from V(0000) to V(0777). |
| 129 |
type: int |
| 130 |
memberof: |
| 131 |
description: |
| 132 |
- String separated by comma each indicating a UNIX group this user shall be a member of. |
| 133 |
- Groups the user should be a member of should be supplied as comma separated list. |
| 134 |
aliases: ['groups'] |
| 135 |
type: str |
| 136 |
skeleton: |
| 137 |
description: |
| 138 |
- The absolute path to the skeleton directory to populate a new home directory from. |
| 139 |
- This is only used when a home directory is first created. |
| 140 |
- If not specified homed by default uses V(/etc/skel). |
| 141 |
aliases: ['skel'] |
| 142 |
type: path |
| 143 |
shell: |
| 144 |
description: |
| 145 |
- Shell binary to use for terminal logins of given user. |
| 146 |
- If not specified homed by default uses V(/bin/bash). |
| 147 |
type: str |
| 148 |
environment: |
| 149 |
description: |
| 150 |
- String separated by comma each containing an environment variable and its value to set for the user's login session, |
| 151 |
in a format compatible with C(putenv(\)). |
| 152 |
- Any environment variable listed here is automatically set by pam_systemd for all login sessions of the user. |
| 153 |
aliases: ['setenv'] |
| 154 |
type: str |
| 155 |
timezone: |
| 156 |
description: |
| 157 |
- Preferred timezone to use for the user. |
| 158 |
- Should be a tzdata compatible location string such as V(America/New_York). |
| 159 |
type: str |
| 160 |
locked: |
| 161 |
description: |
| 162 |
- Whether the user account should be locked or not. |
| 163 |
type: bool |
| 164 |
language: |
| 165 |
description: |
| 166 |
- The preferred language/locale for the user. |
| 167 |
- This should be in a format compatible with the E(LANG) environment variable. |
| 168 |
type: str |
| 169 |
passwordhint: |
| 170 |
description: |
| 171 |
- Password hint for the given user. |
| 172 |
type: str |
| 173 |
sshkeys: |
| 174 |
description: |
| 175 |
- String separated by comma each listing a SSH public key that is authorized to access the account. |
| 176 |
- The keys should follow the same format as the lines in a traditional C(~/.ssh/authorized_key) file. |
| 177 |
type: str |
| 178 |
notbefore: |
| 179 |
description: |
| 180 |
- A time since the UNIX epoch before which the record should be considered invalid for the purpose of logging in. |
| 181 |
type: int |
| 182 |
notafter: |
| 183 |
description: |
| 184 |
- A time since the UNIX epoch after which the record should be considered invalid for the purpose of logging in. |
| 185 |
type: int |
| 186 |
""" |
| 187 |
|
| 188 |
EXAMPLES = r""" |
| 189 |
- name: Add the user 'james' |
| 190 |
community.general.homectl: |
| 191 |
name: johnd |
| 192 |
password: myreallysecurepassword1! |
| 193 |
state: present |
| 194 |
|
| 195 |
- name: Add the user 'alice' with a zsh shell, uid of 1000, and gid of 2000 |
| 196 |
community.general.homectl: |
| 197 |
name: alice |
| 198 |
password: myreallysecurepassword1! |
| 199 |
state: present |
| 200 |
shell: /bin/zsh |
| 201 |
uid: 1000 |
| 202 |
gid: 1000 |
| 203 |
|
| 204 |
- name: Modify an existing user 'frank' to have 10G of diskspace and resize usage now |
| 205 |
community.general.homectl: |
| 206 |
name: frank |
| 207 |
password: myreallysecurepassword1! |
| 208 |
state: present |
| 209 |
disksize: 10G |
| 210 |
resize: true |
| 211 |
|
| 212 |
- name: Remove an existing user 'janet' |
| 213 |
community.general.homectl: |
| 214 |
name: janet |
| 215 |
state: absent |
| 216 |
""" |
| 217 |
|
| 218 |
RETURN = r""" |
| 219 |
data: |
| 220 |
description: Dictionary returned from C(homectl inspect -j). |
| 221 |
returned: success |
| 222 |
type: dict |
| 223 |
sample: |
| 224 |
{ |
| 225 |
"data": { |
| 226 |
"binding": { |
| 227 |
"e9ed2a5b0033427286b228e97c1e8343": { |
| 228 |
"fileSystemType": "btrfs", |
| 229 |
"fileSystemUuid": "7bd59491-2812-4642-a492-220c3f0c6c0b", |
| 230 |
"gid": 60268, |
| 231 |
"imagePath": "/home/james.home", |
| 232 |
"luksCipher": "aes", |
| 233 |
"luksCipherMode": "xts-plain64", |
| 234 |
"luksUuid": "7f05825a-2c38-47b4-90e1-f21540a35a81", |
| 235 |
"luksVolumeKeySize": 32, |
| 236 |
"partitionUuid": "5a906126-d3c8-4234-b230-8f6e9b427b2f", |
| 237 |
"storage": "luks", |
| 238 |
"uid": 60268 |
| 239 |
} |
| 240 |
}, |
| 241 |
"diskSize": 3221225472, |
| 242 |
"disposition": "regular", |
| 243 |
"lastChangeUSec": 1641941238208691, |
| 244 |
"lastPasswordChangeUSec": 1641941238208691, |
| 245 |
"privileged": { |
| 246 |
"hashedPassword": [ |
| 247 |
"$6$ov9AKni.trf76inT$tTtfSyHgbPTdUsG0CvSSQZXGqFGdHKQ9Pb6e0BTZhDmlgrL/vA5BxrXduBi8u/PCBiYUffGLIkGhApjKMK3bV." |
| 248 |
] |
| 249 |
}, |
| 250 |
"signature": [ |
| 251 |
{ |
| 252 |
"data": "o6zVFbymcmk4YTVaY6KPQK23YCp+VkXdGEeniZeV1pzIbFzoaZBvVLPkNKMoPAQbodY5BYfBtuy41prNL78qAg==", |
| 253 |
"key": "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAbs7ELeiEYBxkUQhxZ+5NGyu6J7gTtZtZ5vmIw3jowcY=\n-----END PUBLIC KEY-----\n" |
| 254 |
} |
| 255 |
], |
| 256 |
"status": { |
| 257 |
"e9ed2a5b0033427286b228e97c1e8343": { |
| 258 |
"diskCeiling": 21845405696, |
| 259 |
"diskFloor": 268435456, |
| 260 |
"diskSize": 3221225472, |
| 261 |
"service": "io.systemd.Home", |
| 262 |
"signedLocally": true, |
| 263 |
"state": "inactive" |
| 264 |
} |
| 265 |
}, |
| 266 |
"userName": "james" |
| 267 |
} |
| 268 |
} |
| 269 |
""" |
| 270 |
|
| 271 |
import json |
| 272 |
|
| 273 |
from ansible.module_utils.basic import AnsibleModule, jsonify |
| 274 |
from ansible.module_utils.common.text.formatters import human_to_bytes |
| 275 |
|
| 276 |
from ansible_collections.community.general.plugins.module_utils import _deps as deps |
| 277 |
|
| 278 |
with deps.declare("crypt_context"): |
| 279 |
from ansible_collections.community.general.plugins.module_utils._crypt import CryptContext, has_crypt_context |
| 280 |
|
| 281 |
if not has_crypt_context: |
| 282 |
raise ImportError("Failed to import any of: passlib, crypt, legacycrypt") |
| 283 |
|
| 284 |
|
| 285 |
class Homectl: |
| 286 |
def __init__(self, module, crypt_context): |
| 287 |
self.module = module |
| 288 |
self.crypt_context = crypt_context |
| 289 |
self.state = module.params["state"] |
| 290 |
self.name = module.params["name"] |
| 291 |
self.password = module.params["password"] |
| 292 |
self.storage = module.params["storage"] |
| 293 |
self.disksize = module.params["disksize"] |
| 294 |
self.resize = module.params["resize"] |
| 295 |
self.realname = module.params["realname"] |
| 296 |
self.realm = module.params["realm"] |
| 297 |
self.email = module.params["email"] |
| 298 |
self.location = module.params["location"] |
| 299 |
self.iconname = module.params["iconname"] |
| 300 |
self.homedir = module.params["homedir"] |
| 301 |
self.imagepath = module.params["imagepath"] |
| 302 |
self.uid = module.params["uid"] |
| 303 |
self.gid = module.params["gid"] |
| 304 |
self.umask = module.params["umask"] |
| 305 |
self.memberof = module.params["memberof"] |
| 306 |
self.skeleton = module.params["skeleton"] |
| 307 |
self.shell = module.params["shell"] |
| 308 |
self.environment = module.params["environment"] |
| 309 |
self.timezone = module.params["timezone"] |
| 310 |
self.locked = module.params["locked"] |
| 311 |
self.passwordhint = module.params["passwordhint"] |
| 312 |
self.sshkeys = module.params["sshkeys"] |
| 313 |
self.language = module.params["language"] |
| 314 |
self.notbefore = module.params["notbefore"] |
| 315 |
self.notafter = module.params["notafter"] |
| 316 |
self.mountopts = module.params["mountopts"] |
| 317 |
|
| 318 |
self.result = {} |
| 319 |
|
| 320 |
# Cannot run homectl commands if service is not active |
| 321 |
def homed_service_active(self): |
| 322 |
is_active = True |
| 323 |
cmd = ["systemctl", "show", "systemd-homed.service", "-p", "ActiveState"] |
| 324 |
rc, show_service_stdout, stderr = self.module.run_command(cmd) |
| 325 |
if rc == 0: |
| 326 |
state = show_service_stdout.rsplit("=")[1] |
| 327 |
if state.strip() != "active": |
| 328 |
is_active = False |
| 329 |
return is_active |
| 330 |
|
| 331 |
def user_exists(self): |
| 332 |
exists = False |
| 333 |
valid_pw = False |
| 334 |
# Get user properties if they exist in json |
| 335 |
rc, stdout, stderr = self.get_user_metadata() |
| 336 |
if rc == 0: |
| 337 |
exists = True |
| 338 |
# User exists now compare password given with current hashed password stored in the user metadata. |
| 339 |
if self.state != "absent": # Don't need checking on remove user |
| 340 |
stored_pwhash = json.loads(stdout)["privileged"]["hashedPassword"][0] |
| 341 |
if self._check_password(stored_pwhash): |
| 342 |
valid_pw = True |
| 343 |
return exists, valid_pw |
| 344 |
|
| 345 |
def create_user(self): |
| 346 |
record = self.create_json_record(create=True) |
| 347 |
cmd = [self.module.get_bin_path("homectl", True)] |
| 348 |
cmd.append("create") |
| 349 |
cmd.append("--identity=-") # Read the user record from standard input. |
| 350 |
return self.module.run_command(cmd, data=record) |
| 351 |
|
| 352 |
def _hash_password(self, password): |
| 353 |
return self.crypt_context.hash(password, scheme="sha512_crypt", rounds=10000) |
| 354 |
|
| 355 |
def _check_password(self, pwhash): |
| 356 |
return self.crypt_context.verify(self.password, pwhash) |
| 357 |
|
| 358 |
def remove_user(self): |
| 359 |
cmd = [self.module.get_bin_path("homectl", True)] |
| 360 |
cmd.append("remove") |
| 361 |
cmd.append(self.name) |
| 362 |
return self.module.run_command(cmd) |
| 363 |
|
| 364 |
def prepare_modify_user_command(self): |
| 365 |
record = self.create_json_record() |
| 366 |
cmd = [self.module.get_bin_path("homectl", True)] |
| 367 |
cmd.append("update") |
| 368 |
cmd.append(self.name) |
| 369 |
cmd.append("--identity=-") # Read the user record from standard input. |
| 370 |
# Resize disksize now resize = true |
| 371 |
# This is not valid in user record (json) and requires it to be passed on command. |
| 372 |
if self.disksize and self.resize: |
| 373 |
cmd.append("--and-resize") |
| 374 |
cmd.append("true") |
| 375 |
self.result["changed"] = True |
| 376 |
return cmd, record |
| 377 |
|
| 378 |
def get_user_metadata(self): |
| 379 |
cmd = [self.module.get_bin_path("homectl", True)] |
| 380 |
cmd.append("inspect") |
| 381 |
cmd.append(self.name) |
| 382 |
cmd.append("-j") |
| 383 |
cmd.append("--no-pager") |
| 384 |
rc, stdout, stderr = self.module.run_command(cmd) |
| 385 |
return rc, stdout, stderr |
| 386 |
|
| 387 |
# Build up dictionary to jsonify for homectl commands. |
| 388 |
def create_json_record(self, create=False): |
| 389 |
record = {} |
| 390 |
user_metadata = {} |
| 391 |
self.result["changed"] = False |
| 392 |
# Get the current user record if not creating a new user record. |
| 393 |
if not create: |
| 394 |
rc, user_metadata, stderr = self.get_user_metadata() |
| 395 |
user_metadata = json.loads(user_metadata) |
| 396 |
# Remove elements that are not meant to be updated from record. |
| 397 |
# These are always part of the record when a user exists. |
| 398 |
user_metadata.pop("signature", None) |
| 399 |
user_metadata.pop("binding", None) |
| 400 |
user_metadata.pop("status", None) |
| 401 |
# Let last change Usec be updated by homed when command runs. |
| 402 |
user_metadata.pop("lastChangeUSec", None) |
| 403 |
# Now only change fields that are called on leaving what's currently in the record intact. |
| 404 |
record = user_metadata |
| 405 |
|
| 406 |
record["userName"] = self.name |
| 407 |
record["secret"] = {"password": [self.password]} |
| 408 |
|
| 409 |
if create: |
| 410 |
password_hash = self._hash_password(self.password) |
| 411 |
record["privileged"] = {"hashedPassword": [password_hash]} |
| 412 |
self.result["changed"] = True |
| 413 |
|
| 414 |
if self.uid and self.gid and create: |
| 415 |
record["uid"] = self.uid |
| 416 |
record["gid"] = self.gid |
| 417 |
self.result["changed"] = True |
| 418 |
|
| 419 |
if self.memberof: |
| 420 |
member_list = list(self.memberof.split(",")) |
| 421 |
if member_list != record.get("memberOf", [None]): |
| 422 |
record["memberOf"] = member_list |
| 423 |
self.result["changed"] = True |
| 424 |
|
| 425 |
if self.realname: |
| 426 |
if self.realname != record.get("realName"): |
| 427 |
record["realName"] = self.realname |
| 428 |
self.result["changed"] = True |
| 429 |
|
| 430 |
# Cannot update storage unless were creating a new user. |
| 431 |
# See 'Fields in the binding section' at https://systemd.io/USER_RECORD/ |
| 432 |
if self.storage and create: |
| 433 |
record["storage"] = self.storage |
| 434 |
self.result["changed"] = True |
| 435 |
|
| 436 |
# Cannot update homedir unless were creating a new user. |
| 437 |
# See 'Fields in the binding section' at https://systemd.io/USER_RECORD/ |
| 438 |
if self.homedir and create: |
| 439 |
record["homeDirectory"] = self.homedir |
| 440 |
self.result["changed"] = True |
| 441 |
|
| 442 |
# Cannot update imagepath unless were creating a new user. |
| 443 |
# See 'Fields in the binding section' at https://systemd.io/USER_RECORD/ |
| 444 |
if self.imagepath and create: |
| 445 |
record["imagePath"] = self.imagepath |
| 446 |
self.result["changed"] = True |
| 447 |
|
| 448 |
if self.disksize: |
| 449 |
# convert human readable to bytes |
| 450 |
if self.disksize != record.get("diskSize"): |
| 451 |
record["diskSize"] = human_to_bytes(self.disksize) |
| 452 |
self.result["changed"] = True |
| 453 |
|
| 454 |
if self.realm: |
| 455 |
if self.realm != record.get("realm"): |
| 456 |
record["realm"] = self.realm |
| 457 |
self.result["changed"] = True |
| 458 |
|
| 459 |
if self.email: |
| 460 |
if self.email != record.get("emailAddress"): |
| 461 |
record["emailAddress"] = self.email |
| 462 |
self.result["changed"] = True |
| 463 |
|
| 464 |
if self.location: |
| 465 |
if self.location != record.get("location"): |
| 466 |
record["location"] = self.location |
| 467 |
self.result["changed"] = True |
| 468 |
|
| 469 |
if self.iconname: |
| 470 |
if self.iconname != record.get("iconName"): |
| 471 |
record["iconName"] = self.iconname |
| 472 |
self.result["changed"] = True |
| 473 |
|
| 474 |
if self.skeleton: |
| 475 |
if self.skeleton != record.get("skeletonDirectory"): |
| 476 |
record["skeletonDirectory"] = self.skeleton |
| 477 |
self.result["changed"] = True |
| 478 |
|
| 479 |
if self.shell: |
| 480 |
if self.shell != record.get("shell"): |
| 481 |
record["shell"] = self.shell |
| 482 |
self.result["changed"] = True |
| 483 |
|
| 484 |
if self.umask: |
| 485 |
if self.umask != record.get("umask"): |
| 486 |
record["umask"] = self.umask |
| 487 |
self.result["changed"] = True |
| 488 |
|
| 489 |
if self.environment: |
| 490 |
if self.environment != record.get("environment", [None]): |
| 491 |
record["environment"] = list(self.environment.split(",")) |
| 492 |
self.result["changed"] = True |
| 493 |
|
| 494 |
if self.timezone: |
| 495 |
if self.timezone != record.get("timeZone"): |
| 496 |
record["timeZone"] = self.timezone |
| 497 |
self.result["changed"] = True |
| 498 |
|
| 499 |
if self.locked: |
| 500 |
if self.locked != record.get("locked"): |
| 501 |
record["locked"] = self.locked |
| 502 |
self.result["changed"] = True |
| 503 |
|
| 504 |
if self.passwordhint: |
| 505 |
if self.passwordhint != record.get("privileged", {}).get("passwordHint"): |
| 506 |
record["privileged"]["passwordHint"] = self.passwordhint |
| 507 |
self.result["changed"] = True |
| 508 |
|
| 509 |
if self.sshkeys: |
| 510 |
if self.sshkeys != record.get("privileged", {}).get("sshAuthorizedKeys"): |
| 511 |
record["privileged"]["sshAuthorizedKeys"] = list(self.sshkeys.split(",")) |
| 512 |
self.result["changed"] = True |
| 513 |
|
| 514 |
if self.language: |
| 515 |
if self.locked != record.get("preferredLanguage"): |
| 516 |
record["preferredLanguage"] = self.language |
| 517 |
self.result["changed"] = True |
| 518 |
|
| 519 |
if self.notbefore: |
| 520 |
if self.locked != record.get("notBeforeUSec"): |
| 521 |
record["notBeforeUSec"] = self.notbefore |
| 522 |
self.result["changed"] = True |
| 523 |
|
| 524 |
if self.notafter: |
| 525 |
if self.locked != record.get("notAfterUSec"): |
| 526 |
record["notAfterUSec"] = self.notafter |
| 527 |
self.result["changed"] = True |
| 528 |
|
| 529 |
if self.mountopts: |
| 530 |
opts = list(self.mountopts.split(",")) |
| 531 |
if "nosuid" in opts: |
| 532 |
if record.get("mountNoSuid") is not True: |
| 533 |
record["mountNoSuid"] = True |
| 534 |
self.result["changed"] = True |
| 535 |
else: |
| 536 |
if record.get("mountNoSuid") is not False: |
| 537 |
record["mountNoSuid"] = False |
| 538 |
self.result["changed"] = True |
| 539 |
|
| 540 |
if "nodev" in opts: |
| 541 |
if record.get("mountNoDevices") is not True: |
| 542 |
record["mountNoDevices"] = True |
| 543 |
self.result["changed"] = True |
| 544 |
else: |
| 545 |
if record.get("mountNoDevices") is not False: |
| 546 |
record["mountNoDevices"] = False |
| 547 |
self.result["changed"] = True |
| 548 |
|
| 549 |
if "noexec" in opts: |
| 550 |
if record.get("mountNoExecute") is not True: |
| 551 |
record["mountNoExecute"] = True |
| 552 |
self.result["changed"] = True |
| 553 |
else: |
| 554 |
if record.get("mountNoExecute") is not False: |
| 555 |
record["mountNoExecute"] = False |
| 556 |
self.result["changed"] = True |
| 557 |
|
| 558 |
return jsonify(record) |
| 559 |
|
| 560 |
|
| 561 |
def main(): |
| 562 |
module = AnsibleModule( |
| 563 |
argument_spec=dict( |
| 564 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 565 |
name=dict(type="str", required=True, aliases=["user", "username"]), |
| 566 |
password=dict(type="str", no_log=True), |
| 567 |
storage=dict(type="str", choices=["classic", "luks", "directory", "subvolume", "fscrypt", "cifs"]), |
| 568 |
disksize=dict(type="str"), |
| 569 |
resize=dict(type="bool", default=False), |
| 570 |
realname=dict(type="str", aliases=["comment"]), |
| 571 |
realm=dict(type="str"), |
| 572 |
email=dict(type="str"), |
| 573 |
location=dict(type="str"), |
| 574 |
iconname=dict(type="str"), |
| 575 |
homedir=dict(type="path"), |
| 576 |
imagepath=dict(type="path"), |
| 577 |
uid=dict(type="int"), |
| 578 |
gid=dict(type="int"), |
| 579 |
umask=dict(type="int"), |
| 580 |
environment=dict(type="str", aliases=["setenv"]), |
| 581 |
timezone=dict(type="str"), |
| 582 |
memberof=dict(type="str", aliases=["groups"]), |
| 583 |
skeleton=dict(type="path", aliases=["skel"]), |
| 584 |
shell=dict(type="str"), |
| 585 |
locked=dict(type="bool"), |
| 586 |
passwordhint=dict(type="str", no_log=True), |
| 587 |
sshkeys=dict(type="str", no_log=True), |
| 588 |
language=dict(type="str"), |
| 589 |
notbefore=dict(type="int"), |
| 590 |
notafter=dict(type="int"), |
| 591 |
mountopts=dict(type="str"), |
| 592 |
), |
| 593 |
supports_check_mode=True, |
| 594 |
required_if=[ |
| 595 |
("state", "present", ["password"]), |
| 596 |
("resize", True, ["disksize"]), |
| 597 |
], |
| 598 |
) |
| 599 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 600 |
|
| 601 |
deps.validate(module) |
| 602 |
|
| 603 |
crypt_context = CryptContext(schemes=["sha512_crypt", "sha256_crypt", "md5_crypt", "des_crypt"]) |
| 604 |
homectl = Homectl(module, crypt_context) |
| 605 |
homectl.result["state"] = homectl.state |
| 606 |
|
| 607 |
# First we need to make sure homed service is active |
| 608 |
if not homectl.homed_service_active(): |
| 609 |
module.fail_json(msg="systemd-homed.service is not active") |
| 610 |
|
| 611 |
# handle removing user |
| 612 |
if homectl.state == "absent": |
| 613 |
user_exists, valid_pwhash = homectl.user_exists() |
| 614 |
if user_exists: |
| 615 |
if module.check_mode: |
| 616 |
module.exit_json(changed=True) |
| 617 |
rc, stdout, stderr = homectl.remove_user() |
| 618 |
if rc != 0: |
| 619 |
module.fail_json(name=homectl.name, msg=stderr, rc=rc) |
| 620 |
homectl.result["changed"] = True |
| 621 |
homectl.result["rc"] = rc |
| 622 |
homectl.result["msg"] = f"User {homectl.name} removed!" |
| 623 |
else: |
| 624 |
homectl.result["changed"] = False |
| 625 |
homectl.result["msg"] = "User does not exist!" |
| 626 |
|
| 627 |
# Handle adding a user |
| 628 |
if homectl.state == "present": |
| 629 |
user_exists, valid_pwhash = homectl.user_exists() |
| 630 |
if not user_exists: |
| 631 |
if module.check_mode: |
| 632 |
module.exit_json(changed=True) |
| 633 |
rc, stdout, stderr = homectl.create_user() |
| 634 |
if rc != 0: |
| 635 |
module.fail_json(name=homectl.name, msg=stderr, rc=rc) |
| 636 |
rc, user_metadata, stderr = homectl.get_user_metadata() |
| 637 |
homectl.result["data"] = json.loads(user_metadata) |
| 638 |
homectl.result["rc"] = rc |
| 639 |
homectl.result["msg"] = f"User {homectl.name} created!" |
| 640 |
else: |
| 641 |
if valid_pwhash: |
| 642 |
# Run this to see if changed would be True or False which is useful for check_mode |
| 643 |
cmd, record = homectl.prepare_modify_user_command() |
| 644 |
else: |
| 645 |
# User gave wrong password fail with message |
| 646 |
homectl.result["changed"] = False |
| 647 |
homectl.result["msg"] = "User exists but password is incorrect!" |
| 648 |
module.fail_json(**homectl.result) |
| 649 |
|
| 650 |
if module.check_mode: |
| 651 |
module.exit_json(**homectl.result) |
| 652 |
|
| 653 |
# Now actually modify the user if changed was set to true at any point. |
| 654 |
if homectl.result["changed"]: |
| 655 |
rc, stdout, stderr = module.run_command(cmd, data=record) |
| 656 |
if rc != 0: |
| 657 |
module.fail_json(name=homectl.name, msg=stderr, rc=rc, changed=False) |
| 658 |
rc, user_metadata, stderr = homectl.get_user_metadata() |
| 659 |
homectl.result["data"] = json.loads(user_metadata) |
| 660 |
homectl.result["rc"] = rc |
| 661 |
if homectl.result["changed"]: |
| 662 |
homectl.result["msg"] = f"User {homectl.name} modified" |
| 663 |
|
| 664 |
module.exit_json(**homectl.result) |
| 665 |
|
| 666 |
|
| 667 |
if __name__ == "__main__": |
| 668 |
main() |