login.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2013-2014 K. Lange
  5. *
  6. * Login Service
  7. *
  8. * Provides the user with a login prompt and starts their session.
  9. *
  10. */
  11. #include <stdio.h>
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <time.h>
  17. #include <signal.h>
  18. #include <termios.h>
  19. #include <errno.h>
  20. #include <pwd.h>
  21. #include <sys/wait.h>
  22. #include <sys/utsname.h>
  23. #include <sys/ioctl.h>
  24. #include <toaru/auth.h>
  25. #define LINE_LEN 1024
  26. uint32_t child = 0;
  27. void sig_pass(int sig) {
  28. /* Pass onto the shell */
  29. if (child) {
  30. kill(child, sig);
  31. }
  32. /* Else, ignore */
  33. }
  34. void sig_segv(int sig) {
  35. printf("Segmentation fault.\n");
  36. exit(127 + sig);
  37. /* no return */
  38. }
  39. int main(int argc, char ** argv) {
  40. char * user = NULL;
  41. int uid;
  42. pid_t pid, f;
  43. int opt;
  44. while ((opt = getopt(argc, argv, "f:")) != -1) {
  45. switch (opt) {
  46. case 'f':
  47. user = optarg;
  48. break;
  49. }
  50. }
  51. if (user) {
  52. struct passwd * pw = getpwnam(user);
  53. if (pw) {
  54. uid = pw->pw_uid;
  55. goto do_fork;
  56. } else {
  57. fprintf(stderr, "%s: no such user\n", argv[0]);
  58. return 1;
  59. }
  60. }
  61. printf("\n");
  62. system("uname -a");
  63. printf("\n");
  64. signal(SIGINT, sig_pass);
  65. signal(SIGWINCH, sig_pass);
  66. signal(SIGSEGV, sig_segv);
  67. while (1) {
  68. char username[1024] = {0};
  69. char password[1024] = {0};
  70. /* TODO: gethostname() */
  71. char _hostname[256];
  72. gethostname(_hostname, 255);
  73. fprintf(stdout, "%s login: ", _hostname);
  74. fflush(stdout);
  75. char * r = fgets(username, 1024, stdin);
  76. if (!r) {
  77. clearerr(stdin);
  78. fprintf(stderr, "\n");
  79. sleep(2);
  80. fprintf(stderr, "\nLogin failed.\n");
  81. continue;
  82. }
  83. username[strlen(username)-1] = '\0';
  84. if (!strcmp(username, "reboot")) {
  85. /* Quick hack so vga text mode login can exit */
  86. system("reboot");
  87. }
  88. fprintf(stdout, "password: ");
  89. fflush(stdout);
  90. /* Disable echo */
  91. struct termios old, new;
  92. tcgetattr(fileno(stdin), &old);
  93. new = old;
  94. new.c_lflag &= (~ECHO);
  95. tcsetattr(fileno(stdin), TCSAFLUSH, &new);
  96. r = fgets(password, 1024, stdin);
  97. if (!r) {
  98. clearerr(stdin);
  99. tcsetattr(fileno(stdin), TCSAFLUSH, &old);
  100. fprintf(stderr, "\n");
  101. sleep(2);
  102. fprintf(stderr, "\nLogin failed.\n");
  103. continue;
  104. }
  105. password[strlen(password)-1] = '\0';
  106. tcsetattr(fileno(stdin), TCSAFLUSH, &old);
  107. fprintf(stdout, "\n");
  108. uid = toaru_auth_check_pass(username, password);
  109. if (uid < 0) {
  110. sleep(2);
  111. fprintf(stdout, "\nLogin failed.\n");
  112. continue;
  113. }
  114. break;
  115. }
  116. system("cat /etc/motd");
  117. do_fork:
  118. pid = getpid();
  119. f = fork();
  120. if (getpid() != pid) {
  121. ioctl(STDIN_FILENO, IOCTLTTYLOGIN, &uid);
  122. setuid(uid);
  123. setsid();
  124. toaru_auth_set_vars();
  125. char * args[] = {
  126. getenv("SHELL"),
  127. NULL
  128. };
  129. execvp(args[0], args);
  130. return 1;
  131. } else {
  132. child = f;
  133. int result;
  134. do {
  135. result = waitpid(f, NULL, 0);
  136. } while (result < 0);
  137. }
  138. child = 0;
  139. return 0;
  140. }