| 1 | #!/usr/bin/perl -w |
|---|
| 2 | |
|---|
| 3 | # Script to be used as pbscript to create a potentialy |
|---|
| 4 | # missing pb account on the QEMU VM, and adds it to sudo |
|---|
| 5 | |
|---|
| 6 | # Needs to be extented with the pb Distribution.pm module at the end externally |
|---|
| 7 | # |
|---|
| 8 | # Needs to use root account to connect to the VM |
|---|
| 9 | |
|---|
| 10 | # You need to specify here your local public SSH key used to access |
|---|
| 11 | # the pb account in the VM later on |
|---|
| 12 | |
|---|
| 13 | use strict; |
|---|
| 14 | use File::Basename; |
|---|
| 15 | |
|---|
| 16 | my $file = "/tmp/pbkey"; |
|---|
| 17 | |
|---|
| 18 | open(PBFILE,"> $file") || die "Unable to open $file"; |
|---|
| 19 | print PBFILE "ssh-dss AAAAB3NzaC1kc3MAAACBAMC06bNLMu8ZBJv3Hi77+fi+GUhtQScIbGKru86C/IP27EjrmYhPVccbu8kpX6SDHIouhpZ00gaefP3zHjLUmPKlXkMNUNjxx5JNLPoHhSIuUvoSTNoh9gsi31abAvzbW+RNer7rvBJO4ytNJRdsuNxBwqjxK04JEmatds+aKTQjAAAAFQDHaNmIuvYZ0RtNBVe+tzTzxxCQmQAAAIA4o89Xob2VBRtQKBjmWVAuTS14qiqnQUPNloaRkM+r2odI+NJDFc+WXOIG9QAPKKt1jvFOBOX9Mu3/xwiDLnAGhxud98FoyHevSpIWUld7rG4j1aww1WxolFWY/iPyAvDDMW3iysCoqQeSOlpRd9Mz/dq6/P/VcizhdGOKyc3JBgAAAIAA1eY+BoneiQ/tQ8j3GYbMTu6U7VzZ2OaoVeQn4NtZmPl+ZeQIj83nFpo99mYCbaBPqnZToERndl+ljp/JX5pamwj6RcLIDCyiB6MXLbQSWj0goegU1htp5aMd3NcyR2Jy8gCnf0QjDr7j23Q+CFGExtRb/nsDmaG5W9D6QSWQ/g== bruno\@victoria\n"; |
|---|
| 20 | close(PBFILE); |
|---|
| 21 | chmod 0644,$file; |
|---|
| 22 | |
|---|
| 23 | $file="/etc/passwd"; |
|---|
| 24 | open(PBFILE,$file) || die "Unable to open $file"; |
|---|
| 25 | my $found = 0; |
|---|
| 26 | while (<PBFILE>) { |
|---|
| 27 | $found = 1 if (/^pb:/); |
|---|
| 28 | } |
|---|
| 29 | close(PBFILE); |
|---|
| 30 | |
|---|
| 31 | if ( $found == 0 ) { |
|---|
| 32 | if ( ! -d "/home" ) { |
|---|
| 33 | mkdir "/home"; |
|---|
| 34 | } |
|---|
| 35 | system "groupadd pb"; |
|---|
| 36 | system "useradd pb -g pb -m -d /home/pb"; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | chdir "/home/pb"; |
|---|
| 40 | mkdir ".ssh",0700; |
|---|
| 41 | system 'cp /tmp/pbkey .ssh/authorized_keys'; |
|---|
| 42 | chmod 0600,".ssh/authorized_keys"; |
|---|
| 43 | system 'chown -R pb:pb .ssh'; |
|---|
| 44 | |
|---|
| 45 | # No passwd for pb only keys |
|---|
| 46 | $file="/etc/shadow"; |
|---|
| 47 | open(PBFILE,$file) || die "Unable to open $file"; |
|---|
| 48 | open(PBOUT,"> $file.new") || die "Unable to open $file.new"; |
|---|
| 49 | while (<PBFILE>) { |
|---|
| 50 | s/^pb:\!\!:/pb:*:/; |
|---|
| 51 | s/^pb:\!:/pb:*:/; #SLES 9 e.g. |
|---|
| 52 | print PBOUT $_; |
|---|
| 53 | } |
|---|
| 54 | close(PBFILE); |
|---|
| 55 | close(PBOUT); |
|---|
| 56 | rename("$file.new",$file); |
|---|
| 57 | chmod 0640,$file; |
|---|
| 58 | |
|---|
| 59 | # Adapt sudoers |
|---|
| 60 | $file="/etc/sudoers"; |
|---|
| 61 | $found = 0; |
|---|
| 62 | open(PBFILE,$file) || die "Unable to open $file"; |
|---|
| 63 | open(PBOUT,"> $file.new") || die "Unable to open $file.new"; |
|---|
| 64 | while (<PBFILE>) { |
|---|
| 65 | $found = 1 if (/^pb /); |
|---|
| 66 | s/Defaults[ \t]+requiretty//; |
|---|
| 67 | print PBOUT $_; |
|---|
| 68 | } |
|---|
| 69 | close(PBFILE); |
|---|
| 70 | print PBOUT "pb ALL=(ALL) NOPASSWD:ALL\n" if ( $found == 0 ); |
|---|
| 71 | close(PBOUT); |
|---|
| 72 | rename("$file.new",$file); |
|---|
| 73 | chmod 0440,$file; |
|---|
| 74 | |
|---|
| 75 | unlink "/tmp/pbkey"; |
|---|
| 76 | |
|---|
| 77 | my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init(); |
|---|
| 78 | print "distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n"; |
|---|
| 79 | |
|---|
| 80 | # Get and install pb |
|---|
| 81 | if ( $ddir eq "fedora" ) { |
|---|
| 82 | system "yum clean all"; |
|---|
| 83 | system "yum update -y"; |
|---|
| 84 | my $arch=`uname -m`; |
|---|
| 85 | my $opt = ""; |
|---|
| 86 | chomp($arch); |
|---|
| 87 | if ($arch eq "x86_64") { |
|---|
| 88 | $opt="--exclude=*.i?86"; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | my $topbuilddir=`rpmquery --eval '%{_topdir}' 2> /dev/null`; |
|---|
| 92 | chomp($topbuilddir); |
|---|
| 93 | |
|---|
| 94 | my $p = `rpm -q afio`; |
|---|
| 95 | chomp($p); |
|---|
| 96 | if ($p !~ /^afio/) { |
|---|
| 97 | system "rm -f afio-* ; wget ftp://ftp.mondorescue.org/src/afio-2.4.7-1.src.rpm ; rpm -ivh afio-2.4.7-1.src.rpm ; rpmbuild -ba $topbuilddir/SPECS/afio.spec ; chmod 644 $topbuilddir/RPMS/*/*.rpm"; |
|---|
| 98 | } |
|---|
| 99 | $p = `rpm -q buffer`; |
|---|
| 100 | chomp($p); |
|---|
| 101 | if ($p !~ /^buffer/) { |
|---|
| 102 | system "rm -f buffer-* ; wget ftp://ftp.mondorescue.org/src/buffer-1.19-1.src.rpm ; rpm -ivh buffer-1.19-1.src.rpm ; rpmbuild -ba $topbuilddir/SPECS/buffer.spec ; chmod 644 $topbuilddir/RPMS/*/*.rpm"; |
|---|
| 103 | } |
|---|
| 104 | if ( $dver <= 4 ) { |
|---|
| 105 | # FC4 - AppConfig unavailable |
|---|
| 106 | system "yum -y $opt install neon newt-devel slang-devel autoconf automake libtool gcc rpm-build wget gcc-c++ docbook-utils-pdf ImageMagick man patch cdrecord mkisofs perl-DateManip ntp ; rm -rf AppConfig-* ; wget http://search.cpan.org/CPAN/authors/id/A/AB/ABW/AppConfig-1.66.tar.gz ; tar xvfz AppConfig-1.66.tar.gz ; cd AppConfig* ; perl Makefile.PL ; make ; make install ; cd .."; |
|---|
| 107 | } else { |
|---|
| 108 | # FC5/6/7 |
|---|
| 109 | system "yum -y $opt install neon newt-devel slang-devel autoconf automake libtool gcc rpm-build wget gcc-c++ docbook-utils-pdf ImageMagick man patch cdrecord mkisofs ntp perl-AppConfig perl-DateManip"; |
|---|
| 110 | } |
|---|
| 111 | } elsif (($ddir eq "suse") && (($dver eq "10.2") || ($dver eq "10.3"))) { |
|---|
| 112 | system "export TERM=linux ; liste="" ; for i in neon newt-devel slang-devel autoconf automake libtool gcc wget gcc-c++ docbook-utils ImageMagick man patch mkisofs perl-DateManip perl-AppConfig perl-File-HomeDir xntp; do rpm -q \$i; if [ \$\? ne 0 ]; then liste=\"\$liste \$i\"; fi; done; yast2 -i \$liste"; |
|---|
| 113 | } elsif (( $dfam eq "rh" ) || ($ddir eq "sles") || (($ddir eq "suse") && (($dver eq "10.1") || ($dver eq "10.0"))) || (($ddir eq "mandrake") && ($dver eq "10.1"))) { |
|---|
| 114 | # Suppose pkg are installed already |
|---|
| 115 | system "rpm -e lsb"; |
|---|
| 116 | system "rm -rf AppConfig-* ; wget http://search.cpan.org/CPAN/authors/id/A/AB/ABW/AppConfig-1.66.tar.gz ; tar xvfz AppConfig-1.66.tar.gz ; cd AppConfig* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf DateManip* ; wget http://search.cpan.org/CPAN/authors/id/S/SB/SBECK/Date-Manip-5.46.tar.gz ; tar xvfz Date-Manip-5.46.tar.gz ; cd Date-Manip* ; perl Makefile.PL ; make ; make install ; cd .. "; |
|---|
| 117 | } elsif ( $dfam eq "md" ) { |
|---|
| 118 | if (( $dver eq "2006.0" ) || ( $dver eq "10.2" )) { |
|---|
| 119 | # mondo not available |
|---|
| 120 | system "urpmi.update -a ; urpmi --auto neon newt-devel slang-devel glibc-static-devel autoconf automake libtool gcc rpm-build wget gcc-c++ docbook-utils-pdf ImageMagick man patch mkisofs cdrecord ntp-client perl-AppConfig perl-DateManip"; |
|---|
| 121 | } else { |
|---|
| 122 | system "urpmi.update -a ; urpmi --auto neon newt-devel slang-devel glibc-static-devel autoconf automake libtool gcc rpm-build wget gcc-c++ docbook-utils-pdf ImageMagick man patch mondo mkisofs cdrecord ntp-client perl-AppConfig perl-DateManip"; |
|---|
| 123 | } |
|---|
| 124 | } elsif ( $dfam eq "du" ) { |
|---|
| 125 | system "apt-get install autoconf automake libtool g++ wget patch mondo groff imagemagick docbook-utils docbook2x docbook-to-man openssh-server dpkg-dev debian-builder dh-make fakeroot libnewt-dev ntp-client libncurses5-dev"; |
|---|
| 126 | } elsif ( $dfam eq "gen" ) { |
|---|
| 127 | system "emerge neon newt slang autoconf automake subversion libtool gcc wget vim man groff lynx grub afio buffer mindi mondo-rescue cdrecord mkisofs ntp-client"; |
|---|
| 128 | } else { |
|---|
| 129 | print "No pkg to install\n"; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | # Sync date |
|---|
| 133 | system "/usr/sbin/ntpdate ntp.home.musique-ancienne.org"; |
|---|
| 134 | |
|---|
| 135 | # Patch AppConfig |
|---|
| 136 | my $module=`find /usr/lib/perl5 -type f -name File.pm | grep AppConfig/File.pm`; |
|---|
| 137 | chomp($module); |
|---|
| 138 | $found = 0; |
|---|
| 139 | open(PBFILE,$module) || die "Unable to open $module"; |
|---|
| 140 | while (<PBFILE>) { |
|---|
| 141 | $found = 1 if (/Fix a bug if the variable is a hash/); |
|---|
| 142 | } |
|---|
| 143 | close(PBFILE); |
|---|
| 144 | |
|---|
| 145 | if ( $found == 0 ) { |
|---|
| 146 | chdir basename($module); |
|---|
| 147 | open(PBFILE,$module) || die "Unable to open $module"; |
|---|
| 148 | my $module1 = $module."new"; |
|---|
| 149 | open(PBOUT," > $module1") || die "Unable to open $module1"; |
|---|
| 150 | while (<PBFILE>) { |
|---|
| 151 | s/^use AppConfig;/use AppConfig qw(:argcount);/; |
|---|
| 152 | if (/# variables prefixed '-' are reset to their default values/) { |
|---|
| 153 | print PBOUT << 'EOF'; |
|---|
| 154 | # Fix a bug if the variable is a hash - 1 has been created as a key |
|---|
| 155 | if ($nargs eq AppConfig::ARGCOUNT_HASH) { |
|---|
| 156 | delete $state->{ VARIABLE }->{ $variable }{1}; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | EOF |
|---|
| 160 | } |
|---|
| 161 | print PBOUT "$_"; |
|---|
| 162 | } |
|---|
| 163 | close(PBFILE); |
|---|
| 164 | close(PBOUT); |
|---|
| 165 | rename($module1,$module); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | system "rm -rf project-builder-* ; wget ftp://ftp.mondorescue.org/src/project-builder-latest.tar.gz ; tar xvfz project-builder-latest.tar.gz ; cd project-builder-* ; perl Makefile.PL ; make ; make install ; cd .."; |
|---|
| 169 | |
|---|