Blame 0009-library-add-_adcli_call_external_program.patch

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