Blame SOURCES/0009-library-add-_adcli_call_external_program.patch

48b328
From 63576f12524f521c0cf08d42b279654885135a90 Mon Sep 17 00:00:00 2001
48b328
From: Sumit Bose <sbose@redhat.com>
48b328
Date: Tue, 30 Jan 2018 14:39:17 +0100
48b328
Subject: [PATCH 09/23] library: add _adcli_call_external_program()
48b328
48b328
Allow adcli to call an external program given by an absolute path name
48b328
and an array of options. stdin and stdout can be used if needed.
48b328
48b328
https://bugs.freedesktop.org/show_bug.cgi?id=100118
48b328
48b328
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
48b328
---
48b328
 configure.ac        |  28 +++++++
48b328
 library/adprivate.h |   6 ++
48b328
 library/adutil.c    | 211 ++++++++++++++++++++++++++++++++++++++++++++++++++++
48b328
 3 files changed, 245 insertions(+)
48b328
48b328
diff --git a/configure.ac b/configure.ac
48b328
index 221d8ae..fe86638 100644
48b328
--- a/configure.ac
48b328
+++ b/configure.ac
48b328
@@ -263,6 +263,34 @@ AC_SUBST(LCOV)
48b328
 AC_SUBST(GCOV)
48b328
 AC_SUBST(GENHTML)
48b328
 
48b328
+AC_PATH_PROG(BIN_CAT, cat, no)
48b328
+if test "$BIN_CAT" = "no" ; then
48b328
+	AC_MSG_ERROR([cat is not available])
48b328
+else
48b328
+	AC_DEFINE_UNQUOTED(BIN_CAT, "$BIN_CAT", [path to cat, used in unit test])
48b328
+fi
48b328
+
48b328
+AC_PATH_PROG(BIN_TAC, tac, no)
48b328
+if test "$BIN_TAC" = "no" ; then
48b328
+	AC_MSG_ERROR([tac is not available])
48b328
+else
48b328
+	AC_DEFINE_UNQUOTED(BIN_TAC, "$BIN_TAC", [path to tac, used in unit test])
48b328
+fi
48b328
+
48b328
+AC_PATH_PROG(BIN_REV, rev, no)
48b328
+if test "$BIN_REV" = "no" ; then
48b328
+	AC_MSG_ERROR([rev is not available])
48b328
+else
48b328
+	AC_DEFINE_UNQUOTED(BIN_REV, "$BIN_REV", [path to rev, used in unit test])
48b328
+fi
48b328
+
48b328
+AC_PATH_PROG(BIN_ECHO, echo, no)
48b328
+if test "$BIN_ECHO" = "no" ; then
48b328
+	AC_MSG_ERROR([echo is not available])
48b328
+else
48b328
+	AC_DEFINE_UNQUOTED(BIN_ECHO, "$BIN_ECHO", [path to echo, used in unit test])
48b328
+fi
48b328
+
48b328
 # ---------------------------------------------------------------------
48b328
 
48b328
 ADCLI_LT_RELEASE=$ADCLI_CURRENT:$ADCLI_REVISION:$ADCLI_AGE
48b328
diff --git a/library/adprivate.h b/library/adprivate.h
48b328
index e99f9fc..7257c93 100644
48b328
--- a/library/adprivate.h
48b328
+++ b/library/adprivate.h
48b328
@@ -285,4 +285,10 @@ struct _adcli_attrs {
48b328
 
48b328
 bool             _adcli_check_nt_time_string_lifetime (const char *nt_time_string, unsigned int lifetime);
48b328
 
48b328
+adcli_result     _adcli_call_external_program     (const char *binary,
48b328
+                                                   char * const *argv,
48b328
+                                                   const char *stdin_data,
48b328
+                                                   uint8_t **stdout_data,
48b328
+                                                   size_t *stdout_data_len);
48b328
+
48b328
 #endif /* ADPRIVATE_H_ */
48b328
diff --git a/library/adutil.c b/library/adutil.c
48b328
index 829cdd9..a27bd68 100644
48b328
--- a/library/adutil.c
48b328
+++ b/library/adutil.c
48b328
@@ -36,6 +36,7 @@
48b328
 #include <unistd.h>
48b328
 #include <stdint.h>
48b328
 #include <time.h>
48b328
+#include <sys/wait.h>
48b328
 
48b328
 static adcli_message_func message_func = NULL;
48b328
 static char last_error[2048] = { 0, };
48b328
@@ -506,6 +507,161 @@ _adcli_check_nt_time_string_lifetime (const char *nt_time_string,
48b328
 	return false;
48b328
 }
48b328
 
48b328
+adcli_result
48b328
+_adcli_call_external_program (const char *binary, char * const *argv,
48b328
+                              const char *stdin_data,
48b328
+                              uint8_t **stdout_data, size_t *stdout_data_len)
48b328
+{
48b328
+	int ret;
48b328
+	int pipefd_to_child[2] = { -1, -1};
48b328
+	int pipefd_from_child[2] = { -1, -1};
48b328
+	pid_t child_pid = 0;
48b328
+	int err;
48b328
+	size_t len;
48b328
+	ssize_t rlen;
48b328
+	pid_t wret;
48b328
+	int status;
48b328
+	uint8_t read_buf[4096];
48b328
+	uint8_t *out;
48b328
+
48b328
+	errno = 0;
48b328
+	ret = access (binary, X_OK);
48b328
+	if (ret != 0) {
48b328
+		err = errno;
48b328
+		_adcli_err ("Cannot run [%s]: [%d][%s].", binary, err,
48b328
+		                                          strerror (err));
48b328
+		ret = ADCLI_ERR_FAIL;
48b328
+		goto done;
48b328
+	}
48b328
+
48b328
+	ret = pipe (pipefd_from_child);
48b328
+	if (ret == -1) {
48b328
+		err = errno;
48b328
+		_adcli_err ("pipe failed [%d][%s].", err, strerror (err));
48b328
+		ret = ADCLI_ERR_FAIL;
48b328
+		goto done;
48b328
+	}
48b328
+
48b328
+	ret = pipe (pipefd_to_child);
48b328
+	if (ret == -1) {
48b328
+		err = errno;
48b328
+		_adcli_err ("pipe failed [%d][%s].", err, strerror (err));
48b328
+		ret = ADCLI_ERR_FAIL;
48b328
+		goto done;
48b328
+	}
48b328
+
48b328
+	child_pid = fork ();
48b328
+
48b328
+	if (child_pid == 0) { /* child */
48b328
+		close (pipefd_to_child[1]);
48b328
+		ret = dup2 (pipefd_to_child[0], STDIN_FILENO);
48b328
+		if (ret == -1) {
48b328
+			err = errno;
48b328
+			_adcli_err ("dup2 failed [%d][%s].", err,
48b328
+			                                     strerror (err));
48b328
+			exit (EXIT_FAILURE);
48b328
+		}
48b328
+
48b328
+		close (pipefd_from_child[0]);
48b328
+		ret = dup2 (pipefd_from_child[1], STDOUT_FILENO);
48b328
+		if (ret == -1) {
48b328
+			err = errno;
48b328
+			_adcli_err ("dup2 failed [%d][%s].", err,
48b328
+			                                     strerror (err));
48b328
+			exit (EXIT_FAILURE);
48b328
+		}
48b328
+
48b328
+		execv (binary, argv);
48b328
+		_adcli_err ("Failed to run %s.", binary);
48b328
+		ret = ADCLI_ERR_FAIL;
48b328
+		goto done;
48b328
+	} else if (child_pid > 0) { /* parent */
48b328
+
48b328
+		if (stdin_data != NULL) {
48b328
+			len = strlen (stdin_data);
48b328
+			ret = write (pipefd_to_child[1], stdin_data, len);
48b328
+			if (ret != len) {
48b328
+				_adcli_err ("Failed to send computer account password "
48b328
+				            "to net command.");
48b328
+				ret = ADCLI_ERR_FAIL;
48b328
+				goto done;
48b328
+			}
48b328
+		}
48b328
+
48b328
+		close (pipefd_to_child[0]);
48b328
+		pipefd_to_child[0] = -1;
48b328
+		close (pipefd_to_child[1]);
48b328
+		pipefd_to_child[0] = -1;
48b328
+
48b328
+		if (stdout_data != NULL || stdout_data_len != NULL) {
48b328
+			rlen = read (pipefd_from_child[0], read_buf, sizeof (read_buf));
48b328
+			if (rlen < 0) {
48b328
+				ret = errno;
48b328
+				_adcli_err ("Failed to read from child [%d][%s].\n",
48b328
+				            ret, strerror (ret));
48b328
+				ret = ADCLI_ERR_FAIL;
48b328
+				goto done;
48b328
+			}
48b328
+
48b328
+			out = malloc (sizeof(uint8_t) * rlen);
48b328
+			if (out == NULL) {
48b328
+				_adcli_err ("Failed to allocate memory "
48b328
+				            "for child output.");
48b328
+				ret = ADCLI_ERR_FAIL;
48b328
+				goto done;
48b328
+			} else {
48b328
+				memcpy (out, read_buf, rlen);
48b328
+			}
48b328
+
48b328
+			if (stdout_data != NULL) {
48b328
+				*stdout_data = out;
48b328
+			} else {
48b328
+				free (out);
48b328
+			}
48b328
+
48b328
+			if (stdout_data_len != NULL) {
48b328
+				*stdout_data_len = rlen;
48b328
+			}
48b328
+		}
48b328
+
48b328
+	} else {
48b328
+		_adcli_err ("Cannot run net command.");
48b328
+		ret = ADCLI_ERR_FAIL;
48b328
+		goto done;
48b328
+	}
48b328
+
48b328
+	ret = ADCLI_SUCCESS;
48b328
+
48b328
+done:
48b328
+	if (pipefd_from_child[0] != -1) {
48b328
+		close (pipefd_from_child[0]);
48b328
+	}
48b328
+	if (pipefd_from_child[1] != -1) {
48b328
+		close (pipefd_from_child[1]);
48b328
+	}
48b328
+	if (pipefd_to_child[0] != -1) {
48b328
+		close (pipefd_to_child[0]);
48b328
+	}
48b328
+	if (pipefd_to_child[1] != -1) {
48b328
+		close (pipefd_to_child[1]);
48b328
+	}
48b328
+
48b328
+	if (child_pid > 0) {
48b328
+		wret = waitpid (child_pid, &status, 0);
48b328
+		if (wret == -1) {
48b328
+			_adcli_err ("No sure what happend to net command.");
48b328
+		} else {
48b328
+			if (WIFEXITED (status)) {
48b328
+				_adcli_err ("net command failed with %d.",
48b328
+				            WEXITSTATUS (status));
48b328
+			}
48b328
+		}
48b328
+	}
48b328
+
48b328
+	return ret;
48b328
+}
48b328
+
48b328
+
48b328
 #ifdef UTIL_TESTS
48b328
 
48b328
 #include "test.h"
48b328
@@ -620,6 +776,60 @@ test_bin_sid_to_str (void)
48b328
 	free (str);
48b328
 }
48b328
 
48b328
+static void
48b328
+test_call_external_program (void)
48b328
+{
48b328
+	adcli_result res;
48b328
+	char *argv[] = { NULL, NULL, NULL };
48b328
+	uint8_t *stdout_data;
48b328
+	size_t stdout_data_len;
48b328
+
48b328
+	argv[0] = "/does/not/exists";
48b328
+	res = _adcli_call_external_program (argv[0], argv, NULL, NULL, NULL);
48b328
+	assert (res == ADCLI_ERR_FAIL);
48b328
+
48b328
+#ifdef BIN_CAT
48b328
+	argv[0] = BIN_CAT;
48b328
+	res = _adcli_call_external_program (argv[0], argv, "Hello",
48b328
+	                                    &stdout_data, &stdout_data_len);
48b328
+	assert (res == ADCLI_SUCCESS);
48b328
+	assert (strncmp ("Hello", (char *) stdout_data, stdout_data_len) == 0);
48b328
+	free (stdout_data);
48b328
+
48b328
+	res = _adcli_call_external_program (argv[0], argv, "Hello",
48b328
+	                                    NULL, NULL);
48b328
+	assert (res == ADCLI_SUCCESS);
48b328
+#endif
48b328
+
48b328
+#ifdef BIN_REV
48b328
+	argv[0] = BIN_REV;
48b328
+	res = _adcli_call_external_program (argv[0], argv, "Hello\n",
48b328
+	                                    &stdout_data, &stdout_data_len);
48b328
+	assert (res == ADCLI_SUCCESS);
48b328
+	assert (strncmp ("olleH\n", (char *) stdout_data, stdout_data_len) == 0);
48b328
+	free (stdout_data);
48b328
+#endif
48b328
+
48b328
+#ifdef BIN_TAC
48b328
+	argv[0] = BIN_TAC;
48b328
+	res = _adcli_call_external_program (argv[0], argv, "Hello\nWorld\n",
48b328
+	                                    &stdout_data, &stdout_data_len);
48b328
+	assert (res == ADCLI_SUCCESS);
48b328
+	assert (strncmp ("World\nHello\n", (char *) stdout_data, stdout_data_len) == 0);
48b328
+	free (stdout_data);
48b328
+#endif
48b328
+
48b328
+#ifdef BIN_ECHO
48b328
+	argv[0] = BIN_ECHO;
48b328
+	argv[1] = "Hello";
48b328
+	res = _adcli_call_external_program (argv[0], argv, NULL,
48b328
+	                                    &stdout_data, &stdout_data_len);
48b328
+	assert (res == ADCLI_SUCCESS);
48b328
+	assert (strncmp ("Hello\n", (char *) stdout_data, stdout_data_len) == 0);
48b328
+	free (stdout_data);
48b328
+#endif
48b328
+}
48b328
+
48b328
 int
48b328
 main (int argc,
48b328
       char *argv[])
48b328
@@ -629,6 +839,7 @@ main (int argc,
48b328
 	test_func (test_strv_count, "/util/strv_count");
48b328
 	test_func (test_check_nt_time_string_lifetime, "/util/check_nt_time_string_lifetime");
48b328
 	test_func (test_bin_sid_to_str, "/util/bin_sid_to_str");
48b328
+	test_func (test_call_external_program, "/util/call_external_program");
48b328
 	return test_run (argc, argv);
48b328
 }
48b328
 
48b328
-- 
48b328
2.14.4
48b328