login.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <toaru/auth.h>
  24. #define LINE_LEN 1024
  25. uint32_t child = 0;
  26. void sig_pass(int sig) {
  27. /* Pass onto the shell */
  28. if (child) {
  29. kill(child, sig);
  30. }
  31. /* Else, ignore */
  32. }
  33. void sig_segv(int sig) {
  34. printf("Segmentation fault.\n");
  35. exit(127 + sig);
  36. /* no return */
  37. }
  38. int main(int argc, char ** argv) {
  39. char * user = NULL;
  40. int uid;
  41. pid_t pid, f;
  42. int opt;
  43. while ((opt = getopt(argc, argv, "f:")) != -1) {
  44. switch (opt) {
  45. case 'f':
  46. user = optarg;
  47. break;
  48. }
  49. }
  50. if (user) {
  51. struct passwd * pw = getpwnam(user);
  52. if (pw) {
  53. uid = pw->pw_uid;
  54. goto do_fork;
  55. } else {
  56. fprintf(stderr, "%s: no such user\n", argv[0]);
  57. return 1;
  58. }
  59. }
  60. printf("\n");
  61. system("uname -a");
  62. printf("\n");
  63. signal(SIGINT, sig_pass);
  64. signal(SIGWINCH, sig_pass);
  65. signal(SIGSEGV, sig_segv);
  66. while (1) {
  67. char username[1024] = {0};
  68. char password[1024] = {0};
  69. /* TODO: gethostname() */
  70. char _hostname[256];
  71. gethostname(_hostname, 255);
  72. fprintf(stdout, "%s login: ", _hostname);
  73. fflush(stdout);
  74. char * r = fgets(username, 1024, stdin);
  75. if (!r) {
  76. clearerr(stdin);
  77. fprintf(stderr, "\n");
  78. sleep(2);
  79. fprintf(stderr, "\nLogin failed.\n");
  80. continue;
  81. }
  82. username[strlen(username)-1] = '\0';
  83. if (!strcmp(username, "reboot")) {
  84. /* Quick hack so vga text mode login can exit */
  85. system("reboot");
  86. }
  87. fprintf(stdout, "password: ");
  88. fflush(stdout);
  89. /* Disable echo */
  90. struct termios old, new;
  91. tcgetattr(fileno(stdin), &old);
  92. new = old;
  93. new.c_lflag &= (~ECHO);
  94. tcsetattr(fileno(stdin), TCSAFLUSH, &new);
  95. r = fgets(password, 1024, stdin);
  96. if (!r) {
  97. clearerr(stdin);
  98. tcsetattr(fileno(stdin), TCSAFLUSH, &old);
  99. fprintf(stderr, "\n");
  100. sleep(2);
  101. fprintf(stderr, "\nLogin failed.\n");
  102. continue;
  103. }
  104. password[strlen(password)-1] = '\0';
  105. tcsetattr(fileno(stdin), TCSAFLUSH, &old);
  106. fprintf(stdout, "\n");
  107. uid = toaru_auth_check_pass(username, password);
  108. if (uid < 0) {
  109. sleep(2);
  110. fprintf(stdout, "\nLogin failed.\n");
  111. continue;
  112. }
  113. break;
  114. }
  115. system("cat /etc/motd");
  116. do_fork:
  117. pid = getpid();
  118. f = fork();
  119. if (getpid() != pid) {
  120. setuid(uid);
  121. toaru_auth_set_vars();
  122. char * args[] = {
  123. getenv("SHELL"),
  124. NULL
  125. };
  126. execvp(args[0], args);
  127. return 1;
  128. } else {
  129. child = f;
  130. int result;
  131. do {
  132. result = waitpid(f, NULL, 0);
  133. } while (result < 0);
  134. }
  135. child = 0;
  136. return 0;
  137. }