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 */
130 void *open_settings_w(const char *sessionname
, char **errmsg
)
132 char filename
[FILENAME_MAX
];
138 * Start by making sure the .putty directory and its sessions
139 * subdir actually exist. Ignore error returns from mkdir since
140 * they're perfectly likely to be `already exists', and any
141 * other error will trip us up later on so there's no real need
144 make_filename(filename
, INDEX_DIR
, sessionname
);
145 mkdir(filename
, 0700);
146 make_filename(filename
, INDEX_SESSIONDIR
, sessionname
);
147 mkdir(filename
, 0700);
149 make_filename(filename
, INDEX_SESSION
, sessionname
);
150 fp
= fopen(filename
, "w");
152 *errmsg
= dupprintf("Unable to create %s: %s",
153 filename
, strerror(errno
));
154 return NULL
; /* can't open */
159 void write_setting_s(void *handle
, const char *key
, const char *value
)
161 FILE *fp
= (FILE *)handle
;
162 fprintf(fp
, "%s=%s\n", key
, value
);
165 void write_setting_i(void *handle
, const char *key
, int value
)
167 FILE *fp
= (FILE *)handle
;
168 fprintf(fp
, "%s=%d\n", key
, value
);
171 void close_settings_w(void *handle
)
173 FILE *fp
= (FILE *)handle
;
178 * Reading settings, for the moment, is done by retrieving X
179 * resources from the X display. When we introduce disk files, I
180 * think what will happen is that the X resources will override
181 * PuTTY's inbuilt defaults, but that the disk files will then
182 * override those. This isn't optimal, but it's the best I can
183 * immediately work out.
184 * FIXME: the above comment is a bit out of date. Did it happen?
192 static tree234
*xrmtree
= NULL
;
194 int keycmp(void *av
, void *bv
)
196 struct keyval
*a
= (struct keyval
*)av
;
197 struct keyval
*b
= (struct keyval
*)bv
;
198 return strcmp(a
->key
, b
->key
);
201 void provide_xrm_string(char *string
)
204 struct keyval
*xrms
, *ret
;
206 p
= q
= strchr(string
, ':');
208 fprintf(stderr
, "pterm: expected a colon in resource string"
209 " \"%s\"\n", string
);
213 while (p
> string
&& p
[-1] != '.' && p
[-1] != '*')
215 xrms
= snew(struct keyval
);
216 key
= snewn(q
-p
, char);
220 while (*q
&& isspace((unsigned char)*q
))
222 xrms
->value
= dupstr(q
);
225 xrmtree
= newtree234(keycmp
);
227 ret
= add234(xrmtree
, xrms
);
229 /* Override an existing string. */
230 del234(xrmtree
, ret
);
231 add234(xrmtree
, xrms
);
235 const char *get_setting(const char *key
)
237 struct keyval tmp
, *ret
;
240 ret
= find234(xrmtree
, &tmp
, NULL
);
244 return x_get_default(key
);
247 void *open_settings_r(const char *sessionname
)
249 char filename
[FILENAME_MAX
];
254 make_filename(filename
, INDEX_SESSION
, sessionname
);
255 fp
= fopen(filename
, "r");
257 return NULL
; /* can't open */
259 ret
= newtree234(keycmp
);
261 while ( (line
= fgetline(fp
)) ) {
262 char *value
= strchr(line
, '=');
268 value
[strcspn(value
, "\r\n")] = '\0'; /* trim trailing NL */
270 kv
= snew(struct keyval
);
271 kv
->key
= dupstr(line
);
272 kv
->value
= dupstr(value
);
283 char *read_setting_s(void *handle
, const char *key
, char *buffer
, int buflen
)
285 tree234
*tree
= (tree234
*)handle
;
287 struct keyval tmp
, *kv
;
291 (kv
= find234(tree
, &tmp
, NULL
)) != NULL
) {
295 val
= get_setting(key
);
300 strncpy(buffer
, val
, buflen
);
301 buffer
[buflen
-1] = '\0';
306 int read_setting_i(void *handle
, const char *key
, int defvalue
)
308 tree234
*tree
= (tree234
*)handle
;
310 struct keyval tmp
, *kv
;
314 (kv
= find234(tree
, &tmp
, NULL
)) != NULL
) {
318 val
= get_setting(key
);
326 int read_setting_fontspec(void *handle
, const char *name
, FontSpec
*result
)
328 return !!read_setting_s(handle
, name
, result
->name
, sizeof(result
->name
));
330 int read_setting_filename(void *handle
, const char *name
, Filename
*result
)
332 return !!read_setting_s(handle
, name
, result
->path
, sizeof(result
->path
));
335 void write_setting_fontspec(void *handle
, const char *name
, FontSpec result
)
337 write_setting_s(handle
, name
, result
.name
);
339 void write_setting_filename(void *handle
, const char *name
, Filename result
)
341 write_setting_s(handle
, name
, result
.path
);
344 void close_settings_r(void *handle
)
346 tree234
*tree
= (tree234
*)handle
;
352 while ( (kv
= index234(tree
, 0)) != NULL
) {
354 sfree((char *)kv
->key
);
355 sfree((char *)kv
->value
);
362 void del_settings(const char *sessionname
)
364 char filename
[FILENAME_MAX
];
365 make_filename(filename
, INDEX_SESSION
, sessionname
);
369 void *enum_settings_start(void)
372 char filename
[FILENAME_MAX
];
374 make_filename(filename
, INDEX_SESSIONDIR
, NULL
);
375 dp
= opendir(filename
);
380 char *enum_settings_next(void *handle
, char *buffer
, int buflen
)
382 DIR *dp
= (DIR *)handle
;
385 char fullpath
[FILENAME_MAX
];
389 make_filename(fullpath
, INDEX_SESSIONDIR
, NULL
);
390 len
= strlen(fullpath
);
392 while ( (de
= readdir(dp
)) != NULL
) {
393 if (len
< FILENAME_MAX
) {
395 strncpy(fullpath
+len
+1, de
->d_name
, FILENAME_MAX
-(len
+1));
396 fullpath
[FILENAME_MAX
-1] = '\0';
399 if (stat(fullpath
, &st
) < 0 || !S_ISREG(st
.st_mode
))
400 continue; /* try another one */
402 unmunged
= unmungestr(de
->d_name
);
403 strncpy(buffer
, unmunged
, buflen
);
404 buffer
[buflen
-1] = '\0';
412 void enum_settings_finish(void *handle
)
414 DIR *dp
= (DIR *)handle
;
419 * Lines in the host keys file are of the form
421 * type@port:hostname keydata
425 * rsa@22:foovax.example.org 0x23,0x293487364395345345....2343
427 int verify_host_key(const char *hostname
, int port
,
428 const char *keytype
, const char *key
)
431 char filename
[FILENAME_MAX
];
435 make_filename(filename
, INDEX_HOSTKEYS
, NULL
);
436 fp
= fopen(filename
, "r");
438 return 1; /* key does not exist */
441 while ( (line
= fgetline(fp
)) ) {
446 line
[strcspn(line
, "\n")] = '\0'; /* strip trailing newline */
449 if (strncmp(p
, keytype
, i
))
457 sprintf(porttext
, "%d", port
);
458 i
= strlen(porttext
);
459 if (strncmp(p
, porttext
, i
))
467 i
= strlen(hostname
);
468 if (strncmp(p
, hostname
, i
))
477 * Found the key. Now just work out whether it's the right
481 ret
= 0; /* key matched OK */
483 ret
= 2; /* key mismatch */
495 void store_host_key(const char *hostname
, int port
,
496 const char *keytype
, const char *key
)
500 char filename
[FILENAME_MAX
];
502 make_filename(filename
, INDEX_HOSTKEYS
, NULL
);
503 fd
= open(filename
, O_CREAT
| O_APPEND
| O_RDWR
, 0600);
505 char dir
[FILENAME_MAX
];
507 make_filename(dir
, INDEX_DIR
, NULL
);
509 fd
= open(filename
, O_CREAT
| O_APPEND
| O_RDWR
, 0600);
515 fp
= fdopen(fd
, "a");
516 fprintf(fp
, "%s@%d:%s %s\n", keytype
, port
, hostname
, key
);
520 void read_random_seed(noise_consumer_t consumer
)
523 char fname
[FILENAME_MAX
];
525 make_filename(fname
, INDEX_RANDSEED
, NULL
);
526 fd
= open(fname
, O_RDONLY
);
530 while ( (ret
= read(fd
, buf
, sizeof(buf
))) > 0)
536 void write_random_seed(void *data
, int len
)
539 char fname
[FILENAME_MAX
];
541 make_filename(fname
, INDEX_RANDSEED
, NULL
);
543 * Don't truncate the random seed file if it already exists; if
544 * something goes wrong half way through writing it, it would
545 * be better to leave the old data there than to leave it empty.
547 fd
= open(fname
, O_CREAT
| O_WRONLY
, 0600);
549 char dir
[FILENAME_MAX
];
551 make_filename(dir
, INDEX_DIR
, NULL
);
553 fd
= open(fname
, O_CREAT
| O_WRONLY
, 0600);
557 int ret
= write(fd
, data
, len
);
560 data
= (char *)data
+ len
;
566 void cleanup_all(void)