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