| 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 |
|
|---|
| 12 | #include "libbb.h"
|
|---|
| 13 | /* After libbb.h, since it needs sys/types.h on some systems */
|
|---|
| 14 | #include <sys/utsname.h>
|
|---|
| 15 |
|
|---|
| 16 | #define LOGIN " login: "
|
|---|
| 17 |
|
|---|
| 18 | static const char fmtstr_d[] ALIGN1 = "%A, %d %B %Y";
|
|---|
| 19 |
|
|---|
| 20 | void FAST_FUNC print_login_issue(const char *issue_file, const char *tty)
|
|---|
| 21 | {
|
|---|
| 22 | FILE *fp;
|
|---|
| 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 |
|
|---|
| 32 | puts("\r"); /* start a new line */
|
|---|
| 33 |
|
|---|
| 34 | fp = fopen_for_read(issue_file);
|
|---|
| 35 | if (!fp)
|
|---|
| 36 | return;
|
|---|
| 37 | while ((c = fgetc(fp)) != EOF) {
|
|---|
| 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 == '%') {
|
|---|
| 46 | c = fgetc(fp);
|
|---|
| 47 | switch (c) {
|
|---|
| 48 | case 's':
|
|---|
| 49 | outbuf = uts.sysname;
|
|---|
| 50 | break;
|
|---|
| 51 | case 'n':
|
|---|
| 52 | case 'h':
|
|---|
| 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;
|
|---|
| 64 | /* The field domainname of struct utsname is Linux specific. */
|
|---|
| 65 | #if defined(__linux__)
|
|---|
| 66 | case 'D':
|
|---|
| 67 | case 'o':
|
|---|
| 68 | outbuf = uts.domainname;
|
|---|
| 69 | break;
|
|---|
| 70 | #endif
|
|---|
| 71 | case 'd':
|
|---|
| 72 | strftime(buf, sizeof(buf), fmtstr_d, localtime(&t));
|
|---|
| 73 | break;
|
|---|
| 74 | case 't':
|
|---|
| 75 | strftime_HHMMSS(buf, sizeof(buf), &t);
|
|---|
| 76 | break;
|
|---|
| 77 | case 'l':
|
|---|
| 78 | outbuf = tty;
|
|---|
| 79 | break;
|
|---|
| 80 | default:
|
|---|
| 81 | buf[0] = c;
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 | fputs(outbuf, stdout);
|
|---|
| 85 | }
|
|---|
| 86 | fclose(fp);
|
|---|
| 87 | fflush_all();
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | void FAST_FUNC print_login_prompt(void)
|
|---|
| 91 | {
|
|---|
| 92 | char *hostname = safe_gethostname();
|
|---|
| 93 |
|
|---|
| 94 | fputs(hostname, stdout);
|
|---|
| 95 | fputs(LOGIN, stdout);
|
|---|
| 96 | fflush_all();
|
|---|
| 97 | free(hostname);
|
|---|
| 98 | }
|
|---|
| 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 | }
|
|---|