]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
4f37cbd9 ZJS |
2 | |
3 | #include "acl-util.h" | |
4 | #include "fs-util.h" | |
5 | #include "hashmap.h" | |
6 | #include "journal-internal.h" | |
7 | #include "journal-util.h" | |
8 | #include "log.h" | |
9 | #include "strv.h" | |
10 | #include "user-util.h" | |
11 | ||
e79d0b59 | 12 | static int access_check_var_log_journal(sd_journal *j, bool want_other_users) { |
349cc4a5 | 13 | #if HAVE_ACL |
4f37cbd9 ZJS |
14 | _cleanup_strv_free_ char **g = NULL; |
15 | const char* dir; | |
16 | #endif | |
17 | int r; | |
18 | ||
19 | assert(j); | |
20 | ||
21 | /* If we are root, we should have access, don't warn. */ | |
22 | if (getuid() == 0) | |
23 | return 0; | |
24 | ||
25 | /* If we are in the 'systemd-journal' group, we should have | |
26 | * access too. */ | |
27 | r = in_group("systemd-journal"); | |
28 | if (r < 0) | |
29 | return log_error_errno(r, "Failed to check if we are in the 'systemd-journal' group: %m"); | |
30 | if (r > 0) | |
31 | return 0; | |
32 | ||
349cc4a5 | 33 | #if HAVE_ACL |
4f37cbd9 ZJS |
34 | if (laccess("/run/log/journal", F_OK) >= 0) |
35 | dir = "/run/log/journal"; | |
36 | else | |
37 | dir = "/var/log/journal"; | |
38 | ||
39 | /* If we are in any of the groups listed in the journal ACLs, | |
40 | * then all is good, too. Let's enumerate all groups from the | |
41 | * default ACL of the directory, which generally should allow | |
42 | * access to most journal files too. */ | |
43 | r = acl_search_groups(dir, &g); | |
44 | if (r < 0) | |
45 | return log_error_errno(r, "Failed to search journal ACL: %m"); | |
46 | if (r > 0) | |
47 | return 0; | |
48 | ||
49 | /* Print a pretty list, if there were ACLs set. */ | |
50 | if (!strv_isempty(g)) { | |
51 | _cleanup_free_ char *s = NULL; | |
52 | ||
5238e957 | 53 | /* There are groups in the ACL, let's list them */ |
4f37cbd9 ZJS |
54 | r = strv_extend(&g, "systemd-journal"); |
55 | if (r < 0) | |
56 | return log_oom(); | |
57 | ||
58 | strv_sort(g); | |
59 | strv_uniq(g); | |
60 | ||
61 | s = strv_join(g, "', '"); | |
62 | if (!s) | |
63 | return log_oom(); | |
64 | ||
e79d0b59 | 65 | log_notice("Hint: You are currently not seeing messages from %s.\n" |
4f37cbd9 | 66 | " Users in groups '%s' can see all messages.\n" |
e79d0b59 ZJS |
67 | " Pass -q to turn off this notice.", |
68 | want_other_users ? "other users and the system" : "the system", | |
69 | s); | |
4f37cbd9 ZJS |
70 | return 1; |
71 | } | |
72 | #endif | |
73 | ||
74 | /* If no ACLs were found, print a short version of the message. */ | |
e79d0b59 | 75 | log_notice("Hint: You are currently not seeing messages from %s.\n" |
4f37cbd9 | 76 | " Users in the 'systemd-journal' group can see all messages. Pass -q to\n" |
e79d0b59 ZJS |
77 | " turn off this notice.", |
78 | want_other_users ? "other users and the system" : "the system"); | |
4f37cbd9 ZJS |
79 | |
80 | return 1; | |
81 | } | |
82 | ||
1a8f0ce6 ZJS |
83 | int journal_access_blocked(sd_journal *j) { |
84 | return hashmap_contains(j->errors, INT_TO_PTR(-EACCES)); | |
85 | } | |
86 | ||
e79d0b59 | 87 | int journal_access_check_and_warn(sd_journal *j, bool quiet, bool want_other_users) { |
4f37cbd9 ZJS |
88 | void *code; |
89 | char *path; | |
90 | int r = 0; | |
91 | ||
92 | assert(j); | |
93 | ||
94 | if (hashmap_isempty(j->errors)) { | |
95 | if (ordered_hashmap_isempty(j->files) && !quiet) | |
96 | log_notice("No journal files were found."); | |
97 | ||
98 | return 0; | |
99 | } | |
100 | ||
1a8f0ce6 | 101 | if (journal_access_blocked(j)) { |
4f37cbd9 | 102 | if (!quiet) |
e79d0b59 | 103 | (void) access_check_var_log_journal(j, want_other_users); |
4f37cbd9 ZJS |
104 | |
105 | if (ordered_hashmap_isempty(j->files)) | |
106 | r = log_error_errno(EACCES, "No journal files were opened due to insufficient permissions."); | |
107 | } | |
108 | ||
90e74a66 | 109 | HASHMAP_FOREACH_KEY(path, code, j->errors) { |
4f37cbd9 ZJS |
110 | int err; |
111 | ||
112 | err = abs(PTR_TO_INT(code)); | |
113 | ||
114 | switch (err) { | |
115 | case EACCES: | |
116 | continue; | |
117 | ||
118 | case ENODATA: | |
119 | log_warning_errno(err, "Journal file %s is truncated, ignoring file.", path); | |
120 | break; | |
121 | ||
122 | case EPROTONOSUPPORT: | |
123 | log_warning_errno(err, "Journal file %1$s uses an unsupported feature, ignoring file.\n" | |
124 | "Use SYSTEMD_LOG_LEVEL=debug journalctl --file=%1$s to see the details.", | |
125 | path); | |
126 | break; | |
127 | ||
128 | case EBADMSG: | |
129 | log_warning_errno(err, "Journal file %s corrupted, ignoring file.", path); | |
130 | break; | |
131 | ||
132 | default: | |
133 | log_warning_errno(err, "An error was encountered while opening journal file or directory %s, ignoring file: %m", path); | |
134 | break; | |
135 | } | |
136 | } | |
137 | ||
138 | return r; | |
139 | } |