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

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