From 6ca6ec35449754799e81a2a4baafd32feacaebbc Mon Sep 17 00:00:00 2001 From: Anthony C George Date: Mon, 31 Oct 2016 16:39:48 -0400 Subject: [PATCH] Add block for arbitrary username with password. If the user exists, change the password. --- adduserpass/adduserpass.json | 27 +++++++++++++++++++++++++++ adduserpass/adduserpass.py | 21 +++++++++++++++++++++ info.json | 1 + 3 files changed, 49 insertions(+) create mode 100644 adduserpass/adduserpass.json create mode 100644 adduserpass/adduserpass.py diff --git a/adduserpass/adduserpass.json b/adduserpass/adduserpass.json new file mode 100644 index 0000000..42bb7ac --- /dev/null +++ b/adduserpass/adduserpass.json @@ -0,0 +1,27 @@ +{ + "name": "adduserpass", + "text": "Add %1 user\\n with password %2", + "script": "adduserpass.py", + "args": [ + { + "type":"text", + "default":"newuser", + "maxLength":0 + }, + { + "type": "text", + "default": "newpass", + "maxLength":0 + } + ], + "network": false, + "continue": true, + "type": "setting", + "category":"setting", + "supportedOperatingSystems": [ + "raspbian-pibakery.img", + "raspbian-lite-pibakery.img" + ], + "shortDescription":"Add a user with password or change password", + "longDescription":"If the user doesn't exists, this will add a user(with home dir and group). If the user exists, change the password." +} diff --git a/adduserpass/adduserpass.py b/adduserpass/adduserpass.py new file mode 100644 index 0000000..301bd43 --- /dev/null +++ b/adduserpass/adduserpass.py @@ -0,0 +1,21 @@ +#!/usr/bin/python +# Thanks to Filipe Pina on StackOverflow + +import subprocess,crypt,random, sys + +login = sys.argv[1] +password = sys.argv[2] + +ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./" +salt = ''.join(random.choice(ALPHABET) for i in range(16)) + +shadow_password = crypt.crypt(password,'$6$'+salt+'$') + +r = subprocess.call(('useradd', '-m', '-U', login)) +if r ==9: + print 'Username in use for '+login+', changing password' + +r = subprocess.call(('usermod', '-p', shadow_password, login)) + +if r != 0: + print 'Error changing password for ' + login diff --git a/info.json b/info.json index 61fd5e5..44f690a 100755 --- a/info.json +++ b/info.json @@ -16,6 +16,7 @@ "otgremove", "bootbehaviour", "changepass", + "adduserpass", "sethostname", "newcronjob", "uartconsole",