]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal: move valid_user_field() to journal-util.[ch] and rename it → journal_field_v...
authorLennart Poettering <lennart@poettering.net>
Mon, 30 Oct 2017 18:53:01 +0000 (19:53 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 16 Nov 2017 11:40:17 +0000 (12:40 +0100)
Being able to validate journal field names is useful outside of the
journal itself.

src/journal/journald-native.c
src/shared/journal-util.c
src/shared/journal-util.h

index 1ff4e29849120351e14d45dbae412d9ab77acaf7..0953324cc65a42bede83ac5ff9a0dd40c458c5ef 100644 (file)
@@ -28,6 +28,7 @@
 #include "fs-util.h"
 #include "io-util.h"
 #include "journal-importer.h"
+#include "journal-util.h"
 #include "journald-console.h"
 #include "journald-kmsg.h"
 #include "journald-native.h"
 #include "string-util.h"
 #include "unaligned.h"
 
-bool valid_user_field(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 */
-
-        /* 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;
-}
-
 static bool allow_object_pid(const struct ucred *ucred) {
         return ucred && ucred->uid == 0;
 }
@@ -201,7 +167,7 @@ static int server_process_entry(
 
                 q = memchr(p, '=', e - p);
                 if (q) {
-                        if (valid_user_field(p, q - p, false)) {
+                        if (journal_field_valid(p, q - p, false)) {
                                 size_t l;
 
                                 l = e - p;
@@ -257,7 +223,7 @@ static int server_process_entry(
                         k[e - p] = '=';
                         memcpy(k + (e - p) + 1, e + 1 + sizeof(uint64_t), l);
 
-                        if (valid_user_field(p, e - p, false)) {
+                        if (journal_field_valid(p, e - p, false)) {
                                 iovec[n].iov_base = k;
                                 iovec[n].iov_len = (e - p) + 1 + l;
                                 entry_size += iovec[n].iov_len;
index fff3dfc9d1364847c41a52ea913db8f4c84857ea..82f193ffd88885ad3079e61ed73cd87e667ff5a0 100644 (file)
@@ -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;
+}
index 499e6c62ecf1717a46de4053611d64df440b5768..95613bba10fbf53002937e36d4962cc97d8c9aeb 100644 (file)
 ***/
 
 #include <stdbool.h>
+#include <sys/types.h>
 
 #include "sd-journal.h"
 
+bool journal_field_valid(const char *p, size_t l, bool allow_protected);
+
 int journal_access_check_and_warn(sd_journal *j, bool quiet);