RJK's general signal-handling robustness patch. Should fix the weird
[u/mdw/putty] / unix / uxstore.c
CommitLineData
1709795f 1/*
2 * uxstore.c: Unix-specific implementation of the interface defined
3 * in storage.h.
4 */
5
6#include <stdio.h>
7#include <stdlib.h>
0ac15bdc 8#include <ctype.h>
c5e438ec 9#include <unistd.h>
10#include <fcntl.h>
11#include <sys/stat.h>
12#include <sys/types.h>
1709795f 13#include "putty.h"
14#include "storage.h"
0ac15bdc 15#include "tree234.h"
1709795f 16
faec60ed 17/*
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.
22 */
1709795f 23
24void *open_settings_w(char *sessionname)
25{
26 return NULL;
27}
28
29void write_setting_s(void *handle, char *key, char *value)
30{
31}
32
33void write_setting_i(void *handle, char *key, int value)
34{
35}
36
37void close_settings_w(void *handle)
38{
39}
40
faec60ed 41/*
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.
48 */
49
0ac15bdc 50struct xrm_string {
51 char *key;
52 char *value;
53};
54
55static tree234 *xrmtree = NULL;
56
57int xrmcmp(void *av, void *bv)
58{
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);
62}
63
64void provide_xrm_string(char *string)
65{
66 char *p, *q;
67 struct xrm_string *xrms, *ret;
68
69 p = q = strchr(string, ':');
70 if (!q) {
71 fprintf(stderr, "pterm: expected a colon in resource string"
72 " \"%s\"\n", string);
73 return;
74 }
75 q++;
76 while (p > string && p[-1] != '.' && p[-1] != '*')
77 p--;
78 xrms = smalloc(sizeof(struct xrm_string));
79 xrms->key = smalloc(q-p);
80 memcpy(xrms->key, p, q-p);
81 xrms->key[q-p-1] = '\0';
82 while (*q && isspace(*q))
83 q++;
84 xrms->value = dupstr(q);
85
86 if (!xrmtree)
87 xrmtree = newtree234(xrmcmp);
88
89 ret = add234(xrmtree, xrms);
90 if (ret) {
91 /* Override an existing string. */
92 del234(xrmtree, ret);
93 add234(xrmtree, xrms);
94 }
95}
96
97char *get_setting(char *key)
98{
99 struct xrm_string tmp, *ret;
100 tmp.key = key;
101 if (xrmtree) {
102 ret = find234(xrmtree, &tmp, NULL);
103 if (ret)
104 return ret->value;
105 }
c5e438ec 106 return x_get_default(key);
0ac15bdc 107}
108
1709795f 109void *open_settings_r(char *sessionname)
110{
faec60ed 111 static int thing_to_return_an_arbitrary_non_null_pointer_to;
c5e438ec 112 return &thing_to_return_an_arbitrary_non_null_pointer_to;
1709795f 113}
114
115char *read_setting_s(void *handle, char *key, char *buffer, int buflen)
116{
0ac15bdc 117 char *val = get_setting(key);
faec60ed 118 if (!val)
119 return NULL;
120 else {
121 strncpy(buffer, val, buflen);
122 buffer[buflen-1] = '\0';
123 return buffer;
124 }
1709795f 125}
126
127int read_setting_i(void *handle, char *key, int defvalue)
128{
0ac15bdc 129 char *val = get_setting(key);
faec60ed 130 if (!val)
131 return defvalue;
132 else
133 return atoi(val);
1709795f 134}
135
136void close_settings_r(void *handle)
137{
138}
139
140void del_settings(char *sessionname)
141{
142}
143
144void *enum_settings_start(void)
145{
146 return NULL;
147}
148
149char *enum_settings_next(void *handle, char *buffer, int buflen)
150{
151 return NULL;
152}
153
154void enum_settings_finish(void *handle)
155{
156}
157
c5e438ec 158enum {
159 INDEX_DIR, INDEX_HOSTKEYS
160};
161
162static void make_filename(char *filename, int index)
163{
164 char *home;
165 int len;
166 home = getenv("HOME");
167 strncpy(filename, home, FILENAME_MAX);
168 len = strlen(filename);
169 strncpy(filename + len,
170 index == INDEX_DIR ? "/.putty" :
171 index == INDEX_HOSTKEYS ? "/.putty/sshhostkeys" :
172 "/.putty/ERROR", FILENAME_MAX - len);
173 filename[FILENAME_MAX-1] = '\0';
174}
175
176/*
177 * Read an entire line of text from a file. Return a buffer
178 * malloced to be as big as necessary (caller must free).
179 */
180static char *fgetline(FILE *fp)
181{
182 char *ret = smalloc(512);
183 int size = 512, len = 0;
184 while (fgets(ret + len, size - len, fp)) {
185 len += strlen(ret + len);
186 if (ret[len-1] == '\n')
187 break; /* got a newline, we're done */
188 size = len + 512;
189 ret = srealloc(ret, size);
190 }
191 if (len == 0) { /* first fgets returned NULL */
192 sfree(ret);
193 return NULL;
194 }
195 ret[len] = '\0';
196 return ret;
197}
198
199/*
200 * Lines in the host keys file are of the form
201 *
202 * type@port:hostname keydata
203 *
204 * e.g.
205 *
206 * rsa@22:foovax.example.org 0x23,0x293487364395345345....2343
207 */
1709795f 208int verify_host_key(char *hostname, int port, char *keytype, char *key)
209{
c5e438ec 210 FILE *fp;
211 char filename[FILENAME_MAX];
212 char *line;
213 int ret;
214
215 make_filename(filename, INDEX_HOSTKEYS);
216 fp = fopen(filename, "r");
217 if (!fp)
218 return 1; /* key does not exist */
219
220 ret = 1;
221 while ( (line = fgetline(fp)) ) {
222 int i;
223 char *p = line;
224 char porttext[20];
225
226 line[strcspn(line, "\n")] = '\0'; /* strip trailing newline */
227
228 i = strlen(keytype);
229 if (strncmp(p, keytype, i))
230 goto done;
231 p += i;
232
233 if (*p != '@')
234 goto done;
235 p++;
236
237 sprintf(porttext, "%d", port);
238 i = strlen(porttext);
239 if (strncmp(p, porttext, i))
240 goto done;
241 p += i;
242
243 if (*p != ':')
244 goto done;
245 p++;
246
247 i = strlen(hostname);
248 if (strncmp(p, hostname, i))
249 goto done;
250 p += i;
251
252 if (*p != ' ')
253 goto done;
254 p++;
255
256 /*
257 * Found the key. Now just work out whether it's the right
258 * one or not.
259 */
260 if (!strcmp(p, key))
261 ret = 0; /* key matched OK */
262 else
263 ret = 2; /* key mismatch */
264
265 done:
266 sfree(line);
267 if (ret != 1)
268 break;
269 }
270
271 return ret;
1709795f 272}
273
274void store_host_key(char *hostname, int port, char *keytype, char *key)
275{
c5e438ec 276 FILE *fp;
277 int fd;
278 char filename[FILENAME_MAX];
279
280 make_filename(filename, INDEX_HOSTKEYS);
281 fd = open(filename, O_CREAT | O_APPEND | O_RDWR, 0600);
282 if (fd < 0) {
283 char dir[FILENAME_MAX];
284
285 make_filename(dir, INDEX_DIR);
286 mkdir(dir, 0700);
287 fd = open(filename, O_CREAT | O_APPEND | O_RDWR, 0600);
288 }
289 if (fd < 0) {
290 perror(filename);
291 exit(1);
292 }
293 fp = fdopen(fd, "a");
294 fprintf(fp, "%s@%d:%s %s\n", keytype, port, hostname, key);
295 fclose(fp);
1709795f 296}
297
298void read_random_seed(noise_consumer_t consumer)
299{
300}
301
302void write_random_seed(void *data, int len)
303{
304}
305
306void cleanup_all(void)
307{
308}