summaryrefslogtreecommitdiff
path: root/usr/local/sbin/addaccess
diff options
context:
space:
mode:
Diffstat (limited to 'usr/local/sbin/addaccess')
-rwxr-xr-xusr/local/sbin/addaccess39
1 files changed, 39 insertions, 0 deletions
diff --git a/usr/local/sbin/addaccess b/usr/local/sbin/addaccess
new file mode 100755
index 0000000..07cc893
--- /dev/null
+++ b/usr/local/sbin/addaccess
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+my( $reponame, $uid, $gitowner);
+
+if ( ( ! scalar $ARGV[1] ) or ( scalar $ARGV[2] ) ) { # must have exactly two arguments
+ print( "Usage: addaccess USER REPONAME\n" );
+ exit( 1 );
+} elsif ( ( $ARGV[0] !~ /^[a-zA-Z0-9_.][a-zA-Z0-9_.-]+$/ ) or ( $ARGV[1] !~ /^[a-z0-9][a-z0-9+.-]+$/ ) ) { # Extra paranoid sanity checking
+ print( "Usage: addaccess USER REPONAME\n" );
+ print( " USER must consist only of letters, digits, underscores, periods, and dashes, and not start with a dash.\n" );
+ print( " REPONAME must consist only of lower case letters (a-z), digits (0-9), plus (+) and minus (-) signs, and periods (.).\n" );
+ print( " REPONAME must be at least two characters long and must start with an alphanumeric character.\n" );
+ exit( 1 );
+} else {
+ $reponame = $ARGV[1];
+};
+
+open (PASSWD, '/etc/passwd');
+while(<PASSWD>) {
+ if( $_ =~ /^$ENV{SUDO_USER}:.+?:(.+?):/ ) { $uid = $1; }; # grabbing uid.
+};
+close(PASSWD);
+
+unless( -d "/srv/git/$reponame.git" ) {
+ print( "/srv/git/$reponame.git doesn't exist!\n" );
+ exit( 2 );
+};
+
+$gitowner = (stat( "/srv/git/$reponame.git" ))[4]; # grab owner uid of repository
+
+if( ( $gitowner != $uid ) and ( $gitowner != 65534 ) ) {
+ print( "$reponame is not owned by you!\n" );
+ exit( 3 );
+};
+
+system( "/usr/sbin/adduser $ARGV[0] git-$reponame" );