#!/usr/bin/perl -w
#
# Copyright (c) 2000-2005 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
use English;
use Getopt::Std;

#
# This script will generate new Emulab keys for all users who do not
# already have them.
#

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TBAUDIT     = "@TBAUDITEMAIL@";
my $OURDOMAIN   = "@OURDOMAIN@";
my $ADDPUBKEY	= "$TB/sbin/addpubkey";

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libaudit;
use libdb;
use libtestbed;

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

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Grab all active/frozen users.
#
my $query_result =
    DBQueryFatal("select uid from users ".
		 "where webonly=0 and wikionly=0 and ".
		 "      (status='active' or status='frozen')");

#
# Note that errors are usually the result of over-quota or improper
# permissions on the .ssh directory.
# 
while (my ($uid) = $query_result->fetchrow_array()) {
    my $sshdir = USERROOT() . "/$uid";

    if (! -e $sshdir) {
	print "No home directory ($sshdir) for $uid!\n";
	next;
    }

    system("$ADDPUBKEY -i $uid");
    if ($?) {
	die("*** $0:\n".
	    "    Failed to initialize keys for $uid!\n");
    }
}