Blame SOURCES/0002-tools-remove-errx-from-user-and-group-commands.patch

341f9a
From cac0fa9df8888245399f2db187e05e31f93d1471 Mon Sep 17 00:00:00 2001
341f9a
From: Sumit Bose <sbose@redhat.com>
341f9a
Date: Mon, 15 Apr 2019 17:56:37 +0200
341f9a
Subject: [PATCH 2/7] tools: remove errx from user and group commands
341f9a
341f9a
Related to https://bugzilla.redhat.com/show_bug.cgi?id=1588596
341f9a
---
341f9a
 tools/entry.c | 232 +++++++++++++++++++++++++++++++++-----------------
341f9a
 1 file changed, 154 insertions(+), 78 deletions(-)
341f9a
341f9a
diff --git a/tools/entry.c b/tools/entry.c
341f9a
index de56586..97ec6e7 100644
341f9a
--- a/tools/entry.c
341f9a
+++ b/tools/entry.c
341f9a
@@ -232,21 +232,30 @@ adcli_tool_user_create (adcli_conn *conn,
341f9a
 	argc -= optind;
341f9a
 	argv += optind;
341f9a
 
341f9a
-	if (argc != 1)
341f9a
-		errx (2, "specify one user name to create");
341f9a
+	if (argc != 1) {
341f9a
+		warnx ("specify one user name to create");
341f9a
+		adcli_attrs_free (attrs);
341f9a
+		return 2;
341f9a
+	}
341f9a
 
341f9a
 	entry = adcli_entry_new_user (conn, argv[0]);
341f9a
-	if (entry == NULL)
341f9a
-		errx (-1, "unexpected memory problems");
341f9a
+	if (entry == NULL) {
341f9a
+		warnx ("unexpected memory problems");
341f9a
+		adcli_attrs_free (attrs);
341f9a
+		return -1;
341f9a
+	}
341f9a
 	adcli_entry_set_domain_ou (entry, ou);
341f9a
 
341f9a
 	adcli_conn_set_allowed_login_types (conn, ADCLI_LOGIN_USER_ACCOUNT);
341f9a
 
341f9a
 	res = adcli_conn_connect (conn);
341f9a
 	if (res != ADCLI_SUCCESS) {
341f9a
-		errx (-res, "couldn't connect to %s domain: %s",
341f9a
-		      adcli_conn_get_domain_name (conn),
341f9a
-		      adcli_get_last_error ());
341f9a
+		warnx ("couldn't connect to %s domain: %s",
341f9a
+		       adcli_conn_get_domain_name (conn),
341f9a
+		       adcli_get_last_error ());
341f9a
+		adcli_entry_unref (entry);
341f9a
+		adcli_attrs_free (attrs);
341f9a
+		return -res;
341f9a
 	}
341f9a
 
341f9a
 	if (has_unix_attr && !has_nis_domain) {
341f9a
@@ -254,16 +263,20 @@ adcli_tool_user_create (adcli_conn *conn,
341f9a
 		if (res != ADCLI_SUCCESS) {
341f9a
 			adcli_entry_unref (entry);
341f9a
 			adcli_attrs_free (attrs);
341f9a
-			errx (-res, "couldn't get NIS domain");
341f9a
+			warnx ("couldn't get NIS domain");
341f9a
+			return -res;
341f9a
 		}
341f9a
 	}
341f9a
 
341f9a
 	res = adcli_entry_create (entry, attrs);
341f9a
 	if (res != ADCLI_SUCCESS) {
341f9a
-		errx (-res, "creating user %s in domain %s failed: %s",
341f9a
-		      adcli_entry_get_sam_name (entry),
341f9a
-		      adcli_conn_get_domain_name (conn),
341f9a
-		      adcli_get_last_error ());
341f9a
+		warnx ("creating user %s in domain %s failed: %s",
341f9a
+		       adcli_entry_get_sam_name (entry),
341f9a
+		       adcli_conn_get_domain_name (conn),
341f9a
+		       adcli_get_last_error ());
341f9a
+		adcli_entry_unref (entry);
341f9a
+		adcli_attrs_free (attrs);
341f9a
+		return -res;
341f9a
 	}
341f9a
 
341f9a
 	adcli_entry_unref (entry);
341f9a
@@ -317,28 +330,36 @@ adcli_tool_user_delete (adcli_conn *conn,
341f9a
 	argc -= optind;
341f9a
 	argv += optind;
341f9a
 
341f9a
-	if (argc != 1)
341f9a
-		errx (2, "specify one user name to delete");
341f9a
+	if (argc != 1) {
341f9a
+		warnx ("specify one user name to delete");
341f9a
+		return 2;
341f9a
+	}
341f9a
 
341f9a
 	entry = adcli_entry_new_user (conn, argv[0]);
341f9a
-	if (entry == NULL)
341f9a
-		errx (-1, "unexpected memory problems");
341f9a
+	if (entry == NULL) {
341f9a
+		warnx ("unexpected memory problems");
341f9a
+		return -1;
341f9a
+	}
341f9a
 
341f9a
 	adcli_conn_set_allowed_login_types (conn, ADCLI_LOGIN_USER_ACCOUNT);
341f9a
 
341f9a
 	res = adcli_conn_connect (conn);
341f9a
 	if (res != ADCLI_SUCCESS) {
341f9a
-		errx (-res, "couldn't connect to %s domain: %s",
341f9a
-		      adcli_conn_get_domain_name (conn),
341f9a
-		      adcli_get_last_error ());
341f9a
+		warnx ("couldn't connect to %s domain: %s",
341f9a
+		       adcli_conn_get_domain_name (conn),
341f9a
+		       adcli_get_last_error ());
341f9a
+		adcli_entry_unref (entry);
341f9a
+		return -res;
341f9a
 	}
341f9a
 
341f9a
 	res = adcli_entry_delete (entry);
341f9a
 	if (res != ADCLI_SUCCESS) {
341f9a
-		errx (-res, "deleting user %s in domain %s failed: %s",
341f9a
-		      adcli_entry_get_sam_name (entry),
341f9a
-		      adcli_conn_get_domain_name (conn),
341f9a
-		      adcli_get_last_error ());
341f9a
+		warnx ("deleting user %s in domain %s failed: %s",
341f9a
+		       adcli_entry_get_sam_name (entry),
341f9a
+		       adcli_conn_get_domain_name (conn),
341f9a
+		       adcli_get_last_error ());
341f9a
+		adcli_entry_unref (entry);
341f9a
+		return -res;
341f9a
 	}
341f9a
 
341f9a
 	adcli_entry_unref (entry);
341f9a
@@ -404,29 +425,41 @@ adcli_tool_group_create (adcli_conn *conn,
341f9a
 	argc -= optind;
341f9a
 	argv += optind;
341f9a
 
341f9a
-	if (argc != 1)
341f9a
-		errx (2, "specify one group to create");
341f9a
+	if (argc != 1) {
341f9a
+		warnx ("specify one group to create");
341f9a
+		adcli_attrs_free (attrs);
341f9a
+		return 2;
341f9a
+	}
341f9a
 
341f9a
 	entry = adcli_entry_new_group (conn, argv[0]);
341f9a
-	if (entry == NULL)
341f9a
-		errx (-1, "unexpected memory problems");
341f9a
+	if (entry == NULL) {
341f9a
+		warnx ("unexpected memory problems");
341f9a
+		adcli_attrs_free (attrs);
341f9a
+		return -1;
341f9a
+	}
341f9a
 	adcli_entry_set_domain_ou (entry, ou);
341f9a
 
341f9a
 	adcli_conn_set_allowed_login_types (conn, ADCLI_LOGIN_USER_ACCOUNT);
341f9a
 
341f9a
 	res = adcli_conn_connect (conn);
341f9a
 	if (res != ADCLI_SUCCESS) {
341f9a
-		errx (-res, "couldn't connect to domain %s: %s",
341f9a
-		      adcli_conn_get_domain_name (conn),
341f9a
-		      adcli_get_last_error ());
341f9a
+		warnx ("couldn't connect to domain %s: %s",
341f9a
+		       adcli_conn_get_domain_name (conn),
341f9a
+		       adcli_get_last_error ());
341f9a
+		adcli_entry_unref (entry);
341f9a
+		adcli_attrs_free (attrs);
341f9a
+		return -res;
341f9a
 	}
341f9a
 
341f9a
 	res = adcli_entry_create (entry, attrs);
341f9a
 	if (res != ADCLI_SUCCESS) {
341f9a
-		errx (-res, "creating group %s in domain %s failed: %s",
341f9a
-		      adcli_entry_get_sam_name (entry),
341f9a
-		      adcli_conn_get_domain_name (conn),
341f9a
-		      adcli_get_last_error ());
341f9a
+		warnx ("creating group %s in domain %s failed: %s",
341f9a
+		       adcli_entry_get_sam_name (entry),
341f9a
+		       adcli_conn_get_domain_name (conn),
341f9a
+		       adcli_get_last_error ());
341f9a
+		adcli_entry_unref (entry);
341f9a
+		adcli_attrs_free (attrs);
341f9a
+		return -res;
341f9a
 	}
341f9a
 
341f9a
 	adcli_entry_unref (entry);
341f9a
@@ -480,28 +513,36 @@ adcli_tool_group_delete (adcli_conn *conn,
341f9a
 	argc -= optind;
341f9a
 	argv += optind;
341f9a
 
341f9a
-	if (argc != 1)
341f9a
-		errx (2, "specify one group name to delete");
341f9a
+	if (argc != 1) {
341f9a
+		warnx ("specify one group name to delete");
341f9a
+		return 2;
341f9a
+	}
341f9a
 
341f9a
 	entry = adcli_entry_new_group (conn, argv[0]);
341f9a
-	if (entry == NULL)
341f9a
-		errx (-1, "unexpected memory problems");
341f9a
+	if (entry == NULL) {
341f9a
+		warnx ("unexpected memory problems");
341f9a
+		return -1;
341f9a
+	}
341f9a
 
341f9a
 	adcli_conn_set_allowed_login_types (conn, ADCLI_LOGIN_USER_ACCOUNT);
341f9a
 
341f9a
 	res = adcli_conn_connect (conn);
341f9a
 	if (res != ADCLI_SUCCESS) {
341f9a
-		errx (-res, "couldn't connect to %s domain: %s",
341f9a
-		      adcli_conn_get_domain_name (conn),
341f9a
-		      adcli_get_last_error ());
341f9a
+		warnx ("couldn't connect to %s domain: %s",
341f9a
+		       adcli_conn_get_domain_name (conn),
341f9a
+		       adcli_get_last_error ());
341f9a
+		adcli_entry_unref (entry);
341f9a
+		return -res;
341f9a
 	}
341f9a
 
341f9a
 	res = adcli_entry_delete (entry);
341f9a
 	if (res != ADCLI_SUCCESS) {
341f9a
-		errx (-res, "deleting group %s in domain %s failed: %s",
341f9a
-		      adcli_entry_get_sam_name (entry),
341f9a
-		      adcli_conn_get_domain_name (conn),
341f9a
-		      adcli_get_last_error ());
341f9a
+		warnx ("deleting group %s in domain %s failed: %s",
341f9a
+		       adcli_entry_get_sam_name (entry),
341f9a
+		       adcli_conn_get_domain_name (conn),
341f9a
+		       adcli_get_last_error ());
341f9a
+		adcli_entry_unref (entry);
341f9a
+		return -res;
341f9a
 	}
341f9a
 
341f9a
 	adcli_entry_unref (entry);
341f9a
@@ -509,7 +550,7 @@ adcli_tool_group_delete (adcli_conn *conn,
341f9a
 	return 0;
341f9a
 }
341f9a
 
341f9a
-static void
341f9a
+static int
341f9a
 expand_user_dn_as_member (adcli_conn *conn,
341f9a
                           adcli_attrs *attrs,
341f9a
                           const char *user,
341f9a
@@ -523,16 +564,19 @@ expand_user_dn_as_member (adcli_conn *conn,
341f9a
 
341f9a
 	res = adcli_entry_load (entry);
341f9a
 	if (res != ADCLI_SUCCESS) {
341f9a
-		errx (-res, "couldn't lookup user %s in domain %s: %s",
341f9a
-		      user, adcli_conn_get_domain_name (conn),
341f9a
-		      adcli_get_last_error ());
341f9a
+		warnx ("couldn't lookup user %s in domain %s: %s",
341f9a
+		       user, adcli_conn_get_domain_name (conn),
341f9a
+		       adcli_get_last_error ());
341f9a
+		adcli_entry_unref (entry);
341f9a
+		return -res;
341f9a
 	}
341f9a
 
341f9a
 	dn = adcli_entry_get_dn (entry);
341f9a
 	if (dn == NULL) {
341f9a
-		errx (-ADCLI_ERR_CONFIG,
341f9a
-		      "couldn't found user %s in domain %s",
341f9a
-		      user, adcli_conn_get_domain_name (conn));
341f9a
+		warnx ("couldn't found user %s in domain %s",
341f9a
+		       user, adcli_conn_get_domain_name (conn));
341f9a
+		adcli_entry_unref (entry);
341f9a
+		return -ADCLI_ERR_CONFIG;
341f9a
 	}
341f9a
 
341f9a
 	if (adding)
341f9a
@@ -541,6 +585,8 @@ expand_user_dn_as_member (adcli_conn *conn,
341f9a
 		adcli_attrs_delete1 (attrs, "member", dn);
341f9a
 
341f9a
 	adcli_entry_unref (entry);
341f9a
+
341f9a
+	return ADCLI_SUCCESS;
341f9a
 }
341f9a
 
341f9a
 int
341f9a
@@ -590,33 +636,48 @@ adcli_tool_member_add (adcli_conn *conn,
341f9a
 	argc -= optind;
341f9a
 	argv += optind;
341f9a
 
341f9a
-	if (argc < 2)
341f9a
-		errx (2, "specify a group name and a user to add");
341f9a
+	if (argc < 2) {
341f9a
+		warnx ("specify a group name and a user to add");
341f9a
+		return 2;
341f9a
+	}
341f9a
 
341f9a
 	entry = adcli_entry_new_group (conn, argv[0]);
341f9a
-	if (entry == NULL)
341f9a
-		errx (-1, "unexpected memory problems");
341f9a
+	if (entry == NULL) {
341f9a
+		warnx ("unexpected memory problems");
341f9a
+		return -1;
341f9a
+	}
341f9a
 
341f9a
 	adcli_conn_set_allowed_login_types (conn, ADCLI_LOGIN_USER_ACCOUNT);
341f9a
 
341f9a
 	res = adcli_conn_connect (conn);
341f9a
 	if (res != ADCLI_SUCCESS) {
341f9a
-		errx (-res, "couldn't connect to %s domain: %s",
341f9a
-		      adcli_conn_get_domain_name (conn),
341f9a
-		      adcli_get_last_error ());
341f9a
+		warnx ("couldn't connect to %s domain: %s",
341f9a
+		       adcli_conn_get_domain_name (conn),
341f9a
+		       adcli_get_last_error ());
341f9a
+		adcli_entry_unref (entry);
341f9a
+		return -res;
341f9a
 	}
341f9a
 
341f9a
 	attrs = adcli_attrs_new ();
341f9a
 
341f9a
-	for (i = 1; i < argc; i++)
341f9a
-		expand_user_dn_as_member (conn, attrs, argv[i], 1);
341f9a
+	for (i = 1; i < argc; i++) {
341f9a
+		res = expand_user_dn_as_member (conn, attrs, argv[i], 1);
341f9a
+		if (res != ADCLI_SUCCESS) {
341f9a
+			adcli_attrs_free (attrs);
341f9a
+			adcli_entry_unref (entry);
341f9a
+			return res;
341f9a
+		}
341f9a
+	}
341f9a
 
341f9a
 	res = adcli_entry_modify (entry, attrs);
341f9a
 	if (res != ADCLI_SUCCESS) {
341f9a
-		errx (-res, "adding member(s) to group %s in domain %s failed: %s",
341f9a
-		      adcli_entry_get_sam_name (entry),
341f9a
-		      adcli_conn_get_domain_name (conn),
341f9a
-		      adcli_get_last_error ());
341f9a
+		warnx ("adding member(s) to group %s in domain %s failed: %s",
341f9a
+		       adcli_entry_get_sam_name (entry),
341f9a
+		       adcli_conn_get_domain_name (conn),
341f9a
+		       adcli_get_last_error ());
341f9a
+		adcli_attrs_free (attrs);
341f9a
+		adcli_entry_unref (entry);
341f9a
+		return -res;
341f9a
 	}
341f9a
 
341f9a
 	adcli_attrs_free (attrs);
341f9a
@@ -672,33 +733,48 @@ adcli_tool_member_remove (adcli_conn *conn,
341f9a
 	argc -= optind;
341f9a
 	argv += optind;
341f9a
 
341f9a
-	if (argc < 2)
341f9a
-		errx (2, "specify a group name and a user to remove");
341f9a
+	if (argc < 2) {
341f9a
+		warnx ("specify a group name and a user to remove");
341f9a
+		return 2;
341f9a
+	}
341f9a
 
341f9a
 	entry = adcli_entry_new_group (conn, argv[0]);
341f9a
-	if (entry == NULL)
341f9a
-		errx (-1, "unexpected memory problems");
341f9a
+	if (entry == NULL) {
341f9a
+		warnx ("unexpected memory problems");
341f9a
+		return -1;
341f9a
+	}
341f9a
 
341f9a
 	adcli_conn_set_allowed_login_types (conn, ADCLI_LOGIN_USER_ACCOUNT);
341f9a
 
341f9a
 	res = adcli_conn_connect (conn);
341f9a
 	if (res != ADCLI_SUCCESS) {
341f9a
-		errx (-res, "couldn't connect to %s domain: %s",
341f9a
-		      adcli_conn_get_domain_name (conn),
341f9a
-		      adcli_get_last_error ());
341f9a
+		warnx ("couldn't connect to %s domain: %s",
341f9a
+		       adcli_conn_get_domain_name (conn),
341f9a
+		       adcli_get_last_error ());
341f9a
+		adcli_entry_unref (entry);
341f9a
+		return -res;
341f9a
 	}
341f9a
 
341f9a
 	attrs = adcli_attrs_new ();
341f9a
 
341f9a
-	for (i = 1; i < argc; i++)
341f9a
-		expand_user_dn_as_member (conn, attrs, argv[i], 0);
341f9a
+	for (i = 1; i < argc; i++) {
341f9a
+		res = expand_user_dn_as_member (conn, attrs, argv[i], 0);
341f9a
+		if (res != ADCLI_SUCCESS) {
341f9a
+			adcli_attrs_free (attrs);
341f9a
+			adcli_entry_unref (entry);
341f9a
+			return res;
341f9a
+		}
341f9a
+	}
341f9a
 
341f9a
 	res = adcli_entry_modify (entry, attrs);
341f9a
 	if (res != ADCLI_SUCCESS) {
341f9a
-		errx (-res, "adding member(s) to group %s in domain %s failed: %s",
341f9a
-		      adcli_entry_get_sam_name (entry),
341f9a
-		      adcli_conn_get_domain_name (conn),
341f9a
-		      adcli_get_last_error ());
341f9a
+		warnx ("adding member(s) to group %s in domain %s failed: %s",
341f9a
+		       adcli_entry_get_sam_name (entry),
341f9a
+		       adcli_conn_get_domain_name (conn),
341f9a
+		       adcli_get_last_error ());
341f9a
+		adcli_attrs_free (attrs);
341f9a
+		adcli_entry_unref (entry);
341f9a
+		return -res;
341f9a
 	}
341f9a
 
341f9a
 	adcli_attrs_free (attrs);
341f9a
-- 
341f9a
2.20.1
341f9a