2 * uxstore.c: Unix-specific implementation of the interface defined
17 #include <sys/types.h>
24 #define FNLEN PATH_MAX
26 #define FNLEN 1024 /* XXX */
30 INDEX_DIR
, INDEX_HOSTKEYS
, INDEX_HOSTKEYS_TMP
, INDEX_RANDSEED
,
31 INDEX_SESSIONDIR
, INDEX_SESSION
,
34 static const char hex
[16] = "0123456789ABCDEF";
36 static char *mungestr(const char *in
)
41 in
= "Default Settings";
43 ret
= out
= snewn(3*strlen(in
)+1, char);
47 * There are remarkably few punctuation characters that
48 * aren't shell-special in some way or likely to be used as
49 * separators in some file format or another! Hence we use
50 * opt-in for safe characters rather than opt-out for
51 * specific unsafe ones...
53 if (*in
!='+' && *in
!='-' && *in
!='.' && *in
!='@' && *in
!='_' &&
54 !(*in
>= '0' && *in
<= '9') &&
55 !(*in
>= 'A' && *in
<= 'Z') &&
56 !(*in
>= 'a' && *in
<= 'z')) {
58 *out
++ = hex
[((unsigned char) *in
) >> 4];
59 *out
++ = hex
[((unsigned char) *in
) & 15];
68 static char *unmungestr(const char *in
)
71 out
= ret
= snewn(strlen(in
)+1, char);
73 if (*in
== '%' && in
[1] && in
[2]) {
81 *out
++ = (i
<< 4) + j
;
91 static char *make_filename(int index
, const char *subname
)
93 char *env
, *tmp
, *ret
;
96 * Allow override of the PuTTY configuration location, and of
97 * specific subparts of it, by means of environment variables.
99 if (index
== INDEX_DIR
) {
102 env
= getenv("PUTTYDIR");
105 env
= getenv("HOME");
107 return dupprintf("%s/.putty", env
);
108 pwd
= getpwuid(getuid());
109 if (pwd
&& pwd
->pw_dir
)
110 return dupprintf("%s/.putty", pwd
->pw_dir
);
111 return dupstr("/.putty");
113 if (index
== INDEX_SESSIONDIR
) {
114 env
= getenv("PUTTYSESSIONS");
117 tmp
= make_filename(INDEX_DIR
, NULL
);
118 ret
= dupprintf("%s/sessions", tmp
);
122 if (index
== INDEX_SESSION
) {
123 char *munged
= mungestr(subname
);
124 tmp
= make_filename(INDEX_SESSIONDIR
, NULL
);
125 ret
= dupprintf("%s/%s", tmp
, munged
);
130 if (index
== INDEX_HOSTKEYS
) {
131 env
= getenv("PUTTYSSHHOSTKEYS");
134 tmp
= make_filename(INDEX_DIR
, NULL
);
135 ret
= dupprintf("%s/sshhostkeys", tmp
);
139 if (index
== INDEX_HOSTKEYS_TMP
) {
140 tmp
= make_filename(INDEX_HOSTKEYS
, NULL
);
141 ret
= dupprintf("%s.tmp", tmp
);
145 if (index
== INDEX_RANDSEED
) {
146 env
= getenv("PUTTYRANDOMSEED");
149 tmp
= make_filename(INDEX_DIR
, NULL
);
150 ret
= dupprintf("%s/randomseed", tmp
);
154 tmp
= make_filename(INDEX_DIR
, NULL
);
155 ret
= dupprintf("%s/ERROR", tmp
);
160 void *open_settings_w(const char *sessionname
, char **errmsg
)
168 * Start by making sure the .putty directory and its sessions
169 * subdir actually exist. Ignore error returns from mkdir since
170 * they're perfectly likely to be `already exists', and any
171 * other error will trip us up later on so there's no real need
174 filename
= make_filename(INDEX_SESSIONDIR
, NULL
);
175 if (mkdir(filename
, 0700) != 0) {
176 char *filename2
= make_filename(INDEX_DIR
, NULL
);
177 mkdir(filename2
, 0700);
179 mkdir(filename
, 0700);
183 filename
= make_filename(INDEX_SESSION
, sessionname
);
184 fp
= fopen(filename
, "w");
186 *errmsg
= dupprintf("Unable to create %s: %s",
187 filename
, strerror(errno
));
189 return NULL
; /* can't open */
195 void write_setting_s(void *handle
, const char *key
, const char *value
)
197 FILE *fp
= (FILE *)handle
;
198 fprintf(fp
, "%s=%s\n", key
, value
);
201 void write_setting_i(void *handle
, const char *key
, int value
)
203 FILE *fp
= (FILE *)handle
;
204 fprintf(fp
, "%s=%d\n", key
, value
);
207 void close_settings_w(void *handle
)
209 FILE *fp
= (FILE *)handle
;
214 * Reading settings, for the moment, is done by retrieving X
215 * resources from the X display. When we introduce disk files, I
216 * think what will happen is that the X resources will override
217 * PuTTY's inbuilt defaults, but that the disk files will then
218 * override those. This isn't optimal, but it's the best I can
219 * immediately work out.
220 * FIXME: the above comment is a bit out of date. Did it happen?
228 static tree234
*xrmtree
= NULL
;
230 int keycmp(void *av
, void *bv
)
232 struct keyval
*a
= (struct keyval
*)av
;
233 struct keyval
*b
= (struct keyval
*)bv
;
234 return strcmp(a
->key
, b
->key
);
237 void provide_xrm_string(char *string
)
240 struct keyval
*xrms
, *ret
;
242 p
= q
= strchr(string
, ':');
244 fprintf(stderr
, "pterm: expected a colon in resource string"
245 " \"%s\"\n", string
);
249 while (p
> string
&& p
[-1] != '.' && p
[-1] != '*')
251 xrms
= snew(struct keyval
);
252 key
= snewn(q
-p
, char);
256 while (*q
&& isspace((unsigned char)*q
))
258 xrms
->value
= dupstr(q
);
261 xrmtree
= newtree234(keycmp
);
263 ret
= add234(xrmtree
, xrms
);
265 /* Override an existing string. */
266 del234(xrmtree
, ret
);
267 add234(xrmtree
, xrms
);
271 const char *get_setting(const char *key
)
273 struct keyval tmp
, *ret
;
276 ret
= find234(xrmtree
, &tmp
, NULL
);
280 return x_get_default(key
);
283 void *open_settings_r(const char *sessionname
)
290 filename
= make_filename(INDEX_SESSION
, sessionname
);
291 fp
= fopen(filename
, "r");
294 return NULL
; /* can't open */
296 ret
= newtree234(keycmp
);
298 while ( (line
= fgetline(fp
)) ) {
299 char *value
= strchr(line
, '=');
305 value
[strcspn(value
, "\r\n")] = '\0'; /* trim trailing NL */
307 kv
= snew(struct keyval
);
308 kv
->key
= dupstr(line
);
309 kv
->value
= dupstr(value
);
320 char *read_setting_s(void *handle
, const char *key
, char *buffer
, int buflen
)
322 tree234
*tree
= (tree234
*)handle
;
324 struct keyval tmp
, *kv
;
328 (kv
= find234(tree
, &tmp
, NULL
)) != NULL
) {
332 val
= get_setting(key
);
337 strncpy(buffer
, val
, buflen
);
338 buffer
[buflen
-1] = '\0';
343 int read_setting_i(void *handle
, const char *key
, int defvalue
)
345 tree234
*tree
= (tree234
*)handle
;
347 struct keyval tmp
, *kv
;
351 (kv
= find234(tree
, &tmp
, NULL
)) != NULL
) {
355 val
= get_setting(key
);
363 int read_setting_fontspec(void *handle
, const char *name
, FontSpec
*result
)
366 * In GTK1-only PuTTY, we used to store font names simply as a
367 * valid X font description string (logical or alias), under a
368 * bare key such as "Font".
370 * In GTK2 PuTTY, we have a prefix system where "client:"
371 * indicates a Pango font and "server:" an X one; existing
372 * configuration needs to be reinterpreted as having the
373 * "server:" prefix, so we change the storage key from the
374 * provided name string (e.g. "Font") to a suffixed one
377 char *suffname
= dupcat(name
, "Name", NULL
);
378 if (read_setting_s(handle
, suffname
, result
->name
, sizeof(result
->name
))) {
380 return TRUE
; /* got new-style name */
384 /* Fall back to old-style name. */
385 memcpy(result
->name
, "server:", 7);
386 if (!read_setting_s(handle
, name
,
387 result
->name
+ 7, sizeof(result
->name
) - 7) ||
389 result
->name
[0] = '\0';
395 int read_setting_filename(void *handle
, const char *name
, Filename
*result
)
397 return !!read_setting_s(handle
, name
, result
->path
, sizeof(result
->path
));
400 void write_setting_fontspec(void *handle
, const char *name
, FontSpec result
)
403 * read_setting_fontspec had to handle two cases, but when
404 * writing our settings back out we simply always generate the
407 char *suffname
= dupcat(name
, "Name", NULL
);
408 write_setting_s(handle
, suffname
, result
.name
);
411 void write_setting_filename(void *handle
, const char *name
, Filename result
)
413 write_setting_s(handle
, name
, result
.path
);
416 void close_settings_r(void *handle
)
418 tree234
*tree
= (tree234
*)handle
;
424 while ( (kv
= index234(tree
, 0)) != NULL
) {
426 sfree((char *)kv
->key
);
427 sfree((char *)kv
->value
);
434 void del_settings(const char *sessionname
)
437 filename
= make_filename(INDEX_SESSION
, sessionname
);
442 void *enum_settings_start(void)
447 filename
= make_filename(INDEX_SESSIONDIR
, NULL
);
448 dp
= opendir(filename
);
454 char *enum_settings_next(void *handle
, char *buffer
, int buflen
)
456 DIR *dp
= (DIR *)handle
;
460 int maxlen
, thislen
, len
;
463 fullpath
= make_filename(INDEX_SESSIONDIR
, NULL
);
464 maxlen
= len
= strlen(fullpath
);
466 while ( (de
= readdir(dp
)) != NULL
) {
467 thislen
= len
+ 1 + strlen(de
->d_name
);
468 if (maxlen
< thislen
) {
470 fullpath
= sresize(fullpath
, maxlen
+1, char);
473 strncpy(fullpath
+len
+1, de
->d_name
, thislen
- (len
+1));
474 fullpath
[thislen
] = '\0';
476 if (stat(fullpath
, &st
) < 0 || !S_ISREG(st
.st_mode
))
477 continue; /* try another one */
479 unmunged
= unmungestr(de
->d_name
);
480 strncpy(buffer
, unmunged
, buflen
);
481 buffer
[buflen
-1] = '\0';
491 void enum_settings_finish(void *handle
)
493 DIR *dp
= (DIR *)handle
;
498 * Lines in the host keys file are of the form
500 * type@port:hostname keydata
504 * rsa@22:foovax.example.org 0x23,0x293487364395345345....2343
506 int verify_host_key(const char *hostname
, int port
,
507 const char *keytype
, const char *key
)
514 filename
= make_filename(INDEX_HOSTKEYS
, NULL
);
515 fp
= fopen(filename
, "r");
518 return 1; /* key does not exist */
521 while ( (line
= fgetline(fp
)) ) {
526 line
[strcspn(line
, "\n")] = '\0'; /* strip trailing newline */
529 if (strncmp(p
, keytype
, i
))
537 sprintf(porttext
, "%d", port
);
538 i
= strlen(porttext
);
539 if (strncmp(p
, porttext
, i
))
547 i
= strlen(hostname
);
548 if (strncmp(p
, hostname
, i
))
557 * Found the key. Now just work out whether it's the right
561 ret
= 0; /* key matched OK */
563 ret
= 2; /* key mismatch */
575 void store_host_key(const char *hostname
, int port
,
576 const char *keytype
, const char *key
)
579 char *newtext
, *line
;
581 char *filename
, *tmpfilename
;
583 newtext
= dupprintf("%s@%d:%s %s\n", keytype
, port
, hostname
, key
);
584 headerlen
= 1 + strcspn(newtext
, " "); /* count the space too */
587 * Open both the old file and a new file.
589 tmpfilename
= make_filename(INDEX_HOSTKEYS_TMP
, NULL
);
590 wfp
= fopen(tmpfilename
, "w");
594 dir
= make_filename(INDEX_DIR
, NULL
);
598 wfp
= fopen(tmpfilename
, "w");
604 filename
= make_filename(INDEX_HOSTKEYS
, NULL
);
605 rfp
= fopen(filename
, "r");
608 * Copy all lines from the old file to the new one that _don't_
609 * involve the same host key identifier as the one we're adding.
612 while ( (line
= fgetline(rfp
)) ) {
613 if (strncmp(line
, newtext
, headerlen
))
620 * Now add the new line at the end.
626 rename(tmpfilename
, filename
);
633 void read_random_seed(noise_consumer_t consumer
)
638 fname
= make_filename(INDEX_RANDSEED
, NULL
);
639 fd
= open(fname
, O_RDONLY
);
644 while ( (ret
= read(fd
, buf
, sizeof(buf
))) > 0)
650 void write_random_seed(void *data
, int len
)
655 fname
= make_filename(INDEX_RANDSEED
, NULL
);
657 * Don't truncate the random seed file if it already exists; if
658 * something goes wrong half way through writing it, it would
659 * be better to leave the old data there than to leave it empty.
661 fd
= open(fname
, O_CREAT
| O_WRONLY
, 0600);
665 dir
= make_filename(INDEX_DIR
, NULL
);
669 fd
= open(fname
, O_CREAT
| O_WRONLY
, 0600);
673 int ret
= write(fd
, data
, len
);
676 data
= (char *)data
+ len
;
683 void cleanup_all(void)