forked from tests-always-included/libnss_exec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnss_exec-passwd.c
162 lines (111 loc) · 4.17 KB
/
nss_exec-passwd.c
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
160
161
#include "nss_exec.h"
static long ent_index = 0;
static pthread_mutex_t NSS_EXEC_MUTEX = PTHREAD_MUTEX_INITIALIZER;
static int pack_passwd_struct(struct passwd *result, char *buffer, size_t buffer_length, char *output) {
field_parse_info info;
field_parse_init(&info, buffer, buffer_length, output);
result->pw_name = field_parse_string(&info);
result->pw_passwd = field_parse_string(&info);
result->pw_uid = field_parse_long(&info);
result->pw_gid = field_parse_long(&info);
result->pw_gecos = field_parse_string(&info);
result->pw_dir = field_parse_string(&info);
result->pw_shell = field_parse_string(&info);
return info.return_code;
}
enum nss_status _nss_exec_setpwent_locked(int stayopen) {
enum nss_status result_status;
result_status = nss_exec_script(NULL, "setpwent", NULL);
if (result_status == NSS_STATUS_SUCCESS) {
ent_index = 0;
}
return result_status;
}
// Called to open the passwd file
enum nss_status _nss_exec_setpwent(int stayopen) {
enum nss_status ret;
NSS_EXEC_LOCK();
ret = _nss_exec_setpwent_locked(stayopen);
NSS_EXEC_UNLOCK();
return ret;
}
enum nss_status _nss_exec_endpwent_locked(void) {
enum nss_status result_status;
result_status = nss_exec_script_long(NULL, "endpwent", ent_index);
if (result_status == NSS_STATUS_SUCCESS) {
ent_index = 0;
}
return result_status;
}
// Called to close the passwd file
enum nss_status _nss_exec_endpwent(void) {
enum nss_status ret;
NSS_EXEC_LOCK();
ret = _nss_exec_endpwent_locked();
NSS_EXEC_UNLOCK();
return ret;
}
enum nss_status _nss_exec_getpwent_r_locked(struct passwd *result, char *buffer, size_t buffer_length, int *errnop) {
enum nss_status result_status;
char *output;
int pack_result;
result_status = nss_exec_script_long(&output, "getpwent", ent_index);
if (result_status == NSS_STATUS_SUCCESS) {
ent_index += 1;
pack_result = pack_passwd_struct(result, buffer, buffer_length, output);
result_status = handle_pack_result(pack_result, errnop);
}
if (result_status == NSS_STATUS_SUCCESS) {
return 0;
} else {
return 1;
}
}
// Called to look up next entry in passwd file
enum nss_status _nss_exec_getpwent_r(struct passwd *result, char *buffer, size_t buffer_length, int *errnop) {
enum nss_status ret;
NSS_EXEC_LOCK();
ret = _nss_exec_getpwent_r_locked(result, buffer, buffer_length, errnop);
NSS_EXEC_UNLOCK();
return ret;
}
// Find a passwd by uid
enum nss_status _nss_exec_getpwuid_r_locked(uid_t uid, struct passwd *result, char *buffer, size_t buffer_length, int *errnop) {
enum nss_status result_status;
char *output;
int pack_result;
result_status = nss_exec_script_long(&output, "getpwuid", uid);
if (result_status == NSS_STATUS_SUCCESS) {
ent_index += 1;
pack_result = pack_passwd_struct(result, buffer, buffer_length, output);
result_status = handle_pack_result(pack_result, errnop);
}
return result_status;
}
enum nss_status _nss_exec_getpwuid_r(uid_t uid, struct passwd *result, char *buffer, size_t buffer_length, int *errnop) {
enum nss_status ret;
NSS_EXEC_LOCK();
ret = _nss_exec_getpwuid_r_locked(uid, result, buffer, buffer_length, errnop);
NSS_EXEC_UNLOCK();
return ret;
}
enum nss_status _nss_exec_getpwnam_r_locked(const char *name, struct passwd *result, char *buffer, size_t buffer_length, int *errnop) {
enum nss_status result_status;
char *output;
int pack_result;
result_status = nss_exec_script(&output, "getpwnam", name);
if (result_status == NSS_STATUS_SUCCESS) {
ent_index += 1;
pack_result = pack_passwd_struct(result, buffer, buffer_length, output);
result_status = handle_pack_result(pack_result, errnop);
}
return result_status;
}
// Find a passwd by name
enum nss_status _nss_exec_getpwnam_r(const char *name, struct passwd *result, char *buffer, size_t buffer_length, int *errnop) {
enum nss_status ret;
NSS_EXEC_LOCK();
ret = _nss_exec_getpwnam_r_locked(name, result, buffer, buffer_length, errnop);
NSS_EXEC_UNLOCK();
return ret;
}