7 #define COPY_IF_ROOM(s) \
8 ({ size_t len_ = strlen (s) + 1; \
10 buflen - (cp - buffer) < len_ \
12 : (cp = mempcpy (cp, s, len_), start_); })
15 /* Password handling. */
18 static struct passwd pwd_data
[] =
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 *) "*" }
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
;
39 _nss_test1_setpwent (int stayopen
)
42 return NSS_STATUS_SUCCESS
;
47 _nss_test1_endpwent (void)
49 return NSS_STATUS_SUCCESS
;
54 _nss_test1_getpwent_r (struct passwd
*result
, char *buffer
, size_t buflen
,
58 int res
= NSS_STATUS_SUCCESS
;
60 pthread_mutex_lock (&pwd_lock
);
62 if (pwd_iter
>= npwd_data
)
63 res
= NSS_STATUS_NOTFOUND
;
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
)
79 res
= NSS_STATUS_TRYAGAIN
;
85 pthread_mutex_unlock (&pwd_lock
);
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
)
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
)
114 res
= NSS_STATUS_TRYAGAIN
;
120 return NSS_STATUS_NOTFOUND
;
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)
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
)
147 res
= NSS_STATUS_TRYAGAIN
;
153 return NSS_STATUS_NOTFOUND
;