sfskey_update.proxy.in 2.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use Getopt::Std;

#
# Manage the sfs_users files remotely from boss. The keys to change are
# passed in via stdin. Must use locking of course.
#
sub usage()
{
    print "Usage: sfskey_update.proxy\n";
    exit(-1);
}
my  $optlist = "";

#
# Configure variables
#
my $TB      = "@prefix@";
my $TBOPS   = "@TBOPSEMAIL@";

#
# Locals
#
my $tmpkey  = "/var/tmp/sfs_users";
my $tmppub  = "/var/tmp/sfs_users.pub";
my $realkey = "/etc/sfs/sfs_users";
my $realpub = "/etc/sfs/sfs_users.pub";
my @newkeys = ();

# un-taint path
$ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

# Turn off line buffering on output
$| = 1; 

#
# We don't want to run this script unless its the real version.
#
if ($UID != 0) {
    die("*** $0:\n".
	"    Must be root! Maybe the wrong version?\n");
}

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV != 0) {
    usage();
}

#
# Take in our input and stash it. First field is the unique key.
#
while (<STDIN>) {
    chomp();
    push(@newkeys, $_);
}

if (! @newkeys) {
    print STDERR "No keys! Exiting ...\n";
    exit(0);
}

#
# Create two temp files, one for the key file and one for the pub file.
#
system("rm -f $tmpkey $tmppub") == 0
    or fatal("Could not remove old tmp files");
	     
my $oldmask = umask(0077);
open(OUTKEY, "> $tmpkey")
    or fatal("Could not open $tmpkey: $!");
umask(0022);
open(OUTPUB, "> $tmppub")
    or fatal("Could not open $tmppub: $!");
umask($oldmask);

print OUTKEY "#\n";
print OUTKEY "# This file is autogenerated from boss. DO NOT EDIT!\n";
print OUTKEY "#\n";
print OUTPUB "#\n";
print OUTPUB "# This file is autogenerated from boss. DO NOT EDIT!\n";
print OUTPUB "#\n";

#
# Parse each input line, separating the various sections (by colon).
#
foreach my $key ( @newkeys ) {
    if (! ($key =~ /^(.*):(.*):(.*):(.*):(.*)$/)) {
	print STDERR "Bad key line: $key\n";
	next;
    }

    print OUTKEY "$1:$2:$3:$4:$5\n";
    print OUTPUB "$1:$2:$3::\n";
}
close(OUTKEY)
    or fatal("Error closing $tmpkey: $!");
close(OUTPUB)
    or fatal("Error closing $tmppub: $!");

#
# Back up old ones and move new ones into place.
#
system("cp -pf $realkey ${realkey}.old") == 0
    or fatal("Could not backup $realkey");
system("cp -pf $realpub ${realpub}.old") == 0
    or fatal("Could not backup $realpub");
system("mv -f $tmpkey $realkey") == 0
    or fatal("Could not install $realkey");
system("mv -f $tmppub $realpub") == 0
    or fatal("Could not install $realpub");

exit(0);

#
# Caller will catch error and send email.
# 
sub fatal {
    local($msg) = $_[0];

    die("*** $0:\n".
	"    $msg\n");
}