Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Nov 6, 2018
1 parent 23e648d commit 69eeaa6
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/include/users/dialogs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
# Jiri Suchomel <jsuchome@suse.cz>
#
# $Id$

require "users/ssh_public_key"

module Yast
module UsersDialogsInclude
def initialize_users_dialogs(include_target)
Expand Down Expand Up @@ -200,6 +203,55 @@ def format_days_after_epoch(count, date_format)
`date --date='1970-01-01 00:00:01 #{count} days' +#{date_format}`.chomp
end

def get_public_keys_term(user)
items = user.fetch("public_keys", []).each_with_index.map do |content, idx|
key = Y2Users::SSHPublicKey.new(content)
Item(Id(idx.to_s), key.fingerprint, key.comment)
end

VBox(
Table(
Id(:public_keys_table),
Opt(:notify),
Header(
_("Fingerprint"),
_("Comment")
),
items
),
HBox(
PushButton(Id(:add_public_key), _("Add...")),
PushButton(Id(:remove_public_key), Yast::Label.RemoveButton),
HStretch()
)
)
end

def handle_public_keys_input(action)
case action
when :add_public_key
add_public_key
when :remove_public_key
remove_public_key
end
end

def add_public_key
path = Yast::UI.AskForExistingFile("/home/imo/.ssh/", "*.pub", _("Select a public key"))
return unless path && File.exist?(path)
key = Y2Users::SSHPublicKey.new(File.read(path))
byebug
Users.AddPublicKey(key.to_s)
puts "X"
rescue Y2Users::SSHPublicKey::InvalidKey
Builtins.y2warning("Error")
end

def remove_public_key
selected_row = UI.QueryWidget(Id(:public_keys_table), :SelectedItems)
Users.RemovePublicKey(selected_row.to_i) if selected_row
end

# generate contents for Password Settings Dialog
# @param user [Hash]
# @param exp_date [String] may be MODIFIED on return corresponding to the UI
Expand Down Expand Up @@ -903,6 +955,8 @@ def EditUserDialog(what)
)
end

tabs << Item(Id(:public_keys), _("SSH Public Keys"))

# Now initialize the list of plugins: we must know now if there is some available.
# UsersPlugins will filter out plugins we cannot use for given type
plugin_clients = UsersPlugins.Apply(
Expand Down Expand Up @@ -1593,6 +1647,10 @@ def EditUserDialog(what)
end
end

if current == :public_keys
handle_public_keys_input(ret)
end

# inside plugins dialog
if current == :plugins
plugin_client = Convert.to_string(
Expand Down Expand Up @@ -1778,6 +1836,11 @@ def EditUserDialog(what)
Wizard.SetHelpText(EditUserPasswordDialogHelp())
current = ret
end
if ret == :public_keys
UI.ReplaceWidget(:tabContents, get_public_keys_term(user))
Wizard.SetHelpText("Some useful help!")
current = ret
end
if ret == :plugins
UI.ReplaceWidget(:tabContents, get_plugins_term.call)
Wizard.SetHelpText(PluginDialogHelp())
Expand Down
23 changes: 23 additions & 0 deletions src/modules/Users.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,7 @@ sub ReadLocal {
"base_directory" => $base_directory
);
# id limits are necessary for differ local and system users
y2milestone("DEBUG: UsersPasswd->Read");
my $init = UsersPasswd->Read (\%configuration);
if (!$init) {
my $error = UsersPasswd->GetError ();
Expand Down Expand Up @@ -3050,6 +3051,8 @@ sub AddUser {
}
$user_in_work{"type"} = $type;
$user_in_work{"what"} = "add_user";
my @public_keys = ();
$user_in_work{"public_keys"} = \@public_keys;

UsersCache->SetUserType ($type);

Expand Down Expand Up @@ -6700,5 +6703,25 @@ sub AddPlusGroup {
}
}

##------------------------------------
# Adds a public key to be added to the authorized_keys SSH file
BEGIN { $TYPEINFO{AddPublicKey} = ["function", "void", "string"];}
sub AddPublicKey {
my $self = shift;
my $key = shift;

my @keys = $user_in_work{"public_keys"};
push @keys, $key;
$user_in_work{"public_keys"} = \@keys;

# if (!defined($user_in_work{"public_keys"})) {
# my @keys = ();
# $user_in_work{"public_keys"} = \@keys;
# }
# push $user_in_work{"public_keys"}, $key;
#
my @public_keys = ();
$user_in_work{"public_keys"} = \@public_keys;
}
1
# EOF
2 changes: 2 additions & 0 deletions src/modules/UsersPasswd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ sub read_group {
sub read_authorized_keys {
foreach my $user (values %{$users{"local"}}) {
SSHAuthorizedKeys->read_keys($user->{"homeDirectory"});
$user->{"public_keys"} = SSHAuthorizedKeys->export_keys($user->{"homeDirectory"});
}

# Read authorized keys also from root's home (bsc#1066342)
Expand Down Expand Up @@ -515,6 +516,7 @@ sub GetErrorInfo {
BEGIN { $TYPEINFO{Read} = ["function", "boolean", ["map", "string", "string"]]}
sub Read {

y2milestone("DEBUG: UsersPasswd->Read");
my $self = shift;
my $config = shift;
my $ret = 0;
Expand Down

0 comments on commit 69eeaa6

Please sign in to comment.