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