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

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