]>
git.immae.eu Git - perso/Immae/Config/Nix.git/blob - flakes/mypackages/overlays/gitolite/invite
5 use lib
$ENV{GL_LIBDIR
};
10 Please see usage at https://www.immae.eu/docs/forge-logicielle/gitolite.html#inviter-des-collaborateurs
13 usage
() if @ARGV and ($ARGV[0] eq '-h' or $ARGV[0] eq '--help');
15 my $rb = $rc{GL_REPO_BASE
};
16 my $ab = $rc{GL_ADMIN_BASE
};
20 # save arguments for later
21 my $operation = shift || 'list';
22 my $invitekeyid = shift || '';
23 $invitekeyid and $invitekeyid !~ /^[-0-9a-z_]+@[-0-9a-z_]+$/i and die "invalid keyid $invitekeyid\n";
24 my ($invited, $keyid) = split /@/, $invitekeyid;
26 # get the actual userid and keytype
27 my $gl_user = $ENV{GL_USER
};
28 die "This function is reserved for actual users" if $gl_user =~ s/-invite-(.*)$//;
31 # first collect the keys
35 for my $pubkey (`find . -type f -name "*.pub" | sort`) {
37 $pubkey =~ s
(^./)(); # artifact of the find command
40 $user =~ s
(.*/)(); # foo/bar
/baz
.pub
-> baz
.pub
41 $user =~ s/(\@[^.]+)?\.pub$//; # baz.pub, baz@home.pub -> baz
43 if ( $user =~ m
(^(zzz-marked-for-
...-)?$gl_user-invite
-) ) {
44 push @invited_keys, $pubkey;
49 # list mode; just do it and exit
51 my ( $message, @list ) = @_;
53 print "== $message ==\n";
56 my $fp = fingerprint
($_);
57 s/(zzz-marked-for-...-)?$gl_user-invite-//g;
60 print $count++ . ": $fp : $_\n";
63 if ( $operation eq 'list' ) {
64 print "you have the following invited keys:\n";
65 print_keylist
( "keys for invited persons", @invited_keys );
71 # please see docs for details on how a user interacts with this
73 die "valid operations: add, del\n" unless $operation =~ /^(add|del)$/;
75 if ( $operation eq 'add' ) {
76 print STDERR
"please supply the new key on STDIN. (I recommend you
77 don't try to do this interactively, but use a pipe)\n";
78 kf_add
( $gl_user, $invited, $keyid, safe_stdin
() );
79 } elsif ( $operation eq 'del' ) {
80 kf_del
( $gl_user, $invited, $keyid );
87 # make a temp clone and switch to it
89 BEGIN { $TEMPDIR = `mktemp -d -t tmp.XXXXXXXXXX`; }
90 END { `rm -rf $TEMPDIR`; }
94 hushed_git( "clone", "$rb/gitolite-admin.git", "$TEMPDIR" );
96 my $hostname = `hostname
`; chomp($hostname);
97 hushed_git( "config", "--get", "user.email" ) and hushed_git( "config", "user.email", $ENV{USER} . "@" . $hostname );
98 hushed_git( "config", "--get", "user.name" ) and hushed_git( "config", "user.name", "$ENV{USER} on $hostname" );
102 my ($fp, $output) = ssh_fingerprint_file(shift);
103 # Do not print the output of $output to an untrusted destination.
104 die "does not seem to be a valid pubkey\n" unless $fp;
109 # read one line from STDIN
111 my $ret = read STDIN, $data, 4096;
112 # current pubkeys are approx 400 bytes so we go a little overboard
113 die "could not read pubkey data" . ( defined($ret) ? "" : ": $!" ) . "\n" unless $ret;
114 die "pubkey data seems to have more than one line\n" if $data =~ /\n./;
119 local (*STDOUT) = \*STDOUT;
120 local (*STDERR) = \*STDERR;
121 open( STDOUT, ">", "/dev/null" );
122 open( STDERR, ">", "/dev/null" );
127 # there can be only one
128 my ( $keyid, $die_if_empty, @a ) = @_;
132 more than one key satisfies this condition, and I can't deal with that!
136 print STDERR "\t" . join( "\n\t", @a ), "\n\n";
140 die "no keys with " . ( $keyid || "empty" ) . " keyid found\n" if $die_if_empty and not @a;
146 my ( $gl_user, $invited, $keyid, $keymaterial ) = @_;
148 # add a new "invited" key for $gl_user.
153 _print( "invited/$gl_user-invite-$invited\@$keyid.pub", $keymaterial );
154 hushed_git( "add", "." ) and die "git add failed\n";
155 my $fp = fingerprint("invited/$gl_user-invite-$invited\@$keyid.pub");
156 hushed_git( "commit", "-m", "invite add $gl_user-invite-$invited\@$keyid ($fp)" ) and die "git commit failed\n";
157 system("gitolite push >/dev/null 2>/dev/null") and die "git push failed\n";
161 my ( $gl_user, $invited, $keyid ) = @_;
166 my @pk = highlander( $keyid, 1, grep { m(^(.*/)?(zzz-marked-for-...-)?$gl_user-invite-$invited\@$keyid.pub$) } @invited_keys );
168 my $fp = fingerprint( $pk[0] );
169 hushed_git( "rm", $pk[0]) and die "git mv failed\n";
170 hushed_git( "commit", "-m", "invite del $pk[0] ($fp)" ) and die "git commit failed\n";
171 system("gitolite push >/dev/null 2>/dev/null") and die "git push failed\n";