2007-04-05 16:48:42 +05:30
|
|
|
/*
|
|
|
|
rc-misc.c
|
|
|
|
rc misc functions
|
|
|
|
Copyright 2007 Gentoo Foundation
|
|
|
|
*/
|
|
|
|
|
2007-04-13 19:38:16 +05:30
|
|
|
#include "librc.h"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
#define ERRX fprintf (stderr, "out of memory\n"); exit (1)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-05-02 18:03:56 +05:30
|
|
|
#define PROFILE_ENV "/etc/profile.env"
|
2007-08-09 20:03:20 +05:30
|
|
|
#define SYS_WHITELIST RC_LIBDIR "/conf.d/env_whitelist"
|
2007-05-02 18:03:56 +05:30
|
|
|
#define USR_WHITELIST "/etc/conf.d/env_whitelist"
|
|
|
|
#define RC_CONFIG "/etc/conf.d/rc"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-08-09 20:03:20 +05:30
|
|
|
#define PATH_PREFIX RC_LIBDIR "/bin:/bin:/sbin:/usr/bin:/usr/sbin"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
#ifndef S_IXUGO
|
|
|
|
# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void *rc_xmalloc (size_t size)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
void *value = malloc (size);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (value)
|
|
|
|
return (value);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
ERRX;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_xmalloc)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
void *rc_xrealloc (void *ptr, size_t size)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
void *value = realloc (ptr, size);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (value)
|
|
|
|
return (value);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
ERRX;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_xrealloc)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
char *rc_xstrdup (const char *str)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char *value;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! str)
|
|
|
|
return (NULL);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-19 21:01:01 +05:30
|
|
|
value = strdup (str);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (value)
|
|
|
|
return (value);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
ERRX;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_xstrdup)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-26 12:35:33 +05:30
|
|
|
bool rc_env_bool (const char *var)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char *v;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! var)
|
2007-09-25 23:00:07 +05:30
|
|
|
return (false);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-26 12:35:33 +05:30
|
|
|
if (! (v = getenv (var))) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2007-09-26 13:22:48 +05:30
|
|
|
if (strcasecmp (v, "true") == 0 ||
|
|
|
|
strcasecmp (v, "y") == 0 ||
|
|
|
|
strcasecmp (v, "yes") == 0 ||
|
|
|
|
strcasecmp (v, "1") == 0)
|
2007-09-26 12:35:33 +05:30
|
|
|
return (true);
|
|
|
|
|
2007-09-26 13:22:48 +05:30
|
|
|
if (strcasecmp (v, "false") != 0 &&
|
|
|
|
strcasecmp (v, "n") != 0 &&
|
|
|
|
strcasecmp (v, "no") != 0 &&
|
|
|
|
strcasecmp (v, "0") != 0)
|
2007-09-26 12:35:33 +05:30
|
|
|
errno = EINVAL;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-26 12:35:33 +05:30
|
|
|
return (false);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-09-26 12:35:33 +05:30
|
|
|
librc_hidden_def(rc_env_bool)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
char *rc_strcatpaths (const char *path1, const char *paths, ...)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
va_list ap;
|
|
|
|
int length;
|
|
|
|
int i;
|
|
|
|
char *p;
|
|
|
|
char *path;
|
|
|
|
char *pathp;
|
|
|
|
|
|
|
|
if (! path1 || ! paths)
|
|
|
|
return (NULL);
|
|
|
|
|
2007-08-09 20:03:20 +05:30
|
|
|
length = strlen (path1) + strlen (paths) + 1;
|
|
|
|
if (*paths != '/')
|
|
|
|
length ++;
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
va_start (ap, paths);
|
2007-08-09 20:03:20 +05:30
|
|
|
while ((p = va_arg (ap, char *)) != NULL) {
|
|
|
|
if (*p != '/')
|
|
|
|
length ++;
|
|
|
|
length += strlen (p);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
va_end (ap);
|
|
|
|
|
2007-08-09 20:03:20 +05:30
|
|
|
pathp = path = rc_xmalloc (length * sizeof (char *));
|
2007-04-11 18:14:47 +05:30
|
|
|
memset (path, 0, length);
|
2007-08-09 20:03:20 +05:30
|
|
|
i = strlen (path1);
|
|
|
|
memcpy (path, path1, i);
|
|
|
|
pathp += i;
|
|
|
|
if (*paths != '/')
|
|
|
|
*pathp ++ = '/';
|
|
|
|
i = strlen (paths);
|
|
|
|
memcpy (pathp, paths, i);
|
|
|
|
pathp += i;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
va_start (ap, paths);
|
|
|
|
while ((p = va_arg (ap, char *)) != NULL) {
|
2007-08-09 20:03:20 +05:30
|
|
|
if (*p != '/')
|
|
|
|
*pathp ++= '/';
|
2007-04-11 18:14:47 +05:30
|
|
|
i = strlen (p);
|
|
|
|
memcpy (pathp, p, i);
|
|
|
|
pathp += i;
|
|
|
|
}
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
*pathp++ = 0;
|
|
|
|
|
|
|
|
return (path);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_strcatpaths)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
bool rc_exists (const char *pathname)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
struct stat buf;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! pathname)
|
2007-09-25 23:00:07 +05:30
|
|
|
return (false);
|
|
|
|
|
|
|
|
if (stat (pathname, &buf) == 0)
|
|
|
|
return (true);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
errno = 0;
|
|
|
|
return (false);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_exists)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
bool rc_is_file (const char *pathname)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
struct stat buf;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
if (! pathname)
|
|
|
|
return (false);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
if (stat (pathname, &buf) == 0)
|
|
|
|
return (S_ISREG (buf.st_mode));
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
return (false);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_is_file)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
bool rc_is_dir (const char *pathname)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
struct stat buf;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
if (! pathname)
|
|
|
|
return (false);
|
|
|
|
|
|
|
|
if (stat (pathname, &buf) == 0)
|
|
|
|
return (S_ISDIR (buf.st_mode));
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
errno = 0;
|
|
|
|
return (false);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_is_dir)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
bool rc_is_link (const char *pathname)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
struct stat buf;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
if (! pathname)
|
|
|
|
return (false);
|
|
|
|
|
|
|
|
if (lstat (pathname, &buf) == 0)
|
|
|
|
return (S_ISLNK (buf.st_mode));
|
2007-04-10 16:54:58 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
errno = 0;
|
|
|
|
return (false);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_is_link)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
bool rc_is_exec (const char *pathname)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
struct stat buf;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
if (! pathname)
|
|
|
|
return (false);
|
|
|
|
|
|
|
|
if (lstat (pathname, &buf) == 0)
|
|
|
|
return (buf.st_mode & S_IXUGO);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
errno = 0;
|
|
|
|
return (false);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_is_exec)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-18 17:34:51 +05:30
|
|
|
char **rc_ls_dir (const char *dir, int options)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
DIR *dp;
|
|
|
|
struct dirent *d;
|
2007-09-18 17:34:51 +05:30
|
|
|
char **list = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
if ((dp = opendir (dir)) == NULL)
|
2007-09-18 17:34:51 +05:30
|
|
|
return (NULL);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
while (((d = readdir (dp)) != NULL) && errno == 0) {
|
|
|
|
if (d->d_name[0] != '.') {
|
|
|
|
if (options & RC_LS_INITD) {
|
|
|
|
int l = strlen (d->d_name);
|
|
|
|
char *init = rc_strcatpaths (RC_INITDIR, d->d_name,
|
|
|
|
(char *) NULL);
|
2007-09-25 23:00:07 +05:30
|
|
|
bool ok = rc_exists (init);
|
2007-04-11 18:14:47 +05:30
|
|
|
free (init);
|
2007-09-25 23:00:07 +05:30
|
|
|
if (! ok)
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
/* .sh files are not init scripts */
|
|
|
|
if (l > 2 && d->d_name[l - 3] == '.' &&
|
|
|
|
d->d_name[l - 2] == 's' &&
|
|
|
|
d->d_name[l - 1] == 'h')
|
|
|
|
continue;
|
|
|
|
}
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_addsort (&list, d->d_name);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir (dp);
|
|
|
|
|
|
|
|
return (list);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_ls_dir)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
bool rc_rm_dir (const char *pathname, bool top)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
DIR *dp;
|
|
|
|
struct dirent *d;
|
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
if ((dp = opendir (pathname)) == NULL)
|
2007-09-25 23:00:07 +05:30
|
|
|
return (false);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
while (((d = readdir (dp)) != NULL) && errno == 0) {
|
|
|
|
if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) {
|
|
|
|
char *tmp = rc_strcatpaths (pathname, d->d_name, (char *) NULL);
|
|
|
|
if (d->d_type == DT_DIR) {
|
2007-09-25 23:00:07 +05:30
|
|
|
if (! rc_rm_dir (tmp, true))
|
2007-04-11 18:14:47 +05:30
|
|
|
{
|
|
|
|
free (tmp);
|
|
|
|
closedir (dp);
|
2007-09-25 23:00:07 +05:30
|
|
|
return (false);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (unlink (tmp)) {
|
|
|
|
free (tmp);
|
|
|
|
closedir (dp);
|
2007-09-25 23:00:07 +05:30
|
|
|
return (false);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
free (tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir (dp);
|
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
if (top && rmdir (pathname) != 0)
|
|
|
|
return (false);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
return (true);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_rm_dir)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-18 17:34:51 +05:30
|
|
|
char **rc_get_config (const char *file)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-09-18 17:34:51 +05:30
|
|
|
char **list = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
FILE *fp;
|
|
|
|
char buffer[RC_LINEBUFFER];
|
|
|
|
char *p;
|
|
|
|
char *token;
|
|
|
|
char *line;
|
|
|
|
char *linep;
|
|
|
|
char *linetok;
|
|
|
|
int i = 0;
|
|
|
|
bool replaced;
|
|
|
|
char *entry;
|
|
|
|
char *newline;
|
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
if (! (fp = fopen (file, "r")))
|
2007-09-18 22:27:39 +05:30
|
|
|
return (NULL);
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
while (fgets (buffer, RC_LINEBUFFER, fp)) {
|
|
|
|
p = buffer;
|
|
|
|
|
|
|
|
/* Strip leading spaces/tabs */
|
|
|
|
while ((*p == ' ') || (*p == '\t'))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (! p || strlen (p) < 3 || p[0] == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Get entry */
|
|
|
|
token = strsep (&p, "=");
|
2007-07-20 03:14:55 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! token)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
entry = rc_xstrdup (token);
|
|
|
|
|
2007-07-09 15:56:02 +05:30
|
|
|
/* Preserve shell coloring */
|
|
|
|
if (*p == '$')
|
|
|
|
token = p;
|
|
|
|
else
|
|
|
|
do {
|
|
|
|
/* Bash variables are usually quoted */
|
|
|
|
token = strsep (&p, "\"\'");
|
|
|
|
} while ((token) && (strlen (token) == 0));
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Drop a newline if that's all we have */
|
|
|
|
i = strlen (token) - 1;
|
|
|
|
if (token[i] == 10)
|
|
|
|
token[i] = 0;
|
|
|
|
|
|
|
|
i = strlen (entry) + strlen (token) + 2;
|
|
|
|
newline = rc_xmalloc (i);
|
|
|
|
snprintf (newline, i, "%s=%s", entry, token);
|
|
|
|
|
|
|
|
replaced = false;
|
|
|
|
/* In shells the last item takes precedence, so we need to remove
|
|
|
|
any prior values we may already have */
|
|
|
|
STRLIST_FOREACH (list, line, i) {
|
|
|
|
char *tmp = rc_xstrdup (line);
|
|
|
|
linep = tmp;
|
|
|
|
linetok = strsep (&linep, "=");
|
|
|
|
if (strcmp (linetok, entry) == 0) {
|
|
|
|
/* We have a match now - to save time we directly replace it */
|
|
|
|
free (list[i - 1]);
|
2007-04-13 19:38:16 +05:30
|
|
|
list[i - 1] = newline;
|
2007-04-11 18:14:47 +05:30
|
|
|
replaced = true;
|
|
|
|
free (tmp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free (tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! replaced) {
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_addsort (&list, newline);
|
2007-04-11 18:14:47 +05:30
|
|
|
free (newline);
|
|
|
|
}
|
|
|
|
free (entry);
|
|
|
|
}
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
return (list);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_get_config)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
char *rc_get_config_entry (char **list, const char *entry)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char *line;
|
|
|
|
int i;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
STRLIST_FOREACH (list, line, i) {
|
|
|
|
p = strchr (line, '=');
|
|
|
|
if (p && strncmp (entry, line, p - line) == 0)
|
|
|
|
return (p += 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_get_config_entry)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-18 17:34:51 +05:30
|
|
|
char **rc_get_list (const char *file)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
FILE *fp;
|
|
|
|
char buffer[RC_LINEBUFFER];
|
|
|
|
char *p;
|
|
|
|
char *token;
|
2007-09-18 17:34:51 +05:30
|
|
|
char **list = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
if (! (fp = fopen (file, "r")))
|
2007-09-18 17:34:51 +05:30
|
|
|
return (NULL);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
while (fgets (buffer, RC_LINEBUFFER, fp)) {
|
|
|
|
p = buffer;
|
|
|
|
|
|
|
|
/* Strip leading spaces/tabs */
|
|
|
|
while ((*p == ' ') || (*p == '\t'))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
/* Get entry - we do not want comments */
|
|
|
|
token = strsep (&p, "#");
|
|
|
|
if (token && (strlen (token) > 1)) {
|
|
|
|
/* Stip the newline if present */
|
|
|
|
if (token[strlen (token) - 1] == '\n')
|
|
|
|
token[strlen (token) - 1] = 0;
|
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&list, token);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
return (list);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_get_list)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
char **rc_filter_env (void)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char **env = NULL;
|
|
|
|
char **whitelist = NULL;
|
|
|
|
char *env_name = NULL;
|
|
|
|
char **profile = NULL;
|
|
|
|
int count = 0;
|
|
|
|
bool got_path = false;
|
|
|
|
char *env_var;
|
|
|
|
int env_len;
|
|
|
|
char *p;
|
|
|
|
char *token;
|
|
|
|
char *sep;
|
|
|
|
char *e;
|
|
|
|
int pplen = strlen (PATH_PREFIX);
|
|
|
|
|
2007-09-18 17:34:51 +05:30
|
|
|
whitelist = rc_get_list (SYS_WHITELIST);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! whitelist)
|
2007-09-29 22:12:08 +05:30
|
|
|
fprintf (stderr, "system environment whitelist (" SYS_WHITELIST ") missing\n");
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-09-18 21:13:19 +05:30
|
|
|
env = rc_get_list (USR_WHITELIST);
|
|
|
|
rc_strlist_join (&whitelist, env);
|
|
|
|
rc_strlist_free (env);
|
|
|
|
env = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (! whitelist)
|
|
|
|
return (NULL);
|
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_is_file (PROFILE_ENV))
|
2007-09-18 17:34:51 +05:30
|
|
|
profile = rc_get_config (PROFILE_ENV);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
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;
|
|
|
|
p = rc_xmalloc (sizeof (char *) * env_len);
|
|
|
|
snprintf (p, env_len, "export %s", env_name);
|
|
|
|
env_var = rc_get_config_entry (profile, p);
|
|
|
|
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 &&
|
|
|
|
strncmp (PATH_PREFIX, env_var, pplen) != 0)
|
|
|
|
{
|
|
|
|
got_path = true;
|
|
|
|
env_len = strlen (env_name) + strlen (env_var) + pplen + 2;
|
|
|
|
e = p = rc_xmalloc (sizeof (char *) * env_len);
|
|
|
|
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-04-19 20:24:35 +05:30
|
|
|
char *np = rc_xstrdup (PATH_PREFIX);
|
2007-04-11 18:14:47 +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;
|
|
|
|
e = rc_xmalloc (sizeof (char *) * env_len);
|
|
|
|
snprintf (e, env_len, "%s=%s", env_name, env_var);
|
|
|
|
}
|
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&env, e);
|
2007-04-11 18:14:47 +05:30
|
|
|
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) {
|
|
|
|
env_len = strlen ("PATH=") + strlen (PATH_PREFIX) + 2;
|
|
|
|
p = rc_xmalloc (sizeof (char *) * env_len);
|
|
|
|
snprintf (p, env_len, "PATH=%s", PATH_PREFIX);
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&env, p);
|
2007-04-11 18:14:47 +05:30
|
|
|
free (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc_strlist_free (whitelist);
|
|
|
|
rc_strlist_free (profile);
|
|
|
|
|
|
|
|
return (env);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_filter_env)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
/* Other systems may need this at some point, but for now it's Linux only */
|
|
|
|
#ifdef __linux__
|
|
|
|
static bool file_regex (const char *file, const char *regex)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
FILE *fp;
|
|
|
|
char buffer[RC_LINEBUFFER];
|
|
|
|
regex_t re;
|
|
|
|
bool retval = false;
|
|
|
|
int result;
|
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
if (! (fp = fopen (file, "r")))
|
2007-04-11 18:14:47 +05:30
|
|
|
return (false);
|
|
|
|
|
|
|
|
if ((result = regcomp (&re, regex, REG_EXTENDED | REG_NOSUB)) != 0) {
|
|
|
|
fclose (fp);
|
|
|
|
regerror (result, &re, buffer, sizeof (buffer));
|
2007-09-29 22:12:08 +05:30
|
|
|
fprintf (stderr, "file_regex: %s", buffer);
|
2007-04-11 18:14:47 +05:30
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets (buffer, RC_LINEBUFFER, fp)) {
|
|
|
|
if (regexec (&re, buffer, 0, NULL, 0) == 0)
|
|
|
|
{
|
|
|
|
retval = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose (fp);
|
|
|
|
regfree (&re);
|
|
|
|
|
|
|
|
return (retval);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-09-18 17:50:55 +05:30
|
|
|
char **rc_make_env (void)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-09-18 17:50:55 +05:30
|
|
|
char **env = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
char *line;
|
|
|
|
int i;
|
|
|
|
char *p;
|
2007-05-15 21:26:35 +05:30
|
|
|
char **config;
|
2007-04-11 18:14:47 +05:30
|
|
|
char *e;
|
2007-07-12 15:18:44 +05:30
|
|
|
#ifdef __linux__
|
2007-04-11 18:14:47 +05:30
|
|
|
char sys[6];
|
2007-07-12 15:18:44 +05:30
|
|
|
#endif
|
2007-04-11 18:14:47 +05:30
|
|
|
struct utsname uts;
|
|
|
|
bool has_net_fs_list = false;
|
|
|
|
FILE *fp;
|
|
|
|
char buffer[PATH_MAX];
|
2007-10-02 15:27:23 +05:30
|
|
|
char *runlevel = rc_runlevel_get ();
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-05-15 21:26:35 +05:30
|
|
|
/* Don't trust environ for softlevel yet */
|
2007-09-25 21:08:21 +05:30
|
|
|
snprintf (buffer, PATH_MAX, "%s.%s", RC_CONFIG, runlevel);
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_exists (buffer))
|
2007-09-18 17:34:51 +05:30
|
|
|
config = rc_get_config (buffer);
|
2007-05-15 21:26:35 +05:30
|
|
|
else
|
2007-09-18 17:34:51 +05:30
|
|
|
config = rc_get_config (RC_CONFIG);
|
2007-05-15 21:26:35 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
STRLIST_FOREACH (config, line, i) {
|
|
|
|
p = strchr (line, '=');
|
|
|
|
if (! p)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
*p = 0;
|
|
|
|
e = getenv (line);
|
|
|
|
if (! e) {
|
|
|
|
*p = '=';
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&env, line);
|
2007-04-11 18:14:47 +05:30
|
|
|
} else {
|
|
|
|
int len = strlen (line) + strlen (e) + 2;
|
|
|
|
char *new = rc_xmalloc (sizeof (char *) * len);
|
|
|
|
snprintf (new, len, "%s=%s", line, e);
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&env, new);
|
2007-04-11 18:14:47 +05:30
|
|
|
free (new);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rc_strlist_free (config);
|
|
|
|
|
|
|
|
/* One char less to drop the trailing / */
|
2007-08-09 20:03:20 +05:30
|
|
|
i = strlen ("RC_LIBDIR=") + strlen (RC_LIBDIR) + 1;
|
2007-04-11 18:14:47 +05:30
|
|
|
line = rc_xmalloc (sizeof (char *) * i);
|
|
|
|
snprintf (line, i, "RC_LIBDIR=" RC_LIBDIR);
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&env, line);
|
2007-04-11 18:14:47 +05:30
|
|
|
free (line);
|
|
|
|
|
|
|
|
/* One char less to drop the trailing / */
|
2007-08-09 20:03:20 +05:30
|
|
|
i = strlen ("RC_SVCDIR=") + strlen (RC_SVCDIR) + 1;
|
2007-04-11 18:14:47 +05:30
|
|
|
line = rc_xmalloc (sizeof (char *) * i);
|
|
|
|
snprintf (line, i, "RC_SVCDIR=" RC_SVCDIR);
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&env, line);
|
2007-04-11 18:14:47 +05:30
|
|
|
free (line);
|
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&env, "RC_BOOTLEVEL=" RC_LEVEL_BOOT);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-09-25 21:08:21 +05:30
|
|
|
i = strlen ("RC_SOFTLEVEL=") + strlen (runlevel) + 1;
|
2007-04-11 18:14:47 +05:30
|
|
|
line = rc_xmalloc (sizeof (char *) * i);
|
2007-09-25 21:08:21 +05:30
|
|
|
snprintf (line, i, "RC_SOFTLEVEL=%s", runlevel);
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&env, line);
|
2007-04-11 18:14:47 +05:30
|
|
|
free (line);
|
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
if ((fp = fopen (RC_KSOFTLEVEL, "r"))) {
|
|
|
|
memset (buffer, 0, sizeof (buffer));
|
|
|
|
if (fgets (buffer, sizeof (buffer), fp)) {
|
|
|
|
i = strlen (buffer) - 1;
|
|
|
|
if (buffer[i] == '\n')
|
|
|
|
buffer[i] = 0;
|
|
|
|
i += strlen ("RC_DEFAULTLEVEL=") + 2;
|
|
|
|
line = rc_xmalloc (sizeof (char *) * i);
|
|
|
|
snprintf (line, i, "RC_DEFAULTLEVEL=%s", buffer);
|
|
|
|
rc_strlist_add (&env, line);
|
|
|
|
free (line);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-09-29 22:12:08 +05:30
|
|
|
fclose (fp);
|
2007-04-11 18:14:47 +05:30
|
|
|
} else
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&env, "RC_DEFAULTLEVEL=" RC_LEVEL_DEFAULT);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
|
2007-07-12 15:18:44 +05:30
|
|
|
#ifdef __linux__
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Linux can run some funky stuff like Xen, VServer, UML, etc
|
|
|
|
We store this special system in RC_SYS so our scripts run fast */
|
2007-07-12 15:18:44 +05:30
|
|
|
memset (sys, 0, sizeof (sys));
|
|
|
|
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_is_dir ("/proc/xen")) {
|
2007-09-29 22:12:08 +05:30
|
|
|
if ((fp = fopen ("/proc/xen/capabilities", "r"))) {
|
2007-04-11 18:14:47 +05:30
|
|
|
fclose (fp);
|
|
|
|
if (file_regex ("/proc/xen/capabilities", "control_d"))
|
|
|
|
snprintf (sys, sizeof (sys), "XENU");
|
|
|
|
}
|
2007-05-23 18:00:34 +05:30
|
|
|
if (! sys[0])
|
2007-04-11 18:14:47 +05:30
|
|
|
snprintf (sys, sizeof (sys), "XEN0");
|
2007-05-23 18:00:34 +05:30
|
|
|
} else if (file_regex ("/proc/cpuinfo", "UML")) {
|
2007-04-11 18:14:47 +05:30
|
|
|
snprintf (sys, sizeof (sys), "UML");
|
2007-05-23 18:00:34 +05:30
|
|
|
} else if (file_regex ("/proc/self/status",
|
|
|
|
"(s_context|VxID|envID):[[:space:]]*[1-9]"))
|
|
|
|
{
|
|
|
|
snprintf (sys, sizeof (sys), "VPS");
|
|
|
|
}
|
2007-07-12 15:18:44 +05:30
|
|
|
|
|
|
|
if (sys[0]) {
|
|
|
|
i = strlen ("RC_SYS=") + strlen (sys) + 2;
|
|
|
|
line = rc_xmalloc (sizeof (char *) * i);
|
|
|
|
snprintf (line, i, "RC_SYS=%s", sys);
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&env, line);
|
2007-07-12 15:18:44 +05:30
|
|
|
free (line);
|
|
|
|
}
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Only add a NET_FS list if not defined */
|
|
|
|
STRLIST_FOREACH (env, line, i)
|
|
|
|
if (strncmp (line, "RC_NET_FS_LIST=", strlen ("RC_NET_FS_LIST=")) == 0) {
|
|
|
|
has_net_fs_list = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! has_net_fs_list) {
|
|
|
|
i = strlen ("RC_NET_FS_LIST=") + strlen (RC_NET_FS_LIST_DEFAULT) + 1;
|
|
|
|
line = rc_xmalloc (sizeof (char *) * i);
|
|
|
|
snprintf (line, i, "RC_NET_FS_LIST=%s", RC_NET_FS_LIST_DEFAULT);
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&env, line);
|
2007-04-11 18:14:47 +05:30
|
|
|
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) {
|
|
|
|
i = strlen ("RC_UNAME=") + strlen (uts.sysname) + 2;
|
|
|
|
line = rc_xmalloc (sizeof (char *) * i);
|
|
|
|
snprintf (line, i, "RC_UNAME=%s", uts.sysname);
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&env, line);
|
2007-04-11 18:14:47 +05:30
|
|
|
free (line);
|
|
|
|
}
|
|
|
|
|
2007-09-25 21:08:21 +05:30
|
|
|
free (runlevel);
|
2007-04-11 18:14:47 +05:30
|
|
|
return (env);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-09-18 17:50:55 +05:30
|
|
|
librc_hidden_def(rc_make_env)
|