users.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. vcc_info("WLOGFAIL %s %s %d\n", req->usrname, req->msg, fd);
  135. do_login_failed(fd, out);
  136. afree(usr, sizeof(struct user));
  137. return 1;
  138. }
  139. init_user_info(fd, req->usrname, usr);
  140. return 0;
  141. }
  142. int do_ia_login(int fd, struct vcc_request *req) {
  143. struct user *u;
  144. if (unlikely(!(u = amalloc(sizeof(struct user))))) {
  145. vcc_log("*** malloc() failed\n");
  146. return 1;
  147. }
  148. strcpy(u->username, req->usrname);
  149. init_user_info(fd, req->usrname, u);
  150. return 0;
  151. }
  152. int init_user_info(int fd, char *usrname, struct user *u) {
  153. struct klist_node *n;
  154. struct klist_node *p;
  155. static const char *join_msg = "%s logged in. now there are %d users. \n";
  156. char *t;
  157. struct vcc_request *out;
  158. if (unlikely(!(out = amalloc(sizeof(struct user))))) {
  159. vcc_log("*** malloc() failed\n");
  160. return 1;
  161. }
  162. memset(out, 0, sizeof(struct user));
  163. if (!strncmp(usrname, "system", USRNAME_SIZE)) {
  164. if (system_fd) {
  165. vcc_log("system already logged-in. \n");
  166. do_login_failed(fd, out);
  167. afree(u, sizeof(struct user));
  168. return 1;
  169. }
  170. system_fd = fd;
  171. }
  172. vcc_log("%s logged in. \n", usrname);
  173. vcc_info("ISYSLOG %s %d\n", usrname, fd);
  174. if (unlikely(!(t = amalloc(128)))) {
  175. vcc_log("*** malloc() failed\n");
  176. afree(u, sizeof(struct user));
  177. return 1;
  178. }
  179. if (unlikely(!(n = amalloc(sizeof(struct klist_node))))) {
  180. vcc_log("*** malloc() failed\n");
  181. afree(u, sizeof(struct user));
  182. return 1;
  183. }
  184. memset(t, 0, 128);
  185. klist_init(n);
  186. n->value = fd;
  187. n->data = u;
  188. klist_add(&logged_users, n);
  189. p = find_connection(fd);
  190. p->stat = STAT_LOGGED;
  191. out->magic = htonl(VCC_MAGIC);
  192. out->reqtype = htonl(REQ_CTL_LOGIN);
  193. out->uid = htonl(1);
  194. vcc_send_req(p, out);
  195. nusers++;
  196. sprintf(t, join_msg, usrname, nusers);
  197. vcc_message_broadcast(t, "system", 0, 0);
  198. afree(t, 128);
  199. return 0;
  200. }
  201. int get_user_info(int fd, struct vcc_request *req) {
  202. struct vcc_request *out;
  203. struct user *u;
  204. if (unlikely(!(out = amalloc(sizeof(struct vcc_request))))) {
  205. vcc_log("*** malloc() failed\n");
  206. return 1;
  207. }
  208. out->magic = htonl(VCC_MAGIC);
  209. out->reqtype = htonl(REQ_CTL_UINFO);
  210. u = (struct user *) out->msg;
  211. out->uid = htonl(get_user(req->msg, u));
  212. u->score = htonl(u->score);
  213. u->level = htonl(u->level);
  214. memset(u->passwd, 0, PASSWD_SIZE);
  215. vcc_send_req(find_connection(fd), out);
  216. return 0;
  217. }
  218. int do_user_logout(int fd) {
  219. struct klist_node *n;
  220. struct user *usr;
  221. static const char *leave_msg = "%s left, now there are %d users here. \n";
  222. char *t;
  223. if (unlikely(!(n = find_user(fd))))
  224. return 1;
  225. usr = n->data;
  226. if (unlikely(!(t = amalloc(128)))) {
  227. vcc_log("*** malloc() failed\n");
  228. return 1;
  229. }
  230. sync_user(usr->username, usr);
  231. if (!strncmp(usr->username, "system", USRNAME_SIZE))
  232. system_fd = 0;
  233. vcc_log("Bye, %s. \n", usr->username);
  234. quit_all_session(fd);
  235. nusers--;
  236. sprintf(t, leave_msg, usr->username, nusers);
  237. vcc_message_broadcast(t, "system", fd, 0);
  238. klist_del(&logged_users, n);
  239. afree(usr, sizeof(struct user));
  240. afree(n, sizeof(struct klist_node));
  241. return 0;
  242. }
  243. int incr_score(int fd, struct vcc_request *req) {
  244. struct vcc_request *out;
  245. struct user u;
  246. if (unlikely(!(out = amalloc(sizeof(struct vcc_request ))))) {
  247. vcc_log("*** malloc() failed\n");
  248. return 1;
  249. }
  250. out->magic = htonl(VCC_MAGIC);
  251. out->reqtype = htonl(REQ_SYS_SCRINC);
  252. if (fd != system_fd) {
  253. out->uid = htonl(-1);
  254. vcc_send_req(find_connection(fd), out);
  255. vcc_log("%d:%s tried to modify the score. \n", fd, req->usrname);
  256. vcc_info("WSCRMOD %s %d\n", req->usrname, fd);
  257. return 1;
  258. }
  259. if (unlikely(get_user(req->usrname, &u) == -1)) {
  260. out->uid = htonl(-1);
  261. vcc_send_req(find_connection(fd), out);
  262. return 1;
  263. }
  264. u.score += ntohl(req->session);
  265. if (u.score > 65535) {
  266. u.level -= u.score >> 16;
  267. u.score %= 65535;
  268. }
  269. sync_user(req->usrname, &u);
  270. out->uid = 0;
  271. vcc_send_req(find_connection(fd), out);
  272. vcc_log("%s is %d:%d scores now. \n", req->usrname, u.level, u.score);
  273. return 0;
  274. }