2 * uxstore.c: Unix-specific implementation of the interface defined
12 #include <sys/types.h>
18 * For the moment, the only existing Unix utility is pterm and that
19 * has no GUI configuration at all, so our write routines need do
20 * nothing. Eventually I suppose these will read and write an rc
21 * file somewhere or other.
24 void *open_settings_w(const char *sessionname
)
29 void write_setting_s(void *handle
, const char *key
, const char *value
)
33 void write_setting_i(void *handle
, const char *key
, int value
)
37 void close_settings_w(void *handle
)
42 * Reading settings, for the moment, is done by retrieving X
43 * resources from the X display. When we introduce disk files, I
44 * think what will happen is that the X resources will override
45 * PuTTY's inbuilt defaults, but that the disk files will then
46 * override those. This isn't optimal, but it's the best I can
47 * immediately work out.
55 static tree234
*xrmtree
= NULL
;
57 int xrmcmp(void *av
, void *bv
)
59 struct xrm_string
*a
= (struct xrm_string
*)av
;
60 struct xrm_string
*b
= (struct xrm_string
*)bv
;
61 return strcmp(a
->key
, b
->key
);
64 void provide_xrm_string(char *string
)
67 struct xrm_string
*xrms
, *ret
;
69 p
= q
= strchr(string
, ':');
71 fprintf(stderr
, "pterm: expected a colon in resource string"
76 while (p
> string
&& p
[-1] != '.' && p
[-1] != '*')
78 xrms
= snew(struct xrm_string
);
79 key
= snewn(q
-p
, char);
83 while (*q
&& isspace((unsigned char)*q
))
85 xrms
->value
= dupstr(q
);
88 xrmtree
= newtree234(xrmcmp
);
90 ret
= add234(xrmtree
, xrms
);
92 /* Override an existing string. */
94 add234(xrmtree
, xrms
);
98 const char *get_setting(const char *key
)
100 struct xrm_string tmp
, *ret
;
103 ret
= find234(xrmtree
, &tmp
, NULL
);
107 return x_get_default(key
);
110 void *open_settings_r(const char *sessionname
)
112 static int thing_to_return_an_arbitrary_non_null_pointer_to
;
113 return &thing_to_return_an_arbitrary_non_null_pointer_to
;
116 char *read_setting_s(void *handle
, const char *key
, char *buffer
, int buflen
)
118 const char *val
= get_setting(key
);
122 strncpy(buffer
, val
, buflen
);
123 buffer
[buflen
-1] = '\0';
128 int read_setting_i(void *handle
, const char *key
, int defvalue
)
130 const char *val
= get_setting(key
);
137 int read_setting_fontspec(void *handle
, const char *name
, FontSpec
*result
)
139 return !!read_setting_s(handle
, name
, result
->name
, sizeof(result
->name
));
141 int read_setting_filename(void *handle
, const char *name
, Filename
*result
)
143 return !!read_setting_s(handle
, name
, result
->path
, sizeof(result
->path
));
146 void write_setting_fontspec(void *handle
, const char *name
, FontSpec result
)
148 write_setting_s(handle
, name
, result
.name
);
150 void write_setting_filename(void *handle
, const char *name
, Filename result
)
152 write_setting_s(handle
, name
, result
.path
);
155 void close_settings_r(void *handle
)
159 void del_settings(const char *sessionname
)
163 void *enum_settings_start(void)
168 char *enum_settings_next(void *handle
, char *buffer
, int buflen
)
173 void enum_settings_finish(void *handle
)
178 INDEX_DIR
, INDEX_HOSTKEYS
, INDEX_RANDSEED
181 static void make_filename(char *filename
, int index
)
185 home
= getenv("HOME");
186 strncpy(filename
, home
, FILENAME_MAX
);
187 len
= strlen(filename
);
188 strncpy(filename
+ len
,
189 index
== INDEX_DIR ?
"/.putty" :
190 index
== INDEX_HOSTKEYS ?
"/.putty/sshhostkeys" :
191 index
== INDEX_RANDSEED ?
"/.putty/randomseed" :
192 "/.putty/ERROR", FILENAME_MAX
- len
);
193 filename
[FILENAME_MAX
-1] = '\0';
197 * Read an entire line of text from a file. Return a buffer
198 * malloced to be as big as necessary (caller must free).
200 static char *fgetline(FILE *fp
)
202 char *ret
= snewn(512, char);
203 int size
= 512, len
= 0;
204 while (fgets(ret
+ len
, size
- len
, fp
)) {
205 len
+= strlen(ret
+ len
);
206 if (ret
[len
-1] == '\n')
207 break; /* got a newline, we're done */
209 ret
= sresize(ret
, size
, char);
211 if (len
== 0) { /* first fgets returned NULL */
220 * Lines in the host keys file are of the form
222 * type@port:hostname keydata
226 * rsa@22:foovax.example.org 0x23,0x293487364395345345....2343
228 int verify_host_key(const char *hostname
, int port
,
229 const char *keytype
, const char *key
)
232 char filename
[FILENAME_MAX
];
236 make_filename(filename
, INDEX_HOSTKEYS
);
237 fp
= fopen(filename
, "r");
239 return 1; /* key does not exist */
242 while ( (line
= fgetline(fp
)) ) {
247 line
[strcspn(line
, "\n")] = '\0'; /* strip trailing newline */
250 if (strncmp(p
, keytype
, i
))
258 sprintf(porttext
, "%d", port
);
259 i
= strlen(porttext
);
260 if (strncmp(p
, porttext
, i
))
268 i
= strlen(hostname
);
269 if (strncmp(p
, hostname
, i
))
278 * Found the key. Now just work out whether it's the right
282 ret
= 0; /* key matched OK */
284 ret
= 2; /* key mismatch */
295 void store_host_key(const char *hostname
, int port
,
296 const char *keytype
, const char *key
)
300 char filename
[FILENAME_MAX
];
302 make_filename(filename
, INDEX_HOSTKEYS
);
303 fd
= open(filename
, O_CREAT
| O_APPEND
| O_RDWR
, 0600);
305 char dir
[FILENAME_MAX
];
307 make_filename(dir
, INDEX_DIR
);
309 fd
= open(filename
, O_CREAT
| O_APPEND
| O_RDWR
, 0600);
315 fp
= fdopen(fd
, "a");
316 fprintf(fp
, "%s@%d:%s %s\n", keytype
, port
, hostname
, key
);
320 void read_random_seed(noise_consumer_t consumer
)
323 char fname
[FILENAME_MAX
];
325 make_filename(fname
, INDEX_RANDSEED
);
326 fd
= open(fname
, O_RDONLY
);
330 while ( (ret
= read(fd
, buf
, sizeof(buf
))) > 0)
336 void write_random_seed(void *data
, int len
)
339 char fname
[FILENAME_MAX
];
341 make_filename(fname
, INDEX_RANDSEED
);
343 * Don't truncate the random seed file if it already exists; if
344 * something goes wrong half way through writing it, it would
345 * be better to leave the old data there than to leave it empty.
347 fd
= open(fname
, O_CREAT
| O_WRONLY
, 0600);
349 char dir
[FILENAME_MAX
];
351 make_filename(dir
, INDEX_DIR
);
353 fd
= open(fname
, O_CREAT
| O_WRONLY
, 0600);
357 int ret
= write(fd
, data
, len
);
360 data
= (char *)data
+ len
;
366 void cleanup_all(void)