diff options
| author | Natanael Copa <ncopa@alpinelinux.org> | 2017-07-17 17:24:07 +0200 |
|---|---|---|
| committer | Natanael Copa <ncopa@alpinelinux.org> | 2017-07-17 17:24:07 +0200 |
| commit | 6f60008bc4ea3aa93c969b0e0d48d5535ea11d9f (patch) | |
| tree | d2ab535b2858fbde2cac620e431e1aff54471cfc | |
| parent | f92283f5b96e55f727a05cd0f1c61dc156dc7e6a (diff) | |
| download | abuild-6f60008bc4ea3aa93c969b0e0d48d5535ea11d9f.tar.bz2 abuild-6f60008bc4ea3aa93c969b0e0d48d5535ea11d9f.tar.xz | |
abuild-sudo: fix segfault when there are no controlling term
if there are no controlling reminal getlogin() may return NULL. We use
getpwuid() to try figure out the username and verify that we actually
have a username before we set environment USER.
| -rw-r--r-- | abuild-sudo.c | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/abuild-sudo.c b/abuild-sudo.c index 2d691e8..de8eb94 100644 --- a/abuild-sudo.c +++ b/abuild-sudo.c @@ -12,6 +12,7 @@ #include <err.h> #include <grp.h> +#include <pwd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -70,17 +71,28 @@ int main(int argc, const char *argv[]) const char *cmd; const char *path; int i; + struct passwd *pw; grent = getgrnam(ABUILD_GROUP); if (grent == NULL) errx(1, "%s: Group not found", ABUILD_GROUP); char *name = getlogin(); + if (name == NULL) { + pw = getpwuid(getuid()); + if (pw) + name = pw->pw_name; + } + if (!is_in_group(grent->gr_gid)) { errx(1, "User %s is not a member of group %s\n", name ? name : "(unknown)", ABUILD_GROUP); } - setenv("USER", name, 1); + if (name) { + setenv("USER", name, 1); + } else { + warnx("Could not find username for uid %d\n", getuid()); + } cmd = strrchr(argv[0], '/'); if (cmd) |
