Imported Upstream version 2.6.0p2
[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_PRINCIPAL is defined as HOSTNAME_INSTANCE
44  * then local host is the client host principal.
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     { "amlibexecdir",                   amlibexecdir },
66     { "mandir",                         mandir },
67     { "AMANDA_TMPDIR",                  AMANDA_TMPDIR },
68     { "CONFIG_DIR",                     CONFIG_DIR },
69 #ifdef MAILER
70     { "MAILER",                         MAILER },
71 #endif
72     { "DEFAULT_SERVER",                 DEFAULT_SERVER },
73     { "DEFAULT_CONFIG",                 DEFAULT_CONFIG },
74     { "DEFAULT_TAPE_SERVER",            DEFAULT_TAPE_SERVER },
75 #ifdef DEFAULT_TAPE_DEVICE
76     { "DEFAULT_TAPE_DEVICE",            DEFAULT_TAPE_DEVICE },
77 #endif
78     { "CLIENT_LOGIN",                   CLIENT_LOGIN },
79
80     { "BUILT_DATE",
81 #if defined(BUILT_DATE)
82         BUILT_DATE
83 #else
84         NULL
85 #endif
86     },
87     { "BUILT_MACH",
88 #if defined(BUILT_MACH)
89         BUILT_MACH
90 #else
91         NULL
92 #endif
93     },
94     { "CC",
95 #if defined(CC)
96         CC
97 #else
98         NULL
99 #endif
100     },
101
102     { "AMANDA_DBGDIR",
103 #if defined(AMANDA_DBGDIR)
104         AMANDA_DBGDIR
105 #else
106         NULL
107 #endif
108     },
109     { "DEV_PREFIX",
110 #if defined(DEV_PREFIX)
111         DEV_PREFIX
112 #else
113         NULL
114 #endif
115     },
116     { "RDEV_PREFIX",
117 #if defined(RDEV_PREFIX)
118         RDEV_PREFIX
119 #else
120         NULL
121 #endif
122     },
123     { "DUMP",
124 #if defined(DUMP)
125         DUMP
126 #else
127         NULL
128 #endif
129     },
130     { "RESTORE",
131 #if defined(DUMP)
132         RESTORE
133 #else
134         NULL
135 #endif
136     },
137     { "VDUMP",
138 #if defined(VDUMP)
139         VDUMP
140 #else
141         NULL
142 #endif
143     },
144     { "VRESTORE",
145 #if defined(VDUMP)
146         VRESTORE
147 #else
148         NULL
149 #endif
150     },
151     { "XFSDUMP",
152 #if defined(XFSDUMP)
153         XFSDUMP
154 #else
155         NULL
156 #endif
157     },
158     { "XFSRESTORE",
159 #if defined(XFSDUMP)
160         XFSRESTORE
161 #else
162         NULL
163 #endif
164     },
165     { "VXDUMP",
166 #if defined(VXDUMP)
167         VXDUMP
168 #else
169         NULL
170 #endif
171     },
172     { "VXRESTORE",
173 #if defined(VXDUMP)
174         VXRESTORE
175 #else
176         NULL
177 #endif
178     },
179     { "SAMBA_CLIENT",
180 #if defined(SAMBA_CLIENT)
181         SAMBA_CLIENT
182 #else
183         NULL
184 #endif
185     },
186     { "GNUTAR",
187 #if defined(GNUTAR)
188         GNUTAR
189 #else
190         NULL
191 #endif
192     },
193     { "COMPRESS_PATH",
194 #if defined(COMPRESS_PATH)
195         COMPRESS_PATH
196 #else
197         NULL
198 #endif
199     },
200     { "UNCOMPRESS_PATH",
201 #if defined(UNCOMPRESS_PATH)
202         UNCOMPRESS_PATH
203 #else
204         NULL
205 #endif
206     },
207     { "listed_incr_dir",
208 #if defined(GNUTAR_LISTED_INCREMENTAL_DIR)
209         GNUTAR_LISTED_INCREMENTAL_DIR
210 #else
211         NULL
212 #endif
213     },
214     { "GNUTAR_LISTED_INCREMENTAL_DIR",
215 #if defined(GNUTAR_LISTED_INCREMENTAL_DIR)
216         GNUTAR_LISTED_INCREMENTAL_DIR
217 #else
218         NULL
219 #endif
220     },
221
222     { "AIX_BACKUP",
223 #if defined(AIX_BACKUP)
224         "1"
225 #else
226         NULL
227 #endif
228     },
229     { "DUMP_RETURNS_1",
230 #if defined(DUMP_RETURNS_1)
231         "1"
232 #else
233         NULL
234 #endif
235     },
236
237     { "LOCKING",
238 #if defined(USE_POSIX_FCNTL)
239         "POSIX_FCNTL"
240 #elif defined(USE_FLOCK)
241         "FLOCK"
242 #elif defined(USE_LOCKF)
243         "LOCKF"
244 #elif defined(USE_LNLOCK)
245         "LNLOCK"
246 #else
247         "NONE"
248 #endif
249     },
250
251     { "STATFS_BSD",
252 #if defined(STATFS_BSD)
253         "1"
254 #else
255         NULL
256 #endif
257     },
258     { "STATFS_OSF1",
259 #if defined(STATFS_OSF1)
260         "1"
261 #else
262         NULL
263 #endif
264     },
265     { "STATFS_ULTRIX",
266 #if defined(STATFS_ULTRIX)
267         "1"
268 #else
269         NULL
270 #endif
271     },
272     { "ASSERTIONS",
273 #if defined(ASSERTIONS)
274         "1"
275 #else
276         NULL
277 #endif
278     },
279     { "BSD_SECURITY",
280 #if defined(BSD_SECURITY)
281         "1"
282 #else
283         NULL
284 #endif
285     },
286     { "USE_AMANDAHOSTS",
287 #if defined(USE_AMANDAHOSTS)
288         "1"
289 #else
290         NULL
291 #endif
292     },
293     { "USE_RUNDUMP",
294 #if defined(USE_RUNDUMP)
295         "1"
296 #else
297         NULL
298 #endif
299     },
300     { "CHECK_USERID",
301 #if defined(CHECK_USERID)
302         "1"
303 #else
304         NULL
305 #endif
306     },
307     { "USE_VERSION_SUFFIXES",
308 #if defined(USE_VERSION_SUFFIXES)
309         "1"
310 #else
311         NULL
312 #endif
313     },
314     { "HAVE_GZIP",
315 #if defined(HAVE_GZIP)
316         "1"
317 #else
318         NULL
319 #endif
320     },
321
322     { "KRB4_SECURITY",
323 #if defined(KRB4_SECURITY)
324         "1"
325 #else
326         NULL
327 #endif
328     },
329     { "SERVER_HOST_PRINCIPAL",
330 #if defined(KRB4_SECURITY)
331         SERVER_HOST_PRINCIPAL
332 #else
333         NULL
334 #endif
335     },
336     { "SERVER_HOST_PRINCIPLE", /* backward-compatibility (spelling error) */
337 #if defined(KRB4_SECURITY)
338         SERVER_HOST_PRINCIPAL
339 #else
340         NULL
341 #endif
342     },
343     { "SERVER_HOST_INSTANCE",
344 #if defined(KRB4_SECURITY)
345         SERVER_HOST_INSTANCE
346 #else
347         NULL
348 #endif
349     },
350     { "SERVER_HOST_KEY_FILE",
351 #if defined(KRB4_SECURITY)
352         SERVER_HOST_KEY_FILE
353 #else
354         NULL
355 #endif
356     },
357     { "CLIENT_HOST_PRINCIPAL",
358 #if defined(KRB4_SECURITY)
359         CLIENT_HOST_PRINCIPAL
360 #else
361         NULL
362 #endif
363     },
364     { "CLIENT_HOST_PRINCIPLE", /* backward-compatibility (spelling error) */
365 #if defined(KRB4_SECURITY)
366         CLIENT_HOST_PRINCIPAL
367 #else
368         NULL
369 #endif
370     },
371     { "CLIENT_HOST_INSTANCE",
372 #if defined(KRB4_SECURITY)
373         CLIENT_HOST_INSTANCE
374 #else
375         NULL
376 #endif
377     },
378     { "CLIENT_HOST_KEY_FILE",
379 #if defined(KRB4_SECURITY)
380         CLIENT_HOST_KEY_FILE
381 #else
382         NULL
383 #endif
384     },
385
386     { "COMPRESS_SUFFIX",
387 #if defined(COMPRESS_SUFFIX)
388         COMPRESS_SUFFIX
389 #else
390         NULL
391 #endif
392     },
393     { "COMPRESS_FAST_OPT",
394 #if defined(COMPRESS_FAST_OPT)
395         COMPRESS_FAST_OPT
396 #else
397         NULL
398 #endif
399     },
400     { "COMPRESS_BEST_OPT",
401 #if defined(COMPRESS_BEST_OPT)
402         COMPRESS_BEST_OPT
403 #else
404         NULL
405 #endif
406     },
407     { "UNCOMPRESS_OPT",
408 #if defined(UNCOMPRESS_OPT)
409         UNCOMPRESS_OPT
410 #else
411         NULL
412 #endif
413     },
414
415     { NULL,                     NULL }
416 };
417
418 int
419 main(
420     int         argc,
421     char **     argv)
422 {
423     char *result = NULL;
424     char *pgm = NULL;
425     char *parmname = NULL;
426     int i;
427     int asklist;
428     char number[NUM_STR_SIZE];
429     int myarg;
430     config_overwrites_t *cfg_ovr = NULL;
431     char *cfg_opt = NULL;
432     gboolean cfg_ok;
433
434     /*
435      * Configure program for internationalization:
436      *   1) Only set the message locale for now.
437      *   2) Set textdomain for all amanda related programs to "amanda"
438      *      We don't want to be forced to support dozens of message catalogs.
439      */  
440     setlocale(LC_MESSAGES, "C");
441     textdomain("amanda"); 
442
443     safe_fd(-1, 0);
444
445     cfg_ovr = extract_commandline_config_overwrites(&argc, &argv);
446
447     if((pgm = strrchr(argv[0], '/')) == NULL) {
448         pgm = argv[0];
449     } else {
450         pgm++;
451     }
452     set_pname(pgm);
453
454     /* Don't die when child closes pipe */
455     signal(SIGPIPE, SIG_IGN);
456
457     if(argc < 2) {
458         g_fprintf(stderr, _("Usage: %s [config] [--list] <parmname> [-o configoption]*\n"), pgm);
459         exit(1);
460     }
461
462     asklist = 0;
463     myarg = 1;
464     if (strcmp(argv[1],"--list") == 0) {
465         asklist = 1;
466         myarg = 2;
467     } else if (argc > 2 && strcmp(argv[2],"--list") == 0) {
468         asklist = 1;
469         myarg = 3;
470     } else if (argc > 2) {
471         myarg = 2;
472     }
473
474     if (myarg > asklist + 1)
475         cfg_opt = argv[1];
476
477     if (myarg >= argc) {
478         error(_("Must specify a parameter"));
479     }
480     parmname = argv[myarg];
481
482     /* do the config_init() now, although the result isn't checked until the end,
483      * when we try to look up config parameters */
484     cfg_ok = config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, cfg_opt);
485     if (cfg_ok) apply_config_overwrites(cfg_ovr);
486
487     safe_cd(); /* call this *after* config_init() */
488
489     /* Note that we dont use check_running_as(..) here, because we may not have a configuration
490      * (e.g., when we're examining build parameters).  If folks run this as the wrong user, that's
491      * their own problem. */
492
493     /*
494      * Fill in the build values that need runtime help.
495      */
496     build_info[0].value = stralloc(version());
497 #if defined(AMANDA_DEBUG_DAYS)
498     i = AMANDA_DEBUG_DAYS;
499 #else
500     i = -1;
501 #endif
502     g_snprintf(number, SIZEOF(number), "%ld", (long)i);
503     build_info[1].value = stralloc(number);
504 #if defined(KRB4_SECURITY)
505     i = TICKET_LIFETIME;
506 #else
507     i = -1;
508 #endif
509     g_snprintf(number, SIZEOF(number), "%ld", (long)i);
510     build_info[2].value = stralloc(number);
511
512 #undef p
513 #define p       "build."
514
515     if(strncmp(parmname, p, SIZEOF(p) - 1) == 0) {
516         char *s;
517         char *t;
518
519         t = stralloc(parmname + SIZEOF(p) - 1);
520         for(i = 0; (s = build_info[i].symbol) != NULL; i++) {
521             if(strcasecmp(s, t) == 0) {
522                 break;
523             }
524         }
525         if(s == NULL) {
526             result = NULL;
527         } else {
528             result = build_info[i].value;
529             result = stralloc(result ? result : "");
530         }
531
532 #undef p
533 #define p       "dbopen."
534
535     } else if(strncmp(parmname, p, SIZEOF(p) - 1) == 0) {
536         char *pname;
537         char *dbname;
538
539         if((pname = strrchr(parmname + SIZEOF(p) - 1, '/')) == NULL) {
540             pname = parmname + SIZEOF(p) - 1;
541         } else {
542             pname++;
543         }
544         set_pname(pname);
545         dbopen(DBG_SUBDIR_SERVER);
546         if((dbname = dbfn()) == NULL) {
547             result = stralloc("/dev/null");
548         } else {
549             result = stralloc(dbname);
550         }
551         /*
552          * Note that we deliberately do *not* call dbclose to prevent
553          * the end line from being added to the file.
554          */
555
556 #undef p
557 #define p       "dbclose."
558
559     } else if(strncmp(parmname, p, SIZEOF(p) - 1) == 0) {
560         char *t;
561         char *pname;
562         char *dbname;
563
564         t = stralloc(parmname + SIZEOF(p) - 1);
565         if((dbname = strchr(t, ':')) == NULL) {
566             error(_("cannot parse %s"), parmname);
567             /*NOTREACHED*/
568         }
569         *dbname++ = '\0';
570         if((pname = strrchr(t, '/')) == NULL) {
571             pname = t;
572         } else {
573             pname++;
574         }
575         fflush(stderr);
576         set_pname(pname);
577         dbreopen(dbname, NULL);
578         dbclose();
579         result = stralloc(dbname);
580         amfree(t);
581
582     } else {
583         /* *now* we check the result of config_init */
584         if (!cfg_ok) {
585             if (cfg_opt) {
586                 error(_("errors processing conf file \"%s\""), cfg_opt);
587                 /*NOTREACHED*/
588             } else {
589                 error(_("errors processing conf file in current directory."));
590                 /*NOTREACHED*/
591             }
592         }
593
594         dbrename(config_name, DBG_SUBDIR_SERVER);
595         if (asklist) {
596             GSList *list = getconf_list(parmname);
597             GSList *iter;
598             result = stralloc("");
599
600             for (iter = list; iter != NULL; iter = iter->next) {
601                 result = newvstralloc(result, result, iter->data, "\n", NULL);
602             }
603
604             g_slist_free(list);
605         } else {
606             val_t *val = getconf_byname(parmname);
607             if (val) {
608                 char **dispstrs = val_t_display_strs(val, FALSE);
609                 result = g_strjoinv("\n", dispstrs);
610                 g_strfreev(dispstrs);
611             }
612         }
613     }
614
615     if (result == NULL) {
616         g_fprintf(stderr, _("%s: no such parameter \"%s\"\n"),
617                 get_pname(), parmname);
618         fflush(stderr);
619     } else {
620         if (asklist)
621             fputs(result, stdout); /* don't add a '\n' */
622         else
623             puts(result); /* add a '\n' */
624     }
625
626     amfree(result);
627     for(i = 0; i < 3; i++) {
628         amfree(build_info[i].value);
629     }
630
631     return 0;
632 }