27d02dd660800c97887df948d54a2354c75920aa
[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     { "AIX_TAPEIO",
230 #if defined(AIX_TAPEIO)
231         "1"
232 #else
233         NULL
234 #endif
235     },
236     { "DUMP_RETURNS_1",
237 #if defined(DUMP_RETURNS_1)
238         "1"
239 #else
240         NULL
241 #endif
242     },
243
244     { "LOCKING",
245 #if defined(USE_POSIX_FCNTL)
246         "POSIX_FCNTL"
247 #elif defined(USE_FLOCK)
248         "FLOCK"
249 #elif defined(USE_LOCKF)
250         "LOCKF"
251 #elif defined(USE_LNLOCK)
252         "LNLOCK"
253 #else
254         "NONE"
255 #endif
256     },
257
258     { "STATFS_BSD",
259 #if defined(STATFS_BSD)
260         "1"
261 #else
262         NULL
263 #endif
264     },
265     { "STATFS_OSF1",
266 #if defined(STATFS_OSF1)
267         "1"
268 #else
269         NULL
270 #endif
271     },
272     { "STATFS_ULTRIX",
273 #if defined(STATFS_ULTRIX)
274         "1"
275 #else
276         NULL
277 #endif
278     },
279     { "ASSERTIONS",
280 #if defined(ASSERTIONS)
281         "1"
282 #else
283         NULL
284 #endif
285     },
286     { "DEBUG_CODE",
287 #if defined(DEBUG_CODE)
288         "1"
289 #else
290         NULL
291 #endif
292     },
293     { "BSD_SECURITY",
294 #if defined(BSD_SECURITY)
295         "1"
296 #else
297         NULL
298 #endif
299     },
300     { "USE_AMANDAHOSTS",
301 #if defined(USE_AMANDAHOSTS)
302         "1"
303 #else
304         NULL
305 #endif
306     },
307     { "USE_RUNDUMP",
308 #if defined(USE_RUNDUMP)
309         "1"
310 #else
311         NULL
312 #endif
313     },
314     { "CHECK_USERID",
315 #if defined(CHECK_USERID)
316         "1"
317 #else
318         NULL
319 #endif
320     },
321     { "USE_VERSION_SUFFIXES",
322 #if defined(USE_VERSION_SUFFIXES)
323         "1"
324 #else
325         NULL
326 #endif
327     },
328     { "HAVE_GZIP",
329 #if defined(HAVE_GZIP)
330         "1"
331 #else
332         NULL
333 #endif
334     },
335
336     { "KRB4_SECURITY",
337 #if defined(KRB4_SECURITY)
338         "1"
339 #else
340         NULL
341 #endif
342     },
343     { "SERVER_HOST_PRINCIPAL",
344 #if defined(KRB4_SECURITY)
345         SERVER_HOST_PRINCIPAL
346 #else
347         NULL
348 #endif
349     },
350     { "SERVER_HOST_PRINCIPLE", /* backward-compatibility (spelling error) */
351 #if defined(KRB4_SECURITY)
352         SERVER_HOST_PRINCIPAL
353 #else
354         NULL
355 #endif
356     },
357     { "SERVER_HOST_INSTANCE",
358 #if defined(KRB4_SECURITY)
359         SERVER_HOST_INSTANCE
360 #else
361         NULL
362 #endif
363     },
364     { "SERVER_HOST_KEY_FILE",
365 #if defined(KRB4_SECURITY)
366         SERVER_HOST_KEY_FILE
367 #else
368         NULL
369 #endif
370     },
371     { "CLIENT_HOST_PRINCIPAL",
372 #if defined(KRB4_SECURITY)
373         CLIENT_HOST_PRINCIPAL
374 #else
375         NULL
376 #endif
377     },
378     { "CLIENT_HOST_PRINCIPLE", /* backward-compatibility (spelling error) */
379 #if defined(KRB4_SECURITY)
380         CLIENT_HOST_PRINCIPAL
381 #else
382         NULL
383 #endif
384     },
385     { "CLIENT_HOST_INSTANCE",
386 #if defined(KRB4_SECURITY)
387         CLIENT_HOST_INSTANCE
388 #else
389         NULL
390 #endif
391     },
392     { "CLIENT_HOST_KEY_FILE",
393 #if defined(KRB4_SECURITY)
394         CLIENT_HOST_KEY_FILE
395 #else
396         NULL
397 #endif
398     },
399
400     { "COMPRESS_SUFFIX",
401 #if defined(COMPRESS_SUFFIX)
402         COMPRESS_SUFFIX
403 #else
404         NULL
405 #endif
406     },
407     { "COMPRESS_FAST_OPT",
408 #if defined(COMPRESS_FAST_OPT)
409         COMPRESS_FAST_OPT
410 #else
411         NULL
412 #endif
413     },
414     { "COMPRESS_BEST_OPT",
415 #if defined(COMPRESS_BEST_OPT)
416         COMPRESS_BEST_OPT
417 #else
418         NULL
419 #endif
420     },
421     { "UNCOMPRESS_OPT",
422 #if defined(UNCOMPRESS_OPT)
423         UNCOMPRESS_OPT
424 #else
425         NULL
426 #endif
427     },
428
429     { NULL,                     NULL }
430 };
431
432 int
433 main(
434     int         argc,
435     char **     argv)
436 {
437     char *result = NULL;
438     char *pgm = NULL;
439     char *parmname = NULL;
440     int i;
441     int asklist;
442     char number[NUM_STR_SIZE];
443     int myarg;
444     config_overwrites_t *cfg_ovr = NULL;
445     char *cfg_opt = NULL;
446     gboolean cfg_ok;
447
448     /*
449      * Configure program for internationalization:
450      *   1) Only set the message locale for now.
451      *   2) Set textdomain for all amanda related programs to "amanda"
452      *      We don't want to be forced to support dozens of message catalogs.
453      */  
454     setlocale(LC_MESSAGES, "C");
455     textdomain("amanda"); 
456
457     safe_fd(-1, 0);
458
459     cfg_ovr = extract_commandline_config_overwrites(&argc, &argv);
460
461     if((pgm = strrchr(my_argv[0], '/')) == NULL) {
462         pgm = my_argv[0];
463     } else {
464         pgm++;
465     }
466     set_pname(pgm);
467
468     /* Don't die when child closes pipe */
469     signal(SIGPIPE, SIG_IGN);
470
471     if(argc < 2) {
472         g_fprintf(stderr, _("Usage: %s [config] [--list] <parmname> [-o configoption]*\n"), pgm);
473         exit(1);
474     }
475
476     asklist = 0;
477     myarg = 1;
478     if (strcmp(argv[1],"--list") == 0) {
479         asklist = 1;
480         myarg = 2;
481     } else if (argc > 2 && strcmp(argv[2],"--list") == 0) {
482         asklist = 1;
483         myarg = 3;
484     } else if (argc > 2) {
485         myarg = 2;
486     }
487
488     if (myarg > asklist + 1)
489         cfg_opt = argv[1];
490
491     if (myarg >= argc) {
492         error(_("Must specify a parameter"));
493     }
494     parmname = argv[myarg];
495
496     /* do the config_init() now, although the result isn't checked until the end,
497      * when we try to look up config parameters */
498     cfg_ok = config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, cfg_opt);
499     if (cfg_ok) apply_config_overwrites(cfg_ovr);
500
501     safe_cd(); /* call this *after* config_init() */
502
503     /* Note that we dont use check_running_as(..) here, because we may not have a configuration
504      * (e.g., when we're examining build parameters).  If folks run this as the wrong user, that's
505      * their own problem. */
506
507     /*
508      * Fill in the build values that need runtime help.
509      */
510     build_info[0].value = stralloc(version());
511 #if defined(AMANDA_DEBUG_DAYS)
512     i = AMANDA_DEBUG_DAYS;
513 #else
514     i = -1;
515 #endif
516     g_snprintf(number, SIZEOF(number), "%ld", (long)i);
517     build_info[1].value = stralloc(number);
518 #if defined(KRB4_SECURITY)
519     i = TICKET_LIFETIME;
520 #else
521     i = -1;
522 #endif
523     g_snprintf(number, SIZEOF(number), "%ld", (long)i);
524     build_info[2].value = stralloc(number);
525
526 #undef p
527 #define p       "build."
528
529     if(strncmp(parmname, p, SIZEOF(p) - 1) == 0) {
530         char *s;
531         char *t;
532
533         t = stralloc(parmname + SIZEOF(p) - 1);
534         for(i = 0; (s = build_info[i].symbol) != NULL; i++) {
535             if(strcasecmp(s, t) == 0) {
536                 break;
537             }
538         }
539         if(s == NULL) {
540             result = NULL;
541         } else {
542             result = build_info[i].value;
543             result = stralloc(result ? result : "");
544         }
545
546 #undef p
547 #define p       "dbopen."
548
549     } else if(strncmp(parmname, p, SIZEOF(p) - 1) == 0) {
550         char *pname;
551         char *dbname;
552
553         if((pname = strrchr(parmname + SIZEOF(p) - 1, '/')) == NULL) {
554             pname = parmname + SIZEOF(p) - 1;
555         } else {
556             pname++;
557         }
558         set_pname(pname);
559         dbopen(DBG_SUBDIR_SERVER);
560         if((dbname = dbfn()) == NULL) {
561             result = stralloc("/dev/null");
562         } else {
563             result = stralloc(dbname);
564         }
565         /*
566          * Note that we deliberately do *not* call dbclose to prevent
567          * the end line from being added to the file.
568          */
569
570 #undef p
571 #define p       "dbclose."
572
573     } else if(strncmp(parmname, p, SIZEOF(p) - 1) == 0) {
574         char *t;
575         char *pname;
576         char *dbname;
577
578         t = stralloc(parmname + SIZEOF(p) - 1);
579         if((dbname = strchr(t, ':')) == NULL) {
580             error(_("cannot parse %s"), parmname);
581             /*NOTREACHED*/
582         }
583         *dbname++ = '\0';
584         if((pname = strrchr(t, '/')) == NULL) {
585             pname = t;
586         } else {
587             pname++;
588         }
589         fflush(stderr);
590         set_pname(pname);
591         dbreopen(dbname, NULL);
592         dbclose();
593         result = stralloc(dbname);
594         amfree(t);
595
596     } else {
597         /* *now* we check the result of config_init */
598         if (!cfg_ok) {
599             if (cfg_opt) {
600                 error(_("errors processing conf file \"%s\""), cfg_opt);
601                 /*NOTREACHED*/
602             } else {
603                 error(_("errors processing conf file in current directory."));
604                 /*NOTREACHED*/
605             }
606         }
607
608         dbrename(config_name, DBG_SUBDIR_SERVER);
609         if (asklist) {
610             GSList *list = getconf_list(parmname);
611             GSList *iter;
612             result = stralloc("");
613
614             for (iter = list; iter != NULL; iter = iter->next) {
615                 result = newvstralloc(result, result, iter->data, "\n", NULL);
616             }
617
618             g_slist_free(list);
619         } else {
620             val_t *val = getconf_byname(parmname);
621             if (val) {
622                 char **dispstrs = val_t_display_strs(val, FALSE);
623                 result = g_strjoinv("\n", dispstrs);
624                 g_strfreev(dispstrs);
625             }
626         }
627     }
628
629     if (result == NULL) {
630         g_fprintf(stderr, _("%s: no such parameter \"%s\"\n"),
631                 get_pname(), parmname);
632         fflush(stderr);
633     } else {
634         if (asklist)
635             fputs(result, stdout); /* don't add a '\n' */
636         else
637             puts(result); /* add a '\n' */
638     }
639
640     amfree(result);
641     for(i = 0; i < 3; i++) {
642         amfree(build_info[i].value);
643     }
644
645     return 0;
646 }