55 * requires privileges.
66 */
77
8+ #include "config.h"
89#include <ctype.h>
910#include <errno.h>
1011#include <stdio.h>
1314#include <unistd.h>
1415
1516#include <Security/Authorization.h>
17+ #ifdef HAVE_OS_LOG_H
1618#include <os/log.h>
19+ #endif
1720
1821static const char CP [] = "/bin/cp" ;
1922static const char LAUNCHCTL [] = "/bin/launchctl" ;
@@ -24,11 +27,25 @@ static const char DEFAULT_CONFIG_FILE[] = "/usr/local/etc/stubby/stubby.yml";
2427static const char RIGHT_DAEMON_RUN [] = "net.getdnsapi.stubby.daemon.run" ;
2528static const char RIGHT_DNS_LOCAL [] = "net.getdnsapi.stubby.dns.local" ;
2629
30+ static void log_failure (const char * log_message )
31+ {
32+ fprintf (stderr , "failed: %s\n" , log_message );
33+ #ifdef HAVE_OS_LOG_H
34+ os_log (OS_LOG_DEFAULT , "failed: %s" , log_message );
35+ #endif
36+ }
37+
38+ #ifdef HAVE_OS_LOG_H
39+ static void log_action (const char * log_message )
40+ {
41+ os_log (OS_LOG_DEFAULT , "%s" , log_message );
42+ }
43+ #endif
44+
2745void check_auth (const char * auth , const char * right )
2846{
2947 if (!auth ) {
30- fprintf (stderr , "Authorization required." );
31- os_log (OS_LOG_DEFAULT , "Required authorization not supplied." );
48+ log_failure ("Authorization required." );
3249 exit (1 );
3350 }
3451
@@ -48,8 +65,7 @@ void check_auth(const char *auth, const char *right)
4865 continue ;
4966 }
5067 }
51- fprintf (stderr , "Invalid authorization key text." );
52- os_log (OS_LOG_DEFAULT , "Invalid authorization key test." );
68+ log_failure ("Invalid authorization key text." );
5369 exit (1 );
5470 }
5571
@@ -58,8 +74,7 @@ void check_auth(const char *auth, const char *right)
5874
5975 oss = AuthorizationCreateFromExternalForm (& auth_ext_form , & auth_ref );
6076 if (oss != errAuthorizationSuccess ) {
61- fprintf (stderr , "Bad authorization key form." );
62- os_log (OS_LOG_DEFAULT , "Authorization key is of wrong form." );
77+ log_failure ("Bad authorization key form." );
6378 exit (1 );
6479 }
6580
@@ -73,8 +88,7 @@ void check_auth(const char *auth, const char *right)
7388 kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed ,
7489 NULL );
7590 if (oss != errAuthorizationSuccess ) {
76- fprintf (stderr , "Authorization declined." );
77- os_log (OS_LOG_DEFAULT , "Authorization declined." );
91+ log_failure ("Authorization declined." );
7892 exit (1 );
7993 }
8094
@@ -89,78 +103,85 @@ void usage()
89103
90104void fail_with_errno (const char * op )
91105{
92- fprintf (stderr , "%s failed: %s.\n" , op , strerror (errno ));
93- os_log (OS_LOG_DEFAULT , "%s failed: %s." , op , strerror (errno ));
106+ log_failure (strerror (errno ));
94107 exit (1 );
95108}
96109
97110void start ()
98111{
99- os_log (OS_LOG_DEFAULT , "Starting Stubby." );
100-
112+ #ifdef HAVE_OS_LOG_H
113+ log_action ("Starting Stubby." );
114+ #endif
101115 int err = execl (LAUNCHCTL , LAUNCHCTL , "load" , "/Library/LaunchDaemons/org.getdns.stubby.plist" , NULL );
102116 if (err == -1 )
103117 fail_with_errno ("start" );
104118}
105119
106120void stop ()
107121{
108- os_log (OS_LOG_DEFAULT , "Stopping Stubby." );
109-
122+ #ifdef HAVE_OS_LOG_H
123+ log_action ("Stopping Stubby." );
124+ #endif
110125 int err = execl (LAUNCHCTL , LAUNCHCTL , "unload" , "/Library/LaunchDaemons/org.getdns.stubby.plist" , NULL );
111126 if (err == -1 )
112127 fail_with_errno ("stop" );
113128}
114129
115130void list ()
116131{
117- os_log (OS_LOG_DEFAULT , "Checking Stubby." );
118-
132+ #ifdef HAVE_OS_LOG_H
133+ log_action ("Checking Stubby." );
134+ #endif
119135 int err = execl (LAUNCHCTL , LAUNCHCTL , "list" , "org.getdns.stubby" , NULL );
120136 if (err == -1 )
121137 fail_with_errno ("stop" );
122138}
123139
124140void dns_stubby ()
125141{
126- os_log (OS_LOG_DEFAULT , "DNS resolving via Stubby." );
127-
142+ #ifdef HAVE_OS_LOG_H
143+ log_action ("DNS resolving via Stubby." );
144+ #endif
128145 int err = execl (STUBBY_SETDNS , STUBBY_SETDNS , NULL );
129146 if (err == -1 )
130147 fail_with_errno ("dns_stubby" );
131148}
132149
133150void dns_default ()
134151{
135- os_log (OS_LOG_DEFAULT , "DNS resolving via defaults." );
136-
152+ #ifdef HAVE_OS_LOG_H
153+ log_action ("DNS resolving via defaults." );
154+ #endif
137155 int err = execl (STUBBY_SETDNS , STUBBY_SETDNS , "-r" , NULL );
138156 if (err == -1 )
139157 fail_with_errno ("dns_default" );
140158}
141159
142160void dns_list ()
143161{
144- os_log (OS_LOG_DEFAULT , "List DNS resolver." );
145-
162+ #ifdef HAVE_OS_LOG_H
163+ log_action ("List DNS resolver." );
164+ #endif
146165 int err = execl (STUBBY_SETDNS , STUBBY_SETDNS , "-l" , NULL );
147166 if (err == -1 )
148167 fail_with_errno ("dns_list" );
149168}
150169
151170void check_config (const char * config_file )
152171{
153- os_log (OS_LOG_DEFAULT , "Check configuration." );
154-
172+ #ifdef HAVE_OS_LOG_H
173+ log_action ("Check configuration." );
174+ #endif
155175 int err = execl (STUBBY , STUBBY , "-C" , config_file , "-i" , NULL );
156176 if (err == -1 )
157177 fail_with_errno ("check_config" );
158178}
159179
160180void write_config (const char * config_file )
161181{
162- os_log (OS_LOG_DEFAULT , "Write configuration." );
163-
182+ #ifdef HAVE_OS_LOG_H
183+ log_action ("Write configuration." );
184+ #endif
164185 int err = execl (CP , CP , config_file , DEFAULT_CONFIG_FILE , NULL );
165186 if (err == -1 )
166187 fail_with_errno ("write_config" );
0 commit comments