git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Shawn O. Pearce" <spearce@spearce.org>
To: git@vger.kernel.org
Subject: [RFC PATCH] Use SUDO_UID to guess committer identity
Date: Sat, 7 Jun 2008 03:11:30 -0400	[thread overview]
Message-ID: <20080607071130.GZ12896@spearce.org> (raw)

When invoking Git commands though sudo against a bare repository
with reflogs enabled we should attempt to record the actual user's
information in the reflog, not the identity of the user sudo entered.

For example when executing:

	sudo -u gitadm git --git-dir=/srv/git.git branch -f pu master

We want record information about the caller of sudo, not gitadm.

Relying on $SUDO_UID in this case isn't as bad as it might seem.
Under sudo $HOME is left as the real user's home directory and
$HOME/.gitconfig is used to supply user.name and user.email.
However if the real user does not have ~/.gitconfig or did not set
user.name/email we need to guess it from their GECOS information.

NO SBO - FOR DISCUSSION ONLY
---
 ident.c |   21 ++++++++++++++++++---
 1 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/ident.c b/ident.c
index b35504a..c821d5f 100644
--- a/ident.c
+++ b/ident.c
@@ -7,6 +7,21 @@
  */
 #include "cache.h"
 
+static uid_t caller_uid(void)
+{
+	const char *sudo_uid = getenv("SUDO_UID");
+	char *end;
+	unsigned long who;
+
+	if (!sudo_uid || !*sudo_uid)
+		return getuid();
+
+	who = strtoul(sudo_uid, &end, 10)
+	if (*end)
+		return getuid();
+	return (uid_t)who;
+}
+
 static char git_default_date[50];
 
 static void copy_gecos(const struct passwd *w, char *name, size_t sz)
@@ -76,7 +91,7 @@ static void setup_ident(void)
 
 	/* Get the name ("gecos") */
 	if (!git_default_name[0]) {
-		pw = getpwuid(getuid());
+		pw = getpwuid(caller_uid());
 		if (!pw)
 			die("You don't exist. Go away!");
 		copy_gecos(pw, git_default_name, sizeof(git_default_name));
@@ -90,7 +105,7 @@ static void setup_ident(void)
 				sizeof(git_default_email));
 		else {
 			if (!pw)
-				pw = getpwuid(getuid());
+				pw = getpwuid(caller_uid());
 			if (!pw)
 				die("You don't exist. Go away!");
 			copy_email(pw);
@@ -208,7 +223,7 @@ const char *fmt_ident(const char *name, const char *email,
 		}
 		if (error_on_no_name)
 			die("empty ident %s <%s> not allowed", name, email);
-		pw = getpwuid(getuid());
+		pw = getpwuid(caller_uid());
 		if (!pw)
 			die("You don't exist. Go away!");
 		strlcpy(git_default_name, pw->pw_name,
-- 
Shawn.

             reply	other threads:[~2008-06-07  7:12 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-07  7:11 Shawn O. Pearce [this message]
2008-06-07 21:05 ` [RFC PATCH] Use SUDO_UID to guess committer identity Junio C Hamano
2008-06-08  0:23   ` Shawn O. Pearce
2008-06-08  0:57     ` Junio C Hamano

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=20080607071130.GZ12896@spearce.org \
    --to=spearce@spearce.org \
    --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).