From 6a19ff604a48463e88b21f63e95e1a4f862b81dc Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Wed, 29 May 2019 16:06:11 +0200 Subject: [PATCH] ModUser request. --- src/authd.cr | 28 ++++++++++++++++++++++++++++ src/main.cr | 15 +++++++++++++++ src/passwd.cr | 14 ++++++++------ 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/authd.cr b/src/authd.cr index eb33980..2034e3e 100644 --- a/src/authd.cr +++ b/src/authd.cr @@ -12,6 +12,7 @@ module AuthD AddUser GetUser GetUserByCredentials + ModUser # Edit user attributes. end enum ResponseTypes @@ -54,6 +55,13 @@ module AuthD }) end + class ModUserRequest + JSON.mapping({ + uid: Int32, + password: String? + }) + end + class Client < IPC::Client property key : String @@ -134,6 +142,26 @@ module AuthD Exception.new response.payload end end + + def mod_user(uid : Int32, password : String?) : Bool | Exception + payload = Hash(String, String|Int32).new + payload["uid"] = uid + + password.try do |password| + payload["password"] = password + end + + send RequestTypes::ModUser, payload.to_json + + response = read + + case ResponseTypes.new response.type.to_i + when ResponseTypes::Ok + true + else + Exception.new response.payload + end + end end end diff --git a/src/main.cr b/src/main.cr index ccbf703..2c14dcd 100644 --- a/src/main.cr +++ b/src/main.cr @@ -121,6 +121,21 @@ IPC::Service.new "auth" do |event| else client.send ResponseTypes::UserNotFound, "" end + when RequestTypes::ModUser + begin + request = ModUserRequest.from_json payload + rescue e + client.send ResponseTypes::MalformedRequest, e.message || "" + next + end + + password_hash = request.password.try do |s| + Passwd.hash_password s + end + + passwd.mod_user request.uid, password_hash: password_hash + + client.send ResponseTypes::Ok, "" end end end diff --git a/src/passwd.cr b/src/passwd.cr index 3e76273..79cb4d6 100644 --- a/src/passwd.cr +++ b/src/passwd.cr @@ -69,9 +69,7 @@ class Passwd ## # Will fail if the user is found but the password is invalid. def get_user(login : String, password : String) : AuthD::User? - digest = OpenSSL::Digest.new("sha256") - digest << password - hash = digest.hexdigest + hash = Passwd.hash_password password each_user do |user| if user.login == login @@ -138,6 +136,12 @@ class Passwd gid end + def self.hash_password(password) + digest = OpenSSL::Digest.new("sha256") + digest << password + digest.hexdigest + end + def add_user(login, password = nil, uid = nil, gid = nil, home = "/", shell = "/bin/nologin") # FIXME: If user already exists, exception? Replacement? @@ -146,9 +150,7 @@ class Passwd gid = get_free_gid if gid.nil? password_hash = if password - digest = OpenSSL::Digest.new("sha256") - digest << password - digest.hexdigest + Passwd.hash_password password else "x" end