blob: dcd79044ae97e1d8543513382edd68430996fb5b [file] [log] [blame]
Andrew Geisslerbffdb3e2020-08-21 16:13:29 -05001From 57bd719af1f138f44f71b2078995452582da0da6 Mon Sep 17 00:00:00 2001
2From: Martin Schwenke <martin@meltin.net>
3Date: Fri, 5 Jun 2020 21:52:23 +1000
4Subject: [PATCH 2/3] util: Fix build on FreeBSD by avoiding NSS_BUFLEN_PASSWD
5
6NSS_BUFLEN_PASSWD is not defined on FreeBSD. Use
7sysconf(_SC_GETPW_R_SIZE_MAX) instead, as per POSIX.
8
9Use a dynamically allocated buffer instead of trying to cram all of
10the logic into the declarations. This will come in useful later
11anyway.
12
13Signed-off-by: Martin Schwenke <martin@meltin.net>
14Reviewed-by: Volker Lendecke <vl@samba.org>
15Reviewed-by: Bjoern Jacke <bjacke@samba.org>
16(cherry picked from commit 847208cd8ac68c4c7d1dae63767820db1c69292b)
17
18Upstream-Status:Backport
19[https://gitlab.com/samba-team/samba/-/commit/57bd719af1f138f44f71b2078995452582da0da6]
20
21Signed-off-by: Yi Zhao <yi.zhao@windriver.com>
22---
23 lib/util/util_paths.c | 27 ++++++++++++++++++++++-----
24 1 file changed, 22 insertions(+), 5 deletions(-)
25
26diff --git a/lib/util/util_paths.c b/lib/util/util_paths.c
27index dec91772d9e..9bc6df37e5d 100644
28--- a/lib/util/util_paths.c
29+++ b/lib/util/util_paths.c
30@@ -68,24 +68,41 @@ static char *get_user_home_dir(TALLOC_CTX *mem_ctx)
31 {
32 struct passwd pwd = {0};
33 struct passwd *pwdbuf = NULL;
34- char buf[NSS_BUFLEN_PASSWD] = {0};
35+ char *buf = NULL;
36+ char *out = NULL;
37+ long int initlen;
38 size_t len;
39 int rc;
40
41- rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf);
42+ initlen = sysconf(_SC_GETPW_R_SIZE_MAX);
43+ if (initlen == -1) {
44+ len = 1024;
45+ } else {
46+ len = (size_t)initlen;
47+ }
48+ buf = talloc_size(mem_ctx, len);
49+ if (buf == NULL) {
50+ return NULL;
51+ }
52+
53+ rc = getpwuid_r(getuid(), &pwd, buf, len, &pwdbuf);
54 if (rc != 0 || pwdbuf == NULL ) {
55 const char *szPath = getenv("HOME");
56 if (szPath == NULL) {
57- return NULL;
58+ goto done;
59 }
60 len = strnlen(szPath, PATH_MAX);
61 if (len >= PATH_MAX) {
62 return NULL;
63 }
64- return talloc_strdup(mem_ctx, szPath);
65+ out = talloc_strdup(mem_ctx, szPath);
66+ goto done;
67 }
68
69- return talloc_strdup(mem_ctx, pwd.pw_dir);
70+ out = talloc_strdup(mem_ctx, pwd.pw_dir);
71+done:
72+ TALLOC_FREE(buf);
73+ return out;
74 }
75
76 char *path_expand_tilde(TALLOC_CTX *mem_ctx, const char *d)
77--
782.17.1
79