[Replicant] [libsamsung-ipc] [PATCH 06/21] tools: https-send-sms: don't depend on CURLU_ALLOW_SPACE

Denis 'GNUtoo' Carikli GNUtoo at cyberdimension.org
Fri Jun 17 16:15:38 UTC 2022


CURLU_ALLOW_SPACE is available since curl 7.78.0, and not using it
could enable us to be compatible with curl 7.62.0.

Since Replicant 11 has curl 7.67.0, this should enable to run
https-send-sms on Replicant 11.

Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo at cyberdimension.org>
---
 configure.ac           |  2 +-
 tools/https-send-sms.c | 56 ++++++++++++++++++++++++++----------------
 2 files changed, 36 insertions(+), 22 deletions(-)

diff --git a/configure.ac b/configure.ac
index a61adc1..b7b6f78 100644
--- a/configure.ac
+++ b/configure.ac
@@ -6,7 +6,7 @@ XZ_OPT=-v9e
 m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
 
 OPENSSL_REQUIRED=1.0.0e
-LIBCURL_REQUIRED=7.78.0
+LIBCURL_REQUIRED=7.62.0
 
 #------------------------------------------------------------------------------
 # pkg-config
diff --git a/tools/https-send-sms.c b/tools/https-send-sms.c
index 75442dc..1cb4e03 100644
--- a/tools/https-send-sms.c
+++ b/tools/https-send-sms.c
@@ -24,12 +24,17 @@
 
 #include <curl/curl.h>
 
-int send_https(char *url, __attribute__((unused)) char *post_data)
+/* CURL documentation for curl_easy_escape[1] mentions that "This
+ * function does not accept input strings longer than
+ * CURL_MAX_INPUT_LENGTH (8 MB)." but CURL_MAX_INPUT_LENGTH is not
+ * exported in the curl public headers (in /usr/include/curl).
+ */
+#define CURL_MAX_INPUT_LENGTH 8000000
+
+int send_https(CURL *hnd, char *url, __attribute__((unused)) char *post_data)
 {
 	CURLcode ret;
-	CURL *hnd;
 
-	hnd = curl_easy_init();
 	curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, 102400L);
 	curl_easy_setopt(hnd, CURLOPT_URL, url);
 	curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
@@ -41,9 +46,6 @@ int send_https(char *url, __attribute__((unused)) char *post_data)
 
 	ret = curl_easy_perform(hnd);
 
-	curl_easy_cleanup(hnd);
-	hnd = NULL;
-
 	/*
 	 * TODO: HTTP(s) return codes:
 	 * 200: SMS sent
@@ -58,13 +60,19 @@ int send_https(char *url, __attribute__((unused)) char *post_data)
 	return (int)ret;
 }
 
-int create_parameter(char **out, const char *parameter, const char *value)
+int create_parameter(CURL *hnd,
+		     char **out, const char *parameter, const char *value)
 {
 	char *result = NULL;
+	char *encoded_value;
 	size_t size = 0;
 	size_t rc;
 
-	rc = snprintf(result, size, "%s=%s", parameter, value);
+	assert(strlen(value) < CURL_MAX_INPUT_LENGTH);
+	encoded_value = curl_easy_escape(hnd, value, 0);
+	assert(encoded_value != NULL);
+
+	rc = snprintf(result, size, "%s=%s", parameter, encoded_value);
 
 	assert(rc > 0);
 	size = rc;
@@ -76,52 +84,58 @@ int create_parameter(char **out, const char *parameter, const char *value)
 		return rc;
 	}
 
-	rc = snprintf(result, size + 1, "%s=%s", parameter, value);
+	rc = snprintf(result, size + 1, "%s=%s", parameter, encoded_value);
 	assert(rc == size);
 
+	assert(rc < CURL_MAX_INPUT_LENGTH);
+
 	*out = result;
 
+	curl_free(encoded_value);
+
 	return 0;
 }
 
 int send_sms_get(const char *username, const char *password,
 		 const char *message)
 {
-	int rc;
+	CURL *hnd;
 	char *parameter = NULL;
-	CURLU *url = curl_url();
+	int rc;
+	CURLU *url;
 	char *url_string;
 
+	url = curl_url();
+	hnd = curl_easy_init();
+
 	rc = curl_url_set(url, CURLUPART_URL,
 			  "https://smsapi.free-mobile.fr/sendmsg", 0);
 	assert(rc == 0);
 
-	rc = create_parameter(&parameter, "user", username);
+	rc = create_parameter(hnd, &parameter, "user", username);
 	assert(rc == 0);
-	rc = curl_url_set(url, CURLUPART_QUERY, parameter,
-			  CURLU_URLENCODE|CURLU_APPENDQUERY);
+	rc = curl_url_set(url, CURLUPART_QUERY, parameter, CURLU_APPENDQUERY);
 	assert(rc == 0);
 	free(parameter);
 
-	rc = create_parameter(&parameter, "pass", password);
-	rc = curl_url_set(url, CURLUPART_QUERY, parameter,
-			  CURLU_URLENCODE|CURLU_APPENDQUERY);
+	rc = create_parameter(hnd, &parameter, "pass", password);
+	rc = curl_url_set(url, CURLUPART_QUERY, parameter, CURLU_APPENDQUERY);
 	assert(rc == 0);
 	free(parameter);
 
-	rc = create_parameter(&parameter, "msg", message);
-	rc = curl_url_set(url, CURLUPART_QUERY, parameter,
-			  CURLU_URLENCODE|CURLU_APPENDQUERY|CURLU_ALLOW_SPACE);
+	rc = create_parameter(hnd, &parameter, "msg", message);
+	rc = curl_url_set(url, CURLUPART_QUERY, parameter, CURLU_APPENDQUERY);
 	assert(rc == 0);
 	free(parameter);
 
 	rc = curl_url_get(url, CURLUPART_URL, &url_string, 0);
 	assert(rc == 0);
 
-	rc = send_https(url_string, NULL);
+	rc = send_https(hnd, url_string, NULL);
 
 	assert(rc == CURLE_OK);
 
+	curl_easy_cleanup(hnd);
 	curl_url_cleanup(url);
 
 	return 0;
-- 
2.36.1



More information about the Replicant mailing list