More NEWS changes.
[glibc.git] / nss / nss_test1.c
blob3beb488fcfa20da38b0e63b7e0ddf819a3d167de
1 #include <errno.h>
2 #include <nss.h>
3 #include <pthread.h>
4 #include <string.h>
7 #define COPY_IF_ROOM(s) \
8 ({ size_t len_ = strlen (s) + 1; \
9 char *start_ = cp; \
10 buflen - (cp - buffer) < len_ \
11 ? NULL \
12 : (cp = mempcpy (cp, s, len_), start_); })
15 /* Password handling. */
16 #include <pwd.h>
18 static struct passwd pwd_data[] =
20 #define PWD(u) \
21 { .pw_name = (char *) "name" #u, .pw_passwd = (char *) "*", .pw_uid = u, \
22 .pw_gid = 100, .pw_gecos = (char *) "*", .pw_dir = (char *) "*", \
23 .pw_shell = (char *) "*" }
24 PWD (100),
25 PWD (30),
26 PWD (200),
27 PWD (60),
28 PWD (20000)
30 #define npwd_data (sizeof (pwd_data) / sizeof (pwd_data[0]))
32 static size_t pwd_iter;
33 #define CURPWD pwd_data[pwd_iter]
35 static pthread_mutex_t pwd_lock = PTHREAD_MUTEX_INITIALIZER;
38 enum nss_status
39 _nss_test1_setpwent (int stayopen)
41 pwd_iter = 0;
42 return NSS_STATUS_SUCCESS;
46 enum nss_status
47 _nss_test1_endpwent (void)
49 return NSS_STATUS_SUCCESS;
53 enum nss_status
54 _nss_test1_getpwent_r (struct passwd *result, char *buffer, size_t buflen,
55 int *errnop)
57 char *cp = buffer;
58 int res = NSS_STATUS_SUCCESS;
60 pthread_mutex_lock (&pwd_lock);
62 if (pwd_iter >= npwd_data)
63 res = NSS_STATUS_NOTFOUND;
64 else
66 result->pw_name = COPY_IF_ROOM (CURPWD.pw_name);
67 result->pw_passwd = COPY_IF_ROOM (CURPWD.pw_passwd);
68 result->pw_uid = CURPWD.pw_uid;
69 result->pw_gid = CURPWD.pw_gid;
70 result->pw_gecos = COPY_IF_ROOM (CURPWD.pw_gecos);
71 result->pw_dir = COPY_IF_ROOM (CURPWD.pw_dir);
72 result->pw_shell = COPY_IF_ROOM (CURPWD.pw_shell);
74 if (result->pw_name == NULL || result->pw_passwd == NULL
75 || result->pw_gecos == NULL || result->pw_dir == NULL
76 || result->pw_shell == NULL)
78 *errnop = ERANGE;
79 res = NSS_STATUS_TRYAGAIN;
82 ++pwd_iter;
85 pthread_mutex_unlock (&pwd_lock);
87 return res;
91 enum nss_status
92 _nss_test1_getpwuid_r (uid_t uid, struct passwd *result, char *buffer,
93 size_t buflen, int *errnop)
95 for (size_t idx = 0; idx < npwd_data; ++idx)
96 if (pwd_data[idx].pw_uid == uid)
98 char *cp = buffer;
99 int res = NSS_STATUS_SUCCESS;
101 result->pw_name = COPY_IF_ROOM (pwd_data[idx].pw_name);
102 result->pw_passwd = COPY_IF_ROOM (pwd_data[idx].pw_passwd);
103 result->pw_uid = pwd_data[idx].pw_uid;
104 result->pw_gid = pwd_data[idx].pw_gid;
105 result->pw_gecos = COPY_IF_ROOM (pwd_data[idx].pw_gecos);
106 result->pw_dir = COPY_IF_ROOM (pwd_data[idx].pw_dir);
107 result->pw_shell = COPY_IF_ROOM (pwd_data[idx].pw_shell);
109 if (result->pw_name == NULL || result->pw_passwd == NULL
110 || result->pw_gecos == NULL || result->pw_dir == NULL
111 || result->pw_shell == NULL)
113 *errnop = ERANGE;
114 res = NSS_STATUS_TRYAGAIN;
117 return res;
120 return NSS_STATUS_NOTFOUND;
124 enum nss_status
125 _nss_test1_getpwnam_r (const char *name, struct passwd *result, char *buffer,
126 size_t buflen, int *errnop)
128 for (size_t idx = 0; idx < npwd_data; ++idx)
129 if (strcmp (pwd_data[idx].pw_name, name) == 0)
131 char *cp = buffer;
132 int res = NSS_STATUS_SUCCESS;
134 result->pw_name = COPY_IF_ROOM (pwd_data[idx].pw_name);
135 result->pw_passwd = COPY_IF_ROOM (pwd_data[idx].pw_passwd);
136 result->pw_uid = pwd_data[idx].pw_uid;
137 result->pw_gid = pwd_data[idx].pw_gid;
138 result->pw_gecos = COPY_IF_ROOM (pwd_data[idx].pw_gecos);
139 result->pw_dir = COPY_IF_ROOM (pwd_data[idx].pw_dir);
140 result->pw_shell = COPY_IF_ROOM (pwd_data[idx].pw_shell);
142 if (result->pw_name == NULL || result->pw_passwd == NULL
143 || result->pw_gecos == NULL || result->pw_dir == NULL
144 || result->pw_shell == NULL)
146 *errnop = ERANGE;
147 res = NSS_STATUS_TRYAGAIN;
150 return res;
153 return NSS_STATUS_NOTFOUND;