Skip to content

Commit

Permalink
Allow local users with an expiry date to be created
Browse files Browse the repository at this point in the history
The luseradd / lusermod commands do not support the -e option. Set
the expiry time in this case via lchage after the user was
created / modified.

Fixes: ansible#71942
  • Loading branch information
rpluem-vf committed Sep 30, 2020
1 parent 4197666 commit a09cc85
Show file tree
Hide file tree
Showing 3 changed files with 378 additions and 7 deletions.
5 changes: 5 additions & 0 deletions changelogs/fragments/fix_ansible_issue_71942.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bugfixes:
- >
user - Local users with an expiry date cannot be created as the luseradd /
lusermod commands do not support the -e option. Set the expiry time in
this case via lchage after the user was created / modified. (https://github.com/ansible/ansible/issues/71942)
50 changes: 43 additions & 7 deletions lib/ansible/modules/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@
import socket
import subprocess
import time
import math

from ansible.module_utils import distro
from ansible.module_utils._text import to_bytes, to_native, to_text
Expand Down Expand Up @@ -581,6 +582,7 @@ def create_user_useradd(self):
if self.local:
command_name = 'luseradd'
lgroupmod_cmd = self.module.get_bin_path('lgroupmod', True)
lchage_cmd = self.module.get_bin_path('lchage', True)
else:
command_name = 'useradd'

Expand Down Expand Up @@ -648,7 +650,7 @@ def create_user_useradd(self):
cmd.append('-s')
cmd.append(self.shell)

if self.expires is not None:
if self.expires is not None and not self.local:
cmd.append('-e')
if self.expires < time.gmtime(0):
cmd.append('')
Expand All @@ -674,7 +676,22 @@ def create_user_useradd(self):

cmd.append(self.name)
(rc, err, out) = self.execute_command(cmd)
if not self.local or rc != 0 or self.groups is None or len(self.groups) == 0:
if not self.local or rc != 0:
return (rc, err, out)

if self.expires is not None:
if self.expires < time.gmtime(0):
lexpires = -1
else:
# Convert seconds since Epoch to days since Epoch
lexpires = math.floor(float(module.params['expires'])) // 86400
(rc, _err, _out) = self.execute_command([lchage_cmd, '-E', str(lexpires), self.name])
out += _out
err += _err
if rc != 0:
return (rc, out, err)

if self.groups is None or len(self.groups) == 0:
return (rc, err, out)

for add_group in groups:
Expand Down Expand Up @@ -719,6 +736,8 @@ def modify_user_usermod(self):
lgroupmod_cmd = self.module.get_bin_path('lgroupmod', True)
lgroupmod_add = set()
lgroupmod_del = set()
lchage_cmd = self.module.get_bin_path('lchage', True)
lexpires = None
else:
command_name = 'usermod'

Expand Down Expand Up @@ -801,16 +820,23 @@ def modify_user_usermod(self):

if self.expires < time.gmtime(0):
if current_expires >= 0:
cmd.append('-e')
cmd.append('')
if self.local:
lexpires = -1
else:
cmd.append('-e')
cmd.append('')
else:
# Convert days since Epoch to seconds since Epoch as struct_time
current_expire_date = time.gmtime(current_expires * 86400)

# Current expires is negative or we compare year, month, and day only
if current_expires < 0 or current_expire_date[:3] != self.expires[:3]:
cmd.append('-e')
cmd.append(time.strftime(self.DATE_FORMAT, self.expires))
if self.local:
# Convert seconds since Epoch to days since Epoch
lexpires = math.floor(float(module.params['expires'])) // 86400
else:
cmd.append('-e')
cmd.append(time.strftime(self.DATE_FORMAT, self.expires))

# Lock if no password or unlocked, unlock only if locked
if self.password_lock and not info[1].startswith('!'):
Expand All @@ -830,7 +856,17 @@ def modify_user_usermod(self):
cmd.append(self.name)
(rc, err, out) = self.execute_command(cmd)

if not self.local or not (rc is None or rc == 0) or (len(lgroupmod_add) == 0 and len(lgroupmod_del) == 0):
if not self.local or not (rc is None or rc == 0):
return (rc, err, out)

if lexpires is not None:
(rc, _err, _out) = self.execute_command([lchage_cmd, '-E', str(lexpires), self.name])
out += _out
err += _err
if rc != 0:
return (rc, out, err)

if len(lgroupmod_add) == 0 and len(lgroupmod_del) == 0:
return (rc, err, out)

for add_group in lgroupmod_add:
Expand Down
Loading

0 comments on commit a09cc85

Please sign in to comment.