]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Tools.pm
added: openat, mkdirat
[pve-common.git] / src / PVE / Tools.pm
index cb8d9b27b78558c1d46a28f8dbb320e2eb90c521..3ad37940fdbb1fcfc298957bd3f5784e3baa82aa 100644 (file)
@@ -12,6 +12,7 @@ use Filesys::Df (); # don't overwrite our df()
 use IO::Pipe;
 use IO::File;
 use IO::Dir;
+use IO::Handle;
 use IPC::Open3;
 use Fcntl qw(:DEFAULT :flock);
 use base 'Exporter';
@@ -678,7 +679,7 @@ my $keymaphash =  {
 };
 
 my $kvmkeymaparray = [];
-foreach my $lc (keys %$keymaphash) {
+foreach my $lc (sort keys %$keymaphash) {
     push @$kvmkeymaparray, $keymaphash->{$lc}->[1];
 }
 
@@ -1219,7 +1220,7 @@ sub sync_mountpoint {
 sub sendmail {
     my ($mailto, $subject, $text, $html, $mailfrom, $author) = @_;
 
-    $mailto = [ $mailto ] if ref($mailto) ne 'ARRAY';
+    $mailto = [ $mailto ] if !ref($mailto);
 
     my $rcvrarg = '';
     foreach my $r (@$mailto) {
@@ -1228,7 +1229,7 @@ sub sendmail {
     my $rcvrtxt = join (', ', @$mailto);
 
     $mailfrom = $mailfrom || "root";
-    $author = $author || 'Proxmox VE mail notifier';
+    $author = $author || 'Proxmox VE';
 
     open (MAIL,"|sendmail -B 8BITMIME -f $mailfrom $rcvrarg") ||
        die "unable to open 'sendmail' - $!";
@@ -1247,7 +1248,7 @@ sub sendmail {
     print MAIL "This is a multi-part message in MIME format.\n\n";
     print MAIL "--$boundary\n";
 
-    if ($text) {
+    if (defined($text)) {
        print MAIL "Content-Type: text/plain;\n";
        print MAIL "\tcharset=\"UTF8\"\n";
        print MAIL "Content-Transfer-Encoding: 8bit\n";
@@ -1262,7 +1263,7 @@ sub sendmail {
        print MAIL "\n--$boundary\n";
     }
 
-    if($html) {
+    if (defined($html)) {
        print MAIL "Content-Type: text/html;\n";
        print MAIL "\tcharset=\"UTF8\"\n";
        print MAIL "Content-Transfer-Encoding: 8bit\n";
@@ -1274,7 +1275,6 @@ sub sendmail {
     }
 
     close(MAIL);
-
 }
 
 sub tempfile {
@@ -1308,4 +1308,39 @@ sub tempfile_contents {
     return ("/proc/$$/fd/".$fh->fileno, $fh);
 }
 
+sub validate_ssh_public_keys {
+    my ($raw) = @_;
+    my @lines = split(/\n/, $raw);
+
+    foreach my $line (@lines) {
+       next if $line =~ m/^\s*$/;
+       eval {
+           my ($filename, $handle) = tempfile_contents($line);
+           run_command(["ssh-keygen", "-l", "-f", $filename],
+                       outfunc => sub {}, errfunc => sub {});
+       };
+       die "SSH public key validation error\n" if $@;
+    }
+}
+
+sub openat($$$;$) {
+    my ($dirfd, $pathname, $flags, $mode) = @_;
+    my $fd = syscall(257, $dirfd, $pathname, $flags, $mode//0);
+    return undef if $fd < 0;
+    # sysopen() doesn't deal with numeric file descriptors apparently
+    # so we need to convert to a mode string for IO::Handle->new_from_fd
+    my $flagstr = ($flags & O_RDWR) ? 'rw' : ($flags & O_WRONLY) ? 'w' : 'r';
+    my $handle = IO::Handle->new_from_fd($fd, $flagstr);
+    return $handle if $handle;
+    my $err = $!; # save error before closing the raw fd
+    syscall(3, $fd); # close
+    $! = $err;
+    return undef;
+}
+
+sub mkdirat($$$) {
+    my ($dirfd, $name, $mode) = @_;
+    return syscall(258, $dirfd, $name, $mode) == 0;
+}
+
 1;