2007-10-08 16:37:39 +05:30
|
|
|
/*
|
|
|
|
librc-misc.c
|
|
|
|
rc misc functions
|
|
|
|
*/
|
|
|
|
|
2008-01-14 10:35:22 +05:30
|
|
|
/*
|
2008-02-22 17:37:34 +05:30
|
|
|
* Copyright 2007-2008 Roy Marples
|
2007-11-14 20:52:04 +05:30
|
|
|
* All rights reserved
|
|
|
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2007-10-09 18:22:09 +05:30
|
|
|
#include <sys/types.h>
|
2008-03-16 22:30:56 +05:30
|
|
|
#include <sys/utsname.h>
|
2007-10-09 18:22:09 +05:30
|
|
|
|
2007-10-08 16:37:39 +05:30
|
|
|
#ifdef __linux__
|
|
|
|
#include <sys/sysinfo.h>
|
2007-10-09 18:22:09 +05:30
|
|
|
#include <regex.h>
|
2007-10-08 16:37:39 +05:30
|
|
|
#endif
|
|
|
|
|
2007-11-23 17:34:11 +05:30
|
|
|
#include <ctype.h>
|
2007-10-08 16:37:39 +05:30
|
|
|
#include <limits.h>
|
2008-02-01 18:50:19 +05:30
|
|
|
#include <signal.h>
|
2007-10-08 16:37:39 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-02-01 18:50:19 +05:30
|
|
|
#include "einfo.h"
|
2007-10-08 16:37:39 +05:30
|
|
|
#include "rc.h"
|
2008-01-06 19:17:39 +05:30
|
|
|
#include "rc-misc.h"
|
2007-10-08 16:37:39 +05:30
|
|
|
|
2008-03-05 17:57:11 +05:30
|
|
|
#define PROFILE_ENV SYSCONFDIR "/profile.env"
|
2007-10-08 16:37:39 +05:30
|
|
|
#define SYS_WHITELIST RC_LIBDIR "/conf.d/env_whitelist"
|
2008-03-05 17:57:11 +05:30
|
|
|
#define USR_WHITELIST SYSCONFDIR "/conf.d/env_whitelist"
|
|
|
|
#define RC_CONF SYSCONFDIR "/rc.conf"
|
|
|
|
#define RC_CONF_OLD SYSCONFDIR "/conf.d/rc"
|
2007-10-08 16:37:39 +05:30
|
|
|
|
|
|
|
#define PATH_PREFIX RC_LIBDIR "/bin:/bin:/sbin:/usr/bin:/usr/sbin"
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static RC_STRINGLIST *rc_conf = NULL;
|
|
|
|
|
|
|
|
extern char** environ;
|
2007-11-23 17:34:11 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static void _free_rc_conf(void)
|
2007-11-23 17:34:11 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_stringlist_free(rc_conf);
|
2007-11-23 17:34:11 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
char *rc_conf_value(const char *setting)
|
2007-11-23 17:34:11 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
RC_STRINGLIST *old;
|
|
|
|
RC_STRING *s;
|
|
|
|
char *p;
|
2007-11-23 17:34:11 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (! rc_conf) {
|
|
|
|
rc_conf = rc_config_load(RC_CONF);
|
|
|
|
atexit(_free_rc_conf);
|
2007-11-23 17:34:11 +05:30
|
|
|
|
|
|
|
/* Support old configs */
|
2008-03-16 22:30:56 +05:30
|
|
|
if (exists(RC_CONF_OLD)) {
|
|
|
|
old = rc_config_load(RC_CONF_OLD);
|
|
|
|
if (old) {
|
2008-03-16 22:57:13 +05:30
|
|
|
TAILQ_CONCAT(rc_conf, old, entries);
|
2008-03-16 22:30:56 +05:30
|
|
|
free(old);
|
|
|
|
}
|
2007-11-23 17:34:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert old uppercase to lowercase */
|
2008-03-16 22:30:56 +05:30
|
|
|
TAILQ_FOREACH(s, rc_conf, entries) {
|
|
|
|
p = s->value;
|
2007-11-23 17:34:11 +05:30
|
|
|
while (p && *p && *p != '=') {
|
2008-03-16 22:30:56 +05:30
|
|
|
if (isupper((int) *p))
|
|
|
|
*p = tolower((int) *p);
|
2007-11-23 17:34:11 +05:30
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
return rc_config_value(rc_conf, setting);
|
2007-12-06 16:18:00 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
bool rc_conf_yesno(const char *setting)
|
2007-12-06 16:18:00 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
return rc_yesno(rc_conf_value (setting));
|
2007-11-23 17:34:11 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
static const char *const env_whitelist[] = {
|
|
|
|
"PATH", "SHELL", "USER", "HOME", "TERM",
|
|
|
|
"LANG", "LC_CTYPE", "LC_NUMERIC", "LC_TIME", "LC_COLLATE",
|
|
|
|
"LC_MONETARY", "LC_MESSAGES", "LC_PAPER", "LC_NAME", "LC_ADDRESS",
|
|
|
|
"LC_TELEPHONE", "LC_MEASUREMENT", "LC_IDENTIFICATION", "LC_ALL",
|
|
|
|
"INIT_HALT", "INIT_VERSION", "RUNLEVEL", "PREVLEVEL", "CONSOLE",
|
|
|
|
"IN_HOTPLUG", "IN_BACKGROUND", "RC_INTERFACE_KEEP_CONFIG",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
void env_filter(void)
|
2007-10-08 16:37:39 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
RC_STRINGLIST *env_allow;
|
|
|
|
RC_STRINGLIST *profile = NULL;
|
|
|
|
RC_STRINGLIST *env_list;
|
|
|
|
RC_STRING *env;
|
|
|
|
RC_STRING *s;
|
|
|
|
char *env_name;
|
2007-10-08 16:37:39 +05:30
|
|
|
char *e;
|
2008-03-16 22:30:56 +05:30
|
|
|
char *token;
|
|
|
|
size_t i = 0;
|
2008-01-06 02:13:08 +05:30
|
|
|
|
|
|
|
/* Add the user defined list of vars */
|
2008-03-16 22:30:56 +05:30
|
|
|
env_allow = rc_stringlist_new();
|
|
|
|
e = env_name = xstrdup(rc_conf_value ("rc_env_allow"));
|
|
|
|
while ((token = strsep(&e, " "))) {
|
2008-01-06 02:13:08 +05:30
|
|
|
if (token[0] == '*') {
|
2008-03-16 22:30:56 +05:30
|
|
|
free(env_name);
|
|
|
|
rc_stringlist_free(env_allow);
|
|
|
|
return;
|
2008-01-06 02:13:08 +05:30
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_stringlist_add(env_allow, token);
|
2008-01-06 02:13:08 +05:30
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
free(env_name);
|
2007-10-08 16:37:39 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (exists(PROFILE_ENV))
|
|
|
|
profile = rc_config_load(PROFILE_ENV);
|
2007-10-08 16:37:39 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
/* Copy the env and work from this so we can remove safely */
|
|
|
|
env_list = rc_stringlist_new();
|
|
|
|
while (environ[i])
|
|
|
|
rc_stringlist_add(env_list, environ[i++]);
|
2007-10-08 16:37:39 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
TAILQ_FOREACH(env, env_list, entries) {
|
|
|
|
/* Check the whitelist */
|
|
|
|
i = 0;
|
|
|
|
while (env_whitelist[i]) {
|
|
|
|
if (strcmp(env_whitelist[i++], env->value))
|
|
|
|
break;
|
2007-10-08 16:37:39 +05:30
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
if (env_whitelist[i])
|
|
|
|
continue;
|
2007-10-08 16:37:39 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
/* Check our user defined list */
|
|
|
|
TAILQ_FOREACH(s, env_allow, entries)
|
|
|
|
if (strcmp(s->value, env->value) == 0)
|
|
|
|
break;
|
|
|
|
if (s)
|
2007-10-08 16:37:39 +05:30
|
|
|
continue;
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
/* Now check our profile */
|
2007-10-08 16:37:39 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
/* OK, not allowed! */
|
|
|
|
e = strchr(env->value, '=');
|
|
|
|
*e = '\0';
|
|
|
|
unsetenv(env->value);
|
2007-10-08 16:37:39 +05:30
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
rc_stringlist_free(env_list);
|
|
|
|
rc_stringlist_free(env_allow);
|
|
|
|
rc_stringlist_free(profile);
|
2007-10-08 16:37:39 +05:30
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
void env_config(void)
|
2007-10-08 16:37:39 +05:30
|
|
|
{
|
2008-03-16 22:30:56 +05:30
|
|
|
size_t pplen = strlen(PATH_PREFIX);
|
|
|
|
char *path;
|
|
|
|
char *p;
|
|
|
|
char *e;
|
2008-01-22 16:37:39 +05:30
|
|
|
size_t l;
|
2007-10-08 16:37:39 +05:30
|
|
|
struct utsname uts;
|
|
|
|
FILE *fp;
|
2008-03-16 22:30:56 +05:30
|
|
|
char *token;
|
|
|
|
char *np;
|
|
|
|
char *npp;
|
|
|
|
char *tok;
|
|
|
|
const char *sys = rc_sys();
|
2007-10-08 16:37:39 +05:30
|
|
|
char buffer[PATH_MAX];
|
2008-03-16 22:30:56 +05:30
|
|
|
|
|
|
|
/* Ensure our PATH is prefixed with the system locations first
|
|
|
|
for a little extra security */
|
|
|
|
path = getenv("PATH");
|
|
|
|
if (! path)
|
|
|
|
setenv("PATH", PATH_PREFIX, 1);
|
|
|
|
else if (strncmp (PATH_PREFIX, path, pplen) != 0) {
|
|
|
|
l = strlen(path) + pplen + 3;
|
|
|
|
e = p = xmalloc(sizeof(char) * l);
|
|
|
|
p += snprintf(p, l, "%s", PATH_PREFIX);
|
|
|
|
|
|
|
|
/* Now go through the env var and only add bits not in our PREFIX */
|
|
|
|
while ((token = strsep(&path, ":"))) {
|
|
|
|
np = npp = xstrdup(PATH_PREFIX);
|
|
|
|
while ((tok = strsep(&npp, ":")))
|
|
|
|
if (strcmp(tok, token) == 0)
|
|
|
|
break;
|
|
|
|
if (! tok)
|
|
|
|
p += snprintf(p, l - (p - e), ":%s", token);
|
|
|
|
free (np);
|
|
|
|
}
|
|
|
|
*p++ = '\0';
|
|
|
|
unsetenv("PATH");
|
|
|
|
setenv("PATH", e, 1);
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
setenv("RC_LIBDIR", RC_LIBDIR, 1);
|
|
|
|
setenv("RC_SVCDIR", RC_SVCDIR, 1);
|
|
|
|
setenv("RC_BOOTLEVEL", RC_LEVEL_BOOT, 1);
|
|
|
|
e = rc_runlevel_get();
|
|
|
|
setenv("RC_RUNLEVEL", e, 1);
|
|
|
|
free(e);
|
|
|
|
|
2008-03-19 22:41:50 +05:30
|
|
|
if ((fp = fopen(RC_KRUNLEVEL, "r"))) {
|
2008-03-16 22:30:56 +05:30
|
|
|
memset(buffer, 0, sizeof (buffer));
|
|
|
|
if (fgets(buffer, sizeof (buffer), fp)) {
|
2008-01-22 16:37:39 +05:30
|
|
|
l = strlen (buffer) - 1;
|
|
|
|
if (buffer[l] == '\n')
|
|
|
|
buffer[l] = 0;
|
2008-03-16 22:30:56 +05:30
|
|
|
setenv("RC_DEFAULTLEVEL", buffer, 1);
|
2007-10-08 16:37:39 +05:30
|
|
|
}
|
2008-03-16 22:30:56 +05:30
|
|
|
fclose(fp);
|
2007-10-08 16:37:39 +05:30
|
|
|
} else
|
2008-03-16 22:30:56 +05:30
|
|
|
setenv("RC_DEFAULTLEVEL", RC_LEVEL_DEFAULT, 1);
|
|
|
|
|
|
|
|
if (sys)
|
|
|
|
setenv("RC_SYS", sys, 1);
|
2007-10-08 16:37:39 +05:30
|
|
|
|
|
|
|
/* Some scripts may need to take a different code path if Linux/FreeBSD, etc
|
|
|
|
To save on calling uname, we store it in an environment variable */
|
2008-03-16 22:30:56 +05:30
|
|
|
if (uname(&uts) == 0)
|
|
|
|
setenv("RC_UNAME", uts.sysname, 1);
|
2007-10-08 16:37:39 +05:30
|
|
|
|
2007-12-06 16:18:00 +05:30
|
|
|
/* Be quiet or verbose as necessary */
|
2008-03-16 22:30:56 +05:30
|
|
|
if (rc_conf_yesno("rc_quiet"))
|
|
|
|
setenv("EINFO_QUIET", "YES", 1);
|
|
|
|
if (rc_conf_yesno("rc_verbose"))
|
|
|
|
setenv("EINFO_VERBOSE", "YES", 1);
|
2007-12-06 16:18:00 +05:30
|
|
|
|
|
|
|
errno = 0;
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((! rc_conf_yesno("rc_color") && errno == 0) ||
|
|
|
|
rc_conf_yesno("rc_nocolor"))
|
|
|
|
setenv("EINFO_COLOR", "NO", 1);
|
2007-10-08 16:37:39 +05:30
|
|
|
}
|
2008-01-13 23:17:23 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
bool service_plugable(const char *service)
|
2008-01-13 23:17:23 +05:30
|
|
|
{
|
|
|
|
char *list;
|
|
|
|
char *p;
|
|
|
|
char *star;
|
|
|
|
char *token;
|
|
|
|
bool allow = true;
|
2008-03-16 22:30:56 +05:30
|
|
|
char *match = rc_conf_value("rc_plug_services");
|
|
|
|
bool truefalse;
|
2008-01-18 19:00:12 +05:30
|
|
|
|
2008-01-13 23:17:23 +05:30
|
|
|
if (! match)
|
2008-03-16 22:30:56 +05:30
|
|
|
return true;
|
2008-01-13 23:17:23 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
list = xstrdup(match);
|
2008-01-13 23:17:23 +05:30
|
|
|
p = list;
|
2008-03-16 22:30:56 +05:30
|
|
|
while ((token = strsep(&p, " "))) {
|
2008-01-13 23:17:23 +05:30
|
|
|
if (token[0] == '!') {
|
|
|
|
truefalse = false;
|
|
|
|
token++;
|
2008-03-16 22:30:56 +05:30
|
|
|
} else
|
|
|
|
truefalse = true;
|
2008-01-13 23:17:23 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
star = strchr(token, '*');
|
2008-01-13 23:17:23 +05:30
|
|
|
if (star) {
|
2008-03-16 22:30:56 +05:30
|
|
|
if (strncmp(service, token, (size_t)(star - token)) == 0)
|
2008-01-22 16:37:39 +05:30
|
|
|
{
|
2008-01-13 23:17:23 +05:30
|
|
|
allow = truefalse;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2008-03-16 22:30:56 +05:30
|
|
|
if (strcmp(service, token) == 0) {
|
2008-01-13 23:17:23 +05:30
|
|
|
allow = truefalse;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
free(list);
|
|
|
|
return allow;
|
2008-01-13 23:17:23 +05:30
|
|
|
}
|
2008-02-01 18:50:19 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
int signal_setup(int sig, void (*handler)(int))
|
2008-02-01 18:50:19 +05:30
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
memset(&sa, 0, sizeof (sa));
|
|
|
|
sigemptyset(&sa.sa_mask);
|
2008-02-02 05:47:35 +05:30
|
|
|
sa.sa_handler = handler;
|
2008-03-16 22:30:56 +05:30
|
|
|
return sigaction(sig, &sa, NULL);
|
2008-02-01 18:50:19 +05:30
|
|
|
}
|