[Replicant] [libsamsung-ipc] [PATCH 05/21] tools: Add tool to send SMS from a provider HTTPS interface

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


This can then be used during automatic tests, but the downside is that
for now this tool only works with one provider.

The following symbols from (lib)curl (along with their corresponding
minimal (lib)curl version) have been used:
+------------------------+---------------------------+
| curl symbols           | minimum (lib)curl version |
+------------------------+---------------------------+
| CURL_HTTP_VERSION_2TLS | 7.47.0                    |
| CURLUPART_URL          | 7.62.0                    |
| CURLUPART_QUERY        | 7.62.0                    |
| CURLU_URLENCODE        | 7.62.0                    |
| CURLU_APPENDQUERY      | 7.62.0                    |
| CURLU                  | 7.62.0                    |
| CURLU_ALLOW_SPACE      | 7.78.0                    |
+------------------------+---------------------------+

So we need to depend on curl 7.78.0.

And we currently have the following curl version:
+----------------------------+--------------------+------------+
| Distribution               | (lib)curl version  | Compatible |
+----------------------------+--------------------+------------+
| Replicant 4.2              | Doesn't have curl  | No         |
| Guix 6b9105e557            | 7.28.1             | No         |
| Trisquel 7 x86_64          | 7.35.0-1ubuntu2.20 | No         |
| Replicant 6                | 7.43.0             | No         |
| Debian stretch x86_64      | 7.52.1-5+deb9u10   | No         |
| Replicant 11               | 7.67.0             | No         |
| Parabola i686 (07/04/2022) | 7.79.0-3.0         | Yes        |
| Guix     i686 (07/04/2022) | 7.79.1             | Yes        |
+----------------------------+--------------------+------------+

Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo at cyberdimension.org>
---
 .gitignore             |   1 +
 Android.mk             |  16 ++++
 configure.ac           |  10 +++
 scripts/guix.scm       |   3 +
 tools/Makefile.am      |   6 ++
 tools/https-send-sms.c | 163 +++++++++++++++++++++++++++++++++++++++++
 6 files changed, 199 insertions(+)
 create mode 100644 tools/https-send-sms.c

diff --git a/.gitignore b/.gitignore
index 7f6b81e..3d9bbb6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -27,6 +27,7 @@ valgrind.*.log
 /scripts/src/**
 
 # Tools
+/tools/https-send-sms
 /tools/ipc-modem/ipc-modem
 /tools/ipc-test
 /tools/nv_data-imei
diff --git a/Android.mk b/Android.mk
index d383e84..663de28 100644
--- a/Android.mk
+++ b/Android.mk
@@ -161,6 +161,22 @@ LOCAL_EXPORT_C_INCLUDE_DIRS := $(local_export_c_include_dirs)
 
 include $(BUILD_SHARED_LIBRARY)
 
+#######################
+# https-send-sms tool #
+#######################
+include $(CLEAR_VARS)
+include $(LOCAL_PATH)/android_versions.mk
+
+LOCAL_MODULE := https-send-sms
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_SRC_FILES := tools/https-send-sms.c
+
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/tools/include/glibc
+LOCAL_SHARED_LIBRARIES := libcurl
+
+include $(BUILD_EXECUTABLE)
+
 ##################
 # ipc-modem tool #
 ##################
diff --git a/configure.ac b/configure.ac
index 63eb809..a61adc1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -6,6 +6,7 @@ XZ_OPT=-v9e
 m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
 
 OPENSSL_REQUIRED=1.0.0e
+LIBCURL_REQUIRED=7.78.0
 
 #------------------------------------------------------------------------------
 # pkg-config
@@ -18,8 +19,17 @@ PKG_PROG_PKG_CONFIG
 PKG_CHECK_MODULES(OPENSSL, openssl >= $OPENSSL_REQUIRED)
 AC_SUBST(OPENSSL_CFLAGS)
 AC_SUBST(OPENSSL_LIBS)
+
+#------------------------------------------------------------------------------
+# valgrind
 AC_SUBST(VALGRIND)
 
+#------------------------------------------------------------------------------
+# libcurl
+PKG_CHECK_MODULES(LIBCURL, libcurl >= $LIBCURL_REQUIRED)
+AC_SUBST(LIBCURL_CFLAGS)
+AC_SUBST(LIBCURL_LIBS)
+
 #------------------------------------------------------------------------------
 # check for debugging
 AC_ARG_ENABLE(debug,
diff --git a/scripts/guix.scm b/scripts/guix.scm
index b8e5bef..4a772a9 100644
--- a/scripts/guix.scm
+++ b/scripts/guix.scm
@@ -54,6 +54,7 @@
  (gnu packages android)
  (gnu packages autotools)
  (gnu packages commencement)
+ (gnu packages curl)
  (gnu packages disk)
  (gnu packages linux)
  (gnu packages llvm)
@@ -106,6 +107,7 @@
        ("aclocal" ,automake)
        ("ddrescue", ddrescue)
        ("libc:debug", (@@ (gnu packages commencement) glibc-final) "debug")
+       ("libcurl" ,curl)
        ("libtool" ,libtool)
        ("pkgconfig" ,pkg-config)
        ("python" ,python)
@@ -186,6 +188,7 @@ found in many Samsung smartphones and tablets.")
      `(("autoreconf" ,autoconf)
        ("aclocal" ,automake)
        ("ddrescue", ddrescue)
+       ("libcurl" ,curl)
        ("libtool" ,libtool)
        ("pkgconfig" ,pkg-config)
        ("python" ,python)
diff --git a/tools/Makefile.am b/tools/Makefile.am
index f0ed03b..5d79235 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -2,9 +2,11 @@ NULL =
 
 AM_CFLAGS = \
 	-I$(top_srcdir)/include \
+	$(LIBCURL_CFLAGS) \
 	$(NULL)
 
 bin_PROGRAMS = \
+	https-send-sms \
 	ipc-test \
 	nv_data-imei \
 	nv_data-md5 \
@@ -21,6 +23,10 @@ TESTS = \
 	tests/nv_data-imei.py \
 	tests/nv_data-md5.py
 
+https_send_sms_SOURCES = https-send-sms.c
+https_send_sms_LDADD = $(LIBCURL_LIBS)
+https_send_sms_LDFLAGS =
+
 ipc_test_SOURCES = ipc-test.c
 ipc_test_LDADD = $(top_builddir)/samsung-ipc/libsamsung-ipc.la
 ipc_test_LDFLAGS =
diff --git a/tools/https-send-sms.c b/tools/https-send-sms.c
new file mode 100644
index 0000000..75442dc
--- /dev/null
+++ b/tools/https-send-sms.c
@@ -0,0 +1,163 @@
+/*
+ * This file is part of libsamsung-ipc.
+ *
+ * Copyright (C) 2022 Denis 'GNUtoo' Carikli <GNUtoo at cyberdimension.org>
+ *
+ * libsamsung-ipc is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * libsamsung-ipc is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libsamsung-ipc.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <assert.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+
+#include <curl/curl.h>
+
+int send_https(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);
+	curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
+	curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION,
+			 (long)CURL_HTTP_VERSION_2TLS);
+	curl_easy_setopt(hnd, CURLOPT_FTP_SKIP_PASV_IP, 1L);
+	curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
+
+	ret = curl_easy_perform(hnd);
+
+	curl_easy_cleanup(hnd);
+	hnd = NULL;
+
+	/*
+	 * TODO: HTTP(s) return codes:
+	 * 200: SMS sent
+	 * 400: missing parameter (user, login, message)
+	 * 402: Too many SMS sent in too little time
+	 * 403: Possible causes:
+	 *      - Service is not enabled inside user account web interface
+	 *      - Wrong login or password
+	 * 500: Server error => Retry later
+	 */
+
+	return (int)ret;
+}
+
+int create_parameter(char **out, const char *parameter, const char *value)
+{
+	char *result = NULL;
+	size_t size = 0;
+	size_t rc;
+
+	rc = snprintf(result, size, "%s=%s", parameter, value);
+
+	assert(rc > 0);
+	size = rc;
+
+	result = malloc(size + 1);
+	if (result == NULL) {
+		rc = -errno;
+		printf("%s: error %zd: %s\n", __func__, rc, strerror(rc));
+		return rc;
+	}
+
+	rc = snprintf(result, size + 1, "%s=%s", parameter, value);
+	assert(rc == size);
+
+	*out = result;
+
+	return 0;
+}
+
+int send_sms_get(const char *username, const char *password,
+		 const char *message)
+{
+	int rc;
+	char *parameter = NULL;
+	CURLU *url = curl_url();
+	char *url_string;
+
+	rc = curl_url_set(url, CURLUPART_URL,
+			  "https://smsapi.free-mobile.fr/sendmsg", 0);
+	assert(rc == 0);
+
+	rc = create_parameter(&parameter, "user", username);
+	assert(rc == 0);
+	rc = curl_url_set(url, CURLUPART_QUERY, parameter,
+			  CURLU_URLENCODE|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);
+	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);
+	assert(rc == 0);
+	free(parameter);
+
+	rc = curl_url_get(url, CURLUPART_URL, &url_string, 0);
+	assert(rc == 0);
+
+	rc = send_https(url_string, NULL);
+
+	assert(rc == CURLE_OK);
+
+	curl_url_cleanup(url);
+
+	return 0;
+}
+
+void usage(char *progname)
+{
+	printf("Usage: %s free-mobile <username> <token> <message>\n\n",
+	       progname);
+	printf("Example:\n\t%s %s \"%s\" \"%s\" \"%s\"\n",
+	       progname,
+	       "free-mobile",
+	       "12345678",
+	       "1234abcdEFGH",
+	       "hello world!");
+}
+
+int main(int argc, char *argv[])
+{
+	char *message;
+	char *password;
+	char *username;
+	int rc;
+
+	if (argc != 5 || strncmp("free-mobile", argv[1],
+				 strlen("free-mobile"))) {
+		usage(argv[0]);
+		return EX_USAGE;
+	}
+
+	username = argv[2];
+	password = argv[3];
+	message = argv[4];
+
+	rc = send_sms_get(username, password, message);
+	if (rc == 0)
+		printf("OK\n");
+	return rc;
+}
-- 
2.36.1



More information about the Replicant mailing list