mirror of
https://github.com/troglobit/finit.git
synced 2026-11-07 16:01:08 +00:00
Default to user/group root for services and check for errors
This is a refactor of getuser() and getgroup() so that they always return a valid user, and group, for all normal use-cases. When an error occurs we now handle it properly in service_fork() so as to not attempt to start services with an invalid user/group setting as root. Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
2 files changed
+50
-4
No files matched your search
@@ -556,6 +556,18 @@ static pid_t service_fork(svc_t *svc)
|
|||||||
#else
|
#else
|
||||||
int uid = getuser(svc->username, &home);
|
int uid = getuser(svc->username, &home);
|
||||||
int gid = getgroup(svc->group);
|
int gid = getgroup(svc->group);
|
||||||
|
|
||||||
|
/* Check for invalid user/group - fail rather than silent fallback */
|
||||||
|
if (uid < 0) {
|
||||||
|
err(1, "%s: user '%s' not found, cannot start service",
|
||||||
|
svc_ident(svc, NULL, 0), svc->username);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (gid < 0) {
|
||||||
|
err(1, "%s: group '%s' not found, cannot start service",
|
||||||
|
svc_ident(svc, NULL, 0), svc->group);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
sched_yield();
|
sched_yield();
|
||||||
|
|||||||
+38
-4
@@ -180,16 +180,34 @@ char *str(char *fmt, ...)
|
|||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Return UID for the given user, with fallback to 'root' if user is unset.
|
||||||
|
* Fails with -1 only if user (incl. fallback) does not exist.
|
||||||
|
*/
|
||||||
int getuser(const char *username, char **home)
|
int getuser(const char *username, char **home)
|
||||||
{
|
{
|
||||||
#ifdef ENABLE_STATIC
|
#ifdef ENABLE_STATIC
|
||||||
|
if (!username || !username[0])
|
||||||
|
username = "root";
|
||||||
|
|
||||||
if (home)
|
if (home)
|
||||||
*home = "/"; /* XXX: Fixme */
|
*home = "/"; /* XXX: Fixme */
|
||||||
return fgetint("/etc/passwd", "x:\n", username);
|
return fgetint("/etc/passwd", "x:\n", username);
|
||||||
#else
|
#else
|
||||||
struct passwd *usr;
|
const struct passwd *usr;
|
||||||
|
|
||||||
if (!username || (usr = getpwnam(username)) == NULL)
|
if (!username || !username[0]) {
|
||||||
|
usr = getpwuid(0);
|
||||||
|
if (!usr)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (home)
|
||||||
|
*home = usr->pw_dir;
|
||||||
|
return usr->pw_uid;
|
||||||
|
}
|
||||||
|
|
||||||
|
usr = getpwnam(username);
|
||||||
|
if (!usr)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (home)
|
if (home)
|
||||||
@@ -198,14 +216,30 @@ int getuser(const char *username, char **home)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Return GID for the given group, with fallback to 'root' if group is unset.
|
||||||
|
* Fails with -1 only if user (incl. fallback) does not exist.
|
||||||
|
*/
|
||||||
int getgroup(const char *group)
|
int getgroup(const char *group)
|
||||||
{
|
{
|
||||||
#ifdef ENABLE_STATIC
|
#ifdef ENABLE_STATIC
|
||||||
|
if (!group || !group[0])
|
||||||
|
group = "root";
|
||||||
|
|
||||||
return fgetint("/etc/group", "x:\n", group);
|
return fgetint("/etc/group", "x:\n", group);
|
||||||
#else
|
#else
|
||||||
struct group *grp;
|
const struct group *grp;
|
||||||
|
|
||||||
if ((grp = getgrnam(group)) == NULL)
|
if (!group || !group[0]) {
|
||||||
|
grp = getgrgid(0);
|
||||||
|
if (!grp)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return grp->gr_gid;
|
||||||
|
}
|
||||||
|
|
||||||
|
grp = getgrnam(group);
|
||||||
|
if (!grp)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
return grp->gr_gid;
|
return grp->gr_gid;
|
||||||
|
|||||||
Reference in new issue
Block a user