From 335abdd545cc9bfd76b17fa5fde84d1d14419452 Mon Sep 17 00:00:00 2001 From: Michael Rash Date: Sun, 22 Jul 2012 23:13:01 -0400 Subject: [PATCH] use LOGNAME env var before cuserid() since we're already looking for SPOOF_USER --- lib/fko_user.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/fko_user.c b/lib/fko_user.c index 8511e1a7..37756b35 100644 --- a/lib/fko_user.c +++ b/lib/fko_user.c @@ -50,7 +50,7 @@ fko_set_username(fko_ctx_t ctx, const char *spoof_user) /* If spoof_user was not passed in, check for a SPOOF_USER enviroment * variable. If it is set, use its value. */ - if(spoof_user != NULL && strlen(spoof_user)) + if(spoof_user != NULL && strnlen(spoof_user, MAX_SPA_USERNAME_SIZE)) username = (char*)spoof_user; else username = getenv("SPOOF_USER"); @@ -59,28 +59,29 @@ fko_set_username(fko_ctx_t ctx, const char *spoof_user) */ if(username == NULL) { + /* Since we've already tried looking at an env variable, try + * LOGNAME next (and the cuserid() man page recommends this) + */ + if((username = getenv("LOGNAME")) == NULL) + { #ifdef _XOPEN_SOURCE - /* cuserid will return the effective user (i.e. su or setuid). - */ - username = cuserid(NULL); + /* cuserid will return the effective user (i.e. su or setuid). + */ + username = cuserid(NULL); #else - username = getlogin(); + username = getlogin(); #endif - - /* If we did not get a name using the above methods, try the - * LOGNAME or USER environment variables. If none of those work, - * then we fallback to NO_USER. - */ - if(username == NULL) - if((username = getenv("LOGNAME")) == NULL) - if((username = getenv("USER")) == NULL) - username = strdup("NO_USER"); + /* if we still didn't get a username, fall back + */ + if((username = getenv("USER")) == NULL) + username = strdup("NO_USER"); + } } /* Truncate the username if it is too long. */ - if(strlen(username) > MAX_SPA_USERNAME_SIZE) - *(username + MAX_SPA_USERNAME_SIZE) = '\0'; + if(strnlen(username, MAX_SPA_USERNAME_SIZE) == MAX_SPA_USERNAME_SIZE) + *(username + MAX_SPA_USERNAME_SIZE - 1) = '\0'; /* Just in case this is a subsquent call to this function. We * do not want to be leaking memory.