summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/hostname-setup.c37
-rw-r--r--src/util.c53
-rw-r--r--src/util.h9
3 files changed, 69 insertions, 30 deletions
diff --git a/src/hostname-setup.c b/src/hostname-setup.c
index e9869bb4df..ab49767045 100644
--- a/src/hostname-setup.c
+++ b/src/hostname-setup.c
@@ -40,25 +40,8 @@
#define FILENAME "/etc/conf.d/hostname"
#endif
-static char* strip_bad_chars(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;
-
- return s;
-}
-
static int read_and_strip_hostname(const char *path, char **hn) {
- char *s, *k;
+ char *s;
int r;
assert(path);
@@ -67,20 +50,14 @@ static int read_and_strip_hostname(const char *path, char **hn) {
if ((r = read_one_line_file(path, &s)) < 0)
return r;
- k = strdup(strstrip(s));
- free(s);
-
- if (!k)
- return -ENOMEM;
-
- strip_bad_chars(k);
+ hostname_cleanup(s);
- if (k[0] == 0) {
- free(k);
+ if (isempty(s)) {
+ free(s);
return -ENOENT;
}
- *hn = k;
+ *hn = s;
return 0;
}
@@ -118,9 +95,9 @@ static int read_distro_hostname(char **hn) {
goto finish;
}
- strip_bad_chars(k);
+ hostname_cleanup(k);
- if (k[0] == 0) {
+ if (isempty(k)) {
free(k);
r = -ENOENT;
goto finish;
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",
diff --git a/src/util.h b/src/util.h
index 72ddd36e55..946179a90d 100644
--- a/src/util.h
+++ b/src/util.h
@@ -117,6 +117,10 @@ static inline bool is_path_absolute(const char *p) {
return *p == '/';
}
+static inline bool isempty(const char *p) {
+ return !p || !p[0];
+}
+
bool endswith(const char *s, const char *postfix);
bool startswith(const char *s, const char *prefix);
bool startswith_no_case(const char *s, const char *prefix);
@@ -398,6 +402,11 @@ void parse_syslog_priority(char **p, int *priority);
int have_effective_cap(int value);
+bool hostname_is_valid(const char *s);
+char* hostname_cleanup(char *s);
+
+char* strshorten(char *s, size_t l);
+
#define NULSTR_FOREACH(i, l) \
for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)