users.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* vccd/users.c
  2. *
  3. * This file is part of vccd.
  4. *
  5. * vccd is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vccd is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vccd. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <limits.h>
  25. #include <sys/mman.h>
  26. #include <vcc/vcc.h>
  27. #include <vcc/vccd.h>
  28. #include <vcc/version.h>
  29. #include <klist.h>
  30. char *vccd_dir;
  31. int nusers = 0, system_fd = 0;
  32. struct klist_node logged_users = KLIST_NODE_INIT(&logged_users);
  33. int users_init(void) {
  34. int fdt;
  35. if (unlikely((fdt = open("/etc/vccd-runtime", O_RDONLY)) < 0)) {
  36. perror("open()");
  37. exit(1);
  38. }
  39. if (unlikely(!(vccd_dir = amalloc(PATH_MAX)))) {
  40. vcc_log("*** malloc() failed\n");
  41. exit(1);
  42. }
  43. if (unlikely(read(fdt, vccd_dir, PATH_MAX) < 0)) {
  44. perror("read()");
  45. exit(1);
  46. }
  47. close(fdt);
  48. vcc_log("vccd data directory at %s\n", vccd_dir);
  49. return 0;
  50. }
  51. static struct klist_node *find_user(int fd) {
  52. struct klist_node *n;
  53. for (n = logged_users.next; n != &logged_users; n = n->next)
  54. if (n->value == fd)
  55. return n;
  56. vcc_log("--- user not found: %d\n", fd);
  57. return NULL;
  58. }
  59. static struct user *find_user_name(char *usrname) {
  60. struct klist_node *n;
  61. struct user *u;
  62. for (n = logged_users.next; n != &logged_users; n = n->next) {
  63. u = n->data;
  64. if (!strcmp(u->username, usrname))
  65. return u;
  66. }
  67. vcc_log("--- find_user_name: not found %s. \n", usrname);
  68. return NULL;
  69. }
  70. int open_user_file(char *usr) {
  71. char *fname;
  72. int fdt;
  73. if (unlikely(!(fname = amalloc(PATH_MAX)))) {
  74. vcc_log("*** malloc() failed\n");
  75. return -1;
  76. }
  77. sprintf(fname, "%s/users/%s", vccd_dir, usr);
  78. if (unlikely((fdt = open(fname, O_RDWR)) < 0)) {
  79. perror("open()");
  80. return -1;
  81. }
  82. afree(fname, PATH_MAX);
  83. return fdt;
  84. }
  85. int get_user(char *usr, struct user *buf) {
  86. int fdt;
  87. struct user *u;
  88. if ((u = find_user_name(usr))) {
  89. memcpy(buf, u, sizeof(struct user));
  90. return 0;
  91. }
  92. if (unlikely(((fdt = open_user_file(usr)) == -1))) {
  93. vcc_log("*** user not found. \n");
  94. return -1;
  95. }
  96. read(fdt, buf, sizeof(struct user));
  97. close(fdt);
  98. return 0;
  99. }
  100. int sync_user(char *usr, struct user *buf) {
  101. struct user *u;
  102. int fdt;
  103. if ((u = find_user_name(usr)))
  104. memcpy(u, buf, sizeof(struct user));
  105. if (unlikely((fdt = open_user_file(usr)) == -1)) {
  106. vcc_log("*** user not found. \n");
  107. return -1;
  108. }
  109. write(fdt, buf, sizeof(struct user));
  110. close(fdt);
  111. return 0;
  112. }
  113. int do_user_login(int fd, struct vcc_request *req) {
  114. struct vcc_request *out;
  115. struct user *usr;
  116. struct klist_node *n;
  117. static const char *join_msg = "%s logged in. now there are %d users. \n";
  118. char *t;
  119. if (unlikely(!(out = amalloc(sizeof(struct vcc_request))))) {
  120. vcc_log("*** malloc() failed\n");
  121. return 1;
  122. }
  123. if (unlikely(!(usr = amalloc(sizeof(struct user))))) {
  124. vcc_log("*** malloc() failed\n");
  125. return 1;
  126. }
  127. out->magic = htonl(VCC_MAGIC);
  128. out->reqtype = htonl(REQ_CTL_LOGIN);
  129. if (get_user(req->usrname, usr) || strcmp(req->msg, usr->passwd)) {
  130. vcc_log("%s:%s login failed. \n", req->usrname, req->msg);
  131. out->uid = 0;
  132. vcc_send_req(find_connection(fd), out);
  133. afree(usr, sizeof(struct user));
  134. return 0;
  135. }
  136. if (!strcmp(req->usrname, "system")) {
  137. if (system_fd) {
  138. vcc_log("system already logged-in. \n");
  139. out->uid = 0;
  140. vcc_send_req(find_connection(fd), out);
  141. afree(usr, sizeof(struct user));
  142. return 0;
  143. }
  144. system_fd = fd;
  145. }
  146. vcc_log("%s logged in. \n", req->usrname);
  147. if (unlikely(!(t = amalloc(128)))) {
  148. vcc_log("*** malloc() failed\n");
  149. return 1;
  150. }
  151. if (unlikely(!(n = amalloc(sizeof(struct klist_node))))) {
  152. vcc_log("*** malloc() failed\n");
  153. return 1;
  154. }
  155. klist_init(n);
  156. n->value = fd;
  157. n->data = usr;
  158. klist_add(&logged_users, n);
  159. out->uid = htonl(1);
  160. vcc_send_req(find_connection(fd), out);
  161. /* nusers already inc'ed when it connects */
  162. sprintf(t, join_msg, req->usrname, nusers);
  163. vcc_message_broadcast(t, "system", 0);
  164. afree(t, 128);
  165. return 0;
  166. }
  167. int get_user_info(int fd, struct vcc_request *req) {
  168. struct vcc_request *out;
  169. struct user *u;
  170. if (unlikely(!(out = amalloc(sizeof(struct vcc_request))))) {
  171. vcc_log("*** malloc() failed\n");
  172. return 1;
  173. }
  174. out->magic = htonl(VCC_MAGIC);
  175. out->reqtype = htonl(REQ_CTL_UINFO);
  176. u = (struct user *) out->msg;
  177. out->uid = htonl(get_user(req->msg, u));
  178. u->score = htonl(u->score);
  179. u->level = htonl(u->level);
  180. memset(u->passwd, 0, PASSWD_SIZE);
  181. vcc_send_req(find_connection(fd), out);
  182. return 0;
  183. }
  184. int do_user_logout(int fd) {
  185. struct klist_node *n;
  186. struct user *usr;
  187. static const char *leave_msg = "%s left, now there are %d users here. \n";
  188. char *t;
  189. if (unlikely(!(n = find_user(fd))))
  190. return 1;
  191. usr = n->data;
  192. if (unlikely(!(t = amalloc(128)))) {
  193. vcc_log("*** malloc() failed\n");
  194. return 1;
  195. }
  196. sync_user(usr->username, usr);
  197. if (!strcmp(usr->username, "system"))
  198. system_fd = 0;
  199. vcc_log("Bye, %s. \n", usr->username);
  200. sprintf(t, leave_msg, usr->username, nusers);
  201. vcc_message_broadcast(t, "system", fd);
  202. klist_del(&logged_users, n);
  203. afree(usr, sizeof(struct user));
  204. afree(n, sizeof(struct klist_node));
  205. return 0;
  206. }
  207. int incr_score(int fd, struct vcc_request *req) {
  208. struct vcc_request *out;
  209. struct user u;
  210. if (unlikely(!(out = amalloc(sizeof(struct vcc_request ))))) {
  211. vcc_log("*** malloc() failed\n");
  212. return 1;
  213. }
  214. out->magic = htonl(VCC_MAGIC);
  215. out->reqtype = htonl(REQ_SYS_SCRINC);
  216. if (fd != system_fd) {
  217. out->uid = htonl(-1);
  218. vcc_send_req(find_connection(fd), out);
  219. vcc_log("%d tried to modify the score. \n", fd);
  220. return 1;
  221. }
  222. if (unlikely(get_user(req->usrname, &u) == -1)) {
  223. out->uid = htonl(-1);
  224. vcc_send_req(find_connection(fd), out);
  225. return 1;
  226. }
  227. u.score += ntohl(req->session);
  228. if (u.score > 65535) {
  229. u.level -= u.score >> 16;
  230. u.score %= 65535;
  231. }
  232. sync_user(req->usrname, &u);
  233. out->uid = 0;
  234. vcc_send_req(find_connection(fd), out);
  235. vcc_log("%s is %d:%d scores now. \n", req->usrname, u.level, u.score);
  236. return 0;
  237. }