aboutsummaryrefslogtreecommitdiffstats
path: root/tools/https-send-sms.c
diff options
context:
space:
mode:
authorDenis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>2022-03-15 16:22:47 +0100
committerDenis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>2022-06-14 02:58:47 +0200
commit397cb23c6cd7e0cf941fd8292dc97c7b612756fd (patch)
tree0b6e200aa2c4bd4251d8579a1ca85939e1c72b97 /tools/https-send-sms.c
parent7ce1371f68c924537431ccc75fba743be53729af (diff)
downloadhardware_replicant_libsamsung-ipc-397cb23c6cd7e0cf941fd8292dc97c7b612756fd.tar.gz
hardware_replicant_libsamsung-ipc-397cb23c6cd7e0cf941fd8292dc97c7b612756fd.tar.bz2
hardware_replicant_libsamsung-ipc-397cb23c6cd7e0cf941fd8292dc97c7b612756fd.zip
tools: Add tool to send SMS from a provider HTTPS interface
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@cyberdimension.org>
Diffstat (limited to 'tools/https-send-sms.c')
-rw-r--r--tools/https-send-sms.c163
1 files changed, 163 insertions, 0 deletions
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@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;
+}