static int blob_content_to_file( git_blob *blob, const char *path, mode_t entry_filemode, git_checkout_opts *opts) { int error = -1, nb_filters = 0; mode_t file_mode = opts->file_mode; bool dont_free_filtered = false; git_buf unfiltered = GIT_BUF_INIT, filtered = GIT_BUF_INIT; git_vector filters = GIT_VECTOR_INIT; if (opts->disable_filters || (nb_filters = git_filters_load( &filters, git_object_owner((git_object *)blob), path, GIT_FILTER_TO_WORKTREE)) == 0) { /* Create a fake git_buf from the blob raw data... */ filtered.ptr = blob->odb_object->raw.data; filtered.size = blob->odb_object->raw.len; /* ... and make sure it doesn't get unexpectedly freed */ dont_free_filtered = true; } if (nb_filters < 0) return nb_filters; if (nb_filters > 0) { if ((error = git_blob__getbuf(&unfiltered, blob)) < 0) goto cleanup; if ((error = git_filters_apply(&filtered, &unfiltered, &filters)) < 0) goto cleanup; } /* Allow overriding of file mode */ if (!file_mode) file_mode = entry_filemode; error = buffer_to_file(&filtered, path, opts->dir_mode, opts->file_open_flags, file_mode); cleanup: git_filters_free(&filters); git_buf_free(&unfiltered); if (!dont_free_filtered) git_buf_free(&filtered); return error; }
static zip_int64_t source_hole_cb(void *ud, void *data, zip_uint64_t length, zip_source_cmd_t command) { hole_t *ctx = (hole_t *)ud; switch (command) { case ZIP_SOURCE_BEGIN_WRITE: ctx->out = buffer_new(); return 0; case ZIP_SOURCE_CLOSE: return 0; case ZIP_SOURCE_COMMIT_WRITE: if (buffer_to_file(ctx->out, ctx->fname, &ctx->error) < 0) { return -1; } buffer_free(ctx->in); ctx->in = ctx->out; ctx->out = NULL; return 0; case ZIP_SOURCE_ERROR: return zip_error_to_data(&ctx->error, data, length); case ZIP_SOURCE_FREE: hole_free(ctx); return 0; case ZIP_SOURCE_OPEN: ctx->in->offset = 0; return 0; case ZIP_SOURCE_READ: return buffer_read(ctx->in, data, length, &ctx->error); case ZIP_SOURCE_REMOVE: buffer_free(ctx->in); ctx->in = buffer_new(); buffer_free(ctx->out); ctx->out = NULL; (void)remove(ctx->fname); return 0; case ZIP_SOURCE_ROLLBACK_WRITE: buffer_free(ctx->out); ctx->out = NULL; return 0; case ZIP_SOURCE_SEEK: return buffer_seek(ctx->in, data, length, &ctx->error); case ZIP_SOURCE_SEEK_WRITE: return buffer_seek(ctx->out, data, length, &ctx->error); case ZIP_SOURCE_STAT: { zip_stat_t *st = ZIP_SOURCE_GET_ARGS(zip_stat_t, data, length, &ctx->error); if (st == NULL) { return -1; } /* TODO: return ENOENT if fname doesn't exist */ st->valid |= ZIP_STAT_SIZE; st->size = ctx->in->size; return 0; } case ZIP_SOURCE_TELL: return (zip_int64_t)ctx->in->offset; case ZIP_SOURCE_TELL_WRITE: return (zip_int64_t)ctx->out->offset; case ZIP_SOURCE_WRITE: return buffer_write(ctx->out, data, length, &ctx->error); case ZIP_SOURCE_SUPPORTS: return zip_source_make_command_bitmap(ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_CLOSE, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_REMOVE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_STAT, ZIP_SOURCE_TELL, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_WRITE, -1); default: zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); return -1; } }
DWORD request_core_loadlib(Remote *remote, Packet *packet) { Packet *response = packet_create_response(packet); DWORD res = ERROR_SUCCESS; HMODULE library; PCHAR libraryPath; DWORD flags = 0; BOOL bLibLoadedReflectivly = FALSE; Command *first = extensionCommands; Command *command; do { libraryPath = packet_get_tlv_value_string(packet, TLV_TYPE_LIBRARY_PATH); flags = packet_get_tlv_value_uint(packet, TLV_TYPE_FLAGS); // Invalid library path? if (!libraryPath) { res = ERROR_INVALID_PARAMETER; break; } // If the lib does not exist locally, but is being uploaded... if (!(flags & LOAD_LIBRARY_FLAG_LOCAL)) { PCHAR targetPath; Tlv dataTlv; // Get the library's file contents if ((packet_get_tlv(packet, TLV_TYPE_DATA, &dataTlv) != ERROR_SUCCESS) || (!(targetPath = packet_get_tlv_value_string(packet, TLV_TYPE_TARGET_PATH)))) { res = ERROR_INVALID_PARAMETER; break; } // If the library is not to be stored on disk, if (!(flags & LOAD_LIBRARY_FLAG_ON_DISK)) { // try to load the library via its reflective loader... library = LoadLibraryR(dataTlv.buffer, dataTlv.header.length); if (library == NULL) { // if that fails, presumably besause the library doesn't support // reflective injection, we default to using libloader... library = libloader_load_library(targetPath, dataTlv.buffer, dataTlv.header.length); } else { bLibLoadedReflectivly = TRUE; } res = (library) ? ERROR_SUCCESS : ERROR_NOT_FOUND; } else { // Otherwise, save the library buffer to disk res = buffer_to_file(targetPath, dataTlv.buffer, dataTlv.header.length); } // Override the library path libraryPath = targetPath; } // If a previous operation failed, break out. if (res != ERROR_SUCCESS) break; // Load the library if ((!library) && (!(library = LoadLibrary(libraryPath)))) res = GetLastError(); else res = ERROR_SUCCESS; // If this library is supposed to be an extension library, try to // call its Init routine if ((flags & LOAD_LIBRARY_FLAG_EXTENSION) && (library)) { EXTENSION * extension = (EXTENSION *)malloc(sizeof(EXTENSION)); if (extension) { extension->library = library; // if the library was loaded via its reflective loader we must use GetProcAddressR() if (bLibLoadedReflectivly) { extension->init = (PSRVINIT)GetProcAddressR(extension->library, "InitServerExtension"); extension->deinit = (PSRVDEINIT)GetProcAddressR(extension->library, "DeinitServerExtension"); } else { extension->init = (PSRVINIT)GetProcAddress(extension->library, "InitServerExtension"); extension->deinit = (PSRVDEINIT)GetProcAddress(extension->library, "DeinitServerExtension"); } // patch in the metsrv.dll's HMODULE handle, used by the server extensions for delay loading // functions from the metsrv.dll library. We need to do it this way as LoadLibrary/GetProcAddress // wont work if we have used Reflective DLL Injection as metsrv.dll will be 'invisible' to these functions. remote->hMetSrv = hAppInstance; // Call the init routine in the library if (extension->init) { dprintf("[SERVER] Calling init()..."); res = extension->init(remote); if (res == ERROR_SUCCESS) { list_push(extension_list, extension); } else { free(extension); } } dprintf("[SERVER] Called init()..."); if (response) { for (command = extensionCommands; command != first; command = command->next) { packet_add_tlv_string(response, TLV_TYPE_METHOD, command->method); } } } } } while (0); if (response) { packet_add_tlv_uint(response, TLV_TYPE_RESULT, res); packet_transmit(remote, response, NULL); } return res; }
/* * core_loadlib * ------------ * * Load a library into the address space of the executing process. * * TLVs: * * req: TLV_TYPE_LIBRARY_PATH -- The path of the library to load. * req: TLV_TYPE_FLAGS -- Library loading flags. * opt: TLV_TYPE_TARGET_PATH -- The contents of the library if uploading. * opt: TLV_TYPE_DATA -- The contents of the library if uploading. * * TODO: * * - Implement in-memory library loading */ DWORD request_core_loadlib(Remote *pRemote, Packet *pPacket) { Packet *response = packet_create_response(pPacket); DWORD res = ERROR_SUCCESS; HMODULE library; PCHAR libraryPath; DWORD flags = 0; BOOL bLibLoadedReflectivly = FALSE; Command *first = extensionCommands; do { libraryPath = packet_get_tlv_value_string(pPacket, TLV_TYPE_LIBRARY_PATH); flags = packet_get_tlv_value_uint(pPacket, TLV_TYPE_FLAGS); // Invalid library path? if (!libraryPath) { res = ERROR_INVALID_PARAMETER; break; } // If the lib does not exist locally, but is being uploaded... if (!(flags & LOAD_LIBRARY_FLAG_LOCAL)) { PCHAR targetPath; Tlv dataTlv; // Get the library's file contents if ((packet_get_tlv(pPacket, TLV_TYPE_DATA, &dataTlv) != ERROR_SUCCESS) || (!(targetPath = packet_get_tlv_value_string(pPacket, TLV_TYPE_TARGET_PATH)))) { res = ERROR_INVALID_PARAMETER; break; } // If the library is not to be stored on disk, if (!(flags & LOAD_LIBRARY_FLAG_ON_DISK)) { // try to load the library via its reflective loader... library = LoadLibraryR(dataTlv.buffer, dataTlv.header.length); if (library == NULL) { // if that fails, presumably besause the library doesn't support // reflective injection, we default to using libloader... library = libloader_load_library(targetPath, dataTlv.buffer, dataTlv.header.length); } else { bLibLoadedReflectivly = TRUE; } res = (library) ? ERROR_SUCCESS : ERROR_NOT_FOUND; } else { // Otherwise, save the library buffer to disk res = buffer_to_file(targetPath, dataTlv.buffer, dataTlv.header.length); } // Override the library path libraryPath = targetPath; } // If a previous operation failed, break out. if (res != ERROR_SUCCESS) { break; } // Load the library if (!library && !(library = LoadLibraryA(libraryPath))) { res = GetLastError(); } // If this library is supposed to be an extension library, try to // call its Init routine if ((flags & LOAD_LIBRARY_FLAG_EXTENSION) && library) { res = load_extension(library, bLibLoadedReflectivly, pRemote, response, first); } } while (0); if (response) { packet_transmit_response(res, pRemote, response); } return res; }
DWORD request_core_loadlib(Remote *remote, Packet *packet) { Packet *response = packet_create_response(packet); DWORD res = ERROR_SUCCESS; HMODULE library; PCHAR libraryPath; DWORD flags = 0; PCHAR targetPath; int local_error = 0; do { libraryPath = packet_get_tlv_value_string(packet, TLV_TYPE_LIBRARY_PATH); flags = packet_get_tlv_value_uint(packet, TLV_TYPE_FLAGS); // Invalid library path? if (!libraryPath) { res = ERROR_INVALID_PARAMETER; break; } // If the lib does not exist locally, but is being uploaded... if (!(flags & LOAD_LIBRARY_FLAG_LOCAL)) { Tlv dataTlv; // Get the library's file contents if ((packet_get_tlv(packet, TLV_TYPE_DATA, &dataTlv) != ERROR_SUCCESS) || (!(targetPath = packet_get_tlv_value_string(packet, TLV_TYPE_TARGET_PATH)))) { res = ERROR_INVALID_PARAMETER; break; } // If the library is not to be stored on disk, if (!(flags & LOAD_LIBRARY_FLAG_ON_DISK)) { library = _dlopenbuf(NULL, dataTlv.buffer, dataTlv.header.length ); res = (library) ? ERROR_SUCCESS : ERROR_NOT_FOUND; //Taken from buffer_to_file (should be changed to random) targetPath = "/tmp/foo"; } else { // Otherwise, save the library buffer to disk res = buffer_to_file(targetPath, dataTlv.buffer, dataTlv.header.length); } // Override the library path libraryPath = targetPath; } // If a previous operation failed, break out. if (res != ERROR_SUCCESS) break; // Load the library if ((!library) && (library = dlopen(targetPath, RTLD_GLOBAL|RTLD_LAZY)) == NULL) res = GetLastError(); else res = ERROR_SUCCESS; // If this library is supposed to be an extension library, try to // call its Init routine if ((flags & LOAD_LIBRARY_FLAG_EXTENSION) && (library)){ DWORD (*init)(Remote *remote); init = dlsym(library, "InitServerExtension" ); // Call the init routine in the library if( init ) res = init(remote); } } while (0); if (response) { packet_add_tlv_uint(response, TLV_TYPE_RESULT, res); packet_transmit(remote, response, NULL); } return (res); }