summaryrefslogtreecommitdiff
path: root/src/shared/journal-util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/journal-util.c')
-rw-r--r--src/shared/journal-util.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/shared/journal-util.c b/src/shared/journal-util.c
index fff3dfc9d1..eb7a75295f 100644
--- a/src/shared/journal-util.c
+++ b/src/shared/journal-util.c
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
/***
This file is part of systemd.
@@ -149,3 +150,41 @@ int journal_access_check_and_warn(sd_journal *j, bool quiet) {
return r;
}
+
+bool journal_field_valid(const char *p, size_t l, bool allow_protected) {
+ const char *a;
+
+ /* We kinda enforce POSIX syntax recommendations for
+ environment variables here, but make a couple of additional
+ requirements.
+
+ http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html */
+
+ if (l == (size_t) -1)
+ l = strlen(p);
+
+ /* No empty field names */
+ if (l <= 0)
+ return false;
+
+ /* Don't allow names longer than 64 chars */
+ if (l > 64)
+ return false;
+
+ /* Variables starting with an underscore are protected */
+ if (!allow_protected && p[0] == '_')
+ return false;
+
+ /* Don't allow digits as first character */
+ if (p[0] >= '0' && p[0] <= '9')
+ return false;
+
+ /* Only allow A-Z0-9 and '_' */
+ for (a = p; a < p + l; a++)
+ if ((*a < 'A' || *a > 'Z') &&
+ (*a < '0' || *a > '9') &&
+ *a != '_')
+ return false;
+
+ return true;
+}