users.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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, *getbyte_mmap;
  33. static int get_users(char *, int);
  34. extern int clear_name_list(void);
  35. int users_init(void) {
  36. char *users, *tp;
  37. int fdt, size;
  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. if (unlikely(!(tp = malloc(PATH_MAX)))) {
  53. vcc_log("*** malloc() failed\n");
  54. exit(1);
  55. }
  56. strcpy(tp, vccd_dir);
  57. strcat(tp, "/usrlist");
  58. if (unlikely((fdt = open(tp, O_RDONLY)) < 0)) {
  59. perror("open()");
  60. exit(1);
  61. }
  62. free(tp);
  63. size = lseek(fdt, 0, SEEK_END);
  64. if (unlikely((users = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fdt, 0)) == MAP_FAILED)) {
  65. perror("mmap()");
  66. exit(1);
  67. }
  68. get_users(users, size);
  69. munmap(users, size);
  70. return 0;
  71. }
  72. static int get_password(char *usr) {
  73. char *fname;
  74. int fdt;
  75. if (unlikely(!(fname = malloc(PATH_MAX)))) {
  76. vcc_log("*** malloc() failed\n");
  77. return 1;
  78. }
  79. sprintf(fname, "%s/users/%s", vccd_dir, usr);
  80. if (unlikely((fdt = open(fname, O_RDONLY)) < 0)) {
  81. perror("open()");
  82. return 1;
  83. }
  84. getbyte_fd = fdt;
  85. clear_name_list();
  86. readconf_init();
  87. show_names();
  88. close(fdt);
  89. free(fname);
  90. return 0;
  91. }
  92. static int get_users(char *p, int size) {
  93. struct klist_node *n;
  94. char *end = p + size, *usrname, *s;
  95. int len;
  96. for (; p < end; p++) {
  97. len = 0;
  98. s = p;
  99. while (*s != '\n' && s < end) {
  100. ++s;
  101. ++len;
  102. }
  103. if (unlikely(s == end))
  104. /* no '\n' at end of file */
  105. return 0;
  106. if (unlikely(!(usrname = strndup(p, len)))) {
  107. vcc_log("*** malloc() failed\n");
  108. continue;
  109. }
  110. if (unlikely(!(n = malloc(sizeof(struct klist_node))))) {
  111. vcc_log("*** malloc() failed\n");
  112. continue;
  113. }
  114. klist_init(n);
  115. n->data = usrname;
  116. vcc_log("user %s\n", usrname);
  117. get_password(usrname);
  118. p = s;
  119. }
  120. return 0;
  121. }