[LON-CAPA-cvs] cvs: modules /msu kerbswitch.pl

raeburn raeburn at source.lon-capa.org
Sun Mar 11 14:46:51 EDT 2012


raeburn		Sun Mar 11 18:46:51 2012 EDT

  Added files:                 
    /modules/msu	kerbswitch.pl 
  Log:
  - Custom script for MSU to convert authtype from krb4 to krb5
    - Support for Kerberos 4 will be discontinued by central IT in May 2012.
  
  

Index: modules/msu/kerbswitch.pl
+++ modules/msu/kerbswitch.pl
#!/usr/bin/perl

#
# kerbswitch.pl
#
# Script to change krb4 to krb5 in:
# (a) /home/httpd/lonUsers/msu/x/y/z/xyzmsuid/passwd for MUSNetIDs
#     while preserving existing atime and mtime for passwd file
# (b) internal.authtype in environment.db for:
#     (i) Courses
#     (ii) Communities  
# 
# Stuart Raeburn March 11th, 2012
# $Id: kerbswitch.pl,v 1.1 2012/03/11 18:46:51 raeburn Exp $ 
#

use strict;
use lib '/home/httpd/lib/perl';
use Apache::loncommon;
use Apache::lonnet;

my @domains = sort(&Apache::lonnet::current_machine_domains());
my @hostids = &Apache::lonnet::current_machine_ids();
my ($logfh,$othfh);
if (open($logfh,">>krb4update.txt")) {
    if (open($othfh,">>nonmsunetid.txt")) {
        foreach my $dom (@domains) {
            my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
            my %courses = 
                &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@hostids,'Course');
            my %communities =
                &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@hostids,'Community');
            print $logfh "Domain: $dom\n"; 
            &descend_tree($dom,$dir,0,$logfh,$othfh,\%courses,\%communities);
            &update_authtype($dom,\%courses,$logfh);
            &update_authtype($dom,\%communities,$logfh);
        }
        close($othfh);
    }
    close($logfh);
}

sub update_authtype {
    my ($dom,$courses,$logfh) = @_;
    return unless (ref($courses) eq 'HASH');
    foreach my $course (sort(keys(%{$courses}))) {
        my %args = (
                       one_time => 1,
                   );
        my %settings = &Apache::lonnet::coursedescription($course,\%args);
        my $cnum = $settings{'num'};
        next if ($settings{'domain'} ne $dom);
        my %newenv;
        if ($settings{'internal.authtype'} eq 'krb4') {
            $newenv{'internal.authtype'} = 'krb5';
            if (&Apache::lonnet::put('environment',\%newenv,$dom,$cnum) eq 'ok') {
                print $logfh "$cnum updated from $settings{'internal.authtype'} to $
newenv{'internal.authtype'}\n";
            } else {
                print "Failed to store internal.authtype for $cnum\n";
            }
        }
    }
    return;
}

sub descend_tree {
    my ($dom,$dir,$depth,$logfh,$othfh,$courses,$communities) = @_;
    if (-d $dir) {
        opendir(DIR,$dir);
        my @contents = grep(!/^\./,readdir(DIR));
        closedir(DIR);
        $depth ++;
        foreach my $item (sort(@contents)) {
            if ($depth < 4) {
                &descend_tree($dom,$dir.'/'.$item,$depth,$logfh,$othfh,$courses,
                              $communities);
            } else {
                next if (exists($courses->{$dom.'_'.$item}));
                next if (exists($communities->{$dom.'_'.$item}));
                if ($item !~ /^\w{2,8}$/) {
                    my $file = "$dir/$item/passwd";
                    if (-e $file) { 
                        print $othfh "$item not an MSUNetID\n";
                    }
                    next;
                }
                my $file = "$dir/$item/passwd";
                if (-e $file) {
                    my $contents;
                    my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
                        $atime,$mtime,$ctime,$blksize,$blocks) = stat($file);
                    if (open(my $fh,"<$file")) {
                        $contents = <$fh>;
                        close($fh);
                    }
                    if ($contents =~ /^krb4:MSU.EDU$/) {
                        if (open(my $ofh,">$file")) {
                            print $ofh 'krb5:MSU.EDU';
                            close($ofh);
                            if ($mtime) {
                                my $savedmod = &convertdate($mtime);
                                if (system("touch -m -t $savedmod $file")) {
                                    print "touch for mtime $savedmod failed for $file\n";
                                }
                            }
                            if ($atime) {
                                my $savedaccess =  &convertdate($atime);
                                if (system("touch -a -t $savedaccess $file") != 0) {
                                    print "touch for atime $savedaccess failed for $file\n"; 
                                }
                            }
                            print $logfh "$item\n";
                        } else {
                            print "Could not open $file for writing\n";
                        }
                    }
                } else {
                    print "Missing file for $file\n";
                }
            }
        }
    }
    return;
}

sub convertdate {
    my ($unixtime) = @_;
    my (@dat) = localtime($unixtime);
    $dat[5] += 1900;
    $dat[4] ++;
    for (my $i=0;$i<5; $i++) {
        if (length($dat[$i]) == 1) {
            $dat[$i] = '0'.$dat[$i]; 
        }
    }
    return $dat[5].$dat[4].$dat[3].$dat[2].$dat[1].'.'.$dat[0];
}





More information about the LON-CAPA-cvs mailing list