diff --git a/Source/santasyncservice/BUILD b/Source/santasyncservice/BUILD index 7f9f9b28..cc30b137 100644 --- a/Source/santasyncservice/BUILD +++ b/Source/santasyncservice/BUILD @@ -41,11 +41,11 @@ objc_library( }), ) -# This setting is used to enable storing all event uploads. See the -# -storeEventUploads method in SNTSyncEventUpload.mm for more details. +# This setting is used to enable storing all sync requests as JSON files on disk. +# This can be useful for debugging purposes. config_setting( - name = "store_event_uploads", - values = {"define": "SANTA_STORE_EVENTUPLOADS=1"}, + name = "store_json_uploads", + values = {"define": "SANTA_STORE_SYNC_JSON=1"}, ) objc_library( @@ -78,7 +78,7 @@ objc_library( ], hdrs = ["SNTSyncManager.h"], defines = select({ - ":store_event_uploads": ["SANTA_STORE_EVENTUPLOADS"], + ":store_json_uploads": ["SANTA_STORE_SYNC_JSON"], "//conditions:default": [], }), sdk_dylibs = ["libz"], diff --git a/Source/santasyncservice/SNTSyncEventUpload.mm b/Source/santasyncservice/SNTSyncEventUpload.mm index e014a761..38f676cb 100644 --- a/Source/santasyncservice/SNTSyncEventUpload.mm +++ b/Source/santasyncservice/SNTSyncEventUpload.mm @@ -72,11 +72,9 @@ - (BOOL)uploadEvents:(NSArray *)events { if (self.syncState.syncType == SNTSyncTypeNormal || [[SNTConfigurator configurator] enableCleanSyncEventUpload]) { ::pbv1::EventUploadResponse response; - NSURLRequest *urlReq = [self requestWithMessage:req]; -#if defined(SANTA_STORE_EVENTUPLOADS) && defined(DEBUG) - [self storeEventUploads:urlReq]; -#endif - NSError *err = [self performRequest:urlReq intoMessage:&response timeout:30]; + NSError *err = [self performRequest:[self requestWithMessage:req] + intoMessage:&response + timeout:30]; if (err) { SLOGE(@"Failed to upload events: %@", err); return NO; @@ -214,20 +212,4 @@ - (BOOL)uploadEvents:(NSArray *)events { return *e; } -#if defined(SANTA_STORE_EVENTUPLOADS) && defined(DEBUG) -// If Santa is built with both SANTA_STORE_EVENTUPLOADS and DEBUG defined, -// all event upload requests will be stored, in JSON form, to /var/db/santa/eventuploads. -// As santasyncservice runs as nobody, this directory must already exist and be owned by -// the nobody user. -- (void)storeEventUploads:(NSURLRequest *)request { - NSError *err; - NSString *path = [NSString - stringWithFormat:@"/var/db/santa/eventuploads/%f.json", [NSDate date].timeIntervalSince1970]; - [request.HTTPBody writeToURL:[NSURL fileURLWithPath:path] options:NSDataWritingAtomic error:&err]; - if (err) { - LOGE(@"Failed to write eventupload to file: %@", err); - } -} -#endif - @end diff --git a/Source/santasyncservice/SNTSyncStage.mm b/Source/santasyncservice/SNTSyncStage.mm index 48d89397..9d03e69c 100644 --- a/Source/santasyncservice/SNTSyncStage.mm +++ b/Source/santasyncservice/SNTSyncStage.mm @@ -65,7 +65,7 @@ - (NSString *)stageURL { - (NSMutableURLRequest *)requestWithMessage:(google::protobuf::Message *)message { if (!message) return [self requestWithData:[NSData data] contentType:nil]; -#ifndef SANTA_STORE_EVENTUPLOADS +#ifndef SANTA_STORE_SYNC_JSON if ([[SNTConfigurator configurator] syncEnableProtoTransfer]) { std::string data; if (!message->SerializeToString(&data)) { @@ -76,7 +76,7 @@ - (NSMutableURLRequest *)requestWithMessage:(google::protobuf::Message *)message return [self requestWithData:[NSData dataWithBytes:data.data() length:data.size()] contentType:@"application/x-protobuf"]; } -#endif +#endif // SANTA_STORE_SYNC_JSON google::protobuf::json::PrintOptions options{}; std::string json; @@ -89,8 +89,13 @@ - (NSMutableURLRequest *)requestWithMessage:(google::protobuf::Message *)message return nil; } - return [self requestWithData:[NSData dataWithBytes:json.data() length:json.size()] - contentType:@"application/json"]; + NSData *data = [NSData dataWithBytes:json.data() length:json.size()]; + +#ifdef SANTA_STORE_SYNC_JSON + [self storeJSON:data withMessageType:santa::StringToNSString(message->GetTypeName())]; +#endif + + return [self requestWithData:data contentType:@"application/json"]; } - (NSMutableURLRequest *)requestWithData:(NSData *)requestBody contentType:(NSString *)contentType { @@ -102,7 +107,6 @@ - (NSMutableURLRequest *)requestWithData:(NSData *)requestBody contentType:(NSSt NSString *xsrfHeader = self.syncState.xsrfTokenHeader ?: kDefaultXSRFTokenHeader; [req setValue:self.syncState.xsrfToken forHTTPHeaderField:xsrfHeader]; -#ifndef SANTA_STORE_EVENTUPLOADS NSData *compressed; NSString *contentEncodingHeader; @@ -125,7 +129,6 @@ - (NSMutableURLRequest *)requestWithData:(NSData *)requestBody contentType:(NSSt requestBody = compressed; [req setValue:contentEncodingHeader forHTTPHeaderField:@"Content-Encoding"]; } -#endif [self addExtraRequestHeaders:req]; @@ -232,7 +235,7 @@ - (NSError *)performRequest:(NSURLRequest *)request return nil; } -#ifndef SANTA_STORE_EVENTUPLOADS +#ifndef SANTA_STORE_SYNC_JSON if ([[SNTConfigurator configurator] syncEnableProtoTransfer]) { if (!message->ParseFromString(std::string((const char *)data.bytes, data.length))) { NSString *errStr = @"Failed to parse response proto into message"; @@ -243,7 +246,7 @@ - (NSError *)performRequest:(NSURLRequest *)request } return nil; } -#endif +#endif // SANTA_STORE_SYNC_JSON google::protobuf::json::ParseOptions options{ .ignore_unknown_fields = true, @@ -342,4 +345,20 @@ - (BOOL)fetchXSRFToken { return success; } +#if defined(SANTA_STORE_SYNC_JSON) && defined(DEBUG) +// If Santa is built with both SANTA_STORE_SYNC_JSON and DEBUG defined, +// all sync requests will be stored, in JSON form, to /var/db/santa/storedsyncs. +// As santasyncservice runs as nobody, this directory must already exist and be +// owned by the nobody user. +- (void)storeJSON:(NSData *)json withMessageType:(NSString *)msgType { + NSError *err; + NSString *path = [NSString stringWithFormat:@"/var/db/santa/storedsyncs/%@.%f.json", msgType, + [NSDate date].timeIntervalSince1970]; + [json writeToURL:[NSURL fileURLWithPath:path] options:NSDataWritingAtomic error:&err]; + if (err) { + LOGE(@"Failed to write %@ to file: %@", msgType, err); + } +} +#endif + @end