c645ebdf3a812c42aad96f5c60fded247395a7c0
[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.18 2006/01/14 04:37:19 paddy_s 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 P((int argc, char **argv));
38
39 static struct build_info {
40     char *symbol;
41     char *value;
42 } build_info[] = {
43     { "VERSION",                        "" },   /* must be [0] */
44     { "AMANDA_DEBUG_DAYS",              "" },   /* must be [1] */
45     { "TICKET_LIFETIME",                "" },   /* must be [2] */
46
47     { "bindir",                         bindir },
48     { "sbindir",                        sbindir },
49     { "libexecdir",                     libexecdir },
50     { "mandir",                         mandir },
51     { "AMANDA_TMPDIR",                  AMANDA_TMPDIR },
52     { "CONFIG_DIR",                     CONFIG_DIR },
53     { "MAILER",                         MAILER },
54     { "DEFAULT_SERVER",                 DEFAULT_SERVER },
55     { "DEFAULT_CONFIG",                 DEFAULT_CONFIG },
56     { "DEFAULT_TAPE_SERVER",            DEFAULT_TAPE_SERVER },
57     { "DEFAULT_TAPE_DEVICE",            DEFAULT_TAPE_SERVER },
58     { "CLIENT_LOGIN",                   CLIENT_LOGIN },
59
60     { "BUILT_DATE",
61 #if defined(BUILT_DATE)
62         BUILT_DATE
63 #endif
64     },
65     { "BUILT_MACH",
66 #if defined(BUILT_MACH)
67         BUILT_MACH
68 #endif
69     },
70     { "CC",
71 #if defined(CC)
72         CC
73 #endif
74     },
75
76     { "AMANDA_DBGDIR",
77 #if defined(AMANDA_DBGDIR)
78         AMANDA_DBGDIR
79 #endif
80     },
81     { "DEV_PREFIX",
82 #if defined(DEV_PREFIX)
83         DEV_PREFIX
84 #endif
85     },
86     { "RDEV_PREFIX",
87 #if defined(RDEV_PREFIX)
88         RDEV_PREFIX },
89 #endif
90     { "DUMP",
91 #if defined(DUMP)
92         DUMP
93 #endif
94     },
95     { "RESTORE",
96 #if defined(DUMP)
97         RESTORE
98 #endif
99     },
100     { "VDUMP",
101 #if defined(VDUMP)
102         VDUMP
103 #endif
104     },
105     { "VRESTORE",
106 #if defined(VDUMP)
107         VRESTORE
108 #endif
109     },
110     { "XFSDUMP",
111 #if defined(XFSDUMP)
112         XFSDUMP
113 #endif
114     },
115     { "XFSRESTORE",
116 #if defined(XFSDUMP)
117         XFSRESTORE
118 #endif
119     },
120     { "VXDUMP",
121 #if defined(VXDUMP)
122         VXDUMP
123 #endif
124     },
125     { "VXRESTORE",
126 #if defined(VXDUMP)
127         VXRESTORE
128 #endif
129     },
130     { "SAMBA_CLIENT",
131 #if defined(SAMBA_CLIENT)
132         SAMBA_CLIENT
133 #endif
134     },
135     { "GNUTAR",
136 #if defined(GNUTAR)
137         GNUTAR
138 #endif
139     },
140     { "COMPRESS_PATH",
141 #if defined(COMPRESS_PATH)
142         COMPRESS_PATH
143 #endif
144     },
145     { "UNCOMPRESS_PATH",
146 #if defined(UNCOMPRESS_PATH)
147         UNCOMPRESS_PATH
148 #endif
149     },
150     { "listed_incr_dir",
151 #if defined(GNUTAR_LISTED_INCREMENTAL_DIR)
152         GNUTAR_LISTED_INCREMENTAL_DIR
153 #endif
154     },
155     { "GNUTAR_LISTED_INCREMENTAL_DIR",
156 #if defined(GNUTAR_LISTED_INCREMENTAL_DIR)
157         GNUTAR_LISTED_INCREMENTAL_DIR
158 #endif
159     },
160
161     { "AIX_BACKUP",
162 #if defined(AIX_BACKUP)
163         "1"
164 #endif
165     },
166     { "AIX_TAPEIO",
167 #if defined(AIX_TAPEIO)
168         "1"
169 #endif
170     },
171     { "DUMP_RETURNS_1",
172 #if defined(DUMP_RETURNS_1)
173         "1"
174 #endif
175     },
176
177     { "LOCKING",
178 #if defined(USE_POSIX_FCNTL)
179         "POSIX_FCNTL"
180 #elif defined(USE_FLOCK)
181         "FLOCK"
182 #elif defined(USE_LOCKF)
183         "LOCKF"
184 #elif defined(USE_LNLOCK)
185         "LNLOCK"
186 #else
187         "NONE"
188 #endif
189     },
190
191     { "STATFS_BSD",
192 #if defined(STATFS_BSD)
193         "1"
194 #endif
195     },
196     { "STATFS_OSF1",
197 #if defined(STATFS_OSF1)
198         "1"
199 #endif
200     },
201     { "STATFS_ULTRIX",
202 #if defined(STATFS_ULTRIX)
203         "1"
204 #endif
205     },
206     { "ASSERTIONS",
207 #if defined(ASSERTIONS)
208         "1"
209 #endif
210     },
211     { "DEBUG_CODE",
212 #if defined(DEBUG_CODE)
213         "1"
214 #endif
215     },
216     { "BSD_SECURITY",
217 #if defined(BSD_SECURITY)
218         "1"
219 #endif
220     },
221     { "USE_AMANDAHOSTS",
222 #if defined(USE_AMANDAHOSTS)
223         "1"
224 #endif
225     },
226     { "USE_RUNDUMP",
227 #if defined(USE_RUNDUMP)
228         "1"
229 #endif
230     },
231     { "FORCE_USERID",
232 #if defined(FORCE_USERID)
233         "1"
234 #endif
235     },
236     { "USE_VERSION_SUFFIXES",
237 #if defined(USE_VERSION_SUFFIXES)
238         "1"
239 #endif
240     },
241     { "HAVE_GZIP",
242 #if defined(HAVE_GZIP)
243         "1"
244 #endif
245     },
246
247     { "KRB4_SECURITY",
248 #if defined(KRB4_SECURITY)
249         "1"
250 #endif
251     },
252     { "SERVER_HOST_PRINCIPLE",
253 #if defined(KRB4_SECURITY)
254         SERVER_HOST_PRINCIPLE
255 #endif
256     },
257     { "SERVER_HOST_INSTANCE",
258 #if defined(KRB4_SECURITY)
259         SERVER_HOST_INSTANCE
260 #endif
261     },
262     { "SERVER_HOST_KEY_FILE",
263 #if defined(KRB4_SECURITY)
264         SERVER_HOST_KEY_FILE
265 #endif
266     },
267     { "CLIENT_HOST_PRINCIPLE",
268 #if defined(KRB4_SECURITY)
269         CLIENT_HOST_PRINCIPLE
270 #endif
271     },
272     { "CLIENT_HOST_INSTANCE",
273 #if defined(KRB4_SECURITY)
274         CLIENT_HOST_INSTANCE
275 #endif
276     },
277     { "CLIENT_HOST_KEY_FILE",
278 #if defined(KRB4_SECURITY)
279         CLIENT_HOST_KEY_FILE
280 #endif
281     },
282
283     { "COMPRESS_SUFFIX",
284 #if defined(COMPRESS_SUFFIX)
285         COMPRESS_SUFFIX
286 #endif
287     },
288     { "COMPRESS_FAST_OPT",
289 #if defined(COMPRESS_FAST_OPT)
290         COMPRESS_FAST_OPT
291 #endif
292     },
293     { "COMPRESS_BEST_OPT",
294 #if defined(COMPRESS_BEST_OPT)
295         COMPRESS_BEST_OPT
296 #endif
297     },
298     { "UNCOMPRESS_OPT",
299 #if defined(UNCOMPRESS_OPT)
300         UNCOMPRESS_OPT
301 #endif
302     },
303
304     { NULL,                     NULL }
305 };
306
307 int main(argc, argv)
308 int argc;
309 char **argv;
310 {
311     char *result;
312     unsigned long malloc_hist_1, malloc_size_1;
313     unsigned long malloc_hist_2, malloc_size_2;
314     char *pgm;
315     char *conffile;
316     char *parmname;
317     int i;
318     char number[NUM_STR_SIZE];
319
320     safe_fd(-1, 0);
321
322     malloc_size_1 = malloc_inuse(&malloc_hist_1);
323
324     if((pgm = strrchr(argv[0], '/')) == NULL) {
325         pgm = argv[0];
326     } else {
327         pgm++;
328     }
329     set_pname(pgm);
330
331     /* Don't die when child closes pipe */
332     signal(SIGPIPE, SIG_IGN);
333
334     if(argc < 2) {
335         fprintf(stderr, "Usage: %s [config] <parmname>\n", pgm);
336         exit(1);
337     }
338
339     if (argc > 2) {
340         config_name = stralloc(argv[1]);
341         config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
342         parmname = argv[2];
343     } else {
344         char my_cwd[STR_SIZE];
345
346         if (getcwd(my_cwd, sizeof(my_cwd)) == NULL) {
347             error("cannot determine current working directory");
348         }
349         config_dir = stralloc2(my_cwd, "/");
350         if ((config_name = strrchr(my_cwd, '/')) != NULL) {
351             config_name = stralloc(config_name + 1);
352         }
353         parmname = argv[1];
354     }
355
356     safe_cd();
357
358     /*
359      * Fill in the build values that need runtime help.
360      */
361     build_info[0].value = stralloc(version());
362 #if defined(AMANDA_DEBUG_DAYS)
363     i = AMANDA_DEBUG_DAYS;
364 #else
365     i = -1;
366 #endif
367     snprintf(number, sizeof(number), "%ld", (long)i);
368     build_info[1].value = stralloc(number);
369 #if defined(KRB4_SECURITY)
370     i = TICKET_LIFETIME;
371 #else
372     i = -1;
373 #endif
374     snprintf(number, sizeof(number), "%ld", (long)i);
375     build_info[2].value = stralloc(number);
376
377 #undef p
378 #define p       "build."
379
380     if(strncmp(parmname, p, sizeof(p) - 1) == 0) {
381         char *s;
382         char *t;
383
384         t = stralloc(parmname + sizeof(p) - 1);
385         for(i = 0; (s = build_info[i].symbol) != NULL; i++) {
386             if(strcasecmp(s, t) == 0) {
387                 break;
388             }
389         }
390         if(s == NULL) {
391             result = NULL;
392         } else {
393             result = build_info[i].value;
394             result = stralloc(result ? result : "");
395         }
396
397 #undef p
398 #define p       "dbopen."
399
400     } else if(strncmp(parmname, p, sizeof(p) - 1) == 0) {
401         char *pname;
402         char *dbname;
403
404         if((pname = strrchr(parmname + sizeof(p) - 1, '/')) == NULL) {
405             pname = parmname + sizeof(p) - 1;
406         } else {
407             pname++;
408         }
409         set_pname(pname);
410         dbopen();
411         if((dbname = dbfn()) == NULL) {
412             result = stralloc("/dev/null");
413         } else {
414             result = stralloc(dbname);
415         }
416         /*
417          * Note that we deliberately do *not* call dbclose to prevent
418          * the end line from being added to the file.
419          */
420
421 #undef p
422 #define p       "dbclose."
423
424     } else if(strncmp(parmname, p, sizeof(p) - 1) == 0) {
425         char *t;
426         char *pname;
427         char *dbname;
428
429         t = stralloc(parmname + sizeof(p) - 1);
430         if((dbname = strchr(t, ':')) == NULL) {
431             error("cannot parse %s", parmname);
432         }
433         *dbname++ = '\0';
434         if((pname = strrchr(t, '/')) == NULL) {
435             pname = t;
436         } else {
437             pname++;
438         }
439         fflush(stderr);
440         set_pname(pname);
441         dbreopen(dbname, NULL);
442         dbclose();
443         result = stralloc(dbname);
444         amfree(t);
445
446     } else {
447         conffile = stralloc2(config_dir, CONFFILE_NAME);
448         if(read_conffile(conffile)) {
449             error("errors processing config file \"%s\"", conffile);
450         }
451         amfree(conffile);
452         result = getconf_byname(parmname);
453     }
454     if(result == NULL) {
455         result = stralloc("BUGGY");
456         fprintf(stderr, "%s: no such parameter \"%s\"\n",
457                 get_pname(), parmname);
458         fflush(stderr);
459     }
460
461     puts(result);
462
463     amfree(result);
464     amfree(config_dir);
465     amfree(config_name);
466     for(i = 0; i < 3; i++) {
467         amfree(build_info[i].value);
468     }
469
470     malloc_size_2 = malloc_inuse(&malloc_hist_2);
471
472     if(malloc_size_1 != malloc_size_2) {
473         malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);
474     }
475
476     return 0;
477 }