Skip to content

Commit

Permalink
Merge pull request #59 from saradickinson/os_logging
Browse files Browse the repository at this point in the history
Fix logging on 10.11
  • Loading branch information
saradickinson authored Dec 19, 2017
2 parents 98d8306 + 85d483a commit f0b3304
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 27 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* 2017-12-18: Version 0.2.1
* Fix use of logging on macos 10.11

* 2017-12-18: Version 0.2.0
* Add Powershell scripts for Windows 7 that will update the IPv4 DNS resolvers.
* Add Windows scripts to enable a Scheduled task for stubby
Expand Down
4 changes: 3 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AC_PREREQ([2.68])
AC_INIT([Stubby], [0.2.0], [[email protected]])
AC_INIT([Stubby], [0.2.1], [[email protected]])
AC_CANONICAL_TARGET
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([src/stubby.c])
Expand Down Expand Up @@ -54,6 +54,8 @@ AC_TRY_COMPILE([
AC_MSG_RESULT(no)
])

AC_CHECK_HEADERS([os/log.h])

build_on_windows="0"
build_on_macos="0"
build_on_unix="0"
Expand Down
73 changes: 47 additions & 26 deletions macos/stubby-ui-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* requires privileges.
*/

#include "config.h"
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
Expand All @@ -13,7 +14,9 @@
#include <unistd.h>

#include <Security/Authorization.h>
#ifdef HAVE_OS_LOG_H
#include <os/log.h>
#endif

static const char CP[] = "/bin/cp";
static const char LAUNCHCTL[] = "/bin/launchctl";
Expand All @@ -24,11 +27,25 @@ static const char DEFAULT_CONFIG_FILE[] = "/usr/local/etc/stubby/stubby.yml";
static const char RIGHT_DAEMON_RUN[] = "net.getdnsapi.stubby.daemon.run";
static const char RIGHT_DNS_LOCAL[] = "net.getdnsapi.stubby.dns.local";

static void log_failure(const char *log_message)
{
fprintf(stderr, "failed: %s\n", log_message);
#ifdef HAVE_OS_LOG_H
os_log(OS_LOG_DEFAULT, "failed: %s", log_message);
#endif
}

#ifdef HAVE_OS_LOG_H
static void log_action(const char *log_message)
{
os_log(OS_LOG_DEFAULT, "%s", log_message);
}
#endif

void check_auth(const char *auth, const char *right)
{
if (!auth) {
fprintf(stderr, "Authorization required.");
os_log(OS_LOG_DEFAULT, "Required authorization not supplied.");
log_failure("Authorization required.");
exit(1);
}

Expand All @@ -48,8 +65,7 @@ void check_auth(const char *auth, const char *right)
continue;
}
}
fprintf(stderr, "Invalid authorization key text.");
os_log(OS_LOG_DEFAULT, "Invalid authorization key test.");
log_failure("Invalid authorization key text.");
exit(1);
}

Expand All @@ -58,8 +74,7 @@ void check_auth(const char *auth, const char *right)

oss = AuthorizationCreateFromExternalForm(&auth_ext_form, &auth_ref);
if (oss != errAuthorizationSuccess) {
fprintf(stderr, "Bad authorization key form.");
os_log(OS_LOG_DEFAULT, "Authorization key is of wrong form.");
log_failure("Bad authorization key form.");
exit(1);
}

Expand All @@ -73,8 +88,7 @@ void check_auth(const char *auth, const char *right)
kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed,
NULL);
if (oss != errAuthorizationSuccess) {
fprintf(stderr, "Authorization declined.");
os_log(OS_LOG_DEFAULT, "Authorization declined.");
log_failure("Authorization declined.");
exit(1);
}

Expand All @@ -89,78 +103,85 @@ void usage()

void fail_with_errno(const char *op)
{
fprintf(stderr, "%s failed: %s.\n", op, strerror(errno));
os_log(OS_LOG_DEFAULT, "%s failed: %s.", op, strerror(errno));
log_failure(strerror(errno));
exit(1);
}

void start()
{
os_log(OS_LOG_DEFAULT, "Starting Stubby.");

#ifdef HAVE_OS_LOG_H
log_action("Starting Stubby.");
#endif
int err = execl(LAUNCHCTL, LAUNCHCTL, "load", "/Library/LaunchDaemons/org.getdns.stubby.plist", NULL);
if (err == -1)
fail_with_errno("start");
}

void stop()
{
os_log(OS_LOG_DEFAULT, "Stopping Stubby.");

#ifdef HAVE_OS_LOG_H
log_action("Stopping Stubby.");
#endif
int err = execl(LAUNCHCTL, LAUNCHCTL, "unload", "/Library/LaunchDaemons/org.getdns.stubby.plist", NULL);
if (err == -1)
fail_with_errno("stop");
}

void list()
{
os_log(OS_LOG_DEFAULT, "Checking Stubby.");

#ifdef HAVE_OS_LOG_H
log_action("Checking Stubby.");
#endif
int err = execl(LAUNCHCTL, LAUNCHCTL, "list", "org.getdns.stubby", NULL);
if (err == -1)
fail_with_errno("stop");
}

void dns_stubby()
{
os_log(OS_LOG_DEFAULT, "DNS resolving via Stubby.");

#ifdef HAVE_OS_LOG_H
log_action("DNS resolving via Stubby.");
#endif
int err = execl(STUBBY_SETDNS, STUBBY_SETDNS, NULL);
if (err == -1)
fail_with_errno("dns_stubby");
}

void dns_default()
{
os_log(OS_LOG_DEFAULT, "DNS resolving via defaults.");

#ifdef HAVE_OS_LOG_H
log_action("DNS resolving via defaults.");
#endif
int err = execl(STUBBY_SETDNS, STUBBY_SETDNS, "-r", NULL);
if (err == -1)
fail_with_errno("dns_default");
}

void dns_list()
{
os_log(OS_LOG_DEFAULT, "List DNS resolver.");

#ifdef HAVE_OS_LOG_H
log_action("List DNS resolver.");
#endif
int err = execl(STUBBY_SETDNS, STUBBY_SETDNS, "-l", NULL);
if (err == -1)
fail_with_errno("dns_list");
}

void check_config(const char *config_file)
{
os_log(OS_LOG_DEFAULT, "Check configuration.");

#ifdef HAVE_OS_LOG_H
log_action("Check configuration.");
#endif
int err = execl(STUBBY, STUBBY, "-C", config_file, "-i", NULL);
if (err == -1)
fail_with_errno("check_config");
}

void write_config(const char *config_file)
{
os_log(OS_LOG_DEFAULT, "Write configuration.");

#ifdef HAVE_OS_LOG_H
log_action("Write configuration.");
#endif
int err = execl(CP, CP, config_file, DEFAULT_CONFIG_FILE, NULL);
if (err == -1)
fail_with_errno("write_config");
Expand Down

0 comments on commit f0b3304

Please sign in to comment.