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");
90 strncpy(filename
, home
, FILENAME_MAX
);
91 len
= strlen(filename
);
92 if (index
== INDEX_SESSION
) {
93 char *munged
= mungestr(subname
);
94 char *fn
= dupprintf("/.putty/sessions/%s", munged
);
95 strncpy(filename
+ len
, fn
, FILENAME_MAX
- len
);
99 strncpy(filename
+ len
,
100 index
== INDEX_DIR ?
"/.putty" :
101 index
== INDEX_SESSIONDIR ?
"/.putty/sessions" :
102 index
== INDEX_HOSTKEYS ?
"/.putty/sshhostkeys" :
103 index
== INDEX_HOSTKEYS_TMP ?
"/.putty/sshhostkeys.tmp" :
104 index
== INDEX_RANDSEED ?
"/.putty/randomseed" :
105 "/.putty/ERROR", FILENAME_MAX
- len
);
107 filename
[FILENAME_MAX
-1] = '\0';
110 void *open_settings_w(const char *sessionname
, char **errmsg
)
112 char filename
[FILENAME_MAX
];
118 * Start by making sure the .putty directory and its sessions
119 * subdir actually exist. Ignore error returns from mkdir since
120 * they're perfectly likely to be `already exists', and any
121 * other error will trip us up later on so there's no real need
124 make_filename(filename
, INDEX_DIR
, sessionname
);
125 mkdir(filename
, 0700);
126 make_filename(filename
, INDEX_SESSIONDIR
, sessionname
);
127 mkdir(filename
, 0700);
129 make_filename(filename
, INDEX_SESSION
, sessionname
);
130 fp
= fopen(filename
, "w");
132 *errmsg
= dupprintf("Unable to create %s: %s",
133 filename
, strerror(errno
));
134 return NULL
; /* can't open */
139 void write_setting_s(void *handle
, const char *key
, const char *value
)
141 FILE *fp
= (FILE *)handle
;
142 fprintf(fp
, "%s=%s\n", key
, value
);
145 void write_setting_i(void *handle
, const char *key
, int value
)
147 FILE *fp
= (FILE *)handle
;
148 fprintf(fp
, "%s=%d\n", key
, value
);
151 void close_settings_w(void *handle
)
153 FILE *fp
= (FILE *)handle
;
158 * Reading settings, for the moment, is done by retrieving X
159 * resources from the X display. When we introduce disk files, I
160 * think what will happen is that the X resources will override
161 * PuTTY's inbuilt defaults, but that the disk files will then
162 * override those. This isn't optimal, but it's the best I can
163 * immediately work out.
164 * FIXME: the above comment is a bit out of date. Did it happen?
172 static tree234
*xrmtree
= NULL
;
174 int keycmp(void *av
, void *bv
)
176 struct keyval
*a
= (struct keyval
*)av
;
177 struct keyval
*b
= (struct keyval
*)bv
;
178 return strcmp(a
->key
, b
->key
);
181 void provide_xrm_string(char *string
)
184 struct keyval
*xrms
, *ret
;
186 p
= q
= strchr(string
, ':');
188 fprintf(stderr
, "pterm: expected a colon in resource string"
189 " \"%s\"\n", string
);
193 while (p
> string
&& p
[-1] != '.' && p
[-1] != '*')
195 xrms
= snew(struct keyval
);
196 key
= snewn(q
-p
, char);
200 while (*q
&& isspace((unsigned char)*q
))
202 xrms
->value
= dupstr(q
);
205 xrmtree
= newtree234(keycmp
);
207 ret
= add234(xrmtree
, xrms
);
209 /* Override an existing string. */
210 del234(xrmtree
, ret
);
211 add234(xrmtree
, xrms
);
215 const char *get_setting(const char *key
)
217 struct keyval tmp
, *ret
;
220 ret
= find234(xrmtree
, &tmp
, NULL
);
224 return x_get_default(key
);
227 void *open_settings_r(const char *sessionname
)
229 char filename
[FILENAME_MAX
];
234 make_filename(filename
, INDEX_SESSION
, sessionname
);
235 fp
= fopen(filename
, "r");
237 return NULL
; /* can't open */
239 ret
= newtree234(keycmp
);
241 while ( (line
= fgetline(fp
)) ) {
242 char *value
= strchr(line
, '=');
248 value
[strcspn(value
, "\r\n")] = '\0'; /* trim trailing NL */
250 kv
= snew(struct keyval
);
251 kv
->key
= dupstr(line
);
252 kv
->value
= dupstr(value
);
263 char *read_setting_s(void *handle
, const char *key
, char *buffer
, int buflen
)
265 tree234
*tree
= (tree234
*)handle
;
267 struct keyval tmp
, *kv
;
271 (kv
= find234(tree
, &tmp
, NULL
)) != NULL
) {
275 val
= get_setting(key
);
280 strncpy(buffer
, val
, buflen
);
281 buffer
[buflen
-1] = '\0';
286 int read_setting_i(void *handle
, const char *key
, int defvalue
)
288 tree234
*tree
= (tree234
*)handle
;
290 struct keyval tmp
, *kv
;
294 (kv
= find234(tree
, &tmp
, NULL
)) != NULL
) {
298 val
= get_setting(key
);
306 int read_setting_fontspec(void *handle
, const char *name
, FontSpec
*result
)
308 return !!read_setting_s(handle
, name
, result
->name
, sizeof(result
->name
));
310 int read_setting_filename(void *handle
, const char *name
, Filename
*result
)
312 return !!read_setting_s(handle
, name
, result
->path
, sizeof(result
->path
));
315 void write_setting_fontspec(void *handle
, const char *name
, FontSpec result
)
317 write_setting_s(handle
, name
, result
.name
);
319 void write_setting_filename(void *handle
, const char *name
, Filename result
)
321 write_setting_s(handle
, name
, result
.path
);
324 void close_settings_r(void *handle
)
326 tree234
*tree
= (tree234
*)handle
;
332 while ( (kv
= index234(tree
, 0)) != NULL
) {
334 sfree((char *)kv
->key
);
335 sfree((char *)kv
->value
);
342 void del_settings(const char *sessionname
)
344 char filename
[FILENAME_MAX
];
345 make_filename(filename
, INDEX_SESSION
, sessionname
);
349 void *enum_settings_start(void)
352 char filename
[FILENAME_MAX
];
354 make_filename(filename
, INDEX_SESSIONDIR
, NULL
);
355 dp
= opendir(filename
);
360 char *enum_settings_next(void *handle
, char *buffer
, int buflen
)
362 DIR *dp
= (DIR *)handle
;
365 char fullpath
[FILENAME_MAX
];
369 make_filename(fullpath
, INDEX_SESSIONDIR
, NULL
);
370 len
= strlen(fullpath
);
372 while ( (de
= readdir(dp
)) != NULL
) {
373 if (len
< FILENAME_MAX
) {
375 strncpy(fullpath
+len
+1, de
->d_name
, FILENAME_MAX
-(len
+1));
376 fullpath
[FILENAME_MAX
-1] = '\0';
379 if (stat(fullpath
, &st
) < 0 || !S_ISREG(st
.st_mode
))
380 continue; /* try another one */
382 unmunged
= unmungestr(de
->d_name
);
383 strncpy(buffer
, unmunged
, buflen
);
384 buffer
[buflen
-1] = '\0';
392 void enum_settings_finish(void *handle
)
394 DIR *dp
= (DIR *)handle
;
399 * Lines in the host keys file are of the form
401 * type@port:hostname keydata
405 * rsa@22:foovax.example.org 0x23,0x293487364395345345....2343
407 int verify_host_key(const char *hostname
, int port
,
408 const char *keytype
, const char *key
)
411 char filename
[FILENAME_MAX
];
415 make_filename(filename
, INDEX_HOSTKEYS
, NULL
);
416 fp
= fopen(filename
, "r");
418 return 1; /* key does not exist */
421 while ( (line
= fgetline(fp
)) ) {
426 line
[strcspn(line
, "\n")] = '\0'; /* strip trailing newline */
429 if (strncmp(p
, keytype
, i
))
437 sprintf(porttext
, "%d", port
);
438 i
= strlen(porttext
);
439 if (strncmp(p
, porttext
, i
))
447 i
= strlen(hostname
);
448 if (strncmp(p
, hostname
, i
))
457 * Found the key. Now just work out whether it's the right
461 ret
= 0; /* key matched OK */
463 ret
= 2; /* key mismatch */
475 void store_host_key(const char *hostname
, int port
,
476 const char *keytype
, const char *key
)
479 char *newtext
, *line
;
481 char filename
[FILENAME_MAX
], tmpfilename
[FILENAME_MAX
];
483 newtext
= dupprintf("%s@%d:%s %s\n", keytype
, port
, hostname
, key
);
484 headerlen
= 1 + strcspn(newtext
, " "); /* count the space too */
487 * Open both the old file and a new file.
489 make_filename(tmpfilename
, INDEX_HOSTKEYS_TMP
, NULL
);
490 wfp
= fopen(tmpfilename
, "w");
492 char dir
[FILENAME_MAX
];
494 make_filename(dir
, INDEX_DIR
, NULL
);
496 wfp
= fopen(tmpfilename
, "w");
500 make_filename(filename
, INDEX_HOSTKEYS
, NULL
);
501 rfp
= fopen(filename
, "r");
504 * Copy all lines from the old file to the new one that _don't_
505 * involve the same host key identifier as the one we're adding.
508 while ( (line
= fgetline(rfp
)) ) {
509 if (strncmp(line
, newtext
, headerlen
))
516 * Now add the new line at the end.
522 rename(tmpfilename
, filename
);
527 void read_random_seed(noise_consumer_t consumer
)
530 char fname
[FILENAME_MAX
];
532 make_filename(fname
, INDEX_RANDSEED
, NULL
);
533 fd
= open(fname
, O_RDONLY
);
537 while ( (ret
= read(fd
, buf
, sizeof(buf
))) > 0)
543 void write_random_seed(void *data
, int len
)
546 char fname
[FILENAME_MAX
];
548 make_filename(fname
, INDEX_RANDSEED
, NULL
);
550 * Don't truncate the random seed file if it already exists; if
551 * something goes wrong half way through writing it, it would
552 * be better to leave the old data there than to leave it empty.
554 fd
= open(fname
, O_CREAT
| O_WRONLY
, 0600);
556 char dir
[FILENAME_MAX
];
558 make_filename(dir
, INDEX_DIR
, NULL
);
560 fd
= open(fname
, O_CREAT
| O_WRONLY
, 0600);
564 int ret
= write(fd
, data
, len
);
567 data
= (char *)data
+ len
;
573 void cleanup_all(void)