2 * uxstore.c: Unix-specific implementation of the interface defined
16 #include <sys/types.h>
22 INDEX_DIR, INDEX_HOSTKEYS, 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_RANDSEED ? "/.putty/randomseed" :
102 "/.putty/ERROR", FILENAME_MAX - len);
104 filename[FILENAME_MAX-1] = '\0';
108 * Read an entire line of text from a file. Return a buffer
109 * malloced to be as big as necessary (caller must free).
111 static char *fgetline(FILE *fp)
113 char *ret = snewn(512, char);
114 int size = 512, len = 0;
115 while (fgets(ret + len, size - len, fp)) {
116 len += strlen(ret + len);
117 if (ret[len-1] == '\n')
118 break; /* got a newline, we're done */
120 ret = sresize(ret, size, char);
122 if (len == 0) { /* first fgets returned NULL */
131 * For the moment, the only existing Unix utility is pterm and that
132 * has no GUI configuration at all, so our write routines need do
133 * nothing. Eventually I suppose these will read and write an rc
134 * file somewhere or other.
137 void *open_settings_w(const char *sessionname, char **errmsg)
139 char filename[FILENAME_MAX];
145 * Start by making sure the .putty directory and its sessions
146 * subdir actually exist. Ignore error returns from mkdir since
147 * they're perfectly likely to be `already exists', and any
148 * other error will trip us up later on so there's no real need
151 make_filename(filename, INDEX_DIR, sessionname);
152 mkdir(filename, 0700);
153 make_filename(filename, INDEX_SESSIONDIR, sessionname);
154 mkdir(filename, 0700);
156 make_filename(filename, INDEX_SESSION, sessionname);
157 fp = fopen(filename, "w");
159 *errmsg = dupprintf("Unable to create %s: %s",
160 filename, strerror(errno));
161 return NULL; /* can't open */
166 void write_setting_s(void *handle, const char *key, const char *value)
168 FILE *fp = (FILE *)handle;
169 fprintf(fp, "%s=%s\n", key, value);
172 void write_setting_i(void *handle, const char *key, int value)
174 FILE *fp = (FILE *)handle;
175 fprintf(fp, "%s=%d\n", key, value);
178 void close_settings_w(void *handle)
180 FILE *fp = (FILE *)handle;
185 * Reading settings, for the moment, is done by retrieving X
186 * resources from the X display. When we introduce disk files, I
187 * think what will happen is that the X resources will override
188 * PuTTY's inbuilt defaults, but that the disk files will then
189 * override those. This isn't optimal, but it's the best I can
190 * immediately work out.
198 static tree234 *xrmtree = NULL;
200 int keycmp(void *av, void *bv)
202 struct keyval *a = (struct keyval *)av;
203 struct keyval *b = (struct keyval *)bv;
204 return strcmp(a->key, b->key);
207 void provide_xrm_string(char *string)
210 struct keyval *xrms, *ret;
212 p = q = strchr(string, ':');
214 fprintf(stderr, "pterm: expected a colon in resource string"
215 " \"%s\"\n", string);
219 while (p > string && p[-1] != '.' && p[-1] != '*')
221 xrms = snew(struct keyval);
222 key = snewn(q-p, char);
226 while (*q && isspace((unsigned char)*q))
228 xrms->value = dupstr(q);
231 xrmtree = newtree234(keycmp);
233 ret = add234(xrmtree, xrms);
235 /* Override an existing string. */
236 del234(xrmtree, ret);
237 add234(xrmtree, xrms);
241 const char *get_setting(const char *key)
243 struct keyval tmp, *ret;
246 ret = find234(xrmtree, &tmp, NULL);
250 return x_get_default(key);
253 void *open_settings_r(const char *sessionname)
255 char filename[FILENAME_MAX];
260 make_filename(filename, INDEX_SESSION, sessionname);
261 fp = fopen(filename, "r");
263 return NULL; /* can't open */
265 ret = newtree234(keycmp);
267 while ( (line = fgetline(fp)) ) {
268 char *value = strchr(line, '=');
274 value[strcspn(value, "\r\n")] = '\0'; /* trim trailing NL */
276 kv = snew(struct keyval);
277 kv->key = dupstr(line);
278 kv->value = dupstr(value);
289 char *read_setting_s(void *handle, const char *key, char *buffer, int buflen)
291 tree234 *tree = (tree234 *)handle;
293 struct keyval tmp, *kv;
297 (kv = find234(tree, &tmp, NULL)) != NULL) {
301 val = get_setting(key);
306 strncpy(buffer, val, buflen);
307 buffer[buflen-1] = '\0';
312 int read_setting_i(void *handle, const char *key, int defvalue)
314 tree234 *tree = (tree234 *)handle;
316 struct keyval tmp, *kv;
320 (kv = find234(tree, &tmp, NULL)) != NULL) {
324 val = get_setting(key);
332 int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
334 return !!read_setting_s(handle, name, result->name, sizeof(result->name));
336 int read_setting_filename(void *handle, const char *name, Filename *result)
338 return !!read_setting_s(handle, name, result->path, sizeof(result->path));
341 void write_setting_fontspec(void *handle, const char *name, FontSpec result)
343 write_setting_s(handle, name, result.name);
345 void write_setting_filename(void *handle, const char *name, Filename result)
347 write_setting_s(handle, name, result.path);
350 void close_settings_r(void *handle)
352 tree234 *tree = (tree234 *)handle;
358 while ( (kv = index234(tree, 0)) != NULL) {
360 sfree((char *)kv->key);
361 sfree((char *)kv->value);
368 void del_settings(const char *sessionname)
370 char filename[FILENAME_MAX];
371 make_filename(filename, INDEX_SESSION, sessionname);
375 void *enum_settings_start(void)
378 char filename[FILENAME_MAX];
380 make_filename(filename, INDEX_SESSIONDIR, NULL);
381 dp = opendir(filename);
386 char *enum_settings_next(void *handle, char *buffer, int buflen)
388 DIR *dp = (DIR *)handle;
391 char fullpath[FILENAME_MAX];
395 make_filename(fullpath, INDEX_SESSIONDIR, NULL);
396 len = strlen(fullpath);
398 while ( (de = readdir(dp)) != NULL ) {
399 if (len < FILENAME_MAX) {
401 strncpy(fullpath+len+1, de->d_name, FILENAME_MAX-(len+1));
402 fullpath[FILENAME_MAX-1] = '\0';
405 if (stat(fullpath, &st) < 0 || !S_ISREG(st.st_mode))
406 continue; /* try another one */
408 unmunged = unmungestr(de->d_name);
409 strncpy(buffer, unmunged, buflen);
410 buffer[buflen-1] = '\0';
418 void enum_settings_finish(void *handle)
420 DIR *dp = (DIR *)handle;
425 * Lines in the host keys file are of the form
427 * type@port:hostname keydata
431 * rsa@22:foovax.example.org 0x23,0x293487364395345345....2343
433 int verify_host_key(const char *hostname, int port,
434 const char *keytype, const char *key)
437 char filename[FILENAME_MAX];
441 make_filename(filename, INDEX_HOSTKEYS, NULL);
442 fp = fopen(filename, "r");
444 return 1; /* key does not exist */
447 while ( (line = fgetline(fp)) ) {
452 line[strcspn(line, "\n")] = '\0'; /* strip trailing newline */
455 if (strncmp(p, keytype, i))
463 sprintf(porttext, "%d", port);
464 i = strlen(porttext);
465 if (strncmp(p, porttext, i))
473 i = strlen(hostname);
474 if (strncmp(p, hostname, i))
483 * Found the key. Now just work out whether it's the right
487 ret = 0; /* key matched OK */
489 ret = 2; /* key mismatch */
500 void store_host_key(const char *hostname, int port,
501 const char *keytype, const char *key)
505 char filename[FILENAME_MAX];
507 make_filename(filename, INDEX_HOSTKEYS, NULL);
508 fd = open(filename, O_CREAT | O_APPEND | O_RDWR, 0600);
510 char dir[FILENAME_MAX];
512 make_filename(dir, INDEX_DIR, NULL);
514 fd = open(filename, O_CREAT | O_APPEND | O_RDWR, 0600);
520 fp = fdopen(fd, "a");
521 fprintf(fp, "%s@%d:%s %s\n", keytype, port, hostname, key);
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)