2 * uxstore.c: Unix-specific implementation of the interface defined
14 #include <sys/types.h>
20 INDEX_DIR, INDEX_HOSTKEYS, INDEX_RANDSEED,
21 INDEX_SESSIONDIR, INDEX_SESSION,
24 static const char hex[16] = "0123456789ABCDEF";
26 static char *mungestr(const char *in)
31 in = "Default Settings";
33 ret = out = snewn(3*strlen(in)+1, char);
37 * There are remarkably few punctuation characters that
38 * aren't shell-special in some way or likely to be used as
39 * separators in some file format or another! Hence we use
40 * opt-in for safe characters rather than opt-out for
41 * specific unsafe ones...
43 if (*in!='+' && *in!='-' && *in!='.' && *in!='@' && *in!='_' &&
44 !(*in >= '0' && *in <= '9') &&
45 !(*in >= 'A' && *in <= 'Z') &&
46 !(*in >= 'a' && *in <= 'z')) {
48 *out++ = hex[((unsigned char) *in) >> 4];
49 *out++ = hex[((unsigned char) *in) & 15];
58 static char *unmungestr(const char *in)
61 out = ret = snewn(strlen(in)+1, char);
63 if (*in == '%' && in[1] && in[2]) {
71 *out++ = (i << 4) + j;
81 static void make_filename(char *filename, int index, const char *subname)
85 home = getenv("HOME");
86 strncpy(filename, home, FILENAME_MAX);
87 len = strlen(filename);
88 if (index == INDEX_SESSION) {
89 char *munged = mungestr(subname);
90 char *fn = dupprintf("/.putty/sessions/%s", munged);
91 strncpy(filename + len, fn, FILENAME_MAX - len);
95 strncpy(filename + len,
96 index == INDEX_DIR ? "/.putty" :
97 index == INDEX_SESSIONDIR ? "/.putty/sessions" :
98 index == INDEX_HOSTKEYS ? "/.putty/sshhostkeys" :
99 index == INDEX_RANDSEED ? "/.putty/randomseed" :
100 "/.putty/ERROR", FILENAME_MAX - len);
102 filename[FILENAME_MAX-1] = '\0';
106 * Read an entire line of text from a file. Return a buffer
107 * malloced to be as big as necessary (caller must free).
109 static char *fgetline(FILE *fp)
111 char *ret = snewn(512, char);
112 int size = 512, len = 0;
113 while (fgets(ret + len, size - len, fp)) {
114 len += strlen(ret + len);
115 if (ret[len-1] == '\n')
116 break; /* got a newline, we're done */
118 ret = sresize(ret, size, char);
120 if (len == 0) { /* first fgets returned NULL */
129 * For the moment, the only existing Unix utility is pterm and that
130 * has no GUI configuration at all, so our write routines need do
131 * nothing. Eventually I suppose these will read and write an rc
132 * file somewhere or other.
135 void *open_settings_w(const char *sessionname)
137 char filename[FILENAME_MAX];
141 * Start by making sure the sessions subdir exists. Ignore the
142 * error return from mkdir since it's perfectly likely to be
143 * `already exists', and any other error will trip us up later
144 * on so there's no real need to catch it now.
146 make_filename(filename, INDEX_SESSIONDIR, sessionname);
147 mkdir(filename, 0700);
149 make_filename(filename, INDEX_SESSION, sessionname);
150 fp = fopen(filename, "w");
152 return NULL; /* can't open */
156 void write_setting_s(void *handle, const char *key, const char *value)
158 FILE *fp = (FILE *)handle;
159 fprintf(fp, "%s=%s\n", key, value);
162 void write_setting_i(void *handle, const char *key, int value)
164 FILE *fp = (FILE *)handle;
165 fprintf(fp, "%s=%d\n", key, value);
168 void close_settings_w(void *handle)
170 FILE *fp = (FILE *)handle;
175 * Reading settings, for the moment, is done by retrieving X
176 * resources from the X display. When we introduce disk files, I
177 * think what will happen is that the X resources will override
178 * PuTTY's inbuilt defaults, but that the disk files will then
179 * override those. This isn't optimal, but it's the best I can
180 * immediately work out.
188 static tree234 *xrmtree = NULL;
190 int keycmp(void *av, void *bv)
192 struct keyval *a = (struct keyval *)av;
193 struct keyval *b = (struct keyval *)bv;
194 return strcmp(a->key, b->key);
197 void provide_xrm_string(char *string)
200 struct keyval *xrms, *ret;
202 p = q = strchr(string, ':');
204 fprintf(stderr, "pterm: expected a colon in resource string"
205 " \"%s\"\n", string);
209 while (p > string && p[-1] != '.' && p[-1] != '*')
211 xrms = snew(struct keyval);
212 key = snewn(q-p, char);
216 while (*q && isspace((unsigned char)*q))
218 xrms->value = dupstr(q);
221 xrmtree = newtree234(keycmp);
223 ret = add234(xrmtree, xrms);
225 /* Override an existing string. */
226 del234(xrmtree, ret);
227 add234(xrmtree, xrms);
231 const char *get_setting(const char *key)
233 struct keyval tmp, *ret;
236 ret = find234(xrmtree, &tmp, NULL);
240 return x_get_default(key);
243 void *open_settings_r(const char *sessionname)
245 char filename[FILENAME_MAX];
250 make_filename(filename, INDEX_SESSION, sessionname);
251 fp = fopen(filename, "r");
253 return NULL; /* can't open */
255 ret = newtree234(keycmp);
257 while ( (line = fgetline(fp)) ) {
258 char *value = strchr(line, '=');
264 value[strcspn(value, "\r\n")] = '\0'; /* trim trailing NL */
266 kv = snew(struct keyval);
267 kv->key = dupstr(line);
268 kv->value = dupstr(value);
279 char *read_setting_s(void *handle, const char *key, char *buffer, int buflen)
281 tree234 *tree = (tree234 *)handle;
283 struct keyval tmp, *kv;
287 (kv = find234(tree, &tmp, NULL)) != NULL) {
291 val = get_setting(key);
296 strncpy(buffer, val, buflen);
297 buffer[buflen-1] = '\0';
302 int read_setting_i(void *handle, const char *key, int defvalue)
304 tree234 *tree = (tree234 *)handle;
306 struct keyval tmp, *kv;
310 (kv = find234(tree, &tmp, NULL)) != NULL) {
314 val = get_setting(key);
322 int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
324 return !!read_setting_s(handle, name, result->name, sizeof(result->name));
326 int read_setting_filename(void *handle, const char *name, Filename *result)
328 return !!read_setting_s(handle, name, result->path, sizeof(result->path));
331 void write_setting_fontspec(void *handle, const char *name, FontSpec result)
333 write_setting_s(handle, name, result.name);
335 void write_setting_filename(void *handle, const char *name, Filename result)
337 write_setting_s(handle, name, result.path);
340 void close_settings_r(void *handle)
342 tree234 *tree = (tree234 *)handle;
348 while ( (kv = index234(tree, 0)) != NULL) {
350 sfree((char *)kv->key);
351 sfree((char *)kv->value);
358 void del_settings(const char *sessionname)
360 char filename[FILENAME_MAX];
361 make_filename(filename, INDEX_SESSION, sessionname);
365 void *enum_settings_start(void)
368 char filename[FILENAME_MAX];
370 make_filename(filename, INDEX_SESSIONDIR, NULL);
371 dp = opendir(filename);
376 char *enum_settings_next(void *handle, char *buffer, int buflen)
378 DIR *dp = (DIR *)handle;
381 char fullpath[FILENAME_MAX];
385 make_filename(fullpath, INDEX_SESSIONDIR, NULL);
386 len = strlen(fullpath);
388 while ( (de = readdir(dp)) != NULL ) {
389 if (len < FILENAME_MAX) {
391 strncpy(fullpath+len+1, de->d_name, FILENAME_MAX-(len+1));
392 fullpath[FILENAME_MAX-1] = '\0';
395 if (stat(fullpath, &st) < 0 || !S_ISREG(st.st_mode))
396 continue; /* try another one */
398 unmunged = unmungestr(de->d_name);
399 strncpy(buffer, unmunged, buflen);
400 buffer[buflen-1] = '\0';
408 void enum_settings_finish(void *handle)
410 DIR *dp = (DIR *)handle;
415 * Lines in the host keys file are of the form
417 * type@port:hostname keydata
421 * rsa@22:foovax.example.org 0x23,0x293487364395345345....2343
423 int verify_host_key(const char *hostname, int port,
424 const char *keytype, const char *key)
427 char filename[FILENAME_MAX];
431 make_filename(filename, INDEX_HOSTKEYS, NULL);
432 fp = fopen(filename, "r");
434 return 1; /* key does not exist */
437 while ( (line = fgetline(fp)) ) {
442 line[strcspn(line, "\n")] = '\0'; /* strip trailing newline */
445 if (strncmp(p, keytype, i))
453 sprintf(porttext, "%d", port);
454 i = strlen(porttext);
455 if (strncmp(p, porttext, i))
463 i = strlen(hostname);
464 if (strncmp(p, hostname, i))
473 * Found the key. Now just work out whether it's the right
477 ret = 0; /* key matched OK */
479 ret = 2; /* key mismatch */
490 void store_host_key(const char *hostname, int port,
491 const char *keytype, const char *key)
495 char filename[FILENAME_MAX];
497 make_filename(filename, INDEX_HOSTKEYS, NULL);
498 fd = open(filename, O_CREAT | O_APPEND | O_RDWR, 0600);
500 char dir[FILENAME_MAX];
502 make_filename(dir, INDEX_DIR, NULL);
504 fd = open(filename, O_CREAT | O_APPEND | O_RDWR, 0600);
510 fp = fdopen(fd, "a");
511 fprintf(fp, "%s@%d:%s %s\n", keytype, port, hostname, key);
515 void read_random_seed(noise_consumer_t consumer)
518 char fname[FILENAME_MAX];
520 make_filename(fname, INDEX_RANDSEED, NULL);
521 fd = open(fname, O_RDONLY);
525 while ( (ret = read(fd, buf, sizeof(buf))) > 0)
531 void write_random_seed(void *data, int len)
534 char fname[FILENAME_MAX];
536 make_filename(fname, INDEX_RANDSEED, NULL);
538 * Don't truncate the random seed file if it already exists; if
539 * something goes wrong half way through writing it, it would
540 * be better to leave the old data there than to leave it empty.
542 fd = open(fname, O_CREAT | O_WRONLY, 0600);
544 char dir[FILENAME_MAX];
546 make_filename(dir, INDEX_DIR, NULL);
548 fd = open(fname, O_CREAT | O_WRONLY, 0600);
552 int ret = write(fd, data, len);
555 data = (char *)data + len;
561 void cleanup_all(void)