95aad2ba4731d2c1b75c141f92fcb383039267fe
[tinc] / src / names.c
1 /*
2     names.c -- generate commonly used (file)names
3     Copyright (C) 1998-2005 Ivo Timmermans
4                   2000-2017 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 "names.h"
25 #include "xalloc.h"
26
27 char *netname = NULL;
28 char *myname = NULL;
29 char *confdir = NULL;           /* base configuration directory */
30 char *confbase = NULL;          /* base configuration directory for this instance of tinc */
31 bool confbase_given;
32 char *identname = NULL;         /* program name for syslog */
33 char *unixsocketname = NULL;    /* UNIX socket location */
34 char *logfilename = NULL;       /* log file location */
35 char *pidfilename = NULL;
36 char *program_name = NULL;
37
38 /*
39   Set all files and paths according to netname
40 */
41 void make_names(bool daemon) {
42 #ifdef HAVE_MINGW
43         HKEY key;
44         char installdir[1024] = "";
45         DWORD len = sizeof(installdir);
46 #endif
47         confbase_given = confbase;
48
49         if(netname && confbase)
50                 logger(DEBUG_ALWAYS, LOG_INFO, "Both netname and configuration directory given, using the latter...");
51
52         if(netname)
53                 xasprintf(&identname, "tinc.%s", netname);
54         else
55                 identname = xstrdup("tinc");
56
57 #ifdef HAVE_MINGW
58         if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
59                 if(!RegQueryValueEx(key, NULL, 0, 0, (LPBYTE)installdir, &len)) {
60                         confdir = xstrdup(installdir);
61                         if(!confbase) {
62                                 if(netname)
63                                         xasprintf(&confbase, "%s" SLASH "%s", installdir, netname);
64                                 else
65                                         xasprintf(&confbase, "%s", installdir);
66                         }
67                         if(!logfilename)
68                                 xasprintf(&logfilename, "%s" SLASH "tinc.log", confbase);
69                 }
70                 RegCloseKey(key);
71         }
72 #endif
73         if(!confdir)
74                 confdir = xstrdup(CONFDIR SLASH "tinc");
75
76         if(!confbase) {
77                 if(netname)
78                         xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname);
79                 else
80                         xasprintf(&confbase, CONFDIR SLASH "tinc");
81         }
82
83 #ifdef HAVE_MINGW
84         if(!logfilename)
85                 xasprintf(&logfilename, "%s" SLASH "log", confbase);
86
87         if(!pidfilename)
88                 xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
89 #else
90         bool fallback = false;
91         if(daemon) {
92                 if(access(LOCALSTATEDIR, R_OK | W_OK | X_OK))
93                         fallback = true;
94         } else {
95                 char fname[PATH_MAX];
96                 snprintf(fname, sizeof(fname), LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
97                 if(access(fname, R_OK)) {
98                         snprintf(fname, sizeof(fname), "%s" SLASH "pid", confbase);
99                         if(!access(fname, R_OK))
100                                 fallback = true;
101                 }
102         }
103
104         if(!fallback) {
105                 if(!logfilename)
106                         xasprintf(&logfilename, LOCALSTATEDIR SLASH "log" SLASH "%s.log", identname);
107
108                 if(!pidfilename)
109                         xasprintf(&pidfilename, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
110         } else {
111                 if(!logfilename)
112                         xasprintf(&logfilename, "%s" SLASH "log", confbase);
113
114                 if(!pidfilename) {
115                         if(daemon)
116                                 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not access " LOCALSTATEDIR SLASH " (%s), storing pid and socket files in %s" SLASH, strerror(errno), confbase);
117                         xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
118                 }
119         }
120 #endif
121
122         if(!unixsocketname) {
123                 int len = strlen(pidfilename);
124                 unixsocketname = xmalloc(len + 8);
125                 memcpy(unixsocketname, pidfilename, len);
126                 if(len > 4 && !strcmp(pidfilename + len - 4, ".pid"))
127                         strncpy(unixsocketname + len - 4, ".socket", 8);
128                 else
129                         strncpy(unixsocketname + len, ".socket", 8);
130         }
131 }
132
133 void free_names(void) {
134         free(identname);
135         free(netname);
136         free(unixsocketname);
137         free(pidfilename);
138         free(logfilename);
139         free(confbase);
140         free(confdir);
141         free(myname);
142
143         identname = NULL;
144         netname = NULL;
145         unixsocketname = NULL;
146         pidfilename = NULL;
147         logfilename = NULL;
148         confbase = NULL;
149         confdir = NULL;
150         myname = NULL;
151 }