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