76cf78ddc345427be060b7b01b483e749c0c0387
[debian/amanda] / common-src / security.c
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.c,v 1.28 2006/05/25 01:47:12 johnfranks Exp $
28  *
29  * Security driver interface for the Amanda backup system.
30  */
31
32 #include "amanda.h"
33 #include "arglist.h"
34 #include "packet.h"
35 #include "security.h"
36
37 #ifdef BSD_SECURITY
38 extern const security_driver_t bsd_security_driver;
39 #endif
40 #ifdef KRB4_SECURITY
41 extern const security_driver_t krb4_security_driver;
42 #endif
43 #ifdef KRB5_SECURITY
44 extern const security_driver_t krb5_security_driver;
45 #endif
46 #ifdef RSH_SECURITY
47 extern const security_driver_t rsh_security_driver;
48 #endif
49 #ifdef SSH_SECURITY
50 extern const security_driver_t ssh_security_driver;
51 #endif
52 #ifdef BSDTCP_SECURITY
53 extern const security_driver_t bsdtcp_security_driver;
54 #endif
55 #ifdef BSDUDP_SECURITY
56 extern const security_driver_t bsdudp_security_driver;
57 #endif
58
59 static const security_driver_t *drivers[] = {
60 #ifdef BSD_SECURITY
61     &bsd_security_driver,
62 #endif
63 #ifdef KRB4_SECURITY
64     &krb4_security_driver,
65 #endif
66 #ifdef KRB5_SECURITY
67     &krb5_security_driver,
68 #endif
69 #ifdef RSH_SECURITY
70     &rsh_security_driver,
71 #endif
72 #ifdef SSH_SECURITY
73     &ssh_security_driver,
74 #endif
75 #ifdef BSDTCP_SECURITY
76     &bsdtcp_security_driver,
77 #endif
78 #ifdef BSDUDP_SECURITY
79     &bsdudp_security_driver,
80 #endif
81 };
82 #define NDRIVERS        (size_t)(sizeof(drivers) / sizeof(drivers[0]))
83
84 /*
85  * Given a name of a security type, returns the driver structure
86  */
87 const security_driver_t *
88 security_getdriver(
89     const char *        name)
90 {
91     size_t i;
92
93     assert(name != NULL);
94
95     for (i = 0; i < NDRIVERS; i++) {
96         if (strcasecmp(name, drivers[i]->name) == 0) {
97             dbprintf(("security_getdriver(name=%s) returns %p\n",
98                         name, drivers[i]));
99             return (drivers[i]);
100         }
101     }
102     dbprintf(("security_getdriver(name=%s) returns NULL\n", name));
103     return (NULL);
104 }
105
106 /*
107  * For the drivers: initialize the common part of a security_handle_t
108  */
109 void
110 security_handleinit(
111     security_handle_t *         handle,
112     const security_driver_t *   driver)
113 {
114     dbprintf(("security_handleinit(handle=%p, driver=%p (%s))\n",
115                 handle, driver, driver->name));
116     handle->driver = driver;
117     handle->error = stralloc("unknown protocol error");
118 }
119
120 printf_arglist_function1(void security_seterror, security_handle_t *, handle,
121     const char *, fmt)
122 {
123     static char buf[256];
124     va_list argp;
125
126     assert(handle->error != NULL);
127     arglist_start(argp, fmt);
128     vsnprintf(buf, SIZEOF(buf), fmt, argp);
129     arglist_end(argp);
130     handle->error = newstralloc(handle->error, buf);
131     dbprintf(("security_seterror(handle=%p, driver=%p (%s) error=%s)\n",
132                 handle, handle->driver, handle->driver->name, handle->error));
133 }
134
135 void
136 security_close(
137     security_handle_t * handle)
138 {
139     dbprintf(("security_close(handle=%p, driver=%p (%s))\n",
140                 handle, handle->driver, handle->driver->name));
141     amfree(handle->error);
142     (*handle->driver->close)(handle);
143 }
144
145 /*
146  * For the drivers: initialize the common part of a security_stream_t
147  */
148 void
149 security_streaminit(
150     security_stream_t *         stream,
151     const security_driver_t *   driver)
152 {
153     dbprintf(("security_streaminit(stream=%p, driver=%p (%s))\n",
154                 stream, driver, driver->name));
155     stream->driver = driver;
156     stream->error = stralloc("unknown stream error");
157 }
158
159 printf_arglist_function1(void security_stream_seterror,
160     security_stream_t *, stream,
161     const char *, fmt)
162 {
163     static char buf[256];
164     va_list argp;
165
166     arglist_start(argp, fmt);
167     vsnprintf(buf, SIZEOF(buf), fmt, argp);
168     arglist_end(argp);
169     stream->error = newstralloc(stream->error, buf);
170     dbprintf(("security_stream_seterr(%p, %s)\n", stream, stream->error));
171 }
172
173 void
174 security_stream_close(
175     security_stream_t * stream)
176 {
177     dbprintf(("security_stream_close(%p)\n", stream));
178     amfree(stream->error);
179     (*stream->driver->stream_close)(stream);
180 }