glogin.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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-2015 K. Lange
  5. *
  6. * Graphical login daemon.
  7. *
  8. * Launches graphical login windows and manages login sessions.
  9. *
  10. */
  11. #include <stdlib.h>
  12. #include <assert.h>
  13. #include <unistd.h>
  14. #include <math.h>
  15. #include <string.h>
  16. #include <time.h>
  17. #include <sys/wait.h>
  18. #include <toaru/yutani.h>
  19. #include <toaru/auth.h>
  20. #include <toaru/trace.h>
  21. #define TRACE_APP_NAME "glogin"
  22. int main (int argc, char ** argv) {
  23. if (getuid() != 0) {
  24. return 1;
  25. }
  26. /* Ensure a somewhat sane environment going in */
  27. TRACE("Graphical login starting.");
  28. yutani_init();
  29. setenv("USER", "root", 1);
  30. setenv("HOME", "/", 1);
  31. setenv("SHELL", "/bin/sh", 1);
  32. setenv("PATH", "/usr/bin:/bin", 0);
  33. setenv("WM_THEME", "fancy", 0);
  34. while (1) {
  35. char * username = NULL;
  36. char * password = NULL;
  37. int uid = -1;
  38. int com_pipe[2], rep_pipe[2];
  39. pipe(com_pipe);
  40. pipe(rep_pipe);
  41. TRACE("Starting login client...");
  42. pid_t _gui_login = fork();
  43. if (!_gui_login) {
  44. dup2(com_pipe[1], STDOUT_FILENO);
  45. dup2(rep_pipe[0], STDIN_FILENO);
  46. close(com_pipe[0]);
  47. close(rep_pipe[1]);
  48. TRACE("In client...");
  49. char * args[] = {"/bin/glogin-provider", NULL};
  50. execvp(args[0], args);
  51. TRACE("Exec failure?");
  52. exit(1);
  53. }
  54. close(com_pipe[1]);
  55. close(rep_pipe[0]);
  56. FILE * com = fdopen(com_pipe[0], "r");
  57. FILE * rep = fdopen(rep_pipe[1], "w");
  58. while (!feof(com)) {
  59. char buf[1024]; /* line length? */
  60. char * cmd = fgets(buf, sizeof(buf), com);
  61. size_t r = strlen(cmd);
  62. if (cmd && r) {
  63. if (cmd[r-1] == '\n') {
  64. cmd[r-1] = '\0';
  65. r--;
  66. }
  67. if (!strcmp(buf,"RESTART")) {
  68. TRACE("Client requested system restart, rebooting.");
  69. system("reboot");
  70. } else if (!strcmp(buf,"Hello")) {
  71. TRACE("Hello received from client.");
  72. } else if (!strncmp(buf,"USER ",5)) {
  73. TRACE("Username received.");
  74. if (username) free(username);
  75. username = strdup(buf + 5);
  76. } else if (!strncmp(buf,"PASS ",5)) {
  77. TRACE("Password received.");
  78. if (password) free(password);
  79. password = strdup(buf + 5);
  80. } else if (!strcmp(buf,"AUTH")) {
  81. TRACE("Perform auth request, client wants answer.");
  82. if (!username || !password) {
  83. fprintf(rep, "FAIL\n");
  84. } else {
  85. uid = toaru_auth_check_pass(username, password);
  86. if (uid < 0) {
  87. fprintf(rep, "FAIL\n");
  88. fflush(rep);
  89. } else {
  90. fprintf(rep, "SUCC\n");
  91. fflush(rep);
  92. break;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. waitpid(_gui_login, NULL, 0);
  99. if (uid == -1) {
  100. TRACE("Not a valid session, returning login manager...");
  101. continue;
  102. }
  103. TRACE("Starting session...");
  104. pid_t _session_pid = fork();
  105. if (!_session_pid) {
  106. setuid(uid);
  107. toaru_auth_set_vars();
  108. char * args[] = {"/bin/session", NULL};
  109. execvp(args[0], args);
  110. exit(1);
  111. }
  112. waitpid(_session_pid, NULL, 0);
  113. TRACE("Session ended.");
  114. }
  115. return 0;
  116. }