Guest User

Untitled

a guest
May 27th, 2018
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. // Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
  2. #include <v8.h>
  3. #include <node.h>
  4. #include <node_os.h>
  5.  
  6. #include <sys/types.h>
  7. #include <sys/time.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <assert.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <limits.h>
  14. #include <pwd.h> /* getpwuid() */
  15. #include <grp.h> /* getgrgid() */
  16.  
  17. namespace node {
  18.  
  19. using namespace v8;
  20.  
  21. // User
  22. static Persistent<String> username_symbol;
  23. static Persistent<String> password_symbol;
  24. static Persistent<String> uid_symbol;
  25. static Persistent<String> gid_symbol;
  26. static Persistent<String> class_symbol;
  27. static Persistent<String> dir_symbol;
  28. static Persistent<String> shell_symbol;
  29. static Persistent<String> change_symbol;
  30. static Persistent<String> expire_symbol;
  31. static Persistent<String> gecos_symbol;
  32. static Persistent<String> fields_symbol;
  33.  
  34. // Group
  35. // static Persistent<String> name_symbol;
  36. // static Persistent<String> members_symbol;
  37.  
  38. // Buffer for getpwnam_r(), getgrpam_r(); keep this scoped at file-level rather
  39. // than method-level to avoid excess stack usage.
  40. static char getbuf[1024];
  41.  
  42. /*-----------------------------------------------
  43. Constructs a JS::Object from a *passwd
  44. -----------------------------------------------*/
  45. Local<Object> BuildUserObject(struct passwd *u) {
  46. HandleScope scope;
  47.  
  48. if (username_symbol.IsEmpty()) {
  49. username_symbol = NODE_PSYMBOL("username");
  50. password_symbol = NODE_PSYMBOL("password");
  51. uid_symbol = NODE_PSYMBOL("uid");
  52. gid_symbol = NODE_PSYMBOL("gid");
  53.  
  54. class_symbol = NODE_PSYMBOL("class");
  55. gecos_symbol = NODE_PSYMBOL("gecos");
  56. dir_symbol = NODE_PSYMBOL("dir");
  57. shell_symbol = NODE_PSYMBOL("shell");
  58.  
  59. change_symbol = NODE_PSYMBOL("change");
  60. expire_symbol = NODE_PSYMBOL("expire");
  61. }
  62.  
  63. Local<Object> user = Object::New();
  64.  
  65. user->Set(username_symbol, String::New(u->pw_name));
  66. user->Set(password_symbol, String::New(u->pw_passwd));
  67. user->Set(uid_symbol, Integer::New(u->pw_uid));
  68. user->Set(gid_symbol, Integer::New(u->pw_gid));
  69.  
  70. user->Set(class_symbol, String::New(u->pw_class));
  71. user->Set(gecos_symbol, String::New(u->pw_gecos));
  72. user->Set(dir_symbol, String::New(u->pw_dir));
  73. user->Set(shell_symbol, String::New(u->pw_shell));
  74.  
  75. user->Set(change_symbol, Undefined()); //atldate(u->pw_change/1000));
  76. user->Set(expire_symbol, Undefined());
  77.  
  78. return scope.Close(user);
  79. }
  80.  
  81.  
  82. struct custom_request {
  83. int type; // 0 is with uid, 1 is with uname
  84. int uid;
  85. char *uname;
  86. void *cb;
  87. struct passwd result;
  88. };
  89.  
  90.  
  91. static int EIO_AfterGetUser(eio_req *req) {
  92.  
  93. }
  94.  
  95. static int EIO_GetUser(eio_req *req) {
  96. struct custom_request *creq = (struct custom_request *)(req->data);
  97. struct passwd pwd, *pwdp = NULL;
  98. int err;
  99.  
  100. if(creq->type == 0){
  101. if ((err = getpwuid_r(creq->uid, &pwd, getbuf, sizeof(getbuf), &pwdp)) || pwdp == NULL) {
  102. req->errorno = errno;
  103. }
  104.  
  105. memcpy(&creq->result, pwdp, sizeof(struct passwd));
  106. } else {
  107. if ((err = getpwnam_r(creq->uname, &pwd, getbuf, sizeof(getbuf), &pwdp)) ||
  108. pwdp == NULL) {
  109. req->errorno = errno;
  110. }
  111.  
  112. memcpy(&creq->result, pwdp, sizeof(struct passwd));
  113. }
  114.  
  115. return 0;
  116. }
  117.  
  118.  
  119. static Handle<Value> GetUser(const Arguments& args) {
  120. HandleScope scope;
  121.  
  122. if ( args.Length() < 1) {
  123. return THROW_BAD_ARGS;
  124. }
  125.  
  126. if (args[1]->IsFunction()) {
  127. struct custom_request *creq = (struct custom_request *)
  128. calloc(1, sizeof(struct custom_request)+32);
  129.  
  130. if (!creq) {
  131. V8::LowMemoryNotification();
  132. return ThrowException(Exception::Error(
  133. String::New("Could not allocate enough memory")));
  134. }
  135.  
  136. if (args[0]->IsInt32()) {
  137. creq->uid = args[0]->Int32Value();
  138. creq->type = 0;
  139. } else {
  140. String::Utf8Value pwnam(args[0]->ToString());
  141. creq->uname = strdup(*pwnam);
  142. creq->type = 1;
  143. }
  144.  
  145. creq->cb = cb_persist(args[1]);
  146.  
  147. //ASYNC_CUSTOM_CALL(blocking_func, callback, data);
  148. ASYNC_CUSTOM_CALL(EIO_GetUser, EIO_AfterGetUser, creq);
  149. return Undefined();
  150. } else {
  151. struct passwd pwd, *pwdp = NULL;
  152. int err;
  153.  
  154. if (args[0]->IsInt32()) {
  155. if ((err = getpwuid_r(args[0]->Int32Value(), &pwd, getbuf, sizeof(getbuf), &pwdp)) ||
  156. pwdp == NULL) {
  157. return ThrowException(ErrnoException(errno, "getpwuid_r"));
  158. }
  159. } else {
  160. String::Utf8Value pwnam(args[0]->ToString());
  161.  
  162. if ((err = getpwnam_r(*pwnam, &pwd, getbuf, sizeof(getbuf), &pwdp)) ||
  163. pwdp == NULL) {
  164. return ThrowException(ErrnoException(errno, "getpwnam_r"));
  165. }
  166. }
  167. return scope.Close(BuildUserObject(*&pwdp));
  168. }
  169. }
  170.  
  171.  
  172. /*
  173.  
  174. Local<Object> BuildGroupObject(struct group *g) {
  175. HandleScope scope;
  176.  
  177. if (name_symbol.IsEmpty()) {
  178. name_symbol = NODE_PSYMBOL("name");
  179. password_symbol = NODE_PSYMBOL("password");
  180. gid_symbol = NODE_PSYMBOL("gid");
  181.  
  182. members_symbol = NODE_PSYMBOL("members");
  183. }
  184.  
  185. Local<Object> group = Object::New();
  186.  
  187. group->Set(name_symbol, String::New(g->gr_name));
  188. group->Set(password_symbol, String::New(g->gr_passwd));
  189. group->Set(gid_symbol, Integer::New(g->gr_gid));
  190.  
  191. // group->Set(members_symbol, String::New(g->gr_mem[1]));
  192.  
  193. return scope.Close(group);
  194. }
  195.  
  196.  
  197.  
  198. static Handle<Value> GetGroup(const Arguments& args) {
  199. HandleScope scope;
  200.  
  201. if ( args.Length() < 1) {
  202. return THROW_BAD_ARGS;
  203. }
  204.  
  205. if (args[1]->IsFunction()) {
  206. // Handle with EIO_CUSTOM
  207. return Undefined();
  208. } else {
  209. if (args[0]->IsString()) {
  210. String::Utf8Value grpnam(args[0]->ToString());
  211. struct group grp, *grpp = NULL;
  212. int err;
  213.  
  214. if ((err = getgrnam_r(*grpnam, &grp, getbuf, sizeof(getbuf), &grpp)) ||
  215. grpp == NULL) {
  216. return ThrowException(ErrnoException(errno, "getgrnam_r"));
  217. }
  218.  
  219. //uid = (pwdp->pw_uid);
  220. return scope.Close(BuildGroupObject(*&grpp));
  221. } else {
  222. return Undefined();
  223. }
  224. }
  225. }
  226.  
  227. */
  228.  
  229. void Os::Initialize(Handle<Object> target) {
  230. HandleScope scope;
  231.  
  232. NODE_SET_METHOD(target, "getUser", GetUser);
  233. // NODE_SET_METHOD(target, "getGroup", GetGroup);
  234.  
  235. }
  236.  
  237. } // end namespace node
Add Comment
Please, Sign In to add comment