9bc6df37e5d484e4671e50894e9ba0fb72a1cfea
[samba.git] / lib / util / util_paths.c
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>
10
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.
15
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.
20
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/>.
23 */
24
25 #include "includes.h"
26 #include "dynconfig/dynconfig.h"
27 #include "lib/util/util_paths.h"
28 #include "system/passwd.h"
29
30 /**
31  * @brief Returns an absolute path to a file in the Samba modules directory.
32  *
33  * @param name File to find, relative to MODULESDIR.
34  *
35  * @retval Pointer to a string containing the full path.
36  **/
37
38 char *modules_path(TALLOC_CTX *mem_ctx, const char *name)
39 {
40         return talloc_asprintf(mem_ctx, "%s/%s", get_dyn_MODULESDIR(), name);
41 }
42
43 /**
44  * @brief Returns an absolute path to a file in the Samba data directory.
45  *
46  * @param name File to find, relative to CODEPAGEDIR.
47  *
48  * @retval Pointer to a talloc'ed string containing the full path.
49  **/
50
51 char *data_path(TALLOC_CTX *mem_ctx, const char *name)
52 {
53         return talloc_asprintf(mem_ctx, "%s/%s", get_dyn_CODEPAGEDIR(), name);
54 }
55
56 /**
57  * @brief Returns the platform specific shared library extension.
58  *
59  * @retval Pointer to a const char * containing the extension.
60  **/
61
62 const char *shlib_ext(void)
63 {
64         return get_dyn_SHLIBEXT();
65 }
66
67 static char *get_user_home_dir(TALLOC_CTX *mem_ctx)
68 {
69         struct passwd pwd = {0};
70         struct passwd *pwdbuf = NULL;
71         char *buf = NULL;
72         char *out = NULL;
73         long int initlen;
74         size_t len;
75         int rc;
76
77         initlen = sysconf(_SC_GETPW_R_SIZE_MAX);
78         if (initlen == -1) {
79                 len = 1024;
80         } else {
81                 len = (size_t)initlen;
82         }
83         buf = talloc_size(mem_ctx, len);
84         if (buf == NULL) {
85                 return NULL;
86         }
87
88         rc = getpwuid_r(getuid(), &pwd, buf, len, &pwdbuf);
89         if (rc != 0 || pwdbuf == NULL ) {
90                 const char *szPath = getenv("HOME");
91                 if (szPath == NULL) {
92                         goto done;
93                 }
94                 len = strnlen(szPath, PATH_MAX);
95                 if (len >= PATH_MAX) {
96                         return NULL;
97                 }
98                 out = talloc_strdup(mem_ctx, szPath);
99                 goto done;
100         }
101
102         out = talloc_strdup(mem_ctx, pwd.pw_dir);
103 done:
104         TALLOC_FREE(buf);
105         return out;
106 }
107
108 char *path_expand_tilde(TALLOC_CTX *mem_ctx, const char *d)
109 {
110         char *h = NULL, *r = NULL;
111         const char *p = NULL;
112         struct stat sb = {0};
113         int rc;
114
115         if (d[0] != '~') {
116                 return talloc_strdup(mem_ctx, d);
117         }
118         d++;
119
120         /* handle ~user/path */
121         p = strchr(d, '/');
122         if (p != NULL && p > d) {
123                 struct passwd *pw;
124                 size_t s = p - d;
125                 char u[128];
126
127                 if (s >= sizeof(u)) {
128                         return NULL;
129                 }
130                 memcpy(u, d, s);
131                 u[s] = '\0';
132
133                 pw = getpwnam(u);
134                 if (pw == NULL) {
135                         return NULL;
136                 }
137                 h = talloc_strdup(mem_ctx, pw->pw_dir);
138         } else {
139                 p = d;
140                 h = get_user_home_dir(mem_ctx);
141         }
142         if (h == NULL) {
143                 return NULL;
144         }
145
146         rc = stat(h, &sb);
147         if (rc != 0) {
148                 TALLOC_FREE(h);
149                 return NULL;
150         }
151
152         r = talloc_asprintf(mem_ctx, "%s%s", h, p);
153         TALLOC_FREE(h);
154
155         return r;
156 }