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/>.
26 #include "dynconfig/dynconfig.h"
27 #include "lib/util/util_paths.h"
28 #include "system/passwd.h"
31 * @brief Returns an absolute path to a file in the Samba modules directory.
33 * @param name File to find, relative to MODULESDIR.
35 * @retval Pointer to a string containing the full path.
38 char *modules_path(TALLOC_CTX
*mem_ctx
, const char *name
)
40 return talloc_asprintf(mem_ctx
, "%s/%s", get_dyn_MODULESDIR(), name
);
44 * @brief Returns an absolute path to a file in the Samba data directory.
46 * @param name File to find, relative to CODEPAGEDIR.
48 * @retval Pointer to a talloc'ed string containing the full path.
51 char *data_path(TALLOC_CTX
*mem_ctx
, const char *name
)
53 return talloc_asprintf(mem_ctx
, "%s/%s", get_dyn_CODEPAGEDIR(), name
);
57 * @brief Returns the platform specific shared library extension.
59 * @retval Pointer to a const char * containing the extension.
62 const char *shlib_ext(void)
64 return get_dyn_SHLIBEXT();
67 static char *get_user_home_dir(TALLOC_CTX
*mem_ctx
)
69 struct passwd pwd
= {0};
70 struct passwd
*pwdbuf
= NULL
;
77 initlen
= sysconf(_SC_GETPW_R_SIZE_MAX
);
81 len
= (size_t)initlen
;
83 buf
= talloc_size(mem_ctx
, len
);
88 rc
= getpwuid_r(getuid(), &pwd
, buf
, len
, &pwdbuf
);
89 while (rc
== ERANGE
) {
90 size_t newlen
= 2 * len
;
96 buf
= talloc_realloc_size(mem_ctx
, buf
, len
);
100 rc
= getpwuid_r(getuid(), &pwd
, buf
, len
, &pwdbuf
);
102 if (rc
!= 0 || pwdbuf
== NULL
) {
103 const char *szPath
= getenv("HOME");
104 if (szPath
== NULL
) {
107 len
= strnlen(szPath
, PATH_MAX
);
108 if (len
>= PATH_MAX
) {
111 out
= talloc_strdup(mem_ctx
, szPath
);
115 out
= talloc_strdup(mem_ctx
, pwd
.pw_dir
);
121 char *path_expand_tilde(TALLOC_CTX
*mem_ctx
, const char *d
)
123 char *h
= NULL
, *r
= NULL
;
124 const char *p
= NULL
;
125 struct stat sb
= {0};
129 return talloc_strdup(mem_ctx
, d
);
133 /* handle ~user/path */
135 if (p
!= NULL
&& p
> d
) {
140 if (s
>= sizeof(u
)) {
150 h
= talloc_strdup(mem_ctx
, pw
->pw_dir
);
153 h
= get_user_home_dir(mem_ctx
);
165 r
= talloc_asprintf(mem_ctx
, "%s%s", h
, p
);