Add version consistency checks between applications, libfreeradius-radius, libfreerad...
[freeradius.git] / src / main / radconf2xml.c
1 /*
2  * radconf2xml.c        Converts radiusd.conf to XML.
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2008   The FreeRADIUS server project
21  * Copyright 2008   Alan DeKok <aland@deployingradius.com>
22  */
23
24 RCSID("$Id$")
25
26 #include <freeradius-devel/radiusd.h>
27
28 #ifdef HAVE_GETOPT_H
29 #include <getopt.h>
30 #endif
31
32 /*
33  *      For configuration file stuff.
34  */
35 char const *raddb_dir = RADDBDIR;
36 char const *progname = "radconf2xml";
37
38 /*
39  *      The rest of this is because the conffile.c, etc. assume
40  *      they're running inside of the server.  And we don't (yet)
41  *      have a "libfreeradius-server", or "libfreeradius-util".
42  */
43 log_debug_t debug_flag = 0;
44 struct main_config_t mainconfig;
45 char *request_log_file = NULL;
46 char *debug_log_file = NULL;
47
48 #include <sys/wait.h>
49 pid_t rad_fork(void)
50 {
51         return fork();
52 }
53
54 #ifdef HAVE_PTHREAD_H
55 pid_t rad_waitpid(pid_t pid, int *status)
56 {
57         return waitpid(pid, status, 0);
58 }
59 #endif
60
61 bool check_config = false;
62
63 static int usage(void)
64 {
65         printf("Usage: %s [ -d raddb_dir ] [ -o output_file ] [ -n name ]\n", progname);
66         printf("  -d raddb_dir    Configuration files are in \"raddbdir/*\".\n");
67         printf("  -n name        Read raddb/name.conf instead of raddb/radiusd.conf\n");
68         printf("  -o output_file  File where XML output will be written.\n");
69
70         exit(1);
71 }
72
73 int main(int argc, char **argv)
74 {
75         int argval;
76         CONF_SECTION *cs;
77         char const *file = NULL;
78         char const *name = "radiusd";
79         FILE *fp;
80         char buffer[2048];
81
82         if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL)
83                 progname = argv[0];
84         else
85                 progname++;
86
87         while ((argval = getopt(argc, argv, "d:ho:n:")) != EOF) {
88                 switch(argval) {
89                 case 'd':
90                         if (file) {
91                                 fprintf(stderr, "%s: -d and -f cannot be used together.\n", progname);
92                                 exit(1);
93                         }
94                         raddb_dir = optarg;
95                         break;
96
97                 default:
98                 case 'h':
99                         usage();
100                         break;
101
102                 case 'n':
103                         name = optarg;
104                         break;
105
106                 case 'o':
107                         file = optarg;
108                         break;
109                 }
110         }
111
112         /*
113          *      Mismatch between the binary and the libraries it depends on
114          */
115         if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) {
116                 fr_perror("radconf2xml");
117                 return 1;
118         }
119
120         snprintf(buffer, sizeof(buffer), "%s/%s.conf", raddb_dir, name);
121         cs = cf_file_read(buffer);
122         if (!cs) {
123                 fprintf(stderr, "%s: Errors reading or parsing %s\n",
124                         progname, buffer);
125                 exit(1);
126         }
127
128         if (!file || (strcmp(file, "-") == 0)) {
129                 fp = stdout;
130                 file = NULL;
131         } else {
132                 fp = fopen(file, "w");
133                 if (!fp) {
134                         fprintf(stderr, "%s: Failed openng %s: %s\n",
135                                 progname, file, strerror(errno));
136                         exit(1);
137                 }
138         }
139
140         if (!cf_section2xml(fp, cs)) {
141                 if (file) unlink(file);
142                 return 1;
143         }
144
145         return 0;
146 }