2 * uxstore.c: Unix-specific implementation of the interface defined
17 #include <sys/types.h>
23 #define FNLEN PATH_MAX
25 #define FNLEN 1024 /* XXX */
29 INDEX_DIR, INDEX_HOSTKEYS, INDEX_HOSTKEYS_TMP, INDEX_RANDSEED,
30 INDEX_SESSIONDIR, INDEX_SESSION,
33 static const char hex[16] = "0123456789ABCDEF";
35 static char *mungestr(const char *in)
40 in = "Default Settings";
42 ret = out = snewn(3*strlen(in)+1, char);
46 * There are remarkably few punctuation characters that
47 * aren't shell-special in some way or likely to be used as
48 * separators in some file format or another! Hence we use
49 * opt-in for safe characters rather than opt-out for
50 * specific unsafe ones...
52 if (*in!='+' && *in!='-' && *in!='.' && *in!='@' && *in!='_' &&
53 !(*in >= '0' && *in <= '9') &&
54 !(*in >= 'A' && *in <= 'Z') &&
55 !(*in >= 'a' && *in <= 'z')) {
57 *out++ = hex[((unsigned char) *in) >> 4];
58 *out++ = hex[((unsigned char) *in) & 15];
67 static char *unmungestr(const char *in)
70 out = ret = snewn(strlen(in)+1, char);
72 if (*in == '%' && in[1] && in[2]) {
80 *out++ = (i << 4) + j;
90 static void make_filename(char *filename, int index, const char *subname)
94 home = getenv("HOME");
97 strncpy(filename, home, FNLEN);
98 len = strlen(filename);
99 if (index == INDEX_SESSION) {
100 char *munged = mungestr(subname);
101 char *fn = dupprintf("/.putty/sessions/%s", munged);
102 strncpy(filename + len, fn, FNLEN - len);
106 strncpy(filename + len,
107 index == INDEX_DIR ? "/.putty" :
108 index == INDEX_SESSIONDIR ? "/.putty/sessions" :
109 index == INDEX_HOSTKEYS ? "/.putty/sshhostkeys" :
110 index == INDEX_HOSTKEYS_TMP ? "/.putty/sshhostkeys.tmp" :
111 index == INDEX_RANDSEED ? "/.putty/randomseed" :
112 "/.putty/ERROR", FNLEN - len);
114 filename[FNLEN-1] = '\0';
117 void *open_settings_w(const char *sessionname, char **errmsg)
119 char filename[FNLEN];
125 * Start by making sure the .putty directory and its sessions
126 * subdir actually exist. Ignore error returns from mkdir since
127 * they're perfectly likely to be `already exists', and any
128 * other error will trip us up later on so there's no real need
131 make_filename(filename, INDEX_DIR, sessionname);
132 mkdir(filename, 0700);
133 make_filename(filename, INDEX_SESSIONDIR, sessionname);
134 mkdir(filename, 0700);
136 make_filename(filename, INDEX_SESSION, sessionname);
137 fp = fopen(filename, "w");
139 *errmsg = dupprintf("Unable to create %s: %s",
140 filename, strerror(errno));
141 return NULL; /* can't open */
146 void write_setting_s(void *handle, const char *key, const char *value)
148 FILE *fp = (FILE *)handle;
149 fprintf(fp, "%s=%s\n", key, value);
152 void write_setting_i(void *handle, const char *key, int value)
154 FILE *fp = (FILE *)handle;
155 fprintf(fp, "%s=%d\n", key, value);
158 void close_settings_w(void *handle)
160 FILE *fp = (FILE *)handle;
165 * Reading settings, for the moment, is done by retrieving X
166 * resources from the X display. When we introduce disk files, I
167 * think what will happen is that the X resources will override
168 * PuTTY's inbuilt defaults, but that the disk files will then
169 * override those. This isn't optimal, but it's the best I can
170 * immediately work out.
171 * FIXME: the above comment is a bit out of date. Did it happen?
179 static tree234 *xrmtree = NULL;
181 int keycmp(void *av, void *bv)
183 struct keyval *a = (struct keyval *)av;
184 struct keyval *b = (struct keyval *)bv;
185 return strcmp(a->key, b->key);
188 void provide_xrm_string(char *string)
191 struct keyval *xrms, *ret;
193 p = q = strchr(string, ':');
195 fprintf(stderr, "pterm: expected a colon in resource string"
196 " \"%s\"\n", string);
200 while (p > string && p[-1] != '.' && p[-1] != '*')
202 xrms = snew(struct keyval);
203 key = snewn(q-p, char);
207 while (*q && isspace((unsigned char)*q))
209 xrms->value = dupstr(q);
212 xrmtree = newtree234(keycmp);
214 ret = add234(xrmtree, xrms);
216 /* Override an existing string. */
217 del234(xrmtree, ret);
218 add234(xrmtree, xrms);
222 const char *get_setting(const char *key)
224 struct keyval tmp, *ret;
227 ret = find234(xrmtree, &tmp, NULL);
231 return x_get_default(key);
234 void *open_settings_r(const char *sessionname)
236 char filename[FNLEN];
241 make_filename(filename, INDEX_SESSION, sessionname);
242 fp = fopen(filename, "r");
244 return NULL; /* can't open */
246 ret = newtree234(keycmp);
248 while ( (line = fgetline(fp)) ) {
249 char *value = strchr(line, '=');
255 value[strcspn(value, "\r\n")] = '\0'; /* trim trailing NL */
257 kv = snew(struct keyval);
258 kv->key = dupstr(line);
259 kv->value = dupstr(value);
270 char *read_setting_s(void *handle, const char *key, char *buffer, int buflen)
272 tree234 *tree = (tree234 *)handle;
274 struct keyval tmp, *kv;
278 (kv = find234(tree, &tmp, NULL)) != NULL) {
282 val = get_setting(key);
287 strncpy(buffer, val, buflen);
288 buffer[buflen-1] = '\0';
293 int read_setting_i(void *handle, const char *key, int defvalue)
295 tree234 *tree = (tree234 *)handle;
297 struct keyval tmp, *kv;
301 (kv = find234(tree, &tmp, NULL)) != NULL) {
305 val = get_setting(key);
313 int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
315 return !!read_setting_s(handle, name, result->name, sizeof(result->name));
317 int read_setting_filename(void *handle, const char *name, Filename *result)
319 return !!read_setting_s(handle, name, result->path, sizeof(result->path));
322 void write_setting_fontspec(void *handle, const char *name, FontSpec result)
324 write_setting_s(handle, name, result.name);
326 void write_setting_filename(void *handle, const char *name, Filename result)
328 write_setting_s(handle, name, result.path);
331 void close_settings_r(void *handle)
333 tree234 *tree = (tree234 *)handle;
339 while ( (kv = index234(tree, 0)) != NULL) {
341 sfree((char *)kv->key);
342 sfree((char *)kv->value);
349 void del_settings(const char *sessionname)
351 char filename[FNLEN];
352 make_filename(filename, INDEX_SESSION, sessionname);
356 void *enum_settings_start(void)
359 char filename[FNLEN];
361 make_filename(filename, INDEX_SESSIONDIR, NULL);
362 dp = opendir(filename);
367 char *enum_settings_next(void *handle, char *buffer, int buflen)
369 DIR *dp = (DIR *)handle;
372 char fullpath[FNLEN];
376 make_filename(fullpath, INDEX_SESSIONDIR, NULL);
377 len = strlen(fullpath);
379 while ( (de = readdir(dp)) != NULL ) {
382 strncpy(fullpath+len+1, de->d_name, FNLEN-(len+1));
383 fullpath[FNLEN-1] = '\0';
386 if (stat(fullpath, &st) < 0 || !S_ISREG(st.st_mode))
387 continue; /* try another one */
389 unmunged = unmungestr(de->d_name);
390 strncpy(buffer, unmunged, buflen);
391 buffer[buflen-1] = '\0';
399 void enum_settings_finish(void *handle)
401 DIR *dp = (DIR *)handle;
406 * Lines in the host keys file are of the form
408 * type@port:hostname keydata
412 * rsa@22:foovax.example.org 0x23,0x293487364395345345....2343
414 int verify_host_key(const char *hostname, int port,
415 const char *keytype, const char *key)
418 char filename[FNLEN];
422 make_filename(filename, INDEX_HOSTKEYS, NULL);
423 fp = fopen(filename, "r");
425 return 1; /* key does not exist */
428 while ( (line = fgetline(fp)) ) {
433 line[strcspn(line, "\n")] = '\0'; /* strip trailing newline */
436 if (strncmp(p, keytype, i))
444 sprintf(porttext, "%d", port);
445 i = strlen(porttext);
446 if (strncmp(p, porttext, i))
454 i = strlen(hostname);
455 if (strncmp(p, hostname, i))
464 * Found the key. Now just work out whether it's the right
468 ret = 0; /* key matched OK */
470 ret = 2; /* key mismatch */
482 void store_host_key(const char *hostname, int port,
483 const char *keytype, const char *key)
486 char *newtext, *line;
488 char filename[FNLEN], tmpfilename[FNLEN];
490 newtext = dupprintf("%s@%d:%s %s\n", keytype, port, hostname, key);
491 headerlen = 1 + strcspn(newtext, " "); /* count the space too */
494 * Open both the old file and a new file.
496 make_filename(tmpfilename, INDEX_HOSTKEYS_TMP, NULL);
497 wfp = fopen(tmpfilename, "w");
501 make_filename(dir, INDEX_DIR, NULL);
503 wfp = fopen(tmpfilename, "w");
507 make_filename(filename, INDEX_HOSTKEYS, NULL);
508 rfp = fopen(filename, "r");
511 * Copy all lines from the old file to the new one that _don't_
512 * involve the same host key identifier as the one we're adding.
515 while ( (line = fgetline(rfp)) ) {
516 if (strncmp(line, newtext, headerlen))
523 * Now add the new line at the end.
529 rename(tmpfilename, filename);
534 void read_random_seed(noise_consumer_t consumer)
539 make_filename(fname, INDEX_RANDSEED, NULL);
540 fd = open(fname, O_RDONLY);
544 while ( (ret = read(fd, buf, sizeof(buf))) > 0)
550 void write_random_seed(void *data, int len)
555 make_filename(fname, INDEX_RANDSEED, NULL);
557 * Don't truncate the random seed file if it already exists; if
558 * something goes wrong half way through writing it, it would
559 * be better to leave the old data there than to leave it empty.
561 fd = open(fname, O_CREAT | O_WRONLY, 0600);
565 make_filename(dir, INDEX_DIR, NULL);
567 fd = open(fname, O_CREAT | O_WRONLY, 0600);
571 int ret = write(fd, data, len);
574 data = (char *)data + len;
580 void cleanup_all(void)