]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/syslog-util.c
Merge pull request #17549 from yuwata/tiny-fixes
[thirdparty/systemd.git] / src / basic / syslog-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <syslog.h>
4
5 #include "sd-id128.h"
6
7 #include "glob-util.h"
8 #include "hexdecoct.h"
9 #include "macro.h"
10 #include "path-util.h"
11 #include "string-table.h"
12 #include "syslog-util.h"
13 #include "unit-name.h"
14
15 int syslog_parse_priority(const char **p, int *priority, bool with_facility) {
16 int a = 0, b = 0, c = 0;
17 const char *end;
18 size_t k;
19
20 assert(p);
21 assert(*p);
22 assert(priority);
23
24 if ((*p)[0] != '<')
25 return 0;
26
27 end = strchr(*p, '>');
28 if (!end)
29 return 0;
30
31 k = end - *p;
32 assert(k > 0);
33
34 if (k == 2)
35 c = undecchar((*p)[1]);
36 else if (k == 3) {
37 b = undecchar((*p)[1]);
38 c = undecchar((*p)[2]);
39 } else if (k == 4) {
40 a = undecchar((*p)[1]);
41 b = undecchar((*p)[2]);
42 c = undecchar((*p)[3]);
43 } else
44 return 0;
45
46 if (a < 0 || b < 0 || c < 0 ||
47 (!with_facility && (a || b || c > 7)))
48 return 0;
49
50 if (with_facility)
51 *priority = a*100 + b*10 + c;
52 else
53 *priority = (*priority & LOG_FACMASK) | c;
54
55 *p += k + 1;
56 return 1;
57 }
58
59 static const char *const log_facility_unshifted_table[LOG_NFACILITIES] = {
60 [LOG_FAC(LOG_KERN)] = "kern",
61 [LOG_FAC(LOG_USER)] = "user",
62 [LOG_FAC(LOG_MAIL)] = "mail",
63 [LOG_FAC(LOG_DAEMON)] = "daemon",
64 [LOG_FAC(LOG_AUTH)] = "auth",
65 [LOG_FAC(LOG_SYSLOG)] = "syslog",
66 [LOG_FAC(LOG_LPR)] = "lpr",
67 [LOG_FAC(LOG_NEWS)] = "news",
68 [LOG_FAC(LOG_UUCP)] = "uucp",
69 [LOG_FAC(LOG_CRON)] = "cron",
70 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
71 [LOG_FAC(LOG_FTP)] = "ftp",
72 [LOG_FAC(LOG_LOCAL0)] = "local0",
73 [LOG_FAC(LOG_LOCAL1)] = "local1",
74 [LOG_FAC(LOG_LOCAL2)] = "local2",
75 [LOG_FAC(LOG_LOCAL3)] = "local3",
76 [LOG_FAC(LOG_LOCAL4)] = "local4",
77 [LOG_FAC(LOG_LOCAL5)] = "local5",
78 [LOG_FAC(LOG_LOCAL6)] = "local6",
79 [LOG_FAC(LOG_LOCAL7)] = "local7"
80 };
81
82 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_facility_unshifted, int, LOG_FAC(~0));
83
84 bool log_facility_unshifted_is_valid(int facility) {
85 return facility >= 0 && facility <= LOG_FAC(~0);
86 }
87
88 static const char *const log_level_table[] = {
89 [LOG_EMERG] = "emerg",
90 [LOG_ALERT] = "alert",
91 [LOG_CRIT] = "crit",
92 [LOG_ERR] = "err",
93 [LOG_WARNING] = "warning",
94 [LOG_NOTICE] = "notice",
95 [LOG_INFO] = "info",
96 [LOG_DEBUG] = "debug"
97 };
98
99 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_level, int, LOG_DEBUG);
100
101 bool log_level_is_valid(int level) {
102 return level >= 0 && level <= LOG_DEBUG;
103 }
104
105 /* The maximum size for a log namespace length. This is the file name size limit 255 minus the size of a
106 * formatted machine ID minus a separator char */
107 #define LOG_NAMESPACE_MAX (NAME_MAX - (SD_ID128_STRING_MAX - 1) - 1)
108
109 bool log_namespace_name_valid(const char *s) {
110 /* Let's make sure the namespace fits in a filename that is prefixed with the machine ID and a dot
111 * (so that /var/log/journal/<machine-id>.<namespace> can be created based on it). Also make sure it
112 * is suitable as unit instance name, and does not contain fishy characters. */
113
114 if (!filename_is_valid(s))
115 return false;
116
117 if (strlen(s) > LOG_NAMESPACE_MAX)
118 return false;
119
120 if (!unit_instance_is_valid(s))
121 return false;
122
123 if (!string_is_safe(s))
124 return false;
125
126 /* Let's avoid globbing for now */
127 if (string_is_glob(s))
128 return false;
129
130 return true;
131 }