795d64eda86df73eacd91f34a4f2de0c586a9e33
[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.26 2004/03/16 19:09:39 martinea 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
53 static const security_driver_t *drivers[] = {
54 #ifdef BSD_SECURITY
55     &bsd_security_driver,
56 #endif
57 #ifdef KRB4_SECURITY
58     &krb4_security_driver,
59 #endif
60 #ifdef KRB5_SECURITY
61     &krb5_security_driver,
62 #endif
63 #ifdef RSH_SECURITY
64     &rsh_security_driver,
65 #endif
66 #ifdef SSH_SECURITY
67     &ssh_security_driver,
68 #endif
69 };
70 #define NDRIVERS        (sizeof(drivers) / sizeof(drivers[0]))
71
72 /*
73  * Given a name of a security type, returns the driver structure
74  */
75 const security_driver_t *
76 security_getdriver(name)
77     const char *name;
78 {
79     int i;
80
81     assert(name != NULL);
82
83     for (i = 0; i < NDRIVERS; i++)
84         if (strcasecmp(name, drivers[i]->name) == 0)
85             return (drivers[i]);
86     return (NULL);
87 }
88
89 /*
90  * For the drivers: initialize the common part of a security_handle_t
91  */
92 void
93 security_handleinit(handle, driver)
94     security_handle_t *handle;
95     const security_driver_t *driver;
96 {
97
98     handle->driver = driver;
99     handle->error = stralloc("unknown protocol error");
100 }
101
102 printf_arglist_function1(void security_seterror, security_handle_t *, handle,
103     const char *, fmt)
104 {
105     static char buf[256];
106     va_list argp;
107
108     assert(handle->error != NULL);
109     arglist_start(argp, fmt);
110     vsnprintf(buf, sizeof(buf), fmt, argp);
111     arglist_end(argp);
112     handle->error = newstralloc(handle->error, buf);
113 }
114
115 void
116 security_close(handle)
117     security_handle_t *handle;
118 {
119
120     amfree(handle->error);
121     (*handle->driver->close)(handle);
122 }
123
124 /*
125  * For the drivers: initialize the common part of a security_stream_t
126  */
127 void
128 security_streaminit(stream, driver)
129     security_stream_t *stream;
130     const security_driver_t *driver;
131 {
132
133     stream->driver = driver;
134     stream->error = stralloc("unknown stream error");
135 }
136
137 printf_arglist_function1(void security_stream_seterror,
138     security_stream_t *, stream,
139     const char *, fmt)
140 {
141     static char buf[256];
142     va_list argp;
143
144     assert(stream->error != NULL);
145     arglist_start(argp, fmt);
146     vsnprintf(buf, sizeof(buf), fmt, argp);
147     arglist_end(argp);
148     stream->error = newstralloc(stream->error, buf);
149 }
150
151 void
152 security_stream_close(stream)
153     security_stream_t *stream;
154 {
155
156     amfree(stream->error);
157     (*stream->driver->stream_close)(stream);
158 }