Imported Upstream version 3.3.3
[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  * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
5  * All Rights Reserved.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that
10  * copyright notice and this permission notice appear in supporting
11  * documentation, and that the name of U.M. not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  U.M. makes no representations about the
14  * suitability of this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  *
17  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Authors: the Amanda Development Team.  Its members are listed in a
25  * file named AUTHORS, in the root directory of this distribution.
26  */
27 /*
28  * $Id: security.c,v 1.28 2006/05/25 01:47:12 johnfranks Exp $
29  *
30  * Security driver interface for the Amanda backup system.
31  */
32
33 #include "amanda.h"
34 #include "arglist.h"
35 #include "packet.h"
36 #include "security.h"
37
38 #ifdef BSD_SECURITY
39 extern const security_driver_t bsd_security_driver;
40 #endif
41 #ifdef KRB5_SECURITY
42 extern const security_driver_t krb5_security_driver;
43 #endif
44 #ifdef RSH_SECURITY
45 extern const security_driver_t rsh_security_driver;
46 #endif
47 #ifdef SSH_SECURITY
48 extern const security_driver_t ssh_security_driver;
49 #endif
50 #ifdef BSDTCP_SECURITY
51 extern const security_driver_t bsdtcp_security_driver;
52 #endif
53 #ifdef BSDUDP_SECURITY
54 extern const security_driver_t bsdudp_security_driver;
55 #endif
56 extern const security_driver_t local_security_driver;
57
58 static const security_driver_t *drivers[] = {
59 #ifdef BSD_SECURITY
60     &bsd_security_driver,
61 #endif
62 #ifdef KRB5_SECURITY
63     &krb5_security_driver,
64 #endif
65 #ifdef RSH_SECURITY
66     &rsh_security_driver,
67 #endif
68 #ifdef SSH_SECURITY
69     &ssh_security_driver,
70 #endif
71 #ifdef BSDTCP_SECURITY
72     &bsdtcp_security_driver,
73 #endif
74 #ifdef BSDUDP_SECURITY
75     &bsdudp_security_driver,
76 #endif
77     &local_security_driver,
78 };
79 #define NDRIVERS        (size_t)(sizeof(drivers) / sizeof(drivers[0]))
80
81 /*
82  * Given a name of a security type, returns the driver structure
83  */
84 const security_driver_t *
85 security_getdriver(
86     const char *        name)
87 {
88     size_t i;
89
90     assert(name != NULL);
91
92     for (i = 0; i < NDRIVERS; i++) {
93         if (strcasecmp(name, drivers[i]->name) == 0) {
94             dbprintf(_("security_getdriver(name=%s) returns %p\n"),
95                       name, drivers[i]);
96             return (drivers[i]);
97         }
98     }
99     dbprintf(_("security_getdriver(name=%s) returns NULL\n"), name);
100     return (NULL);
101 }
102
103 /*
104  * For the drivers: initialize the common part of a security_handle_t
105  */
106 void
107 security_handleinit(
108     security_handle_t *         handle,
109     const security_driver_t *   driver)
110 {
111     dbprintf(_("security_handleinit(handle=%p, driver=%p (%s))\n"),
112               handle, driver, driver->name);
113     handle->driver = driver;
114     handle->error = stralloc(_("unknown protocol error"));
115 }
116
117 printf_arglist_function1(void security_seterror, security_handle_t *, handle,
118     const char *, fmt)
119 {
120     static char buf[1024];
121     va_list argp;
122
123     assert(handle->error != NULL);
124     arglist_start(argp, fmt);
125     g_vsnprintf(buf, SIZEOF(buf), fmt, argp);
126     arglist_end(argp);
127     handle->error = newstralloc(handle->error, buf);
128     dbprintf(_("security_seterror(handle=%p, driver=%p (%s) error=%s)\n"),
129               handle, handle->driver,
130               handle->driver->name, handle->error);
131 }
132
133 void
134 security_close(
135     security_handle_t * handle)
136 {
137     dbprintf(_("security_close(handle=%p, driver=%p (%s))\n"),
138               handle, handle->driver,
139               handle->driver->name);
140     amfree(handle->error);
141     (*handle->driver->close)(handle);
142 }
143
144 /*
145  * For the drivers: initialize the common part of a security_stream_t
146  */
147 void
148 security_streaminit(
149     security_stream_t *         stream,
150     const security_driver_t *   driver)
151 {
152     dbprintf(_("security_streaminit(stream=%p, driver=%p (%s))\n"),
153               stream, driver, driver->name);
154     stream->driver = driver;
155     stream->error = stralloc(_("unknown stream error"));
156 }
157
158 printf_arglist_function1(void security_stream_seterror,
159     security_stream_t *, stream,
160     const char *, fmt)
161 {
162     static char buf[1024];
163     va_list argp;
164
165     arglist_start(argp, fmt);
166     g_vsnprintf(buf, SIZEOF(buf), fmt, argp);
167     arglist_end(argp);
168     stream->error = newstralloc(stream->error, buf);
169     dbprintf(_("security_stream_seterr(%p, %s)\n"), stream, stream->error);
170 }
171
172 void
173 security_stream_close(
174     security_stream_t * stream)
175 {
176     dbprintf(_("security_stream_close(%p)\n"), stream);
177     amfree(stream->error);
178     (*stream->driver->stream_close)(stream);
179 }