6f364df6da86e5bbf930b4b73fb3310edae8c4e1
[debian/amanda] / common-src / amfeatures.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 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 /*
28  * $Id: amfeatures.c,v 1.24 2006/07/19 17:46:07 martinea Exp $
29  *
30  * Feature test related code.
31  */
32
33 #include "amanda.h"
34 #include "amfeatures.h"
35
36 /*
37  *=====================================================================
38  * Initialize the base feature set for this version of Amanda.
39  *
40  * am_feature_t *am_init_feature_set()
41  *
42  * entry:       none
43  * exit:        dynamically allocated feature set structure
44  *=====================================================================
45  */
46
47 am_feature_t *
48 am_init_feature_set(void)
49 {
50     am_feature_t                *f;
51
52     if ((f = am_allocate_feature_set()) != NULL) {
53         /*
54          * Whenever a new feature is added, a new line usually needs
55          * to be added here to show that we support it.
56          */
57         am_add_feature(f, have_feature_support);
58         am_add_feature(f, fe_options_auth);
59
60         am_add_feature(f, fe_selfcheck_req);
61         am_add_feature(f, fe_selfcheck_req_device);
62         am_add_feature(f, fe_selfcheck_rep);
63         am_add_feature(f, fe_sendsize_req_no_options);
64         am_add_feature(f, fe_sendsize_req_options);
65         am_add_feature(f, fe_sendsize_req_device);
66         am_add_feature(f, fe_sendsize_rep);
67         am_add_feature(f, fe_sendbackup_req);
68         am_add_feature(f, fe_sendbackup_req_device);
69         am_add_feature(f, fe_sendbackup_rep);
70         am_add_feature(f, fe_noop_req);
71         am_add_feature(f, fe_noop_rep);
72
73         am_add_feature(f, fe_program_dump);
74         am_add_feature(f, fe_program_gnutar);
75         am_add_feature(f, fe_program_application_api);
76
77         am_add_feature(f, fe_options_compress_fast);
78         am_add_feature(f, fe_options_compress_best);
79         am_add_feature(f, fe_options_srvcomp_fast);
80         am_add_feature(f, fe_options_srvcomp_best);
81         am_add_feature(f, fe_options_no_record);
82         am_add_feature(f, fe_options_bsd_auth);
83         am_add_feature(f, fe_options_index);
84         am_add_feature(f, fe_options_exclude_file);
85         am_add_feature(f, fe_options_exclude_list);
86         am_add_feature(f, fe_options_multiple_exclude);
87         am_add_feature(f, fe_options_optional_exclude);
88         am_add_feature(f, fe_options_include_file);
89         am_add_feature(f, fe_options_include_list);
90         am_add_feature(f, fe_options_multiple_include);
91         am_add_feature(f, fe_options_optional_include);
92         am_add_feature(f, fe_options_kencrypt);
93
94         am_add_feature(f, fe_req_options_maxdumps);
95         am_add_feature(f, fe_req_options_hostname);
96         am_add_feature(f, fe_req_options_features);
97
98         am_add_feature(f, fe_rep_options_features);
99
100         am_add_feature(f, fe_amindexd_fileno_in_OLSD);
101         am_add_feature(f, fe_amindexd_fileno_in_ORLD);
102         am_add_feature(f, fe_amidxtaped_fsf);
103         am_add_feature(f, fe_amidxtaped_label);
104         am_add_feature(f, fe_amidxtaped_device);
105         am_add_feature(f, fe_amidxtaped_host);
106         am_add_feature(f, fe_amidxtaped_disk);
107         am_add_feature(f, fe_amidxtaped_datestamp);
108         am_add_feature(f, fe_amidxtaped_header);
109         am_add_feature(f, fe_amidxtaped_config);
110
111         am_add_feature(f, fe_recover_splits);
112         am_add_feature(f, fe_amidxtaped_exchange_features);
113         am_add_feature(f, fe_partial_estimate);
114         am_add_feature(f, fe_calcsize_estimate);
115         am_add_feature(f, fe_selfcheck_calcsize);
116         am_add_feature(f, fe_options_compress_cust);
117         am_add_feature(f, fe_options_srvcomp_cust);
118         am_add_feature(f, fe_options_encrypt_cust);
119         am_add_feature(f, fe_options_encrypt_serv_cust);
120         am_add_feature(f, fe_options_client_decrypt_option);
121         am_add_feature(f, fe_options_server_decrypt_option);
122
123         am_add_feature(f, fe_amindexd_marshall_in_OLSD);
124         am_add_feature(f, fe_amindexd_marshall_in_ORLD);
125         am_add_feature(f, fe_amindexd_marshall_in_DHST);
126
127         am_add_feature(f, fe_amrecover_FEEDME);
128         am_add_feature(f, fe_amrecover_timestamp);
129
130         am_add_feature(f, fe_interface_quoted_text);
131
132         am_add_feature(f, fe_program_star);
133
134         am_add_feature(f, fe_amindexd_options_hostname);
135         am_add_feature(f, fe_amindexd_options_features);
136         am_add_feature(f, fe_amindexd_options_auth);
137
138         am_add_feature(f, fe_amidxtaped_options_hostname);
139         am_add_feature(f, fe_amidxtaped_options_features);
140         am_add_feature(f, fe_amidxtaped_options_auth);
141
142         am_add_feature(f, fe_amrecover_message);
143         am_add_feature(f, fe_amrecover_feedme_tape);
144
145         am_add_feature(f, fe_req_options_config);
146
147         am_add_feature(f, fe_rep_sendsize_quoted_error);
148         am_add_feature(f, fe_req_xml);
149         am_add_feature(f, fe_pp_script);
150         am_add_feature(f, fe_amindexd_DLE);
151         am_add_feature(f, fe_amrecover_dle_in_header);
152         am_add_feature(f, fe_xml_estimate);
153         am_add_feature(f, fe_xml_property_priority);
154         am_add_feature(f, fe_sendsize_rep_warning);
155         am_add_feature(f, fe_xml_estimatelist);
156         am_add_feature(f, fe_xml_level_server);
157         am_add_feature(f, fe_xml_data_path);
158         am_add_feature(f, fe_xml_directtcp_list);
159         am_add_feature(f, fe_amidxtaped_datapath);
160         am_add_feature(f, fe_sendbackup_noop);
161         am_add_feature(f, fe_amrecover_origsize_in_header);
162         am_add_feature(f, fe_amidxtaped_abort);
163         am_add_feature(f, fe_amrecover_correct_disk_quoting);
164     }
165     return f;
166 }
167
168 /*
169  *=====================================================================
170  * Set a default feature set for client that doesn't have noop service.
171  * This is all the features available in 2.4.2p2.
172  *
173  * entry:       none
174  * exit: dynamically allocated feature set
175  *=====================================================================
176  */
177  
178 am_feature_t *
179 am_set_default_feature_set(void)
180 {
181     am_feature_t                *f;
182
183     if ((f = am_allocate_feature_set()) != NULL) {
184
185         am_add_feature(f, fe_selfcheck_req);
186         am_add_feature(f, fe_selfcheck_rep);
187         am_add_feature(f, fe_sendsize_req_no_options);
188         am_add_feature(f, fe_sendsize_rep);
189         am_add_feature(f, fe_sendbackup_req);
190         am_add_feature(f, fe_sendbackup_rep);
191
192         am_add_feature(f, fe_program_dump);
193         am_add_feature(f, fe_program_gnutar);
194
195         am_add_feature(f, fe_options_compress_fast);
196         am_add_feature(f, fe_options_compress_best);
197         am_add_feature(f, fe_options_srvcomp_fast);
198         am_add_feature(f, fe_options_srvcomp_best);
199         am_add_feature(f, fe_options_no_record);
200         am_add_feature(f, fe_options_bsd_auth);
201         am_add_feature(f, fe_options_index);
202         am_add_feature(f, fe_options_exclude_file);
203         am_add_feature(f, fe_options_exclude_list);
204         am_add_feature(f, fe_options_kencrypt);
205
206         am_add_feature(f, fe_req_options_maxdumps);
207         am_add_feature(f, fe_req_options_hostname);
208         am_add_feature(f, fe_req_options_features);
209
210         am_add_feature(f, fe_rep_options_sendbackup_options);
211     }
212     return f;
213 }
214
215 /*
216  *=====================================================================
217  * Allocate space for a feature set.
218  *
219  * am_feature_t *am_allocate_feature_set()
220  *
221  * entry:       none
222  * exit:        dynamically allocated feature set structure
223  *=====================================================================
224  */
225
226 am_feature_t *
227 am_allocate_feature_set(void)
228 {
229     size_t                      nbytes;
230     am_feature_t                *result;
231
232     result = (am_feature_t *)alloc(SIZEOF(*result));
233     memset(result, 0, SIZEOF(*result));
234     nbytes = (((size_t)last_feature) + 8) >> 3;
235     result->size = nbytes;
236     result->bytes = (unsigned char *)alloc(nbytes);
237     memset(result->bytes, 0, nbytes);
238     return result;
239 }
240
241 /*
242  *=====================================================================
243  * Release space allocated to a feature set.
244  *
245  * void am_release_feature_set(am_feature_t *f)
246  *
247  * entry:       f = feature set to release
248  * exit:        none
249  *=====================================================================
250  */
251
252 void
253 am_release_feature_set(
254     am_feature_t        *f)
255 {
256     if (f != NULL) {
257         amfree(f->bytes);
258         f->size = 0;
259     }
260     amfree(f);
261 }
262
263 /*
264  *=====================================================================
265  * Add a feature to a feature set.
266  *
267  * int am_add_feature(am_feature_t *f, am_feature_e n)
268  *
269  * entry:       f = feature set to add to
270  *              n = feature to add
271  * exit:        non-zero if feature added, else zero (e.g. if the feature
272  *              is beyond what is currently supported)
273  *=====================================================================
274  */
275
276 int
277 am_add_feature(
278     am_feature_t        *f,
279     am_feature_e        n)
280 {
281     size_t                      byte;
282     int                         bit;
283     int                         result = 0;
284
285     if (f != NULL && (int)n >= 0) {
286         byte = ((size_t)n) >> 3;
287         if (byte < f->size) {
288             bit = ((int)n) & 0x7;
289             f->bytes[byte] = (unsigned char)((int)f->bytes[byte] | (unsigned char)(1 << bit));
290             result = 1;
291         }
292     }
293     return result;
294 }
295
296 /*
297  *=====================================================================
298  * Remove a feature from a feature set.
299  *
300  * int am_remove_feature(am_feature_t *f, am_feature_e n)
301  *
302  * entry:       f = feature set to remove from
303  *              n = feature to remove
304  * exit:        non-zero if feature removed, else zero (e.g. if the feature
305  *              is beyond what is currently supported)
306  *=====================================================================
307  */
308
309 int
310 am_remove_feature(
311     am_feature_t        *f,
312     am_feature_e        n)
313 {
314     size_t                      byte;
315     int                         bit;
316     int                         result = 0;
317
318     if (f != NULL && (int)n >= 0) {
319         byte = ((size_t)n) >> 3;
320         if (byte < f->size) {
321             bit = ((int)n) & 0x7;
322             f->bytes[byte] = (unsigned char)((int)f->bytes[byte] & (unsigned char)~(1 << bit));
323             result = 1;
324         }
325     }
326     return result;
327 }
328
329 /*
330  *=====================================================================
331  * Return true if a given feature is available.
332  *
333  * int am_has_feature(am_feature_t *f, am_feature_e n)
334  *
335  * entry:       f = feature set to test
336  *              n = feature to test
337  * exit:        non-zero if feature is enabled
338  *=====================================================================
339  */
340
341 int
342 am_has_feature(
343     am_feature_t        *f,
344     am_feature_e        n)
345 {
346     size_t                      byte;
347     int                         bit;
348     int                         result = 0;
349
350     if (f != NULL && (int)n >= 0) {
351         byte = ((size_t)n) >> 3;
352         if (byte < f->size) {
353             bit = ((int)n) & 0x7;
354             result = ((f->bytes[byte] & (1 << bit)) != 0);
355         }
356     }
357     return result;
358 }
359
360 /*
361  *=====================================================================
362  * Convert a feature set to string.
363  *
364  * char *am_feature_to_string(am_feature_t *f)
365  *
366  * entry:       f = feature set to convet
367  * exit:        dynamically allocated string
368  *=====================================================================
369  */
370
371 char *
372 am_feature_to_string(
373     am_feature_t        *f)
374 {
375     char                        *result;
376     size_t                      i;
377
378     if (f == NULL) {
379         result = stralloc(_("UNKNOWNFEATURE"));
380     } else {
381         result = alloc((f->size * 2) + 1);
382         for (i = 0; i < f->size; i++) {
383             g_snprintf(result + (i * 2), 2 + 1, "%02x", f->bytes[i]);
384         }
385         result[i * 2] = '\0';
386     }
387     return result;
388 }
389
390 /*
391  *=====================================================================
392  * Convert a sting back to a feature set.
393  *
394  * am_feature_t *am_string_to_feature(char *s)
395  *
396  * entry:       s = string to convert
397  * exit:        dynamically allocated feature set
398  *
399  * Note: if the string is longer than the list of features we support,
400  * the remaining input features are ignored.  If it is shorter, the
401  * missing features are disabled.
402  *
403  * If the string is not formatted properly (not a multiple of two bytes),
404  * NULL is returned.
405  *
406  * Conversion stops at the first non-hex character.
407  *=====================================================================
408  */
409
410 am_feature_t *
411 am_string_to_feature(
412     char                *s)
413 {
414     am_feature_t                *f = NULL;
415     size_t                      i;
416     int                         ch1, ch2;
417     char *                      orig = s;
418
419     if (s != NULL && strcmp(s,"UNKNOWNFEATURE") != 0) {
420         f = am_allocate_feature_set();
421         for (i = 0; i < f->size && (ch1 = *s++) != '\0'; i++) {
422             if (isdigit(ch1)) {
423                 ch1 -= '0';
424             } else if (ch1 >= 'a' && ch1 <= 'f') {
425                 ch1 -= 'a';
426                 ch1 += 10;
427             } else if (ch1 >= 'A' && ch1 <= 'F') {
428                 ch1 -= 'A';
429                 ch1 += 10;
430             } else {
431                 goto bad;
432             }
433             ch2 = *s++;
434             if (isdigit(ch2)) {
435                 ch2 -= '0';
436             } else if (ch2 >= 'a' && ch2 <= 'f') {
437                 ch2 -= 'a';
438                 ch2 += 10;
439             } else if (ch2 >= 'A' && ch2 <= 'F') {
440                 ch2 -= 'A';
441                 ch2 += 10;
442             } else if (ch2 == '\0') {
443                 g_warning("odd number of digits in amfeature string; truncating");
444                 break;
445             } else {
446                 goto bad;
447             }
448             f->bytes[i] = (unsigned char)((ch1 << 4) | ch2);
449         }
450     }
451     return f;
452
453 bad:
454     g_warning("Bad feature string '%s'", orig);
455     am_release_feature_set(f);
456     return NULL;
457 }