5
5
* requires privileges.
6
6
*/
7
7
8
+ #include "config.h"
8
9
#include <ctype.h>
9
10
#include <errno.h>
10
11
#include <stdio.h>
13
14
#include <unistd.h>
14
15
15
16
#include <Security/Authorization.h>
17
+ #ifdef HAVE_OS_LOG_H
16
18
#include <os/log.h>
19
+ #endif
17
20
18
21
static const char CP [] = "/bin/cp" ;
19
22
static const char LAUNCHCTL [] = "/bin/launchctl" ;
@@ -24,11 +27,25 @@ static const char DEFAULT_CONFIG_FILE[] = "/usr/local/etc/stubby/stubby.yml";
24
27
static const char RIGHT_DAEMON_RUN [] = "net.getdnsapi.stubby.daemon.run" ;
25
28
static const char RIGHT_DNS_LOCAL [] = "net.getdnsapi.stubby.dns.local" ;
26
29
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
+
27
45
void check_auth (const char * auth , const char * right )
28
46
{
29
47
if (!auth ) {
30
- fprintf (stderr , "Authorization required." );
31
- os_log (OS_LOG_DEFAULT , "Required authorization not supplied." );
48
+ log_failure ("Authorization required." );
32
49
exit (1 );
33
50
}
34
51
@@ -48,8 +65,7 @@ void check_auth(const char *auth, const char *right)
48
65
continue ;
49
66
}
50
67
}
51
- fprintf (stderr , "Invalid authorization key text." );
52
- os_log (OS_LOG_DEFAULT , "Invalid authorization key test." );
68
+ log_failure ("Invalid authorization key text." );
53
69
exit (1 );
54
70
}
55
71
@@ -58,8 +74,7 @@ void check_auth(const char *auth, const char *right)
58
74
59
75
oss = AuthorizationCreateFromExternalForm (& auth_ext_form , & auth_ref );
60
76
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." );
63
78
exit (1 );
64
79
}
65
80
@@ -73,8 +88,7 @@ void check_auth(const char *auth, const char *right)
73
88
kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed ,
74
89
NULL );
75
90
if (oss != errAuthorizationSuccess ) {
76
- fprintf (stderr , "Authorization declined." );
77
- os_log (OS_LOG_DEFAULT , "Authorization declined." );
91
+ log_failure ("Authorization declined." );
78
92
exit (1 );
79
93
}
80
94
@@ -89,78 +103,85 @@ void usage()
89
103
90
104
void fail_with_errno (const char * op )
91
105
{
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 ));
94
107
exit (1 );
95
108
}
96
109
97
110
void start ()
98
111
{
99
- os_log (OS_LOG_DEFAULT , "Starting Stubby." );
100
-
112
+ #ifdef HAVE_OS_LOG_H
113
+ log_action ("Starting Stubby." );
114
+ #endif
101
115
int err = execl (LAUNCHCTL , LAUNCHCTL , "load" , "/Library/LaunchDaemons/org.getdns.stubby.plist" , NULL );
102
116
if (err == -1 )
103
117
fail_with_errno ("start" );
104
118
}
105
119
106
120
void stop ()
107
121
{
108
- os_log (OS_LOG_DEFAULT , "Stopping Stubby." );
109
-
122
+ #ifdef HAVE_OS_LOG_H
123
+ log_action ("Stopping Stubby." );
124
+ #endif
110
125
int err = execl (LAUNCHCTL , LAUNCHCTL , "unload" , "/Library/LaunchDaemons/org.getdns.stubby.plist" , NULL );
111
126
if (err == -1 )
112
127
fail_with_errno ("stop" );
113
128
}
114
129
115
130
void list ()
116
131
{
117
- os_log (OS_LOG_DEFAULT , "Checking Stubby." );
118
-
132
+ #ifdef HAVE_OS_LOG_H
133
+ log_action ("Checking Stubby." );
134
+ #endif
119
135
int err = execl (LAUNCHCTL , LAUNCHCTL , "list" , "org.getdns.stubby" , NULL );
120
136
if (err == -1 )
121
137
fail_with_errno ("stop" );
122
138
}
123
139
124
140
void dns_stubby ()
125
141
{
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
128
145
int err = execl (STUBBY_SETDNS , STUBBY_SETDNS , NULL );
129
146
if (err == -1 )
130
147
fail_with_errno ("dns_stubby" );
131
148
}
132
149
133
150
void dns_default ()
134
151
{
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
137
155
int err = execl (STUBBY_SETDNS , STUBBY_SETDNS , "-r" , NULL );
138
156
if (err == -1 )
139
157
fail_with_errno ("dns_default" );
140
158
}
141
159
142
160
void dns_list ()
143
161
{
144
- os_log (OS_LOG_DEFAULT , "List DNS resolver." );
145
-
162
+ #ifdef HAVE_OS_LOG_H
163
+ log_action ("List DNS resolver." );
164
+ #endif
146
165
int err = execl (STUBBY_SETDNS , STUBBY_SETDNS , "-l" , NULL );
147
166
if (err == -1 )
148
167
fail_with_errno ("dns_list" );
149
168
}
150
169
151
170
void check_config (const char * config_file )
152
171
{
153
- os_log (OS_LOG_DEFAULT , "Check configuration." );
154
-
172
+ #ifdef HAVE_OS_LOG_H
173
+ log_action ("Check configuration." );
174
+ #endif
155
175
int err = execl (STUBBY , STUBBY , "-C" , config_file , "-i" , NULL );
156
176
if (err == -1 )
157
177
fail_with_errno ("check_config" );
158
178
}
159
179
160
180
void write_config (const char * config_file )
161
181
{
162
- os_log (OS_LOG_DEFAULT , "Write configuration." );
163
-
182
+ #ifdef HAVE_OS_LOG_H
183
+ log_action ("Write configuration." );
184
+ #endif
164
185
int err = execl (CP , CP , config_file , DEFAULT_CONFIG_FILE , NULL );
165
186
if (err == -1 )
166
187
fail_with_errno ("write_config" );
0 commit comments