diff options
Diffstat (limited to 'src/nvim/os/users.c')
-rw-r--r-- | src/nvim/os/users.c | 83 |
1 files changed, 76 insertions, 7 deletions
diff --git a/src/nvim/os/users.c b/src/nvim/os/users.c index c6463c2c92..9374693550 100644 --- a/src/nvim/os/users.c +++ b/src/nvim/os/users.c @@ -13,6 +13,25 @@ #ifdef HAVE_PWD_H # include <pwd.h> #endif +#ifdef WIN32 +# include <lm.h> +#endif + +// Add a user name to the list of users in garray_T *users. +// Do nothing if user name is NULL or empty. +static void add_user(garray_T *users, char *user, bool need_copy) +{ + char *user_copy = (user != NULL && need_copy) + ? xstrdup(user) : user; + + if (user_copy == NULL || *user_copy == NUL) { + if (need_copy) { + xfree(user); + } + return; + } + GA_APPEND(char *, users, user_copy); +} // Initialize users garray and fill it with os usernames. // Return Ok for success, FAIL for failure. @@ -24,16 +43,66 @@ int os_get_usernames(garray_T *users) ga_init(users, sizeof(char *), 20); # if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H) - struct passwd *pw; + { + struct passwd *pw; + + setpwent(); + while ((pw = getpwent()) != NULL) { + add_user(users, pw->pw_name, true); + } + endpwent(); + } +# elif defined(WIN32) + { + DWORD nusers = 0, ntotal = 0, i; + PUSER_INFO_0 uinfo; + + if (NetUserEnum(NULL, 0, 0, (LPBYTE *)&uinfo, MAX_PREFERRED_LENGTH, + &nusers, &ntotal, NULL) == NERR_Success) { + for (i = 0; i < nusers; i++) { + char *user; + int conversion_result = utf16_to_utf8(uinfo[i].usri0_name, -1, &user); + if (conversion_result != 0) { + EMSG2("utf16_to_utf8 failed: %d", conversion_result); + break; + } + add_user(users, user, false); + } + + NetApiBufferFree(uinfo); + } + } +# endif +# if defined(HAVE_GETPWNAM) + { + const char *user_env = os_getenv("USER"); + + // The $USER environment variable may be a valid remote user name (NIS, + // LDAP) not already listed by getpwent(), as getpwent() only lists + // local user names. If $USER is not already listed, check whether it + // is a valid remote user name using getpwnam() and if it is, add it to + // the list of user names. + + if (user_env != NULL && *user_env != NUL) { + int i; + + for (i = 0; i < users->ga_len; i++) { + char *local_user = ((char **)users->ga_data)[i]; + + if (STRCMP(local_user, user_env) == 0) { + break; + } + } + + if (i == users->ga_len) { + struct passwd *pw = getpwnam(user_env); // NOLINT - setpwent(); - while ((pw = getpwent()) != NULL) { - // pw->pw_name shouldn't be NULL but just in case... - if (pw->pw_name != NULL) { - GA_APPEND(char *, users, xstrdup(pw->pw_name)); + if (pw != NULL) { + add_user(users, pw->pw_name, true); + } + } } } - endpwent(); # endif return OK; |