From: Greg Hudson Date: Wed, 1 Aug 2012 18:05:52 +0000 (-0400) Subject: Add %{username} token to path expansion X-Git-Tag: krb5-1.11-alpha1~366 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=69947d9bf040f01e6ea2d0a6e761692132487b7e;p=thirdparty%2Fkrb5.git Add %{username} token to path expansion For Unix-like platforms, add %{username} to the path expansion facility, expanding to the result of getpwuid on the euid. Also, for manual testing convenience, make t_expand_path print the result if no second argument is given. --- diff --git a/doc/rst_source/krb_admins/conf_files/krb5_conf.rst b/doc/rst_source/krb_admins/conf_files/krb5_conf.rst index ee7344c5d8..689e61c927 100644 --- a/doc/rst_source/krb_admins/conf_files/krb5_conf.rst +++ b/doc/rst_source/krb_admins/conf_files/krb5_conf.rst @@ -991,6 +991,7 @@ to be expanded. Valid parameters are: %{LIBDIR} Installation library directory %{BINDIR} Installation binary directory %{SBINDIR} Installation admin binary directory + %{username} (Unix) Username of effective user ID %{APPDATA} (Windows) Roaming application data for current user %{COMMON_APPDATA} (Windows) Application data for all users %{LOCAL_APPDATA} (Windows) Local application data for current user diff --git a/src/lib/krb5/os/expand_path.c b/src/lib/krb5/os/expand_path.c index deab4e3b1f..2da145fd62 100644 --- a/src/lib/krb5/os/expand_path.c +++ b/src/lib/krb5/os/expand_path.c @@ -264,7 +264,8 @@ expand_csidl(krb5_context context, PTYPE folder, const char *postfix, return 0; } -#else +#else /* not _WIN32 */ +#include static krb5_error_code expand_path(krb5_context context, PTYPE param, const char *postfix, char **ret) @@ -306,6 +307,26 @@ 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 euid = geteuid(); + struct passwd *pw, pwx; + char pwbuf[BUFSIZ]; + + if (k5_getpwuid_r(euid, &pwx, pwbuf, sizeof(pwbuf), &pw) != 0) { + krb5_set_error_message(context, ENOENT, + _("Can't find username for uid %lu"), + (unsigned long)euid); + return ENOENT; + } + *str = strdup(pw->pw_name); + if (*str == NULL) + return ENOMEM; + return 0; +} + #endif /* not _WIN32 */ /* @@ -366,6 +387,7 @@ static const struct token { {"BINDIR", 0, BINDIR, expand_path}, {"SBINDIR", 0, SBINDIR, expand_path}, {"euid", 0, NULL, expand_euid}, + {"username", 0, NULL, expand_username}, #endif {"TEMP", 0, NULL, expand_temp_folder}, {"USERID", 0, NULL, expand_userid}, diff --git a/src/lib/krb5/os/t_expand_path.c b/src/lib/krb5/os/t_expand_path.c index b318ff980a..5f63577a3c 100644 --- a/src/lib/krb5/os/t_expand_path.c +++ b/src/lib/krb5/os/t_expand_path.c @@ -9,7 +9,9 @@ main(int argc, char **argv) if (k5_expand_path_tokens_extra(NULL, argv[1], &path, "animal", "frog", "place", "pad", "s", "s", NULL) != 0) return 2; - if (strcmp(path, argv[2]) != 0) + if (argc == 2) + printf("%s\n", path); + else if (strcmp(path, argv[2]) != 0) return 1; free(path); return 0;