auth4: Fix CID 1034877 Resource leak
[Samba.git] / lib / util / util_paths.c
blobb35cc7f5863b7968965d758e4c7c7642ac2dd2da
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001-2007
6 Copyright (C) Simo Sorce 2001
7 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
8 Copyright (C) James Peach 2006
9 Copyright (c) 2020 Andreas Schneider <asn@samba.org>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "replace.h"
26 #include "dynconfig/dynconfig.h"
27 #include "lib/util/util_paths.h"
28 #include "system/passwd.h"
29 #include "system/filesys.h"
31 /**
32 * @brief Returns an absolute path to a file in the Samba modules directory.
34 * @param name File to find, relative to MODULESDIR.
36 * @retval Pointer to a string containing the full path.
37 **/
39 char *modules_path(TALLOC_CTX *mem_ctx, const char *name)
41 return talloc_asprintf(mem_ctx, "%s/%s", get_dyn_MODULESDIR(), name);
44 /**
45 * @brief Returns an absolute path to a file in the Samba data directory.
47 * @param name File to find, relative to CODEPAGEDIR.
49 * @retval Pointer to a talloc'ed string containing the full path.
50 **/
52 char *data_path(TALLOC_CTX *mem_ctx, const char *name)
54 return talloc_asprintf(mem_ctx, "%s/%s", get_dyn_CODEPAGEDIR(), name);
57 /**
58 * @brief Returns the platform specific shared library extension.
60 * @retval Pointer to a const char * containing the extension.
61 **/
63 const char *shlib_ext(void)
65 return get_dyn_SHLIBEXT();
68 static char *get_user_home_dir(TALLOC_CTX *mem_ctx)
70 struct passwd pwd = {0};
71 struct passwd *pwdbuf = NULL;
72 char *buf = NULL;
73 char *out = NULL;
74 long int initlen;
75 size_t len;
76 int rc;
78 initlen = sysconf(_SC_GETPW_R_SIZE_MAX);
79 if (initlen == -1) {
80 len = 1024;
81 } else {
82 len = (size_t)initlen;
84 buf = talloc_size(mem_ctx, len);
85 if (buf == NULL) {
86 return NULL;
89 rc = getpwuid_r(getuid(), &pwd, buf, len, &pwdbuf);
90 while (rc == ERANGE) {
91 size_t newlen = 2 * len;
92 char *tmp = NULL;
93 if (newlen < len) {
94 /* Overflow */
95 goto done;
97 len = newlen;
98 tmp = talloc_realloc_size(mem_ctx, buf, len);
99 if (tmp == NULL) {
100 goto done;
102 buf = tmp;
103 rc = getpwuid_r(getuid(), &pwd, buf, len, &pwdbuf);
105 if (rc != 0 || pwdbuf == NULL ) {
106 const char *szPath = getenv("HOME");
107 if (szPath == NULL) {
108 goto done;
110 len = strnlen(szPath, PATH_MAX);
111 if (len >= PATH_MAX) {
112 goto done;
114 out = talloc_strdup(mem_ctx, szPath);
115 goto done;
118 out = talloc_strdup(mem_ctx, pwd.pw_dir);
119 done:
120 TALLOC_FREE(buf);
121 return out;
124 char *path_expand_tilde(TALLOC_CTX *mem_ctx, const char *d)
126 char *h = NULL, *r = NULL;
127 const char *p = NULL;
128 struct stat sb = {0};
129 int rc;
131 if (d[0] != '~') {
132 return talloc_strdup(mem_ctx, d);
134 d++;
136 /* handle ~user/path */
137 p = strchr(d, '/');
138 if (p != NULL && p > d) {
139 struct passwd *pw;
140 size_t s = p - d;
141 char u[128];
143 if (s >= sizeof(u)) {
144 return NULL;
146 memcpy(u, d, s);
147 u[s] = '\0';
149 pw = getpwnam(u);
150 if (pw == NULL) {
151 return NULL;
153 h = talloc_strdup(mem_ctx, pw->pw_dir);
154 } else {
155 p = d;
156 h = get_user_home_dir(mem_ctx);
158 if (h == NULL) {
159 return NULL;
162 rc = stat(h, &sb);
163 if (rc != 0) {
164 TALLOC_FREE(h);
165 return NULL;
168 r = talloc_asprintf(mem_ctx, "%s%s", h, p);
169 TALLOC_FREE(h);
171 return r;