1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24 #include "tool_setup.h"
25
26 #ifdef HAVE_PWD_H
27 # undef __NO_NET_API /* required for building for AmigaOS */
28 # include <pwd.h>
29 #endif
30
31 #ifdef HAVE_SYS_STAT_H
32 #include <sys/stat.h>
33 #endif
34 #ifdef HAVE_FCNTL_H
35 #include <fcntl.h>
36 #endif
37
38 #include <curl/mprintf.h>
39
40 #include "tool_findfile.h"
41
42 #include "memdebug.h" /* keep this as LAST include */
43
44 struct finder {
45 const char *env;
46 const char *append;
47 bool withoutdot;
48 };
49
50 /* The order of the variables below is important, as the index number is used
51 in the findfile() function */
52 static const struct finder conf_list[] = {
53 { "CURL_HOME", NULL, FALSE },
54 { "XDG_CONFIG_HOME", NULL, FALSE }, /* index == 1, used in the code */
55 { "HOME", NULL, FALSE },
56 #ifdef _WIN32
57 { "USERPROFILE", NULL, FALSE },
58 { "APPDATA", NULL, FALSE },
59 { "USERPROFILE", "\\Application Data", FALSE},
60 #endif
61 /* these are for .curlrc if XDG_CONFIG_HOME is not defined */
62 { "CURL_HOME", "/.config", TRUE },
63 { "HOME", "/.config", TRUE },
64
65 { NULL, NULL, FALSE }
66 };
67
checkhome(const char * home,const char * fname,bool dotscore)68 static char *checkhome(const char *home, const char *fname, bool dotscore)
69 {
70 const char pref[2] = { '.', '_' };
71 int i;
72 for(i = 0; i < (dotscore ? 2 : 1); i++) {
73 char *c;
74 if(dotscore)
75 c = curl_maprintf("%s" DIR_CHAR "%c%s", home, pref[i], &fname[1]);
76 else
77 c = curl_maprintf("%s" DIR_CHAR "%s", home, fname);
78 if(c) {
79 int fd = open(c, O_RDONLY);
80 if(fd >= 0) {
81 char *path = strdup(c);
82 close(fd);
83 curl_free(c);
84 return path;
85 }
86 curl_free(c);
87 }
88 }
89 return NULL;
90 }
91
92 /*
93 * findfile() - return the full path name of the file.
94 *
95 * If 'dotscore' is TRUE, then check for the file first with a leading dot
96 * and then with a leading underscore.
97 *
98 * 1. Iterate over the environment variables in order, and if set, check for
99 * the given file to be accessed there, then it is a match.
100 * 2. Non-windows: try getpwuid
101 */
findfile(const char * fname,int dotscore)102 char *findfile(const char *fname, int dotscore)
103 {
104 int i;
105 bool xdg = FALSE;
106 DEBUGASSERT(fname && fname[0]);
107 DEBUGASSERT((dotscore != 1) || (fname[0] == '.'));
108
109 if(!fname[0])
110 return NULL;
111
112 for(i = 0; conf_list[i].env; i++) {
113 char *home = curl_getenv(conf_list[i].env);
114 if(home) {
115 char *path;
116 const char *filename = fname;
117 if(i == 1 /* XDG_CONFIG_HOME */)
118 xdg = TRUE;
119 if(!home[0]) {
120 curl_free(home);
121 continue;
122 }
123 if(conf_list[i].append) {
124 char *c = curl_maprintf("%s%s", home, conf_list[i].append);
125 curl_free(home);
126 if(!c)
127 return NULL;
128 home = c;
129 }
130 if(conf_list[i].withoutdot) {
131 if(!dotscore || xdg) {
132 /* this is not looking for .curlrc, or the XDG_CONFIG_HOME was
133 defined so we skip the extended check */
134 curl_free(home);
135 continue;
136 }
137 filename++; /* move past the leading dot */
138 dotscore = 0; /* disable it for this check */
139 }
140 path = checkhome(home, filename, dotscore ? dotscore - 1 : 0);
141 curl_free(home);
142 if(path)
143 return path;
144 }
145 }
146 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
147 {
148 struct passwd *pw = getpwuid(geteuid());
149 if(pw) {
150 char *home = pw->pw_dir;
151 if(home && home[0])
152 return checkhome(home, fname, FALSE);
153 }
154 }
155 #endif /* PWD-stuff */
156 return NULL;
157 }
158