users.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include <readconf.h>
  31. struct klist_node user_list = KLIST_NODE_INIT(&user_list);
  32. char *vccd_dir;
  33. int nusers = 0;
  34. static int get_users(char *, int);
  35. int vcc_send_req(struct klist_node *, struct vcc_request *);
  36. struct klist_node *find_connection(int fd);
  37. extern int clear_name_list(void);
  38. int users_init(void) {
  39. char *users, *tp;
  40. int fdt, size;
  41. if (unlikely((fdt = open("/etc/vccd-runtime", O_RDONLY)) < 0)) {
  42. perror("open()");
  43. exit(1);
  44. }
  45. if (unlikely(!(vccd_dir = malloc(PATH_MAX)))) {
  46. vcc_log("*** malloc() failed\n");
  47. exit(1);
  48. }
  49. if (unlikely(read(fdt, vccd_dir, PATH_MAX) < 0)) {
  50. perror("read()");
  51. exit(1);
  52. }
  53. close(fdt);
  54. vcc_log("vccd data directory at %s\n", vccd_dir);
  55. if (unlikely(!(tp = malloc(PATH_MAX)))) {
  56. vcc_log("*** malloc() failed\n");
  57. exit(1);
  58. }
  59. strcpy(tp, vccd_dir);
  60. strcat(tp, "/usrlist");
  61. if (unlikely((fdt = open(tp, O_RDONLY)) < 0)) {
  62. perror("open()");
  63. exit(1);
  64. }
  65. free(tp);
  66. size = lseek(fdt, 0, SEEK_END);
  67. if (unlikely((users = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fdt, 0)) == MAP_FAILED)) {
  68. perror("mmap()");
  69. exit(1);
  70. }
  71. get_users(users, size);
  72. munmap(users, size);
  73. return 0;
  74. }
  75. int get_password(char *usr, char *passwd_buf) {
  76. struct readconf_strconf *conf;
  77. char *fname;
  78. int fdt;
  79. if (unlikely(!(fname = malloc(PATH_MAX)))) {
  80. vcc_log("*** malloc() failed\n");
  81. return 1;
  82. }
  83. sprintf(fname, "%s/users/%s", vccd_dir, usr);
  84. if (unlikely((fdt = open(fname, O_RDONLY)) < 0)) {
  85. perror("open()");
  86. return 1;
  87. }
  88. getbyte_fd = fdt;
  89. clear_name_list();
  90. readconf_init();
  91. if (unlikely(!(conf = readconf_find_strname("passwd_raw")))) {
  92. vcc_log("get password for user %s failed. ", usr);
  93. return 1;
  94. }
  95. strcpy(passwd_buf, conf->conf_value);
  96. vcc_log("password of %s: %s\n", usr, passwd_buf);
  97. close(fdt);
  98. free(fname);
  99. return 0;
  100. }
  101. static int get_users(char *p, int size) {
  102. struct klist_node *n;
  103. char *end = p + size, *usrname, *s;
  104. int len;
  105. for (; p < end; p++) {
  106. len = 0;
  107. s = p;
  108. while (*s != '\n' && s < end) {
  109. ++s;
  110. ++len;
  111. }
  112. if (unlikely(s == end))
  113. /* no '\n' at end of file */
  114. return 0;
  115. if (unlikely(!(usrname = strndup(p, len)))) {
  116. vcc_log("*** malloc() failed\n");
  117. continue;
  118. }
  119. if (unlikely(!(n = malloc(sizeof(struct klist_node))))) {
  120. vcc_log("*** malloc() failed\n");
  121. continue;
  122. }
  123. klist_init(n);
  124. n->data = usrname;
  125. vcc_log("user %s\n", usrname);
  126. p = s;
  127. }
  128. return 0;
  129. }
  130. int do_user_login(int fd, struct vcc_request *req) {
  131. struct vcc_request *out;
  132. char right_passwd[MSG_SIZE];
  133. const static char *join_msg = "%s logged in. now there are %d users. \n";
  134. char *t;
  135. if (unlikely(!(out = malloc(sizeof(struct vcc_request))))) {
  136. vcc_log("*** malloc() failed\n");
  137. return 1;
  138. }
  139. out->magic = htonl(VCC_MAGIC);
  140. out->reqtype = htonl(REQ_CTL_LOGIN);
  141. if (get_password(req->usrname, right_passwd) || strcmp(req->msg, right_passwd)) {
  142. vcc_log("%s:%s login failed. \n", req->usrname, req->msg);
  143. out->uid = 0;
  144. vcc_send_req(find_connection(fd), out);
  145. return 0;
  146. }
  147. vcc_log("%s logged in. \n", req->usrname);
  148. out->uid = htonl(1);
  149. if (unlikely(!(t = malloc(128)))) {
  150. vcc_log("*** malloc() failed\n");
  151. return 1;
  152. }
  153. vcc_send_req(find_connection(fd), out);
  154. /* nusers incs when it connects */
  155. sprintf(t, join_msg, req->usrname, nusers);
  156. vcc_message_broadcast(t, "system", 0);
  157. free(t);
  158. return 0;
  159. }