login.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * issue.c: issue printing code
  4. *
  5. * Copyright (C) 2003 Bastian Blank <waldi@tuxbox.org>
  6. *
  7. * Optimize and correcting OCRNL by Vladimir Oleynik <dzo@simtreas.ru>
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  10. */
  11. #include "libbb.h"
  12. /* After libbb.h, since it needs sys/types.h on some systems */
  13. #include <sys/utsname.h>
  14. #define LOGIN " login: "
  15. static const char fmtstr_d[] ALIGN1 = "%A, %d %B %Y";
  16. void FAST_FUNC print_login_issue(const char *issue_file, const char *tty)
  17. {
  18. FILE *fp;
  19. int c;
  20. char buf[256+1];
  21. const char *outbuf;
  22. time_t t;
  23. struct utsname uts;
  24. time(&t);
  25. uname(&uts);
  26. puts("\r"); /* start a new line */
  27. fp = fopen_for_read(issue_file);
  28. if (!fp)
  29. return;
  30. while ((c = fgetc(fp)) != EOF) {
  31. outbuf = buf;
  32. buf[0] = c;
  33. buf[1] = '\0';
  34. if (c == '\n') {
  35. buf[1] = '\r';
  36. buf[2] = '\0';
  37. }
  38. if (c == '\\' || c == '%') {
  39. c = fgetc(fp);
  40. switch (c) {
  41. case 's':
  42. outbuf = uts.sysname;
  43. break;
  44. case 'n':
  45. case 'h':
  46. outbuf = uts.nodename;
  47. break;
  48. case 'r':
  49. outbuf = uts.release;
  50. break;
  51. case 'v':
  52. outbuf = uts.version;
  53. break;
  54. case 'm':
  55. outbuf = uts.machine;
  56. break;
  57. /* The field domainname of struct utsname is Linux specific. */
  58. #if defined(__linux__)
  59. case 'D':
  60. case 'o':
  61. outbuf = uts.domainname;
  62. break;
  63. #endif
  64. case 'd':
  65. strftime(buf, sizeof(buf), fmtstr_d, localtime(&t));
  66. break;
  67. case 't':
  68. strftime_HHMMSS(buf, sizeof(buf), &t);
  69. break;
  70. case 'l':
  71. outbuf = tty;
  72. break;
  73. default:
  74. buf[0] = c;
  75. }
  76. }
  77. fputs(outbuf, stdout);
  78. }
  79. fclose(fp);
  80. fflush_all();
  81. }
  82. void FAST_FUNC print_login_prompt(void)
  83. {
  84. char *hostname = safe_gethostname();
  85. fputs(hostname, stdout);
  86. fputs(LOGIN, stdout);
  87. fflush_all();
  88. free(hostname);
  89. }
  90. /* Clear dangerous stuff, set PATH */
  91. static const char forbid[] ALIGN1 =
  92. "ENV" "\0"
  93. "BASH_ENV" "\0"
  94. "HOME" "\0"
  95. "IFS" "\0"
  96. "SHELL" "\0"
  97. "LD_LIBRARY_PATH" "\0"
  98. "LD_PRELOAD" "\0"
  99. "LD_TRACE_LOADED_OBJECTS" "\0"
  100. "LD_BIND_NOW" "\0"
  101. "LD_AOUT_LIBRARY_PATH" "\0"
  102. "LD_AOUT_PRELOAD" "\0"
  103. "LD_NOWARN" "\0"
  104. "LD_KEEPDIR" "\0";
  105. int FAST_FUNC sanitize_env_if_suid(void)
  106. {
  107. const char *p;
  108. if (getuid() == geteuid())
  109. return 0;
  110. p = forbid;
  111. do {
  112. unsetenv(p);
  113. p += strlen(p) + 1;
  114. } while (*p);
  115. putenv((char*)bb_PATH_root_path);
  116. return 1; /* we indeed were run by different user! */
  117. }