Esempio n. 1
0
void protocol_download()
{
	UpdateResp update = { 0 };
	uint8 nIndex;
	uint16 nOffset;

	power_device(ED_GPRS, EP_ON);
	post_trace("gprs power on");
	if(!gprs_dial(APN_USER, APN_PASS)) {
		post_trace("gprs dial error");
		power_device(ED_GPRS, EP_OFF);
		
		return;
	}
	if(!gprs_connect(SERVER_ADDR, SERVER_PORT)) {
		post_trace("gprs connect error");
		power_device(ED_GPRS, EP_OFF);
		
		return;
	}
	if(!request_update(&update)) {
		gprs_disconnect();
		power_device(ED_GPRS, EP_OFF);
		
		return;
	}
	
	complete_communication();
	
	for(nIndex = 0; nIndex < ECT_COUNT; ++nIndex) {
		if(0 == update.update[nIndex] || EV_REMOVE == update.update[nIndex]) {
			continue;
		}
		if(ECT_HEALTH == nIndex) {
			uchar szBuf[SERVER_DATA_SIZE] = { 0 };
			char szPath[max_path] = { 0 };
			uint8 nLen;
			
			sprintf(szPath, "%s0", HEALTH_PATH);
			for(nOffset = 0; nOffset < update.update[nIndex]; ++nOffset) {
				nLen = SERVER_DATA_SIZE;
				if(!download_health(nOffset, szBuf, &nLen)) {
					break;
				}
				if(!write_file_ex(szPath, szBuf, nLen, nOffset * SERVER_DATA_SIZE)) {
					break;
				}
			}
			complete_communication();
		} else if(ECT_CONFIG == nIndex) {
			Config config = { 0 };
			
			if(!download_config(0, &config)) {
				continue;
			}
		   	save_server_config(&config);
			complete_communication();
		} else if(ECT_RELATION == nIndex) {
			Relation relation = { 0 };
			char szPath[max_path] = { 0 };
			uint8 nFile = 0;
			
			if(EV_REMOVE == update.update[nIndex]) {
				// remove relation
				continue;
			}
			for(nOffset = 0; nOffset < update.update[nIndex]; ++nOffset) {
				if(!download_relation(nOffset, &relation)) {
					continue;
				}
				sprintf(szPath, "%s%d", RELATION_PATH, nFile++);
				if(!write_file(szPath, &relation, sizeof(Relation))) {
					--nFile;
				}
			}
			complete_communication();
		} else if(ECT_REMIND == nIndex) {
			Remind remind = { 0 };
			char szPath[max_path] = { 0 };
			uint8 nFile = 0;
			
			if(EV_REMOVE == update.update[nIndex]) {
				// remove remind
				continue;
			}
			for(nOffset = 0; nOffset < update.update[nIndex]; ++nOffset) {
				if(!download_remind(nOffset, &remind)) {
					continue;
				}
				sprintf(szPath, "%s%d", RELATION_PATH, nFile++);
				if(!write_file(szPath, &remind, sizeof(Remind))) {
					--nFile;
				}
			}
			complete_communication();
		}
	}
	
	gprs_disconnect();
	power_device(ED_GPRS, EP_OFF);
}
Esempio n. 2
0
Result httpwn_setup(char *serverconfig_localpath)
{
	Result ret = 0;
	u64 http_sysmodule_titleid = 0x0004013000002902ULL;
	AM_TitleEntry title_entry;

	u8 *cert = (u8*)builtin_rootca_der;
	u32 certsize = builtin_rootca_der_size;

	u8 *filebuffer;
	u32 filebuffer_size = 0x100000;
	u32 statuscode = 0;

	configctx config;
	targeturlctx *first_targeturlctx = NULL;

	FILE *f;

	memset(&config, 0, sizeof(configctx));
	config.first_targeturlctx = &first_targeturlctx;

	ret = amInit();
	if(ret!=0)
	{
		printf("Failed to initialize AM: 0x%08x.\n", (unsigned int)ret);
		if(ret==0xd8e06406)
		{
			printf("The AM service is inaccessible. With the *hax payloads this should never happen. This is normal with plain ninjhax v1.x: this app isn't usable from ninjhax v1.x without any further hax.\n");
		}
		return ret;
	}

	ret = AM_ListTitles(0, 1, &http_sysmodule_titleid, &title_entry);
	amExit();
	if(ret!=0)
	{
		printf("Failed to get the HTTP sysmodule title-version: 0x%08x.\n", (unsigned int)ret);
		return ret;
	}

	http_codebin_buf = NULL;
	http_codebin_buf32 = NULL;
	http_codebin_size = 0;

	ret = loadcodebin(http_sysmodule_titleid, MEDIATYPE_NAND, &http_codebin_buf, &http_codebin_size);
	if(R_FAILED(ret))
	{
		printf("Failed to load the HTTP sysmodule codebin: 0x%08x.\n", (unsigned int)ret);
		return ret;
	}

	http_codebin_buf32 = (u32*)http_codebin_buf;

	ret = httpcInit(0x1000);
	if(ret!=0)
	{
		printf("Failed to initialize HTTPC: 0x%08x.\n", (unsigned int)ret);
		if(ret==0xd8e06406)
		{
			printf("The HTTPC service is inaccessible. With the *hax payload this may happen if the process this app is running under doesn't have access to that service. Please try rebooting the system, boot *hax payload, then directly launch the app.\n");
		}

		free(http_codebin_buf);

		return ret;
	}

	filebuffer = malloc(filebuffer_size);
	if(filebuffer==NULL)
	{
		printf("Failed to allocate the config filebuffer.\n");
		ret = -2;
		httpcExit();
		free(http_codebin_buf);
		return ret;
		
	}
	memset(filebuffer, 0, filebuffer_size);

	printf("Downloading config...\n");
	ret = download_config("https://yls8.mtheall.com/ctr-httpwn/config.php", cert, certsize, filebuffer, filebuffer_size-1, &statuscode);
	if(ret!=0)
	{
		printf("Config downloading failed: 0x%08x.\n", (unsigned int)ret);

		if(statuscode==500)
		{
			printf("HTTP status-code 500 was returned, server reply:\n%s\n", (char*)filebuffer);
		}

		f = fopen(serverconfig_localpath, "rb");
		if(f)
		{
			printf("Use the cached server_config from SD instead?\nPress the A button to continue, B to abort.\n");
			while(1)
			{
				gspWaitForVBlank();
				hidScanInput();
				if(hidKeysDown() & KEY_A)break;
				if(hidKeysDown() & KEY_B)
				{
					fclose(f);
					httpcExit();
					free(http_codebin_buf);
					return ret;
				}
			}

			memset(filebuffer, 0, filebuffer_size);
			fread(filebuffer, 1, filebuffer_size-1, f);
			fclose(f);
		}
		else
		{
			httpcExit();
			free(http_codebin_buf);
			return ret;
		}
	}
	else
	{
		unlink(serverconfig_localpath);
		f = fopen(serverconfig_localpath, "wb");
		if(f)
		{
			fwrite(filebuffer, 1, strlen((char*)filebuffer), f);
			fclose(f);
		}
	}

	ret = config_parse(&config, (char*)filebuffer);

	if(ret==0)
	{
		if(title_entry.version != 13318)
		{
			printf("The installed HTTP sysmodule version(v%u) is not supported.", title_entry.version);
			if(config.incompatsysver_message[0])printf(" %s", config.incompatsysver_message);
			printf("\n");

			httpcExit();
			free(http_codebin_buf);
			free(filebuffer);

			return -1;
		}

		if(display_config_message(&config, "Message from the server:"))
		{
			httpcExit();
			free(http_codebin_buf);
			free(filebuffer);
			return 0;
		}
	}

	if(ret==0)
	{
		f = fopen("user_config.xml", "rb");
		if(f)
		{
			printf("Loading+parsing user_config.xml since it exists on SD...\n");

			memset(filebuffer, 0, filebuffer_size);
			fread(filebuffer, 1, filebuffer_size-1, f);
			fclose(f);

			ret = config_parse(&config, (char*)filebuffer);

			if(ret==0)
			{
				if(display_config_message(&config, "Message from the user_config:"))
				{
					httpcExit();
					free(http_codebin_buf);
					free(filebuffer);
					return 0;
				}
			}
		}
	}

	if(ret!=0)
	{
		printf("Config parsing failed: 0x%08x.\n", (unsigned int)ret);
		httpcExit();
		free(http_codebin_buf);
		free(filebuffer);
		return ret;
	}

	f = fopen("user_nim_rootcertchain_rootca.der", "rb");
	if(f)
	{
		printf("Loading user_nim_rootcertchain_rootca.der since it exists on SD, which will be used instead of the built-in ctr-httpwn cert...\n");

		memset(filebuffer, 0, filebuffer_size);
		certsize = fread(filebuffer, 1, filebuffer_size, f);
		fclose(f);

		cert = filebuffer;
	}

	printf("Preparing the haxx...\n");
	ret = http_haxx("http://localhost/", cert, certsize, first_targeturlctx);//URL doesn't matter much since this won't actually be requested over the network.
	config_freemem(&config);
	httpcExit();
	free(http_codebin_buf);
	free(filebuffer);
	if(ret!=0)
	{
		printf("Haxx setup failed: 0x%08x.\n", (unsigned int)ret);
		return ret;
	}

	return ret;
}