4b4ebee789f8a879aef0e755c2e8f82573f1f2ab
[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.2.3 2007/01/16 12:36:47 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     int asklist;
428     char number[NUM_STR_SIZE];
429     int    new_argc,   my_argc;
430     char **new_argv, **my_argv;
431     int myarg;
432
433     safe_fd(-1, 0);
434
435     malloc_size_1 = malloc_inuse(&malloc_hist_1);
436
437     parse_server_conf(argc, argv, &new_argc, &new_argv);
438     my_argc = new_argc;
439     my_argv = new_argv;
440
441     if((pgm = strrchr(my_argv[0], '/')) == NULL) {
442         pgm = my_argv[0];
443     } else {
444         pgm++;
445     }
446     set_pname(pgm);
447
448     /* Don't die when child closes pipe */
449     signal(SIGPIPE, SIG_IGN);
450
451     if(my_argc < 2) {
452         fprintf(stderr, "Usage: %s [config] [--list] <parmname> [-o configoption]*\n", pgm);
453         exit(1);
454     }
455
456     asklist = 0;
457     myarg = 1;
458     if (strcmp(my_argv[1],"--list") == 0) {
459         asklist = 1;
460         myarg = 2;
461     } else if (my_argc > 2 && strcmp(my_argv[2],"--list") == 0) {
462         asklist = 1;
463         myarg = 3;
464     } else if (my_argc > 2) {
465         myarg = 2;
466     }
467
468     if (myarg > asklist+1) {
469         config_name = stralloc(my_argv[1]);
470         config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
471     } else {
472         char my_cwd[STR_SIZE];
473
474         if (getcwd(my_cwd, SIZEOF(my_cwd)) == NULL) {
475             error("cannot determine current working directory");
476             /*NOTREACHED*/
477         }
478         config_dir = stralloc2(my_cwd, "/");
479         if ((config_name = strrchr(my_cwd, '/')) != NULL) {
480             config_name = stralloc(config_name + 1);
481         }
482     }
483     if (myarg >= my_argc) {
484         error("Must specify a parameter");
485     }
486     parmname = my_argv[myarg];
487
488     safe_cd();
489
490     /*
491      * Fill in the build values that need runtime help.
492      */
493     build_info[0].value = stralloc(version());
494 #if defined(AMANDA_DEBUG_DAYS)
495     i = AMANDA_DEBUG_DAYS;
496 #else
497     i = -1;
498 #endif
499     snprintf(number, SIZEOF(number), "%ld", (long)i);
500     build_info[1].value = stralloc(number);
501 #if defined(KRB4_SECURITY)
502     i = TICKET_LIFETIME;
503 #else
504     i = -1;
505 #endif
506     snprintf(number, SIZEOF(number), "%ld", (long)i);
507     build_info[2].value = stralloc(number);
508
509 #undef p
510 #define p       "build."
511
512     if(strncmp(parmname, p, SIZEOF(p) - 1) == 0) {
513         char *s;
514         char *t;
515
516         t = stralloc(parmname + SIZEOF(p) - 1);
517         for(i = 0; (s = build_info[i].symbol) != NULL; i++) {
518             if(strcasecmp(s, t) == 0) {
519                 break;
520             }
521         }
522         if(s == NULL) {
523             result = NULL;
524         } else {
525             result = build_info[i].value;
526             result = stralloc(result ? result : "");
527         }
528
529 #undef p
530 #define p       "dbopen."
531
532     } else if(strncmp(parmname, p, SIZEOF(p) - 1) == 0) {
533         char *pname;
534         char *dbname;
535
536         if((pname = strrchr(parmname + SIZEOF(p) - 1, '/')) == NULL) {
537             pname = parmname + SIZEOF(p) - 1;
538         } else {
539             pname++;
540         }
541         set_pname(pname);
542         dbopen(DBG_SUBDIR_SERVER);
543         if((dbname = dbfn()) == NULL) {
544             result = stralloc("/dev/null");
545         } else {
546             result = stralloc(dbname);
547         }
548         /*
549          * Note that we deliberately do *not* call dbclose to prevent
550          * the end line from being added to the file.
551          */
552
553 #undef p
554 #define p       "dbclose."
555
556     } else if(strncmp(parmname, p, SIZEOF(p) - 1) == 0) {
557         char *t;
558         char *pname;
559         char *dbname;
560
561         t = stralloc(parmname + SIZEOF(p) - 1);
562         if((dbname = strchr(t, ':')) == NULL) {
563             error("cannot parse %s", parmname);
564             /*NOTREACHED*/
565         }
566         *dbname++ = '\0';
567         if((pname = strrchr(t, '/')) == NULL) {
568             pname = t;
569         } else {
570             pname++;
571         }
572         fflush(stderr);
573         set_pname(pname);
574         dbreopen(dbname, NULL);
575         dbclose();
576         result = stralloc(dbname);
577         amfree(t);
578
579     } else {
580         conffile = stralloc2(config_dir, CONFFILE_NAME);
581         if(read_conffile(conffile)) {
582             error("errors processing config file \"%s\"", conffile);
583             /*NOTREACHED*/
584         }
585         amfree(conffile);
586         dbrename(config_name, DBG_SUBDIR_SERVER);
587         report_bad_conf_arg();
588         if (asklist) {
589             result = getconf_list(parmname);
590         } else {
591             result = getconf_byname(parmname);
592         }
593     }
594
595     if (result == NULL) {
596         fprintf(stderr, "%s: no such parameter \"%s\"\n",
597                 get_pname(), parmname);
598         fflush(stderr);
599     } else {
600         if (asklist)
601             fputs(result, stdout); /* don't add a '\n' */
602         else
603             puts(result); /* add a '\n' */
604     }
605
606     free_new_argv(new_argc, new_argv);
607     free_server_config();
608     amfree(result);
609     amfree(config_dir);
610     amfree(config_name);
611     for(i = 0; i < 3; i++) {
612         amfree(build_info[i].value);
613     }
614
615     malloc_size_2 = malloc_inuse(&malloc_hist_2);
616
617     if(malloc_size_1 != malloc_size_2) {
618         malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);
619     }
620
621     return 0;
622 }