Skip to content
Snippets Groups Projects

oniro-user: Script that allows to edit the default password

+ 68
0
# SPDX-FileCopyrightText: Huawei Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
# Allows to change default password through interaction or command line arguments
from getpass import getpass
import pwd
import crypt
import sys
arguments = sys.argv
path = ""
password = ""
#if the arguments are inserted, the interaction is not used; the arguments are the local.conf path and new password; arguments[0] is the python script name
if(len(arguments) == 3):
path = arguments[1]
password = arguments[2]
else:
print('Insert path to your local.conf file: ')
path = input()
print('Insert new password: ')
password = getpass()
#SHA512 encryption of the password
crypt.METHOD_SHA512
password = crypt.crypt(password)
#insert slash before each $ character in the encrypted password, otherwise it won't work
single_slash2 = "\ "[0]
addSlash = []
pushedRight = 0
for i in range(0,len(password)):
if(password[i] == '$'):
addSlash.append(i)
for pos in addSlash:
password = password[:pos + pushedRight] + single_slash2 + password[pos + pushedRight:]
pushedRight = pushedRight + 1
#read the file and replace the username and password with the newly inserted values
alreadyExistPassword = 0
with open(path,'r' ) as file:
data = file.readlines()
index = 0
for line in data:
correctLinePassword = 0
for word in line.split(" "):
if(word == 'DEFAULT_USER_PASSWORD'):
correctLinePassword = 1
alreadyExistPassword = 1
break
if(correctLinePassword == 1):
data[index] = 'DEFAULT_USER_PASSWORD ?= '+ '"' +password +'"'+ '\n'
index = index + 1
if(alreadyExistPassword == 0):
data.append('DEFAULT_USER_PASSWORD ?= '+ '"' +password +'"'+ '\n')
#apply changes to local.conf file
with open(path, 'w') as file:
file.writelines(data)
Loading