From c004493cdefc1f43a3956ca529e8070f8d70be56 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 25 Oct 2015 14:08:25 +0100 Subject: util-lib: split out IO related calls to io-util.[ch] --- src/libsystemd/sd-id128/sd-id128.c | 3 ++- src/libsystemd/sd-login/sd-login.c | 1 + src/libsystemd/sd-resolve/sd-resolve.c | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) (limited to 'src/libsystemd') diff --git a/src/libsystemd/sd-id128/sd-id128.c b/src/libsystemd/sd-id128/sd-id128.c index ef89fd2572..335ad3b1b4 100644 --- a/src/libsystemd/sd-id128/sd-id128.c +++ b/src/libsystemd/sd-id128/sd-id128.c @@ -25,10 +25,11 @@ #include "sd-id128.h" +#include "fd-util.h" +#include "io-util.h" #include "macro.h" #include "random-util.h" #include "util.h" -#include "fd-util.h" _public_ char *sd_id128_to_string(sd_id128_t id, char s[SD_ID128_STRING_MAX]) { unsigned n; diff --git a/src/libsystemd/sd-login/sd-login.c b/src/libsystemd/sd-login/sd-login.c index 399918f834..e1f480d058 100644 --- a/src/libsystemd/sd-login/sd-login.c +++ b/src/libsystemd/sd-login/sd-login.c @@ -33,6 +33,7 @@ #include "fileio.h" #include "formats-util.h" #include "hostname-util.h" +#include "io-util.h" #include "login-util.h" #include "macro.h" #include "string-util.h" diff --git a/src/libsystemd/sd-resolve/sd-resolve.c b/src/libsystemd/sd-resolve/sd-resolve.c index 38e2dc0fdd..724405d534 100644 --- a/src/libsystemd/sd-resolve/sd-resolve.c +++ b/src/libsystemd/sd-resolve/sd-resolve.c @@ -34,6 +34,7 @@ #include "sd-resolve.h" #include "fd-util.h" +#include "io-util.h" #include "list.h" #include "missing.h" #include "resolve-util.h" -- cgit v1.2.3-54-g00ecf From b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 25 Oct 2015 22:32:30 +0100 Subject: util-lib: split out user/group/uid/gid calls into user-util.[ch] --- Makefile.am | 2 + src/basic/audit.c | 1 + src/basic/cgroup-util.c | 1 + src/basic/process-util.c | 1 + src/basic/user-util.c | 403 +++++++++++++++++++++++++++++++++++ src/basic/user-util.h | 55 +++++ src/basic/util.c | 375 +------------------------------- src/basic/util.h | 25 --- src/bus-proxyd/bus-proxyd.c | 1 + src/bus-proxyd/bus-xml-policy.c | 3 +- src/bus-proxyd/stdio-bridge.c | 1 + src/core/bus-policy.c | 5 +- src/core/execute.c | 1 + src/core/main.c | 1 + src/core/socket.c | 1 + src/core/timer.c | 3 +- src/core/unit-printf.c | 3 +- src/core/unit.c | 3 +- src/journal/coredump-vacuum.c | 1 + src/journal/coredump.c | 1 + src/journal/coredumpctl.c | 1 + src/journal/journalctl.c | 1 + src/libsystemd/sd-bus/bus-socket.c | 1 + src/libsystemd/sd-bus/busctl.c | 1 + src/libsystemd/sd-login/sd-login.c | 1 + src/libsystemd/sd-path/sd-path.c | 1 + src/login/inhibit.c | 1 + src/login/loginctl.c | 1 + src/login/logind-action.c | 1 + src/login/logind-core.c | 1 + src/login/logind-dbus.c | 1 + src/login/logind-inhibit.c | 1 + src/login/logind-session.c | 1 + src/login/logind-user-dbus.c | 7 +- src/login/logind-utmp.c | 14 +- src/machine/machined-dbus.c | 1 + src/network/networkd-netdev-tuntap.c | 3 +- src/network/networkd.c | 4 +- src/nspawn/nspawn-setuid.c | 1 + src/nspawn/nspawn.c | 1 + src/nss-mymachines/nss-mymachines.c | 1 + src/resolve/resolved.c | 11 +- src/run/run.c | 1 + src/shared/acl-util.c | 3 +- src/shared/install-printf.c | 5 +- src/shared/uid-range.c | 3 +- src/shared/utmp-wtmp.c | 1 + src/systemctl/systemctl.c | 1 + src/sysusers/sysusers.c | 1 + src/test/test-ipcrm.c | 3 +- src/test/test-util.c | 1 + src/timesync/timesyncd.c | 1 + src/tmpfiles/tmpfiles.c | 1 + src/udev/udev-rules.c | 1 + 54 files changed, 539 insertions(+), 426 deletions(-) create mode 100644 src/basic/user-util.c create mode 100644 src/basic/user-util.h (limited to 'src/libsystemd') diff --git a/Makefile.am b/Makefile.am index 459d54460c..69a2b732d0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -787,6 +787,8 @@ libbasic_la_SOURCES = \ src/basic/string-util.h \ src/basic/fd-util.c \ src/basic/fd-util.h \ + src/basic/user-util.c \ + src/basic/user-util.h \ src/basic/extract-word.c \ src/basic/extract-word.h \ src/basic/escape.c \ diff --git a/src/basic/audit.c b/src/basic/audit.c index af43ec8097..c9b762151a 100644 --- a/src/basic/audit.c +++ b/src/basic/audit.c @@ -27,6 +27,7 @@ #include "fileio.h" #include "macro.h" #include "process-util.h" +#include "user-util.h" #include "util.h" int audit_session_from_pid(pid_t pid, uint32_t *id) { diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c index 958497543a..4af991200c 100644 --- a/src/basic/cgroup-util.c +++ b/src/basic/cgroup-util.c @@ -43,6 +43,7 @@ #include "special.h" #include "string-util.h" #include "unit-name.h" +#include "user-util.h" #include "util.h" int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) { diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 949bd1f64d..65c93792fb 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -35,6 +35,7 @@ #include "process-util.h" #include "signal-util.h" #include "string-util.h" +#include "user-util.h" #include "util.h" int get_process_state(pid_t pid) { diff --git a/src/basic/user-util.c b/src/basic/user-util.c new file mode 100644 index 0000000000..637391f4a7 --- /dev/null +++ b/src/basic/user-util.c @@ -0,0 +1,403 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include + +#include "user-util.h" +#include "macro.h" +#include "util.h" +#include "string-util.h" +#include "path-util.h" + +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; + int r; + + assert(s); + + r = safe_atolu(s, &ul); + if (r < 0) + return r; + + uid = (uid_t) ul; + + if ((unsigned long) uid != ul) + return -ERANGE; + + if (!uid_is_valid(uid)) + return -ENXIO; /* we return ENXIO instead of EINVAL + * here, to make it easy to distuingish + * invalid numeric uids invalid + * strings. */ + + if (ret_uid) + *ret_uid = uid; + + return 0; +} + +char *lookup_uid(uid_t uid) { + long bufsize; + char *name; + _cleanup_free_ char *buf = NULL; + struct passwd pwbuf, *pw = NULL; + + /* Shortcut things to avoid NSS lookups */ + if (uid == 0) + return strdup("root"); + + bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); + if (bufsize <= 0) + bufsize = 4096; + + buf = malloc(bufsize); + if (!buf) + return NULL; + + if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) + return strdup(pw->pw_name); + + if (asprintf(&name, UID_FMT, uid) < 0) + return NULL; + + return name; +} + +char* getlogname_malloc(void) { + uid_t uid; + struct stat st; + + if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0) + uid = st.st_uid; + else + uid = getuid(); + + return lookup_uid(uid); +} + +char *getusername_malloc(void) { + const char *e; + + e = getenv("USER"); + if (e) + return strdup(e); + + return lookup_uid(getuid()); +} + +int get_user_creds( + const char **username, + uid_t *uid, gid_t *gid, + const char **home, + const char **shell) { + + struct passwd *p; + uid_t u; + + assert(username); + assert(*username); + + /* We enforce some special rules for uid=0: in order to avoid + * NSS lookups for root we hardcode its data. */ + + if (streq(*username, "root") || streq(*username, "0")) { + *username = "root"; + + if (uid) + *uid = 0; + + if (gid) + *gid = 0; + + if (home) + *home = "/root"; + + if (shell) + *shell = "/bin/sh"; + + return 0; + } + + if (parse_uid(*username, &u) >= 0) { + errno = 0; + p = getpwuid(u); + + /* If there are multiple users with the same id, make + * sure to leave $USER to the configured value instead + * of the first occurrence in the database. However if + * the uid was configured by a numeric uid, then let's + * pick the real username from /etc/passwd. */ + if (p) + *username = p->pw_name; + } else { + errno = 0; + p = getpwnam(*username); + } + + if (!p) + return errno > 0 ? -errno : -ESRCH; + + if (uid) + *uid = p->pw_uid; + + if (gid) + *gid = p->pw_gid; + + if (home) + *home = p->pw_dir; + + if (shell) + *shell = p->pw_shell; + + return 0; +} + +int get_group_creds(const char **groupname, gid_t *gid) { + struct group *g; + gid_t id; + + assert(groupname); + + /* We enforce some special rules for gid=0: in order to avoid + * NSS lookups for root we hardcode its data. */ + + if (streq(*groupname, "root") || streq(*groupname, "0")) { + *groupname = "root"; + + if (gid) + *gid = 0; + + return 0; + } + + if (parse_gid(*groupname, &id) >= 0) { + errno = 0; + g = getgrgid(id); + + if (g) + *groupname = g->gr_name; + } else { + errno = 0; + g = getgrnam(*groupname); + } + + if (!g) + return errno > 0 ? -errno : -ESRCH; + + if (gid) + *gid = g->gr_gid; + + return 0; +} + +char* uid_to_name(uid_t uid) { + struct passwd *p; + char *r; + + if (uid == 0) + return strdup("root"); + + p = getpwuid(uid); + if (p) + return strdup(p->pw_name); + + if (asprintf(&r, UID_FMT, uid) < 0) + return NULL; + + return r; +} + +char* gid_to_name(gid_t gid) { + struct group *p; + char *r; + + if (gid == 0) + return strdup("root"); + + p = getgrgid(gid); + if (p) + return strdup(p->gr_name); + + if (asprintf(&r, GID_FMT, gid) < 0) + return NULL; + + return r; +} + +int in_gid(gid_t gid) { + gid_t *gids; + int ngroups_max, r, i; + + if (getgid() == gid) + return 1; + + if (getegid() == gid) + return 1; + + ngroups_max = sysconf(_SC_NGROUPS_MAX); + assert(ngroups_max > 0); + + gids = alloca(sizeof(gid_t) * ngroups_max); + + r = getgroups(ngroups_max, gids); + if (r < 0) + return -errno; + + for (i = 0; i < r; i++) + if (gids[i] == gid) + return 1; + + return 0; +} + +int in_group(const char *name) { + int r; + gid_t gid; + + r = get_group_creds(&name, &gid); + if (r < 0) + return r; + + return in_gid(gid); +} + +int get_home_dir(char **_h) { + struct passwd *p; + const char *e; + char *h; + uid_t u; + + assert(_h); + + /* Take the user specified one */ + e = secure_getenv("HOME"); + if (e && path_is_absolute(e)) { + h = strdup(e); + if (!h) + return -ENOMEM; + + *_h = h; + return 0; + } + + /* Hardcode home directory for root to avoid NSS */ + u = getuid(); + if (u == 0) { + h = strdup("/root"); + if (!h) + return -ENOMEM; + + *_h = h; + return 0; + } + + /* Check the database... */ + errno = 0; + p = getpwuid(u); + if (!p) + return errno > 0 ? -errno : -ESRCH; + + if (!path_is_absolute(p->pw_dir)) + return -EINVAL; + + h = strdup(p->pw_dir); + if (!h) + return -ENOMEM; + + *_h = h; + return 0; +} + +int get_shell(char **_s) { + struct passwd *p; + const char *e; + char *s; + uid_t u; + + assert(_s); + + /* Take the user specified one */ + e = getenv("SHELL"); + if (e) { + s = strdup(e); + if (!s) + return -ENOMEM; + + *_s = s; + return 0; + } + + /* Hardcode home directory for root to avoid NSS */ + u = getuid(); + if (u == 0) { + s = strdup("/bin/sh"); + if (!s) + return -ENOMEM; + + *_s = s; + return 0; + } + + /* Check the database... */ + errno = 0; + p = getpwuid(u); + if (!p) + return errno > 0 ? -errno : -ESRCH; + + if (!path_is_absolute(p->pw_shell)) + return -EINVAL; + + s = strdup(p->pw_shell); + if (!s) + return -ENOMEM; + + *_s = s; + return 0; +} + +int reset_uid_gid(void) { + + if (setgroups(0, NULL) < 0) + return -errno; + + if (setresgid(0, 0, 0) < 0) + return -errno; + + if (setresuid(0, 0, 0) < 0) + return -errno; + + return 0; +} diff --git a/src/basic/user-util.h b/src/basic/user-util.h new file mode 100644 index 0000000000..9263ede741 --- /dev/null +++ b/src/basic/user-util.h @@ -0,0 +1,55 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +#pragma once + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include + +bool uid_is_valid(uid_t uid); + +static inline bool gid_is_valid(gid_t gid) { + return uid_is_valid((uid_t) gid); +} + +int parse_uid(const char *s, uid_t* ret_uid); + +static inline int parse_gid(const char *s, gid_t *ret_gid) { + return parse_uid(s, (uid_t*) ret_gid); +} + +char* lookup_uid(uid_t uid); +char* getlogname_malloc(void); +char* getusername_malloc(void); + +int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell); +int get_group_creds(const char **groupname, gid_t *gid); + +char* uid_to_name(uid_t uid); +char* gid_to_name(gid_t gid); + +int in_gid(gid_t gid); +int in_group(const char *name); + +int get_home_dir(char **ret); +int get_shell(char **_ret); + +int reset_uid_gid(void); diff --git a/src/basic/util.c b/src/basic/util.c index c02dfc5bc9..010261b37d 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -98,6 +98,7 @@ #include "string-util.h" #include "strv.h" #include "terminal-util.h" +#include "user-util.h" #include "utf8.h" #include "util.h" #include "virt.h" @@ -168,47 +169,6 @@ 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; - int r; - - assert(s); - - r = safe_atolu(s, &ul); - if (r < 0) - return r; - - uid = (uid_t) ul; - - if ((unsigned long) uid != ul) - return -ERANGE; - - if (!uid_is_valid(uid)) - return -ENXIO; /* we return ENXIO instead of EINVAL - * here, to make it easy to distuingish - * invalid numeric uids invalid - * strings. */ - - if (ret_uid) - *ret_uid = uid; - - return 0; -} - int safe_atou(const char *s, unsigned *ret_u) { char *x = NULL; unsigned long l; @@ -1437,55 +1397,6 @@ void rename_process(const char name[8]) { } } -char *lookup_uid(uid_t uid) { - long bufsize; - char *name; - _cleanup_free_ char *buf = NULL; - struct passwd pwbuf, *pw = NULL; - - /* Shortcut things to avoid NSS lookups */ - if (uid == 0) - return strdup("root"); - - bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); - if (bufsize <= 0) - bufsize = 4096; - - buf = malloc(bufsize); - if (!buf) - return NULL; - - if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) - return strdup(pw->pw_name); - - if (asprintf(&name, UID_FMT, uid) < 0) - return NULL; - - return name; -} - -char* getlogname_malloc(void) { - uid_t uid; - struct stat st; - - if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0) - uid = st.st_uid; - else - uid = getuid(); - - return lookup_uid(uid); -} - -char *getusername_malloc(void) { - const char *e; - - e = getenv("USER"); - if (e) - return strdup(e); - - return lookup_uid(getuid()); -} - bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) { assert(s); assert_cc(sizeof(statfs_f_type_t) >= sizeof(s->f_type)); @@ -2074,182 +1985,6 @@ int socket_from_display(const char *display, char **path) { return 0; } -int get_user_creds( - const char **username, - uid_t *uid, gid_t *gid, - const char **home, - const char **shell) { - - struct passwd *p; - uid_t u; - - assert(username); - assert(*username); - - /* We enforce some special rules for uid=0: in order to avoid - * NSS lookups for root we hardcode its data. */ - - if (streq(*username, "root") || streq(*username, "0")) { - *username = "root"; - - if (uid) - *uid = 0; - - if (gid) - *gid = 0; - - if (home) - *home = "/root"; - - if (shell) - *shell = "/bin/sh"; - - return 0; - } - - if (parse_uid(*username, &u) >= 0) { - errno = 0; - p = getpwuid(u); - - /* If there are multiple users with the same id, make - * sure to leave $USER to the configured value instead - * of the first occurrence in the database. However if - * the uid was configured by a numeric uid, then let's - * pick the real username from /etc/passwd. */ - if (p) - *username = p->pw_name; - } else { - errno = 0; - p = getpwnam(*username); - } - - if (!p) - return errno > 0 ? -errno : -ESRCH; - - if (uid) - *uid = p->pw_uid; - - if (gid) - *gid = p->pw_gid; - - if (home) - *home = p->pw_dir; - - if (shell) - *shell = p->pw_shell; - - return 0; -} - -char* uid_to_name(uid_t uid) { - struct passwd *p; - char *r; - - if (uid == 0) - return strdup("root"); - - p = getpwuid(uid); - if (p) - return strdup(p->pw_name); - - if (asprintf(&r, UID_FMT, uid) < 0) - return NULL; - - return r; -} - -char* gid_to_name(gid_t gid) { - struct group *p; - char *r; - - if (gid == 0) - return strdup("root"); - - p = getgrgid(gid); - if (p) - return strdup(p->gr_name); - - if (asprintf(&r, GID_FMT, gid) < 0) - return NULL; - - return r; -} - -int get_group_creds(const char **groupname, gid_t *gid) { - struct group *g; - gid_t id; - - assert(groupname); - - /* We enforce some special rules for gid=0: in order to avoid - * NSS lookups for root we hardcode its data. */ - - if (streq(*groupname, "root") || streq(*groupname, "0")) { - *groupname = "root"; - - if (gid) - *gid = 0; - - return 0; - } - - if (parse_gid(*groupname, &id) >= 0) { - errno = 0; - g = getgrgid(id); - - if (g) - *groupname = g->gr_name; - } else { - errno = 0; - g = getgrnam(*groupname); - } - - if (!g) - return errno > 0 ? -errno : -ESRCH; - - if (gid) - *gid = g->gr_gid; - - return 0; -} - -int in_gid(gid_t gid) { - gid_t *gids; - int ngroups_max, r, i; - - if (getgid() == gid) - return 1; - - if (getegid() == gid) - return 1; - - ngroups_max = sysconf(_SC_NGROUPS_MAX); - assert(ngroups_max > 0); - - gids = alloca(sizeof(gid_t) * ngroups_max); - - r = getgroups(ngroups_max, gids); - if (r < 0) - return -errno; - - for (i = 0; i < r; i++) - if (gids[i] == gid) - return 1; - - return 0; -} - -int in_group(const char *name) { - int r; - gid_t gid; - - r = get_group_creds(&name, &gid); - if (r < 0) - return r; - - return in_gid(gid); -} - int glob_exists(const char *path) { _cleanup_globfree_ glob_t g = {}; int k; @@ -2864,100 +2599,6 @@ bool in_initrd(void) { return saved; } -int get_home_dir(char **_h) { - struct passwd *p; - const char *e; - char *h; - uid_t u; - - assert(_h); - - /* Take the user specified one */ - e = secure_getenv("HOME"); - if (e && path_is_absolute(e)) { - h = strdup(e); - if (!h) - return -ENOMEM; - - *_h = h; - return 0; - } - - /* Hardcode home directory for root to avoid NSS */ - u = getuid(); - if (u == 0) { - h = strdup("/root"); - if (!h) - return -ENOMEM; - - *_h = h; - return 0; - } - - /* Check the database... */ - errno = 0; - p = getpwuid(u); - if (!p) - return errno > 0 ? -errno : -ESRCH; - - if (!path_is_absolute(p->pw_dir)) - return -EINVAL; - - h = strdup(p->pw_dir); - if (!h) - return -ENOMEM; - - *_h = h; - return 0; -} - -int get_shell(char **_s) { - struct passwd *p; - const char *e; - char *s; - uid_t u; - - assert(_s); - - /* Take the user specified one */ - e = getenv("SHELL"); - if (e) { - s = strdup(e); - if (!s) - return -ENOMEM; - - *_s = s; - return 0; - } - - /* Hardcode home directory for root to avoid NSS */ - u = getuid(); - if (u == 0) { - s = strdup("/bin/sh"); - if (!s) - return -ENOMEM; - - *_s = s; - return 0; - } - - /* Check the database... */ - errno = 0; - p = getpwuid(u); - if (!p) - return errno > 0 ? -errno : -ESRCH; - - if (!path_is_absolute(p->pw_shell)) - return -EINVAL; - - s = strdup(p->pw_shell); - if (!s) - return -ENOMEM; - - *_s = s; - return 0; -} - bool filename_is_valid(const char *p) { if (isempty(p)) @@ -4631,20 +4272,6 @@ int mount_move_root(const char *path) { return 0; } -int reset_uid_gid(void) { - - if (setgroups(0, NULL) < 0) - return -errno; - - if (setresgid(0, 0, 0) < 0) - return -errno; - - if (setresuid(0, 0, 0) < 0) - return -errno; - - return 0; -} - int getxattr_malloc(const char *path, const char *name, char **value, bool allow_symlink) { char *v; size_t l; diff --git a/src/basic/util.h b/src/basic/util.h index 1a56257cce..7d6412523c 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -91,14 +91,7 @@ int parse_size(const char *t, uint64_t base, uint64_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_gid) parse_uid(s, ret_gid) -bool uid_is_valid(uid_t uid); - -static inline bool gid_is_valid(gid_t gid) { - return uid_is_valid((uid_t) gid); -} int safe_atou(const char *s, unsigned *ret_u); int safe_atoi(const char *s, int *ret_i); @@ -252,10 +245,6 @@ static inline int dir_is_populated(const char *path) { return !r; } -char* lookup_uid(uid_t uid); -char* getlogname_malloc(void); -char* getusername_malloc(void); - int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid); int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid); @@ -304,15 +293,6 @@ int fchmod_umask(int fd, mode_t mode); bool display_is_local(const char *display) _pure_; int socket_from_display(const char *display, char **path); -int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell); -int get_group_creds(const char **groupname, gid_t *gid); - -int in_gid(gid_t gid); -int in_group(const char *name); - -char* uid_to_name(uid_t uid); -char* gid_to_name(gid_t gid); - int glob_exists(const char *path); int glob_extend(char ***strv, const char *path); @@ -378,9 +358,6 @@ bool http_etag_is_valid(const char *etag); bool in_initrd(void); -int get_home_dir(char **ret); -int get_shell(char **_ret); - static inline void freep(void *p) { free(*(void**) p); } @@ -720,8 +697,6 @@ int parse_mode(const char *s, mode_t *ret); int mount_move_root(const char *path); -int reset_uid_gid(void); - int getxattr_malloc(const char *path, const char *name, char **value, bool allow_symlink); int fgetxattr_malloc(int fd, const char *name, char **value); diff --git a/src/bus-proxyd/bus-proxyd.c b/src/bus-proxyd/bus-proxyd.c index 1bbf984a2e..7e7574568c 100644 --- a/src/bus-proxyd/bus-proxyd.c +++ b/src/bus-proxyd/bus-proxyd.c @@ -43,6 +43,7 @@ #include "proxy.h" #include "string-util.h" #include "strv.h" +#include "user-util.h" #include "util.h" static char *arg_address = NULL; diff --git a/src/bus-proxyd/bus-xml-policy.c b/src/bus-proxyd/bus-xml-policy.c index c5a1e09cf8..56dcfeab62 100644 --- a/src/bus-proxyd/bus-xml-policy.c +++ b/src/bus-proxyd/bus-xml-policy.c @@ -22,14 +22,15 @@ #include "sd-login.h" #include "bus-internal.h" +#include "bus-xml-policy.h" #include "conf-files.h" #include "fileio.h" #include "formats-util.h" #include "set.h" #include "string-util.h" #include "strv.h" +#include "user-util.h" #include "xml.h" -#include "bus-xml-policy.h" static void policy_item_free(PolicyItem *i) { assert(i); diff --git a/src/bus-proxyd/stdio-bridge.c b/src/bus-proxyd/stdio-bridge.c index 168fc9ead0..a009ea76c4 100644 --- a/src/bus-proxyd/stdio-bridge.c +++ b/src/bus-proxyd/stdio-bridge.c @@ -37,6 +37,7 @@ #include "log.h" #include "proxy.h" #include "strv.h" +#include "user-util.h" #include "util.h" static char *arg_address = NULL; diff --git a/src/core/bus-policy.c b/src/core/bus-policy.c index a6a8fcd4d3..2490903a8c 100644 --- a/src/core/bus-policy.c +++ b/src/core/bus-policy.c @@ -19,10 +19,11 @@ #include -#include "kdbus.h" -#include "util.h" #include "bus-kernel.h" #include "bus-policy.h" +#include "kdbus.h" +#include "user-util.h" +#include "util.h" int bus_kernel_translate_access(BusPolicyAccess access) { assert(access >= 0); diff --git a/src/core/execute.c b/src/core/execute.c index 55da8ba4b7..3f2607ff1a 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -90,6 +90,7 @@ #include "strv.h" #include "terminal-util.h" #include "unit.h" +#include "user-util.h" #include "util.h" #include "utmp-wtmp.h" diff --git a/src/core/main.c b/src/core/main.c index 68ec730406..b0ca6fa10e 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -76,6 +76,7 @@ #include "strv.h" #include "switch-root.h" #include "terminal-util.h" +#include "user-util.h" #include "virt.h" #include "watchdog.h" diff --git a/src/core/socket.c b/src/core/socket.c index e2085dac1c..7f401025ed 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -54,6 +54,7 @@ #include "unit-name.h" #include "unit-printf.h" #include "unit.h" +#include "user-util.h" static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = { [SOCKET_DEAD] = UNIT_INACTIVE, diff --git a/src/core/timer.c b/src/core/timer.c index 4548a4fa52..908d45ac73 100644 --- a/src/core/timer.c +++ b/src/core/timer.c @@ -26,9 +26,10 @@ #include "dbus-timer.h" #include "special.h" #include "string-util.h" +#include "timer.h" #include "unit-name.h" #include "unit.h" -#include "timer.h" +#include "user-util.h" static const UnitActiveState state_translation_table[_TIMER_STATE_MAX] = { [TIMER_DEAD] = UNIT_INACTIVE, diff --git a/src/core/unit-printf.c b/src/core/unit-printf.c index 5b62f2d65c..4a5c7efdb0 100644 --- a/src/core/unit-printf.c +++ b/src/core/unit-printf.c @@ -26,8 +26,9 @@ #include "string-util.h" #include "strv.h" #include "unit-name.h" -#include "unit.h" #include "unit-printf.h" +#include "unit.h" +#include "user-util.h" static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) { Unit *u = userdata; diff --git a/src/core/unit.c b/src/core/unit.c index a054cc79b0..572b1c1b78 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -51,8 +51,9 @@ #include "string-util.h" #include "strv.h" #include "unit-name.h" -#include "virt.h" #include "unit.h" +#include "user-util.h" +#include "virt.h" const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = { [UNIT_SERVICE] = &service_vtable, diff --git a/src/journal/coredump-vacuum.c b/src/journal/coredump-vacuum.c index 92259fd5ef..bad6ea4242 100644 --- a/src/journal/coredump-vacuum.c +++ b/src/journal/coredump-vacuum.c @@ -27,6 +27,7 @@ #include "macro.h" #include "string-util.h" #include "time-util.h" +#include "user-util.h" #include "util.h" #define DEFAULT_MAX_USE_LOWER (uint64_t) (1ULL*1024ULL*1024ULL) /* 1 MiB */ diff --git a/src/journal/coredump.c b/src/journal/coredump.c index 3b87eb5355..7336db219b 100644 --- a/src/journal/coredump.c +++ b/src/journal/coredump.c @@ -52,6 +52,7 @@ #include "stacktrace.h" #include "string-util.h" #include "strv.h" +#include "user-util.h" #include "util.h" /* The maximum size up to which we process coredumps */ diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c index a497a4781b..f891ddffc6 100644 --- a/src/journal/coredumpctl.c +++ b/src/journal/coredumpctl.c @@ -42,6 +42,7 @@ #include "string-util.h" #include "terminal-util.h" #include "util.h" +#include "user-util.h" static enum { ACTION_NONE, diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index dee25841e1..6e452a4d51 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -62,6 +62,7 @@ #include "strv.h" #include "terminal-util.h" #include "unit-name.h" +#include "user-util.h" #define DEFAULT_FSS_INTERVAL_USEC (15*USEC_PER_MINUTE) diff --git a/src/libsystemd/sd-bus/bus-socket.c b/src/libsystemd/sd-bus/bus-socket.c index 3273d9b0c2..42d7f80461 100644 --- a/src/libsystemd/sd-bus/bus-socket.c +++ b/src/libsystemd/sd-bus/bus-socket.c @@ -36,6 +36,7 @@ #include "missing.h" #include "signal-util.h" #include "string-util.h" +#include "user-util.h" #include "utf8.h" #include "util.h" diff --git a/src/libsystemd/sd-bus/busctl.c b/src/libsystemd/sd-bus/busctl.c index b55e81ce97..d3e846db84 100644 --- a/src/libsystemd/sd-bus/busctl.c +++ b/src/libsystemd/sd-bus/busctl.c @@ -37,6 +37,7 @@ #include "set.h" #include "strv.h" #include "terminal-util.h" +#include "user-util.h" #include "util.h" static bool arg_no_pager = false; diff --git a/src/libsystemd/sd-login/sd-login.c b/src/libsystemd/sd-login/sd-login.c index e1f480d058..6012004a0b 100644 --- a/src/libsystemd/sd-login/sd-login.c +++ b/src/libsystemd/sd-login/sd-login.c @@ -38,6 +38,7 @@ #include "macro.h" #include "string-util.h" #include "strv.h" +#include "user-util.h" #include "util.h" /* Error codes: diff --git a/src/libsystemd/sd-path/sd-path.c b/src/libsystemd/sd-path/sd-path.c index 2467c126ae..8e3eeb15d8 100644 --- a/src/libsystemd/sd-path/sd-path.c +++ b/src/libsystemd/sd-path/sd-path.c @@ -25,6 +25,7 @@ #include "path-util.h" #include "string-util.h" #include "strv.h" +#include "user-util.h" #include "util.h" static int from_environment(const char *envname, const char *fallback, const char **ret) { diff --git a/src/login/inhibit.c b/src/login/inhibit.c index 5ff0957aed..f79f89af7e 100644 --- a/src/login/inhibit.c +++ b/src/login/inhibit.c @@ -34,6 +34,7 @@ #include "process-util.h" #include "signal-util.h" #include "strv.h" +#include "user-util.h" #include "util.h" static const char* arg_what = "idle:sleep:shutdown"; diff --git a/src/login/loginctl.c b/src/login/loginctl.c index bfc8716009..1cd186dec3 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -42,6 +42,7 @@ #include "sysfs-show.h" #include "terminal-util.h" #include "unit-name.h" +#include "user-util.h" #include "util.h" #include "verbs.h" diff --git a/src/login/logind-action.c b/src/login/logind-action.c index a44e369149..f06f8edc07 100644 --- a/src/login/logind-action.c +++ b/src/login/logind-action.c @@ -30,6 +30,7 @@ #include "formats-util.h" #include "process-util.h" #include "terminal-util.h" +#include "user-util.h" int manager_handle_action( Manager *m, diff --git a/src/login/logind-core.c b/src/login/logind-core.c index 3b6e982e9f..c2541e490b 100644 --- a/src/login/logind-core.c +++ b/src/login/logind-core.c @@ -33,6 +33,7 @@ #include "strv.h" #include "terminal-util.h" #include "udev-util.h" +#include "user-util.h" int manager_add_device(Manager *m, const char *sysfs, bool master, Device **_device) { Device *d; diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 1677785467..6da1398b7d 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -46,6 +46,7 @@ #include "terminal-util.h" #include "udev-util.h" #include "unit-name.h" +#include "user-util.h" #include "utmp-wtmp.h" int manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret) { diff --git a/src/login/logind-inhibit.c b/src/login/logind-inhibit.c index ec34535f83..c4b74eb734 100644 --- a/src/login/logind-inhibit.c +++ b/src/login/logind-inhibit.c @@ -31,6 +31,7 @@ #include "logind-inhibit.h" #include "mkdir.h" #include "string-util.h" +#include "user-util.h" #include "util.h" Inhibitor* inhibitor_new(Manager *m, const char* id) { diff --git a/src/login/logind-session.c b/src/login/logind-session.c index 10f1cfef81..714b1cb299 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -43,6 +43,7 @@ #include "mkdir.h" #include "path-util.h" #include "terminal-util.h" +#include "user-util.h" #include "util.h" #define RELEASE_USEC (20*USEC_PER_SEC) diff --git a/src/login/logind-user-dbus.c b/src/login/logind-user-dbus.c index 20ea2fbdc4..5975b579e9 100644 --- a/src/login/logind-user-dbus.c +++ b/src/login/logind-user-dbus.c @@ -22,11 +22,12 @@ #include #include -#include "strv.h" #include "bus-util.h" -#include "logind.h" -#include "logind-user.h" #include "formats-util.h" +#include "logind-user.h" +#include "logind.h" +#include "strv.h" +#include "user-util.h" static int property_get_display( sd_bus *bus, diff --git a/src/login/logind-utmp.c b/src/login/logind-utmp.c index 1e13ff01de..80e461f2dc 100644 --- a/src/login/logind-utmp.c +++ b/src/login/logind-utmp.c @@ -25,15 +25,17 @@ #include #include "sd-messages.h" -#include "strv.h" -#include "special.h" -#include "unit-name.h" + #include "audit.h" -#include "bus-util.h" -#include "bus-error.h" #include "bus-common-errors.h" -#include "logind.h" +#include "bus-error.h" +#include "bus-util.h" #include "formats-util.h" +#include "logind.h" +#include "special.h" +#include "strv.h" +#include "unit-name.h" +#include "user-util.h" #include "utmp-wtmp.h" _const_ static usec_t when_wall(usec_t n, usec_t elapse) { diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c index b5ce6cdca2..3c91fa8644 100644 --- a/src/machine/machined-dbus.c +++ b/src/machine/machined-dbus.c @@ -41,6 +41,7 @@ #include "process-util.h" #include "strv.h" #include "unit-name.h" +#include "user-util.h" static int property_get_pool_path( sd_bus *bus, diff --git a/src/network/networkd-netdev-tuntap.c b/src/network/networkd-netdev-tuntap.c index 3096c4f72a..d04bb9bd9f 100644 --- a/src/network/networkd-netdev-tuntap.c +++ b/src/network/networkd-netdev-tuntap.c @@ -23,8 +23,9 @@ #include #include -#include "networkd-netdev-tuntap.h" #include "fd-util.h" +#include "networkd-netdev-tuntap.h" +#include "user-util.h" #define TUN_DEV "/dev/net/tun" diff --git a/src/network/networkd.c b/src/network/networkd.c index e6259043fa..1a17847715 100644 --- a/src/network/networkd.c +++ b/src/network/networkd.c @@ -20,9 +20,11 @@ ***/ #include "sd-daemon.h" + #include "capability.h" -#include "signal-util.h" #include "networkd.h" +#include "signal-util.h" +#include "user-util.h" int main(int argc, char *argv[]) { _cleanup_manager_free_ Manager *m = NULL; diff --git a/src/nspawn/nspawn-setuid.c b/src/nspawn/nspawn-setuid.c index 2a1dfd83a9..6b4ca5a3f3 100644 --- a/src/nspawn/nspawn-setuid.c +++ b/src/nspawn/nspawn-setuid.c @@ -29,6 +29,7 @@ #include "process-util.h" #include "signal-util.h" #include "string-util.h" +#include "user-util.h" #include "util.h" static int spawn_getent(const char *database, const char *key, pid_t *rpid) { diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index f95842d252..f01a376af8 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -90,6 +90,7 @@ #include "strv.h" #include "terminal-util.h" #include "udev-util.h" +#include "user-util.h" #include "util.h" typedef enum ContainerStatus { diff --git a/src/nss-mymachines/nss-mymachines.c b/src/nss-mymachines/nss-mymachines.c index d05a32290b..b98bde676b 100644 --- a/src/nss-mymachines/nss-mymachines.c +++ b/src/nss-mymachines/nss-mymachines.c @@ -32,6 +32,7 @@ #include "macro.h" #include "nss-util.h" #include "string-util.h" +#include "user-util.h" #include "util.h" NSS_GETHOSTBYNAME_PROTOTYPES(mymachines); diff --git a/src/resolve/resolved.c b/src/resolve/resolved.c index 32e61af925..df4eb6f63e 100644 --- a/src/resolve/resolved.c +++ b/src/resolve/resolved.c @@ -19,15 +19,16 @@ along with systemd; If not, see . ***/ -#include "sd-event.h" #include "sd-daemon.h" -#include "mkdir.h" +#include "sd-event.h" + #include "capability.h" +#include "mkdir.h" +#include "resolved-conf.h" +#include "resolved-manager.h" #include "selinux-util.h" #include "signal-util.h" - -#include "resolved-manager.h" -#include "resolved-conf.h" +#include "user-util.h" int main(int argc, char *argv[]) { _cleanup_(manager_freep) Manager *m = NULL; diff --git a/src/run/run.c b/src/run/run.c index e81d0892e3..3646305961 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -39,6 +39,7 @@ #include "strv.h" #include "terminal-util.h" #include "unit-name.h" +#include "user-util.h" static bool arg_ask_password = true; static bool arg_scope = false; diff --git a/src/shared/acl-util.c b/src/shared/acl-util.c index 47295ae379..e8931daee2 100644 --- a/src/shared/acl-util.c +++ b/src/shared/acl-util.c @@ -22,10 +22,11 @@ #include #include +#include "acl-util.h" #include "string-util.h" #include "strv.h" +#include "user-util.h" #include "util.h" -#include "acl-util.h" int acl_find_uid(acl_t acl, uid_t uid, acl_entry_t *entry) { acl_entry_t i; diff --git a/src/shared/install-printf.c b/src/shared/install-printf.c index cbe984d2fb..224874f65c 100644 --- a/src/shared/install-printf.c +++ b/src/shared/install-printf.c @@ -21,11 +21,12 @@ #include +#include "formats-util.h" +#include "install-printf.h" #include "specifier.h" #include "unit-name.h" +#include "user-util.h" #include "util.h" -#include "install-printf.h" -#include "formats-util.h" static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) { UnitFileInstallInfo *i = userdata; diff --git a/src/shared/uid-range.c b/src/shared/uid-range.c index 4794ff45bb..079dd8752c 100644 --- a/src/shared/uid-range.c +++ b/src/shared/uid-range.c @@ -19,8 +19,9 @@ along with systemd; If not, see . ***/ -#include "util.h" #include "uid-range.h" +#include "user-util.h" +#include "util.h" static bool uid_range_intersect(UidRange *range, uid_t start, uid_t nr) { assert(range); diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c index 93e6313360..1e6ac2f27d 100644 --- a/src/shared/utmp-wtmp.c +++ b/src/shared/utmp-wtmp.c @@ -33,6 +33,7 @@ #include "path-util.h" #include "string-util.h" #include "terminal-util.h" +#include "user-util.h" #include "utmp-wtmp.h" int utmp_get_runlevel(int *runlevel, int *previous) { diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 49acea1dd2..fe4213c085 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -73,6 +73,7 @@ #include "strv.h" #include "terminal-util.h" #include "unit-name.h" +#include "user-util.h" #include "util.h" #include "utmp-wtmp.h" #include "verbs.h" diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c index 547bd1b344..177432bf9f 100644 --- a/src/sysusers/sysusers.c +++ b/src/sysusers/sysusers.c @@ -41,6 +41,7 @@ #include "utf8.h" #include "util.h" #include "fd-util.h" +#include "user-util.h" typedef enum ItemType { ADD_USER = 'u', diff --git a/src/test/test-ipcrm.c b/src/test/test-ipcrm.c index 4944bf6ad9..5841cb3fb1 100644 --- a/src/test/test-ipcrm.c +++ b/src/test/test-ipcrm.c @@ -19,8 +19,9 @@ along with systemd; If not, see . ***/ -#include "util.h" #include "clean-ipc.h" +#include "user-util.h" +#include "util.h" int main(int argc, char *argv[]) { uid_t uid; diff --git a/src/test/test-util.c b/src/test/test-util.c index 86895733c0..c1f8a866af 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -46,6 +46,7 @@ #include "strv.h" #include "util.h" #include "virt.h" +#include "user-util.h" static void test_streq_ptr(void) { assert_se(streq_ptr(NULL, NULL)); diff --git a/src/timesync/timesyncd.c b/src/timesync/timesyncd.c index 7755a6d89f..7a0ab18ca0 100644 --- a/src/timesync/timesyncd.c +++ b/src/timesync/timesyncd.c @@ -29,6 +29,7 @@ #include "signal-util.h" #include "timesyncd-conf.h" #include "timesyncd-manager.h" +#include "user-util.h" static int load_clock_timestamp(uid_t uid, gid_t gid) { _cleanup_close_ int fd = -1; diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 457d721303..05c4661a2c 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -59,6 +59,7 @@ #include "specifier.h" #include "string-util.h" #include "strv.h" +#include "user-util.h" #include "util.h" /* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index f99d1a86c9..311d515645 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -38,6 +38,7 @@ #include "strv.h" #include "sysctl-util.h" #include "udev.h" +#include "user-util.h" #include "util.h" #define PREALLOC_TOKEN 2048 -- cgit v1.2.3-54-g00ecf From 81a56d6f42cc19c01639f728182324c93fe76c45 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 26 Oct 2015 00:54:08 +0100 Subject: util-lib: move ether_addr_to_string() into ether-addr-util.c --- Makefile.am | 1 + src/basic/ether-addr-util.c | 44 ++++++++++++++++++++++++++++++++ src/basic/ether-addr-util.h | 4 +++ src/basic/socket-util.c | 19 -------------- src/basic/socket-util.h | 4 --- src/libsystemd/sd-netlink/test-netlink.c | 1 + 6 files changed, 50 insertions(+), 23 deletions(-) create mode 100644 src/basic/ether-addr-util.c (limited to 'src/libsystemd') diff --git a/Makefile.am b/Makefile.am index 6540402da9..2e960623a5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -850,6 +850,7 @@ libbasic_la_SOURCES = \ src/basic/in-addr-util.c \ src/basic/in-addr-util.h \ src/basic/ether-addr-util.h \ + src/basic/ether-addr-util.c \ src/basic/replace-var.c \ src/basic/replace-var.h \ src/basic/clock-util.c \ diff --git a/src/basic/ether-addr-util.c b/src/basic/ether-addr-util.c new file mode 100644 index 0000000000..2bf3bfec1d --- /dev/null +++ b/src/basic/ether-addr-util.c @@ -0,0 +1,44 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2014 Tom Gundersen + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include + +#include "ether-addr-util.h" +#include "macro.h" + +char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]) { + assert(addr); + assert(buffer); + + /* Like ether_ntoa() but uses %02x instead of %x to print + * ethernet addresses, which makes them look less funny. Also, + * doesn't use a static buffer. */ + + sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x", + addr->ether_addr_octet[0], + addr->ether_addr_octet[1], + addr->ether_addr_octet[2], + addr->ether_addr_octet[3], + addr->ether_addr_octet[4], + addr->ether_addr_octet[5]); + + return buffer; +} diff --git a/src/basic/ether-addr-util.h b/src/basic/ether-addr-util.h index 7033138788..008f3b893e 100644 --- a/src/basic/ether-addr-util.h +++ b/src/basic/ether-addr-util.h @@ -25,3 +25,7 @@ #define ETHER_ADDR_FORMAT_STR "%02X%02X%02X%02X%02X%02X" #define ETHER_ADDR_FORMAT_VAL(x) (x).ether_addr_octet[0], (x).ether_addr_octet[1], (x).ether_addr_octet[2], (x).ether_addr_octet[3], (x).ether_addr_octet[4], (x).ether_addr_octet[5] + +#define ETHER_ADDR_TO_STRING_MAX (3*6) + +char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]); diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index 9ed5feb849..312512ba1d 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -749,22 +749,3 @@ bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b return false; } - -char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]) { - assert(addr); - assert(buffer); - - /* Like ether_ntoa() but uses %02x instead of %x to print - * ethernet addresses, which makes them look less funny. Also, - * doesn't use a static buffer. */ - - sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x", - addr->ether_addr_octet[0], - addr->ether_addr_octet[1], - addr->ether_addr_octet[2], - addr->ether_addr_octet[3], - addr->ether_addr_octet[4], - addr->ether_addr_octet[5]); - - return buffer; -} diff --git a/src/basic/socket-util.h b/src/basic/socket-util.h index 6b0ce7836f..da1273e745 100644 --- a/src/basic/socket-util.h +++ b/src/basic/socket-util.h @@ -115,7 +115,3 @@ int netlink_family_to_string_alloc(int b, char **s); int netlink_family_from_string(const char *s) _pure_; bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b); - -#define ETHER_ADDR_TO_STRING_MAX (3*6) - -char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]); diff --git a/src/libsystemd/sd-netlink/test-netlink.c b/src/libsystemd/sd-netlink/test-netlink.c index 419a28725f..58b774e0e1 100644 --- a/src/libsystemd/sd-netlink/test-netlink.c +++ b/src/libsystemd/sd-netlink/test-netlink.c @@ -24,6 +24,7 @@ #include "sd-netlink.h" +#include "ether-addr-util.h" #include "event-util.h" #include "macro.h" #include "missing.h" -- cgit v1.2.3-54-g00ecf From 58ce77339c60c8c5f1f7789adf0ef80107dbafe3 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 26 Oct 2015 00:56:12 +0100 Subject: build-sys: add missing makefile symlink --- src/libsystemd/sd-device/Makefile | 1 + 1 file changed, 1 insertion(+) create mode 120000 src/libsystemd/sd-device/Makefile (limited to 'src/libsystemd') diff --git a/src/libsystemd/sd-device/Makefile b/src/libsystemd/sd-device/Makefile new file mode 120000 index 0000000000..d0b0e8e008 --- /dev/null +++ b/src/libsystemd/sd-device/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file -- cgit v1.2.3-54-g00ecf From 2583fbea8e762d2e915582af60560f884d5093f5 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 26 Oct 2015 01:09:02 +0100 Subject: socket-util: move remaining socket-related calls from util.[ch] to socket-util.[ch] --- src/basic/socket-util.c | 184 ++++++++++++++++++++++++++++++++- src/basic/socket-util.h | 12 +++ src/basic/terminal-util.c | 1 + src/basic/util.c | 181 -------------------------------- src/basic/util.h | 12 --- src/core/namespace.c | 1 + src/libsystemd/sd-login/sd-login.c | 1 + src/libsystemd/sd-netlink/sd-netlink.c | 1 + src/nspawn/nspawn-expose-ports.c | 1 + src/nspawn/nspawn.c | 1 + 10 files changed, 201 insertions(+), 194 deletions(-) (limited to 'src/libsystemd') diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index 312512ba1d..684ac765f5 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -23,20 +23,22 @@ #include #include #include +#include #include #include #include #include #include +#include "fd-util.h" #include "fileio.h" #include "formats-util.h" #include "macro.h" #include "missing.h" #include "path-util.h" +#include "socket-util.h" #include "string-util.h" #include "util.h" -#include "socket-util.h" int socket_address_parse(SocketAddress *a, const char *s) { char *e, *n; @@ -749,3 +751,183 @@ bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b return false; } + +int fd_inc_sndbuf(int fd, size_t n) { + int r, value; + socklen_t l = sizeof(value); + + r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l); + if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2) + return 0; + + /* If we have the privileges we will ignore the kernel limit. */ + + value = (int) n; + if (setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &value, sizeof(value)) < 0) + if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)) < 0) + return -errno; + + return 1; +} + +int fd_inc_rcvbuf(int fd, size_t n) { + int r, value; + socklen_t l = sizeof(value); + + r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l); + if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2) + return 0; + + /* If we have the privileges we will ignore the kernel limit. */ + + value = (int) n; + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &value, sizeof(value)) < 0) + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)) < 0) + return -errno; + return 1; +} + +static const char* const ip_tos_table[] = { + [IPTOS_LOWDELAY] = "low-delay", + [IPTOS_THROUGHPUT] = "throughput", + [IPTOS_RELIABILITY] = "reliability", + [IPTOS_LOWCOST] = "low-cost", +}; + +DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff); + +int getpeercred(int fd, struct ucred *ucred) { + socklen_t n = sizeof(struct ucred); + struct ucred u; + int r; + + assert(fd >= 0); + assert(ucred); + + r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &u, &n); + if (r < 0) + return -errno; + + if (n != sizeof(struct ucred)) + return -EIO; + + /* Check if the data is actually useful and not suppressed due + * to namespacing issues */ + if (u.pid <= 0) + return -ENODATA; + if (u.uid == UID_INVALID) + return -ENODATA; + if (u.gid == GID_INVALID) + return -ENODATA; + + *ucred = u; + return 0; +} + +int getpeersec(int fd, char **ret) { + socklen_t n = 64; + char *s; + int r; + + assert(fd >= 0); + assert(ret); + + s = new0(char, n); + if (!s) + return -ENOMEM; + + r = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n); + if (r < 0) { + free(s); + + if (errno != ERANGE) + return -errno; + + s = new0(char, n); + if (!s) + return -ENOMEM; + + r = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n); + if (r < 0) { + free(s); + return -errno; + } + } + + if (isempty(s)) { + free(s); + return -EOPNOTSUPP; + } + + *ret = s; + return 0; +} + +int send_one_fd(int transport_fd, int fd, int flags) { + union { + struct cmsghdr cmsghdr; + uint8_t buf[CMSG_SPACE(sizeof(int))]; + } control = {}; + struct msghdr mh = { + .msg_control = &control, + .msg_controllen = sizeof(control), + }; + struct cmsghdr *cmsg; + + assert(transport_fd >= 0); + assert(fd >= 0); + + cmsg = CMSG_FIRSTHDR(&mh); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + cmsg->cmsg_len = CMSG_LEN(sizeof(int)); + memcpy(CMSG_DATA(cmsg), &fd, sizeof(int)); + + mh.msg_controllen = CMSG_SPACE(sizeof(int)); + if (sendmsg(transport_fd, &mh, MSG_NOSIGNAL | flags) < 0) + return -errno; + + return 0; +} + +int receive_one_fd(int transport_fd, int flags) { + union { + struct cmsghdr cmsghdr; + uint8_t buf[CMSG_SPACE(sizeof(int))]; + } control = {}; + struct msghdr mh = { + .msg_control = &control, + .msg_controllen = sizeof(control), + }; + struct cmsghdr *cmsg, *found = NULL; + + assert(transport_fd >= 0); + + /* + * Receive a single FD via @transport_fd. We don't care for + * the transport-type. We retrieve a single FD at most, so for + * packet-based transports, the caller must ensure to send + * only a single FD per packet. This is best used in + * combination with send_one_fd(). + */ + + if (recvmsg(transport_fd, &mh, MSG_NOSIGNAL | MSG_CMSG_CLOEXEC | flags) < 0) + return -errno; + + CMSG_FOREACH(cmsg, &mh) { + if (cmsg->cmsg_level == SOL_SOCKET && + cmsg->cmsg_type == SCM_RIGHTS && + cmsg->cmsg_len == CMSG_LEN(sizeof(int))) { + assert(!found); + found = cmsg; + break; + } + } + + if (!found) { + cmsg_close_all(&mh); + return -EIO; + } + + return *(int*) CMSG_DATA(found); +} diff --git a/src/basic/socket-util.h b/src/basic/socket-util.h index da1273e745..8b5410b391 100644 --- a/src/basic/socket-util.h +++ b/src/basic/socket-util.h @@ -115,3 +115,15 @@ int netlink_family_to_string_alloc(int b, char **s); int netlink_family_from_string(const char *s) _pure_; bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b); + +int fd_inc_sndbuf(int fd, size_t n); +int fd_inc_rcvbuf(int fd, size_t n); + +int ip_tos_to_string_alloc(int i, char **s); +int ip_tos_from_string(const char *s); + +int getpeercred(int fd, struct ucred *ucred); +int getpeersec(int fd, char **ret); + +int send_one_fd(int transport_fd, int fd, int flags); +int receive_one_fd(int transport_fd, int flags); diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index 3b3ca775d0..7dfab0af62 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -36,6 +36,7 @@ #include "io-util.h" #include "path-util.h" #include "process-util.h" +#include "socket-util.h" #include "string-util.h" #include "terminal-util.h" #include "time-util.h" diff --git a/src/basic/util.c b/src/basic/util.c index 010261b37d..2129d46aef 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -2271,15 +2270,6 @@ static const char* const rlimit_table[_RLIMIT_MAX] = { DEFINE_STRING_TABLE_LOOKUP(rlimit, int); -static const char* const ip_tos_table[] = { - [IPTOS_LOWDELAY] = "low-delay", - [IPTOS_THROUGHPUT] = "throughput", - [IPTOS_RELIABILITY] = "reliability", - [IPTOS_LOWCOST] = "low-cost", -}; - -DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff); - bool kexec_loaded(void) { bool loaded = false; char *s; @@ -2362,41 +2352,6 @@ void* memdup(const void *p, size_t l) { return r; } -int fd_inc_sndbuf(int fd, size_t n) { - int r, value; - socklen_t l = sizeof(value); - - r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l); - if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2) - return 0; - - /* If we have the privileges we will ignore the kernel limit. */ - - value = (int) n; - if (setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &value, sizeof(value)) < 0) - if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)) < 0) - return -errno; - - return 1; -} - -int fd_inc_rcvbuf(int fd, size_t n) { - int r, value; - socklen_t l = sizeof(value); - - r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l); - if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2) - return 0; - - /* If we have the privileges we will ignore the kernel limit. */ - - value = (int) n; - if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &value, sizeof(value)) < 0) - if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)) < 0) - return -errno; - return 1; -} - int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...) { bool stdout_is_tty, stderr_is_tty; pid_t parent_pid, agent_pid; @@ -3264,73 +3219,6 @@ int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int return reset_uid_gid(); } -int getpeercred(int fd, struct ucred *ucred) { - socklen_t n = sizeof(struct ucred); - struct ucred u; - int r; - - assert(fd >= 0); - assert(ucred); - - r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &u, &n); - if (r < 0) - return -errno; - - if (n != sizeof(struct ucred)) - return -EIO; - - /* Check if the data is actually useful and not suppressed due - * to namespacing issues */ - if (u.pid <= 0) - return -ENODATA; - if (u.uid == UID_INVALID) - return -ENODATA; - if (u.gid == GID_INVALID) - return -ENODATA; - - *ucred = u; - return 0; -} - -int getpeersec(int fd, char **ret) { - socklen_t n = 64; - char *s; - int r; - - assert(fd >= 0); - assert(ret); - - s = new0(char, n); - if (!s) - return -ENOMEM; - - r = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n); - if (r < 0) { - free(s); - - if (errno != ERANGE) - return -errno; - - s = new0(char, n); - if (!s) - return -ENOMEM; - - r = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n); - if (r < 0) { - free(s); - return -errno; - } - } - - if (isempty(s)) { - free(s); - return -EOPNOTSUPP; - } - - *ret = s; - return 0; -} - /* This is much like like mkostemp() but is subject to umask(). */ int mkostemp_safe(char *pattern, int flags) { _cleanup_umask_ mode_t u; @@ -4342,75 +4230,6 @@ int fgetxattr_malloc(int fd, const char *name, char **value) { } } -int send_one_fd(int transport_fd, int fd, int flags) { - union { - struct cmsghdr cmsghdr; - uint8_t buf[CMSG_SPACE(sizeof(int))]; - } control = {}; - struct msghdr mh = { - .msg_control = &control, - .msg_controllen = sizeof(control), - }; - struct cmsghdr *cmsg; - - assert(transport_fd >= 0); - assert(fd >= 0); - - cmsg = CMSG_FIRSTHDR(&mh); - cmsg->cmsg_level = SOL_SOCKET; - cmsg->cmsg_type = SCM_RIGHTS; - cmsg->cmsg_len = CMSG_LEN(sizeof(int)); - memcpy(CMSG_DATA(cmsg), &fd, sizeof(int)); - - mh.msg_controllen = CMSG_SPACE(sizeof(int)); - if (sendmsg(transport_fd, &mh, MSG_NOSIGNAL | flags) < 0) - return -errno; - - return 0; -} - -int receive_one_fd(int transport_fd, int flags) { - union { - struct cmsghdr cmsghdr; - uint8_t buf[CMSG_SPACE(sizeof(int))]; - } control = {}; - struct msghdr mh = { - .msg_control = &control, - .msg_controllen = sizeof(control), - }; - struct cmsghdr *cmsg, *found = NULL; - - assert(transport_fd >= 0); - - /* - * Receive a single FD via @transport_fd. We don't care for - * the transport-type. We retrieve a single FD at most, so for - * packet-based transports, the caller must ensure to send - * only a single FD per packet. This is best used in - * combination with send_one_fd(). - */ - - if (recvmsg(transport_fd, &mh, MSG_NOSIGNAL | MSG_CMSG_CLOEXEC | flags) < 0) - return -errno; - - CMSG_FOREACH(cmsg, &mh) { - if (cmsg->cmsg_level == SOL_SOCKET && - cmsg->cmsg_type == SCM_RIGHTS && - cmsg->cmsg_len == CMSG_LEN(sizeof(int))) { - assert(!found); - found = cmsg; - break; - } - } - - if (!found) { - cmsg_close_all(&mh); - return -EIO; - } - - return *(int*) CMSG_DATA(found); -} - void nop_signal_handler(int sig) { /* nothing here */ } diff --git a/src/basic/util.h b/src/basic/util.h index 7d6412523c..55b428fa63 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -330,9 +330,6 @@ int sched_policy_from_string(const char *s); const char *rlimit_to_string(int i) _const_; int rlimit_from_string(const char *s) _pure_; -int ip_tos_to_string_alloc(int i, char **s); -int ip_tos_from_string(const char *s); - extern int saved_argc; extern char **saved_argv; @@ -344,9 +341,6 @@ char *format_bytes(char *buf, size_t l, uint64_t t); void* memdup(const void *p, size_t l) _alloc_(2); -int fd_inc_sndbuf(int fd, size_t n); -int fd_inc_rcvbuf(int fd, size_t n); - int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...); int setrlimit_closest(int resource, const struct rlimit *rlim); @@ -611,9 +605,6 @@ int container_get_leader(const char *machine, pid_t *pid); int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd); int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd); -int getpeercred(int fd, struct ucred *ucred); -int getpeersec(int fd, char **ret); - int mkostemp_safe(char *pattern, int flags); int open_tmpfile(const char *path, int flags); @@ -700,9 +691,6 @@ int mount_move_root(const char *path); int getxattr_malloc(const char *path, const char *name, char **value, bool allow_symlink); int fgetxattr_malloc(int fd, const char *name, char **value); -int send_one_fd(int transport_fd, int fd, int flags); -int receive_one_fd(int transport_fd, int flags); - void nop_signal_handler(int sig); int version(void); diff --git a/src/core/namespace.c b/src/core/namespace.c index 470f384056..4d11d54bfe 100644 --- a/src/core/namespace.c +++ b/src/core/namespace.c @@ -36,6 +36,7 @@ #include "namespace.h" #include "path-util.h" #include "selinux-util.h" +#include "socket-util.h" #include "string-util.h" #include "strv.h" #include "util.h" diff --git a/src/libsystemd/sd-login/sd-login.c b/src/libsystemd/sd-login/sd-login.c index 6012004a0b..f564d43e06 100644 --- a/src/libsystemd/sd-login/sd-login.c +++ b/src/libsystemd/sd-login/sd-login.c @@ -36,6 +36,7 @@ #include "io-util.h" #include "login-util.h" #include "macro.h" +#include "socket-util.h" #include "string-util.h" #include "strv.h" #include "user-util.h" diff --git a/src/libsystemd/sd-netlink/sd-netlink.c b/src/libsystemd/sd-netlink/sd-netlink.c index 57c5db1231..ba1f2a5a42 100644 --- a/src/libsystemd/sd-netlink/sd-netlink.c +++ b/src/libsystemd/sd-netlink/sd-netlink.c @@ -30,6 +30,7 @@ #include "missing.h" #include "netlink-internal.h" #include "netlink-util.h" +#include "socket-util.h" #include "util.h" static int sd_netlink_new(sd_netlink **ret) { diff --git a/src/nspawn/nspawn-expose-ports.c b/src/nspawn/nspawn-expose-ports.c index df9fbe185b..98e4a7682f 100644 --- a/src/nspawn/nspawn-expose-ports.c +++ b/src/nspawn/nspawn-expose-ports.c @@ -27,6 +27,7 @@ #include "local-addresses.h" #include "netlink-util.h" #include "nspawn-expose-ports.h" +#include "socket-util.h" #include "string-util.h" #include "util.h" diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index f01a376af8..6502fe1943 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -86,6 +86,7 @@ #include "seccomp-util.h" #endif #include "signal-util.h" +#include "socket-util.h" #include "string-util.h" #include "strv.h" #include "terminal-util.h" -- cgit v1.2.3-54-g00ecf