Fix tincctl init when /etc/tinc does not yet exist.
[tinc] / src / names.c
1 /*
2     names.c -- generate commonly used (file)names
3     Copyright (C) 1998-2005 Ivo Timmermans
4                   2000-2013 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 char *identname = NULL;         /* program name for syslog */
30 char *unixsocketname = NULL;    /* UNIX socket location */
31 char *logfilename = NULL;       /* log file location */
32 char *pidfilename = NULL;
33 char *program_name = NULL;
34
35 /*
36   Set all files and paths according to netname
37 */
38 void make_names(void) {
39 #ifdef HAVE_MINGW
40         HKEY key;
41         char installdir[1024] = "";
42         long len = sizeof installdir;
43 #endif
44
45         if(netname)
46                 xasprintf(&identname, "tinc.%s", netname);
47         else
48                 identname = xstrdup("tinc");
49
50 #ifdef HAVE_MINGW
51         if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
52                 if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
53                         confdir = xstrdup(installdir);
54                         if(!logfilename)
55                                 xasprintf(&logfilename, "%s" SLASH "log" SLASH "%s.log", installdir, identname);
56                         if(!confbase) {
57                                 if(netname)
58                                         xasprintf(&confbase, "%s" SLASH "%s", installdir, netname);
59                                 else
60                                         xasprintf(&confbase, "%s", installdir);
61                         }
62                         if(!pidfilename)
63                                 xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
64                 }
65                 RegCloseKey(key);
66         }
67 #endif
68         if(!confdir)
69                 confdir = xstrdup(CONFDIR SLASH "tinc");
70
71         if(!logfilename)
72                 xasprintf(&logfilename, LOCALSTATEDIR SLASH "log" SLASH "%s.log", identname);
73
74         if(!pidfilename)
75                 xasprintf(&pidfilename, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
76
77         if(!unixsocketname)
78                 xasprintf(&unixsocketname, LOCALSTATEDIR SLASH "run" SLASH "%s.socket", identname);
79
80         if(netname) {
81                 if(!confbase)
82                         xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname);
83                 else
84                         logger(DEBUG_ALWAYS, LOG_INFO, "Both netname and configuration directory given, using the latter...");
85         } else {
86                 if(!confbase)
87                         xasprintf(&confbase, CONFDIR SLASH "tinc");
88         }
89 }
90
91 void free_names(void) {
92         free(identname);
93         free(netname);
94         free(unixsocketname);
95         free(pidfilename);
96         free(logfilename);
97         free(confbase);
98         free(confdir);
99 }