git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alex Vandiver <alex@chmrr.net>
To: git <git@vger.kernel.org>
Subject: Re: avoiding anonymous commits from root/shared accounts
Date: Mon, 10 May 2010 17:11:23 -0400	[thread overview]
Message-ID: <1273525498-sup-6628@utwig> (raw)
In-Reply-To: <4BE83CCD.2090505@letterboxes.org>

At Mon May 10 13:05:17 -0400 2010, Nick wrote:
> [snip]
> The best idea I've come across seems to be some sort of wrapper for git, which
> if no $GIT_USER_* is defined, can use $SUDO_USER and/or `who am i` to identify
> the original log-in account, and sets $GIT_AUTHOR_NAME etc. - else if it can't
> do this, it refuses to commit.  Or perhaps it would be a script which spawns a
> shell with the right environment to invoke git commands from, after successfully
> determining the identity.

At work, we have a number of repositories which we store server
configurations in, most of which are only writable as root.  We use
the script below to ensure that git mostly doesn't lie about the
authors of commits.  This won't solve your problem of people logging
in under shared credentials -- and it also _does_ allow commits as
'root' if you logged in directly as root -- but it's perhaps a partial
solution for you.

 - Alex

-------------------->8--------------------
#!/usr/bin/perl

use strict;
use warnings;
use constant EMAIL_DOMAIN => "example.com";

setenv( get_user($$) );
exec("/usr/bin/git", @ARGV);

sub setenv {
    my $user = shift;

    # If they're _really_ _really_ root, just bail now
    return if $user eq "root";

    # Ditto if we can't find the user (?!)
    my @getpw = getpwnam($user);
    return unless @getpw;

    my $name;
    my $email;

    # See if we can pull from the user's config
    my $gitconfig = "$getpw[7]/.gitconfig";
    if (-r $gitconfig) {
        $name  = `/usr/bin/git config --file $gitconfig user.name`;
        chomp $name;
        $email = `/usr/bin/git config --file $gitconfig user.email`;
        chomp $email;
    }

    # Fall back to getent
    $name  ||= $getpw[6] || $user;
    $email ||= $user . '@' . EMAIL_DOMAIN;

    $ENV{GIT_AUTHOR_NAME} = $name;
    $ENV{GIT_AUTHOR_EMAIL} = $email;
}

sub get_user {
    my $pid = shift;

    # See if the PID is bogus
    return "root" unless $pid and kill 0, $pid;

    # Pull out the env from it
    my %env = getenv($pid);

    # Simplest case -- check USER first
    if ($env{USER} and $env{USER} ne "root") {
        return $env{USER};
    }

    # Or we're running under sudo
    if ($env{SUDO_USER} and $env{SUDO_USER} ne "root") {
        return $env{SUDO_USER};
    }

    # They did something like `sudo su -`
    return get_user(parent_pid($pid));
}

sub getenv {
    my $pid = shift;
    my $env = do {local @ARGV = ("/proc/$pid/environ"); local $/; <>};
    my @lines = split /\0/, $env;
    return () unless grep {/=/} @lines;
    my %env = map {split /=/, $_, 2} @lines;
    return %env;
}

sub parent_pid {
    my $pid = shift;
    my $stat = do {local @ARGV = ("/proc/$pid/stat"); local $/; <>};
    my (undef, undef, undef, $ppid) = split ' ', $stat;
    return $ppid;
}
-- 
Networking -- only one letter away from not working

      reply	other threads:[~2010-05-10 21:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-10 17:05 avoiding anonymous commits from root/shared accounts Nick
2010-05-10 21:11 ` Alex Vandiver [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1273525498-sup-6628@utwig \
    --to=alex@chmrr.net \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).