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

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