1*03514f19SPatrick WilliamsFrom b7c827bb44edbb6251c9fcdb80aa03982c0e7bf3 Mon Sep 17 00:00:00 2001
2*03514f19SPatrick WilliamsFrom: Alex Kiernan <alex.kiernan@gmail.com>
3*03514f19SPatrick WilliamsDate: Tue, 10 Mar 2020 11:05:20 +0000
4*03514f19SPatrick WilliamsSubject: [PATCH 14/22] Handle missing gshadow
5*03514f19SPatrick Williams
6*03514f19SPatrick Williamsgshadow usage is now present in the userdb code. Mask all uses of it to
7*03514f19SPatrick Williamsallow compilation on musl
8*03514f19SPatrick Williams
9*03514f19SPatrick WilliamsUpstream-Status: Inappropriate [musl specific]
10*03514f19SPatrick WilliamsSigned-off-by: Alex Kiernan <alex.kiernan@gmail.com>
11*03514f19SPatrick Williams[Rebased for v247]
12*03514f19SPatrick WilliamsSigned-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
13*03514f19SPatrick Williams---
14*03514f19SPatrick Williams src/shared/user-record-nss.c | 20 ++++++++++++++++++++
15*03514f19SPatrick Williams src/shared/user-record-nss.h |  4 ++++
16*03514f19SPatrick Williams src/shared/userdb.c          |  7 ++++++-
17*03514f19SPatrick Williams 3 files changed, 30 insertions(+), 1 deletion(-)
18*03514f19SPatrick Williams
19*03514f19SPatrick Williamsdiff --git a/src/shared/user-record-nss.c b/src/shared/user-record-nss.c
20*03514f19SPatrick Williamsindex 414a49331b..1a4e1b628c 100644
21*03514f19SPatrick Williams--- a/src/shared/user-record-nss.c
22*03514f19SPatrick Williams+++ b/src/shared/user-record-nss.c
23*03514f19SPatrick Williams@@ -329,8 +329,10 @@ int nss_group_to_group_record(
24*03514f19SPatrick Williams         if (isempty(grp->gr_name))
25*03514f19SPatrick Williams                 return -EINVAL;
26*03514f19SPatrick Williams
27*03514f19SPatrick Williams+#if ENABLE_GSHADOW
28*03514f19SPatrick Williams         if (sgrp && !streq_ptr(sgrp->sg_namp, grp->gr_name))
29*03514f19SPatrick Williams                 return -EINVAL;
30*03514f19SPatrick Williams+#endif
31*03514f19SPatrick Williams
32*03514f19SPatrick Williams         g = group_record_new();
33*03514f19SPatrick Williams         if (!g)
34*03514f19SPatrick Williams@@ -346,6 +348,7 @@ int nss_group_to_group_record(
35*03514f19SPatrick Williams
36*03514f19SPatrick Williams         g->gid = grp->gr_gid;
37*03514f19SPatrick Williams
38*03514f19SPatrick Williams+#if ENABLE_GSHADOW
39*03514f19SPatrick Williams         if (sgrp) {
40*03514f19SPatrick Williams                 if (looks_like_hashed_password(utf8_only(sgrp->sg_passwd))) {
41*03514f19SPatrick Williams                         g->hashed_password = strv_new(sgrp->sg_passwd);
42*03514f19SPatrick Williams@@ -361,6 +364,7 @@ int nss_group_to_group_record(
43*03514f19SPatrick Williams                 if (r < 0)
44*03514f19SPatrick Williams                         return r;
45*03514f19SPatrick Williams         }
46*03514f19SPatrick Williams+#endif
47*03514f19SPatrick Williams
48*03514f19SPatrick Williams         r = json_build(&g->json, JSON_BUILD_OBJECT(
49*03514f19SPatrick Williams                                        JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(g->group_name)),
50*03514f19SPatrick Williams@@ -387,6 +391,7 @@ int nss_sgrp_for_group(const struct group *grp, struct sgrp *ret_sgrp, char **re
51*03514f19SPatrick Williams         assert(ret_sgrp);
52*03514f19SPatrick Williams         assert(ret_buffer);
53*03514f19SPatrick Williams
54*03514f19SPatrick Williams+#if ENABLE_GSHADOW
55*03514f19SPatrick Williams         for (;;) {
56*03514f19SPatrick Williams                 _cleanup_free_ char *buf = NULL;
57*03514f19SPatrick Williams                 struct sgrp sgrp, *result;
58*03514f19SPatrick Williams@@ -415,6 +420,9 @@ int nss_sgrp_for_group(const struct group *grp, struct sgrp *ret_sgrp, char **re
59*03514f19SPatrick Williams                 buflen *= 2;
60*03514f19SPatrick Williams                 buf = mfree(buf);
61*03514f19SPatrick Williams         }
62*03514f19SPatrick Williams+#else
63*03514f19SPatrick Williams+        return -ESRCH;
64*03514f19SPatrick Williams+#endif
65*03514f19SPatrick Williams }
66*03514f19SPatrick Williams
67*03514f19SPatrick Williams int nss_group_record_by_name(
68*03514f19SPatrick Williams@@ -426,7 +434,9 @@ int nss_group_record_by_name(
69*03514f19SPatrick Williams         struct group grp, *result;
70*03514f19SPatrick Williams         bool incomplete = false;
71*03514f19SPatrick Williams         size_t buflen = 4096;
72*03514f19SPatrick Williams+#if ENABLE_GSHADOW
73*03514f19SPatrick Williams         struct sgrp sgrp, *sresult = NULL;
74*03514f19SPatrick Williams+#endif
75*03514f19SPatrick Williams         int r;
76*03514f19SPatrick Williams
77*03514f19SPatrick Williams         assert(name);
78*03514f19SPatrick Williams@@ -455,6 +465,7 @@ int nss_group_record_by_name(
79*03514f19SPatrick Williams                 buf = mfree(buf);
80*03514f19SPatrick Williams         }
81*03514f19SPatrick Williams
82*03514f19SPatrick Williams+#if ENABLE_GSHADOW
83*03514f19SPatrick Williams         if (with_shadow) {
84*03514f19SPatrick Williams                 r = nss_sgrp_for_group(result, &sgrp, &sbuf);
85*03514f19SPatrick Williams                 if (r < 0) {
86*03514f19SPatrick Williams@@ -466,6 +477,9 @@ int nss_group_record_by_name(
87*03514f19SPatrick Williams                 incomplete = true;
88*03514f19SPatrick Williams
89*03514f19SPatrick Williams         r = nss_group_to_group_record(result, sresult, ret);
90*03514f19SPatrick Williams+#else
91*03514f19SPatrick Williams+        r = nss_group_to_group_record(result, NULL, ret);
92*03514f19SPatrick Williams+#endif
93*03514f19SPatrick Williams         if (r < 0)
94*03514f19SPatrick Williams                 return r;
95*03514f19SPatrick Williams
96*03514f19SPatrick Williams@@ -483,7 +497,9 @@ int nss_group_record_by_gid(
97*03514f19SPatrick Williams         struct group grp, *result;
98*03514f19SPatrick Williams         bool incomplete = false;
99*03514f19SPatrick Williams         size_t buflen = 4096;
100*03514f19SPatrick Williams+#if ENABLE_GSHADOW
101*03514f19SPatrick Williams         struct sgrp sgrp, *sresult = NULL;
102*03514f19SPatrick Williams+#endif
103*03514f19SPatrick Williams         int r;
104*03514f19SPatrick Williams
105*03514f19SPatrick Williams         for (;;) {
106*03514f19SPatrick Williams@@ -509,6 +525,7 @@ int nss_group_record_by_gid(
107*03514f19SPatrick Williams                 buf = mfree(buf);
108*03514f19SPatrick Williams         }
109*03514f19SPatrick Williams
110*03514f19SPatrick Williams+#if ENABLE_GSHADOW
111*03514f19SPatrick Williams         if (with_shadow) {
112*03514f19SPatrick Williams                 r = nss_sgrp_for_group(result, &sgrp, &sbuf);
113*03514f19SPatrick Williams                 if (r < 0) {
114*03514f19SPatrick Williams@@ -520,6 +537,9 @@ int nss_group_record_by_gid(
115*03514f19SPatrick Williams                 incomplete = true;
116*03514f19SPatrick Williams
117*03514f19SPatrick Williams         r = nss_group_to_group_record(result, sresult, ret);
118*03514f19SPatrick Williams+#else
119*03514f19SPatrick Williams+        r = nss_group_to_group_record(result, NULL, ret);
120*03514f19SPatrick Williams+#endif
121*03514f19SPatrick Williams         if (r < 0)
122*03514f19SPatrick Williams                 return r;
123*03514f19SPatrick Williams
124*03514f19SPatrick Williamsdiff --git a/src/shared/user-record-nss.h b/src/shared/user-record-nss.h
125*03514f19SPatrick Williamsindex 22ab04d6ee..4e52e7a911 100644
126*03514f19SPatrick Williams--- a/src/shared/user-record-nss.h
127*03514f19SPatrick Williams+++ b/src/shared/user-record-nss.h
128*03514f19SPatrick Williams@@ -2,7 +2,11 @@
129*03514f19SPatrick Williams #pragma once
130*03514f19SPatrick Williams
131*03514f19SPatrick Williams #include <grp.h>
132*03514f19SPatrick Williams+#if ENABLE_GSHADOW
133*03514f19SPatrick Williams #include <gshadow.h>
134*03514f19SPatrick Williams+#else
135*03514f19SPatrick Williams+struct sgrp;
136*03514f19SPatrick Williams+#endif
137*03514f19SPatrick Williams #include <pwd.h>
138*03514f19SPatrick Williams #include <shadow.h>
139*03514f19SPatrick Williams
140*03514f19SPatrick Williamsdiff --git a/src/shared/userdb.c b/src/shared/userdb.c
141*03514f19SPatrick Williamsindex f60d48ace4..e878199a28 100644
142*03514f19SPatrick Williams--- a/src/shared/userdb.c
143*03514f19SPatrick Williams+++ b/src/shared/userdb.c
144*03514f19SPatrick Williams@@ -1038,13 +1038,15 @@ int groupdb_iterator_get(UserDBIterator *iterator, GroupRecord **ret) {
145*03514f19SPatrick Williams                 if (gr) {
146*03514f19SPatrick Williams                         _cleanup_free_ char *buffer = NULL;
147*03514f19SPatrick Williams                         bool incomplete = false;
148*03514f19SPatrick Williams+#if ENABLE_GSHADOW
149*03514f19SPatrick Williams                         struct sgrp sgrp;
150*03514f19SPatrick Williams-
151*03514f19SPatrick Williams+#endif
152*03514f19SPatrick Williams                         if (streq_ptr(gr->gr_name, "root"))
153*03514f19SPatrick Williams                                 iterator->synthesize_root = false;
154*03514f19SPatrick Williams                         if (gr->gr_gid == GID_NOBODY)
155*03514f19SPatrick Williams                                 iterator->synthesize_nobody = false;
156*03514f19SPatrick Williams
157*03514f19SPatrick Williams+#if ENABLE_GSHADOW
158*03514f19SPatrick Williams                         if (!FLAGS_SET(iterator->flags, USERDB_SUPPRESS_SHADOW)) {
159*03514f19SPatrick Williams                                 r = nss_sgrp_for_group(gr, &sgrp, &buffer);
160*03514f19SPatrick Williams                                 if (r < 0) {
161*03514f19SPatrick Williams@@ -1057,6 +1059,9 @@ int groupdb_iterator_get(UserDBIterator *iterator, GroupRecord **ret) {
162*03514f19SPatrick Williams                         }
163*03514f19SPatrick Williams
164*03514f19SPatrick Williams                         r = nss_group_to_group_record(gr, r >= 0 ? &sgrp : NULL, ret);
165*03514f19SPatrick Williams+#else
166*03514f19SPatrick Williams+                        r = nss_group_to_group_record(gr, NULL, ret);
167*03514f19SPatrick Williams+#endif
168*03514f19SPatrick Williams                         if (r < 0)
169*03514f19SPatrick Williams                                 return r;
170*03514f19SPatrick Williams
171*03514f19SPatrick Williams--
172*03514f19SPatrick Williams2.34.1
173*03514f19SPatrick Williams
174