aboutsummaryrefslogtreecommitdiff
path: root/nss/nss_test1.c
blob: 3beb488fcfa20da38b0e63b7e0ddf819a3d167de (plain)
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
#include <errno.h>
#include <nss.h>
#include <pthread.h>
#include <string.h>


#define COPY_IF_ROOM(s) \
  ({ size_t len_ = strlen (s) + 1;		\
     char *start_ = cp;				\
     buflen - (cp - buffer) < len_		\
     ? NULL					\
     : (cp = mempcpy (cp, s, len_), start_); })


/* Password handling.  */
#include <pwd.h>

static struct passwd pwd_data[] =
  {
#define PWD(u) \
    { .pw_name = (char *) "name" #u, .pw_passwd = (char *) "*", .pw_uid = u,  \
      .pw_gid = 100, .pw_gecos = (char *) "*", .pw_dir = (char *) "*",	      \
      .pw_shell = (char *) "*" }
    PWD (100),
    PWD (30),
    PWD (200),
    PWD (60),
    PWD (20000)
  };
#define npwd_data (sizeof (pwd_data) / sizeof (pwd_data[0]))

static size_t pwd_iter;
#define CURPWD pwd_data[pwd_iter]

static pthread_mutex_t pwd_lock = PTHREAD_MUTEX_INITIALIZER;


enum nss_status
_nss_test1_setpwent (int stayopen)
{
  pwd_iter = 0;
  return NSS_STATUS_SUCCESS;
}


enum nss_status
_nss_test1_endpwent (void)
{
  return NSS_STATUS_SUCCESS;
}


enum nss_status
_nss_test1_getpwent_r (struct passwd *result, char *buffer, size_t buflen,
		       int *errnop)
{
  char *cp = buffer;
  int res = NSS_STATUS_SUCCESS;

  pthread_mutex_lock (&pwd_lock);

  if (pwd_iter >= npwd_data)
    res = NSS_STATUS_NOTFOUND;
  else
    {
      result->pw_name = COPY_IF_ROOM (CURPWD.pw_name);
      result->pw_passwd = COPY_IF_ROOM (CURPWD.pw_passwd);
      result->pw_uid = CURPWD.pw_uid;
      result->pw_gid = CURPWD.pw_gid;
      result->pw_gecos = COPY_IF_ROOM (CURPWD.pw_gecos);
      result->pw_dir = COPY_IF_ROOM (CURPWD.pw_dir);
      result->pw_shell = COPY_IF_ROOM (CURPWD.pw_shell);

      if (result->pw_name == NULL || result->pw_passwd == NULL
	  || result->pw_gecos == NULL || result->pw_dir == NULL
	  || result->pw_shell == NULL)
	{
	  *errnop = ERANGE;
	  res = NSS_STATUS_TRYAGAIN;
	}

      ++pwd_iter;
    }

  pthread_mutex_unlock (&pwd_lock);

  return res;
}


enum nss_status
_nss_test1_getpwuid_r (uid_t uid, struct passwd *result, char *buffer,
		       size_t buflen, int *errnop)
{
  for (size_t idx = 0; idx < npwd_data; ++idx)
    if (pwd_data[idx].pw_uid == uid)
      {
	char *cp = buffer;
	int res = NSS_STATUS_SUCCESS;

	result->pw_name = COPY_IF_ROOM (pwd_data[idx].pw_name);
	result->pw_passwd = COPY_IF_ROOM (pwd_data[idx].pw_passwd);
	result->pw_uid = pwd_data[idx].pw_uid;
	result->pw_gid = pwd_data[idx].pw_gid;
	result->pw_gecos = COPY_IF_ROOM (pwd_data[idx].pw_gecos);
	result->pw_dir = COPY_IF_ROOM (pwd_data[idx].pw_dir);
	result->pw_shell = COPY_IF_ROOM (pwd_data[idx].pw_shell);

	if (result->pw_name == NULL || result->pw_passwd == NULL
	    || result->pw_gecos == NULL || result->pw_dir == NULL
	    || result->pw_shell == NULL)
	  {
	    *errnop = ERANGE;
	    res = NSS_STATUS_TRYAGAIN;
	  }

	return res;
      }

  return NSS_STATUS_NOTFOUND;
}


enum nss_status
_nss_test1_getpwnam_r (const char *name, struct passwd *result, char *buffer,
		       size_t buflen, int *errnop)
{
  for (size_t idx = 0; idx < npwd_data; ++idx)
    if (strcmp (pwd_data[idx].pw_name, name) == 0)
      {
	char *cp = buffer;
	int res = NSS_STATUS_SUCCESS;

	result->pw_name = COPY_IF_ROOM (pwd_data[idx].pw_name);
	result->pw_passwd = COPY_IF_ROOM (pwd_data[idx].pw_passwd);
	result->pw_uid = pwd_data[idx].pw_uid;
	result->pw_gid = pwd_data[idx].pw_gid;
	result->pw_gecos = COPY_IF_ROOM (pwd_data[idx].pw_gecos);
	result->pw_dir = COPY_IF_ROOM (pwd_data[idx].pw_dir);
	result->pw_shell = COPY_IF_ROOM (pwd_data[idx].pw_shell);

	if (result->pw_name == NULL || result->pw_passwd == NULL
	    || result->pw_gecos == NULL || result->pw_dir == NULL
	    || result->pw_shell == NULL)
	  {
	    *errnop = ERANGE;
	    res = NSS_STATUS_TRYAGAIN;
	  }

	return res;
      }

  return NSS_STATUS_NOTFOUND;
}