summaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2011-04-16 01:57:23 +0200
committerLennart Poettering <lennart@poettering.net>2011-04-16 02:03:35 +0200
commit9beb3f4d5c527f995e0e44308d7372fa4ea5470f (patch)
tree09c81142ac28e90e330caf0987dce341b93a5966 /src/util.c
parentf653f683d8994ca4b33f48d6adf2c6ee3c58e13e (diff)
hostname: split out hostname validation into util.c
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
index fb8df84e19..bec6e0c8e8 100644
--- a/src/util.c
+++ b/src/util.c
@@ -4248,6 +4248,59 @@ int have_effective_cap(int value) {
return r;
}
+char* strshorten(char *s, size_t l) {
+ assert(s);
+
+ if (l < strlen(s))
+ s[l] = 0;
+
+ return s;
+}
+
+static bool hostname_valid_char(char c) {
+ return
+ (c >= 'a' && c <= 'z') ||
+ (c >= 'A' && c <= 'Z') ||
+ (c >= '0' && c <= '9') ||
+ c == '-' ||
+ c == '_' ||
+ c == '.';
+}
+
+bool hostname_is_valid(const char *s) {
+ const char *p;
+
+ if (isempty(s))
+ return false;
+
+ for (p = s; *p; p++)
+ if (!hostname_valid_char(*p))
+ return false;
+
+ if (p-s > HOST_NAME_MAX)
+ return false;
+
+ return true;
+}
+
+char* hostname_cleanup(char *s) {
+ char *p, *d;
+
+ for (p = s, d = s; *p; p++)
+ if ((*p >= 'a' && *p <= 'z') ||
+ (*p >= 'A' && *p <= 'Z') ||
+ (*p >= '0' && *p <= '9') ||
+ *p == '-' ||
+ *p == '_' ||
+ *p == '.')
+ *(d++) = *p;
+
+ *d = 0;
+
+ strshorten(s, HOST_NAME_MAX);
+ return s;
+}
+
static const char *const ioprio_class_table[] = {
[IOPRIO_CLASS_NONE] = "none",
[IOPRIO_CLASS_RT] = "realtime",