summaryrefslogtreecommitdiff
path: root/src/shared/journal-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2017-10-30 19:53:01 +0100
committerLennart Poettering <lennart@poettering.net>2017-11-16 12:40:17 +0100
commit53978b98f9ecc16dca216e8dab17d0d5622c9056 (patch)
tree58378560fa9f1f98e09c347b1e009be05fb876fd /src/shared/journal-util.c
parenteabd4eb934e729b076415420937b2dbc42c1993a (diff)
downloadsystemd-53978b98f9ecc16dca216e8dab17d0d5622c9056.tar.gz
journal: move valid_user_field() to journal-util.[ch] and rename it → journal_field_valid()
Being able to validate journal field names is useful outside of the journal itself.
Diffstat (limited to 'src/shared/journal-util.c')
-rw-r--r--src/shared/journal-util.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/shared/journal-util.c b/src/shared/journal-util.c
index fff3dfc9d1..82f193ffd8 100644
--- a/src/shared/journal-util.c
+++ b/src/shared/journal-util.c
@@ -149,3 +149,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;
+}