Imported Upstream version 3.1.0
[debian/amanda] / common-src / security.h
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1999 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: security.h,v 1.17 2006/05/26 14:00:58 martinea Exp $
28  *
29  * security api
30  */
31 #ifndef SECURITY_H
32 #define SECURITY_H
33
34 #include "packet.h"
35
36 struct security_handle;
37
38 /*
39  * This is a type that gets passed to the security_recvpkt() and
40  * security_connect() callbacks.
41  * It details what the status of this callback is.
42  */
43 typedef enum { S_OK, S_TIMEOUT, S_ERROR } security_status_t;
44
45 /*
46  * This structure defines a security driver.  This driver abstracts
47  * common security actions behind a set of function pointers.  Macros
48  * mask this.
49  */
50 typedef struct security_driver {
51     /*
52      * The name of this driver, eg, "BSD", "BSDTCP", "KRB5", etc...
53      */
54     const char *name;
55
56     /*
57      * Connects a security handle, for this driver to a remote
58      * host.
59      */
60     void (*connect)(const char *,
61         char *(*)(char *, void *),
62         void (*)(void *, struct security_handle *, security_status_t),
63         void *, void *);
64
65     /*
66      * This form sets up a callback that returns new handles as
67      * they are received.  It takes an input and output file descriptor.
68      */
69     void (*accept)(const struct security_driver *,
70                         char *(*)(char *, void *),
71                         int, int,
72                         void (*)(struct security_handle *, pkt_t *),
73                         void *);
74
75     /*
76      * Frees up handles allocated by the previous
77      */
78     void (*close)(void *);
79
80     /*
81      * This transmits a packet after adding the security information
82      * Returns 0 on success, negative on error.
83      */
84     ssize_t (*sendpkt)(void *, pkt_t *);
85
86     /*
87      * This creates an event in the event handler for receiving pkt_t's
88      * on a security_handle.  The given callback with the given arg
89      * will be called when the driver determines that it has data
90      * for that handle.  The last argument is a timeout, in seconds.
91      * This may be -1 to indicate no timeout.
92      *
93      * If there was an error or timeout, this will be indicated in
94      * the status argument.
95      * 
96      * Only one recvpkt request can exist per handle.
97      */
98     void (*recvpkt)(void *, void (*)(void *, pkt_t *,
99         security_status_t), void *, int);
100
101     /*
102      * Cancel an outstanding recvpkt request on a handle.
103      */
104     void (*recvpkt_cancel)(void *);
105
106     /*
107      * Get a stream given a security handle
108      */
109     void *(*stream_server)(void *);
110
111     /*
112      * Accept a stream created by stream_server
113      */
114     int (*stream_accept)(void *);
115
116     /*
117      * Get a stream and connect it to a remote given a security handle
118      * and a stream id.
119      */
120     void *(*stream_client)(void *, int);
121
122     /*
123      * Close a stream opened with stream_server or stream_client
124      */
125     void (*stream_close)(void *);
126
127     /*
128      * Authenticate a stream.
129      */
130     int (*stream_auth)(void *);
131
132     /*
133      * Return a numeric id for a stream.
134      */
135     int (*stream_id)(void *);
136
137     /*
138      * Write to a stream.
139      */
140     int (*stream_write)(void *, const void *, size_t);
141
142     /*
143      * Read asyncronously from a stream.  Only one request can exist
144      * per stream.
145      */
146     void (*stream_read)(void *, void (*)(void *, void *, ssize_t), void *);
147
148     /*
149      * Read syncronously from a stream.
150      */
151     ssize_t (*stream_read_sync)(void *, void **);
152
153     /*
154      * Cancel a stream read request
155      */
156     void (*stream_read_cancel)(void *);
157
158     void (*close_connection)(void *, char *);
159
160     int (*data_encrypt)(void *, void *, ssize_t, void **, ssize_t *);
161     int (*data_decrypt)(void *, void *, ssize_t, void **, ssize_t *);
162 } security_driver_t;
163
164 /*
165  * This structure is a handle to a connection to a host for transmission
166  * of protocol packets (pkt_t's).  The underlying security type defines
167  * the actual protocol and transport.
168  *
169  * This handle is reference counted so that it can be used inside of
170  * security streams after it has been closed by our callers.
171  */
172 typedef struct security_handle {
173     const security_driver_t *driver;
174     char *error;
175 } security_handle_t;
176
177 /*
178  * This structure is a handle to a stream connection to a host for
179  * transmission of random data such as dumps or index data.
180  */
181 typedef struct security_stream {
182     const security_driver_t *driver;
183     char *error;
184 } security_stream_t;
185
186
187 const security_driver_t *security_getdriver(const char *);
188 void security_handleinit(security_handle_t *, const security_driver_t *);
189 void security_streaminit(security_stream_t *, const security_driver_t *);
190
191 /* const char *security_geterror(security_handle_t *); */
192 #define security_geterror(handle)       ((handle)->error)
193 void security_seterror(security_handle_t *, const char *, ...)
194      G_GNUC_PRINTF(2,3);
195
196
197 /* void security_connect(const security_driver_t *, const char *, 
198     char *(*)(char *, void *),
199     void (*)(void *, security_handle_t *, security_status_t), 
200     void *, 
201     void *); */
202 #define security_connect(driver, hostname, conf_fn, fn, arg, datap)     \
203     (*(driver)->connect)(hostname, conf_fn, fn, arg, datap)
204
205 /* void security_accept(const security_driver_t *,
206     char *(*)(char *, void *), int, int,
207     void (*)(security_handle_t *, pkt_t *), void *); */
208 #define security_accept(driver, conf_fn, in, out, fn, datap)    \
209     (*(driver)->accept)(driver, conf_fn, in, out, fn, datap)
210 void security_close(security_handle_t *);
211
212 /* ssize_t security_sendpkt(security_handle_t *, const pkt_t *); */
213 #define security_sendpkt(handle, pkt)           \
214     (*(handle)->driver->sendpkt)(handle, pkt)
215
216 /* void security_recvpkt(security_handle_t *,
217     void (*)(void *, pkt_t *, security_status_t), void *, int); */
218 #define security_recvpkt(handle, fn, arg, timeout)      \
219     (*(handle)->driver->recvpkt)(handle, fn, arg, timeout)
220
221 /* void security_recvpkt_cancel(security_handle_t *); */
222 #define security_recvpkt_cancel(handle)         \
223     (*(handle)->driver->recvpkt_cancel)(handle)
224
225 /* const char *security_stream_geterror(security_stream_t *); */
226 #define security_stream_geterror(stream)        ((stream)->error)
227 void security_stream_seterror(security_stream_t *, const char *, ...)
228      G_GNUC_PRINTF(2,3);
229
230 /* security_stream_t *security_stream_server(security_handle_t *); */
231 #define security_stream_server(handle)  \
232     (*(handle)->driver->stream_server)(handle)
233
234 /* int security_stream_accept(security_stream_t *); */
235 #define security_stream_accept(stream)          \
236     (*(stream)->driver->stream_accept)(stream)
237
238 /* security_stream_t *security_stream_client(security_handle_t *, int); */
239 #define security_stream_client(handle, id)      \
240     (*(handle)->driver->stream_client)(handle, id)
241
242 void security_stream_close(security_stream_t *);
243
244 /* int security_stream_auth(security_stream_t *); */
245 #define security_stream_auth(stream)            \
246     (*(stream)->driver->stream_auth)(stream)
247
248 /* int security_stream_id(security_stream_t *); */
249 #define security_stream_id(stream)              \
250     (*(stream)->driver->stream_id)(stream)
251
252 /* int security_stream_write(security_stream_t *, const void *, size_t); */
253 #define security_stream_write(stream, buf, size)        \
254     (*(stream)->driver->stream_write)(stream, buf, size)
255
256 /* void security_stream_read(security_stream_t *,
257     void (*)(void *, void *, size_t), void *); */
258 #define security_stream_read(stream, fn, arg)           \
259     (*(stream)->driver->stream_read)(stream, fn, arg)
260
261 /* void security_stream_read_sync(security_stream_t *, void *); */
262 #define security_stream_read_sync(stream, buf)          \
263     (*(stream)->driver->stream_read_sync)(stream, buf)
264
265 /* void security_stream_read_cancel(security_stream_t *); */
266 #define security_stream_read_cancel(stream)             \
267     (*(stream)->driver->stream_read_cancel)(stream)
268
269 #define security_close_connection(handle, hostname) \
270     (*(handle)->driver->close_connection)(handle, hostname)
271 #endif  /* SECURITY_H */