apt_gitrepo.hook.in 4.12 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 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
#!/usr/bin/perl -w
#
# Copyright (c) 2008-2017 University of Utah and the Flux Group.
# 
# {{{GENIPUBLIC-LICENSE
# 
# GENI Public License
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
# 
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;
use Data::Dumper;
use JSON;
use CGI;

# Configure variables
my $TB               = "@prefix@";
my $MAINSITE 	     = @TBMAINSITE@;
my $TBOPS            = "@TBOPSEMAIL@";
my $MANAGEPROFILE    = "$TB/bin/manage_profile";

# Locals
my $debug      = 0;
my $token;

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

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

# Testbed libraries.
use lib '@prefix@/lib';
use emutil;
use libaudit;
use libtestbed;
use APT_Profile;
use User;
use Group;

#
# We want to make sure we send back a header.
#
sub SendStatus($)
{
    my ($status) = @_;
    
    if (1) {
	# Error, we are done.
	LogEnd($status);
    }
    else {
	# We want to keep logging, not send an email here.
	LogStop();
    }
    print "Content-Type: text/plain \n\n";
    print "We love all profiles equally.\n";
    print "Exited with $status\n";
    exit(0);    
}

#
# Send logs to tblogs
#
LogStart(0, undef, LIBAUDIT_LOGTBLOGS());

# The query holds the token we need to find the profile.
my $query = new CGI();

if ($debug > 1) {
    my %headers = map { $_ => $query->http($_) } $query->http();
    print STDERR Dumper(\%headers);
    print STDERR Dumper($query);
}

#
# The profile is provided in the path.
#
if (!exists($ENV{'PATH_INFO'}) || $ENV{'PATH_INFO'} eq "") {
    print STDERR "No path info\n";
    SendStatus(1);
}
my $pathinfo = $ENV{'PATH_INFO'};
if ($pathinfo =~ /^\/([-\w]+)$/) {
    $token = $1;
}
else {
    print STDERR "Bad path info\n";
    SendStatus(1);
}
if (!defined($token)) {
    print STDERR "No token provided\n";
    SendStatus(1);
}
if ($token !~ /^[-\w]+$/ || length($token) > 64) {
    print STDERR "Bad token format\n";
    SendStatus(1);
}
#
# Before calling out, find the profile.
#
my $profile = APT_Profile->LookupByRepoKey($token);
if (!defined($profile)) {
    print STDERR "No profile for token $token\n";
    SendStatus(1);
}
if (!defined($profile->repourl())) {
    print STDERR "Not a repo based profile for token $token\n";
    SendStatus(1);
}
#
# Let the parent return, no need to keep the client waiting since it
# don't care anyway.
#
my $mypid = fork();
if ($mypid) {
    #
    # Must end with this for the client. Does not return;
    #
    SendStatus(0);
}
sleep(1);
libaudit::AuditFork();

if ($debug) {
    print "$profile\n";
}

#
# We are going to do the update as the profile creator.
#
my $creator = User->Lookup($profile->creator_idx());
if (!defined($creator)) {
    print STDERR "Cannot lookup creator for $profile\n";
    exit(1);
}
my $group = Group->Lookup($profile->gid_idx());
if (!defined($group)) {
    print STDERR "Cannot lookup group for $profile\n";
    exit(1);
}
if ($creator->FlipTo($group)) {
    print STDERR "Could not flip to $creator\n";
    exit(1);
}
my $output =
    emutil::ExecQuiet("$MANAGEPROFILE updatefromrepo " . $profile->uuid());
if ($?) {
    print STDERR $output;
    exit(1);
}
if ($debug > 1) {
    print $output;
}
LogEnd(0);
exit(0);