xref: /netbsd/external/bsd/ntp/dist/lib/isc/win32/ntpaths.c (revision 6550d01e)
1 /*	$NetBSD: ntpaths.c,v 1.1.1.1 2009/12/13 16:54:40 kardel Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2001  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: ntpaths.c,v 1.11 2007/06/18 23:47:49 tbox Exp */
21 
22 /*
23  * This module fetches the required path information that is specific
24  * to NT systems which can have its configuration and system files
25  * almost anywhere. It can be used to override whatever the application
26  * had previously assigned to the pointer. Basic information about the
27  * file locations are stored in the registry.
28  */
29 
30 #include <config.h>
31 #include <isc/bind_registry.h>
32 #include <isc/ntpaths.h>
33 
34 /*
35  * Module Variables
36  */
37 
38 static char systemDir[MAX_PATH];
39 static char namedBase[MAX_PATH];
40 static char ns_confFile[MAX_PATH];
41 static char lwresd_confFile[MAX_PATH];
42 static char lwresd_resolvconfFile[MAX_PATH];
43 static char rndc_confFile[MAX_PATH];
44 static char ns_defaultpidfile[MAX_PATH];
45 static char lwresd_defaultpidfile[MAX_PATH];
46 static char local_state_dir[MAX_PATH];
47 static char sys_conf_dir[MAX_PATH];
48 static char rndc_keyFile[MAX_PATH];
49 
50 static DWORD baseLen = MAX_PATH;
51 static BOOL Initialized = FALSE;
52 
53 void
54 isc_ntpaths_init() {
55 	HKEY hKey;
56 	BOOL keyFound = TRUE;
57 
58 	memset(namedBase, 0, MAX_PATH);
59 	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, BIND_SUBKEY, 0, KEY_READ, &hKey)
60 		!= ERROR_SUCCESS)
61 		keyFound = FALSE;
62 
63 	if (keyFound == TRUE) {
64 		/* Get the named directory */
65 		if (RegQueryValueEx(hKey, "InstallDir", NULL, NULL,
66 			(LPBYTE)namedBase, &baseLen) != ERROR_SUCCESS)
67 			keyFound = FALSE;
68 		RegCloseKey(hKey);
69 	}
70 
71 	GetSystemDirectory(systemDir, MAX_PATH);
72 
73 	if (keyFound == FALSE)
74 		/* Use the System Directory as a default */
75 		strcpy(namedBase, systemDir);
76 
77 	strcpy(ns_confFile, namedBase);
78 	strcat(ns_confFile, "\\etc\\named.conf");
79 
80 	strcpy(lwresd_confFile, namedBase);
81 	strcat(lwresd_confFile, "\\etc\\lwresd.conf");
82 
83 	strcpy(lwresd_resolvconfFile, systemDir);
84 	strcat(lwresd_resolvconfFile, "\\Drivers\\etc\\resolv.conf");
85 
86 	strcpy(rndc_keyFile, namedBase);
87 	strcat(rndc_keyFile, "\\etc\\rndc.key");
88 
89 	strcpy(rndc_confFile, namedBase);
90 	strcat(rndc_confFile, "\\etc\\rndc.conf");
91 	strcpy(ns_defaultpidfile, namedBase);
92 	strcat(ns_defaultpidfile, "\\etc\\named.pid");
93 
94 	strcpy(lwresd_defaultpidfile, namedBase);
95 	strcat(lwresd_defaultpidfile, "\\etc\\lwresd.pid");
96 
97 	strcpy(local_state_dir, namedBase);
98 	strcat(local_state_dir, "\\bin");
99 
100 	strcpy(sys_conf_dir, namedBase);
101 	strcat(sys_conf_dir, "\\etc");
102 
103 	Initialized = TRUE;
104 }
105 
106 char *
107 isc_ntpaths_get(int ind) {
108 	if (!Initialized)
109 		isc_ntpaths_init();
110 
111 	switch (ind) {
112 	case NAMED_CONF_PATH:
113 		return (ns_confFile);
114 		break;
115 	case LWRES_CONF_PATH:
116 		return (lwresd_confFile);
117 		break;
118 	case RESOLV_CONF_PATH:
119 		return (lwresd_resolvconfFile);
120 		break;
121 	case RNDC_CONF_PATH:
122 		return (rndc_confFile);
123 		break;
124 	case NAMED_PID_PATH:
125 		return (ns_defaultpidfile);
126 		break;
127 	case LWRESD_PID_PATH:
128 		return (lwresd_defaultpidfile);
129 		break;
130 	case LOCAL_STATE_DIR:
131 		return (local_state_dir);
132 		break;
133 	case SYS_CONF_DIR:
134 		return (sys_conf_dir);
135 		break;
136 	case RNDC_KEY_PATH:
137 		return (rndc_keyFile);
138 		break;
139 	default:
140 		return (NULL);
141 	}
142 }
143