/* 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");
*/
if(username == NULL)
{
-#ifdef _XOPEN_SOURCE
- /* cuserid will return the effective user (i.e. su or setuid).
+ /* Since we've already tried looking at an env variable, try
+ * LOGNAME next (and the cuserid() man page recommends this)
*/
- username = cuserid(NULL);
+ if((username = getenv("LOGNAME")) == NULL)
+ {
+#ifdef _XOPEN_SOURCE
+ /* 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.