Fix check for LOCALSTATEDIR accessibility for the CLI.
[tinc] / src / names.c
1 /*
2     names.c -- generate commonly used (file)names
3     Copyright (C) 1998-2005 Ivo Timmermans
4                   2000-2015 Guus Sliepen <guus@tinc-vpn.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "logger.h"
24 #include "xalloc.h"
25
26 char *netname = NULL;
27 char *confdir = NULL;           /* base configuration directory */
28 char *confbase = NULL;          /* base configuration directory for this instance of tinc */
29 bool confbase_given;
30 char *identname = NULL;         /* program name for syslog */
31 char *unixsocketname = NULL;    /* UNIX socket location */
32 char *logfilename = NULL;       /* log file location */
33 char *pidfilename = NULL;
34 char *program_name = NULL;
35
36 /*
37   Set all files and paths according to netname
38 */
39 void make_names(bool daemon) {
40 #ifdef HAVE_MINGW
41         HKEY key;
42         char installdir[1024] = "";
43         DWORD len = sizeof installdir;
44 #endif
45         confbase_given = confbase;
46
47         if(netname && confbase)
48                 logger(DEBUG_ALWAYS, LOG_INFO, "Both netname and configuration directory given, using the latter...");
49
50         if(netname)
51                 xasprintf(&identname, "tinc.%s", netname);
52         else
53                 identname = xstrdup("tinc");
54
55 #ifdef HAVE_MINGW
56         if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
57                 if(!RegQueryValueEx(key, NULL, 0, 0, (LPBYTE)installdir, &len)) {
58                         confdir = xstrdup(installdir);
59                         if(!logfilename)
60                                 xasprintf(&logfilename, "%s" SLASH "log" SLASH "%s.log", installdir, identname);
61                         if(!confbase) {
62                                 if(netname)
63                                         xasprintf(&confbase, "%s" SLASH "%s", installdir, netname);
64                                 else
65                                         xasprintf(&confbase, "%s", installdir);
66                         }
67                 }
68                 RegCloseKey(key);
69         }
70 #endif
71         if(!confdir)
72                 confdir = xstrdup(CONFDIR SLASH "tinc");
73
74         if(!confbase) {
75                 if(netname)
76                         xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname);
77                 else
78                         xasprintf(&confbase, CONFDIR SLASH "tinc");
79         }
80
81 #ifdef HAVE_MINGW
82         if(!logfilename)
83                 xasprintf(&logfilename, "%s" SLASH "log", confbase);
84
85         if(!pidfilename)
86                 xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
87 #else
88         bool fallback = false;
89         if(daemon) {
90                 if(access(LOCALSTATEDIR, R_OK | W_OK | X_OK))
91                         fallback = true;
92         } else {
93                 char fname[PATH_MAX];
94                 snprintf(fname, sizeof fname, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
95                 if(access(fname, R_OK)) {
96                         snprintf(fname, sizeof fname, "%s" SLASH "pid", confbase);
97                         if(!access(fname, R_OK))
98                                 fallback = true;
99                 }
100         }
101
102         if(!fallback) {
103                 if(!logfilename)
104                         xasprintf(&logfilename, LOCALSTATEDIR SLASH "log" SLASH "%s.log", identname);
105
106                 if(!pidfilename)
107                         xasprintf(&pidfilename, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
108         } else {
109                 if(!logfilename)
110                         xasprintf(&logfilename, "%s" SLASH "log", confbase);
111
112                 if(!pidfilename) {
113                         if(daemon)
114                                 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not access " LOCALSTATEDIR SLASH " (%s), storing pid and socket files in %s" SLASH, strerror(errno), confbase);
115                         xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
116                 }
117         }
118 #endif
119
120         if(!unixsocketname) {
121                 int len = strlen(pidfilename);
122                 unixsocketname = xmalloc(len + 8);
123                 strcpy(unixsocketname, pidfilename);
124                 if(len > 4 && !strcmp(pidfilename + len - 4, ".pid"))
125                         strcpy(unixsocketname + len - 4, ".socket");
126                 else
127                         strcpy(unixsocketname + len, ".socket");
128         }
129 }
130
131 void free_names(void) {
132         free(identname);
133         free(netname);
134         free(unixsocketname);
135         free(pidfilename);
136         free(logfilename);
137         free(confbase);
138         free(confdir);
139 }