users.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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;
  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. int open_user_file(char *usr) {
  52. char *fname;
  53. int fdt;
  54. if (unlikely(!(fname = amalloc(PATH_MAX)))) {
  55. vcc_log("*** malloc() failed\n");
  56. return -1;
  57. }
  58. sprintf(fname, "%s/users/%s", vccd_dir, usr);
  59. if (unlikely((fdt = open(fname, O_RDONLY)) < 0)) {
  60. perror("open()");
  61. return -1;
  62. }
  63. afree(fname, PATH_MAX);
  64. return fdt;
  65. }
  66. int get_user(char *usr, struct user *buf) {
  67. int fdt;
  68. if (unlikely(((fdt = open_user_file(usr)) == -1))) {
  69. vcc_log("*** user not found. \n");
  70. return -1;
  71. }
  72. read(fdt, buf, sizeof(struct user));
  73. close(fdt);
  74. return 0;
  75. }
  76. int do_user_login(int fd, struct vcc_request *req) {
  77. struct vcc_request *out;
  78. struct user *usr;
  79. struct klist_node *n;
  80. static const char *join_msg = "%s logged in. now there are %d users. \n";
  81. char *t;
  82. if (unlikely(!(out = amalloc(sizeof(struct vcc_request))))) {
  83. vcc_log("*** malloc() failed\n");
  84. return 1;
  85. }
  86. if (unlikely(!(usr = amalloc(sizeof(struct user))))) {
  87. vcc_log("*** malloc() failed\n");
  88. return 1;
  89. }
  90. out->magic = htonl(VCC_MAGIC);
  91. out->reqtype = htonl(REQ_CTL_LOGIN);
  92. if (get_user(req->usrname, usr) || strcmp(req->msg, usr->passwd)) {
  93. vcc_log("%s:%s login failed. \n", req->usrname, req->msg);
  94. out->uid = 0;
  95. vcc_send_req(find_connection(fd), out);
  96. afree(usr, sizeof(struct user));
  97. return 0;
  98. }
  99. vcc_log("%s logged in. \n", req->usrname);
  100. if (unlikely(!(t = amalloc(128)))) {
  101. vcc_log("*** malloc() failed\n");
  102. return 1;
  103. }
  104. if (unlikely(!(n = amalloc(sizeof(struct klist_node))))) {
  105. vcc_log("*** malloc() failed\n");
  106. return 1;
  107. }
  108. klist_init(n);
  109. n->value = fd;
  110. n->data = usr;
  111. klist_add(&logged_users, n);
  112. out->uid = htonl(1);
  113. vcc_send_req(find_connection(fd), out);
  114. /* nusers already inc'ed when it connects */
  115. sprintf(t, join_msg, req->usrname, nusers);
  116. vcc_message_broadcast(t, "system", 0);
  117. afree(t, 128);
  118. return 0;
  119. }
  120. static struct klist_node *find_user(int fd) {
  121. struct klist_node *n;
  122. for (n = logged_users.next; n != &logged_users; n = n->next)
  123. if (n->value == fd)
  124. return n;
  125. vcc_log("--- user not found: %d\n", fd);
  126. return NULL;
  127. }
  128. int do_user_logout(int fd) {
  129. struct klist_node *n;
  130. struct user *usr;
  131. static const char *leave_msg = "%s left, now there are %d users here. \n";
  132. char *t;
  133. int fdt;
  134. if (unlikely(!(n = find_user(fd))))
  135. return 1;
  136. usr = n->data;
  137. if (unlikely(((fdt = open_user_file(usr->username)) == -1))) {
  138. vcc_log("*** user not found: %s. \n", usr->username);
  139. return 1;
  140. }
  141. if (unlikely(!(t = amalloc(128)))) {
  142. vcc_log("*** malloc() failed\n");
  143. return 1;
  144. }
  145. write(fdt, usr, sizeof(struct user));
  146. vcc_log("Bye, %s. \n", usr->username);
  147. sprintf(t, leave_msg, usr->username, nusers);
  148. vcc_message_broadcast(t, "system", fd);
  149. klist_del(&logged_users, n);
  150. afree(usr, sizeof(struct user));
  151. afree(n, sizeof(struct klist_node));
  152. close(fdt);
  153. return 0;
  154. }