2 * uxstore.c: Unix-specific implementation of the interface defined
12 #include <sys/types.h>
18 * For the moment, the only existing Unix utility is pterm and that
19 * has no GUI configuration at all, so our write routines need do
20 * nothing. Eventually I suppose these will read and write an rc
21 * file somewhere or other.
24 void *open_settings_w(char *sessionname)
29 void write_setting_s(void *handle, char *key, char *value)
33 void write_setting_i(void *handle, char *key, int value)
37 void close_settings_w(void *handle)
42 * Reading settings, for the moment, is done by retrieving X
43 * resources from the X display. When we introduce disk files, I
44 * think what will happen is that the X resources will override
45 * PuTTY's inbuilt defaults, but that the disk files will then
46 * override those. This isn't optimal, but it's the best I can
47 * immediately work out.
55 static tree234 *xrmtree = NULL;
57 int xrmcmp(void *av, void *bv)
59 struct xrm_string *a = (struct xrm_string *)av;
60 struct xrm_string *b = (struct xrm_string *)bv;
61 return strcmp(a->key, b->key);
64 void provide_xrm_string(char *string)
67 struct xrm_string *xrms, *ret;
69 p = q = strchr(string, ':');
71 fprintf(stderr, "pterm: expected a colon in resource string"
76 while (p > string && p[-1] != '.' && p[-1] != '*')
78 xrms = smalloc(sizeof(struct xrm_string));
79 xrms->key = smalloc(q-p);
80 memcpy(xrms->key, p, q-p);
81 xrms->key[q-p-1] = '\0';
82 while (*q && isspace(*q))
84 xrms->value = dupstr(q);
87 xrmtree = newtree234(xrmcmp);
89 ret = add234(xrmtree, xrms);
91 /* Override an existing string. */
93 add234(xrmtree, xrms);
97 char *get_setting(char *key)
99 struct xrm_string tmp, *ret;
102 ret = find234(xrmtree, &tmp, NULL);
106 return x_get_default(key);
109 void *open_settings_r(char *sessionname)
111 static int thing_to_return_an_arbitrary_non_null_pointer_to;
112 return &thing_to_return_an_arbitrary_non_null_pointer_to;
115 char *read_setting_s(void *handle, char *key, char *buffer, int buflen)
117 char *val = get_setting(key);
121 strncpy(buffer, val, buflen);
122 buffer[buflen-1] = '\0';
127 int read_setting_i(void *handle, char *key, int defvalue)
129 char *val = get_setting(key);
136 void close_settings_r(void *handle)
140 void del_settings(char *sessionname)
144 void *enum_settings_start(void)
149 char *enum_settings_next(void *handle, char *buffer, int buflen)
154 void enum_settings_finish(void *handle)
159 INDEX_DIR, INDEX_HOSTKEYS, INDEX_RANDSEED
162 static void make_filename(char *filename, int index)
166 home = getenv("HOME");
167 strncpy(filename, home, FILENAME_MAX);
168 len = strlen(filename);
169 strncpy(filename + len,
170 index == INDEX_DIR ? "/.putty" :
171 index == INDEX_HOSTKEYS ? "/.putty/sshhostkeys" :
172 index == INDEX_RANDSEED ? "/.putty/randomseed" :
173 "/.putty/ERROR", FILENAME_MAX - len);
174 filename[FILENAME_MAX-1] = '\0';
178 * Read an entire line of text from a file. Return a buffer
179 * malloced to be as big as necessary (caller must free).
181 static char *fgetline(FILE *fp)
183 char *ret = smalloc(512);
184 int size = 512, len = 0;
185 while (fgets(ret + len, size - len, fp)) {
186 len += strlen(ret + len);
187 if (ret[len-1] == '\n')
188 break; /* got a newline, we're done */
190 ret = srealloc(ret, size);
192 if (len == 0) { /* first fgets returned NULL */
201 * Lines in the host keys file are of the form
203 * type@port:hostname keydata
207 * rsa@22:foovax.example.org 0x23,0x293487364395345345....2343
209 int verify_host_key(char *hostname, int port, char *keytype, char *key)
212 char filename[FILENAME_MAX];
216 make_filename(filename, INDEX_HOSTKEYS);
217 fp = fopen(filename, "r");
219 return 1; /* key does not exist */
222 while ( (line = fgetline(fp)) ) {
227 line[strcspn(line, "\n")] = '\0'; /* strip trailing newline */
230 if (strncmp(p, keytype, i))
238 sprintf(porttext, "%d", port);
239 i = strlen(porttext);
240 if (strncmp(p, porttext, i))
248 i = strlen(hostname);
249 if (strncmp(p, hostname, i))
258 * Found the key. Now just work out whether it's the right
262 ret = 0; /* key matched OK */
264 ret = 2; /* key mismatch */
275 void store_host_key(char *hostname, int port, char *keytype, char *key)
279 char filename[FILENAME_MAX];
281 make_filename(filename, INDEX_HOSTKEYS);
282 fd = open(filename, O_CREAT | O_APPEND | O_RDWR, 0600);
284 char dir[FILENAME_MAX];
286 make_filename(dir, INDEX_DIR);
288 fd = open(filename, O_CREAT | O_APPEND | O_RDWR, 0600);
294 fp = fdopen(fd, "a");
295 fprintf(fp, "%s@%d:%s %s\n", keytype, port, hostname, key);
299 void read_random_seed(noise_consumer_t consumer)
302 char fname[FILENAME_MAX];
304 make_filename(fname, INDEX_RANDSEED);
305 fd = open(fname, O_RDONLY);
309 while ( (ret = read(fd, buf, sizeof(buf))) > 0)
315 void write_random_seed(void *data, int len)
318 char fname[FILENAME_MAX];
320 make_filename(fname, INDEX_RANDSEED);
322 * Don't truncate the random seed file if it already exists; if
323 * something goes wrong half way through writing it, it would
324 * be better to leave the old data there than to leave it empty.
326 fd = open(fname, O_CREAT | O_WRONLY, 0600);
328 char dir[FILENAME_MAX];
330 make_filename(dir, INDEX_DIR);
332 fd = open(fname, O_CREAT | O_WRONLY, 0600);
336 int ret = write(fd, data, len);
339 data = (char *)data + len;
345 void cleanup_all(void)