From 6018d55f5fcab127fef9acbd250cdc185983ba59 Mon Sep 17 00:00:00 2001
From: Alex Kiernan <alex.kiernan@gmail.com>
Date: Tue, 10 Mar 2020 11:05:20 +0000
Subject: [PATCH 14/16] Handle missing gshadow for musl

gshadow usage is now present in the userdb code. Mask all uses of it to
allow compilation on musl

Upstream-Status: Inappropriate [musl specific]
Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
[Rebased for v247]
Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
[Rebased for v258]
Signed-off-by: Chen Qi <Qi.Chen@windriver.com>
---
 src/include/override/gshadow.h | 10 ++++++++++
 src/shared/user-record-nss.c   | 20 ++++++++++++++++++++
 src/shared/userdb.c            |  7 ++++++-
 3 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 src/include/override/gshadow.h

diff --git a/src/include/override/gshadow.h b/src/include/override/gshadow.h
new file mode 100644
index 0000000000..568a9bb2d9
--- /dev/null
+++ b/src/include/override/gshadow.h
@@ -0,0 +1,10 @@
+#ifndef _GSHADOW_H
+#define _GSHADOW_H    1
+
+#if ENABLE_GSHADOW
+#include_next <gshadow.h>
+#else
+struct sgrp;
+#endif
+
+#endif /* gshadow.h */
diff --git a/src/shared/user-record-nss.c b/src/shared/user-record-nss.c
index 10a5a75e9f..24db7c953d 100644
--- a/src/shared/user-record-nss.c
+++ b/src/shared/user-record-nss.c
@@ -296,8 +296,10 @@ int nss_group_to_group_record(
         if (isempty(grp->gr_name))
                 return -EINVAL;
 
+#if ENABLE_GSHADOW
         if (sgrp && !streq_ptr(sgrp->sg_namp, grp->gr_name))
                 return -EINVAL;
+#endif
 
         g = group_record_new();
         if (!g)
@@ -313,6 +315,7 @@ int nss_group_to_group_record(
 
         g->gid = grp->gr_gid;
 
+#if ENABLE_GSHADOW
         if (sgrp) {
                 if (looks_like_hashed_password(utf8_only(sgrp->sg_passwd))) {
                         g->hashed_password = strv_new(sgrp->sg_passwd);
@@ -328,6 +331,7 @@ int nss_group_to_group_record(
                 if (r < 0)
                         return r;
         }
+#endif
 
         r = sd_json_buildo(
                         &g->json,
@@ -355,6 +359,7 @@ int nss_sgrp_for_group(const struct group *grp, struct sgrp *ret_sgrp, char **re
         assert(ret_sgrp);
         assert(ret_buffer);
 
+#if ENABLE_GSHADOW
         for (;;) {
                 _cleanup_free_ char *buf = NULL;
                 struct sgrp sgrp = {}, *result = NULL;
@@ -383,6 +388,9 @@ int nss_sgrp_for_group(const struct group *grp, struct sgrp *ret_sgrp, char **re
                 buflen *= 2;
                 buf = mfree(buf);
         }
+#else
+        return -ESRCH;
+#endif
 }
 
 int nss_group_record_by_name(
@@ -393,7 +401,9 @@ int nss_group_record_by_name(
         _cleanup_free_ char *sbuf = NULL;
         _cleanup_free_ struct group *result = NULL;
         bool incomplete = false;
+#if ENABLE_GSHADOW
         struct sgrp sgrp, *sresult = NULL;
+#endif
         int r;
 
         assert(name);
@@ -402,6 +412,7 @@ int nss_group_record_by_name(
         if (r < 0)
                 return r;
 
+#if ENABLE_GSHADOW
         if (with_shadow) {
                 r = nss_sgrp_for_group(result, &sgrp, &sbuf);
                 if (r < 0) {
@@ -413,6 +424,9 @@ int nss_group_record_by_name(
                 incomplete = true;
 
         r = nss_group_to_group_record(result, sresult, ret);
+#else
+        r = nss_group_to_group_record(result, NULL, ret);
+#endif
         if (r < 0)
                 return r;
 
@@ -429,13 +443,16 @@ int nss_group_record_by_gid(
         _cleanup_free_ char *sbuf = NULL;
         _cleanup_free_ struct group *result = NULL;
         bool incomplete = false;
+#if ENABLE_GSHADOW
         struct sgrp sgrp, *sresult = NULL;
+#endif
         int r;
 
         r = getgrgid_malloc(gid, &result);
         if (r < 0)
                 return r;
 
+#if ENABLE_GSHADOW
         if (with_shadow) {
                 r = nss_sgrp_for_group(result, &sgrp, &sbuf);
                 if (r < 0) {
@@ -447,6 +464,9 @@ int nss_group_record_by_gid(
                 incomplete = true;
 
         r = nss_group_to_group_record(result, sresult, ret);
+#else
+        r = nss_group_to_group_record(result, NULL, ret);
+#endif
         if (r < 0)
                 return r;
 
diff --git a/src/shared/userdb.c b/src/shared/userdb.c
index 49850ff216..f4217c0971 100644
--- a/src/shared/userdb.c
+++ b/src/shared/userdb.c
@@ -1551,13 +1551,15 @@ static int groupdb_iterator_get_one(UserDBIterator *iterator, GroupRecord **ret)
                 if (gr) {
                         _cleanup_free_ char *buffer = NULL;
                         bool incomplete = false;
+#if ENABLE_GSHADOW
                         struct sgrp sgrp;
-
+#endif
                         if (streq_ptr(gr->gr_name, "root"))
                                 iterator->synthesize_root = false;
                         if (gr->gr_gid == GID_NOBODY)
                                 iterator->synthesize_nobody = false;
 
+#if ENABLE_GSHADOW
                         if (!FLAGS_SET(iterator->flags, USERDB_SUPPRESS_SHADOW)) {
                                 r = nss_sgrp_for_group(gr, &sgrp, &buffer);
                                 if (r < 0) {
@@ -1570,6 +1572,9 @@ static int groupdb_iterator_get_one(UserDBIterator *iterator, GroupRecord **ret)
                         }
 
                         r = nss_group_to_group_record(gr, r >= 0 ? &sgrp : NULL, ret);
+#else
+                        r = nss_group_to_group_record(gr, NULL, ret);
+#endif
                         if (r < 0)
                                 return r;
 
-- 
2.34.1

