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