Ejemplo n.º 1
0
static int
add_option (char *p[], int line, int unit)
{
	char buf[32] = {0};
	FILE *fp;
	char file_path[128] ={0};

	if  (streq (p[0], "dev") && p[1])
	{
		sprintf(buf, "vpn_client%d_if", unit);
		if(!strncmp(p[1], "tun", 3))
			nvram_set(buf, "tun");
		else if(!strncmp(p[1], "tap", 3))
			nvram_set(buf, "tap");
	}
	else if  (streq (p[0], "proto") && p[1])
	{
		sprintf(buf, "vpn_client%d_proto", unit);
		nvram_set(buf, p[1]);
	}
	else if  (streq (p[0], "remote") && p[1])
	{
		sprintf(buf, "vpn_client%d_addr", unit);
		nvram_set(buf, p[1]);

		sprintf(buf, "vpn_client%d_port", unit);
		if(p[2])
			nvram_set(buf, p[2]);
		else
			nvram_set(buf, "1194");
	}
	else if (streq (p[0], "resolv-retry") && p[1])
	{
		sprintf(buf, "vpn_client%d_retry", unit);
		if (streq (p[1], "infinite"))
			nvram_set(buf, "-1");
		else
			nvram_set(buf, p[1]);
	}
	else if (streq (p[0], "comp-lzo"))
	{
		sprintf(buf, "vpn_client%d_comp", unit);
		if(p[1])
			nvram_set(buf, p[1]);
		else
			nvram_set(buf, "adaptive");
	}
	else if (streq (p[0], "cipher") && p[1])
	{
		sprintf(buf, "vpn_client%d_cipher", unit);
		nvram_set(buf, p[1]);
	}
	else if (streq (p[0], "redirect-gateway") && (!p[1] || streq (p[1], "def1")))	// Only handle if default GW
	{
		sprintf(buf, "vpn_client%d_rgw", unit);
		nvram_set(buf, "1");
	}
	else if (streq (p[0], "verb") && p[1])
	{
		nvram_set("vpn_loglevel", p[1]);
	}
	else if  (streq (p[0], "ca") && p[1])
	{
		sprintf(buf, "vpn_client%d_crypt", unit);
		nvram_set(buf, "tls");
		if (streq (p[1], INLINE_FILE_TAG) && p[2])
		{
			sprintf(buf, "vpn_crt_client%d_ca", unit);
#if defined(RTCONFIG_JFFS2) || defined(RTCONFIG_BRCM_NAND_JFFS2) || defined(RTCONFIG_UBIFS)
			snprintf(file_path, sizeof(file_path) -1, "%s/%s", OVPN_FS_PATH, buf);
			fp = fopen(file_path, "w");
			if(fp) {
				chmod(file_path, S_IRUSR|S_IWUSR);
				fprintf(fp, "%s", strstr(p[2], "-----BEGIN"));
				fclose(fp);
			}
			else
#endif
			write_encoded_crt(buf, strstr(p[2], "-----BEGIN"));
		}
		else
		{
			return VPN_UPLOAD_NEED_CA_CERT;
		}
	}
	else if  (streq (p[0], "cert") && p[1])
	{
		if (streq (p[1], INLINE_FILE_TAG) && p[2])
		{
			sprintf(buf, "vpn_crt_client%d_crt", unit);
#if defined(RTCONFIG_JFFS2) || defined(RTCONFIG_BRCM_NAND_JFFS2) || defined(RTCONFIG_UBIFS)
			snprintf(file_path, sizeof(file_path) -1, "%s/%s", OVPN_FS_PATH, buf);
			fp = fopen(file_path, "w");
			if(fp) {
				chmod(file_path, S_IRUSR|S_IWUSR);
				fprintf(fp, "%s", strstr(p[2], "-----BEGIN"));
				fclose(fp);
			}
			else
#endif
			write_encoded_crt(buf, strstr(p[2], "-----BEGIN"));
		}
		else
		{
			return VPN_UPLOAD_NEED_CERT;
		}
	}
	else if  (streq (p[0], "key") && p[1])
	{
		if (streq (p[1], INLINE_FILE_TAG) && p[2])
		{
			sprintf(buf, "vpn_crt_client%d_key", unit);
#if defined(RTCONFIG_JFFS2) || defined(RTCONFIG_BRCM_NAND_JFFS2) || defined(RTCONFIG_UBIFS)
			snprintf(file_path, sizeof(file_path) -1, "%s/%s", OVPN_FS_PATH, buf);
			fp = fopen(file_path, "w");
			if(fp) {
				chmod(file_path, S_IRUSR|S_IWUSR);
				fprintf(fp, "%s", strstr(p[2], "-----BEGIN"));
				fclose(fp);
			}
			else
#endif
			write_encoded_crt(buf, strstr(p[2], "-----BEGIN"));
		}
		else
		{
			return VPN_UPLOAD_NEED_KEY;
		}
	}
	else if (streq (p[0], "tls-auth") && p[1])
	{
		if (streq (p[1], INLINE_FILE_TAG) && p[2])
		{
			sprintf(buf, "vpn_crt_client%d_static", unit);
#if defined(RTCONFIG_JFFS2) || defined(RTCONFIG_BRCM_NAND_JFFS2) || defined(RTCONFIG_UBIFS)
			snprintf(file_path, sizeof(file_path) -1, "%s/%s", OVPN_FS_PATH, buf);
			fp = fopen(file_path, "w");
			if(fp) {
				chmod(file_path, S_IRUSR|S_IWUSR);
				fprintf(fp, "%s", strstr(p[2], "-----BEGIN"));
				fclose(fp);
			}
			else
#endif
			write_encoded_crt(buf, strstr(p[2], "-----BEGIN"));
		}
		else
		{
			if(p[2]) {
				sprintf(buf, "vpn_client%d_hmac", unit);
				nvram_set(buf, p[2]);
			}
			return VPN_UPLOAD_NEED_STATIC;
		}
	}
	else if (streq (p[0], "secret") && p[1])
	{
		sprintf(buf, "vpn_client%d_crypt", unit);
		nvram_set(buf, "secret");
		if (streq (p[1], INLINE_FILE_TAG) && p[2])
		{
			sprintf(buf, "vpn_crt_client%d_static", unit);
#if defined(RTCONFIG_JFFS2) || defined(RTCONFIG_BRCM_NAND_JFFS2) || defined(RTCONFIG_UBIFS)
			snprintf(file_path, sizeof(file_path) -1, "%s/%s", OVPN_FS_PATH, buf);
			fp = fopen(file_path, "w");
			if(fp) {
				chmod(file_path, S_IRUSR|S_IWUSR);
				fprintf(fp, "%s", strstr(p[2], "-----BEGIN"));
				fclose(fp);
			}
			else
#endif
			write_encoded_crt(buf, strstr(p[2], "-----BEGIN"));
		}
		else
		{
			return VPN_UPLOAD_NEED_STATIC;
		}
	}
	else if (streq (p[0], "extra-certs") && p[1])
	{
		if (streq (p[1], INLINE_FILE_TAG) && p[2])
		{
			sprintf(buf, "vpn_crt_client%d_extra", unit);
#if defined(RTCONFIG_JFFS2) || defined(RTCONFIG_BRCM_NAND_JFFS2) || defined(RTCONFIG_UBIFS)
			snprintf(file_path, sizeof(file_path) -1, "%s/%s", OVPN_FS_PATH, buf);
			fp = fopen(file_path, "w");
			if(fp) {
				chmod(file_path, S_IRUSR|S_IWUSR);
				fprintf(fp, "%s", strstr(p[2], "-----BEGIN"));
				fclose(fp);
			}
			else
#endif
			write_encoded_crt(buf, strstr(p[2], "-----BEGIN"));
		}
		else
		{
			return VPN_UPLOAD_NEED_EXTRA;
		}
	}

	else if (streq (p[0], "auth-user-pass"))
	{
		sprintf(buf, "vpn_client%d_userauth", unit);
		nvram_set(buf, "1");
	}
	else if (streq (p[0], "tls-remote") && p[1])
	{
		sprintf(buf, "vpn_client%d_tlsremote", unit);
		nvram_set(buf, "1");
		sprintf(buf, "vpn_client%d_cn", unit);
		nvram_set(buf, p[1]);
	}
	else if (streq (p[0], "key-direction") && p[1])
	{
		sprintf(buf, "vpn_client%d_hmac", unit);
		nvram_set(buf, p[1]);
	}
	// These are already added by us
	else if (streq (p[0], "client") ||
		 streq (p[0], "nobind") ||
		 streq (p[0], "persist-key") ||
		 streq (p[0], "persist-tun"))
	{
		return 0;	// Don't duplicate them
	}
	else if (streq (p[0], "crl-verify") && p[1])
	{
		if (p[2] && streq(p[2], "dir"))
			;//TODO: not support?
		return VPN_UPLOAD_NEED_CRL;
	}
	else
	{
		sprintf(buf, "vpn_client%d_custom", unit);
		add_custom(buf, p);
	}
	return 0;
}
Ejemplo n.º 2
0
static int
add_option (char *p[], int line, int unit)
{
	char buf[32] = {0};

	if  (streq (p[0], "dev") && p[1])
	{
		sprintf(buf, "vpn_client%d_if", unit);
		nvram_set(buf, p[1]);
	}
	else if  (streq (p[0], "proto") && p[1])
	{
		sprintf(buf, "vpn_client%d_proto", unit);
		nvram_set(buf, p[1]);
	}
	else if  (streq (p[0], "remote") && p[1])
	{
		sprintf(buf, "vpn_client%d_addr", unit);
		nvram_set(buf, p[1]);

		sprintf(buf, "vpn_client%d_port", unit);
		if(p[2])
			nvram_set(buf, p[2]);
		else
			nvram_set(buf, "1194");
	}
	else if (streq (p[0], "resolv-retry") && p[1])
	{
		sprintf(buf, "vpn_client%d_retry", unit);
		if (streq (p[1], "infinite"))
			nvram_set(buf, "-1");
		else
			nvram_set(buf, p[1]);
	}
	else if (streq (p[0], "comp-lzo"))
	{
		sprintf(buf, "vpn_client%d_comp", unit);
		if(p[1])
			nvram_set(buf, p[1]);
		else
			nvram_set(buf, "adaptive");
	}
	else if (streq (p[0], "cipher") && p[1])
	{
		sprintf(buf, "vpn_client%d_cipher", unit);
		nvram_set(buf, p[1]);
	}
	else if (streq (p[0], "redirect-gateway") && (!p[1] || streq (p[1], "def1")))	// Only handle if default GW
	{
		sprintf(buf, "vpn_client%d_rgw", unit);
		nvram_set(buf, "1");
	}
	else if (streq (p[0], "verb") && p[1])
	{
		nvram_set("vpn_loglevel", p[1]);
	}
	else if  (streq (p[0], "ca") && p[1])
	{
		sprintf(buf, "vpn_client%d_crypt", unit);
		nvram_set(buf, "tls");
		if (streq (p[1], INLINE_FILE_TAG) && p[2])
		{
			sprintf(buf, "vpn_crt_client%d_ca", unit);
			write_encoded_crt(buf, strstr(p[2], "-----BEGIN"));
		}
		else
		{
			return VPN_UPLOAD_NEED_CA_CERT;
		}
	}
	else if  (streq (p[0], "cert") && p[1])
	{
		if (streq (p[1], INLINE_FILE_TAG) && p[2])
		{
			sprintf(buf, "vpn_crt_client%d_crt", unit);
			write_encoded_crt(buf, strstr(p[2], "-----BEGIN"));
		}
		else
		{
			return VPN_UPLOAD_NEED_CERT;
		}
	}
	else if  (streq (p[0], "key") && p[1])
	{
		if (streq (p[1], INLINE_FILE_TAG) && p[2])
		{
			sprintf(buf, "vpn_crt_client%d_key", unit);
			write_encoded_crt(buf, strstr(p[2], "-----BEGIN"));
		}
		else
		{
			return VPN_UPLOAD_NEED_KEY;
		}
	}
	else if (streq (p[0], "tls-auth") && p[1])
	{
		if (streq (p[1], INLINE_FILE_TAG) && p[2])
		{
			sprintf(buf, "vpn_crt_client%d_static", unit);
			write_encoded_crt(buf, strstr(p[2], "-----BEGIN"));
		}
		else
		{
			if(p[2]) {
				sprintf(buf, "vpn_server%d_hmac", unit);
				nvram_set(buf, p[2]);
			}
			return VPN_UPLOAD_NEED_STATIC;
		}
	}
	else if (streq (p[0], "secret") && p[1])
	{
		sprintf(buf, "vpn_client%d_crypt", unit);
		nvram_set(buf, "secret");
		if (streq (p[1], INLINE_FILE_TAG) && p[2])
		{
			sprintf(buf, "vpn_crt_client%d_static", unit);
			write_encoded_crt(buf, strstr(p[2], "-----BEGIN"));
		}
		else
		{
			return VPN_UPLOAD_NEED_STATIC;
		}
	}
	else if (streq (p[0], "auth-user-pass"))
	{
		sprintf(buf, "vpn_client%d_userauth", unit);
		nvram_set(buf, "1");
	}
	else if (streq (p[0], "tls-remote") && p[1])
	{
		sprintf(buf, "vpn_client%d_tlsremote", unit);
		nvram_set(buf, "1");
		sprintf(buf, "vpn_client%d_cn", unit);
		nvram_set(buf, p[1]);
	}
	else
	{
		sprintf(buf, "vpn_client%d_custom", unit);
		add_custom(buf, p);
	}
	return 0;
}