Imported Upstream version 2.4.4p3
[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 2003/12/16 22:36:45 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 #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
61     { "BUILT_DATE",
62 #if defined(BUILT_DATE)
63         BUILT_DATE
64 #endif
65     },
66     { "BUILT_MACH",
67 #if defined(BUILT_MACH)
68         BUILT_MACH
69 #endif
70     },
71     { "CC",
72 #if defined(CC)
73         CC
74 #endif
75     },
76
77     { "AMANDA_DBGDIR",
78 #if defined(AMANDA_DBGDIR)
79         AMANDA_DBGDIR
80 #endif
81     },
82     { "DEV_PREFIX",
83 #if defined(DEV_PREFIX)
84         DEV_PREFIX
85 #endif
86     },
87     { "RDEV_PREFIX",
88 #if defined(RDEV_PREFIX)
89         RDEV_PREFIX },
90 #endif
91     { "DUMP",
92 #if defined(DUMP)
93         DUMP
94 #endif
95     },
96     { "RESTORE",
97 #if defined(DUMP)
98         RESTORE
99 #endif
100     },
101     { "VDUMP",
102 #if defined(VDUMP)
103         VDUMP
104 #endif
105     },
106     { "VRESTORE",
107 #if defined(VDUMP)
108         VRESTORE
109 #endif
110     },
111     { "XFSDUMP",
112 #if defined(XFSDUMP)
113         XFSDUMP
114 #endif
115     },
116     { "XFSRESTORE",
117 #if defined(XFSDUMP)
118         XFSRESTORE
119 #endif
120     },
121     { "VXDUMP",
122 #if defined(VXDUMP)
123         VXDUMP
124 #endif
125     },
126     { "VXRESTORE",
127 #if defined(VXDUMP)
128         VXRESTORE
129 #endif
130     },
131     { "SAMBA_CLIENT",
132 #if defined(SAMBA_CLIENT)
133         SAMBA_CLIENT
134 #endif
135     },
136     { "GNUTAR",
137 #if defined(GNUTAR)
138         GNUTAR
139 #endif
140     },
141     { "COMPRESS_PATH",
142 #if defined(COMPRESS_PATH)
143         COMPRESS_PATH
144 #endif
145     },
146     { "UNCOMPRESS_PATH",
147 #if defined(UNCOMPRESS_PATH)
148         UNCOMPRESS_PATH
149 #endif
150     },
151     { "listed_incr_dir",
152 #if defined(GNUTAR_LISTED_INCREMENTAL_DIR)
153         GNUTAR_LISTED_INCREMENTAL_DIR
154 #endif
155     },
156     { "GNUTAR_LISTED_INCREMENTAL_DIR",
157 #if defined(GNUTAR_LISTED_INCREMENTAL_DIR)
158         GNUTAR_LISTED_INCREMENTAL_DIR
159 #endif
160     },
161
162     { "AIX_BACKUP",
163 #if defined(AIX_BACKUP)
164         "1"
165 #endif
166     },
167     { "AIX_TAPEIO",
168 #if defined(AIX_TAPEIO)
169         "1"
170 #endif
171     },
172     { "DUMP_RETURNS_1",
173 #if defined(DUMP_RETURNS_1)
174         "1"
175 #endif
176     },
177
178     { "LOCKING",
179 #if defined(USE_POSIX_FCNTL)
180         "POSIX_FCNTL"
181 #elif defined(USE_FLOCK)
182         "FLOCK"
183 #elif defined(USE_LOCKF)
184         "LOCKF"
185 #elif defined(USE_LNLOCK)
186         "LNLOCK"
187 #else
188         "NONE"
189 #endif
190     },
191
192     { "STATFS_BSD",
193 #if defined(STATFS_BSD)
194         "1"
195 #endif
196     },
197     { "STATFS_OSF1",
198 #if defined(STATFS_OSF1)
199         "1"
200 #endif
201     },
202     { "STATFS_ULTRIX",
203 #if defined(STATFS_ULTRIX)
204         "1"
205 #endif
206     },
207     { "ASSERTIONS",
208 #if defined(ASSERTIONS)
209         "1"
210 #endif
211     },
212     { "DEBUG_CODE",
213 #if defined(DEBUG_CODE)
214         "1"
215 #endif
216     },
217     { "BSD_SECURITY",
218 #if defined(BSD_SECURITY)
219         "1"
220 #endif
221     },
222     { "USE_AMANDAHOSTS",
223 #if defined(USE_AMANDAHOSTS)
224         "1"
225 #endif
226     },
227     { "USE_RUNDUMP",
228 #if defined(USE_RUNDUMP)
229         "1"
230 #endif
231     },
232     { "FORCE_USERID",
233 #if defined(FORCE_USERID)
234         "1"
235 #endif
236     },
237     { "USE_VERSION_SUFFIXES",
238 #if defined(USE_VERSION_SUFFIXES)
239         "1"
240 #endif
241     },
242     { "HAVE_GZIP",
243 #if defined(HAVE_GZIP)
244         "1"
245 #endif
246     },
247
248     { "KRB4_SECURITY",
249 #if defined(KRB4_SECURITY)
250         "1"
251 #endif
252     },
253     { "SERVER_HOST_PRINCIPLE",
254 #if defined(KRB4_SECURITY)
255         SERVER_HOST_PRINCIPLE
256 #endif
257     },
258     { "SERVER_HOST_INSTANCE",
259 #if defined(KRB4_SECURITY)
260         SERVER_HOST_INSTANCE
261 #endif
262     },
263     { "SERVER_HOST_KEY_FILE",
264 #if defined(KRB4_SECURITY)
265         SERVER_HOST_KEY_FILE
266 #endif
267     },
268     { "CLIENT_HOST_PRINCIPLE",
269 #if defined(KRB4_SECURITY)
270         CLIENT_HOST_PRINCIPLE
271 #endif
272     },
273     { "CLIENT_HOST_INSTANCE",
274 #if defined(KRB4_SECURITY)
275         CLIENT_HOST_INSTANCE
276 #endif
277     },
278     { "CLIENT_HOST_KEY_FILE",
279 #if defined(KRB4_SECURITY)
280         CLIENT_HOST_KEY_FILE
281 #endif
282     },
283
284     { "COMPRESS_SUFFIX",
285 #if defined(COMPRESS_SUFFIX)
286         COMPRESS_SUFFIX
287 #endif
288     },
289     { "COMPRESS_FAST_OPT",
290 #if defined(COMPRESS_FAST_OPT)
291         COMPRESS_FAST_OPT
292 #endif
293     },
294     { "COMPRESS_BEST_OPT",
295 #if defined(COMPRESS_BEST_OPT)
296         COMPRESS_BEST_OPT
297 #endif
298     },
299     { "UNCOMPRESS_OPT",
300 #if defined(UNCOMPRESS_OPT)
301         UNCOMPRESS_OPT
302 #endif
303     },
304
305     { NULL,                     NULL }
306 };
307
308 int main(argc, argv)
309 int argc;
310 char **argv;
311 {
312     char *result;
313     int fd;
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     for(fd = 3; fd < FD_SETSIZE; fd++) {
323         /*
324          * Make sure nobody spoofs us with a lot of extra open files
325          * that would cause an open we do to get a very high file
326          * descriptor, which in turn might be used as an index into
327          * an array (e.g. an fd_set).
328          */
329         close(fd);
330     }
331
332     malloc_size_1 = malloc_inuse(&malloc_hist_1);
333
334     if((pgm = strrchr(argv[0], '/')) == NULL) {
335         pgm = argv[0];
336     } else {
337         pgm++;
338     }
339     set_pname(pgm);
340
341     if(argc < 2) {
342         fprintf(stderr, "Usage: %s [config] <parmname>\n", pgm);
343         exit(1);
344     }
345
346     if (argc > 2) {
347         config_name = stralloc(argv[1]);
348         config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
349         parmname = argv[2];
350     } else {
351         char my_cwd[STR_SIZE];
352
353         if (getcwd(my_cwd, sizeof(my_cwd)) == NULL) {
354             error("cannot determine current working directory");
355         }
356         config_dir = stralloc2(my_cwd, "/");
357         if ((config_name = strrchr(my_cwd, '/')) != NULL) {
358             config_name = stralloc(config_name + 1);
359         }
360         parmname = argv[1];
361     }
362
363     safe_cd();
364
365     conffile = stralloc2(config_dir, CONFFILE_NAME);
366     if(read_conffile(conffile)) {
367         error("errors processing config file \"%s\"", conffile);
368     }
369     amfree(conffile);
370
371     /*
372      * Fill in the build values that need runtime help.
373      */
374     build_info[0].value = stralloc(version());
375 #if defined(AMANDA_DEBUG_DAYS)
376     i = AMANDA_DEBUG_DAYS;
377 #else
378     i = -1;
379 #endif
380     ap_snprintf(number, sizeof(number), "%ld", (long)i);
381     build_info[1].value = stralloc(number);
382 #if defined(KRB4_SECURITY)
383     i = TICKET_LIFETIME;
384 #else
385     i = -1;
386 #endif
387     ap_snprintf(number, sizeof(number), "%ld", (long)i);
388     build_info[2].value = stralloc(number);
389
390 #undef p
391 #define p       "build."
392
393     if(strncmp(parmname, p, sizeof(p) - 1) == 0) {
394         char *s;
395         char *t;
396
397         t = stralloc(parmname + sizeof(p) - 1);
398         for(i = 0; (s = build_info[i].symbol) != NULL; i++) {
399             if(strcasecmp(s, t) == 0) {
400                 break;
401             }
402         }
403         if(s == NULL) {
404             result = NULL;
405         } else {
406             result = build_info[i].value;
407             result = stralloc(result ? result : "");
408         }
409
410 #undef p
411 #define p       "dbopen."
412
413     } else if(strncmp(parmname, p, sizeof(p) - 1) == 0) {
414         char *pname;
415         char *dbname;
416
417         if((pname = strrchr(parmname + sizeof(p) - 1, '/')) == NULL) {
418             pname = parmname + sizeof(p) - 1;
419         } else {
420             pname++;
421         }
422         set_pname(pname);
423         dbopen();
424         if((dbname = dbfn()) == NULL) {
425             result = stralloc("/dev/null");
426         } else {
427             result = stralloc(dbname);
428         }
429
430 #undef p
431 #define p       "dbclose."
432
433     } else if(strncmp(parmname, p, sizeof(p) - 1) == 0) {
434         char *t;
435         char *pname;
436         char *dbname;
437         int old_fd2;
438         int new_fd2;
439
440         t = stralloc(parmname + sizeof(p) - 1);
441         if((dbname = strchr(t, ':')) == NULL) {
442             error("cannot parse %s", parmname);
443         }
444         *dbname++ = '\0';
445         if((pname = strrchr(t, '/')) == NULL) {
446             pname = t;
447         } else {
448             pname++;
449         }
450         fflush(stderr);
451         if((old_fd2 = dup(2)) < 0) {
452             error("cannot dup2 fd2");
453         }
454         close(2);
455         if((new_fd2 = open(dbname, O_RDWR|O_APPEND, 0600)) != 2) {
456             int save_errno = errno;
457             FILE *f;
458
459             if((f = fdopen(old_fd2, "a")) == NULL) {
460                 /* give up */
461                 return 3;
462             }
463             fprintf(f, "%s: cannot open %s: ", get_pname(), dbname);
464             if(new_fd2 < 0) {
465                 fputs(strerror(save_errno), f);
466             } else {
467                 fprintf(f, "got %d instead of 2", new_fd2);
468             }
469             fputc('\n', f);
470             fclose(f);
471         }
472         /*
473          * Because we have not called dbopen(), dbclose() will write the
474          * end line to stderr, which we just redirected to the file on
475          * the command line.
476          */
477         set_pname(pname);
478         dbclose();
479         result = stralloc(dbname);
480         amfree(t);
481
482     } else {
483         result = getconf_byname(parmname);
484     }
485     if(result == NULL) {
486         result = stralloc("BUGGY");
487         fprintf(stderr, "%s: no such parameter \"%s\"\n",
488                 get_pname(), parmname);
489         fflush(stderr);
490     }
491
492     puts(result);
493
494     amfree(result);
495     amfree(config_dir);
496     amfree(config_name);
497     for(i = 0; i < 3; i++) {
498         amfree(build_info[i].value);
499     }
500
501     malloc_size_2 = malloc_inuse(&malloc_hist_2);
502
503     if(malloc_size_1 != malloc_size_2) {
504         malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);
505     }
506
507     return 0;
508 }