1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2020, 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.haxx.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  ***************************************************************************/
22 #include "tool_setup.h"
23 
24 #ifdef HAVE_PWD_H
25 #  include <pwd.h>
26 #endif
27 
28 #ifdef HAVE_SYS_STAT_H
29 #include <sys/stat.h>
30 #endif
31 #ifdef HAVE_FCNTL_H
32 #include <fcntl.h>
33 #endif
34 
35 #include <curl/mprintf.h>
36 
37 #include "tool_homedir.h"
38 
39 #include "memdebug.h" /* keep this as LAST include */
40 
GetEnv(const char * variable)41 static char *GetEnv(const char *variable)
42 {
43   char *dupe, *env;
44 
45   env = curl_getenv(variable);
46   if(!env)
47     return NULL;
48 
49   dupe = strdup(env);
50   curl_free(env);
51   return dupe;
52 }
53 
54 /* return the home directory of the current user as an allocated string */
55 
56 /*
57  * The original logic found a home dir to use (by checking a range of
58  * environment variables and last using getpwuid) and returned that for the
59  * parent to use.
60  *
61  * With the XDG_CONFIG_HOME support (added much later than the other), this
62  * variable is treated differently in order to not ruin existing installations
63  * even if this environment variable is set. If this variable is set, and a
64  * file name is set to check, then only if that file name exists in that
65  * directory will it be returned as a "home directory".
66  *
67  * 1. use CURL_HOME if set
68  * 2. use XDG_CONFIG_HOME if set and fname is present
69  * 3. use HOME if set
70  * 4. Non-windows: use getpwuid
71  * 5. Windows: use APPDATA if set
72  * 6. Windows: use "USERPROFILE\Application Data" is set
73  */
74 
homedir(const char * fname)75 char *homedir(const char *fname)
76 {
77   char *home;
78 
79   home = GetEnv("CURL_HOME");
80   if(home)
81     return home;
82 
83   if(fname) {
84     home = GetEnv("XDG_CONFIG_HOME");
85     if(home) {
86       char *c = curl_maprintf("%s" DIR_CHAR "%s", home, fname);
87       if(c) {
88         int fd = open(c, O_RDONLY);
89         curl_free(c);
90         if(fd >= 0) {
91           close(fd);
92           return home;
93         }
94       }
95       free(home);
96     }
97   }
98 
99   home = GetEnv("HOME");
100   if(home)
101     return home;
102 
103 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
104  {
105    struct passwd *pw = getpwuid(geteuid());
106 
107    if(pw) {
108      home = pw->pw_dir;
109      if(home && home[0])
110        home = strdup(home);
111      else
112        home = NULL;
113    }
114  }
115 #endif /* PWD-stuff */
116 #ifdef WIN32
117   home = GetEnv("APPDATA");
118   if(!home) {
119     char *env = GetEnv("USERPROFILE");
120     if(env) {
121       char *path = curl_maprintf("%s\\Application Data", env);
122       if(path) {
123         home = strdup(path);
124         curl_free(path);
125       }
126       free(env);
127     }
128   }
129 #endif /* WIN32 */
130   return home;
131 }
132