From 886a17a034ea116ab1179c7d6422dd74c9a06bc1 Mon Sep 17 00:00:00 2001
From: Ghassane Ben El Aattar <ghassaneb.aattar@huawei.com>
Date: Wed, 10 Aug 2022 09:27:20 +0000
Subject: [PATCH] oniro-user: Script that allows to edit the default password

This python script allows the user to edit the default password,
by modifying the local.conf file (the path has to be specified).

Signed-off-by: Ghassane Ben El Aattar  <ghassaneb.aattar@huawei.com>
---
 scripts/changeCredentials.py | 68 ++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 scripts/changeCredentials.py

diff --git a/scripts/changeCredentials.py b/scripts/changeCredentials.py
new file mode 100644
index 00000000..bbb54088
--- /dev/null
+++ b/scripts/changeCredentials.py
@@ -0,0 +1,68 @@
+# 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)
+   
-- 
GitLab