]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Tools.pm
mark decode_utf8_parameters() as depreciated
[pve-common.git] / src / PVE / Tools.pm
index fe35ef225e46f35d52d5c374c00e3738b8b3871b..1fe7f4c8ae12e49515d3d27072980f9587cc55d8 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use warnings;
 use POSIX qw(EINTR EEXIST EOPNOTSUPP);
 use IO::Socket::IP;
-use Socket qw(AF_INET AF_INET6 AI_ALL AI_V4MAPPED);
+use Socket qw(AF_INET AF_INET6 AI_ALL AI_V4MAPPED AI_CANONNAME SOCK_DGRAM);
 use IO::Select;
 use File::Basename;
 use File::Path qw(make_path);
@@ -341,7 +341,7 @@ sub run_command {
     my $timeout;
     my $oldtimeout;
     my $pid;
-    my $exitcode;
+    my $exitcode = -1;
 
     my $outfunc;
     my $errfunc;
@@ -977,6 +977,8 @@ sub decode_text {
     return Encode::decode("utf8", uri_unescape($data));
 }
 
+# depreciated - do not use!
+# we now decode all parameters by default
 sub decode_utf8_parameters {
     my ($param) = @_;
 
@@ -1081,7 +1083,7 @@ sub dump_logfile {
 }
 
 sub dump_journal {
-    my ($start, $limit, $since, $until) = @_;
+    my ($start, $limit, $since, $until, $service) = @_;
 
     my $lines = [];
     my $count = 0;
@@ -1100,6 +1102,7 @@ sub dump_journal {
 
     my $cmd = ['journalctl', '-o', 'short', '--no-pager'];
 
+    push @$cmd, '--unit', $service if $service;
     push @$cmd, '--since', $since if $since;
     push @$cmd, '--until', $until if $until;
     run_command($cmd, outfunc => $parser);
@@ -1197,6 +1200,24 @@ sub get_host_address_family {
     return $res[0]->{family};
 }
 
+# get the fully qualified domain name of a host
+# same logic as hostname(1): The FQDN is the name getaddrinfo(3) returns,
+# given a nodename as a parameter
+sub get_fqdn {
+    my ($nodename) = @_;
+
+    my $hints = {
+       flags => AI_CANONNAME,
+       socktype => SOCK_DGRAM
+    };
+
+    my ($err, @addrs) = Socket::getaddrinfo($nodename, undef, $hints);
+
+    die "getaddrinfo: $err" if $err;
+
+    return $addrs[0]->{canonname};
+}
+
 # Parses any sane kind of host, or host+port pair:
 # The port is always optional and thus may be undef.
 sub parse_host_and_port {
@@ -1443,4 +1464,18 @@ sub enter_systemd_scope {
     die "systemd job never completed\n" if !$done;
 }
 
+my $salt_starter = time();
+
+sub encrypt_pw {
+    my ($pw) = @_;
+
+    $salt_starter++;
+    my $salt = substr(Digest::SHA::sha1_base64(time() + $salt_starter + $$), 0, 8);
+
+    # crypt does not want '+' in salt (see 'man crypt')
+    $salt =~ s/\+/X/g;
+
+    return crypt(encode("utf8", $pw), "\$5\$$salt\$");
+}
+
 1;