GitHub CI: update list of container images
[tinc] / src / names.c
1 /*
2     names.c -- generate commonly used (file)names
3     Copyright (C) 1998-2005 Ivo Timmermans
4                   2000-2018 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_WINDOWS
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
53         free(identname);
54
55         if(netname) {
56                 xasprintf(&identname, "tinc.%s", netname);
57         } else {
58                 identname = xstrdup("tinc");
59         }
60
61 #ifdef HAVE_WINDOWS
62
63         if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
64                 if(!RegQueryValueEx(key, NULL, 0, 0, (LPBYTE)installdir, &len)) {
65                         confdir = xstrdup(installdir);
66
67                         if(!confbase) {
68                                 if(netname) {
69                                         xasprintf(&confbase, "%s" SLASH "%s", installdir, netname);
70                                 } else {
71                                         xasprintf(&confbase, "%s", installdir);
72                                 }
73                         }
74
75                         if(!logfilename) {
76                                 xasprintf(&logfilename, "%s" SLASH "tinc.log", confbase);
77                         }
78                 }
79
80                 RegCloseKey(key);
81         }
82
83 #endif
84
85         if(!confdir) {
86                 confdir = xstrdup(CONFDIR SLASH "tinc");
87         }
88
89         if(!confbase) {
90                 if(netname) {
91                         xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname);
92                 } else {
93                         xasprintf(&confbase, CONFDIR SLASH "tinc");
94                 }
95         }
96
97 #ifdef HAVE_WINDOWS
98         (void)daemon;
99
100         if(!logfilename) {
101                 xasprintf(&logfilename, "%s" SLASH "log", confbase);
102         }
103
104         if(!pidfilename) {
105                 xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
106         }
107
108 #else
109         bool fallback = false;
110
111         if(daemon) {
112                 if(access(LOCALSTATEDIR, R_OK | W_OK | X_OK)) {
113                         fallback = true;
114                 }
115         } else {
116                 char fname[PATH_MAX];
117                 snprintf(fname, sizeof(fname), LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
118
119                 if(access(fname, R_OK)) {
120                         snprintf(fname, sizeof(fname), "%s" SLASH "pid", confbase);
121
122                         if(!access(fname, R_OK)) {
123                                 fallback = true;
124                         }
125                 }
126         }
127
128         if(!fallback) {
129                 if(!logfilename) {
130                         xasprintf(&logfilename, LOCALSTATEDIR SLASH "log" SLASH "%s.log", identname);
131                 }
132
133                 if(!pidfilename) {
134                         xasprintf(&pidfilename, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
135                 }
136         } else {
137                 if(!logfilename) {
138                         xasprintf(&logfilename, "%s" SLASH "log", confbase);
139                 }
140
141                 if(!pidfilename) {
142                         if(daemon) {
143                                 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not access " LOCALSTATEDIR SLASH " (%s), storing pid and socket files in %s" SLASH, strerror(errno), confbase);
144                         }
145
146                         xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
147                 }
148         }
149
150 #endif
151
152         if(!unixsocketname) {
153                 size_t len = strlen(pidfilename);
154                 unixsocketname = xmalloc(len + 8);
155                 memcpy(unixsocketname, pidfilename, len);
156
157                 if(len > 4 && !strcmp(pidfilename + len - 4, ".pid")) {
158                         strncpy(unixsocketname + len - 4, ".socket", 8);
159                 } else {
160                         strncpy(unixsocketname + len, ".socket", 8);
161                 }
162         }
163 }
164
165 void free_names(void) {
166         free(identname);
167         free(netname);
168         free(unixsocketname);
169         free(pidfilename);
170         free(logfilename);
171         free(confbase);
172         free(confdir);
173         free(myname);
174
175         identname = NULL;
176         netname = NULL;
177         unixsocketname = NULL;
178         pidfilename = NULL;
179         logfilename = NULL;
180         confbase = NULL;
181         confdir = NULL;
182         myname = NULL;
183 }