spki_commands.c 7.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* spki_commands.c
 *
 * SPKI interface
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1999 Niels Mller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "spki_commands.h"

#include "sexp_commands.h"
#include "werror.h"
#include "xalloc.h"

/* Forward declarations */
struct command_simple spki_add_acl_command;
#define SPKI_ADD_ACL (&spki_add_acl_command.super.super)

struct command_simple spki_return_hostkeys;
#define RETURN_HOSTKEYS (&spki_return_hostkeys.super.super)

struct command_simple spki_add_hostkey_command;
#define SPKI_ADD_HOSTKEY (&spki_add_hostkey_command.super.super)

struct command_simple spki_add_userkey_command;
#define SPKI_ADD_USERKEY (&spki_add_userkey_command.super.super)

#include "spki_commands.c.x"


/* GABA:
   (class
     (name spki_command)
     (super command)
     (vars
       (ctx object spki_context)))
*/

/* Reading of ACL:s
 * ****************/
 
/* Adds an ACL s-expression to an SPIK-context. Returns the context. */
/* ;; GABA:
   (class
     (name spki_add_acl_command_1)
     (super command)
     (vars
       (ctx object spki_context)))
*/

static void
do_spki_add_acl(struct command *s,
		struct lsh_object *a,
		struct command_continuation *c,
		struct exception_handler *e UNUSED)
{
  CAST(spki_command, self, s);
  CAST_SUBTYPE(sexp, expr, a);

77
  trace("do_spki_add_acl\n");
78
79
80
81
82
83
84
85
86
87
88
89
90
  spki_add_acl(self->ctx, expr);

  COMMAND_RETURN(c, self->ctx);
}

COMMAND_SIMPLE(spki_add_acl_command)
{
  CAST_SUBTYPE(spki_context, ctx, a);

  NEW(spki_command, self);
  self->super.call = do_spki_add_acl;
  self->ctx = ctx;

91
92
  trace("spki_add_acl_command\n");

93
94
95
96
97
98
  return &self->super.super;
}

COMMAND_SIMPLE(spki_make_context_command)
{
  CAST_SUBTYPE(alist, algorithms, a);
99
100
  trace("spki_make_context_command\n");
  
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  return &make_spki_context(algorithms)->super;
}


/* Reads a file of ACL:s, and returns an spki_context. */

/* GABA:
   (expr
     (name spki_read_acl)
     (params
       (algorithms object alist))
     (expr
       (lambda (file)
         (let ((ctx (spki_make_context
	              ;; Delay call, so that we really
		      ;; create one context for each file.
	              (prog1 algorithms file))))
	   (for_sexp
	     (lambda (e) ctx) ; Return ctx
  	     ;; Keep on reading until an SEXP_EOF or
	     ;; SEXP_SYNTAX exception is raised. 
122
123
	     (spki_add_acl ctx)
	     file)))))
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
*/

struct command *
make_spki_read_acls(struct alist *algorithms)
{
  CAST_SUBTYPE(command, res, spki_read_acl(algorithms));

  trace("make_spki_read_acl()\n");
  return res;
}

COMMAND_SIMPLE(spki_read_acls_command)
{
  CAST_SUBTYPE(alist, algorithms, a);
  CAST_SUBTYPE(command, res, spki_read_acl(algorithms));

  return &res->super;
}
  
/* Reading of host-keys
 * ********************/

/* GABA:
   (class
     (name spki_read_hostkey_context)
     (super command)
     (vars
       (keys object alist)))
*/

static void
do_spki_add_hostkey(struct command *s,
		    struct lsh_object *a,
		    struct command_continuation *c,
		    struct exception_handler *e UNUSED)
{
  CAST(spki_read_hostkey_context, self, s);
  CAST(keypair, key, a);

163
164
  trace("do_spki_add_hostkey\n");
  
165
166
167
168
169
170
171
172
173
174
175
176
  if (ALIST_GET(self->keys, key->type))
    werror("Multiple host keys of type %a.\n", key->type);
  else
    ALIST_SET(self->keys, key->type, key);

  COMMAND_RETURN(c, self->keys);
}

/* Ignores its argument */
COMMAND_SIMPLE(spki_add_hostkey_command)
{
  NEW(spki_read_hostkey_context, self);
177
178
179

  trace("spki_add_hostkey_command\n");

180
181
182
183
184
185
186
187
188
189
190
  (void) a;
  
  self->super.call = do_spki_add_hostkey;
  self->keys = make_alist(0, -1);

  return &self->super.super;
}     

COMMAND_SIMPLE(spki_return_hostkeys)
{
  CAST(spki_read_hostkey_context, self, a);
191
  trace("spki_return_hostkeys\n");
192
193
194
195
196
197
198
199
200
201
202
203
204
205
  return &self->keys->super;
}

/* GABA:
   (expr
     (name spki_read_hostkeys)
     (params
       (algorithms object alist))
     (expr
       (lambda (file)
         (let ((add (spki_add_hostkey file)))
           (for_sexp (lambda (e) (return_hostkeys add))
	             (lambda (key)
		       (add (spki_parse_private_key
206
207
		               algorithms key)))
		     file)))))
208
209
210
211
212
213
214
*/

COMMAND_SIMPLE(spki_read_hostkeys_command)
{
  CAST_SUBTYPE(alist, algorithms, a);
  CAST_SUBTYPE(command, res, spki_read_hostkeys(algorithms));

215
216
  trace("spki_read_hostkeys_command\n");
  
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
  return &res->super;
}

/* Reading of private user-keys
 * ****************************/

/* GABA:
   (class
     (name spki_read_userkey_context)
     (super command)
     (vars
       (keys struct object_queue)))
*/

static void
do_spki_add_userkey(struct command *s,
		    struct lsh_object *a,
		    struct command_continuation *c,
		    struct exception_handler *e UNUSED)
{
  CAST(spki_read_userkey_context, self, s);
  CAST(keypair, key, a);

240
241
  trace("do_spki_add_userkey\n");
  
242
243
244
245
246
247
248
249
250
251
  object_queue_add_tail(&self->keys, &key->super);

  COMMAND_RETURN(c, s);
}

/* Ignores its argument */
COMMAND_SIMPLE(spki_add_userkey_command)
{
  NEW(spki_read_userkey_context, self);
  (void) a;
252
253

  trace("spki_add_userkey_command\n");
254
255
256
257
258
259
260
261
262
  self->super.call = do_spki_add_userkey;
  object_queue_init(&self->keys);

  return &self->super.super;
}     

COMMAND_SIMPLE(spki_return_userkeys)
{
  CAST(spki_read_userkey_context, self, a);
263
264
  trace("spki_return_userkeys\n");
  
265
266
267
268
269
270
271
272
273
274
275
276
277
278
  return &queue_to_list(&self->keys)->super.super;
}

/* GABA:
   (expr
     (name spki_read_userkeys)
     (params
       (algorithms object alist))
     (expr
       (lambda (file)
         (let ((ctx (spki_add_userkey file)))
           (for_sexp (lambda (e) (return_hostkeys ctx))
	             (lambda (key)
		       (ctx (spki_parse_private_key
279
280
		               algorithms key)))
		     file)))))
281
282
283
284
285
286
287
*/

COMMAND_SIMPLE(spki_read_userkeys_command)
{
  CAST_SUBTYPE(alist, algorithms, a);
  CAST_SUBTYPE(command, res, spki_read_userkeys(algorithms));

288
289
  trace("spki_read_userkeys_command\n");
  
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
  return &res->super;
}

#if 0
/* This class keeps track of the keypairs we have read, and it also
 * works as the exception handler for catching sexp-exceptions. */

/* ;;GABA:
   (class
     (name user_key_context)
     (super command)
     (vars
       (algorithms object alist)
       (keys struct object_queue)))
*/

static void do_user_key_context(struct command *s,
				struct lsh_object *a,
				struct command_continuation *c,
				struct exception_handler *e)
{
  CAST(user_key_context, self, s);
  CAST_SUBTYPE(exception, exc, a);

  switch(exc->type)
    {
    case SEXP_EOF:
      COMMAND_RETURN(c, queue_to_list(&self->keys));
      break;
    default:
      EXCEPTION_RAISE(e, exc);
      break;
    }
}

static void user_key_context_add(struct user_key_context *ctx, struct keypair *key)
{
  object_queue_add_tail(&ctx->keys, &key->super);
  
COMMAND_SIMPLE(make_user_key_context)
{
  CAST_SUBTYPE(alist, algorithms, a);

  NEW(user_key_context, self);
  self->super.call = do_user_key_context;
  self->algorithms = algorithms;
  object_queue_init(&self->keys);

  return &self->super.super;
}
 
/* ;; GABA:
   (expr
     (name spki_read_private_keys)
     (params
       (algorithms object alist)
       (add_key))
     (expr
       (lambda (file)
         (catch_sexp ctx (add_key ctx (spki_parse_private_key (read_sexp file)))))))
*/

 
/* ;; GABA:
   (class
     (name add_user_key)
     (command 
*/
#endif