aboutsummaryrefslogtreecommitdiffstats
path: root/testing/libsemanage/0001-libsemanage-remove-use-of-getpwent_r.patch
blob: 9547e8d0d44b251d7f6ef74cad9354953ac1e414 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
From 687398b9df5a8fc2d17bd9d83e067812980ec853 Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho@docker.com>
Date: Tue, 14 Mar 2017 13:57:35 -0700
Subject: [PATCH] libsemanage: remove use of getpwent_r

getpwent_r isn't really re-entrant, and the _r version is a GNU extension,
which won't work with libcs that don't implement it. Let's just use
getpwent instead.

Signed-off-by: Tycho Andersen <tycho@docker.com>
---
 src/genhomedircon.c | 54 ++++++++++++++++++-----------------------
 1 file changed, 24 insertions(+), 30 deletions(-)

diff --git a/src/genhomedircon.c b/libsemanage/src/genhomedircon.c
index 465dd88..839280b 100644
--- a/src/genhomedircon.c
+++ b/src/genhomedircon.c
@@ -290,14 +290,11 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s)
 	semanage_list_t *homedir_list = NULL;
 	semanage_list_t *shells = NULL;
 	fc_match_handle_t hand;
-	char *rbuf = NULL;
 	char *path = NULL;
-	long rbuflen;
 	uid_t temp, minuid = 500, maxuid = 60000;
 	int minuid_set = 0;
-	struct passwd pwstorage, *pwbuf;
+	struct passwd *pwbuf;
 	struct stat buf;
-	int retval;
 
 	path = semanage_findval(PATH_ETC_USERADD, "HOME", "=");
 	if (path && *path) {
@@ -362,14 +359,18 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s)
 	free(path);
 	path = NULL;
 
-	rbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
-	if (rbuflen <= 0)
-		goto fail;
-	rbuf = malloc(rbuflen);
-	if (rbuf == NULL)
-		goto fail;
 	setpwent();
-	while ((retval = getpwent_r(&pwstorage, rbuf, rbuflen, &pwbuf)) == 0) {
+	while (1) {
+		errno = 0;
+		pwbuf = getpwent();
+		if (!pwbuf) {
+			if (errno) {
+				WARN(s->h_semanage, "Error while fetching users.  "
+				     "Returning list so far.");
+			}
+			break;
+		}
+
 		if (pwbuf->pw_uid < minuid || pwbuf->pw_uid > maxuid)
 			continue;
 		if (!semanage_list_find(shells, pwbuf->pw_shell))
@@ -413,23 +414,16 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s)
 		path = NULL;
 	}
 
-	if (retval && retval != ENOENT) {
-		WARN(s->h_semanage, "Error while fetching users.  "
-		     "Returning list so far.");
-	}
-
 	if (semanage_list_sort(&homedir_list))
 		goto fail;
 
 	endpwent();
-	free(rbuf);
 	semanage_list_destroy(&shells);
 
 	return homedir_list;
 
       fail:
 	endpwent();
-	free(rbuf);
 	free(path);
 	semanage_list_destroy(&homedir_list);
 	semanage_list_destroy(&shells);
@@ -1063,9 +1057,7 @@ static int get_group_users(genhomedircon_settings_t * s,
 	char *grbuf = NULL;
 	struct group grstorage, *group = NULL;
 
-	long prbuflen;
-	char *pwbuf = NULL;
-	struct passwd pwstorage, *pw = NULL;
+	struct passwd *pw = NULL;
 
 	grbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
 	if (grbuflen <= 0)
@@ -1102,15 +1094,18 @@ static int get_group_users(genhomedircon_settings_t * s,
 		}
 	}
 
-	prbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
-	if (prbuflen <= 0)
-		goto cleanup;
-	pwbuf = malloc(prbuflen);
-	if (pwbuf == NULL)
-		goto cleanup;
-
 	setpwent();
-	while ((retval = getpwent_r(&pwstorage, pwbuf, prbuflen, &pw)) == 0) {
+	while (1) {
+		errno = 0;
+		pw = getpwent();
+		if (!pw) {
+			if (errno) {
+				WARN(s->h_semanage, "Error while fetching groups.  "
+				     "Returning list so far.");
+			}
+			break;
+		}
+
 		// skip users who also have this group as their
 		// primary group
 		if (lfind(pw->pw_name, group->gr_mem, &nmembers,
@@ -1129,7 +1124,6 @@ static int get_group_users(genhomedircon_settings_t * s,
 	retval = STATUS_SUCCESS;
 cleanup:
 	endpwent();
-	free(pwbuf);
 	free(grbuf);
 
 	return retval;
-- 
2.9.3