9d8448e5dcf85284ab93c334c6c81779578f6c53
[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.11 2003/04/26 02:02:19 kovert Exp $
28  *
29  * security api
30  */
31 #ifndef SECURITY_H
32 #define SECURITY_H
33
34 struct security_handle;
35
36 /*
37  * This is a type that gets passed to the security_recvpkt() and
38  * security_connect() callbacks.
39  * It details what the status of this callback is.
40  */
41 typedef enum { S_OK, S_TIMEOUT, S_ERROR } security_status_t;
42
43 /*
44  * This structure defines a security driver.  This driver abstracts
45  * common security actions behind a set of function pointers.  Macros
46  * mask this.
47  */
48 typedef struct security_driver {
49     /*
50      * The name of this driver, eg, "BSD", "KRB4", etc...
51      */
52     const char *name;
53
54     /*
55      * Connects a security handle, for this driver to a remote
56      * host.
57      */
58     void (*connect) P((const char *,
59         char *(*)(char *, void *),
60         void (*)(void *, struct security_handle *, security_status_t),
61         void *));
62
63     /*
64      * This form sets up a callback that returns new handles as
65      * they are received.  It takes an input and output file descriptor.
66      */
67     void (*accept) P((int, int, void (*)(struct security_handle *, pkt_t *)));
68
69     /*
70      * Frees up handles allocated by the previous
71      */
72     void (*close) P((void *));
73
74     /*
75      * This transmits a packet after adding the security information
76      * Returns 0 on success, negative on error.
77      */
78     int (*sendpkt) P((void *, pkt_t *));
79
80     /*
81      * This creates an event in the event handler for receiving pkt_t's
82      * on a security_handle.  The given callback with the given arg
83      * will be called when the driver determines that it has data
84      * for that handle.  The last argument is a timeout, in seconds.
85      * This may be -1 to indicate no timeout.
86      *
87      * If there was an error or timeout, this will be indicated in
88      * the status argument.
89      * 
90      * Only one recvpkt request can exist per handle.
91      */
92     void (*recvpkt) P((void *, void (*)(void *, pkt_t *,
93         security_status_t), void *, int));
94
95     /*
96      * Cancel an outstanding recvpkt request on a handle.
97      */
98     void (*recvpkt_cancel) P((void *));
99
100     /*
101      * Get a stream given a security handle
102      */
103     void *(*stream_server) P((void *));
104
105     /*
106      * Accept a stream created by stream_server
107      */
108     int (*stream_accept) P((void *));
109
110     /*
111      * Get a stream and connect it to a remote given a security handle
112      * and a stream id.
113      */
114     void *(*stream_client) P((void *, int));
115
116     /*
117      * Close a stream opened with stream_server or stream_client
118      */
119     void (*stream_close) P((void *));
120
121     /*
122      * Authenticate a stream.
123      */
124     int (*stream_auth) P((void *));
125
126     /*
127      * Return a numeric id for a stream.
128      */
129     int (*stream_id) P((void *));
130
131     /*
132      * Write to a stream.
133      */
134     int (*stream_write) P((void *, const void *, size_t));
135
136     /*
137      * Read asyncronously from a stream.  Only one request can exist
138      * per stream.
139      */
140     void (*stream_read) P((void *, void (*)(void *, void *, ssize_t), void *));
141
142     /*
143      * Cancel a stream read request
144      */
145     void (*stream_read_cancel) P((void *));
146
147 } security_driver_t;
148
149 /*
150  * This structure is a handle to a connection to a host for transmission
151  * of protocol packets (pkt_t's).  The underlying security type defines
152  * the actual protocol and transport.
153  *
154  * This handle is reference counted so that it can be used inside of
155  * security streams after it has been closed by our callers.
156  */
157 typedef struct security_handle {
158     const security_driver_t *driver;
159     char *error;
160 } security_handle_t;
161
162 /*
163  * This structure is a handle to a stream connection to a host for
164  * transmission of random data such as dumps or index data.
165  */
166 typedef struct security_stream {
167     const security_driver_t *driver;
168     char *error;
169 } security_stream_t;
170
171
172 const security_driver_t *security_getdriver P((const char *));
173 void security_handleinit P((security_handle_t *, const security_driver_t *));
174 void security_streaminit P((security_stream_t *, const security_driver_t *));
175
176 /* const char *security_geterror P((security_handle_t *)); */
177 #define security_geterror(handle)       ((handle)->error)
178 void security_seterror P((security_handle_t *, const char *, ...))
179     __attribute__ ((format (printf, 2, 3)));
180
181
182 /* void security_connect P((const security_driver_t *, const char *,
183     void (*)(void *, security_handle_t *, security_status_t), void *)); */
184 #define security_connect(driver, hostname, conf_fn, fn, arg)    \
185     (*(driver)->connect)(hostname, conf_fn, fn, arg)
186 /* void security_accept P((const security_driver_t *, int, int,
187     void (*)(security_handle_t *, pkt_t *))); */
188 #define security_accept(driver, in, out, fn)    \
189     (*(driver)->accept)(in, out, fn)
190 void security_close P((security_handle_t *));
191
192 /* int security_sendpkt P((security_handle_t *, const pkt_t *)); */
193 #define security_sendpkt(handle, pkt)           \
194     (*(handle)->driver->sendpkt)(handle, pkt)
195
196 /* void security_recvpkt P((security_handle_t *,
197     void (*)(void *, pkt_t *, security_status_t), void *, int); */
198 #define security_recvpkt(handle, fn, arg, timeout)      \
199     (*(handle)->driver->recvpkt)(handle, fn, arg, timeout)
200
201 /* void security_recvpkt_cancel P((security_handle_t *)); */
202 #define security_recvpkt_cancel(handle)         \
203     (*(handle)->driver->recvpkt_cancel)(handle)
204
205 /* const char *security_stream_geterror P((security_stream_t *)); */
206 #define security_stream_geterror(stream)        ((stream)->error)
207 void security_stream_seterror P((security_stream_t *, const char *, ...))
208     __attribute__ ((format (printf, 2, 3)));
209
210 /* security_stream_t *security_stream_server P((security_handle_t *)); */
211 #define security_stream_server(handle)  \
212     (*(handle)->driver->stream_server)(handle)
213
214 /* int security_stream_accept P((security_stream_t *)); */
215 #define security_stream_accept(stream)          \
216     (*(stream)->driver->stream_accept)(stream)
217
218 /* security_stream_t *security_stream_client P((security_handle_t *, int)); */
219 #define security_stream_client(handle, id)      \
220     (*(handle)->driver->stream_client)(handle, id)
221
222 void security_stream_close P((security_stream_t *));
223
224 /* int security_stream_auth P((security_stream_t *)); */
225 #define security_stream_auth(stream)            \
226     (*(stream)->driver->stream_auth)(stream)
227
228 /* int security_stream_id P((security_stream_t *)); */
229 #define security_stream_id(stream)              \
230     (*(stream)->driver->stream_id)(stream)
231
232 /* int security_stream_write P((security_stream_t *, const void *, size_t)); */
233 #define security_stream_write(stream, buf, size)        \
234     (*(stream)->driver->stream_write)(stream, buf, size)
235
236 /* void security_stream_read P((security_stream_t *,
237     void (*)(void *, void *, size_t), void *)); */
238 #define security_stream_read(stream, fn, arg)           \
239     (*(stream)->driver->stream_read)(stream, fn, arg)
240
241 /* void security_stream_read_cancel P((security_stream_t *)); */
242 #define security_stream_read_cancel(stream)             \
243     (*(stream)->driver->stream_read_cancel)(stream)
244
245 #endif  /* SECURITY_H */