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 <curlx.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, TRUE },
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 = aprintf("%s" DIR_CHAR "%c%s", home, pref[i], &fname[1]);
76 else
77 c = aprintf("%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 DEBUGASSERT(fname && fname[0]);
106 DEBUGASSERT((dotscore != 1) || (fname[0] == '.'));
107
108 if(!fname[0])
109 return NULL;
110
111 for(i = 0; conf_list[i].env; i++) {
112 char *home = curl_getenv(conf_list[i].env);
113 if(home) {
114 char *path;
115 const char *filename = fname;
116 if(!home[0]) {
117 curl_free(home);
118 continue;
119 }
120 if(conf_list[i].append) {
121 char *c = aprintf("%s%s", home, conf_list[i].append);
122 curl_free(home);
123 if(!c)
124 return NULL;
125 home = c;
126 }
127 if(conf_list[i].withoutdot) {
128 if(!dotscore) {
129 /* this is not looking for .curlrc, or the XDG_CONFIG_HOME was
130 defined so we skip the extended check */
131 curl_free(home);
132 continue;
133 }
134 filename++; /* move past the leading dot */
135 dotscore = 0; /* disable it for this check */
136 }
137 path = checkhome(home, filename, dotscore ? dotscore - 1 : 0);
138 curl_free(home);
139 if(path)
140 return path;
141 }
142 }
143 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
144 {
145 struct passwd *pw = getpwuid(geteuid());
146 if(pw) {
147 char *home = pw->pw_dir;
148 if(home && home[0])
149 return checkhome(home, fname, FALSE);
150 }
151 }
152 #endif /* PWD-stuff */
153 return NULL;
154 }
155