From nobody Wed Nov 27 22:28:37 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1536764965174542.8299296159996; Wed, 12 Sep 2018 08:09:25 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A2B94821C3; Wed, 12 Sep 2018 15:09:22 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AE1CE5C20D; Wed, 12 Sep 2018 15:09:21 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id E73B24BB75; Wed, 12 Sep 2018 15:09:19 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w8CF9IZ3003403 for ; Wed, 12 Sep 2018 11:09:18 -0400 Received: by smtp.corp.redhat.com (Postfix) id A454663F70; Wed, 12 Sep 2018 15:09:18 +0000 (UTC) Received: from wheatley (unknown [10.34.244.254]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 618ABFA99B for ; Wed, 12 Sep 2018 15:09:16 +0000 (UTC) Received: from wheatley.brq.redhat.com (wheatley.usersys.redhat.com [127.0.0.1]) by wheatley (Postfix) with ESMTP id 703C37E002E for ; Wed, 12 Sep 2018 17:09:15 +0200 (CEST) From: Martin Kletzander To: libvir-list@redhat.com Date: Wed, 12 Sep 2018 17:09:12 +0200 Message-Id: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 1/2] Add functions for checking if user or group exists X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 12 Sep 2018 15:09:23 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Instead of duplicating the code from virGet{User,Group}IDByName(), which are static anyway, extend those functions to accept NULL pointers for the resul= t and a boolean for controlling the error reporting. Signed-off-by: Martin Kletzander --- Feel free to suggest any other way of doing this, I just felt like this is = the most readable way of doing things src/libvirt_private.syms | 2 ++ src/util/virutil.c | 40 ++++++++++++++++++++++++++++++++-------- src/util/virutil.h | 4 ++++ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index a0d229a79f0c..e7a4d61f25fd 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3090,6 +3090,8 @@ virUSBDeviceSetUsedBy; =20 =20 # util/virutil.h +virDoesGroupExist; +virDoesUserExist; virDoubleToStr; virEnumFromString; virEnumToString; diff --git a/src/util/virutil.c b/src/util/virutil.c index a908422feb7f..2290020e494e 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -971,9 +971,11 @@ char *virGetGroupName(gid_t gid) =20 /* Search in the password database for a user id that matches the user name * `name`. Returns 0 on success, -1 on failure or 1 if name cannot be foun= d. + * + * Warns if @missing_ok is false */ static int -virGetUserIDByName(const char *name, uid_t *uid) +virGetUserIDByName(const char *name, uid_t *uid, bool missing_ok) { char *strbuf =3D NULL; struct passwd pwbuf; @@ -996,7 +998,7 @@ virGetUserIDByName(const char *name, uid_t *uid) } =20 if (!pw) { - if (rc !=3D 0) { + if (rc !=3D 0 && !missing_ok) { char buf[1024]; /* log the possible error from getpwnam_r. Unfortunately error * reporting from this function is bad and we can't really @@ -1009,7 +1011,8 @@ virGetUserIDByName(const char *name, uid_t *uid) goto cleanup; } =20 - *uid =3D pw->pw_uid; + if (uid) + *uid =3D pw->pw_uid; ret =3D 0; =20 cleanup: @@ -1032,7 +1035,7 @@ virGetUserID(const char *user, uid_t *uid) if (*user =3D=3D '+') { user++; } else { - int rc =3D virGetUserIDByName(user, uid); + int rc =3D virGetUserIDByName(user, uid, false); if (rc <=3D 0) return rc; } @@ -1051,9 +1054,11 @@ virGetUserID(const char *user, uid_t *uid) =20 /* Search in the group database for a group id that matches the group name * `name`. Returns 0 on success, -1 on failure or 1 if name cannot be foun= d. + * + * Warns if @missing_ok is false */ static int -virGetGroupIDByName(const char *name, gid_t *gid) +virGetGroupIDByName(const char *name, gid_t *gid, bool missing_ok) { char *strbuf =3D NULL; struct group grbuf; @@ -1076,7 +1081,7 @@ virGetGroupIDByName(const char *name, gid_t *gid) } =20 if (!gr) { - if (rc !=3D 0) { + if (rc !=3D 0 && !missing_ok) { char buf[1024]; /* log the possible error from getgrnam_r. Unfortunately error * reporting from this function is bad and we can't really @@ -1089,7 +1094,8 @@ virGetGroupIDByName(const char *name, gid_t *gid) goto cleanup; } =20 - *gid =3D gr->gr_gid; + if (gid) + *gid =3D gr->gr_gid; ret =3D 0; =20 cleanup: @@ -1112,7 +1118,7 @@ virGetGroupID(const char *group, gid_t *gid) if (*group =3D=3D '+') { group++; } else { - int rc =3D virGetGroupIDByName(group, gid); + int rc =3D virGetGroupIDByName(group, gid, false); if (rc <=3D 0) return rc; } @@ -1129,6 +1135,24 @@ virGetGroupID(const char *group, gid_t *gid) return 0; } =20 +/* Silently checks if User @name exists. + * Returns if the user exists and fallbacks to false on error. + */ +int +virDoesUserExist(const char *name, bool *exists) +{ + return virGetUserIDByName(name, NULL, true) =3D=3D 0; +} + +/* Silently checks if Group @name exists. + * Returns if the group exists and fallbacks to false on error. + */ +int +virDoesGroupExist(const char *name) +{ + return virGetGroupIDByName(name, NULL, true) =3D=3D 0; +} + =20 /* Compute the list of primary and supplementary groups associated * with @uid, and including @gid in the list (unless it is -1), diff --git a/src/util/virutil.h b/src/util/virutil.h index 1ba9635bd991..8a27cca523c8 100644 --- a/src/util/virutil.h +++ b/src/util/virutil.h @@ -152,6 +152,10 @@ int virGetUserID(const char *name, int virGetGroupID(const char *name, gid_t *gid) ATTRIBUTE_RETURN_CHECK; =20 +int virDoesUserExist(const char *name, bool *exists); +int virDoesGroupExist(const char *name, bool *exists); + + bool virIsDevMapperDevice(const char *dev_name) ATTRIBUTE_NONNULL(1); =20 bool virValidateWWN(const char *wwn); --=20 2.18.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list