adduser.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* vccd/scripts/adduser.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 <unistd.h>
  19. #include <fcntl.h>
  20. #include <sys/types.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <limits.h>
  25. #include <vcc/vcc.h>
  26. #include <vcc/vccd.h>
  27. static const char usage[] = "Usage: adduser username password\n";
  28. static inline int fatal(const char *s) {
  29. printf("fatal: %s\n", s);
  30. exit(1);
  31. }
  32. static inline int warn(const char *s) {
  33. fprintf(stderr, "warning: %s\n", s);
  34. return 0;
  35. }
  36. /* put $HOME to /etc/vccd-runtime */
  37. int main(int argc, const char **argv) {
  38. struct user usr;
  39. char *runtime, *dir;
  40. int fd;
  41. memset(&usr, 0, sizeof(struct user));
  42. if (unlikely(argc != 3))
  43. fatal(usage);
  44. if (unlikely(!(runtime = malloc(PATH_MAX))))
  45. fatal("malloc() failed. ");
  46. if (unlikely(!(dir = malloc(PATH_MAX))))
  47. fatal("malloc() failed. ");
  48. if (unlikely((fd = open("/etc/vccd-runtime", O_RDONLY | O_CREAT)) < 0)) {
  49. perror(argv[0]);
  50. return 1;
  51. }
  52. read(fd, runtime, PATH_MAX);
  53. close(fd);
  54. strncpy(dir, runtime, PATH_MAX);
  55. /* in install.c, the runtime path always ends with '/' */
  56. strcat(dir, "users/");
  57. strncat(dir, argv[1], PATH_MAX);
  58. if (unlikely((fd = open(dir, O_WRONLY | O_CREAT | O_EXCL, 0644)) < 0)) {
  59. perror(argv[0]);
  60. return 1;
  61. }
  62. usr.score = 0;
  63. usr.level = MIN_LEVEL;
  64. strncpy(usr.username, argv[1], USRNAME_SIZE);
  65. strncpy(usr.passwd, argv[2], PASSWD_SIZE);
  66. write(fd, &usr, sizeof(struct user));
  67. close(fd);
  68. return 0;
  69. }