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

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