From 6654ad1300aab0f6bc53bfe2b06877bf315505e5 Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Wed, 8 May 2013 08:14:23 -0400 Subject: [PATCH] ncmlib should be tracked as an external repo. --- ncmlib/CMakeLists.txt | 3 -- ncmlib/cap.c | 63 ----------------------- ncmlib/cap.h | 35 ------------- ncmlib/chroot.c | 115 ------------------------------------------ ncmlib/chroot.h | 43 ---------------- ncmlib/defines.h | 38 -------------- ncmlib/io.c | 90 --------------------------------- ncmlib/io.h | 40 --------------- ncmlib/log.c | 86 ------------------------------- ncmlib/log.h | 47 ----------------- ncmlib/malloc.c | 51 ------------------- ncmlib/malloc.h | 37 -------------- ncmlib/pidfile.c | 79 ----------------------------- ncmlib/pidfile.h | 37 -------------- ncmlib/random.c | 38 -------------- ncmlib/random.h | 6 --- ncmlib/seccomp-bpf.h | 81 ----------------------------- ncmlib/signals.c | 59 ---------------------- ncmlib/signals.h | 36 ------------- ncmlib/strl.c | 52 ------------------- ncmlib/strl.h | 38 -------------- ncmlib/strlist.c | 108 --------------------------------------- ncmlib/strlist.h | 43 ---------------- 23 files changed, 1225 deletions(-) delete mode 100644 ncmlib/CMakeLists.txt delete mode 100644 ncmlib/cap.c delete mode 100644 ncmlib/cap.h delete mode 100644 ncmlib/chroot.c delete mode 100644 ncmlib/chroot.h delete mode 100644 ncmlib/defines.h delete mode 100644 ncmlib/io.c delete mode 100644 ncmlib/io.h delete mode 100644 ncmlib/log.c delete mode 100644 ncmlib/log.h delete mode 100644 ncmlib/malloc.c delete mode 100644 ncmlib/malloc.h delete mode 100644 ncmlib/pidfile.c delete mode 100644 ncmlib/pidfile.h delete mode 100644 ncmlib/random.c delete mode 100644 ncmlib/random.h delete mode 100644 ncmlib/seccomp-bpf.h delete mode 100644 ncmlib/signals.c delete mode 100644 ncmlib/signals.h delete mode 100644 ncmlib/strl.c delete mode 100644 ncmlib/strl.h delete mode 100644 ncmlib/strlist.c delete mode 100644 ncmlib/strlist.h diff --git a/ncmlib/CMakeLists.txt b/ncmlib/CMakeLists.txt deleted file mode 100644 index fe56908..0000000 --- a/ncmlib/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -file(GLOB NCMLIB_SRCS "*.c") - -add_library(ncmlib ${NCMLIB_SRCS}) diff --git a/ncmlib/cap.c b/ncmlib/cap.c deleted file mode 100644 index dd8f530..0000000 --- a/ncmlib/cap.c +++ /dev/null @@ -1,63 +0,0 @@ -/* cap.c - POSIX capability support - * Time-stamp: <2010-11-12 09:01:07 njk> - * - * (c) 2004-2010 Nicholas J. Kain - * 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 -#include -#include -#include -#include -#include - -#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); -} diff --git a/ncmlib/cap.h b/ncmlib/cap.h deleted file mode 100644 index 9e1ae74..0000000 --- a/ncmlib/cap.h +++ /dev/null @@ -1,35 +0,0 @@ -/* cap.h - POSIX capability support - * Time-stamp: <2010-11-12 08:59:46 njk> - * - * (c) 2005-2010 Nicholas J. Kain - * 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 */ diff --git a/ncmlib/chroot.c b/ncmlib/chroot.c deleted file mode 100644 index 4766484..0000000 --- a/ncmlib/chroot.c +++ /dev/null @@ -1,115 +0,0 @@ -/* chroot.c - chroots jobs and drops privs - * - * (c) 2005-2013 Nicholas J. Kain - * 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 -#include -#include -#include -#include - -#include "defines.h" -#include "log.h" -#include "strl.h" - -static char chrootd[MAX_PATH_LENGTH] = "\0"; -static char chroot_modified; -static char chroot_enable = 1; - -void disable_chroot(void) -{ - chroot_enable = 0; -} - -int chroot_enabled(void) -{ - return chroot_enable; -} - -void update_chroot(const char *path) -{ - strnkcpy(chrootd, path, sizeof chrootd); - chroot_modified = 1; -} - -int chroot_exists(void) -{ - return chroot_modified; -} - -char *get_chroot(void) -{ - return chrootd; -} - -void wipe_chroot(void) -{ - memset(chrootd, '\0', sizeof chrootd); -} - -void imprison(const char *chroot_dir) -{ - if (chdir(chroot_dir)) { - log_line("Failed to chdir(%s)!", chroot_dir); - exit(EXIT_FAILURE); - } - if (!chroot_enable) - return; - if (chroot(chroot_dir)) { - log_line("Failed to chroot(%s)!", chroot_dir); - exit(EXIT_FAILURE); - } -} - -void drop_root(uid_t uid, gid_t gid) -{ - if (uid == 0 || gid == 0) { - log_line("FATAL - drop_root: attempt to drop root to root?\n"); - exit(EXIT_FAILURE); - } - - if (getgid() == 0) { - if (setregid(gid, gid) == -1) { - log_line("FATAL - drop_root: failed to drop real gid == root!\n"); - exit(EXIT_FAILURE); - } - } - - if (getuid() == 0) { - if (setreuid(uid, uid) == -1) { - log_line("FATAL - drop_root: failed to drop real uid == root!\n"); - exit(EXIT_FAILURE); - } - } - - /* be absolutely sure */ - if (getgid() == 0 || getuid() == 0) { - log_line("FATAL - drop_root: tried to drop root, but still have root!\n"); - exit(EXIT_FAILURE); - } -} - diff --git a/ncmlib/chroot.h b/ncmlib/chroot.h deleted file mode 100644 index b859f86..0000000 --- a/ncmlib/chroot.h +++ /dev/null @@ -1,43 +0,0 @@ -/* chroot.h - include file for chroot.c - * Time-stamp: <2010-11-03 05:24:09 njk> - * - * (c) 2005-2010 Nicholas J. Kain - * 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_CHROOT_H_ -#define NCM_CHROOT_H_ - -void disable_chroot(void); -int chroot_enabled(void); -void update_chroot(const char *path); -char *get_chroot(void); -int chroot_exists(void); -void wipe_chroot(void); -void imprison(const char *path); -void drop_root(uid_t uid, gid_t gid); - -#endif - diff --git a/ncmlib/defines.h b/ncmlib/defines.h deleted file mode 100644 index a567f39..0000000 --- a/ncmlib/defines.h +++ /dev/null @@ -1,38 +0,0 @@ -/* defines.h - general #defines - * Time-stamp: <2010-11-02 22:27:10 njk> - * - * (c) 2004-2010 Nicholas J. Kain - * 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_DEFINES_H_ -#define NCM_DEFINES_H_ - -#define DEFAULT_PATH "/bin:/usr/bin:/usr/local/bin" -#define MAXLINE 1024 -#define MAX_ARGS 30 -#define MAX_PATH_LENGTH 1024 - -#endif diff --git a/ncmlib/io.c b/ncmlib/io.c deleted file mode 100644 index 0847755..0000000 --- a/ncmlib/io.c +++ /dev/null @@ -1,90 +0,0 @@ -/* io.c - light wrappers for POSIX i/o functions - * Time-stamp: <2011-06-10 13:51:03 njk> - * - * (c) 2010 Nicholas J. Kain - * 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 -#include -#include -#include - -/* returns -1 on error, >= 0 and equal to # chars read on success */ -int safe_read(int fd, char *buf, int len) -{ - int r, s = 0; - while (s < len) { - r = read(fd, buf + s, len - s); - if (r == 0) - break; - if (r == -1) { - if (errno == EINTR) - continue; - else if ((errno == EAGAIN || errno == EWOULDBLOCK) && s > 0) - return s; - else - return -1; - } - s += r; - } - return s; -} - -/* returns -1 on error, >= 0 and equal to # chars written on success */ -int safe_write(int fd, const char *buf, int len) -{ - int r, s = 0; - while (s < len) { - r = write(fd, buf + s, len - s); - if (r == -1) { - if (errno == EINTR) - continue; - else - return -1; - } - s += r; - } - return s; -} - -/* returns -1 on error, >= 0 and equal to # chars written on success */ -int safe_sendto(int fd, const char *buf, int len, int flags, - const struct sockaddr *dest_addr, socklen_t addrlen) -{ - int r, s = 0; - while (s < len) { - r = sendto(fd, buf + s, len - s, flags, dest_addr, addrlen); - if (r == -1) { - if (errno == EINTR) - continue; - else - return -1; - } - s += r; - } - return s; -} - diff --git a/ncmlib/io.h b/ncmlib/io.h deleted file mode 100644 index 552907e..0000000 --- a/ncmlib/io.h +++ /dev/null @@ -1,40 +0,0 @@ -/* io.h - light wrappers for POSIX i/o functions - * Time-stamp: <2010-11-15 19:45:32 njk> - * - * (c) 2010 Nicholas J. Kain - * 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_IO_H_ -#define NCM_IO_H_ - -#include - -int safe_read(int fd, char *buf, int len); -int safe_write(int fd, const char *buf, int len); -int safe_sendto(int fd, const char *buf, int len, int flags, - const struct sockaddr *dest_addr, socklen_t addrlen); - -#endif /* NCM_IO_H_ */ diff --git a/ncmlib/log.c b/ncmlib/log.c deleted file mode 100644 index 6654c30..0000000 --- a/ncmlib/log.c +++ /dev/null @@ -1,86 +0,0 @@ -/* log.c - simple logging support - * Time-stamp: <2010-11-12 05:19:46 njk> - * - * (c) 2003-2010 Nicholas J. Kain - * 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 -#include -#include -#include -#include - -/* global logging flags */ -int gflags_quiet = 0; -int gflags_detach = 1; -char *gflags_log_name = NULL; - -#define log_syslog(level) do { \ - openlog(gflags_log_name, LOG_PID, LOG_DAEMON); \ - va_start(argp, format); \ - vsyslog(level | LOG_DAEMON, format, argp); \ - va_end(argp); \ - closelog(); } while(0) - -#define log_stdio() do { \ - va_start(argp, format); \ - vfprintf(stderr, format, argp); \ - fprintf(stderr, "\n"); \ - va_end(argp); } while(0) - -void log_line_l(int level, const char *format, ...) -{ - va_list argp; - - if (gflags_quiet) - return; - - if (gflags_detach) - log_syslog(level); - else - log_stdio(); -} - -void suicide(const char *format, ...) -{ - va_list argp; - - if (gflags_quiet) - goto out; - - if (gflags_detach) - log_syslog(LOG_ERR); - else { - log_stdio(); - perror(NULL); - } -out: - exit(EXIT_FAILURE); -} - -#undef log_syslog -#undef log_stdio - diff --git a/ncmlib/log.h b/ncmlib/log.h deleted file mode 100644 index fc155f3..0000000 --- a/ncmlib/log.h +++ /dev/null @@ -1,47 +0,0 @@ -/* log.h - simple logging support - * Time-stamp: <2010-11-12 05:25:04 njk> - * - * (c) 2003-2010 Nicholas J. Kain - * 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_LOG_H_ -#define NCM_LOG_H_ 1 - -#include - -extern int gflags_quiet; -extern int gflags_detach; -extern char *gflags_log_name; - -#define log_line(...) log_line_l(LOG_INFO, __VA_ARGS__) -#define log_warning(...) log_line_l(LOG_WARNING, __VA_ARGS__) -#define log_error(...) log_line_l(LOG_ERR, __VA_ARGS__) - -void log_line_l(int level, const char *format, ...); -void suicide(const char *format, ...); - -#endif - diff --git a/ncmlib/malloc.c b/ncmlib/malloc.c deleted file mode 100644 index e598527..0000000 --- a/ncmlib/malloc.c +++ /dev/null @@ -1,51 +0,0 @@ -/* - * malloc.c - memory allocation functions - * Time-stamp: <2010-11-02 03:17:31 nk> - * - * (c) 2005-2010 Nicholas J. Kain - * 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 -#include "log.h" - -void *xmalloc(size_t size) { - void *ret; - - ret = malloc(size); - if (ret == NULL) - suicide("FATAL - malloc() failed\n"); - return ret; -} - -void *xrealloc(void *ptr, size_t size) -{ - void *ret; - - ret = realloc(ptr, size); - if (size && ret == NULL) - suicide("FATAL - realloc() failed\n"); - return ret; -} diff --git a/ncmlib/malloc.h b/ncmlib/malloc.h deleted file mode 100644 index d82b7f9..0000000 --- a/ncmlib/malloc.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * malloc.h - memory allocation functions - * Time-stamp: <2010-11-02 03:17:44 nk> - * - * (c) 2005-2010 Nicholas J. Kain - * 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_MALLOC_H_ -#define NCM_MALLOC_H_ - -void *xmalloc(size_t size); -void *xrealloc(void *ptr, size_t size); - -#endif diff --git a/ncmlib/pidfile.c b/ncmlib/pidfile.c deleted file mode 100644 index ff88b8b..0000000 --- a/ncmlib/pidfile.c +++ /dev/null @@ -1,79 +0,0 @@ -/* pidfile.c - process id file functions - * Time-stamp: <2010-11-03 05:19:23 nk> - * - * (c) 2003-2010 Nicholas J. Kain - * 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 -#include -#include -#include -#include - -#include "defines.h" -#include "log.h" - -void write_pid(const char *file) { - FILE *f; - size_t written, len; - char buf[MAXLINE]; - - f = fopen(file, "w"); - if (f == NULL) { - log_line("FATAL - failed to open pid file \"%s\"!\n", file); - exit(EXIT_FAILURE); - } - - snprintf(buf, sizeof buf - 1, "%i", (unsigned int)getpid()); - len = strlen(buf); - written = 0; - while (written < len) - written = fwrite(buf + written, sizeof (char), len - written, f); - - if (fclose(f) != 0) { - log_line("FATAL - failed to close pid file \"%s\"!\n", file); - exit(EXIT_FAILURE); - } -} - -/* Return 0 on success, -1 on failure. */ -int file_exists(const char *file, const char *mode) { - FILE *f; - - if (file == NULL || mode == NULL) { - log_line("file_exists: FATAL - coding bug: NULL passed\n"); - return -1; - } - - f = fopen(file, mode); - if (f == NULL) { - log_line("file_exists: FATAL - can't open file %s with mode %s!\n", - file, mode); - return -1; - } - fclose(f); - return 0; -} diff --git a/ncmlib/pidfile.h b/ncmlib/pidfile.h deleted file mode 100644 index a9828a4..0000000 --- a/ncmlib/pidfile.h +++ /dev/null @@ -1,37 +0,0 @@ -/* pidfile.h - process id file functions - * Time-stamp: <2010-11-03 05:19:41 nk> - * - * (c) 2003-2010 Nicholas J. Kain - * 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_PIDFILE_H_ -#define NCM_PIDFILE_H_ 1 - -void write_pid(const char *file); -int file_exists(const char *file, const char *mode); - -#endif - diff --git a/ncmlib/random.c b/ncmlib/random.c deleted file mode 100644 index 839f699..0000000 --- a/ncmlib/random.c +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "log.h" -#include "io.h" - -// Generate a 32-bit pseudorandom number using libc rand() -uint32_t libc_random_u32(void) -{ - static int initialized; - if (initialized) - return rand(); - - uint32_t seed; - int fd = open("/dev/urandom", O_RDONLY); - if (fd != -1) { - int r = safe_read(fd, (char *)&seed, sizeof seed); - if (r == -1) { - log_warning("Could not read /dev/urandom: %s", strerror(errno)); - close(fd); - seed = time(0); - } - } else { - log_warning("Could not open /dev/urandom: %s", strerror(errno)); - seed = time(0); - } - srand(seed); - initialized = 1; - return rand(); -} - - diff --git a/ncmlib/random.h b/ncmlib/random.h deleted file mode 100644 index 47ca3fa..0000000 --- a/ncmlib/random.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef NCMLIB_RANDOM__ -#define NCMLIB_RANDOM__ -uint32_t libc_random_u32(void); - -#endif - diff --git a/ncmlib/seccomp-bpf.h b/ncmlib/seccomp-bpf.h deleted file mode 100644 index b123787..0000000 --- a/ncmlib/seccomp-bpf.h +++ /dev/null @@ -1,81 +0,0 @@ -/* - * seccomp example for x86 (32-bit and 64-bit) with BPF macros - * - * Copyright (c) 2012 The Chromium OS Authors - * Authors: - * Will Drewry - * Kees Cook - * - * The code may be used by anyone for any purpose, and can serve as a - * starting point for developing applications using mode 2 seccomp. - */ -#ifndef _SECCOMP_BPF_H_ -#define _SECCOMP_BPF_H_ - -#define _GNU_SOURCE 1 -#include -#include -#include -#include -#include -#include -#include - -#include -#ifndef PR_SET_NO_NEW_PRIVS -# define PR_SET_NO_NEW_PRIVS 38 -#endif - -#include -#include -#include -#ifdef HAVE_LINUX_SECCOMP_H -# include -#endif -#ifndef SECCOMP_MODE_FILTER -# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */ -# define SECCOMP_RET_KILL 0x00000000U /* kill the task immediately */ -# define SECCOMP_RET_TRAP 0x00030000U /* disallow and force a SIGSYS */ -# define SECCOMP_RET_ALLOW 0x7fff0000U /* allow */ -struct seccomp_data { - int nr; - __u32 arch; - __u64 instruction_pointer; - __u64 args[6]; -}; -#endif -#ifndef SYS_SECCOMP -# define SYS_SECCOMP 1 -#endif - -#define syscall_nr (offsetof(struct seccomp_data, nr)) -#define arch_nr (offsetof(struct seccomp_data, arch)) - -#if defined(__i386__) -# define REG_SYSCALL REG_EAX -# define ARCH_NR AUDIT_ARCH_I386 -#elif defined(__x86_64__) -# define REG_SYSCALL REG_RAX -# define ARCH_NR AUDIT_ARCH_X86_64 -#else -# warning "Platform does not support seccomp filter yet" -# define REG_SYSCALL 0 -# define ARCH_NR 0 -#endif - -#define VALIDATE_ARCHITECTURE \ - BPF_STMT(BPF_LD+BPF_W+BPF_ABS, arch_nr), \ - BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ARCH_NR, 1, 0), \ - BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL) - -#define EXAMINE_SYSCALL \ - BPF_STMT(BPF_LD+BPF_W+BPF_ABS, syscall_nr) - -#define ALLOW_SYSCALL(name) \ - BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_##name, 0, 1), \ - BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW) - -#define KILL_PROCESS \ - BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL) - -#endif /* _SECCOMP_BPF_H_ */ diff --git a/ncmlib/signals.c b/ncmlib/signals.c deleted file mode 100644 index dd8223d..0000000 --- a/ncmlib/signals.c +++ /dev/null @@ -1,59 +0,0 @@ -/* signals.c - abstracts signal handling - * Time-stamp: <2010-11-01 17:25:41 nk> - * - * (c) 2004-2010 Nicholas J. Kain - * 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 -#include -#include -#include "log.h" - -void hook_signal(int signum, void (*fn)(int), int flags) { - struct sigaction new_action; - - new_action.sa_handler = fn; - sigemptyset(&new_action.sa_mask); - new_action.sa_flags = flags; - - if (sigaction(signum, &new_action, NULL)) { - log_line("FATAL - failed to hook signal %i\n", signum); - exit(EXIT_FAILURE); - } -} - -void disable_signal(int signum) { - struct sigaction new_action; - - new_action.sa_handler = SIG_IGN; - sigemptyset(&new_action.sa_mask); - new_action.sa_flags = 0; - - if (sigaction(signum, &new_action, NULL)) { - log_line("FATAL - failed to ignore signal %i\n", signum); - exit(EXIT_FAILURE); - } -} diff --git a/ncmlib/signals.h b/ncmlib/signals.h deleted file mode 100644 index 9a0a876..0000000 --- a/ncmlib/signals.h +++ /dev/null @@ -1,36 +0,0 @@ -/* signals.h - abstracts signal handling - * Time-stamp: <2010-11-01 17:26:11 nk> - * - * (c) 2004-2010 Nicholas J. Kain - * 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_SIGNALS_H_ -#define NCM_SIGNALS_H_ 1 - -void hook_signal(int signum, void (*fn)(int), int flags); -void disable_signal(int signum); - -#endif diff --git a/ncmlib/strl.c b/ncmlib/strl.c deleted file mode 100644 index a23211e..0000000 --- a/ncmlib/strl.c +++ /dev/null @@ -1,52 +0,0 @@ -/* strl.c - strnkcpy/strnkcat implementation - * - * (c) 2003-2013 Nicholas J. Kain - * 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 -#include "strl.h" - -// true == truncated, false == no truncation -bool strnkcpy (char *dest, const char *src, size_t size) -{ - if (size > 0) { - size_t i = 0; - size--; - for (; size > 0 && src[i] != '\0'; ++i, size--) - dest[i] = src[i]; - dest[i] = '\0'; - return size ? false : true; - } else - return true; -} - -// true == truncated, false == no truncation -bool strnkcat (char *dest, const char *src, size_t size) -{ - for (; size > 0 && *dest != '\0'; size--, dest++); - return strnkcpy(dest, src, size); -} - diff --git a/ncmlib/strl.h b/ncmlib/strl.h deleted file mode 100644 index deea5e8..0000000 --- a/ncmlib/strl.h +++ /dev/null @@ -1,38 +0,0 @@ -/* strl.h - strnkcpy/strnkcat implementation - * - * (c) 2003-2013 Nicholas J. Kain - * 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_STRL_H_ -#define NCM_STRL_H_ 1 - -#include - -bool strnkcpy (char *dest, const char *src, size_t size); -bool strnkcat (char *dest, const char *src, size_t size); - -#endif - diff --git a/ncmlib/strlist.c b/ncmlib/strlist.c deleted file mode 100644 index a54499f..0000000 --- a/ncmlib/strlist.c +++ /dev/null @@ -1,108 +0,0 @@ -/* strlist.c - string list functions - * Time-stamp: <2010-11-02 02:39:06 nk> - * - * (c) 2005-2010 Nicholas J. Kain - * 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 -#include -#include - -#include "strl.h" -#include "malloc.h" -#include "strlist.h" - -void add_to_strlist(strlist_t **list, char *name) -{ - strlist_t *item, *t; - char *s; - unsigned int len; - - if (!list || !name) return; - - len = strlen(name) + 1; - if (len == 1) return; - s = xmalloc(len); - strnkcpy(s, name, len); - - item = xmalloc(sizeof (strlist_t)); - item->str = s; - item->next = NULL; - - if (!*list) { - *list = item; - return; - } - - t = *list; - while (t) { - if (t->next == NULL) { - t->next = item; - return; - } - t = t->next; - } - - free(item); /* should be impossible, but hey */ - free(s); - return; -} - -void free_strlist(strlist_t *head) -{ - strlist_t *p = head, *q = NULL; - - while (p != NULL) { - free(p->str); - q = p; - p = q->next; - free(q); - } -} - -void free_stritem(strlist_t **p) -{ - strlist_t *q; - - if (!p) return; - if (!*p) return; - - q = (*p)->next; - free((*p)->str); - free(*p); - *p = q; -} - -int get_strlist_arity(strlist_t *list) -{ - int i; - strlist_t *c; - - for (c = list, i = 0; c != NULL; c = c->next, ++i); - return i; -} - - diff --git a/ncmlib/strlist.h b/ncmlib/strlist.h deleted file mode 100644 index f30e4ba..0000000 --- a/ncmlib/strlist.h +++ /dev/null @@ -1,43 +0,0 @@ -/* strlist.h - string list functions - * Time-stamp: <2010-11-02 02:39:23 nk> - * - * (c) 2005-2010 Nicholas J. Kain - * 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_STRLIST_H_ -#define NCM_STRLIST_H_ - -typedef struct { - char *str; - void *next; -} strlist_t; - -void add_to_strlist(strlist_t **list, char *name); -void free_strlist(strlist_t *head); -void free_stritem(strlist_t **p); -int get_strlist_arity(strlist_t *list); - -#endif