/src/systemd/src/shared/pager.c
Line | Count | Source |
1 | | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
2 | | |
3 | | #include <stdio.h> |
4 | | #include <stdlib.h> |
5 | | #include <unistd.h> |
6 | | |
7 | | #include "sd-login.h" |
8 | | |
9 | | #include "copy.h" |
10 | | #include "env-util.h" |
11 | | #include "fd-util.h" |
12 | | #include "fileio.h" |
13 | | #include "io-util.h" |
14 | | #include "locale-util.h" |
15 | | #include "log.h" |
16 | | #include "pager.h" |
17 | | #include "process-util.h" |
18 | | #include "signal-util.h" |
19 | | #include "string-util.h" |
20 | | #include "strv.h" |
21 | | #include "terminal-util.h" |
22 | | |
23 | | static pid_t pager_pid = 0; |
24 | | |
25 | | static int stored_stdout = -1; |
26 | | static int stored_stderr = -1; |
27 | | static bool stdout_redirected = false; |
28 | | static bool stderr_redirected = false; |
29 | | |
30 | 0 | _noreturn_ static void pager_fallback(void) { |
31 | 0 | int r; |
32 | |
|
33 | 0 | r = copy_bytes(STDIN_FILENO, STDOUT_FILENO, UINT64_MAX, 0); |
34 | 0 | if (r < 0) { |
35 | 0 | log_error_errno(r, "Internal pager failed: %m"); |
36 | 0 | _exit(EXIT_FAILURE); |
37 | 0 | } |
38 | | |
39 | 0 | _exit(EXIT_SUCCESS); |
40 | 0 | } |
41 | | |
42 | 0 | static int no_quit_on_interrupt(int exe_name_fd, const char *less_opts) { |
43 | 0 | _cleanup_fclose_ FILE *file = NULL; |
44 | 0 | _cleanup_free_ char *line = NULL; |
45 | 0 | int r; |
46 | |
|
47 | 0 | assert(exe_name_fd >= 0); |
48 | 0 | assert(less_opts); |
49 | | |
50 | | /* This takes ownership of exe_name_fd */ |
51 | 0 | file = fdopen(exe_name_fd, "r"); |
52 | 0 | if (!file) { |
53 | 0 | safe_close(exe_name_fd); |
54 | 0 | return log_error_errno(errno, "Failed to create FILE object: %m"); |
55 | 0 | } |
56 | | |
57 | | /* Find the last line */ |
58 | 0 | for (;;) { |
59 | 0 | _cleanup_free_ char *t = NULL; |
60 | |
|
61 | 0 | r = read_line(file, LONG_LINE_MAX, &t); |
62 | 0 | if (r < 0) |
63 | 0 | return log_error_errno(r, "Failed to read from socket: %m"); |
64 | 0 | if (r == 0) |
65 | 0 | break; |
66 | | |
67 | 0 | free_and_replace(line, t); |
68 | 0 | } |
69 | | |
70 | | /* We only treat "less" specially. |
71 | | * Return true whenever option K is *not* set. */ |
72 | 0 | r = streq_ptr(line, "less") && !strchr(less_opts, 'K'); |
73 | |
|
74 | 0 | log_debug("Pager executable is \"%s\", options \"%s\", quit_on_interrupt: %s", |
75 | 0 | strnull(line), less_opts, yes_no(!r)); |
76 | 0 | return r; |
77 | 0 | } |
78 | | |
79 | 0 | static bool running_with_escalated_privileges(void) { |
80 | 0 | int r; |
81 | |
|
82 | 0 | if (getenv("SUDO_UID")) |
83 | 0 | return true; |
84 | | |
85 | 0 | uid_t uid; |
86 | 0 | r = sd_pid_get_owner_uid(0, &uid); |
87 | 0 | if (r < 0) { |
88 | 0 | log_debug_errno(r, "sd_pid_get_owner_uid() failed, enabling pager secure mode: %m"); |
89 | 0 | return true; |
90 | 0 | } |
91 | | |
92 | 0 | return uid != geteuid(); |
93 | 0 | } |
94 | | |
95 | 4 | void pager_open(PagerFlags flags) { |
96 | 4 | _cleanup_close_pair_ int fd[2] = EBADF_PAIR, exe_name_pipe[2] = EBADF_PAIR; |
97 | 4 | _cleanup_strv_free_ char **pager_args = NULL; |
98 | 4 | _cleanup_free_ char *l = NULL; |
99 | 4 | const char *pager, *less_opts; |
100 | 4 | int r; |
101 | | |
102 | 4 | if (flags & PAGER_DISABLE) |
103 | 4 | return; |
104 | | |
105 | 0 | if (pager_pid > 0) |
106 | 0 | return; |
107 | | |
108 | 0 | if (terminal_is_dumb()) |
109 | 0 | return; |
110 | | |
111 | 0 | if (!is_main_thread()) |
112 | 0 | return (void) log_error_errno(SYNTHETIC_ERRNO(EPERM), "Pager invoked from wrong thread."); |
113 | | |
114 | 0 | pager = getenv("SYSTEMD_PAGER"); |
115 | 0 | if (!pager) |
116 | 0 | pager = getenv("PAGER"); |
117 | |
|
118 | 0 | if (pager) { |
119 | 0 | pager_args = strv_split(pager, WHITESPACE); |
120 | 0 | if (!pager_args) |
121 | 0 | return (void) log_oom(); |
122 | | |
123 | | /* If the pager is explicitly turned off, honour it */ |
124 | 0 | if (strv_isempty(pager_args) || strv_equal(pager_args, STRV_MAKE("cat"))) |
125 | 0 | return; |
126 | 0 | } |
127 | | |
128 | | /* Determine and cache number of columns/lines before we spawn the pager so that we get the value from the |
129 | | * actual tty */ |
130 | 0 | (void) columns(); |
131 | 0 | (void) lines(); |
132 | |
|
133 | 0 | if (pipe2(fd, O_CLOEXEC) < 0) |
134 | 0 | return (void) log_error_errno(errno, "Failed to create pager pipe: %m"); |
135 | | |
136 | | /* This is a pipe to feed the name of the executed pager binary into the parent */ |
137 | 0 | if (pipe2(exe_name_pipe, O_CLOEXEC) < 0) |
138 | 0 | return (void) log_error_errno(errno, "Failed to create exe_name pipe: %m"); |
139 | | |
140 | | /* Initialize a good set of less options */ |
141 | 0 | less_opts = getenv("SYSTEMD_LESS"); |
142 | 0 | if (!less_opts) |
143 | 0 | less_opts = "FRSXMK"; |
144 | 0 | if (flags & PAGER_JUMP_TO_END) { |
145 | 0 | l = strjoin(less_opts, " +G"); |
146 | 0 | if (!l) |
147 | 0 | return (void) log_oom(); |
148 | 0 | less_opts = l; |
149 | 0 | } |
150 | | |
151 | | /* We set SIGINT as PR_DEATHSIG signal here, to match the "K" parameter we set in $LESS, which enables SIGINT behaviour. */ |
152 | 0 | r = safe_fork("(pager)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGINT|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pager_pid); |
153 | 0 | if (r < 0) |
154 | 0 | return; |
155 | 0 | if (r == 0) { |
156 | 0 | const char *less_charset; |
157 | | |
158 | | /* In the child start the pager */ |
159 | |
|
160 | 0 | if (dup2(fd[0], STDIN_FILENO) < 0) { |
161 | 0 | log_error_errno(errno, "Failed to duplicate file descriptor to STDIN: %m"); |
162 | 0 | _exit(EXIT_FAILURE); |
163 | 0 | } |
164 | | |
165 | 0 | safe_close_pair(fd); |
166 | |
|
167 | 0 | if (setenv("LESS", less_opts, 1) < 0) { |
168 | 0 | log_error_errno(errno, "Failed to set environment variable LESS: %m"); |
169 | 0 | _exit(EXIT_FAILURE); |
170 | 0 | } |
171 | | |
172 | | /* Initialize a good charset for less. This is particularly important if we output UTF-8 |
173 | | * characters. */ |
174 | 0 | less_charset = getenv("SYSTEMD_LESSCHARSET"); |
175 | 0 | if (!less_charset && is_locale_utf8()) |
176 | 0 | less_charset = "utf-8"; |
177 | 0 | if (less_charset && |
178 | 0 | setenv("LESSCHARSET", less_charset, 1) < 0) { |
179 | 0 | log_error_errno(errno, "Failed to set environment variable LESSCHARSET: %m"); |
180 | 0 | _exit(EXIT_FAILURE); |
181 | 0 | } |
182 | | |
183 | | /* People might invoke us from sudo, don't needlessly allow less to be a way to shell out |
184 | | * privileged stuff. If the user set $SYSTEMD_PAGERSECURE, trust their configuration of the |
185 | | * pager. If they didn't, use secure mode when under euid is changed. If $SYSTEMD_PAGERSECURE |
186 | | * wasn't explicitly set, and we autodetect the need for secure mode, only use the pager we |
187 | | * know to be good. */ |
188 | 0 | int use_secure_mode = secure_getenv_bool("SYSTEMD_PAGERSECURE"); |
189 | 0 | bool trust_pager = use_secure_mode >= 0; |
190 | 0 | if (use_secure_mode == -ENXIO) |
191 | 0 | use_secure_mode = running_with_escalated_privileges(); |
192 | 0 | else if (use_secure_mode < 0) { |
193 | 0 | log_warning_errno(use_secure_mode, "Unable to parse $SYSTEMD_PAGERSECURE, assuming true: %m"); |
194 | 0 | use_secure_mode = true; |
195 | 0 | } |
196 | | |
197 | | /* We generally always set variables used by less, even if we end up using a different pager. |
198 | | * They shouldn't hurt in any case, and ideally other pagers would look at them too. */ |
199 | 0 | r = set_unset_env("LESSSECURE", use_secure_mode ? "1" : NULL, true); |
200 | 0 | if (r < 0) { |
201 | 0 | log_error_errno(r, "Failed to adjust environment variable LESSSECURE: %m"); |
202 | 0 | _exit(EXIT_FAILURE); |
203 | 0 | } |
204 | | |
205 | 0 | if (trust_pager && pager_args) { /* The pager config might be set globally, and we cannot |
206 | | * know if the user adjusted it to be appropriate for the |
207 | | * secure mode. Thus, start the pager specified through |
208 | | * envvars only when $SYSTEMD_PAGERSECURE was explicitly set |
209 | | * as well. */ |
210 | 0 | r = loop_write(exe_name_pipe[1], pager_args[0], strlen(pager_args[0]) + 1); |
211 | 0 | if (r < 0) { |
212 | 0 | log_error_errno(r, "Failed to write pager name to socket: %m"); |
213 | 0 | _exit(EXIT_FAILURE); |
214 | 0 | } |
215 | | |
216 | 0 | execvp(pager_args[0], pager_args); |
217 | 0 | log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno, |
218 | 0 | "Failed to execute '%s', using fallback pagers: %m", pager_args[0]); |
219 | 0 | } |
220 | | |
221 | | /* Debian's alternatives command for pagers is called 'pager'. Note that we do not call |
222 | | * sensible-pagers here, since that is just a shell script that implements a logic that is |
223 | | * similar to this one anyway, but is Debian-specific. */ |
224 | 0 | static const char* pagers[] = { "pager", "less", "more", "(built-in)" }; |
225 | |
|
226 | 0 | for (unsigned i = 0; i < ELEMENTSOF(pagers); i++) { |
227 | | /* Only less (and our trivial fallback) implement secure mode right now. */ |
228 | 0 | if (use_secure_mode && !STR_IN_SET(pagers[i], "less", "(built-in)")) |
229 | 0 | continue; |
230 | | |
231 | 0 | r = loop_write(exe_name_pipe[1], pagers[i], strlen(pagers[i]) + 1); |
232 | 0 | if (r < 0) { |
233 | 0 | log_error_errno(r, "Failed to write pager name to socket: %m"); |
234 | 0 | _exit(EXIT_FAILURE); |
235 | 0 | } |
236 | | |
237 | 0 | if (i < ELEMENTSOF(pagers) - 1) { |
238 | 0 | execlp(pagers[i], pagers[i], NULL); |
239 | 0 | log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno, |
240 | 0 | "Failed to execute '%s', will try '%s' next: %m", pagers[i], pagers[i+1]); |
241 | 0 | } else { |
242 | | /* Close pipe to signal the parent to start sending data */ |
243 | 0 | safe_close_pair(exe_name_pipe); |
244 | 0 | pager_fallback(); |
245 | 0 | assert_not_reached(); |
246 | 0 | } |
247 | 0 | } |
248 | 0 | } |
249 | | |
250 | | /* Return in the parent */ |
251 | 0 | stored_stdout = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 3); |
252 | 0 | if (dup2(fd[1], STDOUT_FILENO) < 0) { |
253 | 0 | stored_stdout = safe_close(stored_stdout); |
254 | 0 | return (void) log_error_errno(errno, "Failed to duplicate pager pipe: %m"); |
255 | 0 | } |
256 | 0 | stdout_redirected = true; |
257 | |
|
258 | 0 | stored_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3); |
259 | 0 | if (dup2(fd[1], STDERR_FILENO) < 0) { |
260 | 0 | stored_stderr = safe_close(stored_stderr); |
261 | 0 | return (void) log_error_errno(errno, "Failed to duplicate pager pipe: %m"); |
262 | 0 | } |
263 | 0 | stderr_redirected = true; |
264 | |
|
265 | 0 | exe_name_pipe[1] = safe_close(exe_name_pipe[1]); |
266 | |
|
267 | 0 | r = no_quit_on_interrupt(TAKE_FD(exe_name_pipe[0]), less_opts); |
268 | 0 | if (r > 0) |
269 | 0 | (void) ignore_signals(SIGINT); |
270 | 0 | } |
271 | | |
272 | 0 | void pager_close(void) { |
273 | |
|
274 | 0 | if (pager_pid <= 0) |
275 | 0 | return; |
276 | | |
277 | | /* Inform pager that we are done */ |
278 | 0 | (void) fflush(stdout); |
279 | 0 | if (stdout_redirected) |
280 | 0 | if (stored_stdout < 0 || dup2(stored_stdout, STDOUT_FILENO) < 0) |
281 | 0 | (void) close(STDOUT_FILENO); |
282 | 0 | stored_stdout = safe_close(stored_stdout); |
283 | 0 | (void) fflush(stderr); |
284 | 0 | if (stderr_redirected) |
285 | 0 | if (stored_stderr < 0 || dup2(stored_stderr, STDERR_FILENO) < 0) |
286 | 0 | (void) close(STDERR_FILENO); |
287 | 0 | stored_stderr = safe_close(stored_stderr); |
288 | 0 | stdout_redirected = stderr_redirected = false; |
289 | |
|
290 | 0 | (void) kill(pager_pid, SIGCONT); |
291 | 0 | (void) wait_for_terminate(TAKE_PID(pager_pid), NULL); |
292 | 0 | pager_pid = 0; |
293 | 0 | } |
294 | | |
295 | 8 | bool pager_have(void) { |
296 | 8 | return pager_pid > 0; |
297 | 8 | } |
298 | | |
299 | 0 | int show_man_page(const char *desc, bool null_stdio) { |
300 | 0 | const char *args[4] = { "man", NULL, NULL, NULL }; |
301 | 0 | const char *e = NULL; |
302 | 0 | pid_t pid; |
303 | 0 | size_t k; |
304 | 0 | int r; |
305 | |
|
306 | 0 | k = strlen(desc); |
307 | |
|
308 | 0 | if (desc[k-1] == ')') |
309 | 0 | e = strrchr(desc, '('); |
310 | |
|
311 | 0 | if (e) { |
312 | 0 | char *page = NULL, *section = NULL; |
313 | |
|
314 | 0 | page = strndupa_safe(desc, e - desc); |
315 | 0 | section = strndupa_safe(e + 1, desc + k - e - 2); |
316 | |
|
317 | 0 | args[1] = section; |
318 | 0 | args[2] = page; |
319 | 0 | } else |
320 | 0 | args[1] = desc; |
321 | |
|
322 | 0 | r = safe_fork("(man)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|(null_stdio ? FORK_REARRANGE_STDIO : 0)|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
323 | 0 | if (r < 0) |
324 | 0 | return r; |
325 | 0 | if (r == 0) { |
326 | | /* Child */ |
327 | 0 | execvp(args[0], (char**) args); |
328 | 0 | log_error_errno(errno, "Failed to execute man: %m"); |
329 | 0 | _exit(EXIT_FAILURE); |
330 | 0 | } |
331 | | |
332 | 0 | return wait_for_terminate_and_check(NULL, pid, 0); |
333 | 0 | } |