forked from tests-always-included/libnss_exec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnss_exec-group.c
159 lines (108 loc) · 4.02 KB
/
nss_exec-group.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
#include "nss_exec.h"
static int ent_index = 0;
static pthread_mutex_t NSS_EXEC_MUTEX = PTHREAD_MUTEX_INITIALIZER;
static int pack_group_struct(struct group *result, char *buffer, size_t buffer_length, char *output) {
field_parse_info info;
field_parse_init(&info, buffer, buffer_length, output);
result->gr_name = field_parse_string(&info);
result->gr_passwd = field_parse_string(&info);
result->gr_gid = field_parse_long(&info);
result->gr_mem = field_parse_string_array(&info);
return info.return_code;
}
enum nss_status _nss_exec_setgrent_locked(int stayopen) {
enum nss_status result_status;
result_status = nss_exec_script(NULL, "setgrent", NULL);
if (result_status == NSS_STATUS_SUCCESS) {
ent_index = 0;
}
return result_status;
}
// Called to open the group file
enum nss_status _nss_exec_setgrent(int stayopen) {
enum nss_status ret;
NSS_EXEC_LOCK();
ret = _nss_exec_setgrent_locked(stayopen);
NSS_EXEC_UNLOCK();
return ret;
}
enum nss_status _nss_exec_endgrent_locked(void) {
enum nss_status result_status;
result_status = nss_exec_script(NULL, "endgrent", NULL);
if (result_status == NSS_STATUS_SUCCESS) {
ent_index = 0;
}
return result_status;
}
// Called to close the group file
enum nss_status _nss_exec_endgrent(void) {
enum nss_status ret;
NSS_EXEC_LOCK();
ret = _nss_exec_endgrent_locked();
NSS_EXEC_UNLOCK();
return ret;
}
enum nss_status _nss_exec_getgrent_r_locked(struct group *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, "getgrent", ent_index);
if (result_status == NSS_STATUS_SUCCESS) {
ent_index += 1;
pack_result = pack_group_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 group file
enum nss_status _nss_exec_getgrent_r(struct group *result, char *buffer, size_t buffer_length, int *errnop) {
enum nss_status ret;
NSS_EXEC_LOCK();
ret = _nss_exec_getgrent_r_locked(result, buffer, buffer_length, errnop);
NSS_EXEC_UNLOCK();
return ret;
}
// Find a group by gid
enum nss_status _nss_exec_getgrgid_r_locked(gid_t gid, struct group *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, "getgrgid", gid);
if (result_status == NSS_STATUS_SUCCESS) {
ent_index += 1;
pack_result = pack_group_struct(result, buffer, buffer_length, output);
result_status = handle_pack_result(pack_result, errnop);
}
return result_status;
}
enum nss_status _nss_exec_getgrgid_r(gid_t gid, struct group *result, char *buffer, size_t buffer_length, int *errnop) {
enum nss_status ret;
NSS_EXEC_LOCK();
ret = _nss_exec_getgrgid_r_locked(gid, result, buffer, buffer_length, errnop);
NSS_EXEC_UNLOCK();
return ret;
}
enum nss_status _nss_exec_getgrnam_r_locked(const char *name, struct group *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, "getgrnam", name);
if (result_status == NSS_STATUS_SUCCESS) {
ent_index += 1;
pack_result = pack_group_struct(result, buffer, buffer_length, output);
result_status = handle_pack_result(pack_result, errnop);
}
return result_status;
}
// Find a group by name
enum nss_status _nss_exec_getgrnam_r(const char *name, struct group *result, char *buffer, size_t buffer_length, int *errnop) {
enum nss_status ret;
NSS_EXEC_LOCK();
ret = _nss_exec_getgrnam_r_locked(name, result, buffer, buffer_length, errnop);
NSS_EXEC_UNLOCK();
return ret;
}