Imported Upstream version 2.4.4p3
[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.1.2.9 2003/01/01 23:28:52 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()
49 {
50     am_feature_t                *f = NULL;
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
76         am_add_feature(f, fe_options_compress_fast);
77         am_add_feature(f, fe_options_compress_best);
78         am_add_feature(f, fe_options_srvcomp_fast);
79         am_add_feature(f, fe_options_srvcomp_best);
80         am_add_feature(f, fe_options_no_record);
81         am_add_feature(f, fe_options_bsd_auth);
82         am_add_feature(f, fe_options_index);
83         am_add_feature(f, fe_options_exclude_file);
84         am_add_feature(f, fe_options_exclude_list);
85         am_add_feature(f, fe_options_multiple_exclude);
86         am_add_feature(f, fe_options_optional_exclude);
87         am_add_feature(f, fe_options_include_file);
88         am_add_feature(f, fe_options_include_list);
89         am_add_feature(f, fe_options_multiple_include);
90         am_add_feature(f, fe_options_optional_include);
91         am_add_feature(f, fe_options_krb4_auth);
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_nargs);
110         am_add_feature(f, fe_amidxtaped_config);
111     }
112     return f;
113 }
114
115 /*
116  *=====================================================================
117  * Set a default feature set for client that doesn't have noop service.
118  * This is all the features available in 2.4.2p2.
119  *
120  * entry:       none
121  * exit: dynamically allocated feature set
122  *=====================================================================
123  */
124  
125 am_feature_t *
126 am_set_default_feature_set(void)
127 {
128     am_feature_t                *f;
129
130     if ((f = am_allocate_feature_set()) != NULL) {
131
132         am_add_feature(f, fe_selfcheck_req);
133         am_add_feature(f, fe_selfcheck_rep);
134         am_add_feature(f, fe_sendsize_req_no_options);
135         am_add_feature(f, fe_sendsize_rep);
136         am_add_feature(f, fe_sendbackup_req);
137         am_add_feature(f, fe_sendbackup_rep);
138
139         am_add_feature(f, fe_program_dump);
140         am_add_feature(f, fe_program_gnutar);
141
142         am_add_feature(f, fe_options_compress_fast);
143         am_add_feature(f, fe_options_compress_best);
144         am_add_feature(f, fe_options_srvcomp_fast);
145         am_add_feature(f, fe_options_srvcomp_best);
146         am_add_feature(f, fe_options_no_record);
147         am_add_feature(f, fe_options_bsd_auth);
148         am_add_feature(f, fe_options_index);
149         am_add_feature(f, fe_options_exclude_file);
150         am_add_feature(f, fe_options_exclude_list);
151         am_add_feature(f, fe_options_krb4_auth);
152         am_add_feature(f, fe_options_kencrypt);
153
154         am_add_feature(f, fe_req_options_maxdumps);
155         am_add_feature(f, fe_req_options_hostname);
156         am_add_feature(f, fe_req_options_features);
157
158         am_add_feature(f, fe_rep_options_sendbackup_options);
159     }
160     return f;
161 }
162
163 /*
164  *=====================================================================
165  * Allocate space for a feature set.
166  *
167  * am_feature_t *am_allocate_feature_set()
168  *
169  * entry:       none
170  * exit:        dynamically allocated feature set structure
171  *=====================================================================
172  */
173
174 am_feature_t *
175 am_allocate_feature_set()
176 {
177     size_t                      nbytes;
178     am_feature_t                *result;
179
180     result = (am_feature_t *)alloc(sizeof(*result));
181     memset(result, 0, sizeof(*result));
182     nbytes = (((size_t)last_feature) + 8) >> 3;
183     result->size = nbytes;
184     result->bytes = (unsigned char *)alloc(nbytes);
185     memset(result->bytes, 0, nbytes);
186     return result;
187 }
188
189 /*
190  *=====================================================================
191  * Release space allocated to a feature set.
192  *
193  * void am_release_feature_set(am_feature_t *f)
194  *
195  * entry:       f = feature set to release
196  * exit:        none
197  *=====================================================================
198  */
199
200 void
201 am_release_feature_set(f)
202     am_feature_t                *f;
203 {
204     if (f != NULL) {
205         amfree(f->bytes);
206         f->size = 0;
207     }
208     amfree(f);
209 }
210
211 /*
212  *=====================================================================
213  * Add a feature to a feature set.
214  *
215  * int am_add_feature(am_feature_t *f, am_feature_e n)
216  *
217  * entry:       f = feature set to add to
218  *              n = feature to add
219  * exit:        non-zero if feature added, else zero (e.g. if the feature
220  *              is beyond what is currently supported)
221  *=====================================================================
222  */
223
224 int
225 am_add_feature(f, n)
226     am_feature_t                *f;
227     am_feature_e                n;
228 {
229     size_t                      byte;
230     int                         bit;
231     int                         result = 0;
232
233     if (f != NULL && (int)n >= 0) {
234         byte = ((size_t)n) >> 3;
235         if (byte < f->size) {
236             bit = ((int)n) & 0x7;
237             f->bytes[byte] |= (1 << bit);
238             result = 1;
239         }
240     }
241     return result;
242 }
243
244 /*
245  *=====================================================================
246  * Remove a feature from a feature set.
247  *
248  * int am_remove_feature(am_feature_t *f, am_feature_e n)
249  *
250  * entry:       f = feature set to remove from
251  *              n = feature to remove
252  * exit:        non-zero if feature removed, else zero (e.g. if the feature
253  *              is beyond what is currently supported)
254  *=====================================================================
255  */
256
257 int
258 am_remove_feature(f, n)
259     am_feature_t                *f;
260     am_feature_e                n;
261 {
262     size_t                      byte;
263     int                         bit;
264     int                         result = 0;
265
266     if (f != NULL && (int)n >= 0) {
267         byte = ((size_t)n) >> 3;
268         if (byte < f->size) {
269             bit = ((int)n) & 0x7;
270             f->bytes[byte] &= ~(1 << bit);
271             result = 1;
272         }
273     }
274     return result;
275 }
276
277 /*
278  *=====================================================================
279  * Return true if a given feature is available.
280  *
281  * int am_has_feature(am_feature_t *f, am_feature_e n)
282  *
283  * entry:       f = feature set to test
284  *              n = feature to test
285  * exit:        non-zero if feature is enabled
286  *=====================================================================
287  */
288
289 int
290 am_has_feature(f, n)
291     am_feature_t                *f;
292     am_feature_e                n;
293 {
294     size_t                      byte;
295     int                         bit;
296     int                         result = 0;
297
298     if (f != NULL && (int)n >= 0) {
299         byte = ((size_t)n) >> 3;
300         if (byte < f->size) {
301             bit = ((int)n) & 0x7;
302             result = ((f->bytes[byte] & (1 << bit)) != 0);
303         }
304     }
305     return result;
306 }
307
308 /*
309  *=====================================================================
310  * Convert a feature set to string.
311  *
312  * char *am_feature_to_string(am_feature_t *f)
313  *
314  * entry:       f = feature set to convet
315  * exit:        dynamically allocated string
316  *=====================================================================
317  */
318
319 char *
320 am_feature_to_string(f)
321     am_feature_t                *f;
322 {
323     char                        *result;
324     size_t                      i;
325
326     if (f == NULL) {
327         result = stralloc("UNKNOWNFEATURE");
328     } else {
329         result = alloc((f->size * 2) + 1);
330         for (i = 0; i < f->size; i++) {
331             ap_snprintf(result + (i * 2), 2 + 1, "%02x", f->bytes[i]);
332         }
333         result[i * 2] = '\0';
334     }
335     return result;
336 }
337
338 /*
339  *=====================================================================
340  * Convert a sting back to a feature set.
341  *
342  * am_feature_t *am_string_to_feature(char *s)
343  *
344  * entry:       s = string to convert
345  * exit:        dynamically allocated feature set
346  *
347  * Note: if the string is longer than the list of features we support,
348  * the remaining input features are ignored.  If it is shorter, the
349  * missing features are disabled.
350  *
351  * If the string is not formatted properly (not a multiple of two bytes),
352  * NULL is returned.
353  *
354  * Conversion stops at the first non-hex character.
355  *=====================================================================
356  */
357
358 am_feature_t *
359 am_string_to_feature(s)
360     char                        *s;
361 {
362     am_feature_t                *f = NULL;
363     size_t                      i;
364     int                         ch1, ch2;
365
366     if (s != NULL && strcmp(s,"UNKNOWNFEATURE") != 0) {
367         f = am_allocate_feature_set();
368         for (i = 0; i < f->size && (ch1 = *s++) != '\0'; i++) {
369             if (isdigit(ch1)) {
370                 ch1 -= '0';
371             } else if (ch1 >= 'a' && ch1 <= 'f') {
372                 ch1 -= 'a';
373                 ch1 += 10;
374             } else if (ch1 >= 'A' && ch1 <= 'F') {
375                 ch1 -= 'a';
376                 ch1 += 10;
377             } else {
378                 break;
379             }
380             ch2 = *s++;
381             if (isdigit(ch2)) {
382                 ch2 -= '0';
383             } else if (ch2 >= 'a' && ch2 <= 'f') {
384                 ch2 -= 'a';
385                 ch2 += 10;
386             } else if (ch2 >= 'A' && ch2 <= 'F') {
387                 ch2 -= 'a';
388                 ch2 += 10;
389             } else {
390                 amfree(f);                              /* bad conversion */
391                 break;
392             }
393             f->bytes[i] = (ch1 << 4) | ch2;
394         }
395     }
396     return f;
397 }
398
399 #if defined(TEST)
400 int
401 main(argc, argv)
402     int                         argc;
403     char                        **argv;
404 {
405     am_feature_t                *f;
406     am_feature_t                *f1;
407     char                        *s;
408     char                        *s1;
409     int                         i;
410     int                         n;
411
412     f = am_init_feature_set();
413     if (f == NULL) {
414         fprintf(stderr, "cannot initialize feature set\n");
415         return 1;
416     }
417
418     s = am_feature_to_string(f);
419     printf("base features=%s\n", s);
420
421     f1 = am_string_to_feature(s);
422     s1 = am_feature_to_string(f1);
423     if (strcmp(s, s1) != 0) {
424         fprintf(stderr, "base feature -> string -> feature set mismatch\n");
425         fprintf(stderr, "conv features=%s\n", s);
426     }
427
428     amfree(s1);
429     amfree(s);
430
431     for (i = 1; i < argc; i++) {
432         if (argv[i][0] == '+') {
433             n = atoi(&argv[i][1]);
434             if (am_add_feature(f, (am_feature_e)n)) {
435                 printf("added feature number %d\n", n);
436             } else {
437                 printf("could not add feature number %d\n", n);
438             }
439         } else if (argv[i][0] == '-') {
440             n = atoi(&argv[i][1]);
441             if (am_remove_feature(f, (am_feature_e)n)) {
442                 printf("removed feature number %d\n", n);
443             } else {
444                 printf("could not remove feature number %d\n", n);
445             }
446         } else {
447             n = atoi(argv[i]);
448             if (am_has_feature(f, (am_feature_e)n)) {
449                 printf("feature %d is set\n", n);
450             } else {
451                 printf("feature %d is not set\n", n);
452             }
453         }
454     }
455
456     s = am_feature_to_string(f);
457     printf(" new features=%s\n", s);
458     amfree(s);
459
460     return 0;
461 }
462 #endif