users.c 2.8 KB

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