]> git.proxmox.com Git - libgit2.git/blob - src/sysdir.c
Add dependency on ca-certificates and libpcre3-dev
[libgit2.git] / src / sysdir.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "sysdir.h"
9
10 #include "global.h"
11 #include "buffer.h"
12 #include "path.h"
13 #include <ctype.h>
14 #if GIT_WIN32
15 #include "win32/findfile.h"
16 #else
17 #include <unistd.h>
18 #include <pwd.h>
19 #endif
20
21 static int git_sysdir_guess_programdata_dirs(git_buf *out)
22 {
23 #ifdef GIT_WIN32
24 return git_win32__find_programdata_dirs(out);
25 #else
26 git_buf_clear(out);
27 return 0;
28 #endif
29 }
30
31 static int git_sysdir_guess_system_dirs(git_buf *out)
32 {
33 #ifdef GIT_WIN32
34 return git_win32__find_system_dirs(out, L"etc\\");
35 #else
36 return git_buf_sets(out, "/etc");
37 #endif
38 }
39
40 #ifndef GIT_WIN32
41 static int get_passwd_home(git_buf *out, uid_t uid)
42 {
43 struct passwd pwd, *pwdptr;
44 char *buf = NULL;
45 long buflen;
46 int error;
47
48 assert(out);
49
50 if ((buflen = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1)
51 buflen = 1024;
52
53 do {
54 buf = git__realloc(buf, buflen);
55 error = getpwuid_r(uid, &pwd, buf, buflen, &pwdptr);
56 buflen *= 2;
57 } while (error == ERANGE && buflen <= 8192);
58
59 if (error) {
60 git_error_set(GIT_ERROR_OS, "failed to get passwd entry");
61 goto out;
62 }
63
64 if (!pwdptr) {
65 git_error_set(GIT_ERROR_OS, "no passwd entry found for user");
66 goto out;
67 }
68
69 if ((error = git_buf_puts(out, pwdptr->pw_dir)) < 0)
70 goto out;
71
72 out:
73 git__free(buf);
74 return error;
75 }
76 #endif
77
78 static int git_sysdir_guess_global_dirs(git_buf *out)
79 {
80 #ifdef GIT_WIN32
81 return git_win32__find_global_dirs(out);
82 #else
83 int error;
84 uid_t uid, euid;
85 const char *sandbox_id;
86
87 uid = getuid();
88 euid = geteuid();
89
90 /**
91 * If APP_SANDBOX_CONTAINER_ID is set, we are running in a
92 * sandboxed environment on macOS.
93 */
94 sandbox_id = getenv("APP_SANDBOX_CONTAINER_ID");
95
96 /*
97 * In case we are running setuid, use the configuration
98 * of the effective user.
99 *
100 * If we are running in a sandboxed environment on macOS,
101 * we have to get the HOME dir from the password entry file.
102 */
103 if (!sandbox_id && uid == euid)
104 error = git__getenv(out, "HOME");
105 else
106 error = get_passwd_home(out, euid);
107
108 if (error == GIT_ENOTFOUND) {
109 git_error_clear();
110 error = 0;
111 }
112
113 return error;
114 #endif
115 }
116
117 static int git_sysdir_guess_xdg_dirs(git_buf *out)
118 {
119 #ifdef GIT_WIN32
120 return git_win32__find_xdg_dirs(out);
121 #else
122 git_buf env = GIT_BUF_INIT;
123 int error;
124 uid_t uid, euid;
125
126 uid = getuid();
127 euid = geteuid();
128
129 /*
130 * In case we are running setuid, only look up passwd
131 * directory of the effective user.
132 */
133 if (uid == euid) {
134 if ((error = git__getenv(&env, "XDG_CONFIG_HOME")) == 0)
135 error = git_buf_joinpath(out, env.ptr, "git");
136
137 if (error == GIT_ENOTFOUND && (error = git__getenv(&env, "HOME")) == 0)
138 error = git_buf_joinpath(out, env.ptr, ".config/git");
139 } else {
140 if ((error = get_passwd_home(&env, euid)) == 0)
141 error = git_buf_joinpath(out, env.ptr, ".config/git");
142 }
143
144 if (error == GIT_ENOTFOUND) {
145 git_error_clear();
146 error = 0;
147 }
148
149 git_buf_dispose(&env);
150 return error;
151 #endif
152 }
153
154 static int git_sysdir_guess_template_dirs(git_buf *out)
155 {
156 #ifdef GIT_WIN32
157 return git_win32__find_system_dirs(out, L"share\\git-core\\templates");
158 #else
159 return git_buf_sets(out, "/usr/share/git-core/templates");
160 #endif
161 }
162
163 struct git_sysdir__dir {
164 git_buf buf;
165 int (*guess)(git_buf *out);
166 };
167
168 static struct git_sysdir__dir git_sysdir__dirs[] = {
169 { GIT_BUF_INIT, git_sysdir_guess_system_dirs },
170 { GIT_BUF_INIT, git_sysdir_guess_global_dirs },
171 { GIT_BUF_INIT, git_sysdir_guess_xdg_dirs },
172 { GIT_BUF_INIT, git_sysdir_guess_programdata_dirs },
173 { GIT_BUF_INIT, git_sysdir_guess_template_dirs },
174 };
175
176 static void git_sysdir_global_shutdown(void)
177 {
178 size_t i;
179
180 for (i = 0; i < ARRAY_SIZE(git_sysdir__dirs); ++i)
181 git_buf_dispose(&git_sysdir__dirs[i].buf);
182 }
183
184 int git_sysdir_global_init(void)
185 {
186 size_t i;
187 int error = 0;
188
189 for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); i++)
190 error = git_sysdir__dirs[i].guess(&git_sysdir__dirs[i].buf);
191
192 git__on_shutdown(git_sysdir_global_shutdown);
193
194 return error;
195 }
196
197 static int git_sysdir_check_selector(git_sysdir_t which)
198 {
199 if (which < ARRAY_SIZE(git_sysdir__dirs))
200 return 0;
201
202 git_error_set(GIT_ERROR_INVALID, "config directory selector out of range");
203 return -1;
204 }
205
206
207 int git_sysdir_get(const git_buf **out, git_sysdir_t which)
208 {
209 assert(out);
210
211 *out = NULL;
212
213 GIT_ERROR_CHECK_ERROR(git_sysdir_check_selector(which));
214
215 *out = &git_sysdir__dirs[which].buf;
216 return 0;
217 }
218
219 int git_sysdir_get_str(
220 char *out,
221 size_t outlen,
222 git_sysdir_t which)
223 {
224 const git_buf *path = NULL;
225
226 GIT_ERROR_CHECK_ERROR(git_sysdir_check_selector(which));
227 GIT_ERROR_CHECK_ERROR(git_sysdir_get(&path, which));
228
229 if (!out || path->size >= outlen) {
230 git_error_set(GIT_ERROR_NOMEMORY, "buffer is too short for the path");
231 return GIT_EBUFS;
232 }
233
234 git_buf_copy_cstr(out, outlen, path);
235 return 0;
236 }
237
238 #define PATH_MAGIC "$PATH"
239
240 int git_sysdir_set(git_sysdir_t which, const char *search_path)
241 {
242 const char *expand_path = NULL;
243 git_buf merge = GIT_BUF_INIT;
244
245 GIT_ERROR_CHECK_ERROR(git_sysdir_check_selector(which));
246
247 if (search_path != NULL)
248 expand_path = strstr(search_path, PATH_MAGIC);
249
250 /* reset the default if this path has been cleared */
251 if (!search_path)
252 git_sysdir__dirs[which].guess(&git_sysdir__dirs[which].buf);
253
254 /* if $PATH is not referenced, then just set the path */
255 if (!expand_path) {
256 if (search_path)
257 git_buf_sets(&git_sysdir__dirs[which].buf, search_path);
258
259 goto done;
260 }
261
262 /* otherwise set to join(before $PATH, old value, after $PATH) */
263 if (expand_path > search_path)
264 git_buf_set(&merge, search_path, expand_path - search_path);
265
266 if (git_buf_len(&git_sysdir__dirs[which].buf))
267 git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR,
268 merge.ptr, git_sysdir__dirs[which].buf.ptr);
269
270 expand_path += strlen(PATH_MAGIC);
271 if (*expand_path)
272 git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR, merge.ptr, expand_path);
273
274 git_buf_swap(&git_sysdir__dirs[which].buf, &merge);
275 git_buf_dispose(&merge);
276
277 done:
278 if (git_buf_oom(&git_sysdir__dirs[which].buf))
279 return -1;
280
281 return 0;
282 }
283
284 static int git_sysdir_find_in_dirlist(
285 git_buf *path,
286 const char *name,
287 git_sysdir_t which,
288 const char *label)
289 {
290 size_t len;
291 const char *scan, *next = NULL;
292 const git_buf *syspath;
293
294 GIT_ERROR_CHECK_ERROR(git_sysdir_get(&syspath, which));
295 if (!syspath || !git_buf_len(syspath))
296 goto done;
297
298 for (scan = git_buf_cstr(syspath); scan; scan = next) {
299 /* find unescaped separator or end of string */
300 for (next = scan; *next; ++next) {
301 if (*next == GIT_PATH_LIST_SEPARATOR &&
302 (next <= scan || next[-1] != '\\'))
303 break;
304 }
305
306 len = (size_t)(next - scan);
307 next = (*next ? next + 1 : NULL);
308 if (!len)
309 continue;
310
311 GIT_ERROR_CHECK_ERROR(git_buf_set(path, scan, len));
312 if (name)
313 GIT_ERROR_CHECK_ERROR(git_buf_joinpath(path, path->ptr, name));
314
315 if (git_path_exists(path->ptr))
316 return 0;
317 }
318
319 done:
320 git_buf_dispose(path);
321 git_error_set(GIT_ERROR_OS, "the %s file '%s' doesn't exist", label, name);
322 return GIT_ENOTFOUND;
323 }
324
325 int git_sysdir_find_system_file(git_buf *path, const char *filename)
326 {
327 return git_sysdir_find_in_dirlist(
328 path, filename, GIT_SYSDIR_SYSTEM, "system");
329 }
330
331 int git_sysdir_find_global_file(git_buf *path, const char *filename)
332 {
333 return git_sysdir_find_in_dirlist(
334 path, filename, GIT_SYSDIR_GLOBAL, "global");
335 }
336
337 int git_sysdir_find_xdg_file(git_buf *path, const char *filename)
338 {
339 return git_sysdir_find_in_dirlist(
340 path, filename, GIT_SYSDIR_XDG, "global/xdg");
341 }
342
343 int git_sysdir_find_programdata_file(git_buf *path, const char *filename)
344 {
345 return git_sysdir_find_in_dirlist(
346 path, filename, GIT_SYSDIR_PROGRAMDATA, "ProgramData");
347 }
348
349 int git_sysdir_find_template_dir(git_buf *path)
350 {
351 return git_sysdir_find_in_dirlist(
352 path, NULL, GIT_SYSDIR_TEMPLATE, "template");
353 }
354
355 int git_sysdir_expand_global_file(git_buf *path, const char *filename)
356 {
357 int error;
358
359 if ((error = git_sysdir_find_global_file(path, NULL)) == 0) {
360 if (filename)
361 error = git_buf_joinpath(path, path->ptr, filename);
362 }
363
364 return error;
365 }