1N/A/***************************************************************************
1N/A *
1N/A * util_helper.c - HAL utilities for helper (as e.g. prober/addons) et al.
1N/A *
1N/A * Copyright (C) 2006 David Zeuthen, <david@fubar.dk>
1N/A *
1N/A * Licensed under the Academic Free License version 2.1
1N/A *
1N/A * This program is free software; you can redistribute it and/or modify
1N/A * it under the terms of the GNU General Public License as published by
1N/A * the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program is distributed in the hope that it will be useful,
1N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program; if not, write to the Free Software
1N/A * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1N/A *
1N/A **************************************************************************/
1N/A
1N/A#ifdef HAVE_CONFIG_H
1N/A# include <config.h>
1N/A#endif
1N/A
1N/A#include <grp.h>
1N/A#include <stdarg.h>
1N/A#include <stdlib.h>
1N/A#include <string.h>
1N/A#include <sys/time.h>
1N/A#include <time.h>
1N/A#include <pwd.h>
1N/A#include <unistd.h>
1N/A
1N/A#include "logger.h"
1N/A
1N/A#include "util_helper.h"
1N/A
1N/A#ifdef __linux__
1N/Aextern char **environ;
1N/A#endif
1N/A
1N/Astatic char **argv_buffer = NULL;
1N/Astatic size_t argv_size = 0;
1N/A
1N/A#ifdef sun
1N/A#include <priv.h>
1N/Avoid
1N/Adrop_privileges(int keep_auxgroups)
1N/A{
1N/A priv_set_t *pPrivSet;
1N/A
1N/A /*
1N/A * Start with the 'basic' privilege set and then remove any
1N/A * of the 'basic' privileges that will not be needed.
1N/A */
1N/A if ((pPrivSet = priv_allocset()) == NULL) {
1N/A return;
1N/A }
1N/A
1N/A /*
1N/A * Establish the basic set of privileges.
1N/A * Note: fork/exec required for libdevinfo devlink
1N/A * interfaces are included in the basic set.
1N/A */
1N/A priv_basicset(pPrivSet);
1N/A
1N/A /* Clear privileges we will not need from the 'basic' set */
1N/A (void) priv_delset(pPrivSet, PRIV_FILE_LINK_ANY);
1N/A
1N/A /* for sysevent need to be root and have this privilege */
1N/A (void) priv_addset(pPrivSet, PRIV_SYS_CONFIG);
1N/A
1N/A /* need proc_audit privilege */
1N/A (void) priv_addset(pPrivSet, PRIV_PROC_AUDIT);
1N/A
1N/A /* Set the permitted privilege set. */
1N/A (void) setppriv(PRIV_SET, PRIV_PERMITTED, pPrivSet);
1N/A
1N/A /* Set the limit privilege set. */
1N/A (void) setppriv(PRIV_SET, PRIV_LIMIT, pPrivSet);
1N/A
1N/A priv_freeset(pPrivSet);
1N/A}
1N/A#else /* !sun */
1N/A
1N/A/** Drop root privileges: Set the running user id to HAL_USER and
1N/A * group to HAL_GROUP, and optionally retain auxiliary groups of HAL_USER.
1N/A */
1N/Avoid
1N/Adrop_privileges (int keep_auxgroups)
1N/A{
1N/A struct passwd *pw = NULL;
1N/A struct group *gr = NULL;
1N/A
1N/A /* determine user id */
1N/A pw = getpwnam (HAL_USER);
1N/A if (!pw) {
1N/A HAL_DEBUG (("drop_privileges: user " HAL_USER " does not exist"));
1N/A exit (-1);
1N/A }
1N/A
1N/A /* determine primary group id */
1N/A gr = getgrnam (HAL_GROUP);
1N/A if (!gr) {
1N/A HAL_DEBUG (("drop_privileges: group " HAL_GROUP " does not exist"));
1N/A exit (-1);
1N/A }
1N/A
1N/A if (keep_auxgroups) {
1N/A if (initgroups (HAL_USER, gr->gr_gid)) {
1N/A HAL_DEBUG(("drop_privileges: could not initialize groups"));
1N/A exit (-1);
1N/A }
1N/A }
1N/A
1N/A if (setgid (gr->gr_gid)) {
1N/A HAL_DEBUG (("drop_privileges: could not set group id"));
1N/A exit (-1);
1N/A }
1N/A
1N/A if (setuid (pw->pw_uid)) {
1N/A HAL_DEBUG (("drop_privileges: could not set user id"));
1N/A exit (-1);
1N/A }
1N/A}
1N/A#endif /* !sun */