1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "napi/native_api.h"
17 #include <cerrno>
18 #include <cstring>
19 #include <grp.h>
20 #include <ifaddrs.h>
21 #include <js_native_api_types.h>
22 #include <malloc.h>
23 #include <net/if.h>
24 #include <sys/inotify.h>
25 #include <unistd.h>
26 #include <utmp.h>
27 #include <uv.h>
28
29 #define ONEVAL 1
30 #define MINUSONE (-1)
31 #define TWOVAL 2
32 #define THRVAL 3
33 #define MAX_NAMBER 1024
34 #define NO_ERR 0
35 #define SUCCESS 1
36 #define FAIL (-1)
37 #define PARAM_0 0
38 #define TEN 10
39 #define ERRON_0 0
40
GetGrNamR(napi_env env,napi_callback_info info)41 static napi_value GetGrNamR(napi_env env, napi_callback_info info)
42 {
43 char buf[512];
44 struct group *grp;
45 struct group grpStorage;
46 const char *groupName = "root";
47 int getInfo = getgrnam_r(groupName, &grpStorage, buf, sizeof(buf), &grp);
48 napi_value result = nullptr;
49 napi_create_int32(env, getInfo, &result);
50 return result;
51 }
52
GetGroupList(napi_env env,napi_callback_info info)53 static napi_value GetGroupList(napi_env env, napi_callback_info info)
54 {
55 static int serverNgroups;
56 static gid_t *server_groups;
57 int ret;
58 const char *serverUser = "root";
59 struct passwd *pwd = getpwnam(serverUser);
60 gid_t server_gid = pwd->pw_gid;
61 ret = getgrouplist(serverUser, server_gid, nullptr, &serverNgroups);
62 server_groups = (gid_t *)malloc(serverNgroups * sizeof(gid_t));
63 ret = getgrouplist(serverUser, server_gid, server_groups, &serverNgroups);
64 if (ret != FAIL) {
65 ret = SUCCESS;
66 }
67 napi_value result = nullptr;
68 napi_create_int32(env, ret, &result);
69 return result;
70 }
71
InitGroups(napi_env env,napi_callback_info info)72 static napi_value InitGroups(napi_env env, napi_callback_info info)
73 {
74 napi_value result = nullptr;
75 return result;
76 }
Setgrent(napi_env env,napi_callback_info info)77 static napi_value Setgrent(napi_env env, napi_callback_info info)
78 {
79 errno = ERRON_0;
80 struct group *getval = getgrent();
81 napi_value result;
82 if (getval == nullptr) {
83 napi_create_int32(env, PARAM_0, &result);
84 }
85 if (errno != PARAM_0) {
86 napi_create_int32(env, ONEVAL, &result);
87 }
88 char buf[MAX_NAMBER] = {PARAM_0};
89 strcpy(buf, getval->gr_name);
90 setgrent();
91 getval = getgrent();
92 if (getval == nullptr) {
93 napi_create_int32(env, TWOVAL, &result);
94 }
95 if (strcmp(getval->gr_name, buf)) {
96 napi_create_int32(env, THRVAL, &result);
97 }
98 if (errno == PARAM_0) {
99 napi_create_int32(env, errno, &result);
100 } else {
101 napi_create_int32(env, MINUSONE, &result);
102 }
103 return result;
104 }
105
EndGRent(napi_env env,napi_callback_info info)106 static napi_value EndGRent(napi_env env, napi_callback_info info)
107 {
108 errno = NO_ERR;
109 struct group *getgrent(void);
110 setgrent();
111 endgrent();
112 napi_value result;
113 napi_create_int32(env, errno, &result);
114 return result;
115 }
116
Getgrgid(napi_env env,napi_callback_info info)117 static napi_value Getgrgid(napi_env env, napi_callback_info info)
118 {
119 gid_t gid = PARAM_0;
120 struct group *grp = getgrgid(gid);
121 int resultValue = FAIL;
122 if (grp) {
123 resultValue = SUCCESS;
124 }
125 napi_value result = nullptr;
126 napi_create_int32(env, resultValue, &result);
127 return result;
128 }
129
Getgrgid_r(napi_env env,napi_callback_info info)130 static napi_value Getgrgid_r(napi_env env, napi_callback_info info)
131 {
132 char buf[512];
133 gid_t gid = PARAM_0;
134 struct group *grp;
135 struct group grpStorage;
136
137 int resultValue = getgrgid_r(gid, &grpStorage, buf, sizeof(buf), &grp);
138 napi_value result = nullptr;
139 napi_create_int32(env, resultValue, &result);
140 return result;
141 }
142
Getgrnam(napi_env env,napi_callback_info info)143 static napi_value Getgrnam(napi_env env, napi_callback_info info)
144 {
145 const char *groupName = "root";
146 struct group *grp = getgrnam(groupName);
147
148 int resultValue = FAIL;
149 if (grp) {
150 resultValue = SUCCESS;
151 }
152 napi_value result = nullptr;
153 napi_create_int32(env, resultValue, &result);
154 return result;
155 }
156
Getgrent(napi_env env,napi_callback_info info)157 static napi_value Getgrent(napi_env env, napi_callback_info info)
158 {
159 int resultValue = FAIL;
160 errno = NO_ERR;
161 struct group *grp = nullptr;
162 setgrent();
163 grp = getgrent();
164 if (grp != nullptr) {
165 resultValue = SUCCESS;
166 }
167
168 if (errno == NO_ERR) {
169 resultValue = SUCCESS;
170 }
171 endgrent();
172 napi_value result = nullptr;
173 napi_create_int32(env, resultValue, &result);
174 return result;
175 }
176
177 EXTERN_C_START
Init(napi_env env,napi_value exports)178 static napi_value Init(napi_env env, napi_value exports)
179 {
180 napi_property_descriptor desc[] = {
181 {"setgrent", nullptr, Setgrent, nullptr, nullptr, nullptr, napi_default, nullptr},
182 {"getGrNamR", nullptr, GetGrNamR, nullptr, nullptr, nullptr, napi_default, nullptr},
183 {"getGroupList", nullptr, GetGroupList, nullptr, nullptr, nullptr, napi_default, nullptr},
184 {"initGroups", nullptr, InitGroups, nullptr, nullptr, nullptr, napi_default, nullptr},
185 {"getgrgid", nullptr, Getgrgid, nullptr, nullptr, nullptr, napi_default, nullptr},
186 {"getgrgidR", nullptr, Getgrgid_r, nullptr, nullptr, nullptr, napi_default, nullptr},
187 {"getgrnam", nullptr, Getgrnam, nullptr, nullptr, nullptr, napi_default, nullptr},
188 {"endgrent", nullptr, EndGRent, nullptr, nullptr, nullptr, napi_default, nullptr},
189 {"getgrent", nullptr, Getgrent, nullptr, nullptr, nullptr, napi_default, nullptr}};
190 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
191 return exports;
192 }
193 EXTERN_C_END
194
195 static napi_module demoModule = {
196 .nm_version = 1,
197 .nm_flags = 0,
198 .nm_filename = nullptr,
199 .nm_register_func = Init,
200 .nm_modname = "grp",
201 .nm_priv = ((void *)0),
202 .reserved = {0},
203 };
204
RegisterModule(void)205 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
206