diff options
author | Lennart Poettering <lennart@poettering.net> | 2011-04-16 01:57:23 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2011-04-16 02:03:35 +0200 |
commit | 9beb3f4d5c527f995e0e44308d7372fa4ea5470f (patch) | |
tree | 09c81142ac28e90e330caf0987dce341b93a5966 /src | |
parent | cryptsetup: simplifications (diff) | |
download | systemd-9beb3f4d5c527f995e0e44308d7372fa4ea5470f.tar.xz systemd-9beb3f4d5c527f995e0e44308d7372fa4ea5470f.zip |
hostname: split out hostname validation into util.c
Diffstat (limited to 'src')
-rw-r--r-- | src/hostname-setup.c | 37 | ||||
-rw-r--r-- | src/util.c | 53 | ||||
-rw-r--r-- | src/util.h | 9 |
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) |