40dfc0cd8d86c2b029af0924afa143ca0842df93
[debian/amanda] / server-src / getconf.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  * Author: James da Silva, Systems Design and Analysis Group
24  *                         Computer Science Department
25  *                         University of Maryland at College Park
26  */
27 /*
28  * $Id: getconf.c,v 1.26 2006/07/25 19:00:56 martinea Exp $
29  *
30  * a little wrapper to extract config variables for shell scripts
31  */
32 #include "amanda.h"
33 #include "version.h"
34 #include "genversion.h"
35 #include "conffile.h"
36
37 int main(int argc, char **argv);
38
39 /*
40  * HOSTNAME_INSTANCE may not be defined at this point.
41  * We define it locally if it is needed...
42  *
43  * If CLIENT_HOST_PRINCIPLE is defined as HOSTNAME_INSTANCE
44  * then local host is the client host principle.
45  */
46 #ifndef HOSTNAME_INSTANCE
47 #  define HOSTNAME_INSTANCE "localhost"
48 #endif
49
50 #ifndef KEYFILE
51 #  define KEYFILE "id_rsa"
52 #endif
53
54 static struct build_info {
55     char *symbol;
56     char *value;
57 } build_info[] = {
58     { "VERSION",                        "" },   /* must be [0] */
59     { "AMANDA_DEBUG_DAYS",              "" },   /* must be [1] */
60     { "TICKET_LIFETIME",                "" },   /* must be [2] */
61
62     { "bindir",                         bindir },
63     { "sbindir",                        sbindir },
64     { "libexecdir",                     libexecdir },
65     { "mandir",                         mandir },
66     { "AMANDA_TMPDIR",                  AMANDA_TMPDIR },
67     { "CONFIG_DIR",                     CONFIG_DIR },
68     { "MAILER",                         MAILER },
69     { "DEFAULT_SERVER",                 DEFAULT_SERVER },
70     { "DEFAULT_CONFIG",                 DEFAULT_CONFIG },
71     { "DEFAULT_TAPE_SERVER",            DEFAULT_TAPE_SERVER },
72 #ifdef DEFAULT_TAPE_DEVICE
73     { "DEFAULT_TAPE_DEVICE",            DEFAULT_TAPE_DEVICE },
74 #endif
75     { "CLIENT_LOGIN",                   CLIENT_LOGIN },
76
77     { "BUILT_DATE",
78 #if defined(BUILT_DATE)
79         BUILT_DATE
80 #else
81         NULL
82 #endif
83     },
84     { "BUILT_MACH",
85 #if defined(BUILT_MACH)
86         BUILT_MACH
87 #else
88         NULL
89 #endif
90     },
91     { "CC",
92 #if defined(CC)
93         CC
94 #else
95         NULL
96 #endif
97     },
98
99     { "AMANDA_DBGDIR",
100 #if defined(AMANDA_DBGDIR)
101         AMANDA_DBGDIR
102 #else
103         NULL
104 #endif
105     },
106     { "DEV_PREFIX",
107 #if defined(DEV_PREFIX)
108         DEV_PREFIX
109 #else
110         NULL
111 #endif
112     },
113     { "RDEV_PREFIX",
114 #if defined(RDEV_PREFIX)
115         RDEV_PREFIX
116 #else
117         NULL
118 #endif
119     },
120     { "DUMP",
121 #if defined(DUMP)
122         DUMP
123 #else
124         NULL
125 #endif
126     },
127     { "RESTORE",
128 #if defined(DUMP)
129         RESTORE
130 #else
131         NULL
132 #endif
133     },
134     { "VDUMP",
135 #if defined(VDUMP)
136         VDUMP
137 #else
138         NULL
139 #endif
140     },
141     { "VRESTORE",
142 #if defined(VDUMP)
143         VRESTORE
144 #else
145         NULL
146 #endif
147     },
148     { "XFSDUMP",
149 #if defined(XFSDUMP)
150         XFSDUMP
151 #else
152         NULL
153 #endif
154     },
155     { "XFSRESTORE",
156 #if defined(XFSDUMP)
157         XFSRESTORE
158 #else
159         NULL
160 #endif
161     },
162     { "VXDUMP",
163 #if defined(VXDUMP)
164         VXDUMP
165 #else
166         NULL
167 #endif
168     },
169     { "VXRESTORE",
170 #if defined(VXDUMP)
171         VXRESTORE
172 #else
173         NULL
174 #endif
175     },
176     { "SAMBA_CLIENT",
177 #if defined(SAMBA_CLIENT)
178         SAMBA_CLIENT
179 #else
180         NULL
181 #endif
182     },
183     { "GNUTAR",
184 #if defined(GNUTAR)
185         GNUTAR
186 #else
187         NULL
188 #endif
189     },
190     { "COMPRESS_PATH",
191 #if defined(COMPRESS_PATH)
192         COMPRESS_PATH
193 #else
194         NULL
195 #endif
196     },
197     { "UNCOMPRESS_PATH",
198 #if defined(UNCOMPRESS_PATH)
199         UNCOMPRESS_PATH
200 #else
201         NULL
202 #endif
203     },
204     { "listed_incr_dir",
205 #if defined(GNUTAR_LISTED_INCREMENTAL_DIR)
206         GNUTAR_LISTED_INCREMENTAL_DIR
207 #else
208         NULL
209 #endif
210     },
211     { "GNUTAR_LISTED_INCREMENTAL_DIR",
212 #if defined(GNUTAR_LISTED_INCREMENTAL_DIR)
213         GNUTAR_LISTED_INCREMENTAL_DIR
214 #else
215         NULL
216 #endif
217     },
218
219     { "AIX_BACKUP",
220 #if defined(AIX_BACKUP)
221         "1"
222 #else
223         NULL
224 #endif
225     },
226     { "AIX_TAPEIO",
227 #if defined(AIX_TAPEIO)
228         "1"
229 #else
230         NULL
231 #endif
232     },
233     { "DUMP_RETURNS_1",
234 #if defined(DUMP_RETURNS_1)
235         "1"
236 #else
237         NULL
238 #endif
239     },
240
241     { "LOCKING",
242 #if defined(USE_POSIX_FCNTL)
243         "POSIX_FCNTL"
244 #elif defined(USE_FLOCK)
245         "FLOCK"
246 #elif defined(USE_LOCKF)
247         "LOCKF"
248 #elif defined(USE_LNLOCK)
249         "LNLOCK"
250 #else
251         "NONE"
252 #endif
253     },
254
255     { "STATFS_BSD",
256 #if defined(STATFS_BSD)
257         "1"
258 #else
259         NULL
260 #endif
261     },
262     { "STATFS_OSF1",
263 #if defined(STATFS_OSF1)
264         "1"
265 #else
266         NULL
267 #endif
268     },
269     { "STATFS_ULTRIX",
270 #if defined(STATFS_ULTRIX)
271         "1"
272 #else
273         NULL
274 #endif
275     },
276     { "ASSERTIONS",
277 #if defined(ASSERTIONS)
278         "1"
279 #else
280         NULL
281 #endif
282     },
283     { "DEBUG_CODE",
284 #if defined(DEBUG_CODE)
285         "1"
286 #else
287         NULL
288 #endif
289     },
290     { "BSD_SECURITY",
291 #if defined(BSD_SECURITY)
292         "1"
293 #else
294         NULL
295 #endif
296     },
297     { "USE_AMANDAHOSTS",
298 #if defined(USE_AMANDAHOSTS)
299         "1"
300 #else
301         NULL
302 #endif
303     },
304     { "USE_RUNDUMP",
305 #if defined(USE_RUNDUMP)
306         "1"
307 #else
308         NULL
309 #endif
310     },
311     { "FORCE_USERID",
312 #if defined(FORCE_USERID)
313         "1"
314 #else
315         NULL
316 #endif
317     },
318     { "USE_VERSION_SUFFIXES",
319 #if defined(USE_VERSION_SUFFIXES)
320         "1"
321 #else
322         NULL
323 #endif
324     },
325     { "HAVE_GZIP",
326 #if defined(HAVE_GZIP)
327         "1"
328 #else
329         NULL
330 #endif
331     },
332
333     { "KRB4_SECURITY",
334 #if defined(KRB4_SECURITY)
335         "1"
336 #else
337         NULL
338 #endif
339     },
340     { "SERVER_HOST_PRINCIPLE",
341 #if defined(KRB4_SECURITY)
342         SERVER_HOST_PRINCIPLE
343 #else
344         NULL
345 #endif
346     },
347     { "SERVER_HOST_INSTANCE",
348 #if defined(KRB4_SECURITY)
349         SERVER_HOST_INSTANCE
350 #else
351         NULL
352 #endif
353     },
354     { "SERVER_HOST_KEY_FILE",
355 #if defined(KRB4_SECURITY)
356         SERVER_HOST_KEY_FILE
357 #else
358         NULL
359 #endif
360     },
361     { "CLIENT_HOST_PRINCIPLE",
362 #if defined(KRB4_SECURITY)
363         CLIENT_HOST_PRINCIPLE
364 #else
365         NULL
366 #endif
367     },
368     { "CLIENT_HOST_INSTANCE",
369 #if defined(KRB4_SECURITY)
370         CLIENT_HOST_INSTANCE
371 #else
372         NULL
373 #endif
374     },
375     { "CLIENT_HOST_KEY_FILE",
376 #if defined(KRB4_SECURITY)
377         CLIENT_HOST_KEY_FILE
378 #else
379         NULL
380 #endif
381     },
382
383     { "COMPRESS_SUFFIX",
384 #if defined(COMPRESS_SUFFIX)
385         COMPRESS_SUFFIX
386 #else
387         NULL
388 #endif
389     },
390     { "COMPRESS_FAST_OPT",
391 #if defined(COMPRESS_FAST_OPT)
392         COMPRESS_FAST_OPT
393 #else
394         NULL
395 #endif
396     },
397     { "COMPRESS_BEST_OPT",
398 #if defined(COMPRESS_BEST_OPT)
399         COMPRESS_BEST_OPT
400 #else
401         NULL
402 #endif
403     },
404     { "UNCOMPRESS_OPT",
405 #if defined(UNCOMPRESS_OPT)
406         UNCOMPRESS_OPT
407 #else
408         NULL
409 #endif
410     },
411
412     { NULL,                     NULL }
413 };
414
415 int
416 main(
417     int         argc,
418     char **     argv)
419 {
420     char *result;
421     unsigned long malloc_hist_1, malloc_size_1;
422     unsigned long malloc_hist_2, malloc_size_2;
423     char *pgm;
424     char *conffile;
425     char *parmname;
426     int i;
427     char number[NUM_STR_SIZE];
428     int    new_argc,   my_argc;
429     char **new_argv, **my_argv;
430
431     safe_fd(-1, 0);
432
433     malloc_size_1 = malloc_inuse(&malloc_hist_1);
434
435     parse_server_conf(argc, argv, &new_argc, &new_argv);
436     my_argc = new_argc;
437     my_argv = new_argv;
438
439     if((pgm = strrchr(my_argv[0], '/')) == NULL) {
440         pgm = my_argv[0];
441     } else {
442         pgm++;
443     }
444     set_pname(pgm);
445
446     /* Don't die when child closes pipe */
447     signal(SIGPIPE, SIG_IGN);
448
449     if(my_argc < 2) {
450         fprintf(stderr, "Usage: %s [config] <parmname> [-o configoption]*\n", pgm);
451         exit(1);
452     }
453
454     if (my_argc > 2) {
455         config_name = stralloc(my_argv[1]);
456         config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
457         parmname = my_argv[2];
458     } else {
459         char my_cwd[STR_SIZE];
460
461         if (getcwd(my_cwd, SIZEOF(my_cwd)) == NULL) {
462             error("cannot determine current working directory");
463             /*NOTREACHED*/
464         }
465         config_dir = stralloc2(my_cwd, "/");
466         if ((config_name = strrchr(my_cwd, '/')) != NULL) {
467             config_name = stralloc(config_name + 1);
468         }
469         parmname = my_argv[1];
470     }
471
472     safe_cd();
473
474     /*
475      * Fill in the build values that need runtime help.
476      */
477     build_info[0].value = stralloc(version());
478 #if defined(AMANDA_DEBUG_DAYS)
479     i = AMANDA_DEBUG_DAYS;
480 #else
481     i = -1;
482 #endif
483     snprintf(number, SIZEOF(number), "%ld", (long)i);
484     build_info[1].value = stralloc(number);
485 #if defined(KRB4_SECURITY)
486     i = TICKET_LIFETIME;
487 #else
488     i = -1;
489 #endif
490     snprintf(number, SIZEOF(number), "%ld", (long)i);
491     build_info[2].value = stralloc(number);
492
493 #undef p
494 #define p       "build."
495
496     if(strncmp(parmname, p, SIZEOF(p) - 1) == 0) {
497         char *s;
498         char *t;
499
500         t = stralloc(parmname + SIZEOF(p) - 1);
501         for(i = 0; (s = build_info[i].symbol) != NULL; i++) {
502             if(strcasecmp(s, t) == 0) {
503                 break;
504             }
505         }
506         if(s == NULL) {
507             result = NULL;
508         } else {
509             result = build_info[i].value;
510             result = stralloc(result ? result : "");
511         }
512
513 #undef p
514 #define p       "dbopen."
515
516     } else if(strncmp(parmname, p, SIZEOF(p) - 1) == 0) {
517         char *pname;
518         char *dbname;
519
520         if((pname = strrchr(parmname + SIZEOF(p) - 1, '/')) == NULL) {
521             pname = parmname + SIZEOF(p) - 1;
522         } else {
523             pname++;
524         }
525         set_pname(pname);
526         dbopen(DBG_SUBDIR_SERVER);
527         if((dbname = dbfn()) == NULL) {
528             result = stralloc("/dev/null");
529         } else {
530             result = stralloc(dbname);
531         }
532         /*
533          * Note that we deliberately do *not* call dbclose to prevent
534          * the end line from being added to the file.
535          */
536
537 #undef p
538 #define p       "dbclose."
539
540     } else if(strncmp(parmname, p, SIZEOF(p) - 1) == 0) {
541         char *t;
542         char *pname;
543         char *dbname;
544
545         t = stralloc(parmname + SIZEOF(p) - 1);
546         if((dbname = strchr(t, ':')) == NULL) {
547             error("cannot parse %s", parmname);
548             /*NOTREACHED*/
549         }
550         *dbname++ = '\0';
551         if((pname = strrchr(t, '/')) == NULL) {
552             pname = t;
553         } else {
554             pname++;
555         }
556         fflush(stderr);
557         set_pname(pname);
558         dbreopen(dbname, NULL);
559         dbclose();
560         result = stralloc(dbname);
561         amfree(t);
562
563     } else {
564         conffile = stralloc2(config_dir, CONFFILE_NAME);
565         if(read_conffile(conffile)) {
566             error("errors processing config file \"%s\"", conffile);
567             /*NOTREACHED*/
568         }
569         amfree(conffile);
570         dbrename(config_name, DBG_SUBDIR_SERVER);
571         report_bad_conf_arg();
572         result = getconf_byname(parmname);
573     }
574
575     if (result == NULL) {
576         fprintf(stderr, "%s: no such parameter \"%s\"\n",
577                 get_pname(), parmname);
578         fflush(stderr);
579     } else {
580         puts(result);
581     }
582
583     free_new_argv(new_argc, new_argv);
584     free_server_config();
585     amfree(result);
586     amfree(config_dir);
587     amfree(config_name);
588     for(i = 0; i < 3; i++) {
589         amfree(build_info[i].value);
590     }
591
592     malloc_size_2 = malloc_inuse(&malloc_hist_2);
593
594     if(malloc_size_1 != malloc_size_2) {
595         malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);
596     }
597
598     return 0;
599 }