users.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. char *vccd_dir;
  32. int nusers = 0;
  33. int vcc_send_req(struct klist_node *, struct vcc_request *);
  34. struct klist_node *find_connection(int fd);
  35. extern int clear_name_list(void);
  36. int users_init(void) {
  37. int fdt;
  38. if (unlikely((fdt = open("/etc/vccd-runtime", O_RDONLY)) < 0)) {
  39. perror("open()");
  40. exit(1);
  41. }
  42. if (unlikely(!(vccd_dir = malloc(PATH_MAX)))) {
  43. vcc_log("*** malloc() failed\n");
  44. exit(1);
  45. }
  46. if (unlikely(read(fdt, vccd_dir, PATH_MAX) < 0)) {
  47. perror("read()");
  48. exit(1);
  49. }
  50. close(fdt);
  51. vcc_log("vccd data directory at %s\n", vccd_dir);
  52. return 0;
  53. }
  54. int get_password(char *usr, char *passwd_buf) {
  55. struct readconf_strconf *conf;
  56. char *fname;
  57. int fdt;
  58. if (unlikely(!(fname = malloc(PATH_MAX)))) {
  59. vcc_log("*** malloc() failed\n");
  60. return 1;
  61. }
  62. sprintf(fname, "%s/users/%s", vccd_dir, usr);
  63. if (unlikely((fdt = open(fname, O_RDONLY)) < 0)) {
  64. perror("open()");
  65. return 1;
  66. }
  67. getbyte_fd = fdt;
  68. clear_name_list();
  69. readconf_init();
  70. if (unlikely(!(conf = readconf_find_strname("passwd_raw")))) {
  71. vcc_log("get password for user %s failed. ", usr);
  72. return 1;
  73. }
  74. strcpy(passwd_buf, conf->conf_value);
  75. vcc_log("password of %s: %s\n", usr, passwd_buf);
  76. close(fdt);
  77. free(fname);
  78. return 0;
  79. }
  80. int do_user_login(int fd, struct vcc_request *req) {
  81. struct vcc_request *out;
  82. char right_passwd[MSG_SIZE];
  83. const static char *join_msg = "%s logged in. now there are %d users. \n";
  84. char *t;
  85. if (unlikely(!(out = malloc(sizeof(struct vcc_request))))) {
  86. vcc_log("*** malloc() failed\n");
  87. return 1;
  88. }
  89. out->magic = htonl(VCC_MAGIC);
  90. out->reqtype = htonl(REQ_CTL_LOGIN);
  91. if (get_password(req->usrname, right_passwd) || strcmp(req->msg, right_passwd)) {
  92. vcc_log("%s:%s login failed. \n", req->usrname, req->msg);
  93. out->uid = 0;
  94. vcc_send_req(find_connection(fd), out);
  95. return 0;
  96. }
  97. vcc_log("%s logged in. \n", req->usrname);
  98. out->uid = htonl(1);
  99. if (unlikely(!(t = malloc(128)))) {
  100. vcc_log("*** malloc() failed\n");
  101. return 1;
  102. }
  103. vcc_send_req(find_connection(fd), out);
  104. /* nusers incs when it connects */
  105. sprintf(t, join_msg, req->usrname, nusers);
  106. vcc_message_broadcast(t, "system", 0);
  107. free(t);
  108. return 0;
  109. }