54c6b99b1b2f9b96c09f46ceadb4b9486305429f
[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 extern const security_driver_t local_security_driver;
59
60 static const security_driver_t *drivers[] = {
61 #ifdef BSD_SECURITY
62     &bsd_security_driver,
63 #endif
64 #ifdef KRB4_SECURITY
65     &krb4_security_driver,
66 #endif
67 #ifdef KRB5_SECURITY
68     &krb5_security_driver,
69 #endif
70 #ifdef RSH_SECURITY
71     &rsh_security_driver,
72 #endif
73 #ifdef SSH_SECURITY
74     &ssh_security_driver,
75 #endif
76 #ifdef BSDTCP_SECURITY
77     &bsdtcp_security_driver,
78 #endif
79 #ifdef BSDUDP_SECURITY
80     &bsdudp_security_driver,
81 #endif
82     &local_security_driver,
83 };
84 #define NDRIVERS        (size_t)(sizeof(drivers) / sizeof(drivers[0]))
85
86 /*
87  * Given a name of a security type, returns the driver structure
88  */
89 const security_driver_t *
90 security_getdriver(
91     const char *        name)
92 {
93     size_t i;
94
95     assert(name != NULL);
96
97     for (i = 0; i < NDRIVERS; i++) {
98         if (strcasecmp(name, drivers[i]->name) == 0) {
99             dbprintf(_("security_getdriver(name=%s) returns %p\n"),
100                       name, drivers[i]);
101             return (drivers[i]);
102         }
103     }
104     dbprintf(_("security_getdriver(name=%s) returns NULL\n"), name);
105     return (NULL);
106 }
107
108 /*
109  * For the drivers: initialize the common part of a security_handle_t
110  */
111 void
112 security_handleinit(
113     security_handle_t *         handle,
114     const security_driver_t *   driver)
115 {
116     dbprintf(_("security_handleinit(handle=%p, driver=%p (%s))\n"),
117               handle, driver, driver->name);
118     handle->driver = driver;
119     handle->error = stralloc(_("unknown protocol error"));
120 }
121
122 printf_arglist_function1(void security_seterror, security_handle_t *, handle,
123     const char *, fmt)
124 {
125     static char buf[1024];
126     va_list argp;
127
128     assert(handle->error != NULL);
129     arglist_start(argp, fmt);
130     g_vsnprintf(buf, SIZEOF(buf), fmt, argp);
131     arglist_end(argp);
132     handle->error = newstralloc(handle->error, buf);
133     dbprintf(_("security_seterror(handle=%p, driver=%p (%s) error=%s)\n"),
134               handle, handle->driver,
135               handle->driver->name, handle->error);
136 }
137
138 void
139 security_close(
140     security_handle_t * handle)
141 {
142     dbprintf(_("security_close(handle=%p, driver=%p (%s))\n"),
143               handle, handle->driver,
144               handle->driver->name);
145     amfree(handle->error);
146     (*handle->driver->close)(handle);
147 }
148
149 /*
150  * For the drivers: initialize the common part of a security_stream_t
151  */
152 void
153 security_streaminit(
154     security_stream_t *         stream,
155     const security_driver_t *   driver)
156 {
157     dbprintf(_("security_streaminit(stream=%p, driver=%p (%s))\n"),
158               stream, driver, driver->name);
159     stream->driver = driver;
160     stream->error = stralloc(_("unknown stream error"));
161 }
162
163 printf_arglist_function1(void security_stream_seterror,
164     security_stream_t *, stream,
165     const char *, fmt)
166 {
167     static char buf[1024];
168     va_list argp;
169
170     arglist_start(argp, fmt);
171     g_vsnprintf(buf, SIZEOF(buf), fmt, argp);
172     arglist_end(argp);
173     stream->error = newstralloc(stream->error, buf);
174     dbprintf(_("security_stream_seterr(%p, %s)\n"), stream, stream->error);
175 }
176
177 void
178 security_stream_close(
179     security_stream_t * stream)
180 {
181     dbprintf(_("security_stream_close(%p)\n"), stream);
182     amfree(stream->error);
183     (*stream->driver->stream_close)(stream);
184 }