package bootsplash; use common; use Xconfig::resolution_and_depth; my $themes_dir = "/usr/share/bootsplash/themes"; my $themes_config_dir = "/etc/bootsplash/themes"; my $sysconfig_file = "/etc/sysconfig/bootsplash"; my $bootsplash_scripts = "/usr/share/bootsplash/scripts"; my $default_theme = 'Mandrivalinux'; our $default_thumbnail = '/usr/share/libDrakX/pixmaps/nosplash_thumb.png'; our @resolutions = uniq(map { "$_->{X}x$_->{Y}" } Xconfig::resolution_and_depth::bios_vga_modes()); sub get_framebuffer_resolution() { require bootloader; require fsedit; my $all_hds = fsedit::get_hds(); fs::get_info_from_fstab($all_hds); my $bootloader = bootloader::read($all_hds); my $x_res = Xconfig::resolution_and_depth::from_bios($bootloader->{default_options}{vga}); $x_res ? ($x_res->{X} . 'x' . $x_res->{Y}, 1) : (first(@resolutions), 0); } sub themes_read_sysconfig { my ($res) = @_; my %theme = ( name => $default_theme, enabled => 1, keep_logo => 1 ); if (-r $::prefix . $sysconfig_file) { local $_; foreach (cat_($::prefix . $sysconfig_file)) { /^SPLASH=no/ and $theme{enabled} = 0; /^THEME=(.*)/ && -f theme_get_image_for_resolution($1, $res) and $theme{name} = $1; /^LOGO_CONSOLE=(.*)/ and $theme{keep_logo} = $1 ne "no"; } } \%theme; } sub theme_get_image_for_resolution { my ($theme, $res) = @_; $::prefix . $themes_dir . '/' . $theme . '/images/bootsplash-' . $res . ".jpg"; } sub theme_get_config_for_resolution { my ($theme, $res) = @_; $::prefix . $themes_config_dir . '/' . $theme . '/config/bootsplash-' . $res . ".cfg"; } sub theme_exists_for_resolution { my ($theme, $res) = @_; -f theme_get_image_for_resolution($theme, $res) && -f theme_get_config_for_resolution($theme, $res); } sub themes_list() { grep { !/^\./ && -d $::prefix . $themes_dir . '/' . $_ } sort(all($::prefix . $themes_dir)); } sub themes_list_for_resolution { my ($res) = @_; grep { theme_exists_for_resolution($_, $res) } themes_list(); } sub switch { my ($theme) = @_; if ($::testing) { print "enabling bootsplash theme $theme\n"; } else { #- theme scripts will update SPLASH value in sysconfig file system($::prefix . $bootsplash_scripts . '/switch-themes', $theme); } } sub remove() { if ($::testing) { print "disabling bootsplash theme\n"; } else { system($::prefix . $bootsplash_scripts . '/remove-theme'); } } sub set_logo_console { my ($keep_logo) = @_; my $logo_console = $keep_logo ? 'theme' : 'no'; substInFile { s/^LOGO_CONSOLE=.*/LOGO_CONSOLE=$logo_console/ } $::prefix . $sysconfig_file; } sub create_path { my ($file) = @_; mkdir_p(dirname($file)); } sub theme_set_image_for_resolution { my ($name, $res, $source_image) = @_; my $dest_image = theme_get_image_for_resolution($name, $res); create_path($dest_image); #- Append an exclamation point to the geometry to force the image size to exactly the size you specify. system('convert', '-geometry', $res . '!', $source_image, $dest_image); system($::prefix . $bootsplash_scripts . '/rewritejpeg', $dest_image); } sub theme_read_config_for_resolution { my ($theme, $res) = @_; my $file = theme_get_config_for_resolution($theme, $res); my $contents = cat_($file); my ($pb_x1, $pb_y1, $pb_x2, $pb_y2, $pbg_c, $ptransp) = $contents =~ /^box\s+silent\s+noover\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(#\w{6})(\w{2})/m; my ($tb_x1, $tb_y1, $tb_x2, $tb_y2, $tc, $transp) = $contents =~ /^box\s+noover\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(#\w{6})(\w{2})/m; my ($pc1, $pc2, $pc3, $_pc4) = $contents =~ /^box\s+silent\s+inter\s+\d+\s+\d+\s+\d+\s+\d+\s+(#\w+)\s+(#\w+)\s+(#\w+)\s+(#\w+)/m; my ($text_color) = $contents =~ /^text_color=0x(\w+)/m; my $gradient; if ($pc1 eq $pc2) { $gradient = 'vertical'; $pc2 = $pc3; } { pc1 => $pc1, pc2 => $pc2, gradient => $gradient, transp => hex $transp, ptransp => hex $ptransp, tb_x => $tb_x1, tb_y => $tb_y1, tb_w => $tb_x2 - $tb_x1, tb_h => $tb_y2 - $tb_y1, tc => $tc, pbg_c => $pbg_c, px => $pb_x1, pw => $pb_x2 - $pb_x1, py => $pb_y1, ph => $pb_y2 - $pb_y1, getVarsFromSh($file), text_color => "#$text_color" }; } sub theme_write_config_for_resolution { my ($name, $res, $conf) = @_; my $config = theme_get_config_for_resolution($name, $res); create_path($config); my $jpeg = theme_get_image_for_resolution($name, $res); # progress/text rectangles border/inter coordinates my ($pb_x1, $pb_x2, $pb_y1, $pb_y2) = ($conf->{px}, $conf->{px} + $conf->{pw}, $conf->{py}, $conf->{py} + $conf->{ph}); my ($pi_y1, $pi_y2) = ($pb_y1 + 1, $pb_y2 - 1); my ($tb_x1, $tb_y1, $tb_x2, $tb_y2) = ($conf->{tb_x}, $conf->{tb_y}, $conf->{tb_x} + $conf->{tb_w}, $conf->{tb_y} + $conf->{tb_h}); my ($tx, $ty, $tw, $th) = ($tb_x1 + 10, $tb_y1 + 5, $conf->{tb_w} - 20 , $conf->{tb_h} - 10); my ($ti_x1, $ti_x2, $ti_y1, $ti_y2) = ($tb_x1 - 1, $tb_x2 + 1, $tb_y1 + 1, $tb_y2 + 1); my ($pc1, $pc2, $pc3, $pc4); if ($conf->{gradient} eq 'vertical') { ($pc1, $pc2, $pc3, $pc4) = ($conf->{pc1}, $conf->{pc1}, $conf->{pc2}, $conf->{pc2}); } else { ($pc1, $pc2, $pc3, $pc4) = ($conf->{pc1}, $conf->{pc2}, $conf->{pc1}, $conf->{pc2}); } if (!$pc1) { ($pc1, $pc2, $pc3, $pc4) = ('#ffffff', '#ffffff', '#000000', '#000000') } my $ptransp = sprintf '%02x', $conf->{ptransp}; my $transp = sprintf '%02x', $conf->{transp}; $conf->{pbg_c} ||= '#aaaaaa'; $conf->{tc} ||= '#ffffff'; my $text_color = $conf->{text_color} ? "0x$conf->{text_color}" : '0xaaaaaa'; $text_color =~ s/#//; output($config, qq(# This is the configuration file for the $res bootsplash picture # this file is necessary to specify the coordinates of the text box on the # splash screen. # config file version version=3 # should the picture be displayed? state=1 # fgcolor is the text forground color. # bgcolor is the text background (i.e. transparent) color. fgcolor=$conf->{fgcolor} bgcolor=$conf->{bgcolor} # (tx, ty) are the (x, y) coordinates of the text window in pixels. # tw/th is the width/height of the text window in pixels. tx=$tx ty=$ty tw=$tw th=$th # ttf message output parameters text_x=$conf->{text_x} text_y=$conf->{text_y} text_size=$conf->{text_size} text_color=$text_color # name of the picture file (full path recommended) jpeg=$jpeg silentjpeg=$jpeg progress_enable=1 # background # b(order) or i(nter) box silent noover $pb_x1 $pb_y1 $pb_x2 $pb_y2 $conf->{pbg_c}$ptransp # progress bar box silent inter $pb_x1 $pi_y1 $pb_x1 $pi_y2 $pc1 $pc2 $pc3 $pc4 box silent $pb_x1 $pi_y1 $pb_x2 $pi_y2 $pc1 $pc2 $pc3 $pc4 # black border (top, bottom, left, right) box silent $pb_x1 $pb_y1 $pb_x2 $pb_y1 #313234 box silent $pb_x1 $pb_y2 $pb_x2 $pb_y2 #889499 box silent $pb_x1 $pb_y1 $pb_x1 $pb_y2 #313234 box silent $pb_x2 $pb_y1 $pb_x2 $pb_y2 #889499 # text box box noover $tb_x1 $tb_y1 $tb_x2 $tb_y2 $conf->{tc}$transp # black border (top, bottom, left, right) box $ti_x1 $tb_y1 $ti_x1 $ti_y2 #313234 box $tb_x1 $tb_y1 $ti_x2 $tb_y1 #313234 box $ti_x2 $ti_y1 $ti_x2 $ti_y2 #889499 box $tb_x1 $ti_y2 $ti_x2 $ti_y2 #889499 overpaintok=1 LOGO_CONSOLE=$conf->{LOGO_CONSOLE} )); } sub rectangle2xywh { my ($rect) = @_; my $x = min($rect->[0]{X} , $rect->[1]{X}); my $y = min($rect->[0]{Y} , $rect->[1]{Y}); my $w = abs($rect->[0]{X} - $rect->[1]{X}); my $h = abs($rect->[0]{Y} - $rect->[1]{Y}); ($x, $y, $w, $h); } sub xywh2rectangle { my ($x, $y, $w, $h) = @_; [ { X => $x, Y => $y }, { X => $x+$w, Y => $y+$h } ]; } sub distance { my ($p1, $p2) = @_; sqr($p1->{X} - $p2->{X}) + sqr($p1->{Y} - $p2->{Y}); } sub farthest { my ($point, @others) = @_; my $dist = 0; my $farthest; foreach (@others) { my $d = distance($point, $_); if ($d >= $dist) { $dist = $d; $farthest = $_; } } $farthest; } sub nearest { my ($point, @others) = @_; my $dist; my $nearest; foreach (@others) { my $d = distance($point, $_); if (! defined $dist || $d < $dist) { $dist = $d; $nearest = $_; } } $nearest; } 1; ='n68' href='#n68'>68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
package authentication; # $Id$

use common;
use any;


sub kinds() { 
    ('local', 'LDAP', 'NIS', 'winbind');
}
sub kind2description {
    my ($kind) = @_;
    ${{ local => N("Local files"), LDAP => N("LDAP"), NIS => N("NIS"), winbind => N("Windows Domain") }}{$kind};
}
sub to_kind {
    my ($authentication) = @_;
    (find { exists $authentication->{$_} } kinds()) || 'local';
}

sub ask_parameters {
    my ($in, $netc, $authentication, $kind) = @_;

    #- keep only this authentication kind
    foreach (kinds()) {
	delete $authentication->{$_} if $_ ne $kind;
    }

    my $val = $authentication->{$kind} ||= '';

    if ($kind eq 'LDAP') {
	$val ||= 'ldap.' . $netc->{DOMAINNAME};
	$netc->{LDAPDOMAIN} ||= join(',', map { "dc=$_" } split /\./, $netc->{DOMAINNAME});
	$in->ask_from('',
		     N("Authentication LDAP"),
		     [ { label => N("LDAP Base dn"), val => \$netc->{LDAPDOMAIN} },
		       { label => N("LDAP Server"), val => \$val },
		     ]) or return;
    } elsif ($kind eq 'NIS') { 
	$val ||= 'broadcast';
	$in->ask_from('',
		     N("Authentication NIS"),
		     [ { label => N("NIS Domain"), val => \ ($netc->{NISDOMAIN} ||= $netc->{DOMAINNAME}) },
		       { label => N("NIS Server"), val => \$val, list => ["broadcast"], not_edit => 0 },
		     ]) or return;
    } elsif ($kind eq 'winbind') {
	#- maybe we should browse the network like diskdrake --smb and get the 'doze server names in a list 
	#- but networking isn't setup yet necessarily
	$in->ask_warn('', N("For this to work for a W2K PDC, you will probably need to have the admin run: C:\\>net localgroup \"Pre-Windows 2000 Compatible Access\" everyone /add and reboot the server.\nYou will also need the username/password of a Domain Admin to join the machine to the Windows(TM) domain.\nIf networking is not yet enabled, Drakx will attempt to join the domain after the network setup step.\nShould this setup fail for some reason and domain authentication is not working, run 'smbpasswd -j DOMAIN -U USER%%PASSWORD' using your Windows(tm) Domain, and Admin Username/Password, after system boot.\nThe command 'wbinfo -t' will test whether your authentication secrets are good."));
	$in->ask_from('',
			N("Authentication Windows Domain"),
			[ { label => N("Windows Domain"), val => \ ($netc->{WINDOMAIN} ||= $netc->{DOMAINNAME}) },
			  { label => N("Domain Admin User Name"), val => \$val },
			  { label => N("Domain Admin Password"), val => \$authentication->{winpass}, hidden => 1 },
			]) or return;
    }
    $authentication->{$kind} = $val;
    1;
}

sub set {
    my ($in, $netc, $authentication, $when_network_is_up) = @_;

    any::enableShadow() if $authentication->{shadow};    

    my $kind = authentication::to_kind($authentication);
    my $val = $authentication->{$kind};

    log::l("authentication::set $kind with $val");

    if ($kind eq 'LDAP') {
	$in->do_pkgs->install(qw(openldap-clients nss_ldap pam_ldap autofs));

	my $domain = $netc->{LDAPDOMAIN} || do {
	    my $s = run_program::rooted_get_stdout($::prefix, 'ldapsearch', '-x', '-h', $val, '-b', '', '-s', 'base', '+');
	    first($s =~ /namingContexts: (.+)/);
	} or log::l("no ldap domain found on server $val"), return;

	set_nsswitch_priority('ldap');
	set_pam_authentication('ldap');
	set_ldap_conf($domain, $val, 1);
    } elsif ($kind eq 'NIS') {
	$in->do_pkgs->install(qw(ypbind autofs));
	my $domain = $netc->{NISDOMAIN};
	$domain || $val ne "broadcast" or die N("Can't use broadcast with no NIS domain");
	my $t = $domain ? "domain $domain" . ($val ne "broadcast" && " server") : "ypserver";
	substInFile {
	    $_ = "#~$_" unless /^#/;
	    $_ .= "$t $val\n" if eof;
	} "$::prefix/etc/yp.conf";

	set_nsswitch_priority('nis');
	#- no need to modify system-auth for nis

	$when_network_is_up->(sub {
	    run_program::rooted($::prefix, 'nisdomainname', $domain);
	    run_program::rooted($::prefix, 'service', 'ypbind', 'restart');
	}) if !$::isInstall; #- TODO: also do it during install since nis can be useful to resolve domain names. Not done because 9.2-RC
    } elsif ($kind eq 'winbind') {
	my $domain = $netc->{WINDOMAIN};
	$domain =~ tr/a-z/A-Z/;

	$in->do_pkgs->install(qw(samba-winbind samba-common));
	set_pam_authentication('winbind');

	require network::smb;
	network::smb::write_smb_conf($domain);
	run_program::rooted($::prefix, "chkconfig", "--level", "35", "winbind", "on");
	mkdir_p("$::prefix/home/$domain");
	
	#- defer running smbpassword until the network is up
	$when_network_is_up->(sub {
	    run_program::rooted($::prefix, 'smbpasswd', '-j', $domain, '-U', $val . '%' . $authentication->{winpass});
	});
    }
}


sub pam_modules() {
    'pam_ldap', 'pam_winbind', 'pam_mkhomedir';
}
sub pam_module_from_path { 
    $_[0] && $_[0] =~ m|(/lib/security/)?(pam_.*)\.so| && $2;
}
sub pam_module_to_path { 
    "$_[0].so";
}
sub pam_format_line {
    my ($type, $control, $module, @para) = @_;
    sprintf("%-11s %-13s %s\n", $type, $control, join(' ', pam_module_to_path($module), @para));
}

sub get_raw_pam_authentication() {
    my %before_deny;
    foreach (cat_("$::prefix/etc/pam.d/system-auth")) {
	my ($type, $control, $module, @para) = split;
	if ($module = pam_module_from_path($module)) {
	    $before_deny{$type}{$module} = \@para if $control eq 'sufficient' && member($module, pam_modules());
	}
    }
    \%before_deny;
}

sub set_raw_pam_authentication {
    my ($before_deny, $before_first) = @_;
    substInFile {
	my ($type, $control, $module, @para) = split;
	my $added_pre_line = '';
	if ($module = pam_module_from_path($module)) {
	    if ($module eq 'pam_unix' && member($type, 'auth', 'account')) {
		#- remove likeauth, nullok and use_first_pass
		$_ = pam_format_line($type, 'sufficient', $module, grep { !member($_, qw(likeauth nullok use_first_pass)) } @para);
		if ($control eq 'required') {
		    #- ensure a pam_deny line is there
		    ($control, $module, @para) = ('required', 'pam_deny');
		    ($added_pre_line, $_) = ($_, pam_format_line($type, $control, $module));
		}
	    }
	    if (member($module, pam_modules())) {
		#- first removing previous config
		warn "dropping line $_";
		$_ = '';
	    } else {
		if ($before_first->{$type}) {
		    foreach my $module (keys %{$before_first->{$type}}) {
			$_ = pam_format_line($type, 'required', $module, @{$before_first->{$type}{$module}}) . $_;
		    }
		    delete $before_first->{$type};
		}		
		if ($control eq 'required' && $module eq 'pam_deny') {
		    if ($before_deny->{$type}) {
			foreach my $module (keys %{$before_deny->{$type}}) {
			    $_ = pam_format_line($type, 'sufficient', $module, @{$before_deny->{$type}{$module}}) . $_;
			}
		    }
		}
	    }
	    $_ = $added_pre_line . $_;
	}
    } "$::prefix/etc/pam.d/system-auth";
}

sub get_pam_authentication_kinds() {
    my $before_deny = get_raw_pam_authentication();
    map { s/pam_//; $_ } keys %{$before_deny->{auth}};
}

sub set_pam_authentication {
    my (@authentication_kinds) = @_;
    my $before_deny = {};
    my $before_first = {};
    foreach (@authentication_kinds) {
	my $module = 'pam_' . $_;
	$before_deny->{auth}{$module} = [ 'likeauth', 'nullok', 'use_first_pass' ];
	$before_deny->{account}{$module} = [ 'use_first_pass' ];
	$before_deny->{password}{$module} = [] if $_ eq 'ldap';
	$before_first->{session}{pam_mkhomedir} = [ 'skel=/etc/skel/', 'umask=0022' ] if $_ eq 'winbind';
    }
    set_raw_pam_authentication($before_deny, $before_first);
}

sub set_nsswitch_priority {
    my (@kinds) = @_;
    # allowed: files nis ldap dns
    substInFile {
	if (my ($database, $l) = /^(\s*(?:passwd|shadow|group|automount):\s*)(.*)/) {
	    $_ = $database . join(' ', uniq('files', @kinds, split(' ', $l))) . "\n";
	}	
    } "$::prefix/etc/nsswitch.conf";
}

sub read_ldap_conf() {
    my %conf = map { s/^\s*#.*//; if_(/(\S+)\s+(.*)/, $1 => $2) } cat_("$::prefix/etc/ldap.conf");
    \%conf;
}

sub set_ldap_conf {    
    my ($domain, $servers, $b_ssl) = @_;

    my %wanted_conf = (
	host => $servers,
	base => $domain,
	port => $b_ssl ? 636 : 389,
	ssl => $b_ssl ? 'on' : 'off',
	nss_base_shadow => "ou=People,$domain",
	nss_base_passwd => "ou=People,$domain",
	nss_base_group => "ou=Group,$domain",
    );

    substInFile {
	my ($cmd) = /^#?\s*(\w+)\s/;
	if ($cmd && exists $wanted_conf{$cmd}) {
	    my $val = $wanted_conf{$cmd};
	    $wanted_conf{$cmd} = '';
	    $_ = $val ? "$cmd $val\n" : '';
        }
    } "$::prefix/etc/ldap.conf";
}

1;