Move set_cap() to ncmlib/cap.[ch].

This commit is contained in:
Nicholas J. Kain 2010-11-12 09:04:43 -05:00
parent a7ea0dd378
commit b2daf09c10
9 changed files with 103 additions and 100 deletions

View File

@ -1,5 +1,5 @@
/* ifchd.c - interface change daemon /* ifchd.c - interface change daemon
* Time-stamp: <2010-11-12 06:02:59 njk> * Time-stamp: <2010-11-12 09:02:54 njk>
* *
* (C) 2004 Nicholas J. Kain <njk@aerifal.cx> * (C) 2004 Nicholas J. Kain <njk@aerifal.cx>
* *
@ -49,6 +49,7 @@
#include "strlist.h" #include "strlist.h"
#include "ifproto.h" #include "ifproto.h"
#include "strl.h" #include "strl.h"
#include "cap.h"
#include "linux.h" #include "linux.h"
enum states { enum states {

View File

@ -1,5 +1,5 @@
/* linux.c - ifchd Linux-specific functions /* linux.c - ifchd Linux-specific functions
* Time-stamp: <2010-11-12 05:14:52 njk> * Time-stamp: <2010-11-12 08:45:42 njk>
* *
* (C) 2004 Nicholas J. Kain <njk@aerifal.cx> * (C) 2004 Nicholas J. Kain <njk@aerifal.cx>
* *
@ -27,8 +27,6 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/capability.h>
#include <sys/prctl.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
@ -333,41 +331,3 @@ void perform_broadcast(int idx, char *str)
ifnam[idx], strerror(errno)); ifnam[idx], strerror(errno));
close(fd); close(fd);
} }
void set_cap(uid_t uid, gid_t gid, char *captxt)
{
cap_t caps;
if (!captxt) {
log_line("FATAL - set_cap: captxt == NULL\n");
exit(EXIT_FAILURE);
}
if (prctl(PR_SET_KEEPCAPS, 1)) {
log_line("FATAL - set_cap: prctl() failed\n");
exit(EXIT_FAILURE);
}
if (setgroups(0, NULL) == -1) {
log_line("FATAL - set_cap: setgroups() failed\n");
exit(EXIT_FAILURE);
}
if (setegid(gid) == -1 || seteuid(uid) == -1) {
log_line("FATAL - set_cap: seteuid() failed\n");
exit(EXIT_FAILURE);
}
caps = cap_from_text(captxt);
if (!caps) {
log_line("FATAL - set_cap: cap_from_text() failed\n");
exit(EXIT_FAILURE);
}
if (cap_set_proc(caps) == -1) {
log_line("FATAL - set_cap: cap_set_proc() failed\n");
exit(EXIT_FAILURE);
}
cap_free(caps);
}

View File

@ -1,5 +1,5 @@
/* linux.h - ifchd Linux-specific functions include /* linux.h - ifchd Linux-specific functions include
* Time-stamp: <2010-11-12 04:59:01 njk> * Time-stamp: <2010-11-12 09:03:29 njk>
* *
* (C) 2004 Nicholas J. Kain <njk@aerifal.cx> * (C) 2004 Nicholas J. Kain <njk@aerifal.cx>
* *
@ -31,6 +31,5 @@ void perform_subnet(int idx, char *str);
void perform_router(int idx, char *str); void perform_router(int idx, char *str);
void perform_mtu(int idx, char *str); void perform_mtu(int idx, char *str);
void perform_broadcast(int idx, char *str); void perform_broadcast(int idx, char *str);
void set_cap(uid_t uid, gid_t gid, char *captxt);
#endif #endif

63
ncmlib/cap.c Normal file
View File

@ -0,0 +1,63 @@
/* cap.c - POSIX capability support
* Time-stamp: <2010-11-12 09:01:07 njk>
*
* (c) 2004-2010 Nicholas J. Kain <njkain at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
*/
#include <unistd.h>
#include <sys/capability.h>
#include <sys/prctl.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include "log.h"
void set_cap(uid_t uid, gid_t gid, char *captxt)
{
cap_t caps;
if (!captxt)
suicide("FATAL - set_cap: captxt == NULL");
if (prctl(PR_SET_KEEPCAPS, 1))
suicide("FATAL - set_cap: prctl() failed");
if (setgroups(0, NULL) == -1)
suicide("FATAL - set_cap: setgroups() failed");
if (setegid(gid) == -1 || seteuid(uid) == -1)
suicide("FATAL - set_cap: seteuid() failed");
caps = cap_from_text(captxt);
if (!caps)
suicide("FATAL - set_cap: cap_from_text() failed");
if (cap_set_proc(caps) == -1)
suicide("FATAL - set_cap: cap_set_proc() failed");
cap_free(caps);
}

35
ncmlib/cap.h Normal file
View File

@ -0,0 +1,35 @@
/* cap.h - POSIX capability support
* Time-stamp: <2010-11-12 08:59:46 njk>
*
* (c) 2005-2010 Nicholas J. Kain <njkain at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
*/
#ifndef NCM_CAP_H
#define NCM_CAP_H
void set_cap(uid_t uid, gid_t gid, char *captxt);
#endif /* NCM_CAP_H */

View File

@ -8,7 +8,6 @@ set(NDHC_SRCS
packet.c packet.c
script.c script.c
clientpacket.c clientpacket.c
rootcap.c
dhcpc.c dhcpc.c
) )

View File

@ -36,8 +36,6 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <net/if.h> #include <net/if.h>
#include <errno.h> #include <errno.h>
#include <sys/capability.h>
#include <sys/prctl.h>
#include <pwd.h> #include <pwd.h>
#include <grp.h> #include <grp.h>
@ -50,7 +48,7 @@
#include "socket.h" #include "socket.h"
#include "log.h" #include "log.h"
#include "chroot.h" #include "chroot.h"
#include "rootcap.h" #include "cap.h"
#include "strl.h" #include "strl.h"
#define VERSION "1.0" #define VERSION "1.0"

View File

@ -1,46 +0,0 @@
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/capability.h>
#include <sys/prctl.h>
#include <grp.h>
#include "log.h"
void set_cap(uid_t uid, gid_t gid, char *captxt)
{
cap_t caps;
if (!captxt) {
log_error("FATAL - set_cap: captxt == NULL");
exit(EXIT_FAILURE);
}
if (prctl(PR_SET_KEEPCAPS, 1)) {
log_error("FATAL - set_cap: prctl() failed");
exit(EXIT_FAILURE);
}
if (setgroups(0, NULL) == -1) {
log_error("FATAL - set_cap: setgroups() failed");
exit(EXIT_FAILURE);
}
if (setegid(gid) == -1 || seteuid(uid) == -1) {
log_error("FATAL - set_cap: seteuid() failed");
exit(EXIT_FAILURE);
}
caps = cap_from_text(captxt);
if (!caps) {
log_error("FATAL - set_cap: cap_from_text() failed");
exit(EXIT_FAILURE);
}
if (cap_set_proc(caps) == -1) {
log_error("FATAL - set_cap: cap_set_proc() failed");
exit(EXIT_FAILURE);
}
cap_free(caps);
}

View File

@ -1,6 +0,0 @@
#ifndef ROOTCAP_H_
#define ROOTCAP_H_
void set_cap(uid_t uid, gid_t gid, char *captxt);
#endif /* ROOTCAP_H_ */