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