summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2015-09-03 13:29:53 +0200
committerLennart Poettering <lennart@poettering.net>2015-09-04 09:07:30 +0200
commita1f686daf55636f9446a4c017f2488a35595395e (patch)
treefed98d6c92f557097cb35a055dfca55dd573e8d5 /src/basic
parentfea72cc0336f4d90875cdddc1aa9739dcbb174f5 (diff)
util: add new uid_is_valid() call
This simply factors out the uid validation checks from parse_uid() and uses them everywhere. This simply verifies that the passed UID is neither 64bit -1 nor 32bit -1.
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/util.c20
-rw-r--r--src/basic/util.h5
2 files changed, 18 insertions, 7 deletions
diff --git a/src/basic/util.c b/src/basic/util.c
index f01f5f237b..beec7cb7dd 100644
--- a/src/basic/util.c
+++ b/src/basic/util.c
@@ -373,6 +373,19 @@ int parse_pid(const char *s, pid_t* ret_pid) {
return 0;
}
+bool uid_is_valid(uid_t uid) {
+
+ /* Some libc APIs use UID_INVALID as special placeholder */
+ if (uid == (uid_t) 0xFFFFFFFF)
+ return false;
+
+ /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
+ if (uid == (uid_t) 0xFFFF)
+ return false;
+
+ return true;
+}
+
int parse_uid(const char *s, uid_t* ret_uid) {
unsigned long ul = 0;
uid_t uid;
@@ -389,12 +402,7 @@ int parse_uid(const char *s, uid_t* ret_uid) {
if ((unsigned long) uid != ul)
return -ERANGE;
- /* Some libc APIs use UID_INVALID as special placeholder */
- if (uid == (uid_t) 0xFFFFFFFF)
- return -ENXIO;
-
- /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
- if (uid == (uid_t) 0xFFFF)
+ if (!uid_is_valid(uid))
return -ENXIO;
if (ret_uid)
diff --git a/src/basic/util.h b/src/basic/util.h
index ff7a00e928..f8e32360f0 100644
--- a/src/basic/util.h
+++ b/src/basic/util.h
@@ -154,7 +154,10 @@ int parse_size(const char *t, off_t base, off_t *size);
int parse_boolean(const char *v) _pure_;
int parse_pid(const char *s, pid_t* ret_pid);
int parse_uid(const char *s, uid_t* ret_uid);
-#define parse_gid(s, ret_uid) parse_uid(s, ret_uid)
+#define parse_gid(s, ret_gid) parse_uid(s, ret_gid)
+
+bool uid_is_valid(uid_t uid);
+#define gid_is_valid(gid) uid_is_valid(gid)
int safe_atou(const char *s, unsigned *ret_u);
int safe_atoi(const char *s, int *ret_i);