blob: 5a7acfcf058c192413b516227f22add01b7eb727
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 | //From getty manpage (* - supported by us) |
49 | //======================================== |
50 | //4 or 4{interface} |
51 | // Insert the IPv4 address of the network interface (example: \4{eth0}). |
52 | // If the interface argument is not specified, then select the first |
53 | // fully configured (UP, non-LOOPBACK, RUNNING) interface. |
54 | //6 or 6{interface} -- The same as \4 but for IPv6. |
55 | //b -- Insert the baudrate of the current line. |
56 | //*d -- Insert the current date. |
57 | //*t -- Insert the current time. |
58 | //e or e{name} |
59 | // Translate the human-readable name to an escape sequence and insert it |
60 | // (for example: \e{red}Alert text.\e{reset}). If the name argument |
61 | // is not specified, then insert \033. The currently supported names are: |
62 | // black, blink, blue, bold, brown, cyan, darkgray, gray, green, halfbright, |
63 | // lightblue, lightcyan, lightgray, lightgreen, lightmagenta, lightred, |
64 | // magenta, red, reset, reverse, and yellow. Unknown names are ignored. |
65 | //*s |
66 | // Insert the system name (the name of the operating system - `uname -s`) |
67 | //*S or S{VARIABLE} |
68 | // Insert the VARIABLE data from /etc/os-release. |
69 | // If the VARIABLE argument is not specified, use PRETTY_NAME. |
70 | // If PRETTY_NAME is not in /etc/os-release, \S is the same as \s. |
71 | //*l -- Insert the name of the current tty line. |
72 | //*m -- Insert the architecture identifier of the machine: `uname -m`. |
73 | //*n -- Insert the nodename of the machine: `uname -n`. |
74 | //*o -- Insert the NIS domainname of the machine. Same as `hostname -d'. |
75 | //*O -- Insert the DNS domainname of the machine. |
76 | //*r -- Insert the release number of the OS: `uname -r`. |
77 | //u -- Insert the number of current users logged in. |
78 | //U -- Insert the string "1 user" or "N users" (current users logged in). |
79 | //*v -- Insert the version of the OS, e.g. the build-date etc: `uname -v`. |
80 | //We also implement: |
81 | //*D -- same as \O "DNS domainname" |
82 | //*h -- same as \n "nodename" |
83 | |
84 | case 'S': |
85 | /* minimal implementation, not reading /etc/os-release */ |
86 | /*FALLTHROUGH*/ |
87 | case 's': |
88 | outbuf = uts.sysname; |
89 | break; |
90 | case 'n': |
91 | case 'h': |
92 | outbuf = uts.nodename; |
93 | break; |
94 | case 'r': |
95 | outbuf = uts.release; |
96 | break; |
97 | case 'v': |
98 | outbuf = uts.version; |
99 | break; |
100 | case 'm': |
101 | outbuf = uts.machine; |
102 | break; |
103 | /* The field domainname of struct utsname is Linux specific. */ |
104 | #if defined(__linux__) |
105 | case 'D': |
106 | case 'o': |
107 | case 'O': |
108 | outbuf = uts.domainname; |
109 | break; |
110 | #endif |
111 | case 'd': |
112 | strftime(buf, sizeof(buf), fmtstr_d, localtime(&t)); |
113 | break; |
114 | case 't': |
115 | strftime_HHMMSS(buf, sizeof(buf), &t); |
116 | break; |
117 | case 'l': |
118 | outbuf = tty; |
119 | break; |
120 | default: |
121 | buf[0] = c; |
122 | } |
123 | } |
124 | fputs(outbuf, stdout); |
125 | } |
126 | fclose(fp); |
127 | fflush_all(); |
128 | } |
129 | |
130 | void FAST_FUNC print_login_prompt(void) |
131 | { |
132 | char *hostname = safe_gethostname(); |
133 | |
134 | fputs(hostname, stdout); |
135 | fputs(LOGIN, stdout); |
136 | fflush_all(); |
137 | free(hostname); |
138 | } |
139 | |
140 | /* Clear dangerous stuff, set PATH */ |
141 | static const char forbid[] ALIGN1 = |
142 | "ENV" "\0" |
143 | "BASH_ENV" "\0" |
144 | "HOME" "\0" |
145 | "IFS" "\0" |
146 | "SHELL" "\0" |
147 | "LD_LIBRARY_PATH" "\0" |
148 | "LD_PRELOAD" "\0" |
149 | "LD_TRACE_LOADED_OBJECTS" "\0" |
150 | "LD_BIND_NOW" "\0" |
151 | "LD_AOUT_LIBRARY_PATH" "\0" |
152 | "LD_AOUT_PRELOAD" "\0" |
153 | "LD_NOWARN" "\0" |
154 | "LD_KEEPDIR" "\0"; |
155 | |
156 | int FAST_FUNC sanitize_env_if_suid(void) |
157 | { |
158 | const char *p; |
159 | |
160 | if (getuid() == geteuid()) |
161 | return 0; |
162 | |
163 | p = forbid; |
164 | do { |
165 | unsetenv(p); |
166 | p += strlen(p) + 1; |
167 | } while (*p); |
168 | putenv((char*)bb_PATH_root_path); |
169 | |
170 | return 1; /* we indeed were run by different user! */ |
171 | } |
172 |