aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/os/users.c
blob: 9952e2b387d1b516394e9c287ac3725d505e4ecc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com

// users.c -- operating system user information

#include <uv.h>

#include "auto/config.h"
#include "nvim/ascii.h"
#include "nvim/garray.h"
#include "nvim/memory.h"
#include "nvim/os/os.h"
#include "nvim/strings.h"
#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.
int os_get_usernames(garray_T *users)
{
  if (users == NULL) {
    return FAIL;
  }
  ga_init(users, sizeof(char *), 20);

#if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
  {
    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) {
          semsg("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

        if (pw != NULL) {
          add_user(users, pw->pw_name, true);
        }
      }
    }
  }
#endif

  return OK;
}

// Insert user name in s[len].
// Return OK if a name found.
int os_get_user_name(char *s, size_t len)
{
#ifdef UNIX
  return os_get_uname((uv_uid_t)getuid(), s, len);
#else
  // TODO(equalsraf): Windows GetUserName()
  return os_get_uname((uv_uid_t)0, s, len);
#endif
}

// Insert user name for "uid" in s[len].
// Return OK if a name found.
// If the name is not found, write the uid into s[len] and return FAIL.
int os_get_uname(uv_uid_t uid, char *s, size_t len)
{
#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
  struct passwd *pw;

  if ((pw = getpwuid(uid)) != NULL  // NOLINT(runtime/threadsafe_fn)
      && pw->pw_name != NULL && *(pw->pw_name) != NUL) {
    STRLCPY(s, pw->pw_name, len);
    return OK;
  }
#endif
  snprintf(s, len, "%d", (int)uid);
  return FAIL;  // a number is not a name
}

// Returns the user directory for the given username.
// The caller has to free() the returned string.
// If the username is not found, NULL is returned.
char *os_get_user_directory(const char *name)
{
#if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
  if (name == NULL || *name == NUL) {
    return NULL;
  }
  struct passwd *pw = getpwnam(name);  // NOLINT(runtime/threadsafe_fn)
  if (pw != NULL) {
    // save the string from the static passwd entry into malloced memory
    return xstrdup(pw->pw_dir);
  }
#endif
  return NULL;
}