2 * uxstore.c: Unix-specific implementation of the interface defined
16 #include <sys/types.h>
22 INDEX_DIR, INDEX_HOSTKEYS, INDEX_HOSTKEYS_TMP, INDEX_RANDSEED,
23 INDEX_SESSIONDIR, INDEX_SESSION,
26 static const char hex[16] = "0123456789ABCDEF";
28 static char *mungestr(const char *in)
33 in = "Default Settings";
35 ret = out = snewn(3*strlen(in)+1, char);
39 * There are remarkably few punctuation characters that
40 * aren't shell-special in some way or likely to be used as
41 * separators in some file format or another! Hence we use
42 * opt-in for safe characters rather than opt-out for
43 * specific unsafe ones...
45 if (*in!='+' && *in!='-' && *in!='.' && *in!='@' && *in!='_' &&
46 !(*in >= '0' && *in <= '9') &&
47 !(*in >= 'A' && *in <= 'Z') &&
48 !(*in >= 'a' && *in <= 'z')) {
50 *out++ = hex[((unsigned char) *in) >> 4];
51 *out++ = hex[((unsigned char) *in) & 15];
60 static char *unmungestr(const char *in)
63 out = ret = snewn(strlen(in)+1, char);
65 if (*in == '%' && in[1] && in[2]) {
73 *out++ = (i << 4) + j;
83 static void make_filename(char *filename, int index, const char *subname)
87 home = getenv("HOME");
88 strncpy(filename, home, FILENAME_MAX);
89 len = strlen(filename);
90 if (index == INDEX_SESSION) {
91 char *munged = mungestr(subname);
92 char *fn = dupprintf("/.putty/sessions/%s", munged);
93 strncpy(filename + len, fn, FILENAME_MAX - len);
97 strncpy(filename + len,
98 index == INDEX_DIR ? "/.putty" :
99 index == INDEX_SESSIONDIR ? "/.putty/sessions" :
100 index == INDEX_HOSTKEYS ? "/.putty/sshhostkeys" :
101 index == INDEX_HOSTKEYS_TMP ? "/.putty/sshhostkeys.tmp" :
102 index == INDEX_RANDSEED ? "/.putty/randomseed" :
103 "/.putty/ERROR", FILENAME_MAX - len);
105 filename[FILENAME_MAX-1] = '\0';
108 void *open_settings_w(const char *sessionname, char **errmsg)
110 char filename[FILENAME_MAX];
116 * Start by making sure the .putty directory and its sessions
117 * subdir actually exist. Ignore error returns from mkdir since
118 * they're perfectly likely to be `already exists', and any
119 * other error will trip us up later on so there's no real need
122 make_filename(filename, INDEX_DIR, sessionname);
123 mkdir(filename, 0700);
124 make_filename(filename, INDEX_SESSIONDIR, sessionname);
125 mkdir(filename, 0700);
127 make_filename(filename, INDEX_SESSION, sessionname);
128 fp = fopen(filename, "w");
130 *errmsg = dupprintf("Unable to create %s: %s",
131 filename, strerror(errno));
132 return NULL; /* can't open */
137 void write_setting_s(void *handle, const char *key, const char *value)
139 FILE *fp = (FILE *)handle;
140 fprintf(fp, "%s=%s\n", key, value);
143 void write_setting_i(void *handle, const char *key, int value)
145 FILE *fp = (FILE *)handle;
146 fprintf(fp, "%s=%d\n", key, value);
149 void close_settings_w(void *handle)
151 FILE *fp = (FILE *)handle;
156 * Reading settings, for the moment, is done by retrieving X
157 * resources from the X display. When we introduce disk files, I
158 * think what will happen is that the X resources will override
159 * PuTTY's inbuilt defaults, but that the disk files will then
160 * override those. This isn't optimal, but it's the best I can
161 * immediately work out.
162 * FIXME: the above comment is a bit out of date. Did it happen?
170 static tree234 *xrmtree = NULL;
172 int keycmp(void *av, void *bv)
174 struct keyval *a = (struct keyval *)av;
175 struct keyval *b = (struct keyval *)bv;
176 return strcmp(a->key, b->key);
179 void provide_xrm_string(char *string)
182 struct keyval *xrms, *ret;
184 p = q = strchr(string, ':');
186 fprintf(stderr, "pterm: expected a colon in resource string"
187 " \"%s\"\n", string);
191 while (p > string && p[-1] != '.' && p[-1] != '*')
193 xrms = snew(struct keyval);
194 key = snewn(q-p, char);
198 while (*q && isspace((unsigned char)*q))
200 xrms->value = dupstr(q);
203 xrmtree = newtree234(keycmp);
205 ret = add234(xrmtree, xrms);
207 /* Override an existing string. */
208 del234(xrmtree, ret);
209 add234(xrmtree, xrms);
213 const char *get_setting(const char *key)
215 struct keyval tmp, *ret;
218 ret = find234(xrmtree, &tmp, NULL);
222 return x_get_default(key);
225 void *open_settings_r(const char *sessionname)
227 char filename[FILENAME_MAX];
232 make_filename(filename, INDEX_SESSION, sessionname);
233 fp = fopen(filename, "r");
235 return NULL; /* can't open */
237 ret = newtree234(keycmp);
239 while ( (line = fgetline(fp)) ) {
240 char *value = strchr(line, '=');
246 value[strcspn(value, "\r\n")] = '\0'; /* trim trailing NL */
248 kv = snew(struct keyval);
249 kv->key = dupstr(line);
250 kv->value = dupstr(value);
261 char *read_setting_s(void *handle, const char *key, char *buffer, int buflen)
263 tree234 *tree = (tree234 *)handle;
265 struct keyval tmp, *kv;
269 (kv = find234(tree, &tmp, NULL)) != NULL) {
273 val = get_setting(key);
278 strncpy(buffer, val, buflen);
279 buffer[buflen-1] = '\0';
284 int read_setting_i(void *handle, const char *key, int defvalue)
286 tree234 *tree = (tree234 *)handle;
288 struct keyval tmp, *kv;
292 (kv = find234(tree, &tmp, NULL)) != NULL) {
296 val = get_setting(key);
304 int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
306 return !!read_setting_s(handle, name, result->name, sizeof(result->name));
308 int read_setting_filename(void *handle, const char *name, Filename *result)
310 return !!read_setting_s(handle, name, result->path, sizeof(result->path));
313 void write_setting_fontspec(void *handle, const char *name, FontSpec result)
315 write_setting_s(handle, name, result.name);
317 void write_setting_filename(void *handle, const char *name, Filename result)
319 write_setting_s(handle, name, result.path);
322 void close_settings_r(void *handle)
324 tree234 *tree = (tree234 *)handle;
330 while ( (kv = index234(tree, 0)) != NULL) {
332 sfree((char *)kv->key);
333 sfree((char *)kv->value);
340 void del_settings(const char *sessionname)
342 char filename[FILENAME_MAX];
343 make_filename(filename, INDEX_SESSION, sessionname);
347 void *enum_settings_start(void)
350 char filename[FILENAME_MAX];
352 make_filename(filename, INDEX_SESSIONDIR, NULL);
353 dp = opendir(filename);
358 char *enum_settings_next(void *handle, char *buffer, int buflen)
360 DIR *dp = (DIR *)handle;
363 char fullpath[FILENAME_MAX];
367 make_filename(fullpath, INDEX_SESSIONDIR, NULL);
368 len = strlen(fullpath);
370 while ( (de = readdir(dp)) != NULL ) {
371 if (len < FILENAME_MAX) {
373 strncpy(fullpath+len+1, de->d_name, FILENAME_MAX-(len+1));
374 fullpath[FILENAME_MAX-1] = '\0';
377 if (stat(fullpath, &st) < 0 || !S_ISREG(st.st_mode))
378 continue; /* try another one */
380 unmunged = unmungestr(de->d_name);
381 strncpy(buffer, unmunged, buflen);
382 buffer[buflen-1] = '\0';
390 void enum_settings_finish(void *handle)
392 DIR *dp = (DIR *)handle;
397 * Lines in the host keys file are of the form
399 * type@port:hostname keydata
403 * rsa@22:foovax.example.org 0x23,0x293487364395345345....2343
405 int verify_host_key(const char *hostname, int port,
406 const char *keytype, const char *key)
409 char filename[FILENAME_MAX];
413 make_filename(filename, INDEX_HOSTKEYS, NULL);
414 fp = fopen(filename, "r");
416 return 1; /* key does not exist */
419 while ( (line = fgetline(fp)) ) {
424 line[strcspn(line, "\n")] = '\0'; /* strip trailing newline */
427 if (strncmp(p, keytype, i))
435 sprintf(porttext, "%d", port);
436 i = strlen(porttext);
437 if (strncmp(p, porttext, i))
445 i = strlen(hostname);
446 if (strncmp(p, hostname, i))
455 * Found the key. Now just work out whether it's the right
459 ret = 0; /* key matched OK */
461 ret = 2; /* key mismatch */
473 void store_host_key(const char *hostname, int port,
474 const char *keytype, const char *key)
477 char *newtext, *line;
479 char filename[FILENAME_MAX], tmpfilename[FILENAME_MAX];
481 newtext = dupprintf("%s@%d:%s %s\n", keytype, port, hostname, key);
482 headerlen = 1 + strcspn(newtext, " "); /* count the space too */
485 * Open both the old file and a new file.
487 make_filename(tmpfilename, INDEX_HOSTKEYS_TMP, NULL);
488 wfp = fopen(tmpfilename, "w");
490 char dir[FILENAME_MAX];
492 make_filename(dir, INDEX_DIR, NULL);
494 wfp = fopen(tmpfilename, "w");
498 make_filename(filename, INDEX_HOSTKEYS, NULL);
499 rfp = fopen(filename, "r");
502 * Copy all lines from the old file to the new one that _don't_
503 * involve the same host key identifier as the one we're adding.
506 while ( (line = fgetline(rfp)) ) {
507 if (strncmp(line, newtext, headerlen))
514 * Now add the new line at the end.
520 rename(tmpfilename, filename);
525 void read_random_seed(noise_consumer_t consumer)
528 char fname[FILENAME_MAX];
530 make_filename(fname, INDEX_RANDSEED, NULL);
531 fd = open(fname, O_RDONLY);
535 while ( (ret = read(fd, buf, sizeof(buf))) > 0)
541 void write_random_seed(void *data, int len)
544 char fname[FILENAME_MAX];
546 make_filename(fname, INDEX_RANDSEED, NULL);
548 * Don't truncate the random seed file if it already exists; if
549 * something goes wrong half way through writing it, it would
550 * be better to leave the old data there than to leave it empty.
552 fd = open(fname, O_CREAT | O_WRONLY, 0600);
554 char dir[FILENAME_MAX];
556 make_filename(dir, INDEX_DIR, NULL);
558 fd = open(fname, O_CREAT | O_WRONLY, 0600);
562 int ret = write(fd, data, len);
565 data = (char *)data + len;
571 void cleanup_all(void)