X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=8fa01fa8a49609baf640ef850da891ac19dfcb85;hp=872f6f737192c827212f45a1aed2fa3c1c12b167;hb=f36a783ca7b8200d4e865a49342e44929c0f4c06;hpb=4750fade135aed733aa7a5fda7c670e6b4391538 diff --git a/src/shared/util.c b/src/shared/util.c index 872f6f737..8fa01fa8a 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -219,6 +219,8 @@ void close_nointr_nofail(int fd) { void close_many(const int fds[], unsigned n_fd) { unsigned i; + assert(fds || n_fd <= 0); + for (i = 0; i < n_fd; i++) close_nointr_nofail(fds[i]); } @@ -290,7 +292,7 @@ int safe_atou(const char *s, unsigned *ret_u) { l = strtoul(s, &x, 0); if (!x || x == s || *x || errno) - return errno ? -errno : -EINVAL; + return errno > 0 ? -errno : -EINVAL; if ((unsigned long) (unsigned) l != l) return -ERANGE; @@ -310,7 +312,7 @@ int safe_atoi(const char *s, int *ret_i) { l = strtol(s, &x, 0); if (!x || x == s || *x || errno) - return errno ? -errno : -EINVAL; + return errno > 0 ? -errno : -EINVAL; if ((long) (int) l != l) return -ERANGE; @@ -3773,13 +3775,27 @@ static bool hostname_valid_char(char c) { bool hostname_is_valid(const char *s) { const char *p; + bool dot; if (isempty(s)) return false; - for (p = s; *p; p++) - if (!hostname_valid_char(*p)) - return false; + for (p = s, dot = true; *p; p++) { + if (*p == '.') { + if (dot) + return false; + + dot = true; + } else { + if (!hostname_valid_char(*p)) + return false; + + dot = false; + } + } + + if (dot) + return false; if (p-s > HOST_NAME_MAX) return false; @@ -4190,6 +4206,23 @@ char* uid_to_name(uid_t uid) { 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, "%lu", (unsigned long) gid) < 0) + return NULL; + + return r; +} + int get_group_creds(const char **groupname, gid_t *gid) { struct group *g; gid_t id; @@ -4228,14 +4261,10 @@ int get_group_creds(const char **groupname, gid_t *gid) { return 0; } -int in_group(const char *name) { - gid_t gid, *gids; +int in_gid(gid_t gid) { + gid_t *gids; int ngroups_max, r, i; - r = get_group_creds(&name, &gid); - if (r < 0) - return r; - if (getgid() == gid) return 1; @@ -4258,6 +4287,17 @@ int in_group(const char *name) { 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) { glob_t g; int r, k; @@ -5206,53 +5246,6 @@ int get_home_dir(char **_h) { return 0; } -int get_shell(char **_sh) { - char *sh; - const char *e; - uid_t u; - struct passwd *p; - - assert(_sh); - - /* Take the user specified one */ - e = getenv("SHELL"); - if (e) { - sh = strdup(e); - if (!sh) - return -ENOMEM; - - *_sh = sh; - return 0; - } - - /* Hardcode home directory for root to avoid NSS */ - u = getuid(); - if (u == 0) { - sh = strdup("/bin/sh"); - if (!sh) - return -ENOMEM; - - *_sh = sh; - return 0; - } - - /* Check the database... */ - errno = 0; - p = getpwuid(u); - if (!p) - return errno ? -errno : -ESRCH; - - if (!path_is_absolute(p->pw_shell)) - return -EINVAL; - - sh = strdup(p->pw_shell); - if (!sh) - return -ENOMEM; - - *_sh = sh; - return 0; -} - void fclosep(FILE **f) { if (*f) fclose(*f); @@ -5715,7 +5708,7 @@ int create_tmp_dir(char template[], char** dir_name) { dt = strjoin(d, "/tmp", NULL); if (!dt) { r = log_oom(); - goto fail2; + goto fail3; } umask(0000); @@ -5723,7 +5716,7 @@ int create_tmp_dir(char template[], char** dir_name) { if (r) { log_error("Can't create directory %s: %m", dt); r = -errno; - goto fail1; + goto fail2; } log_debug("Created temporary directory %s", dt); @@ -5741,6 +5734,8 @@ int create_tmp_dir(char template[], char** dir_name) { fail1: rmdir(dt); fail2: + free(dt); +fail3: rmdir(template); return r; }