From 862133e2da874e0e28cc329c1ea589fb57cd27d6 Mon Sep 17 00:00:00 2001 From: Luke Howard Date: Sat, 22 Dec 2018 16:31:19 +1100 Subject: [PATCH] krb5: support %{username} path expansion token --- lib/krb5/expand_path.c | 20 ++++++++++++++++++++ lib/krb5/test_cc.c | 1 + 2 files changed, 21 insertions(+) diff --git a/lib/krb5/expand_path.c b/lib/krb5/expand_path.c index 8024e30aa..c80329347 100644 --- a/lib/krb5/expand_path.c +++ b/lib/krb5/expand_path.c @@ -308,6 +308,25 @@ _expand_euid(krb5_context context, PTYPE param, const char *postfix, char **str) return 0; } +static krb5_error_code +_expand_username(krb5_context context, PTYPE param, const char *postfix, char **str) +{ + uid_t uid = geteuid(); + struct passwd *pwd, pw; + char pwbuf[2048]; + + if (rk_getpwuid_r(uid, &pw, pwbuf, sizeof(pwbuf), &pwd) != 0) { + krb5_set_error_message(context, ENOENT, + "Could not find username for UID '%ld'", (long)uid); + return ENOENT; + } + + *str = strdup(pwd->pw_name); + if (*str == NULL) + return krb5_enomem(context); + + return 0; +} #endif /* _WIN32 */ /** @@ -375,6 +394,7 @@ static const struct { {"LIBEXEC", FTYPE_SPECIAL, 0, LIBEXECDIR, _expand_path}, {"SBINDIR", FTYPE_SPECIAL, 0, SBINDIR, _expand_path}, {"euid", SPECIAL(_expand_euid)}, + {"username", SPECIAL(_expand_username)}, #endif {"TEMP", SPECIAL(_expand_temp_folder)}, {"USERID", SPECIAL(_expand_userid)}, diff --git a/lib/krb5/test_cc.c b/lib/krb5/test_cc.c index c27015139..a6f7d312d 100644 --- a/lib/krb5/test_cc.c +++ b/lib/krb5/test_cc.c @@ -295,6 +295,7 @@ struct { { "foo%}", 0, "foo%}" }, { "%{uid}", 0, NULL }, { "%{euid}", 0, NULL }, + { "%{username}", 0, NULL }, { "foo%{null}", 0, "foo" }, { "foo%{null}bar", 0, "foobar" }, { "%{", 1, NULL },