summaryrefslogtreecommitdiffstats
path: root/src/windows/cns/cns_reg.c
blob: 357e5d636f2979b3def089668b25bea722a2efa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright (c) 1997 Cygnus Solutions
 *
 * Author:  Michael Graff
 */

#include <windows.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>

#include "cns.h"
#include "cns_reg.h"

#include "../lib/registry.h"

cns_reg_t cns_res;  /* yes, a global.  Sue me. */

/*
 * function to load all the data we will want from the registry.  If the
 * registry data cannot be found this function will initialize a default
 * environment.
 */
void
cns_load_registry(void)
{
  char    tmp[1024];
  DWORD   tdw;
  char   *ts;
  HKEY    key;
  int     i;

  /*
   * Set up reasonable default values.  These will all be overwritten if
   * the registry is successfully opened.
   */
  cns_res.name[0] = '\0';
  cns_res.realm[0] = '\0';
  cns_res.x = 0;
  cns_res.y = 0;
  cns_res.cx = 0;
  cns_res.cy = 0;

  cns_res.alert = 0;
  cns_res.beep = 0;
  cns_res.lifetime = DEFAULT_TKT_LIFE * 5;
  cns_res.forwardable = 1;
  cns_res.noaddresses = 0;

  for (i = 1 ; i < FILE_MENU_MAX_LOGINS ; i++)
    cns_res.logins[i][0] = '\0';

  /*
   * by default, allow the user to override the config file location and NOT the
   * cred cache name.
   */
  cns_res.conf_override = 1;
  cns_res.cc_override = 0;

  {
	char *s;
	s = krb5_cc_default_name(k5_context);

	strcpy(cns_res.def_ccname, s);
  }

  cns_res.def_confname[0] = '\0';

  /*
   * If the system has these keys in the registry, do not allow the user to
   * override the config file and ccache locations.
   */
  key = registry_open(HKEY_LOCAL_MACHINE, KERBNET_BASE, KEY_READ);
  if (key != INVALID_HANDLE_VALUE) {
	if (registry_string_get(key, KERBNET_HOME, &ts) == 0) {
		cns_res.conf_override = 0;
		cns_res.def_confname[sizeof(cns_res.def_confname) - 1];
		strncpy(cns_res.def_confname, ts,
		        sizeof(cns_res.def_confname) - 1);
		strncat(cns_res.def_confname, "\\etc\\krb5.conf",
			sizeof(cns_res.def_confname) - 1 -
			strlen(cns_res.def_confname));
		free(ts);
	  }

	  if (registry_string_get(key, "ccname", &ts) == 0) {
		cns_res.cc_override = 0;
		strcpy(cns_res.def_ccname, ts);
		free(ts);
	  }
  }

  /*
   * Try to open the registry.  If we succeed, read the last used values from there.  If we
   * do not get the registry open simply return.
   */
  key = registry_open(HKEY_CURRENT_USER, KERBNET_CNS_BASE, KEY_ALL_ACCESS);

  if (key == INVALID_HANDLE_VALUE)
	return;

  if (registry_dword_get(key, "x", &tdw) == 0)
	  cns_res.x = tdw;

  if (registry_dword_get(key, "y", &tdw) == 0)
	  cns_res.y = tdw;

  if (registry_dword_get(key, "cx", &tdw) == 0)
	  cns_res.cx = tdw;

  if (registry_dword_get(key, "cy", &tdw) == 0)
	  cns_res.cy = tdw;

  if (registry_dword_get(key, "lifetime", &tdw) == 0)
	  cns_res.lifetime = tdw;

  if (registry_dword_get(key, "forwardable", &tdw) == 0)
	  cns_res.forwardable = tdw;

  if (registry_dword_get(key, "noaddresses", &tdw) == 0)
   	  cns_res.noaddresses = tdw;

  if (registry_dword_get(key, "alert", &tdw) == 0)
	  cns_res.alert = tdw;

  if (registry_dword_get(key, "beep", &tdw) == 0)
	  cns_res.beep = tdw;

  if (registry_string_get(key, "name", &ts) == 0) {
	strcpy(cns_res.name, ts);
	free(ts);
  }

  if (registry_string_get(key, "realm", &ts) == 0) {
	strcpy(cns_res.realm, ts);
	free(ts);
  }

  if (cns_res.conf_override && (registry_string_get(key, "confname", &ts) == 0)) {
	strcpy(cns_res.confname, ts);
	free(ts);
  } else
	  strcpy(cns_res.confname, cns_res.def_confname);

  if (cns_res.cc_override && (registry_string_get(key, "ccname", &ts) == 0)) {
	strcpy(cns_res.ccname, ts);
	free(ts);
  } else
	  strcpy(cns_res.ccname, cns_res.def_ccname);

  for (i = 0 ; i < FILE_MENU_MAX_LOGINS ; i++) {
    sprintf(tmp, "login_%02d", i);
    if (registry_string_get(key, tmp, &ts) == 0) {
      strcpy(cns_res.logins[i], ts);
      free(ts);
    }
  }

  registry_close(key);
}

/*
 * save all the registry data, creating the keys if needed.
 */
void
cns_save_registry(void)
{
  char    tmp[1024];
  HKEY    key;
  int     i;

  /*
   * First, create the heirachy...  This is gross, but functional
   */
  key = registry_key_create(HKEY_CURRENT_USER, CYGNUS_SOLUTIONS, KEY_WRITE);
  if (key == INVALID_HANDLE_VALUE)
	  return;

  key = registry_key_create(HKEY_CURRENT_USER, KERBNET_SANS_VERSION, KEY_WRITE);
  if (key == INVALID_HANDLE_VALUE)
	  return;
  registry_close(key);

  key = registry_key_create(HKEY_CURRENT_USER, KERBNET_BASE, KEY_WRITE);
  if (key == INVALID_HANDLE_VALUE)
	  return;
  registry_close(key);

  key = registry_key_create(HKEY_CURRENT_USER, KERBNET_CNS_BASE, KEY_WRITE);
  if (key == INVALID_HANDLE_VALUE)
	return;

  registry_dword_set(key, "x", cns_res.x);
  registry_dword_set(key, "y", cns_res.y);
  registry_dword_set(key, "cx", cns_res.cx);
  registry_dword_set(key, "cy", cns_res.cy);

  registry_dword_set(key, "alert", cns_res.alert);
  registry_dword_set(key, "beep", cns_res.beep);
  registry_dword_set(key, "lifetime", cns_res.lifetime);
  registry_dword_set(key, "forwardable", cns_res.forwardable);
  registry_dword_set(key, "noaddresses", cns_res.noaddresses);

  registry_string_set(key, "name", cns_res.name);
  registry_string_set(key, "realm", cns_res.realm);

  if (cns_res.conf_override)
  {
      if (strcmp(cns_res.confname, cns_res.def_confname))
	  registry_string_set(key, "confname", cns_res.confname);
      else
	  registry_value_delete(key, "confname");
  }

  if (cns_res.cc_override)
  {
      if (strcmp(cns_res.ccname, cns_res.def_ccname))
	  registry_string_set(key, "ccname", cns_res.ccname);
      else
	  registry_value_delete(key, "ccname");
  }

  for (i = 0 ; i < FILE_MENU_MAX_LOGINS ; i++)
    if (cns_res.logins[i][0] != '\0') {
      sprintf(tmp, "login_%02d", i);
      registry_string_set(key, tmp, cns_res.logins[i]);
    }

  registry_close(key);
}