Skip to content

Commit

Permalink
python: T5026: Replace deprecated Python modules crypt, spwd
Browse files Browse the repository at this point in the history
DeprecationWarning: 'crypt' is deprecated and slated for removal in Python 3.13
DeprecationWarning: 'spwd' is deprecated and slated for removal in Python 3.13
  • Loading branch information
sarthurdev authored and c-po committed Feb 25, 2023
1 parent 893ead2 commit 3bad1d0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions debian/control
Expand Up @@ -131,6 +131,7 @@ Depends:
python3-netaddr,
python3-netifaces,
python3-paramiko,
python3-passlib,
python3-psutil,
python3-pyhumps,
python3-pystache,
Expand Down
15 changes: 10 additions & 5 deletions src/conf_mode/system-login.py
Expand Up @@ -16,12 +16,10 @@

import os

from crypt import crypt
from crypt import METHOD_SHA512
from passlib.hosts import linux_context
from psutil import users
from pwd import getpwall
from pwd import getpwnam
from spwd import getspnam
from sys import exit
from time import sleep

Expand Down Expand Up @@ -55,6 +53,13 @@ def get_local_users():

return local_users

def get_shadow_password(username):
with open('/etc/shadow') as f:
for user in f.readlines():
items = user.split(":")
if username == items[0]:
return items[1]
return None

def get_config(config=None):
if config:
Expand Down Expand Up @@ -154,7 +159,7 @@ def generate(login):
for user, user_config in login['user'].items():
tmp = dict_search('authentication.plaintext_password', user_config)
if tmp:
encrypted_password = crypt(tmp, METHOD_SHA512)
encrypted_password = linux_context.hash(tmp)
login['user'][user]['authentication']['encrypted_password'] = encrypted_password
del login['user'][user]['authentication']['plaintext_password']

Expand Down Expand Up @@ -187,7 +192,7 @@ def generate(login):
call(f"/opt/vyatta/sbin/my_set {add_user_encrypt}", env=env)
else:
try:
if getspnam(user).sp_pwdp == dict_search('authentication.encrypted_password', user_config):
if get_shadow_password(user) == dict_search('authentication.encrypted_password', user_config):
# If the current encrypted bassword matches the encrypted password
# from the config - do not update it. This will remove the encrypted
# value from the system logs.
Expand Down

0 comments on commit 3bad1d0

Please sign in to comment.