]> git.saurik.com Git - apple/security.git/blob - OSX/sec/ProjectHeaders/Security/Tool/log_control.c
Security-57336.1.9.tar.gz
[apple/security.git] / OSX / sec / ProjectHeaders / Security / Tool / log_control.c
1 //
2 // log_control.c
3 //
4 // sec
5 //
6
7 #include <string.h>
8 #include <getopt.h>
9 #include <stdlib.h>
10
11 #include <Security/SecItem.h>
12 #include <CoreFoundation/CoreFoundation.h>
13
14 #include <SecurityTool/tool_errors.h>
15
16 #include <Security/SecLogging.h>
17
18 #include <utilities/debugging.h>
19
20 #include <utilities/SecCFWrappers.h>
21
22 #include "SecurityCommands.h"
23
24
25 static void
26 set_log_settings(const char * settings)
27 {
28 CFErrorRef error = NULL;
29
30 CFStringRef scope = CFStringCreateWithCString(kCFAllocatorDefault, settings, kCFStringEncodingUTF8);
31
32 if (!SecSetLoggingInfoForXPCScope((CFPropertyListRef) scope, &error)) {
33 fprintf(stderr, "Failed: ");
34 CFShow(error);
35 }
36
37 CFReleaseSafe(scope);
38 CFReleaseSafe(error);
39 }
40
41 static void
42 set_circle_settings(const char * settings)
43 {
44 CFErrorRef error = NULL;
45
46 CFStringRef scope = CFStringCreateWithCString(kCFAllocatorDefault, settings, kCFStringEncodingUTF8);
47
48 if (!SecSetLoggingInfoForCircleScope((CFPropertyListRef) scope, &error)) {
49 fprintf(stderr, "Failed: ");
50 CFShow(error);
51 }
52
53 CFReleaseSafe(scope);
54 CFReleaseSafe(error);
55 }
56
57 static const char * getScopeIDName(int id)
58 {
59 switch (id) {
60 case kScopeIDXPC: return "XPC";
61 case kScopeIDDefaults: return "Defaults";
62 case kScopeIDEnvironment: return "Environment Variables";
63 case kScopeIDConfig: return "Config";
64 case kScopeIDCircle: return "Circle";
65 default: return "Unknown";
66 }
67 };
68
69 static const char * getPriorityName(CFNumberRef id_number)
70 {
71 int priority = -1;
72
73 CFNumberGetValue(id_number, kCFNumberIntType, &priority);
74
75 switch (priority) {
76 case ASL_LEVEL_EMERG: return ASL_STRING_EMERG;
77 case ASL_LEVEL_ALERT: return ASL_STRING_ALERT;
78 case ASL_LEVEL_CRIT: return ASL_STRING_CRIT;
79 case ASL_LEVEL_ERR: return ASL_STRING_ERR;
80 case ASL_LEVEL_WARNING: return ASL_STRING_WARNING;
81 case ASL_LEVEL_NOTICE: return ASL_STRING_NOTICE;
82 case ASL_LEVEL_INFO: return ASL_STRING_INFO;
83 case ASL_LEVEL_DEBUG: return ASL_STRING_DEBUG;
84 default: return "Unknown";
85
86 }
87 };
88
89 static void print_comma_separated(FILE* file, CFArrayRef array)
90 {
91 fprintf(file, "[");
92 __block const char *separator = "";
93 CFArrayForEach(array, ^(const void *value) {
94 cffprint(file, CFSTR("%s%@"), separator, value);
95 separator = ", ";
96 });
97 fprintf(file, "]");
98
99 }
100
101 static void
102 list_log_settings()
103 {
104 CFErrorRef error = NULL;
105
106 CFArrayRef result = SecGetCurrentServerLoggingInfo(&error);
107 if (result) {
108 __block int index = 0;
109 CFArrayForEach(result, ^(const void *value) {
110 printf("%s: ", getScopeIDName(index));
111
112 if (isArray(value)) {
113 print_comma_separated(stdout, (CFArrayRef) value);
114 printf("\n");
115 } else if (isDictionary(value)) {
116 printf("\n");
117 CFDictionaryForEach((CFDictionaryRef) value, ^(const void *level, const void *array) {
118 printf(" %s: ", getPriorityName(level));
119 if (isArray(array)) {
120 print_comma_separated(stdout, (CFArrayRef) array);
121 } else {
122 cffprint(stdout, CFSTR("%@"), array);
123 }
124 printf("\n");
125 });
126 } else {
127 cffprint(stdout, CFSTR("%@\n"), value);
128 }
129
130 ++index;
131 });
132 } else {
133 fprintf(stderr, "Failed: ");
134 CFShow(error);
135 }
136
137 CFReleaseSafe(error);
138 }
139
140 int log_control(int argc, char * const *argv)
141 {
142 int ch, result = 2; /* @@@ Return 2 triggers usage message. */
143
144 bool list = false;
145 /*
146 "-l - list"
147 */
148 while ((ch = getopt(argc, argv, "ls:c:")) != -1)
149 {
150 switch (ch)
151 {
152 case 'l':
153 list = true;
154 break;
155 case 's':
156 set_log_settings(optarg);
157 break;
158 case 'c':
159 set_circle_settings(optarg);
160 break;
161 case '?':
162 default:
163 goto fail;
164 }
165 }
166
167 argc -= optind;
168 argv += optind;
169
170 if (argc > 1)
171 goto fail;
172
173 if (argc == 1) {
174 set_log_settings(argv[0]);
175
176 argc -= 1;
177 argv += 1;
178 }
179
180 (void) argv;
181
182 if (argc != 0)
183 goto fail;
184
185 if (list)
186 list_log_settings();
187
188 result = 0;
189
190 fail:
191 return result;
192 }