mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-22 07:03:38 +00:00
Only in end-user messages and documentation. This change was done mostly mechanically using the Perl script attached below. I also changed don't -> don’t etc and made some other simple fixes. See also: https://www.cl.cam.ac.uk/~mgk25/ucs/quotes.html ---------- #!/usr/bin/perl -w use strict; use Locale::PO; my $re = qr{'([-\w%.,=?*/]+)'}; my %files = (); foreach my $filename ("po/libguestfs.pot", "po-docs/libguestfs-docs.pot") { my $poref = Locale::PO->load_file_asarray($filename); foreach my $po (@$poref) { if ($po->msgid =~ $re) { my @refs = split /\s+/, $po->reference; foreach my $ref (@refs) { my ($file, $lineno) = split /:/, $ref, 2; $file =~ s{^\.\./}{}; if (exists $files{$file}) { push @{$files{$file}}, $lineno; } else { $files{$file} = [$lineno]; } } } } } foreach my $file (sort keys %files) { unless (-w $file) { warn "warning: $file is probably generated\n"; # have to edit generator next; } my @lines = sort { $a <=> $b } @{$files{$file}}; #print "editing $file at lines ", join (", ", @lines), " ...\n"; open FILE, "<$file" or die "$file: $!"; my @all = (); push @all, $_ while <FILE>; close FILE; my $ext = $file; $ext =~ s/^.*\.//; foreach (@lines) { # Don't mess with verbatim sections in POD files. next if $ext eq "pod" && $all[$_-1] =~ m/^ /; unless ($all[$_-1] =~ $re) { # this can happen for multi-line strings, have to edit it # by hand warn "warning: $file:$_ does not contain expected content\n"; next; } $all[$_-1] =~ s/$re/‘$1’/g; } rename "$file", "$file.bak"; open FILE, ">$file" or die "$file: $!"; print FILE $_ for @all; close FILE; my $mode = (stat ("$file.bak"))[2]; chmod ($mode & 0777, "$file"); }
138 lines
3.9 KiB
OCaml
138 lines
3.9 KiB
OCaml
(* virt-customize
|
||
* Copyright (C) 2014 Red Hat Inc.
|
||
*
|
||
* This program is free software; you can redistribute it and/or modify
|
||
* it under the terms of the GNU General Public License as published by
|
||
* the Free Software Foundation; either version 2 of the License, or
|
||
* (at your option) any later version.
|
||
*
|
||
* This program is distributed in the hope that it will be useful,
|
||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
* GNU General Public License for more details.
|
||
*
|
||
* You should have received a copy of the GNU General Public License along
|
||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||
*)
|
||
|
||
open Common_gettext.Gettext
|
||
open Common_utils
|
||
|
||
open Printf
|
||
open Sys
|
||
open Unix
|
||
|
||
module G = Guestfs
|
||
|
||
type ssh_key_selector =
|
||
| SystemKey
|
||
| KeyFile of string
|
||
| KeyString of string
|
||
|
||
let rec parse_selector arg =
|
||
parse_selector_list arg (String.nsplit ":" arg)
|
||
|
||
and parse_selector_list orig_arg = function
|
||
| [] | [ "" ] ->
|
||
SystemKey
|
||
| [ "file"; f ] ->
|
||
KeyFile f
|
||
| [ "string"; s ] ->
|
||
KeyString s
|
||
| _ ->
|
||
error (f_"invalid ssh-inject selector ‘%s’; see the man page") orig_arg
|
||
|
||
(* Find the local [on the host] user's SSH public key. See
|
||
* ssh-copy-id(1) default_ID_file for rationale.
|
||
*)
|
||
let pubkey_re = Str.regexp "^id.*\\.pub$"
|
||
let pubkey_ignore_re = Str.regexp ".*-cert\\.pub$"
|
||
|
||
let local_user_ssh_pubkey () =
|
||
let home_dir =
|
||
try getenv "HOME"
|
||
with Not_found ->
|
||
error (f_"ssh-inject: $HOME environment variable is not set") in
|
||
let ssh_dir = home_dir // ".ssh" in
|
||
let files = Sys.readdir ssh_dir in
|
||
let files = Array.to_list files in
|
||
let files = List.filter (
|
||
fun file ->
|
||
Str.string_match pubkey_re file 0 &&
|
||
not (Str.string_match pubkey_ignore_re file 0)
|
||
) files in
|
||
if files = [] then
|
||
error (f_"ssh-inject: no public key file found in %s") ssh_dir;
|
||
|
||
(* Newest file. *)
|
||
let files = List.map (
|
||
fun file ->
|
||
let file = ssh_dir // file in
|
||
let stat = stat file in
|
||
(file, stat.st_mtime)
|
||
) files in
|
||
let files = List.sort (fun (_,m1) (_,m2) -> compare m2 m1) files in
|
||
|
||
fst (List.hd files)
|
||
|
||
let read_key file =
|
||
(* Read and return the public key. *)
|
||
let key = read_whole_file file in
|
||
if key = "" then
|
||
error (f_"ssh-inject: public key file (%s) is empty") file;
|
||
key
|
||
|
||
let key_string_from_selector = function
|
||
| SystemKey ->
|
||
read_key (local_user_ssh_pubkey ())
|
||
| KeyFile f ->
|
||
read_key f
|
||
| KeyString s ->
|
||
if String.length s < 1 then
|
||
error (f_"ssh-inject: key is an empty string");
|
||
s
|
||
|
||
(* Inject SSH key, where possible. *)
|
||
let do_ssh_inject_unix (g : Guestfs.guestfs) user selector =
|
||
let key = key_string_from_selector selector in
|
||
assert (String.length key > 0);
|
||
|
||
(* If the key doesn't have \n at the end, add it. *)
|
||
let len = String.length key in
|
||
let key = if key.[len-1] = '\n' then key else key ^ "\n" in
|
||
|
||
(* Get user's home directory. *)
|
||
g#aug_init "/" 0;
|
||
let read_user_detail what =
|
||
try
|
||
let expr = sprintf "/files/etc/passwd/%s/%s" user what in
|
||
g#aug_get expr
|
||
with G.Error _ ->
|
||
error (f_"ssh-inject: the user %s does not exist on the guest")
|
||
user
|
||
in
|
||
let home_dir = read_user_detail "home" in
|
||
let uid = int_of_string (read_user_detail "uid") in
|
||
let gid = int_of_string (read_user_detail "gid") in
|
||
g#aug_close ();
|
||
|
||
(* Create ~user/.ssh if it doesn't exist. *)
|
||
let ssh_dir = sprintf "%s/.ssh" home_dir in
|
||
if not (g#exists ssh_dir) then (
|
||
g#mkdir ssh_dir;
|
||
g#chmod 0o700 ssh_dir;
|
||
g#chown uid gid ssh_dir;
|
||
);
|
||
|
||
(* Create ~user/.ssh/authorized_keys if it doesn't exist. *)
|
||
let auth_keys = sprintf "%s/authorized_keys" ssh_dir in
|
||
if not (g#exists auth_keys) then (
|
||
g#touch auth_keys;
|
||
g#chmod 0o600 auth_keys;
|
||
g#chown uid gid auth_keys;
|
||
);
|
||
|
||
(* Append the key. *)
|
||
g#write_append auth_keys key
|