users.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. memset(vccd_dir, 0, PATH_MAX);
  44. if (unlikely(read(fdt, vccd_dir, PATH_MAX) < 0)) {
  45. perror("read()");
  46. exit(1);
  47. }
  48. close(fdt);
  49. vcc_log("vccd data directory at %s\n", vccd_dir);
  50. return 0;
  51. }
  52. static struct klist_node *find_user(int fd) {
  53. struct klist_node *n;
  54. for (n = logged_users.next; n != &logged_users; n = n->next)
  55. if (n->value == fd)
  56. return n;
  57. vcc_log("--- user not found: %d\n", fd);
  58. return NULL;
  59. }
  60. static struct user *find_user_name(char *usrname) {
  61. struct klist_node *n;
  62. struct user *u;
  63. for (n = logged_users.next; n != &logged_users; n = n->next) {
  64. u = n->data;
  65. if (!strncmp(u->username, usrname, USRNAME_SIZE))
  66. return u;
  67. }
  68. vcc_log("--- find_user_name: not found %s. \n", usrname);
  69. return NULL;
  70. }
  71. int open_user_file(char *usr) {
  72. char *fname;
  73. int fdt;
  74. if (unlikely(!(fname = amalloc(PATH_MAX)))) {
  75. vcc_log("*** malloc() failed\n");
  76. return -1;
  77. }
  78. sprintf(fname, "%s/users/%s", vccd_dir, usr);
  79. if (unlikely((fdt = open(fname, O_RDWR)) < 0)) {
  80. perror("open()");
  81. return -1;
  82. }
  83. afree(fname, PATH_MAX);
  84. return fdt;
  85. }
  86. int get_user(char *usr, struct user *buf) {
  87. int fdt;
  88. struct user *u;
  89. if ((u = find_user_name(usr))) {
  90. memcpy(buf, u, sizeof(struct user));
  91. return 0;
  92. }
  93. if (unlikely(((fdt = open_user_file(usr)) == -1))) {
  94. vcc_log("*** user not found. \n");
  95. return -1;
  96. }
  97. read(fdt, buf, sizeof(struct user));
  98. close(fdt);
  99. return 0;
  100. }
  101. int sync_user(char *usr, struct user *buf) {
  102. struct user *u;
  103. int fdt;
  104. if ((u = find_user_name(usr)))
  105. memcpy(u, buf, sizeof(struct user));
  106. if (unlikely((fdt = open_user_file(usr)) == -1)) {
  107. vcc_log("*** user not found. \n");
  108. return -1;
  109. }
  110. write(fdt, buf, sizeof(struct user));
  111. close(fdt);
  112. return 0;
  113. }
  114. int do_login_failed(int fd, struct vcc_request *out) {
  115. out->magic = htonl(VCC_MAGIC);
  116. out->reqtype = htonl(REQ_CTL_LOGIN);
  117. out->uid = 0;
  118. vcc_send_req(find_connection(fd), out);
  119. return 0;
  120. }
  121. int do_user_login(int fd, struct vcc_request *req) {
  122. struct user *usr;
  123. struct vcc_request *out;
  124. if (unlikely(!(out = amalloc(sizeof(struct vcc_request))))) {
  125. vcc_log("*** malloc() failed\n");
  126. return 1;
  127. }
  128. if (unlikely(!(usr = amalloc(sizeof(struct user))))) {
  129. vcc_log("*** malloc() failed\n");
  130. return 1;
  131. }
  132. if (get_user(req->usrname, usr) || strncmp(req->msg, usr->passwd, PASSWD_SIZE)) {
  133. vcc_log("%s:%s login failed. \n", req->usrname, req->msg);
  134. do_login_failed(fd, out);
  135. afree(usr, sizeof(struct user));
  136. return 0;
  137. }
  138. init_user_info(fd, req->usrname, usr);
  139. return 0;
  140. }
  141. int do_ia_login(int fd, struct vcc_request *req) {
  142. struct user *u;
  143. if (unlikely(!(u = amalloc(sizeof(struct user))))) {
  144. vcc_log("*** malloc() failed\n");
  145. return 1;
  146. }
  147. strcpy(u->username, req->usrname);
  148. init_user_info(fd, req->usrname, u);
  149. return 0;
  150. }
  151. int init_user_info(int fd, char *usrname, struct user *u) {
  152. struct klist_node *n;
  153. static const char *join_msg = "%s logged in. now there are %d users. \n";
  154. char *t;
  155. struct vcc_request *out;
  156. if (unlikely(!(out = amalloc(sizeof(struct user))))) {
  157. vcc_log("*** malloc() failed\n");
  158. return 1;
  159. }
  160. memset(out, 0, sizeof(struct user));
  161. if (!strncmp(usrname, "system", USRNAME_SIZE)) {
  162. if (system_fd) {
  163. vcc_log("system already logged-in. \n");
  164. do_login_failed(fd, out);
  165. afree(u, sizeof(struct user));
  166. return 1;
  167. }
  168. system_fd = fd;
  169. }
  170. vcc_log("%s logged in. \n", usrname);
  171. if (unlikely(!(t = amalloc(128)))) {
  172. vcc_log("*** malloc() failed\n");
  173. afree(u, sizeof(struct user));
  174. return 1;
  175. }
  176. if (unlikely(!(n = amalloc(sizeof(struct klist_node))))) {
  177. vcc_log("*** malloc() failed\n");
  178. afree(u, sizeof(struct user));
  179. return 1;
  180. }
  181. memset(t, 0, 128);
  182. klist_init(n);
  183. n->value = fd;
  184. n->data = u;
  185. klist_add(&logged_users, n);
  186. out->magic = htonl(VCC_MAGIC);
  187. out->reqtype = htonl(REQ_CTL_LOGIN);
  188. out->uid = htonl(1);
  189. vcc_send_req(find_connection(fd), out);
  190. /* nusers already inc'ed when it connects */
  191. sprintf(t, join_msg, usrname, nusers);
  192. vcc_message_broadcast(t, "system", 0, 0);
  193. afree(t, 128);
  194. return 0;
  195. }
  196. int get_user_info(int fd, struct vcc_request *req) {
  197. struct vcc_request *out;
  198. struct user *u;
  199. if (unlikely(!(out = amalloc(sizeof(struct vcc_request))))) {
  200. vcc_log("*** malloc() failed\n");
  201. return 1;
  202. }
  203. out->magic = htonl(VCC_MAGIC);
  204. out->reqtype = htonl(REQ_CTL_UINFO);
  205. u = (struct user *) out->msg;
  206. out->uid = htonl(get_user(req->msg, u));
  207. u->score = htonl(u->score);
  208. u->level = htonl(u->level);
  209. memset(u->passwd, 0, PASSWD_SIZE);
  210. vcc_send_req(find_connection(fd), out);
  211. return 0;
  212. }
  213. int do_user_logout(int fd) {
  214. struct klist_node *n;
  215. struct user *usr;
  216. static const char *leave_msg = "%s left, now there are %d users here. \n";
  217. char *t;
  218. if (unlikely(!(n = find_user(fd))))
  219. return 1;
  220. usr = n->data;
  221. if (unlikely(!(t = amalloc(128)))) {
  222. vcc_log("*** malloc() failed\n");
  223. return 1;
  224. }
  225. sync_user(usr->username, usr);
  226. if (!strncmp(usr->username, "system", USRNAME_SIZE))
  227. system_fd = 0;
  228. vcc_log("Bye, %s. \n", usr->username);
  229. sprintf(t, leave_msg, usr->username, nusers);
  230. vcc_message_broadcast(t, "system", fd, 0);
  231. klist_del(&logged_users, n);
  232. afree(usr, sizeof(struct user));
  233. afree(n, sizeof(struct klist_node));
  234. return 0;
  235. }
  236. int incr_score(int fd, struct vcc_request *req) {
  237. struct vcc_request *out;
  238. struct user u;
  239. if (unlikely(!(out = amalloc(sizeof(struct vcc_request ))))) {
  240. vcc_log("*** malloc() failed\n");
  241. return 1;
  242. }
  243. out->magic = htonl(VCC_MAGIC);
  244. out->reqtype = htonl(REQ_SYS_SCRINC);
  245. if (fd != system_fd) {
  246. out->uid = htonl(-1);
  247. vcc_send_req(find_connection(fd), out);
  248. vcc_log("%d:%s tried to modify the score. \n", fd, req->usrname);
  249. return 1;
  250. }
  251. if (unlikely(get_user(req->usrname, &u) == -1)) {
  252. out->uid = htonl(-1);
  253. vcc_send_req(find_connection(fd), out);
  254. return 1;
  255. }
  256. u.score += ntohl(req->session);
  257. if (u.score > 65535) {
  258. u.level -= u.score >> 16;
  259. u.score %= 65535;
  260. }
  261. sync_user(req->usrname, &u);
  262. out->uid = 0;
  263. vcc_send_req(find_connection(fd), out);
  264. vcc_log("%s is %d:%d scores now. \n", req->usrname, u.level, u.score);
  265. return 0;
  266. }