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>
|
|
|
|
|
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
|
|
|
|
|
|
|
|
#include <sys/utsname.h>
|
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"
|
|
|
|
#include "strlist.h"
|
2007-10-08 16:37:39 +05:30
|
|
|
|
|
|
|
#define PROFILE_ENV "/etc/profile.env"
|
|
|
|
#define SYS_WHITELIST RC_LIBDIR "/conf.d/env_whitelist"
|
|
|
|
#define USR_WHITELIST "/etc/conf.d/env_whitelist"
|
2007-11-23 17:34:11 +05:30
|
|
|
#define RC_CONF "/etc/rc.conf"
|
|
|
|
#define RC_CONF_OLD "/etc/conf.d/rc"
|
2007-10-08 16:37:39 +05:30
|
|
|
|
|
|
|
#define PATH_PREFIX RC_LIBDIR "/bin:/bin:/sbin:/usr/bin:/usr/sbin"
|
|
|
|
|
2007-11-23 17:34:11 +05:30
|
|
|
static char **rc_conf = NULL;
|
|
|
|
|
|
|
|
static void _free_rc_conf (void)
|
|
|
|
{
|
|
|
|
rc_strlist_free (rc_conf);
|
|
|
|
}
|
|
|
|
|
2007-12-06 16:18:00 +05:30
|
|
|
char *rc_conf_value (const char *setting)
|
2007-11-23 17:34:11 +05:30
|
|
|
{
|
|
|
|
if (! rc_conf) {
|
|
|
|
char *line;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
rc_conf = rc_config_load (RC_CONF);
|
|
|
|
atexit (_free_rc_conf);
|
|
|
|
|
|
|
|
/* Support old configs */
|
|
|
|
if (exists (RC_CONF_OLD)) {
|
|
|
|
char **old = rc_config_load (RC_CONF_OLD);
|
|
|
|
rc_strlist_join (&rc_conf, old);
|
|
|
|
rc_strlist_free (old);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert old uppercase to lowercase */
|
|
|
|
STRLIST_FOREACH (rc_conf, line, i) {
|
|
|
|
char *p = line;
|
|
|
|
while (p && *p && *p != '=') {
|
2008-01-10 04:52:04 +05:30
|
|
|
if (isupper ((int) *p))
|
|
|
|
*p = tolower ((int) *p);
|
2007-11-23 17:34:11 +05:30
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-18 19:31:48 +05:30
|
|
|
return (rc_config_value ((const char *const *)rc_conf, setting));
|
2007-12-06 16:18:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool rc_conf_yesno (const char *setting)
|
|
|
|
{
|
|
|
|
return (rc_yesno (rc_conf_value (setting)));
|
2007-11-23 17:34:11 +05:30
|
|
|
}
|
|
|
|
|
2007-10-08 16:37:39 +05:30
|
|
|
char **env_filter (void)
|
|
|
|
{
|
|
|
|
char **env = NULL;
|
|
|
|
char **whitelist = NULL;
|
|
|
|
char *env_name = NULL;
|
|
|
|
char **profile = NULL;
|
|
|
|
int count = 0;
|
|
|
|
bool got_path = false;
|
|
|
|
char *env_var;
|
2008-01-22 16:37:39 +05:30
|
|
|
size_t env_len;
|
2007-10-08 16:37:39 +05:30
|
|
|
char *token;
|
|
|
|
char *sep;
|
|
|
|
char *e;
|
2007-11-23 17:34:11 +05:30
|
|
|
char *p;
|
2008-01-22 16:37:39 +05:30
|
|
|
size_t pplen = strlen (PATH_PREFIX);
|
2007-10-08 16:37:39 +05:30
|
|
|
|
2008-01-06 02:13:08 +05:30
|
|
|
/* Init a system whitelist, start with shell vars we need */
|
|
|
|
rc_strlist_add (&whitelist, "PATH");
|
|
|
|
rc_strlist_add (&whitelist, "SHELL");
|
|
|
|
rc_strlist_add (&whitelist, "USER");
|
|
|
|
rc_strlist_add (&whitelist, "HOME");
|
|
|
|
rc_strlist_add (&whitelist, "TERM");
|
|
|
|
|
|
|
|
/* Add Language vars */
|
|
|
|
rc_strlist_add (&whitelist, "LANG");
|
|
|
|
rc_strlist_add (&whitelist, "LC_CTYPE");
|
|
|
|
rc_strlist_add (&whitelist, "LC_NUMERIC");
|
|
|
|
rc_strlist_add (&whitelist, "LC_TIME");
|
|
|
|
rc_strlist_add (&whitelist, "LC_COLLATE");
|
|
|
|
rc_strlist_add (&whitelist, "LC_MONETARY");
|
|
|
|
rc_strlist_add (&whitelist, "LC_MESSAGES");
|
|
|
|
rc_strlist_add (&whitelist, "LC_PAPER");
|
|
|
|
rc_strlist_add (&whitelist, "LC_NAME");
|
|
|
|
rc_strlist_add (&whitelist, "LC_ADDRESS");
|
|
|
|
rc_strlist_add (&whitelist, "LC_TELEPHONE");
|
|
|
|
rc_strlist_add (&whitelist, "LC_MEASUREMENT");
|
|
|
|
rc_strlist_add (&whitelist, "LC_IDENTIFICATION");
|
|
|
|
rc_strlist_add (&whitelist, "LC_ALL");
|
|
|
|
|
|
|
|
/* Allow rc to override library path */
|
|
|
|
rc_strlist_add (&whitelist, "LD_LIBRARY_PATH");
|
|
|
|
|
|
|
|
/* We need to know sysvinit stuff - we emulate this for BSD too */
|
|
|
|
rc_strlist_add (&whitelist, "INIT_HALT");
|
|
|
|
rc_strlist_add (&whitelist, "INIT_VERSION");
|
|
|
|
rc_strlist_add (&whitelist, "RUNLEVEL");
|
|
|
|
rc_strlist_add (&whitelist, "PREVLEVEL");
|
|
|
|
rc_strlist_add (&whitelist, "CONSOLE");
|
|
|
|
|
|
|
|
/* Hotplug and daemon vars */
|
|
|
|
rc_strlist_add (&whitelist, "IN_HOTPLUG");
|
|
|
|
rc_strlist_add (&whitelist, "IN_BACKGROUND");
|
|
|
|
rc_strlist_add (&whitelist, "RC_INTERFACE_KEEP_CONFIG");
|
|
|
|
|
|
|
|
/* Add the user defined list of vars */
|
|
|
|
e = env_name = xstrdup (rc_conf_value ("rc_env_allow"));
|
|
|
|
while ((token = strsep (&e, " "))) {
|
|
|
|
if (token[0] == '*') {
|
|
|
|
free (env_name);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
rc_strlist_add (&whitelist, token);
|
|
|
|
}
|
|
|
|
free (env_name);
|
2007-10-08 16:37:39 +05:30
|
|
|
|
2007-10-08 16:41:21 +05:30
|
|
|
if (exists (PROFILE_ENV))
|
2007-10-08 16:37:39 +05:30
|
|
|
profile = rc_config_load (PROFILE_ENV);
|
|
|
|
|
|
|
|
STRLIST_FOREACH (whitelist, env_name, count) {
|
|
|
|
char *space = strchr (env_name, ' ');
|
|
|
|
if (space)
|
|
|
|
*space = 0;
|
|
|
|
|
|
|
|
env_var = getenv (env_name);
|
|
|
|
|
|
|
|
if (! env_var && profile) {
|
|
|
|
env_len = strlen (env_name) + strlen ("export ") + 1;
|
2007-10-12 04:47:53 +05:30
|
|
|
p = xmalloc (sizeof (char) * env_len);
|
2007-10-08 16:37:39 +05:30
|
|
|
snprintf (p, env_len, "export %s", env_name);
|
2008-01-18 19:31:48 +05:30
|
|
|
env_var = rc_config_value ((const char *const *) profile, p);
|
2007-10-08 16:37:39 +05:30
|
|
|
free (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! env_var)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Ensure our PATH is prefixed with the system locations first
|
|
|
|
for a little extra security */
|
|
|
|
if (strcmp (env_name, "PATH") == 0 &&
|
2008-01-11 21:21:40 +05:30
|
|
|
strncmp (PATH_PREFIX, env_var, pplen) != 0)
|
2007-10-08 16:37:39 +05:30
|
|
|
{
|
|
|
|
got_path = true;
|
2008-02-09 23:46:53 +05:30
|
|
|
env_len = strlen (env_name) + strlen (env_var) + pplen + 3;
|
2007-10-12 04:47:53 +05:30
|
|
|
e = p = xmalloc (sizeof (char) * env_len);
|
2007-10-08 16:37:39 +05:30
|
|
|
p += snprintf (e, env_len, "%s=%s", env_name, PATH_PREFIX);
|
|
|
|
|
|
|
|
/* Now go through the env var and only add bits not in our PREFIX */
|
|
|
|
sep = env_var;
|
|
|
|
while ((token = strsep (&sep, ":"))) {
|
2007-10-08 16:46:22 +05:30
|
|
|
char *np = xstrdup (PATH_PREFIX);
|
2007-10-08 16:37:39 +05:30
|
|
|
char *npp = np;
|
|
|
|
char *tok = NULL;
|
|
|
|
while ((tok = strsep (&npp, ":")))
|
|
|
|
if (strcmp (tok, token) == 0)
|
|
|
|
break;
|
|
|
|
if (! tok)
|
|
|
|
p += snprintf (p, env_len - (p - e), ":%s", token);
|
|
|
|
free (np);
|
|
|
|
}
|
|
|
|
*p++ = 0;
|
|
|
|
} else {
|
|
|
|
env_len = strlen (env_name) + strlen (env_var) + 2;
|
2007-10-12 04:47:53 +05:30
|
|
|
e = xmalloc (sizeof (char) * env_len);
|
2007-10-08 16:37:39 +05:30
|
|
|
snprintf (e, env_len, "%s=%s", env_name, env_var);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc_strlist_add (&env, e);
|
|
|
|
free (e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We filtered the env but didn't get a PATH? Very odd.
|
|
|
|
However, we do need a path, so use a default. */
|
|
|
|
if (! got_path) {
|
2008-02-09 23:46:53 +05:30
|
|
|
env_len = strlen ("PATH=") + strlen (PATH_PREFIX) + 1;
|
2007-12-06 16:18:00 +05:30
|
|
|
e = xmalloc (sizeof (char) * env_len);
|
|
|
|
snprintf (e, env_len, "PATH=%s", PATH_PREFIX);
|
|
|
|
rc_strlist_add (&env, e);
|
|
|
|
free (e);
|
2007-10-08 16:37:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
rc_strlist_free (whitelist);
|
|
|
|
rc_strlist_free (profile);
|
|
|
|
|
|
|
|
return (env);
|
|
|
|
}
|
|
|
|
|
|
|
|
char **env_config (void)
|
|
|
|
{
|
|
|
|
char **env = NULL;
|
|
|
|
char *line;
|
2008-01-22 16:37:39 +05:30
|
|
|
size_t l;
|
2008-02-19 19:45:53 +05:30
|
|
|
const char *sys = rc_sys ();
|
2007-10-08 16:37:39 +05:30
|
|
|
struct utsname uts;
|
|
|
|
FILE *fp;
|
|
|
|
char buffer[PATH_MAX];
|
|
|
|
char *runlevel = rc_runlevel_get ();
|
|
|
|
|
|
|
|
/* One char less to drop the trailing / */
|
2008-01-22 16:37:39 +05:30
|
|
|
l = strlen ("RC_LIBDIR=") + strlen (RC_LIBDIR) + 1;
|
|
|
|
line = xmalloc (sizeof (char) * l);
|
|
|
|
snprintf (line, l, "RC_LIBDIR=" RC_LIBDIR);
|
2007-10-08 16:37:39 +05:30
|
|
|
rc_strlist_add (&env, line);
|
|
|
|
free (line);
|
|
|
|
|
|
|
|
/* One char less to drop the trailing / */
|
2008-01-22 16:37:39 +05:30
|
|
|
l = strlen ("RC_SVCDIR=") + strlen (RC_SVCDIR) + 1;
|
|
|
|
line = xmalloc (sizeof (char) * l);
|
|
|
|
snprintf (line, l, "RC_SVCDIR=" RC_SVCDIR);
|
2007-10-08 16:37:39 +05:30
|
|
|
rc_strlist_add (&env, line);
|
|
|
|
free (line);
|
|
|
|
|
|
|
|
rc_strlist_add (&env, "RC_BOOTLEVEL=" RC_LEVEL_BOOT);
|
|
|
|
|
2008-01-22 16:37:39 +05:30
|
|
|
l = strlen ("RC_SOFTLEVEL=") + strlen (runlevel) + 1;
|
|
|
|
line = xmalloc (sizeof (char) * l);
|
|
|
|
snprintf (line, l, "RC_SOFTLEVEL=%s", runlevel);
|
2007-10-08 16:37:39 +05:30
|
|
|
rc_strlist_add (&env, line);
|
|
|
|
free (line);
|
|
|
|
|
|
|
|
if ((fp = fopen (RC_KSOFTLEVEL, "r"))) {
|
|
|
|
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;
|
|
|
|
l += strlen ("RC_DEFAULTLEVEL=") + 2;
|
|
|
|
line = xmalloc (sizeof (char) * l);
|
|
|
|
snprintf (line, l, "RC_DEFAULTLEVEL=%s", buffer);
|
2007-10-08 16:37:39 +05:30
|
|
|
rc_strlist_add (&env, line);
|
|
|
|
free (line);
|
|
|
|
}
|
|
|
|
fclose (fp);
|
|
|
|
} else
|
|
|
|
rc_strlist_add (&env, "RC_DEFAULTLEVEL=" RC_LEVEL_DEFAULT);
|
|
|
|
|
2008-02-19 19:45:53 +05:30
|
|
|
if (sys) {
|
2008-01-22 16:37:39 +05:30
|
|
|
l = strlen ("RC_SYS=") + strlen (sys) + 2;
|
|
|
|
line = xmalloc (sizeof (char) * l);
|
|
|
|
snprintf (line, l, "RC_SYS=%s", sys);
|
2007-10-08 16:37:39 +05:30
|
|
|
rc_strlist_add (&env, line);
|
|
|
|
free (line);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
if (uname (&uts) == 0) {
|
2008-01-22 16:37:39 +05:30
|
|
|
l = strlen ("RC_UNAME=") + strlen (uts.sysname) + 2;
|
|
|
|
line = xmalloc (sizeof (char) * l);
|
|
|
|
snprintf (line, l, "RC_UNAME=%s", uts.sysname);
|
2007-10-08 16:37:39 +05:30
|
|
|
rc_strlist_add (&env, line);
|
|
|
|
free (line);
|
|
|
|
}
|
|
|
|
|
2007-12-06 16:18:00 +05:30
|
|
|
/* Be quiet or verbose as necessary */
|
2008-02-20 19:34:30 +05:30
|
|
|
if (rc_conf_yesno ("rc_quiet"))
|
|
|
|
rc_strlist_add (&env, "EINFO_QUIET=YES");
|
|
|
|
if (rc_conf_yesno ("rc_verbose"))
|
|
|
|
rc_strlist_add (&env, "EINFO_VERBOSE=YES");
|
2007-12-06 16:18:00 +05:30
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
if ((! rc_conf_yesno ("rc_color") && errno == 0) ||
|
2008-01-11 21:21:40 +05:30
|
|
|
rc_conf_yesno ("rc_nocolor"))
|
2008-02-20 19:34:30 +05:30
|
|
|
rc_strlist_add (&env, "EINFO_COLOR=NO");
|
2007-12-06 16:18:00 +05:30
|
|
|
|
2007-10-08 16:37:39 +05:30
|
|
|
free (runlevel);
|
|
|
|
return (env);
|
|
|
|
}
|
2008-01-13 23:17:23 +05:30
|
|
|
|
|
|
|
bool service_plugable (const char *service)
|
|
|
|
{
|
|
|
|
char *list;
|
|
|
|
char *p;
|
|
|
|
char *star;
|
|
|
|
char *token;
|
|
|
|
bool allow = true;
|
|
|
|
char *match = rc_conf_value ("rc_plug_services");
|
2008-01-18 19:00:12 +05:30
|
|
|
|
2008-01-13 23:17:23 +05:30
|
|
|
if (! match)
|
2008-01-18 19:00:12 +05:30
|
|
|
return (true);
|
2008-01-13 23:17:23 +05:30
|
|
|
|
|
|
|
list = xstrdup (match);
|
|
|
|
p = list;
|
|
|
|
while ((token = strsep (&p, " "))) {
|
|
|
|
bool truefalse = true;
|
2008-01-18 19:00:12 +05:30
|
|
|
|
2008-01-13 23:17:23 +05:30
|
|
|
if (token[0] == '!') {
|
|
|
|
truefalse = false;
|
|
|
|
token++;
|
|
|
|
}
|
|
|
|
|
|
|
|
star = strchr (token, '*');
|
|
|
|
if (star) {
|
2008-01-22 16:37:39 +05:30
|
|
|
if (strncmp (service, token, (size_t) (star - token))
|
|
|
|
== 0)
|
|
|
|
{
|
2008-01-13 23:17:23 +05:30
|
|
|
allow = truefalse;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (strcmp (service, token) == 0) {
|
|
|
|
allow = truefalse;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free (list);
|
|
|
|
return (allow);
|
|
|
|
}
|
2008-02-01 18:50:19 +05:30
|
|
|
|
2008-02-02 05:47:35 +05:30
|
|
|
int signal_setup (int sig, void (*handler)(int))
|
2008-02-01 18:50:19 +05:30
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
memset (&sa, 0, sizeof (sa));
|
|
|
|
sigemptyset (&sa.sa_mask);
|
2008-02-02 05:47:35 +05:30
|
|
|
sa.sa_handler = handler;
|
|
|
|
return (sigaction (sig, &sa, NULL));
|
2008-02-01 18:50:19 +05:30
|
|
|
}
|