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