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");
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_HOSTKEYS_TMP ?
"/.putty/sshhostkeys.tmp" :
102 index
== INDEX_RANDSEED ?
"/.putty/randomseed" :
103 "/.putty/ERROR", FILENAME_MAX
- len
);
105 filename
[FILENAME_MAX
-1] = '\0';
109 * Read an entire line of text from a file. Return a buffer
110 * malloced to be as big as necessary (caller must free).
112 static char *fgetline(FILE *fp
)
114 char *ret
= snewn(512, char);
115 int size
= 512, len
= 0;
116 while (fgets(ret
+ len
, size
- len
, fp
)) {
117 len
+= strlen(ret
+ len
);
118 if (ret
[len
-1] == '\n')
119 break; /* got a newline, we're done */
121 ret
= sresize(ret
, size
, char);
123 if (len
== 0) { /* first fgets returned NULL */
131 void *open_settings_w(const char *sessionname
, char **errmsg
)
133 char filename
[FILENAME_MAX
];
139 * Start by making sure the .putty directory and its sessions
140 * subdir actually exist. Ignore error returns from mkdir since
141 * they're perfectly likely to be `already exists', and any
142 * other error will trip us up later on so there's no real need
145 make_filename(filename
, INDEX_DIR
, sessionname
);
146 mkdir(filename
, 0700);
147 make_filename(filename
, INDEX_SESSIONDIR
, sessionname
);
148 mkdir(filename
, 0700);
150 make_filename(filename
, INDEX_SESSION
, sessionname
);
151 fp
= fopen(filename
, "w");
153 *errmsg
= dupprintf("Unable to create %s: %s",
154 filename
, strerror(errno
));
155 return NULL
; /* can't open */
160 void write_setting_s(void *handle
, const char *key
, const char *value
)
162 FILE *fp
= (FILE *)handle
;
163 fprintf(fp
, "%s=%s\n", key
, value
);
166 void write_setting_i(void *handle
, const char *key
, int value
)
168 FILE *fp
= (FILE *)handle
;
169 fprintf(fp
, "%s=%d\n", key
, value
);
172 void close_settings_w(void *handle
)
174 FILE *fp
= (FILE *)handle
;
179 * Reading settings, for the moment, is done by retrieving X
180 * resources from the X display. When we introduce disk files, I
181 * think what will happen is that the X resources will override
182 * PuTTY's inbuilt defaults, but that the disk files will then
183 * override those. This isn't optimal, but it's the best I can
184 * immediately work out.
185 * FIXME: the above comment is a bit out of date. Did it happen?
193 static tree234
*xrmtree
= NULL
;
195 int keycmp(void *av
, void *bv
)
197 struct keyval
*a
= (struct keyval
*)av
;
198 struct keyval
*b
= (struct keyval
*)bv
;
199 return strcmp(a
->key
, b
->key
);
202 void provide_xrm_string(char *string
)
205 struct keyval
*xrms
, *ret
;
207 p
= q
= strchr(string
, ':');
209 fprintf(stderr
, "pterm: expected a colon in resource string"
210 " \"%s\"\n", string
);
214 while (p
> string
&& p
[-1] != '.' && p
[-1] != '*')
216 xrms
= snew(struct keyval
);
217 key
= snewn(q
-p
, char);
221 while (*q
&& isspace((unsigned char)*q
))
223 xrms
->value
= dupstr(q
);
226 xrmtree
= newtree234(keycmp
);
228 ret
= add234(xrmtree
, xrms
);
230 /* Override an existing string. */
231 del234(xrmtree
, ret
);
232 add234(xrmtree
, xrms
);
236 const char *get_setting(const char *key
)
238 struct keyval tmp
, *ret
;
241 ret
= find234(xrmtree
, &tmp
, NULL
);
245 return x_get_default(key
);
248 void *open_settings_r(const char *sessionname
)
250 char filename
[FILENAME_MAX
];
255 make_filename(filename
, INDEX_SESSION
, sessionname
);
256 fp
= fopen(filename
, "r");
258 return NULL
; /* can't open */
260 ret
= newtree234(keycmp
);
262 while ( (line
= fgetline(fp
)) ) {
263 char *value
= strchr(line
, '=');
269 value
[strcspn(value
, "\r\n")] = '\0'; /* trim trailing NL */
271 kv
= snew(struct keyval
);
272 kv
->key
= dupstr(line
);
273 kv
->value
= dupstr(value
);
284 char *read_setting_s(void *handle
, const char *key
, char *buffer
, int buflen
)
286 tree234
*tree
= (tree234
*)handle
;
288 struct keyval tmp
, *kv
;
292 (kv
= find234(tree
, &tmp
, NULL
)) != NULL
) {
296 val
= get_setting(key
);
301 strncpy(buffer
, val
, buflen
);
302 buffer
[buflen
-1] = '\0';
307 int read_setting_i(void *handle
, const char *key
, int defvalue
)
309 tree234
*tree
= (tree234
*)handle
;
311 struct keyval tmp
, *kv
;
315 (kv
= find234(tree
, &tmp
, NULL
)) != NULL
) {
319 val
= get_setting(key
);
327 int read_setting_fontspec(void *handle
, const char *name
, FontSpec
*result
)
329 return !!read_setting_s(handle
, name
, result
->name
, sizeof(result
->name
));
331 int read_setting_filename(void *handle
, const char *name
, Filename
*result
)
333 return !!read_setting_s(handle
, name
, result
->path
, sizeof(result
->path
));
336 void write_setting_fontspec(void *handle
, const char *name
, FontSpec result
)
338 write_setting_s(handle
, name
, result
.name
);
340 void write_setting_filename(void *handle
, const char *name
, Filename result
)
342 write_setting_s(handle
, name
, result
.path
);
345 void close_settings_r(void *handle
)
347 tree234
*tree
= (tree234
*)handle
;
353 while ( (kv
= index234(tree
, 0)) != NULL
) {
355 sfree((char *)kv
->key
);
356 sfree((char *)kv
->value
);
363 void del_settings(const char *sessionname
)
365 char filename
[FILENAME_MAX
];
366 make_filename(filename
, INDEX_SESSION
, sessionname
);
370 void *enum_settings_start(void)
373 char filename
[FILENAME_MAX
];
375 make_filename(filename
, INDEX_SESSIONDIR
, NULL
);
376 dp
= opendir(filename
);
381 char *enum_settings_next(void *handle
, char *buffer
, int buflen
)
383 DIR *dp
= (DIR *)handle
;
386 char fullpath
[FILENAME_MAX
];
390 make_filename(fullpath
, INDEX_SESSIONDIR
, NULL
);
391 len
= strlen(fullpath
);
393 while ( (de
= readdir(dp
)) != NULL
) {
394 if (len
< FILENAME_MAX
) {
396 strncpy(fullpath
+len
+1, de
->d_name
, FILENAME_MAX
-(len
+1));
397 fullpath
[FILENAME_MAX
-1] = '\0';
400 if (stat(fullpath
, &st
) < 0 || !S_ISREG(st
.st_mode
))
401 continue; /* try another one */
403 unmunged
= unmungestr(de
->d_name
);
404 strncpy(buffer
, unmunged
, buflen
);
405 buffer
[buflen
-1] = '\0';
413 void enum_settings_finish(void *handle
)
415 DIR *dp
= (DIR *)handle
;
420 * Lines in the host keys file are of the form
422 * type@port:hostname keydata
426 * rsa@22:foovax.example.org 0x23,0x293487364395345345....2343
428 int verify_host_key(const char *hostname
, int port
,
429 const char *keytype
, const char *key
)
432 char filename
[FILENAME_MAX
];
436 make_filename(filename
, INDEX_HOSTKEYS
, NULL
);
437 fp
= fopen(filename
, "r");
439 return 1; /* key does not exist */
442 while ( (line
= fgetline(fp
)) ) {
447 line
[strcspn(line
, "\n")] = '\0'; /* strip trailing newline */
450 if (strncmp(p
, keytype
, i
))
458 sprintf(porttext
, "%d", port
);
459 i
= strlen(porttext
);
460 if (strncmp(p
, porttext
, i
))
468 i
= strlen(hostname
);
469 if (strncmp(p
, hostname
, i
))
478 * Found the key. Now just work out whether it's the right
482 ret
= 0; /* key matched OK */
484 ret
= 2; /* key mismatch */
496 void store_host_key(const char *hostname
, int port
,
497 const char *keytype
, const char *key
)
500 char *newtext
, *line
;
502 char filename
[FILENAME_MAX
], tmpfilename
[FILENAME_MAX
];
504 newtext
= dupprintf("%s@%d:%s %s\n", keytype
, port
, hostname
, key
);
505 headerlen
= 1 + strcspn(newtext
, " "); /* count the space too */
508 * Open both the old file and a new file.
510 make_filename(tmpfilename
, INDEX_HOSTKEYS_TMP
, NULL
);
511 wfp
= fopen(tmpfilename
, "w");
513 char dir
[FILENAME_MAX
];
515 make_filename(dir
, INDEX_DIR
, NULL
);
517 wfp
= fopen(tmpfilename
, "w");
521 make_filename(filename
, INDEX_HOSTKEYS
, NULL
);
522 rfp
= fopen(filename
, "r");
525 * Copy all lines from the old file to the new one that _don't_
526 * involve the same host key identifier as the one we're adding.
529 while ( (line
= fgetline(rfp
)) ) {
530 if (strncmp(line
, newtext
, headerlen
))
537 * Now add the new line at the end.
543 rename(tmpfilename
, filename
);
548 void read_random_seed(noise_consumer_t consumer
)
551 char fname
[FILENAME_MAX
];
553 make_filename(fname
, INDEX_RANDSEED
, NULL
);
554 fd
= open(fname
, O_RDONLY
);
558 while ( (ret
= read(fd
, buf
, sizeof(buf
))) > 0)
564 void write_random_seed(void *data
, int len
)
567 char fname
[FILENAME_MAX
];
569 make_filename(fname
, INDEX_RANDSEED
, NULL
);
571 * Don't truncate the random seed file if it already exists; if
572 * something goes wrong half way through writing it, it would
573 * be better to leave the old data there than to leave it empty.
575 fd
= open(fname
, O_CREAT
| O_WRONLY
, 0600);
577 char dir
[FILENAME_MAX
];
579 make_filename(dir
, INDEX_DIR
, NULL
);
581 fd
= open(fname
, O_CREAT
| O_WRONLY
, 0600);
585 int ret
= write(fd
, data
, len
);
588 data
= (char *)data
+ len
;
594 void cleanup_all(void)